1 /* Generic BFD support for file formats.
2 Copyright (C) 1990-2020 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 bfd_boolean 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_arch_info
*arch_info
;
103 struct bfd_section
*sections
;
104 struct bfd_section
*section_last
;
105 unsigned int section_count
;
106 unsigned int section_id
;
107 struct bfd_hash_table section_htab
;
108 const struct bfd_build_id
*build_id
;
111 /* When testing an object for compatibility with a particular target
112 back-end, the back-end object_p function needs to set up certain
113 fields in the bfd on successfully recognizing the object. This
114 typically happens in a piecemeal fashion, with failures possible at
115 many points. On failure, the bfd is supposed to be restored to its
116 initial state, which is virtually impossible. However, restoring a
117 subset of the bfd state works in practice. This function stores
121 bfd_preserve_save (bfd
*abfd
, struct bfd_preserve
*preserve
)
123 preserve
->tdata
= abfd
->tdata
.any
;
124 preserve
->arch_info
= abfd
->arch_info
;
125 preserve
->flags
= abfd
->flags
;
126 preserve
->sections
= abfd
->sections
;
127 preserve
->section_last
= abfd
->section_last
;
128 preserve
->section_count
= abfd
->section_count
;
129 preserve
->section_id
= _bfd_section_id
;
130 preserve
->section_htab
= abfd
->section_htab
;
131 preserve
->marker
= bfd_alloc (abfd
, 1);
132 preserve
->build_id
= abfd
->build_id
;
133 if (preserve
->marker
== NULL
)
136 return bfd_hash_table_init (&abfd
->section_htab
, bfd_section_hash_newfunc
,
137 sizeof (struct section_hash_entry
));
140 /* Clear out a subset of BFD state. */
143 bfd_reinit (bfd
*abfd
, unsigned int section_id
)
145 abfd
->tdata
.any
= NULL
;
146 abfd
->arch_info
= &bfd_default_arch_struct
;
147 abfd
->flags
&= BFD_FLAGS_SAVED
;
148 bfd_section_list_clear (abfd
);
149 _bfd_section_id
= section_id
;
152 /* Restores bfd state saved by bfd_preserve_save. */
155 bfd_preserve_restore (bfd
*abfd
, struct bfd_preserve
*preserve
)
157 bfd_hash_table_free (&abfd
->section_htab
);
159 abfd
->tdata
.any
= preserve
->tdata
;
160 abfd
->arch_info
= preserve
->arch_info
;
161 abfd
->flags
= preserve
->flags
;
162 abfd
->section_htab
= preserve
->section_htab
;
163 abfd
->sections
= preserve
->sections
;
164 abfd
->section_last
= preserve
->section_last
;
165 abfd
->section_count
= preserve
->section_count
;
166 _bfd_section_id
= preserve
->section_id
;
167 abfd
->build_id
= preserve
->build_id
;
169 /* bfd_release frees all memory more recently bfd_alloc'd than
170 its arg, as well as its arg. */
171 bfd_release (abfd
, preserve
->marker
);
172 preserve
->marker
= NULL
;
175 /* Called when the bfd state saved by bfd_preserve_save is no longer
179 bfd_preserve_finish (bfd
*abfd ATTRIBUTE_UNUSED
, struct bfd_preserve
*preserve
)
181 /* It would be nice to be able to free more memory here, eg. old
182 tdata, but that's not possible since these blocks are sitting
183 inside bfd_alloc'd memory. The section hash is on a separate
185 bfd_hash_table_free (&preserve
->section_htab
);
186 preserve
->marker
= NULL
;
191 bfd_check_format_matches
194 bfd_boolean bfd_check_format_matches
195 (bfd *abfd, bfd_format format, char ***matching);
198 Like <<bfd_check_format>>, except when it returns FALSE with
199 <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that
200 case, if @var{matching} is not NULL, it will be filled in with
201 a NULL-terminated list of the names of the formats that matched,
202 allocated with <<malloc>>.
203 Then the user may choose a format and try again.
205 When done with the list that @var{matching} points to, the caller
210 bfd_check_format_matches (bfd
*abfd
, bfd_format format
, char ***matching
)
212 extern const bfd_target binary_vec
;
213 #if BFD_SUPPORTS_PLUGINS
214 extern const bfd_target plugin_vec
;
216 const bfd_target
* const *target
;
217 const bfd_target
**matching_vector
= NULL
;
218 const bfd_target
*save_targ
, *right_targ
, *ar_right_targ
, *match_targ
;
219 int match_count
, best_count
, best_match
;
221 unsigned int initial_section_id
= _bfd_section_id
;
222 struct bfd_preserve preserve
, preserve_match
;
224 if (matching
!= NULL
)
227 if (!bfd_read_p (abfd
)
228 || (unsigned int) abfd
->format
>= (unsigned int) bfd_type_end
)
230 bfd_set_error (bfd_error_invalid_operation
);
234 if (abfd
->format
!= bfd_unknown
)
235 return abfd
->format
== format
;
237 if (matching
!= NULL
|| *bfd_associated_vector
!= NULL
)
241 amt
= sizeof (*matching_vector
) * 2 * _bfd_target_vector_entries
;
242 matching_vector
= (const bfd_target
**) bfd_malloc (amt
);
243 if (!matching_vector
)
247 /* Presume the answer is yes. */
248 abfd
->format
= format
;
249 save_targ
= abfd
->xvec
;
251 preserve_match
.marker
= NULL
;
252 if (!bfd_preserve_save (abfd
, &preserve
))
255 /* If the target type was explicitly specified, just check that target. */
256 if (!abfd
->target_defaulted
)
258 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0) /* rewind! */
261 right_targ
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
266 /* For a long time the code has dropped through to check all
267 targets if the specified target was wrong. I don't know why,
268 and I'm reluctant to change it. However, in the case of an
269 archive, it can cause problems. If the specified target does
270 not permit archives (e.g., the binary target), then we should
271 not allow some other target to recognize it as an archive, but
272 should instead allow the specified target to recognize it as an
273 object. When I first made this change, it broke the PE target,
274 because the specified pei-i386 target did not recognize the
275 actual pe-i386 archive. Since there may be other problems of
276 this sort, I changed this test to check only for the binary
278 if (format
== bfd_archive
&& save_targ
== &binary_vec
)
282 /* Since the target type was defaulted, check them all in the hope
283 that one will be uniquely recognized. */
285 ar_right_targ
= NULL
;
290 ar_match_index
= _bfd_target_vector_entries
;
292 for (target
= bfd_target_vector
; *target
!= NULL
; target
++)
294 const bfd_target
*temp
;
297 /* The binary target matches anything, so don't return it when
298 searching. Don't match the plugin target if we have another
299 alternative since we want to properly set the input format
300 before allowing a plugin to claim the file. Also, don't
301 check the default target twice. */
302 if (*target
== &binary_vec
303 #if BFD_SUPPORTS_PLUGINS
304 || (match_count
!= 0 && *target
== &plugin_vec
)
306 || (!abfd
->target_defaulted
&& *target
== save_targ
))
309 /* If we already tried a match, the bfd is modified and may
310 have sections attached, which will confuse the next
311 _bfd_check_format call. */
312 bfd_reinit (abfd
, initial_section_id
);
313 /* Free bfd_alloc memory too. If we have matched and preserved
314 a target then the high water mark is that much higher. */
315 if (preserve_match
.marker
)
316 high_water
= &preserve_match
.marker
;
318 high_water
= &preserve
.marker
;
319 bfd_release (abfd
, *high_water
);
320 *high_water
= bfd_alloc (abfd
, 1);
322 /* Change BFD's target temporarily. */
323 abfd
->xvec
= *target
;
325 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
328 temp
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
331 int match_priority
= temp
->match_priority
;
332 #if BFD_SUPPORTS_PLUGINS
333 /* If this object can be handled by a plugin, give that the
334 lowest priority; objects both handled by a plugin and
335 with an underlying object format will be claimed
336 separately by the plugin. */
337 if (*target
== &plugin_vec
)
338 match_priority
= (*target
)->match_priority
;
341 if (abfd
->format
!= bfd_archive
342 || (bfd_has_map (abfd
)
343 && bfd_get_error () != bfd_error_wrong_object_format
))
345 /* If this is the default target, accept it, even if
346 other targets might match. People who want those
347 other targets have to set the GNUTARGET variable. */
348 if (temp
== bfd_default_vector
[0])
352 matching_vector
[match_count
] = temp
;
355 if (match_priority
< best_match
)
357 best_match
= match_priority
;
360 if (match_priority
<= best_match
)
362 /* This format checks out as ok! */
369 /* An archive with no armap or objects of the wrong
370 type. We want this target to match if we get no
372 if (ar_right_targ
!= bfd_default_vector
[0])
373 ar_right_targ
= *target
;
375 matching_vector
[ar_match_index
] = *target
;
379 if (preserve_match
.marker
== NULL
)
382 if (!bfd_preserve_save (abfd
, &preserve_match
))
391 if (match_count
== 0)
393 /* Try partial matches. */
394 right_targ
= ar_right_targ
;
396 if (right_targ
== bfd_default_vector
[0])
402 match_count
= ar_match_index
- _bfd_target_vector_entries
;
404 if (matching_vector
&& match_count
> 1)
405 memcpy (matching_vector
,
406 matching_vector
+ _bfd_target_vector_entries
,
407 sizeof (*matching_vector
) * match_count
);
411 /* We have more than one equally good match. If any of the best
412 matches is a target in config.bfd targ_defvec or targ_selvecs,
416 const bfd_target
* const *assoc
= bfd_associated_vector
;
418 while ((right_targ
= *assoc
++) != NULL
)
423 if (matching_vector
[i
] == right_targ
424 && right_targ
->match_priority
<= best_match
)
435 /* We still have more than one equally good match, and at least some
436 of the targets support match priority. Choose the first of the
438 if (matching_vector
&& match_count
> 1 && best_count
!= match_count
)
442 for (i
= 0; i
< match_count
; i
++)
444 right_targ
= matching_vector
[i
];
445 if (right_targ
->match_priority
<= best_match
)
451 /* There is way too much undoing of half-known state here. We
452 really shouldn't iterate on live bfd's. Note that saving the
453 whole bfd and restoring it would be even worse; the first thing
454 you notice is that the cached bfd file position gets out of sync. */
455 if (preserve_match
.marker
!= NULL
)
456 bfd_preserve_restore (abfd
, &preserve_match
);
458 if (match_count
== 1)
460 abfd
->xvec
= right_targ
;
461 /* If we come out of the loop knowing that the last target that
462 matched is the one we want, then ABFD should still be in a usable
463 state (except possibly for XVEC). This is not just an
464 optimisation. In the case of plugins a match against the
465 plugin target can result in the bfd being changed such that
466 it no longer matches the plugin target, nor will it match
468 if (match_targ
!= right_targ
)
470 bfd_reinit (abfd
, initial_section_id
);
471 bfd_release (abfd
, preserve
.marker
);
472 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
474 match_targ
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
475 BFD_ASSERT (match_targ
!= NULL
);
479 /* If the file was opened for update, then `output_has_begun'
480 some time ago when the file was created. Do not recompute
481 sections sizes or alignments in _bfd_set_section_contents.
482 We can not set this flag until after checking the format,
483 because it will interfere with creation of BFD sections. */
484 if (abfd
->direction
== both_direction
)
485 abfd
->output_has_begun
= TRUE
;
488 free (matching_vector
);
489 if (preserve_match
.marker
!= NULL
)
490 bfd_preserve_finish (abfd
, &preserve_match
);
491 bfd_preserve_finish (abfd
, &preserve
);
493 /* File position has moved, BTW. */
497 if (match_count
== 0)
500 bfd_set_error (bfd_error_file_not_recognized
);
502 abfd
->xvec
= save_targ
;
503 abfd
->format
= bfd_unknown
;
505 free (matching_vector
);
506 if (preserve_match
.marker
!= NULL
)
507 bfd_preserve_finish (abfd
, &preserve_match
);
508 bfd_preserve_restore (abfd
, &preserve
);
512 /* Restore original target type and format. */
513 abfd
->xvec
= save_targ
;
514 abfd
->format
= bfd_unknown
;
515 bfd_set_error (bfd_error_file_ambiguously_recognized
);
519 *matching
= (char **) matching_vector
;
520 matching_vector
[match_count
] = NULL
;
521 /* Return target names. This is a little nasty. Maybe we
522 should do another bfd_malloc? */
523 while (--match_count
>= 0)
525 const char *name
= matching_vector
[match_count
]->name
;
526 *(const char **) &matching_vector
[match_count
] = name
;
529 else if (matching_vector
)
530 free (matching_vector
);
531 if (preserve_match
.marker
!= NULL
)
532 bfd_preserve_finish (abfd
, &preserve_match
);
533 bfd_preserve_restore (abfd
, &preserve
);
542 bfd_boolean bfd_set_format (bfd *abfd, bfd_format format);
545 This function sets the file format of the BFD @var{abfd} to the
546 format @var{format}. If the target set in the BFD does not
547 support the format requested, the format is invalid, or the BFD
548 is not open for writing, then an error occurs.
552 bfd_set_format (bfd
*abfd
, bfd_format format
)
554 if (bfd_read_p (abfd
)
555 || (unsigned int) abfd
->format
>= (unsigned int) bfd_type_end
)
557 bfd_set_error (bfd_error_invalid_operation
);
561 if (abfd
->format
!= bfd_unknown
)
562 return abfd
->format
== format
;
564 /* Presume the answer is yes. */
565 abfd
->format
= format
;
567 if (!BFD_SEND_FMT (abfd
, _bfd_set_format
, (abfd
)))
569 abfd
->format
= bfd_unknown
;
581 const char *bfd_format_string (bfd_format format);
584 Return a pointer to a const string
585 <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
586 depending upon the value of @var{format}.
590 bfd_format_string (bfd_format format
)
592 if (((int) format
< (int) bfd_unknown
)
593 || ((int) format
>= (int) bfd_type_end
))
599 return "object"; /* Linker/assembler/compiler output. */
601 return "archive"; /* Object archive file. */
603 return "core"; /* Core dump. */