1377e23a2Schristos /* Generic BFD support for file formats.
2*1424dfb3Schristos Copyright (C) 1990-2020 Free Software Foundation, Inc.
3377e23a2Schristos Written by Cygnus Support.
4377e23a2Schristos
5377e23a2Schristos This file is part of BFD, the Binary File Descriptor library.
6377e23a2Schristos
7377e23a2Schristos This program is free software; you can redistribute it and/or modify
8377e23a2Schristos it under the terms of the GNU General Public License as published by
9377e23a2Schristos the Free Software Foundation; either version 3 of the License, or
10377e23a2Schristos (at your option) any later version.
11377e23a2Schristos
12377e23a2Schristos This program is distributed in the hope that it will be useful,
13377e23a2Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
14377e23a2Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15377e23a2Schristos GNU General Public License for more details.
16377e23a2Schristos
17377e23a2Schristos You should have received a copy of the GNU General Public License
18377e23a2Schristos along with this program; if not, write to the Free Software
19377e23a2Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20377e23a2Schristos MA 02110-1301, USA. */
21377e23a2Schristos
22377e23a2Schristos
23377e23a2Schristos /*
24377e23a2Schristos SECTION
25377e23a2Schristos File formats
26377e23a2Schristos
27377e23a2Schristos A format is a BFD concept of high level file contents type. The
28377e23a2Schristos formats supported by BFD are:
29377e23a2Schristos
30377e23a2Schristos o <<bfd_object>>
31377e23a2Schristos
32377e23a2Schristos The BFD may contain data, symbols, relocations and debug info.
33377e23a2Schristos
34377e23a2Schristos o <<bfd_archive>>
35377e23a2Schristos
36377e23a2Schristos The BFD contains other BFDs and an optional index.
37377e23a2Schristos
38377e23a2Schristos o <<bfd_core>>
39377e23a2Schristos
40377e23a2Schristos The BFD contains the result of an executable core dump.
41377e23a2Schristos
42377e23a2Schristos SUBSECTION
43377e23a2Schristos File format functions
44377e23a2Schristos */
45377e23a2Schristos
46377e23a2Schristos #include "sysdep.h"
47377e23a2Schristos #include "bfd.h"
48377e23a2Schristos #include "libbfd.h"
49377e23a2Schristos
50377e23a2Schristos /* IMPORT from targets.c. */
51377e23a2Schristos extern const size_t _bfd_target_vector_entries;
52377e23a2Schristos
53377e23a2Schristos /*
54377e23a2Schristos FUNCTION
55377e23a2Schristos bfd_check_format
56377e23a2Schristos
57377e23a2Schristos SYNOPSIS
58377e23a2Schristos bfd_boolean bfd_check_format (bfd *abfd, bfd_format format);
59377e23a2Schristos
60377e23a2Schristos DESCRIPTION
61377e23a2Schristos Verify if the file attached to the BFD @var{abfd} is compatible
62377e23a2Schristos with the format @var{format} (i.e., one of <<bfd_object>>,
63377e23a2Schristos <<bfd_archive>> or <<bfd_core>>).
64377e23a2Schristos
65377e23a2Schristos If the BFD has been set to a specific target before the
66377e23a2Schristos call, only the named target and format combination is
67377e23a2Schristos checked. If the target has not been set, or has been set to
68377e23a2Schristos <<default>>, then all the known target backends is
69377e23a2Schristos interrogated to determine a match. If the default target
70377e23a2Schristos matches, it is used. If not, exactly one target must recognize
71377e23a2Schristos the file, or an error results.
72377e23a2Schristos
73377e23a2Schristos The function returns <<TRUE>> on success, otherwise <<FALSE>>
74377e23a2Schristos with one of the following error codes:
75377e23a2Schristos
76377e23a2Schristos o <<bfd_error_invalid_operation>> -
77377e23a2Schristos if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
78377e23a2Schristos <<bfd_core>>.
79377e23a2Schristos
80377e23a2Schristos o <<bfd_error_system_call>> -
81377e23a2Schristos if an error occured during a read - even some file mismatches
82377e23a2Schristos can cause bfd_error_system_calls.
83377e23a2Schristos
84377e23a2Schristos o <<file_not_recognised>> -
85377e23a2Schristos none of the backends recognised the file format.
86377e23a2Schristos
87377e23a2Schristos o <<bfd_error_file_ambiguously_recognized>> -
88377e23a2Schristos more than one backend recognised the file format.
89377e23a2Schristos */
90377e23a2Schristos
91377e23a2Schristos bfd_boolean
bfd_check_format(bfd * abfd,bfd_format format)92377e23a2Schristos bfd_check_format (bfd *abfd, bfd_format format)
93377e23a2Schristos {
94377e23a2Schristos return bfd_check_format_matches (abfd, format, NULL);
95377e23a2Schristos }
96377e23a2Schristos
9748596154Schristos struct bfd_preserve
9848596154Schristos {
9948596154Schristos void *marker;
10048596154Schristos void *tdata;
10148596154Schristos flagword flags;
10248596154Schristos const struct bfd_arch_info *arch_info;
10348596154Schristos struct bfd_section *sections;
10448596154Schristos struct bfd_section *section_last;
10548596154Schristos unsigned int section_count;
10607163879Schristos unsigned int section_id;
10748596154Schristos struct bfd_hash_table section_htab;
1081c468f90Schristos const struct bfd_build_id *build_id;
109*1424dfb3Schristos bfd_cleanup cleanup;
11048596154Schristos };
11148596154Schristos
11248596154Schristos /* When testing an object for compatibility with a particular target
11348596154Schristos back-end, the back-end object_p function needs to set up certain
11448596154Schristos fields in the bfd on successfully recognizing the object. This
11548596154Schristos typically happens in a piecemeal fashion, with failures possible at
11648596154Schristos many points. On failure, the bfd is supposed to be restored to its
11748596154Schristos initial state, which is virtually impossible. However, restoring a
11848596154Schristos subset of the bfd state works in practice. This function stores
11948596154Schristos the subset. */
12048596154Schristos
12148596154Schristos static bfd_boolean
bfd_preserve_save(bfd * abfd,struct bfd_preserve * preserve,bfd_cleanup cleanup)122*1424dfb3Schristos bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve,
123*1424dfb3Schristos bfd_cleanup cleanup)
12448596154Schristos {
12548596154Schristos preserve->tdata = abfd->tdata.any;
12648596154Schristos preserve->arch_info = abfd->arch_info;
12748596154Schristos preserve->flags = abfd->flags;
12848596154Schristos preserve->sections = abfd->sections;
12948596154Schristos preserve->section_last = abfd->section_last;
13048596154Schristos preserve->section_count = abfd->section_count;
13107163879Schristos preserve->section_id = _bfd_section_id;
13248596154Schristos preserve->section_htab = abfd->section_htab;
13348596154Schristos preserve->marker = bfd_alloc (abfd, 1);
1341c468f90Schristos preserve->build_id = abfd->build_id;
135*1424dfb3Schristos preserve->cleanup = cleanup;
13648596154Schristos if (preserve->marker == NULL)
13748596154Schristos return FALSE;
13848596154Schristos
13948596154Schristos return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
14048596154Schristos sizeof (struct section_hash_entry));
14148596154Schristos }
14248596154Schristos
14348596154Schristos /* Clear out a subset of BFD state. */
14448596154Schristos
14548596154Schristos static void
bfd_reinit(bfd * abfd,unsigned int section_id,bfd_cleanup cleanup)146*1424dfb3Schristos bfd_reinit (bfd *abfd, unsigned int section_id, bfd_cleanup cleanup)
14748596154Schristos {
148*1424dfb3Schristos _bfd_section_id = section_id;
149*1424dfb3Schristos if (cleanup)
150*1424dfb3Schristos cleanup (abfd);
15148596154Schristos abfd->tdata.any = NULL;
15248596154Schristos abfd->arch_info = &bfd_default_arch_struct;
15348596154Schristos abfd->flags &= BFD_FLAGS_SAVED;
15448596154Schristos bfd_section_list_clear (abfd);
15548596154Schristos }
15648596154Schristos
15748596154Schristos /* Restores bfd state saved by bfd_preserve_save. */
15848596154Schristos
159*1424dfb3Schristos static bfd_cleanup
bfd_preserve_restore(bfd * abfd,struct bfd_preserve * preserve)16048596154Schristos bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
16148596154Schristos {
16248596154Schristos bfd_hash_table_free (&abfd->section_htab);
16348596154Schristos
16448596154Schristos abfd->tdata.any = preserve->tdata;
16548596154Schristos abfd->arch_info = preserve->arch_info;
16648596154Schristos abfd->flags = preserve->flags;
16748596154Schristos abfd->section_htab = preserve->section_htab;
16848596154Schristos abfd->sections = preserve->sections;
16948596154Schristos abfd->section_last = preserve->section_last;
17048596154Schristos abfd->section_count = preserve->section_count;
17107163879Schristos _bfd_section_id = preserve->section_id;
1721c468f90Schristos abfd->build_id = preserve->build_id;
17348596154Schristos
17448596154Schristos /* bfd_release frees all memory more recently bfd_alloc'd than
17548596154Schristos its arg, as well as its arg. */
17648596154Schristos bfd_release (abfd, preserve->marker);
17748596154Schristos preserve->marker = NULL;
178*1424dfb3Schristos return preserve->cleanup;
17948596154Schristos }
18048596154Schristos
18148596154Schristos /* Called when the bfd state saved by bfd_preserve_save is no longer
18248596154Schristos needed. */
18348596154Schristos
18448596154Schristos static void
bfd_preserve_finish(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_preserve * preserve)18548596154Schristos bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
18648596154Schristos {
187*1424dfb3Schristos if (preserve->cleanup)
188*1424dfb3Schristos {
189*1424dfb3Schristos /* Run the cleanup, assuming that all it will need is the
190*1424dfb3Schristos tdata at the time the cleanup was returned. */
191*1424dfb3Schristos void *tdata = abfd->tdata.any;
192*1424dfb3Schristos abfd->tdata.any = preserve->tdata;
193*1424dfb3Schristos preserve->cleanup (abfd);
194*1424dfb3Schristos abfd->tdata.any = tdata;
195*1424dfb3Schristos }
19648596154Schristos /* It would be nice to be able to free more memory here, eg. old
19748596154Schristos tdata, but that's not possible since these blocks are sitting
19848596154Schristos inside bfd_alloc'd memory. The section hash is on a separate
19948596154Schristos objalloc. */
20048596154Schristos bfd_hash_table_free (&preserve->section_htab);
20148596154Schristos preserve->marker = NULL;
20248596154Schristos }
20348596154Schristos
204377e23a2Schristos /*
205377e23a2Schristos FUNCTION
206377e23a2Schristos bfd_check_format_matches
207377e23a2Schristos
208377e23a2Schristos SYNOPSIS
209377e23a2Schristos bfd_boolean bfd_check_format_matches
210377e23a2Schristos (bfd *abfd, bfd_format format, char ***matching);
211377e23a2Schristos
212377e23a2Schristos DESCRIPTION
213377e23a2Schristos Like <<bfd_check_format>>, except when it returns FALSE with
214377e23a2Schristos <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that
215377e23a2Schristos case, if @var{matching} is not NULL, it will be filled in with
216377e23a2Schristos a NULL-terminated list of the names of the formats that matched,
217377e23a2Schristos allocated with <<malloc>>.
218377e23a2Schristos Then the user may choose a format and try again.
219377e23a2Schristos
220377e23a2Schristos When done with the list that @var{matching} points to, the caller
221377e23a2Schristos should free it.
222377e23a2Schristos */
223377e23a2Schristos
224377e23a2Schristos bfd_boolean
bfd_check_format_matches(bfd * abfd,bfd_format format,char *** matching)225377e23a2Schristos bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
226377e23a2Schristos {
227377e23a2Schristos extern const bfd_target binary_vec;
228c03b94e9Schristos #if BFD_SUPPORTS_PLUGINS
229c03b94e9Schristos extern const bfd_target plugin_vec;
230c03b94e9Schristos #endif
231377e23a2Schristos const bfd_target * const *target;
232377e23a2Schristos const bfd_target **matching_vector = NULL;
23348596154Schristos const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
23448596154Schristos int match_count, best_count, best_match;
235377e23a2Schristos int ar_match_index;
23607163879Schristos unsigned int initial_section_id = _bfd_section_id;
237*1424dfb3Schristos struct bfd_preserve preserve, preserve_match;
238*1424dfb3Schristos bfd_cleanup cleanup = NULL;
239377e23a2Schristos
240377e23a2Schristos if (matching != NULL)
241377e23a2Schristos *matching = NULL;
242377e23a2Schristos
243377e23a2Schristos if (!bfd_read_p (abfd)
244377e23a2Schristos || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
245377e23a2Schristos {
246377e23a2Schristos bfd_set_error (bfd_error_invalid_operation);
247377e23a2Schristos return FALSE;
248377e23a2Schristos }
249377e23a2Schristos
250377e23a2Schristos if (abfd->format != bfd_unknown)
251377e23a2Schristos return abfd->format == format;
252377e23a2Schristos
253377e23a2Schristos if (matching != NULL || *bfd_associated_vector != NULL)
254377e23a2Schristos {
255*1424dfb3Schristos size_t amt;
256377e23a2Schristos
257377e23a2Schristos amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
258377e23a2Schristos matching_vector = (const bfd_target **) bfd_malloc (amt);
259377e23a2Schristos if (!matching_vector)
260377e23a2Schristos return FALSE;
261377e23a2Schristos }
262377e23a2Schristos
263377e23a2Schristos /* Presume the answer is yes. */
264377e23a2Schristos abfd->format = format;
26548596154Schristos save_targ = abfd->xvec;
266*1424dfb3Schristos
267*1424dfb3Schristos preserve_match.marker = NULL;
268*1424dfb3Schristos if (!bfd_preserve_save (abfd, &preserve, NULL))
269*1424dfb3Schristos goto err_ret;
270377e23a2Schristos
271377e23a2Schristos /* If the target type was explicitly specified, just check that target. */
272377e23a2Schristos if (!abfd->target_defaulted)
273377e23a2Schristos {
274377e23a2Schristos if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) /* rewind! */
275377e23a2Schristos goto err_ret;
276377e23a2Schristos
277*1424dfb3Schristos cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
278377e23a2Schristos
279*1424dfb3Schristos if (cleanup)
280377e23a2Schristos goto ok_ret;
281377e23a2Schristos
282377e23a2Schristos /* For a long time the code has dropped through to check all
283377e23a2Schristos targets if the specified target was wrong. I don't know why,
284377e23a2Schristos and I'm reluctant to change it. However, in the case of an
285377e23a2Schristos archive, it can cause problems. If the specified target does
286377e23a2Schristos not permit archives (e.g., the binary target), then we should
287377e23a2Schristos not allow some other target to recognize it as an archive, but
288377e23a2Schristos should instead allow the specified target to recognize it as an
289377e23a2Schristos object. When I first made this change, it broke the PE target,
290377e23a2Schristos because the specified pei-i386 target did not recognize the
291377e23a2Schristos actual pe-i386 archive. Since there may be other problems of
292377e23a2Schristos this sort, I changed this test to check only for the binary
293377e23a2Schristos target. */
294377e23a2Schristos if (format == bfd_archive && save_targ == &binary_vec)
295377e23a2Schristos goto err_unrecog;
296377e23a2Schristos }
297377e23a2Schristos
29848596154Schristos /* Since the target type was defaulted, check them all in the hope
29948596154Schristos that one will be uniquely recognized. */
30048596154Schristos right_targ = NULL;
30148596154Schristos ar_right_targ = NULL;
30248596154Schristos match_targ = NULL;
30348596154Schristos best_match = 256;
30448596154Schristos best_count = 0;
30548596154Schristos match_count = 0;
30648596154Schristos ar_match_index = _bfd_target_vector_entries;
30748596154Schristos
308377e23a2Schristos for (target = bfd_target_vector; *target != NULL; target++)
309377e23a2Schristos {
310*1424dfb3Schristos void **high_water;
311377e23a2Schristos
312*1424dfb3Schristos /* The binary target matches anything, so don't return it when
313*1424dfb3Schristos searching. Don't match the plugin target if we have another
314*1424dfb3Schristos alternative since we want to properly set the input format
315*1424dfb3Schristos before allowing a plugin to claim the file. Also, don't
316*1424dfb3Schristos check the default target twice. */
317377e23a2Schristos if (*target == &binary_vec
318*1424dfb3Schristos #if BFD_SUPPORTS_PLUGINS
319*1424dfb3Schristos || (match_count != 0 && *target == &plugin_vec)
320*1424dfb3Schristos #endif
32107163879Schristos || (!abfd->target_defaulted && *target == save_targ))
322377e23a2Schristos continue;
323377e23a2Schristos
32448596154Schristos /* If we already tried a match, the bfd is modified and may
32548596154Schristos have sections attached, which will confuse the next
32648596154Schristos _bfd_check_format call. */
327*1424dfb3Schristos bfd_reinit (abfd, initial_section_id, cleanup);
328*1424dfb3Schristos /* Free bfd_alloc memory too. If we have matched and preserved
329*1424dfb3Schristos a target then the high water mark is that much higher. */
330*1424dfb3Schristos if (preserve_match.marker)
331*1424dfb3Schristos high_water = &preserve_match.marker;
332*1424dfb3Schristos else
333*1424dfb3Schristos high_water = &preserve.marker;
334*1424dfb3Schristos bfd_release (abfd, *high_water);
335*1424dfb3Schristos *high_water = bfd_alloc (abfd, 1);
33648596154Schristos
33748596154Schristos /* Change BFD's target temporarily. */
33848596154Schristos abfd->xvec = *target;
339377e23a2Schristos
340377e23a2Schristos if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
341377e23a2Schristos goto err_ret;
342377e23a2Schristos
343*1424dfb3Schristos cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
344*1424dfb3Schristos if (cleanup)
34548596154Schristos {
346*1424dfb3Schristos int match_priority = abfd->xvec->match_priority;
347c03b94e9Schristos #if BFD_SUPPORTS_PLUGINS
348c03b94e9Schristos /* If this object can be handled by a plugin, give that the
349c03b94e9Schristos lowest priority; objects both handled by a plugin and
350c03b94e9Schristos with an underlying object format will be claimed
351c03b94e9Schristos separately by the plugin. */
352c03b94e9Schristos if (*target == &plugin_vec)
353c03b94e9Schristos match_priority = (*target)->match_priority;
354c03b94e9Schristos #endif
355c03b94e9Schristos
35648596154Schristos if (abfd->format != bfd_archive
35748596154Schristos || (bfd_has_map (abfd)
35848596154Schristos && bfd_get_error () != bfd_error_wrong_object_format))
359377e23a2Schristos {
36048596154Schristos /* If this is the default target, accept it, even if
36148596154Schristos other targets might match. People who want those
36248596154Schristos other targets have to set the GNUTARGET variable. */
363*1424dfb3Schristos if (abfd->xvec == bfd_default_vector[0])
36448596154Schristos goto ok_ret;
365377e23a2Schristos
366377e23a2Schristos if (matching_vector)
367*1424dfb3Schristos matching_vector[match_count] = abfd->xvec;
368377e23a2Schristos match_count++;
36948596154Schristos
370c03b94e9Schristos if (match_priority < best_match)
371377e23a2Schristos {
372c03b94e9Schristos best_match = match_priority;
37348596154Schristos best_count = 0;
37448596154Schristos }
37507163879Schristos if (match_priority <= best_match)
37607163879Schristos {
37707163879Schristos /* This format checks out as ok! */
378*1424dfb3Schristos right_targ = abfd->xvec;
37948596154Schristos best_count++;
38048596154Schristos }
38107163879Schristos }
38248596154Schristos else
38348596154Schristos {
38448596154Schristos /* An archive with no armap or objects of the wrong
38548596154Schristos type. We want this target to match if we get no
38648596154Schristos better matches. */
387377e23a2Schristos if (ar_right_targ != bfd_default_vector[0])
388377e23a2Schristos ar_right_targ = *target;
389377e23a2Schristos if (matching_vector)
390377e23a2Schristos matching_vector[ar_match_index] = *target;
391377e23a2Schristos ar_match_index++;
392377e23a2Schristos }
39348596154Schristos
394*1424dfb3Schristos if (preserve_match.marker == NULL)
395*1424dfb3Schristos {
396*1424dfb3Schristos match_targ = abfd->xvec;
397*1424dfb3Schristos if (!bfd_preserve_save (abfd, &preserve_match, cleanup))
398377e23a2Schristos goto err_ret;
399*1424dfb3Schristos cleanup = NULL;
400377e23a2Schristos }
401*1424dfb3Schristos }
40248596154Schristos }
40348596154Schristos
40448596154Schristos if (best_count == 1)
40548596154Schristos match_count = 1;
406377e23a2Schristos
407377e23a2Schristos if (match_count == 0)
408377e23a2Schristos {
409377e23a2Schristos /* Try partial matches. */
410377e23a2Schristos right_targ = ar_right_targ;
411377e23a2Schristos
412377e23a2Schristos if (right_targ == bfd_default_vector[0])
413377e23a2Schristos {
414377e23a2Schristos match_count = 1;
415377e23a2Schristos }
416377e23a2Schristos else
417377e23a2Schristos {
418377e23a2Schristos match_count = ar_match_index - _bfd_target_vector_entries;
419377e23a2Schristos
420377e23a2Schristos if (matching_vector && match_count > 1)
421377e23a2Schristos memcpy (matching_vector,
422377e23a2Schristos matching_vector + _bfd_target_vector_entries,
423377e23a2Schristos sizeof (*matching_vector) * match_count);
424377e23a2Schristos }
425377e23a2Schristos }
426377e23a2Schristos
4277af5a897Schristos /* We have more than one equally good match. If any of the best
4287af5a897Schristos matches is a target in config.bfd targ_defvec or targ_selvecs,
4297af5a897Schristos choose it. */
430377e23a2Schristos if (match_count > 1)
431377e23a2Schristos {
432377e23a2Schristos const bfd_target * const *assoc = bfd_associated_vector;
433377e23a2Schristos
434377e23a2Schristos while ((right_targ = *assoc++) != NULL)
435377e23a2Schristos {
436377e23a2Schristos int i = match_count;
437377e23a2Schristos
438377e23a2Schristos while (--i >= 0)
4397af5a897Schristos if (matching_vector[i] == right_targ
4407af5a897Schristos && right_targ->match_priority <= best_match)
441377e23a2Schristos break;
442377e23a2Schristos
443377e23a2Schristos if (i >= 0)
444377e23a2Schristos {
445377e23a2Schristos match_count = 1;
446377e23a2Schristos break;
447377e23a2Schristos }
448377e23a2Schristos }
449377e23a2Schristos }
450377e23a2Schristos
4517af5a897Schristos /* We still have more than one equally good match, and at least some
4527af5a897Schristos of the targets support match priority. Choose the first of the
4537af5a897Schristos best matches. */
4545e098073Schristos if (matching_vector && match_count > 1 && best_count != match_count)
4557af5a897Schristos {
4567af5a897Schristos int i;
4577af5a897Schristos
4587af5a897Schristos for (i = 0; i < match_count; i++)
4597af5a897Schristos {
4607af5a897Schristos right_targ = matching_vector[i];
4617af5a897Schristos if (right_targ->match_priority <= best_match)
4627af5a897Schristos break;
4637af5a897Schristos }
4647af5a897Schristos match_count = 1;
4657af5a897Schristos }
4667af5a897Schristos
46748596154Schristos /* There is way too much undoing of half-known state here. We
46848596154Schristos really shouldn't iterate on live bfd's. Note that saving the
46948596154Schristos whole bfd and restoring it would be even worse; the first thing
47048596154Schristos you notice is that the cached bfd file position gets out of sync. */
471*1424dfb3Schristos if (preserve_match.marker != NULL)
472*1424dfb3Schristos cleanup = bfd_preserve_restore (abfd, &preserve_match);
47348596154Schristos
474377e23a2Schristos if (match_count == 1)
475377e23a2Schristos {
47648596154Schristos abfd->xvec = right_targ;
47748596154Schristos /* If we come out of the loop knowing that the last target that
47848596154Schristos matched is the one we want, then ABFD should still be in a usable
479*1424dfb3Schristos state (except possibly for XVEC). This is not just an
480*1424dfb3Schristos optimisation. In the case of plugins a match against the
481*1424dfb3Schristos plugin target can result in the bfd being changed such that
482*1424dfb3Schristos it no longer matches the plugin target, nor will it match
483*1424dfb3Schristos RIGHT_TARG again. */
48448596154Schristos if (match_targ != right_targ)
48548596154Schristos {
486*1424dfb3Schristos bfd_reinit (abfd, initial_section_id, cleanup);
487*1424dfb3Schristos bfd_release (abfd, preserve.marker);
48848596154Schristos if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
48948596154Schristos goto err_ret;
490*1424dfb3Schristos cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
491*1424dfb3Schristos BFD_ASSERT (cleanup != NULL);
49248596154Schristos }
493377e23a2Schristos
49448596154Schristos ok_ret:
495377e23a2Schristos /* If the file was opened for update, then `output_has_begun'
496377e23a2Schristos some time ago when the file was created. Do not recompute
497377e23a2Schristos sections sizes or alignments in _bfd_set_section_contents.
498377e23a2Schristos We can not set this flag until after checking the format,
499377e23a2Schristos because it will interfere with creation of BFD sections. */
500377e23a2Schristos if (abfd->direction == both_direction)
501377e23a2Schristos abfd->output_has_begun = TRUE;
502377e23a2Schristos
503377e23a2Schristos free (matching_vector);
504*1424dfb3Schristos if (preserve_match.marker != NULL)
505*1424dfb3Schristos bfd_preserve_finish (abfd, &preserve_match);
506*1424dfb3Schristos bfd_preserve_finish (abfd, &preserve);
50748596154Schristos
50848596154Schristos /* File position has moved, BTW. */
50948596154Schristos return TRUE;
510377e23a2Schristos }
511377e23a2Schristos
512377e23a2Schristos if (match_count == 0)
513377e23a2Schristos {
514377e23a2Schristos err_unrecog:
515377e23a2Schristos bfd_set_error (bfd_error_file_not_recognized);
516377e23a2Schristos err_ret:
517*1424dfb3Schristos if (cleanup)
518*1424dfb3Schristos cleanup (abfd);
519377e23a2Schristos abfd->xvec = save_targ;
520377e23a2Schristos abfd->format = bfd_unknown;
521377e23a2Schristos free (matching_vector);
522*1424dfb3Schristos if (preserve_match.marker != NULL)
523*1424dfb3Schristos bfd_preserve_finish (abfd, &preserve_match);
52448596154Schristos bfd_preserve_restore (abfd, &preserve);
525377e23a2Schristos return FALSE;
526377e23a2Schristos }
527377e23a2Schristos
52848596154Schristos /* Restore original target type and format. */
52948596154Schristos abfd->xvec = save_targ;
53048596154Schristos abfd->format = bfd_unknown;
531377e23a2Schristos bfd_set_error (bfd_error_file_ambiguously_recognized);
532377e23a2Schristos
533377e23a2Schristos if (matching)
534377e23a2Schristos {
535377e23a2Schristos *matching = (char **) matching_vector;
536377e23a2Schristos matching_vector[match_count] = NULL;
537377e23a2Schristos /* Return target names. This is a little nasty. Maybe we
538377e23a2Schristos should do another bfd_malloc? */
539377e23a2Schristos while (--match_count >= 0)
540377e23a2Schristos {
541377e23a2Schristos const char *name = matching_vector[match_count]->name;
542377e23a2Schristos *(const char **) &matching_vector[match_count] = name;
543377e23a2Schristos }
544377e23a2Schristos }
545*1424dfb3Schristos else
546*1424dfb3Schristos free (matching_vector);
547*1424dfb3Schristos if (cleanup)
548*1424dfb3Schristos cleanup (abfd);
549*1424dfb3Schristos if (preserve_match.marker != NULL)
550*1424dfb3Schristos bfd_preserve_finish (abfd, &preserve_match);
551*1424dfb3Schristos bfd_preserve_restore (abfd, &preserve);
552377e23a2Schristos return FALSE;
553377e23a2Schristos }
554377e23a2Schristos
555377e23a2Schristos /*
556377e23a2Schristos FUNCTION
557377e23a2Schristos bfd_set_format
558377e23a2Schristos
559377e23a2Schristos SYNOPSIS
560377e23a2Schristos bfd_boolean bfd_set_format (bfd *abfd, bfd_format format);
561377e23a2Schristos
562377e23a2Schristos DESCRIPTION
563377e23a2Schristos This function sets the file format of the BFD @var{abfd} to the
564377e23a2Schristos format @var{format}. If the target set in the BFD does not
565377e23a2Schristos support the format requested, the format is invalid, or the BFD
566377e23a2Schristos is not open for writing, then an error occurs.
567377e23a2Schristos */
568377e23a2Schristos
569377e23a2Schristos bfd_boolean
bfd_set_format(bfd * abfd,bfd_format format)570377e23a2Schristos bfd_set_format (bfd *abfd, bfd_format format)
571377e23a2Schristos {
572377e23a2Schristos if (bfd_read_p (abfd)
573377e23a2Schristos || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
574377e23a2Schristos {
575377e23a2Schristos bfd_set_error (bfd_error_invalid_operation);
576377e23a2Schristos return FALSE;
577377e23a2Schristos }
578377e23a2Schristos
579377e23a2Schristos if (abfd->format != bfd_unknown)
580377e23a2Schristos return abfd->format == format;
581377e23a2Schristos
582377e23a2Schristos /* Presume the answer is yes. */
583377e23a2Schristos abfd->format = format;
584377e23a2Schristos
585377e23a2Schristos if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
586377e23a2Schristos {
587377e23a2Schristos abfd->format = bfd_unknown;
588377e23a2Schristos return FALSE;
589377e23a2Schristos }
590377e23a2Schristos
591377e23a2Schristos return TRUE;
592377e23a2Schristos }
593377e23a2Schristos
594377e23a2Schristos /*
595377e23a2Schristos FUNCTION
596377e23a2Schristos bfd_format_string
597377e23a2Schristos
598377e23a2Schristos SYNOPSIS
599377e23a2Schristos const char *bfd_format_string (bfd_format format);
600377e23a2Schristos
601377e23a2Schristos DESCRIPTION
602377e23a2Schristos Return a pointer to a const string
603377e23a2Schristos <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
604377e23a2Schristos depending upon the value of @var{format}.
605377e23a2Schristos */
606377e23a2Schristos
607377e23a2Schristos const char *
bfd_format_string(bfd_format format)608377e23a2Schristos bfd_format_string (bfd_format format)
609377e23a2Schristos {
610377e23a2Schristos if (((int) format < (int) bfd_unknown)
611377e23a2Schristos || ((int) format >= (int) bfd_type_end))
612377e23a2Schristos return "invalid";
613377e23a2Schristos
614377e23a2Schristos switch (format)
615377e23a2Schristos {
616377e23a2Schristos case bfd_object:
617377e23a2Schristos return "object"; /* Linker/assembler/compiler output. */
618377e23a2Schristos case bfd_archive:
619377e23a2Schristos return "archive"; /* Object archive file. */
620377e23a2Schristos case bfd_core:
621377e23a2Schristos return "core"; /* Core dump. */
622377e23a2Schristos default:
623377e23a2Schristos return "unknown";
624377e23a2Schristos }
625377e23a2Schristos }
626