1 /* Generic BFD support for file formats.
2 Copyright (C) 1990-2023 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. */
27 A format is a BFD concept of high level file contents type. The
28 formats supported by BFD are:
32 The BFD may contain data, symbols, relocations and debug info.
36 The BFD contains other BFDs and an optional index.
40 The BFD contains the result of an executable core dump.
50 /* IMPORT from targets.c. */
51 extern const size_t _bfd_target_vector_entries
;
58 bool bfd_check_format (bfd *abfd, bfd_format format);
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
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.
92 bfd_check_format (bfd
*abfd
, bfd_format format
)
94 return bfd_check_format_matches (abfd
, format
, NULL
);
102 const struct bfd_iovec
*iovec
;
104 const struct bfd_arch_info
*arch_info
;
105 const struct bfd_build_id
*build_id
;
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
;
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
127 bfd_preserve_save (bfd
*abfd
, struct bfd_preserve
*preserve
,
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
)
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. */
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 bfd_cache_close (abfd
);
168 abfd
->iovec
= preserve
->iovec
;
169 abfd
->iostream
= preserve
->iostream
;
170 /* Handle in-memory to file backed transition. */
171 if ((abfd
->flags
& BFD_CLOSED_BY_CACHE
) != 0
172 && (abfd
->flags
& BFD_IN_MEMORY
) != 0
173 && (preserve
->flags
& BFD_CLOSED_BY_CACHE
) == 0
174 && (preserve
->flags
& BFD_IN_MEMORY
) == 0)
175 bfd_open_file (abfd
);
177 abfd
->flags
= preserve
->flags
;
180 /* Clear out a subset of BFD state. */
183 bfd_reinit (bfd
*abfd
, unsigned int section_id
,
184 struct bfd_preserve
*preserve
, bfd_cleanup cleanup
)
186 _bfd_section_id
= section_id
;
189 abfd
->tdata
.any
= NULL
;
190 abfd
->arch_info
= &bfd_default_arch_struct
;
191 io_reinit (abfd
, preserve
);
194 abfd
->start_address
= 0;
195 abfd
->build_id
= NULL
;
196 bfd_section_list_clear (abfd
);
199 /* Restores bfd state saved by bfd_preserve_save. */
202 bfd_preserve_restore (bfd
*abfd
, struct bfd_preserve
*preserve
)
204 bfd_hash_table_free (&abfd
->section_htab
);
206 abfd
->tdata
.any
= preserve
->tdata
;
207 abfd
->arch_info
= preserve
->arch_info
;
208 io_reinit (abfd
, preserve
);
209 abfd
->section_htab
= preserve
->section_htab
;
210 abfd
->sections
= preserve
->sections
;
211 abfd
->section_last
= preserve
->section_last
;
212 abfd
->section_count
= preserve
->section_count
;
213 _bfd_section_id
= preserve
->section_id
;
214 abfd
->symcount
= preserve
->symcount
;
215 abfd
->read_only
= preserve
->read_only
;
216 abfd
->start_address
= preserve
->start_address
;
217 abfd
->build_id
= preserve
->build_id
;
219 /* bfd_release frees all memory more recently bfd_alloc'd than
220 its arg, as well as its arg. */
221 bfd_release (abfd
, preserve
->marker
);
222 preserve
->marker
= NULL
;
223 return preserve
->cleanup
;
226 /* Called when the bfd state saved by bfd_preserve_save is no longer
230 bfd_preserve_finish (bfd
*abfd ATTRIBUTE_UNUSED
, struct bfd_preserve
*preserve
)
232 if (preserve
->cleanup
)
234 /* Run the cleanup, assuming that all it will need is the
235 tdata at the time the cleanup was returned. */
236 void *tdata
= abfd
->tdata
.any
;
237 abfd
->tdata
.any
= preserve
->tdata
;
238 preserve
->cleanup (abfd
);
239 abfd
->tdata
.any
= tdata
;
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
245 bfd_hash_table_free (&preserve
->section_htab
);
246 preserve
->marker
= NULL
;
250 print_warnmsg (struct per_xvec_message
**list
)
253 fprintf (stderr
, "%s: ", _bfd_get_error_program_name ());
255 for (struct per_xvec_message
*warn
= *list
; warn
; warn
= warn
->next
)
257 fputs (warn
->message
, stderr
);
258 fputc ('\n', stderr
);
264 clear_warnmsg (struct per_xvec_message
**list
)
266 struct per_xvec_message
*warn
= *list
;
269 struct per_xvec_message
*next
= warn
->next
;
277 null_error_handler (const char *fmt ATTRIBUTE_UNUSED
,
278 va_list ap ATTRIBUTE_UNUSED
)
284 bfd_check_format_matches
287 bool bfd_check_format_matches
288 (bfd *abfd, bfd_format format, char ***matching);
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.
298 When done with the list that @var{matching} points to, the caller
303 bfd_check_format_matches (bfd
*abfd
, bfd_format format
, char ***matching
)
305 extern const bfd_target binary_vec
;
306 #if BFD_SUPPORTS_PLUGINS
307 extern const bfd_target plugin_vec
;
309 const bfd_target
* const *target
;
310 const bfd_target
**matching_vector
= NULL
;
311 const bfd_target
*save_targ
, *right_targ
, *ar_right_targ
, *match_targ
;
312 int match_count
, best_count
, best_match
;
314 unsigned int initial_section_id
= _bfd_section_id
;
315 struct bfd_preserve preserve
, preserve_match
;
316 bfd_cleanup cleanup
= NULL
;
317 bfd_error_handler_type orig_error_handler
;
318 static int in_check_format
;
320 if (matching
!= NULL
)
323 if (!bfd_read_p (abfd
)
324 || (unsigned int) abfd
->format
>= (unsigned int) bfd_type_end
)
326 bfd_set_error (bfd_error_invalid_operation
);
330 if (abfd
->format
!= bfd_unknown
)
331 return abfd
->format
== format
;
333 if (matching
!= NULL
|| *bfd_associated_vector
!= NULL
)
337 amt
= sizeof (*matching_vector
) * 2 * _bfd_target_vector_entries
;
338 matching_vector
= (const bfd_target
**) bfd_malloc (amt
);
339 if (!matching_vector
)
343 /* Presume the answer is yes. */
344 abfd
->format
= format
;
345 save_targ
= abfd
->xvec
;
347 /* Don't report errors on recursive calls checking the first element
350 orig_error_handler
= bfd_set_error_handler (null_error_handler
);
352 orig_error_handler
= _bfd_set_error_handler_caching (abfd
);
355 preserve_match
.marker
= NULL
;
356 if (!bfd_preserve_save (abfd
, &preserve
, NULL
))
359 /* If the target type was explicitly specified, just check that target. */
360 if (!abfd
->target_defaulted
)
362 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0) /* rewind! */
365 cleanup
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
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
382 if (format
== bfd_archive
&& save_targ
== &binary_vec
)
386 /* Since the target type was defaulted, check them all in the hope
387 that one will be uniquely recognized. */
389 ar_right_targ
= NULL
;
394 ar_match_index
= _bfd_target_vector_entries
;
396 for (target
= bfd_target_vector
; *target
!= NULL
; target
++)
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 if (*target
== &binary_vec
406 #if BFD_SUPPORTS_PLUGINS
407 || (match_count
!= 0 && *target
== &plugin_vec
)
409 || (!abfd
->target_defaulted
&& *target
== save_targ
))
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 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 if (preserve_match
.marker
)
419 high_water
= &preserve_match
.marker
;
421 high_water
= &preserve
.marker
;
422 bfd_release (abfd
, *high_water
);
423 *high_water
= bfd_alloc (abfd
, 1);
425 /* Change BFD's target temporarily. */
426 abfd
->xvec
= *target
;
428 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
431 cleanup
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
434 int match_priority
= abfd
->xvec
->match_priority
;
435 #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 if (*target
== &plugin_vec
)
441 match_priority
= (*target
)->match_priority
;
444 if (abfd
->format
!= bfd_archive
445 || (bfd_has_map (abfd
)
446 && bfd_get_error () != bfd_error_wrong_object_format
))
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 if (abfd
->xvec
== bfd_default_vector
[0])
455 matching_vector
[match_count
] = abfd
->xvec
;
458 if (match_priority
< best_match
)
460 best_match
= match_priority
;
463 if (match_priority
<= best_match
)
465 /* This format checks out as ok! */
466 right_targ
= abfd
->xvec
;
472 /* An archive with no armap or objects of the wrong
473 type. We want this target to match if we get no
475 if (ar_right_targ
!= bfd_default_vector
[0])
476 ar_right_targ
= *target
;
478 matching_vector
[ar_match_index
] = *target
;
482 if (preserve_match
.marker
== NULL
)
484 match_targ
= abfd
->xvec
;
485 if (!bfd_preserve_save (abfd
, &preserve_match
, cleanup
))
495 if (match_count
== 0)
497 /* Try partial matches. */
498 right_targ
= ar_right_targ
;
500 if (right_targ
== bfd_default_vector
[0])
506 match_count
= ar_match_index
- _bfd_target_vector_entries
;
508 if (matching_vector
&& match_count
> 1)
509 memcpy (matching_vector
,
510 matching_vector
+ _bfd_target_vector_entries
,
511 sizeof (*matching_vector
) * match_count
);
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,
520 const bfd_target
* const *assoc
= bfd_associated_vector
;
522 while ((right_targ
= *assoc
++) != NULL
)
527 if (matching_vector
[i
] == right_targ
528 && right_targ
->match_priority
<= best_match
)
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
542 if (matching_vector
&& match_count
> 1 && best_count
!= match_count
)
546 for (i
= 0; i
< match_count
; i
++)
548 right_targ
= matching_vector
[i
];
549 if (right_targ
->match_priority
<= best_match
)
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 if (preserve_match
.marker
!= NULL
)
560 cleanup
= bfd_preserve_restore (abfd
, &preserve_match
);
562 if (match_count
== 1)
564 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
572 if (match_targ
!= right_targ
)
574 bfd_reinit (abfd
, initial_section_id
, &preserve
, cleanup
);
575 bfd_release (abfd
, preserve
.marker
);
576 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
578 cleanup
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
579 BFD_ASSERT (cleanup
!= NULL
);
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 if (abfd
->direction
== both_direction
)
589 abfd
->output_has_begun
= true;
591 free (matching_vector
);
592 if (preserve_match
.marker
!= NULL
)
593 bfd_preserve_finish (abfd
, &preserve_match
);
594 bfd_preserve_finish (abfd
, &preserve
);
595 bfd_set_error_handler (orig_error_handler
);
597 struct per_xvec_message
**list
= _bfd_per_xvec_warn (abfd
->xvec
, 0);
599 print_warnmsg (list
);
600 list
= _bfd_per_xvec_warn (NULL
, 0);
601 for (size_t i
= 0; i
< _bfd_target_vector_entries
+ 1; i
++)
602 clear_warnmsg (list
++);
605 /* File position has moved, BTW. */
609 if (match_count
== 0)
612 bfd_set_error (bfd_error_file_not_recognized
);
616 abfd
->xvec
= save_targ
;
617 abfd
->format
= bfd_unknown
;
618 free (matching_vector
);
622 /* Restore original target type and format. */
623 abfd
->xvec
= save_targ
;
624 abfd
->format
= bfd_unknown
;
625 bfd_set_error (bfd_error_file_ambiguously_recognized
);
629 *matching
= (char **) matching_vector
;
630 matching_vector
[match_count
] = NULL
;
631 /* Return target names. This is a little nasty. Maybe we
632 should do another bfd_malloc? */
633 while (--match_count
>= 0)
635 const char *name
= matching_vector
[match_count
]->name
;
636 *(const char **) &matching_vector
[match_count
] = name
;
640 free (matching_vector
);
644 if (preserve_match
.marker
!= NULL
)
645 bfd_preserve_finish (abfd
, &preserve_match
);
646 bfd_preserve_restore (abfd
, &preserve
);
647 bfd_set_error_handler (orig_error_handler
);
648 struct per_xvec_message
**list
= _bfd_per_xvec_warn (NULL
, 0);
649 struct per_xvec_message
**one
= NULL
;
650 for (size_t i
= 0; i
< _bfd_target_vector_entries
+ 1; i
++)
665 for (size_t i
= 0; i
< _bfd_target_vector_entries
+ 1; i
++)
666 clear_warnmsg (list
++);
676 bool bfd_set_format (bfd *abfd, bfd_format format);
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.
686 bfd_set_format (bfd
*abfd
, bfd_format format
)
688 if (bfd_read_p (abfd
)
689 || (unsigned int) abfd
->format
>= (unsigned int) bfd_type_end
)
691 bfd_set_error (bfd_error_invalid_operation
);
695 if (abfd
->format
!= bfd_unknown
)
696 return abfd
->format
== format
;
698 /* Presume the answer is yes. */
699 abfd
->format
= format
;
701 if (!BFD_SEND_FMT (abfd
, _bfd_set_format
, (abfd
)))
703 abfd
->format
= bfd_unknown
;
715 const char *bfd_format_string (bfd_format format);
718 Return a pointer to a const string
719 <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
720 depending upon the value of @var{format}.
724 bfd_format_string (bfd_format format
)
726 if (((int) format
< (int) bfd_unknown
)
727 || ((int) format
>= (int) bfd_type_end
))
733 return "object"; /* Linker/assembler/compiler output. */
735 return "archive"; /* Object archive file. */
737 return "core"; /* Core dump. */