Fix test for sections with different VMA<->LMA relationships so that it only applies...
[binutils-gdb.git] / bfd / format.c
blob6d95683acb566cc898185b2dda7ea396298ff458
1 /* Generic BFD support for file formats.
2 Copyright (C) 1990-2024 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"
50 /* IMPORT from targets.c. */
51 extern const size_t _bfd_target_vector_entries;
54 FUNCTION
55 bfd_check_format
57 SYNOPSIS
58 bool bfd_check_format (bfd *abfd, bfd_format format);
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>>).
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.
73 The function returns <<TRUE>> on success, otherwise <<FALSE>>
74 with one of the following error codes:
76 o <<bfd_error_invalid_operation>> -
77 if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
78 <<bfd_core>>.
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.
84 o <<file_not_recognised>> -
85 none of the backends recognised the file format.
87 o <<bfd_error_file_ambiguously_recognized>> -
88 more than one backend recognised the file format.
91 bool
92 bfd_check_format (bfd *abfd, bfd_format format)
94 return bfd_check_format_matches (abfd, format, NULL);
97 struct bfd_preserve
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;
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. */
126 static bool
127 bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve,
128 bfd_cleanup cleanup)
130 preserve->tdata = abfd->tdata.any;
131 preserve->arch_info = abfd->arch_info;
132 preserve->flags = abfd->flags;
133 preserve->iovec = abfd->iovec;
134 preserve->iostream = abfd->iostream;
135 preserve->sections = abfd->sections;
136 preserve->section_last = abfd->section_last;
137 preserve->section_count = abfd->section_count;
138 preserve->section_id = _bfd_section_id;
139 preserve->symcount = abfd->symcount;
140 preserve->read_only = abfd->read_only;
141 preserve->start_address = abfd->start_address;
142 preserve->section_htab = abfd->section_htab;
143 preserve->marker = bfd_alloc (abfd, 1);
144 preserve->build_id = abfd->build_id;
145 preserve->cleanup = cleanup;
146 if (preserve->marker == NULL)
147 return false;
149 return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
150 sizeof (struct section_hash_entry));
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. */
160 static void
161 io_reinit (bfd *abfd, struct bfd_preserve *preserve)
163 if (abfd->iovec != preserve->iovec)
165 /* Handle file backed to in-memory transition. bfd_cache_close
166 won't do anything unless abfd->iovec is the cache_iovec.
167 Don't be tempted to call iovec->bclose here. We don't want
168 to call memory_bclose, which would free the bim. The bim
169 must be kept if bfd_check_format_matches is going to decide
170 later that the PE format needing it is in fact the correct
171 target match. */
172 bfd_cache_close (abfd);
173 abfd->iovec = preserve->iovec;
174 abfd->iostream = preserve->iostream;
176 /* Handle in-memory to file backed transition. */
177 if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0
178 && (abfd->flags & BFD_IN_MEMORY) != 0
179 && (preserve->flags & BFD_CLOSED_BY_CACHE) == 0
180 && (preserve->flags & BFD_IN_MEMORY) == 0)
181 bfd_open_file (abfd);
183 abfd->flags = preserve->flags;
186 /* Clear out a subset of BFD state. */
188 static void
189 bfd_reinit (bfd *abfd, unsigned int section_id,
190 struct bfd_preserve *preserve, bfd_cleanup cleanup)
192 _bfd_section_id = section_id;
193 if (cleanup)
194 cleanup (abfd);
195 abfd->tdata.any = NULL;
196 abfd->arch_info = &bfd_default_arch_struct;
197 io_reinit (abfd, preserve);
198 abfd->symcount = 0;
199 abfd->read_only = 0;
200 abfd->start_address = 0;
201 abfd->build_id = NULL;
202 bfd_section_list_clear (abfd);
205 /* Restores bfd state saved by bfd_preserve_save. */
207 static bfd_cleanup
208 bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
210 bfd_hash_table_free (&abfd->section_htab);
212 abfd->tdata.any = preserve->tdata;
213 abfd->arch_info = preserve->arch_info;
214 io_reinit (abfd, preserve);
215 abfd->section_htab = preserve->section_htab;
216 abfd->sections = preserve->sections;
217 abfd->section_last = preserve->section_last;
218 abfd->section_count = preserve->section_count;
219 _bfd_section_id = preserve->section_id;
220 abfd->symcount = preserve->symcount;
221 abfd->read_only = preserve->read_only;
222 abfd->start_address = preserve->start_address;
223 abfd->build_id = preserve->build_id;
225 /* bfd_release frees all memory more recently bfd_alloc'd than
226 its arg, as well as its arg. */
227 bfd_release (abfd, preserve->marker);
228 preserve->marker = NULL;
229 return preserve->cleanup;
232 /* Called when the bfd state saved by bfd_preserve_save is no longer
233 needed. */
235 static void
236 bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
238 if (preserve->cleanup)
240 /* Run the cleanup, assuming that all it will need is the
241 tdata at the time the cleanup was returned. */
242 void *tdata = abfd->tdata.any;
243 abfd->tdata.any = preserve->tdata;
244 preserve->cleanup (abfd);
245 abfd->tdata.any = tdata;
247 /* It would be nice to be able to free more memory here, eg. old
248 tdata, but that's not possible since these blocks are sitting
249 inside bfd_alloc'd memory. The section hash is on a separate
250 objalloc. */
251 bfd_hash_table_free (&preserve->section_htab);
252 preserve->marker = NULL;
255 static void
256 print_warnmsg (struct per_xvec_message **list)
258 for (struct per_xvec_message *warn = *list; warn; warn = warn->next)
259 _bfd_error_handler ("%s", warn->message);
262 static void
263 clear_warnmsg (struct per_xvec_message **list)
265 struct per_xvec_message *warn = *list;
266 while (warn)
268 struct per_xvec_message *next = warn->next;
269 free (warn);
270 warn = next;
272 *list = NULL;
275 static void
276 null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
277 va_list ap ATTRIBUTE_UNUSED)
281 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
283 struct lto_section
285 int16_t major_version;
286 int16_t minor_version;
287 unsigned char slim_object;
289 /* Flags is a private field that is not defined publicly. */
290 uint16_t flags;
293 /* Set lto_type in ABFD. */
295 static void
296 bfd_set_lto_type (bfd *abfd ATTRIBUTE_UNUSED)
298 #if BFD_SUPPORTS_PLUGINS
299 if (abfd->format == bfd_object
300 && abfd->lto_type == lto_non_object
301 && (abfd->flags & (DYNAMIC | EXEC_P)) == 0)
303 asection *sec;
304 enum bfd_lto_object_type type = lto_non_ir_object;
305 struct lto_section lsection;
306 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
307 section. */
308 for (sec = abfd->sections; sec != NULL; sec = sec->next)
309 if (startswith (sec->name, ".gnu.lto_.lto.")
310 && bfd_get_section_contents (abfd, sec, &lsection, 0,
311 sizeof (struct lto_section)))
313 if (lsection.slim_object)
314 type = lto_slim_ir_object;
315 else
316 type = lto_fat_ir_object;
317 break;
320 abfd->lto_type = type;
322 #endif
326 FUNCTION
327 bfd_check_format_matches
329 SYNOPSIS
330 bool bfd_check_format_matches
331 (bfd *abfd, bfd_format format, char ***matching);
333 DESCRIPTION
334 Like <<bfd_check_format>>, except when it returns FALSE with
335 <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that
336 case, if @var{matching} is not NULL, it will be filled in with
337 a NULL-terminated list of the names of the formats that matched,
338 allocated with <<malloc>>.
339 Then the user may choose a format and try again.
341 When done with the list that @var{matching} points to, the caller
342 should free it.
345 bool
346 bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
348 extern const bfd_target binary_vec;
349 #if BFD_SUPPORTS_PLUGINS
350 extern const bfd_target plugin_vec;
351 #endif
352 const bfd_target * const *target;
353 const bfd_target **matching_vector = NULL;
354 const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
355 int match_count, best_count, best_match;
356 int ar_match_index;
357 unsigned int initial_section_id = _bfd_section_id;
358 struct bfd_preserve preserve, preserve_match;
359 bfd_cleanup cleanup = NULL;
360 bfd_error_handler_type orig_error_handler;
361 static int in_check_format;
363 if (matching != NULL)
364 *matching = NULL;
366 if (!bfd_read_p (abfd)
367 || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
369 bfd_set_error (bfd_error_invalid_operation);
370 return false;
373 if (abfd->format != bfd_unknown)
375 bfd_set_lto_type (abfd);
376 return abfd->format == format;
379 if (matching != NULL || *bfd_associated_vector != NULL)
381 size_t amt;
383 amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
384 matching_vector = (const bfd_target **) bfd_malloc (amt);
385 if (!matching_vector)
386 return false;
389 /* Presume the answer is yes. */
390 abfd->format = format;
391 save_targ = abfd->xvec;
393 /* Don't report errors on recursive calls checking the first element
394 of an archive. */
395 if (in_check_format)
396 orig_error_handler = bfd_set_error_handler (null_error_handler);
397 else
398 orig_error_handler = _bfd_set_error_handler_caching (abfd);
399 ++in_check_format;
401 preserve_match.marker = NULL;
402 if (!bfd_preserve_save (abfd, &preserve, NULL))
403 goto err_ret;
405 /* If the target type was explicitly specified, just check that target. */
406 if (!abfd->target_defaulted)
408 if (bfd_seek (abfd, 0, SEEK_SET) != 0) /* rewind! */
409 goto err_ret;
411 cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
413 if (cleanup)
414 goto ok_ret;
416 /* For a long time the code has dropped through to check all
417 targets if the specified target was wrong. I don't know why,
418 and I'm reluctant to change it. However, in the case of an
419 archive, it can cause problems. If the specified target does
420 not permit archives (e.g., the binary target), then we should
421 not allow some other target to recognize it as an archive, but
422 should instead allow the specified target to recognize it as an
423 object. When I first made this change, it broke the PE target,
424 because the specified pei-i386 target did not recognize the
425 actual pe-i386 archive. Since there may be other problems of
426 this sort, I changed this test to check only for the binary
427 target. */
428 if (format == bfd_archive && save_targ == &binary_vec)
429 goto err_unrecog;
432 /* Since the target type was defaulted, check them all in the hope
433 that one will be uniquely recognized. */
434 right_targ = NULL;
435 ar_right_targ = NULL;
436 match_targ = NULL;
437 best_match = 256;
438 best_count = 0;
439 match_count = 0;
440 ar_match_index = _bfd_target_vector_entries;
442 for (target = bfd_target_vector; *target != NULL; target++)
444 void **high_water;
446 /* The binary target matches anything, so don't return it when
447 searching. Don't match the plugin target if we have another
448 alternative since we want to properly set the input format
449 before allowing a plugin to claim the file. Also, don't
450 check the default target twice. */
451 if (*target == &binary_vec
452 #if BFD_SUPPORTS_PLUGINS
453 || (match_count != 0 && *target == &plugin_vec)
454 #endif
455 || (!abfd->target_defaulted && *target == save_targ))
456 continue;
458 /* If we already tried a match, the bfd is modified and may
459 have sections attached, which will confuse the next
460 _bfd_check_format call. */
461 bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
462 /* Free bfd_alloc memory too. If we have matched and preserved
463 a target then the high water mark is that much higher. */
464 if (preserve_match.marker)
465 high_water = &preserve_match.marker;
466 else
467 high_water = &preserve.marker;
468 bfd_release (abfd, *high_water);
469 *high_water = bfd_alloc (abfd, 1);
471 /* Change BFD's target temporarily. */
472 abfd->xvec = *target;
474 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
475 goto err_ret;
477 cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
478 if (cleanup)
480 int match_priority = abfd->xvec->match_priority;
481 #if BFD_SUPPORTS_PLUGINS
482 /* If this object can be handled by a plugin, give that the
483 lowest priority; objects both handled by a plugin and
484 with an underlying object format will be claimed
485 separately by the plugin. */
486 if (*target == &plugin_vec)
487 match_priority = (*target)->match_priority;
488 #endif
490 if (abfd->format != bfd_archive
491 || (bfd_has_map (abfd)
492 && bfd_get_error () != bfd_error_wrong_object_format))
494 /* If this is the default target, accept it, even if
495 other targets might match. People who want those
496 other targets have to set the GNUTARGET variable. */
497 if (abfd->xvec == bfd_default_vector[0])
498 goto ok_ret;
500 if (matching_vector)
501 matching_vector[match_count] = abfd->xvec;
502 match_count++;
504 if (match_priority < best_match)
506 best_match = match_priority;
507 best_count = 0;
509 if (match_priority <= best_match)
511 /* This format checks out as ok! */
512 right_targ = abfd->xvec;
513 best_count++;
516 else
518 /* An archive with no armap or objects of the wrong
519 type. We want this target to match if we get no
520 better matches. */
521 if (ar_right_targ != bfd_default_vector[0])
522 ar_right_targ = *target;
523 if (matching_vector)
524 matching_vector[ar_match_index] = *target;
525 ar_match_index++;
528 if (preserve_match.marker == NULL)
530 match_targ = abfd->xvec;
531 if (!bfd_preserve_save (abfd, &preserve_match, cleanup))
532 goto err_ret;
533 cleanup = NULL;
538 if (best_count == 1)
539 match_count = 1;
541 if (match_count == 0)
543 /* Try partial matches. */
544 right_targ = ar_right_targ;
546 if (right_targ == bfd_default_vector[0])
548 match_count = 1;
550 else
552 match_count = ar_match_index - _bfd_target_vector_entries;
554 if (matching_vector && match_count > 1)
555 memcpy (matching_vector,
556 matching_vector + _bfd_target_vector_entries,
557 sizeof (*matching_vector) * match_count);
561 /* We have more than one equally good match. If any of the best
562 matches is a target in config.bfd targ_defvec or targ_selvecs,
563 choose it. */
564 if (match_count > 1)
566 const bfd_target * const *assoc = bfd_associated_vector;
568 while ((right_targ = *assoc++) != NULL)
570 int i = match_count;
572 while (--i >= 0)
573 if (matching_vector[i] == right_targ
574 && right_targ->match_priority <= best_match)
575 break;
577 if (i >= 0)
579 match_count = 1;
580 break;
585 /* We still have more than one equally good match, and at least some
586 of the targets support match priority. Choose the first of the
587 best matches. */
588 if (matching_vector && match_count > 1 && best_count != match_count)
590 int i;
592 for (i = 0; i < match_count; i++)
594 right_targ = matching_vector[i];
595 if (right_targ->match_priority <= best_match)
596 break;
598 match_count = 1;
601 /* There is way too much undoing of half-known state here. We
602 really shouldn't iterate on live bfd's. Note that saving the
603 whole bfd and restoring it would be even worse; the first thing
604 you notice is that the cached bfd file position gets out of sync. */
605 if (preserve_match.marker != NULL)
606 cleanup = bfd_preserve_restore (abfd, &preserve_match);
608 if (match_count == 1)
610 abfd->xvec = right_targ;
611 /* If we come out of the loop knowing that the last target that
612 matched is the one we want, then ABFD should still be in a usable
613 state (except possibly for XVEC). This is not just an
614 optimisation. In the case of plugins a match against the
615 plugin target can result in the bfd being changed such that
616 it no longer matches the plugin target, nor will it match
617 RIGHT_TARG again. */
618 if (match_targ != right_targ)
620 bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
621 bfd_release (abfd, preserve.marker);
622 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
623 goto err_ret;
624 cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
625 BFD_ASSERT (cleanup != NULL);
628 ok_ret:
629 /* If the file was opened for update, then `output_has_begun'
630 some time ago when the file was created. Do not recompute
631 sections sizes or alignments in _bfd_set_section_contents.
632 We can not set this flag until after checking the format,
633 because it will interfere with creation of BFD sections. */
634 if (abfd->direction == both_direction)
635 abfd->output_has_begun = true;
637 free (matching_vector);
638 if (preserve_match.marker != NULL)
639 bfd_preserve_finish (abfd, &preserve_match);
640 bfd_preserve_finish (abfd, &preserve);
641 bfd_set_error_handler (orig_error_handler);
643 struct per_xvec_message **list = _bfd_per_xvec_warn (abfd->xvec, 0);
644 if (*list)
645 print_warnmsg (list);
646 list = _bfd_per_xvec_warn (NULL, 0);
647 for (size_t i = 0; i < _bfd_target_vector_entries + 1; i++)
648 clear_warnmsg (list++);
649 --in_check_format;
651 bfd_set_lto_type (abfd);
653 /* File position has moved, BTW. */
654 return true;
657 if (match_count == 0)
659 err_unrecog:
660 bfd_set_error (bfd_error_file_not_recognized);
661 err_ret:
662 if (cleanup)
663 cleanup (abfd);
664 abfd->xvec = save_targ;
665 abfd->format = bfd_unknown;
666 free (matching_vector);
667 goto out;
670 /* Restore original target type and format. */
671 abfd->xvec = save_targ;
672 abfd->format = bfd_unknown;
673 bfd_set_error (bfd_error_file_ambiguously_recognized);
675 if (matching)
677 *matching = (char **) matching_vector;
678 matching_vector[match_count] = NULL;
679 /* Return target names. This is a little nasty. Maybe we
680 should do another bfd_malloc? */
681 while (--match_count >= 0)
683 const char *name = matching_vector[match_count]->name;
684 *(const char **) &matching_vector[match_count] = name;
687 else
688 free (matching_vector);
689 if (cleanup)
690 cleanup (abfd);
691 out:
692 if (preserve_match.marker != NULL)
693 bfd_preserve_finish (abfd, &preserve_match);
694 bfd_preserve_restore (abfd, &preserve);
695 bfd_set_error_handler (orig_error_handler);
696 struct per_xvec_message **list = _bfd_per_xvec_warn (NULL, 0);
697 struct per_xvec_message **one = NULL;
698 for (size_t i = 0; i < _bfd_target_vector_entries + 1; i++)
700 if (list[i])
702 if (!one)
703 one = list + i;
704 else
706 one = NULL;
707 break;
711 if (one)
712 print_warnmsg (one);
713 for (size_t i = 0; i < _bfd_target_vector_entries + 1; i++)
714 clear_warnmsg (list++);
715 --in_check_format;
716 return false;
720 FUNCTION
721 bfd_set_format
723 SYNOPSIS
724 bool bfd_set_format (bfd *abfd, bfd_format format);
726 DESCRIPTION
727 This function sets the file format of the BFD @var{abfd} to the
728 format @var{format}. If the target set in the BFD does not
729 support the format requested, the format is invalid, or the BFD
730 is not open for writing, then an error occurs.
733 bool
734 bfd_set_format (bfd *abfd, bfd_format format)
736 if (bfd_read_p (abfd)
737 || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
739 bfd_set_error (bfd_error_invalid_operation);
740 return false;
743 if (abfd->format != bfd_unknown)
744 return abfd->format == format;
746 /* Presume the answer is yes. */
747 abfd->format = format;
749 if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
751 abfd->format = bfd_unknown;
752 return false;
755 return true;
759 FUNCTION
760 bfd_format_string
762 SYNOPSIS
763 const char *bfd_format_string (bfd_format format);
765 DESCRIPTION
766 Return a pointer to a const string
767 <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
768 depending upon the value of @var{format}.
771 const char *
772 bfd_format_string (bfd_format format)
774 if (((int) format < (int) bfd_unknown)
775 || ((int) format >= (int) bfd_type_end))
776 return "invalid";
778 switch (format)
780 case bfd_object:
781 return "object"; /* Linker/assembler/compiler output. */
782 case bfd_archive:
783 return "archive"; /* Object archive file. */
784 case bfd_core:
785 return "core"; /* Core dump. */
786 default:
787 return "unknown";