xref: /netbsd/external/gpl3/gdb.old/dist/bfd/format.c (revision 184b2d41)
1a1ba9ba4Schristos /* Generic BFD support for file formats.
2*184b2d41Schristos    Copyright (C) 1990-2020 Free Software Foundation, Inc.
3a1ba9ba4Schristos    Written by Cygnus Support.
4a1ba9ba4Schristos 
5a1ba9ba4Schristos    This file is part of BFD, the Binary File Descriptor library.
6a1ba9ba4Schristos 
7a1ba9ba4Schristos    This program is free software; you can redistribute it and/or modify
8a1ba9ba4Schristos    it under the terms of the GNU General Public License as published by
9a1ba9ba4Schristos    the Free Software Foundation; either version 3 of the License, or
10a1ba9ba4Schristos    (at your option) any later version.
11a1ba9ba4Schristos 
12a1ba9ba4Schristos    This program is distributed in the hope that it will be useful,
13a1ba9ba4Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14a1ba9ba4Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15a1ba9ba4Schristos    GNU General Public License for more details.
16a1ba9ba4Schristos 
17a1ba9ba4Schristos    You should have received a copy of the GNU General Public License
18a1ba9ba4Schristos    along with this program; if not, write to the Free Software
19a1ba9ba4Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20a1ba9ba4Schristos    MA 02110-1301, USA.  */
21a1ba9ba4Schristos 
22a1ba9ba4Schristos 
23a1ba9ba4Schristos /*
24a1ba9ba4Schristos SECTION
25a1ba9ba4Schristos 	File formats
26a1ba9ba4Schristos 
27a1ba9ba4Schristos 	A format is a BFD concept of high level file contents type. The
28a1ba9ba4Schristos 	formats supported by BFD are:
29a1ba9ba4Schristos 
30a1ba9ba4Schristos 	o <<bfd_object>>
31a1ba9ba4Schristos 
32a1ba9ba4Schristos 	The BFD may contain data, symbols, relocations and debug info.
33a1ba9ba4Schristos 
34a1ba9ba4Schristos 	o <<bfd_archive>>
35a1ba9ba4Schristos 
36a1ba9ba4Schristos 	The BFD contains other BFDs and an optional index.
37a1ba9ba4Schristos 
38a1ba9ba4Schristos 	o <<bfd_core>>
39a1ba9ba4Schristos 
40a1ba9ba4Schristos 	The BFD contains the result of an executable core dump.
41a1ba9ba4Schristos 
42a1ba9ba4Schristos SUBSECTION
43a1ba9ba4Schristos 	File format functions
44a1ba9ba4Schristos */
45a1ba9ba4Schristos 
46a1ba9ba4Schristos #include "sysdep.h"
47a1ba9ba4Schristos #include "bfd.h"
48a1ba9ba4Schristos #include "libbfd.h"
49a1ba9ba4Schristos 
50a1ba9ba4Schristos /* IMPORT from targets.c.  */
51a1ba9ba4Schristos extern const size_t _bfd_target_vector_entries;
52a1ba9ba4Schristos 
53a1ba9ba4Schristos /*
54a1ba9ba4Schristos FUNCTION
55a1ba9ba4Schristos 	bfd_check_format
56a1ba9ba4Schristos 
57a1ba9ba4Schristos SYNOPSIS
58a1ba9ba4Schristos 	bfd_boolean bfd_check_format (bfd *abfd, bfd_format format);
59a1ba9ba4Schristos 
60a1ba9ba4Schristos DESCRIPTION
61a1ba9ba4Schristos 	Verify if the file attached to the BFD @var{abfd} is compatible
62a1ba9ba4Schristos 	with the format @var{format} (i.e., one of <<bfd_object>>,
63a1ba9ba4Schristos 	<<bfd_archive>> or <<bfd_core>>).
64a1ba9ba4Schristos 
65a1ba9ba4Schristos 	If the BFD has been set to a specific target before the
66a1ba9ba4Schristos 	call, only the named target and format combination is
67a1ba9ba4Schristos 	checked. If the target has not been set, or has been set to
68a1ba9ba4Schristos 	<<default>>, then all the known target backends is
69a1ba9ba4Schristos 	interrogated to determine a match.  If the default target
70a1ba9ba4Schristos 	matches, it is used.  If not, exactly one target must recognize
71a1ba9ba4Schristos 	the file, or an error results.
72a1ba9ba4Schristos 
73a1ba9ba4Schristos 	The function returns <<TRUE>> on success, otherwise <<FALSE>>
74a1ba9ba4Schristos 	with one of the following error codes:
75a1ba9ba4Schristos 
76a1ba9ba4Schristos 	o <<bfd_error_invalid_operation>> -
77a1ba9ba4Schristos 	if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
78a1ba9ba4Schristos 	<<bfd_core>>.
79a1ba9ba4Schristos 
80a1ba9ba4Schristos 	o <<bfd_error_system_call>> -
81a1ba9ba4Schristos 	if an error occured during a read - even some file mismatches
82a1ba9ba4Schristos 	can cause bfd_error_system_calls.
83a1ba9ba4Schristos 
84a1ba9ba4Schristos 	o <<file_not_recognised>> -
85a1ba9ba4Schristos 	none of the backends recognised the file format.
86a1ba9ba4Schristos 
87a1ba9ba4Schristos 	o <<bfd_error_file_ambiguously_recognized>> -
88a1ba9ba4Schristos 	more than one backend recognised the file format.
89a1ba9ba4Schristos */
90a1ba9ba4Schristos 
91a1ba9ba4Schristos bfd_boolean
bfd_check_format(bfd * abfd,bfd_format format)92a1ba9ba4Schristos bfd_check_format (bfd *abfd, bfd_format format)
93a1ba9ba4Schristos {
94a1ba9ba4Schristos   return bfd_check_format_matches (abfd, format, NULL);
95a1ba9ba4Schristos }
96a1ba9ba4Schristos 
97a1ba9ba4Schristos struct bfd_preserve
98a1ba9ba4Schristos {
99a1ba9ba4Schristos   void *marker;
100a1ba9ba4Schristos   void *tdata;
101a1ba9ba4Schristos   flagword flags;
102a1ba9ba4Schristos   const struct bfd_arch_info *arch_info;
103a1ba9ba4Schristos   struct bfd_section *sections;
104a1ba9ba4Schristos   struct bfd_section *section_last;
105a1ba9ba4Schristos   unsigned int section_count;
106051580eeSchristos   unsigned int section_id;
107a1ba9ba4Schristos   struct bfd_hash_table section_htab;
10815d8e94aSchristos   const struct bfd_build_id *build_id;
109*184b2d41Schristos   bfd_cleanup cleanup;
110a1ba9ba4Schristos };
111a1ba9ba4Schristos 
112a1ba9ba4Schristos /* When testing an object for compatibility with a particular target
113a1ba9ba4Schristos    back-end, the back-end object_p function needs to set up certain
114a1ba9ba4Schristos    fields in the bfd on successfully recognizing the object.  This
115a1ba9ba4Schristos    typically happens in a piecemeal fashion, with failures possible at
116a1ba9ba4Schristos    many points.  On failure, the bfd is supposed to be restored to its
117a1ba9ba4Schristos    initial state, which is virtually impossible.  However, restoring a
118a1ba9ba4Schristos    subset of the bfd state works in practice.  This function stores
119a1ba9ba4Schristos    the subset.  */
120a1ba9ba4Schristos 
121a1ba9ba4Schristos static bfd_boolean
bfd_preserve_save(bfd * abfd,struct bfd_preserve * preserve,bfd_cleanup cleanup)122*184b2d41Schristos bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve,
123*184b2d41Schristos 		   bfd_cleanup cleanup)
124a1ba9ba4Schristos {
125a1ba9ba4Schristos   preserve->tdata = abfd->tdata.any;
126a1ba9ba4Schristos   preserve->arch_info = abfd->arch_info;
127a1ba9ba4Schristos   preserve->flags = abfd->flags;
128a1ba9ba4Schristos   preserve->sections = abfd->sections;
129a1ba9ba4Schristos   preserve->section_last = abfd->section_last;
130a1ba9ba4Schristos   preserve->section_count = abfd->section_count;
131051580eeSchristos   preserve->section_id = _bfd_section_id;
132a1ba9ba4Schristos   preserve->section_htab = abfd->section_htab;
133a1ba9ba4Schristos   preserve->marker = bfd_alloc (abfd, 1);
13415d8e94aSchristos   preserve->build_id = abfd->build_id;
135*184b2d41Schristos   preserve->cleanup = cleanup;
136a1ba9ba4Schristos   if (preserve->marker == NULL)
137a1ba9ba4Schristos     return FALSE;
138a1ba9ba4Schristos 
139a1ba9ba4Schristos   return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
140a1ba9ba4Schristos 			      sizeof (struct section_hash_entry));
141a1ba9ba4Schristos }
142a1ba9ba4Schristos 
143a1ba9ba4Schristos /* Clear out a subset of BFD state.  */
144a1ba9ba4Schristos 
145a1ba9ba4Schristos static void
bfd_reinit(bfd * abfd,unsigned int section_id,bfd_cleanup cleanup)146*184b2d41Schristos bfd_reinit (bfd *abfd, unsigned int section_id, bfd_cleanup cleanup)
147a1ba9ba4Schristos {
148*184b2d41Schristos   _bfd_section_id = section_id;
149*184b2d41Schristos   if (cleanup)
150*184b2d41Schristos     cleanup (abfd);
151a1ba9ba4Schristos   abfd->tdata.any = NULL;
152a1ba9ba4Schristos   abfd->arch_info = &bfd_default_arch_struct;
153a1ba9ba4Schristos   abfd->flags &= BFD_FLAGS_SAVED;
154a1ba9ba4Schristos   bfd_section_list_clear (abfd);
155a1ba9ba4Schristos }
156a1ba9ba4Schristos 
157a1ba9ba4Schristos /* Restores bfd state saved by bfd_preserve_save.  */
158a1ba9ba4Schristos 
159*184b2d41Schristos static bfd_cleanup
bfd_preserve_restore(bfd * abfd,struct bfd_preserve * preserve)160a1ba9ba4Schristos bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
161a1ba9ba4Schristos {
162a1ba9ba4Schristos   bfd_hash_table_free (&abfd->section_htab);
163a1ba9ba4Schristos 
164a1ba9ba4Schristos   abfd->tdata.any = preserve->tdata;
165a1ba9ba4Schristos   abfd->arch_info = preserve->arch_info;
166a1ba9ba4Schristos   abfd->flags = preserve->flags;
167a1ba9ba4Schristos   abfd->section_htab = preserve->section_htab;
168a1ba9ba4Schristos   abfd->sections = preserve->sections;
169a1ba9ba4Schristos   abfd->section_last = preserve->section_last;
170a1ba9ba4Schristos   abfd->section_count = preserve->section_count;
171051580eeSchristos   _bfd_section_id = preserve->section_id;
17215d8e94aSchristos   abfd->build_id = preserve->build_id;
173a1ba9ba4Schristos 
174a1ba9ba4Schristos   /* bfd_release frees all memory more recently bfd_alloc'd than
175a1ba9ba4Schristos      its arg, as well as its arg.  */
176a1ba9ba4Schristos   bfd_release (abfd, preserve->marker);
177a1ba9ba4Schristos   preserve->marker = NULL;
178*184b2d41Schristos   return preserve->cleanup;
179a1ba9ba4Schristos }
180a1ba9ba4Schristos 
181a1ba9ba4Schristos /* Called when the bfd state saved by bfd_preserve_save is no longer
182a1ba9ba4Schristos    needed.  */
183a1ba9ba4Schristos 
184a1ba9ba4Schristos static void
bfd_preserve_finish(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_preserve * preserve)185a1ba9ba4Schristos bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
186a1ba9ba4Schristos {
187*184b2d41Schristos   if (preserve->cleanup)
188*184b2d41Schristos     {
189*184b2d41Schristos       /* Run the cleanup, assuming that all it will need is the
190*184b2d41Schristos 	 tdata at the time the cleanup was returned.  */
191*184b2d41Schristos       void *tdata = abfd->tdata.any;
192*184b2d41Schristos       abfd->tdata.any = preserve->tdata;
193*184b2d41Schristos       preserve->cleanup (abfd);
194*184b2d41Schristos       abfd->tdata.any = tdata;
195*184b2d41Schristos     }
196a1ba9ba4Schristos   /* It would be nice to be able to free more memory here, eg. old
197a1ba9ba4Schristos      tdata, but that's not possible since these blocks are sitting
198a1ba9ba4Schristos      inside bfd_alloc'd memory.  The section hash is on a separate
199a1ba9ba4Schristos      objalloc.  */
200a1ba9ba4Schristos   bfd_hash_table_free (&preserve->section_htab);
201a1ba9ba4Schristos   preserve->marker = NULL;
202a1ba9ba4Schristos }
203a1ba9ba4Schristos 
204a1ba9ba4Schristos /*
205a1ba9ba4Schristos FUNCTION
206a1ba9ba4Schristos 	bfd_check_format_matches
207a1ba9ba4Schristos 
208a1ba9ba4Schristos SYNOPSIS
209a1ba9ba4Schristos 	bfd_boolean bfd_check_format_matches
210a1ba9ba4Schristos 	  (bfd *abfd, bfd_format format, char ***matching);
211a1ba9ba4Schristos 
212a1ba9ba4Schristos DESCRIPTION
213a1ba9ba4Schristos 	Like <<bfd_check_format>>, except when it returns FALSE with
214a1ba9ba4Schristos 	<<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>.  In that
215a1ba9ba4Schristos 	case, if @var{matching} is not NULL, it will be filled in with
216a1ba9ba4Schristos 	a NULL-terminated list of the names of the formats that matched,
217a1ba9ba4Schristos 	allocated with <<malloc>>.
218a1ba9ba4Schristos 	Then the user may choose a format and try again.
219a1ba9ba4Schristos 
220a1ba9ba4Schristos 	When done with the list that @var{matching} points to, the caller
221a1ba9ba4Schristos 	should free it.
222a1ba9ba4Schristos */
223a1ba9ba4Schristos 
224a1ba9ba4Schristos bfd_boolean
bfd_check_format_matches(bfd * abfd,bfd_format format,char *** matching)225a1ba9ba4Schristos bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
226a1ba9ba4Schristos {
227a1ba9ba4Schristos   extern const bfd_target binary_vec;
228b2396a7bSchristos #if BFD_SUPPORTS_PLUGINS
229b2396a7bSchristos   extern const bfd_target plugin_vec;
230b2396a7bSchristos #endif
231a1ba9ba4Schristos   const bfd_target * const *target;
232a1ba9ba4Schristos   const bfd_target **matching_vector = NULL;
233a1ba9ba4Schristos   const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
234a1ba9ba4Schristos   int match_count, best_count, best_match;
235a1ba9ba4Schristos   int ar_match_index;
236051580eeSchristos   unsigned int initial_section_id = _bfd_section_id;
237*184b2d41Schristos   struct bfd_preserve preserve, preserve_match;
238*184b2d41Schristos   bfd_cleanup cleanup = NULL;
239a1ba9ba4Schristos 
240a1ba9ba4Schristos   if (matching != NULL)
241a1ba9ba4Schristos     *matching = NULL;
242a1ba9ba4Schristos 
243a1ba9ba4Schristos   if (!bfd_read_p (abfd)
244a1ba9ba4Schristos       || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
245a1ba9ba4Schristos     {
246a1ba9ba4Schristos       bfd_set_error (bfd_error_invalid_operation);
247a1ba9ba4Schristos       return FALSE;
248a1ba9ba4Schristos     }
249a1ba9ba4Schristos 
250a1ba9ba4Schristos   if (abfd->format != bfd_unknown)
251a1ba9ba4Schristos     return abfd->format == format;
252a1ba9ba4Schristos 
253a1ba9ba4Schristos   if (matching != NULL || *bfd_associated_vector != NULL)
254a1ba9ba4Schristos     {
255*184b2d41Schristos       size_t amt;
256a1ba9ba4Schristos 
257a1ba9ba4Schristos       amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
258a1ba9ba4Schristos       matching_vector = (const bfd_target **) bfd_malloc (amt);
259a1ba9ba4Schristos       if (!matching_vector)
260a1ba9ba4Schristos 	return FALSE;
261a1ba9ba4Schristos     }
262a1ba9ba4Schristos 
263a1ba9ba4Schristos   /* Presume the answer is yes.  */
264a1ba9ba4Schristos   abfd->format = format;
265a1ba9ba4Schristos   save_targ = abfd->xvec;
266*184b2d41Schristos 
267*184b2d41Schristos   preserve_match.marker = NULL;
268*184b2d41Schristos   if (!bfd_preserve_save (abfd, &preserve, NULL))
269*184b2d41Schristos     goto err_ret;
270a1ba9ba4Schristos 
271a1ba9ba4Schristos   /* If the target type was explicitly specified, just check that target.  */
272a1ba9ba4Schristos   if (!abfd->target_defaulted)
273a1ba9ba4Schristos     {
274a1ba9ba4Schristos       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)	/* rewind! */
275a1ba9ba4Schristos 	goto err_ret;
276a1ba9ba4Schristos 
277*184b2d41Schristos       cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
278a1ba9ba4Schristos 
279*184b2d41Schristos       if (cleanup)
280a1ba9ba4Schristos 	goto ok_ret;
281a1ba9ba4Schristos 
282a1ba9ba4Schristos       /* For a long time the code has dropped through to check all
283a1ba9ba4Schristos 	 targets if the specified target was wrong.  I don't know why,
284a1ba9ba4Schristos 	 and I'm reluctant to change it.  However, in the case of an
285a1ba9ba4Schristos 	 archive, it can cause problems.  If the specified target does
286a1ba9ba4Schristos 	 not permit archives (e.g., the binary target), then we should
287a1ba9ba4Schristos 	 not allow some other target to recognize it as an archive, but
288a1ba9ba4Schristos 	 should instead allow the specified target to recognize it as an
289a1ba9ba4Schristos 	 object.  When I first made this change, it broke the PE target,
290a1ba9ba4Schristos 	 because the specified pei-i386 target did not recognize the
291a1ba9ba4Schristos 	 actual pe-i386 archive.  Since there may be other problems of
292a1ba9ba4Schristos 	 this sort, I changed this test to check only for the binary
293a1ba9ba4Schristos 	 target.  */
294a1ba9ba4Schristos       if (format == bfd_archive && save_targ == &binary_vec)
295a1ba9ba4Schristos 	goto err_unrecog;
296a1ba9ba4Schristos     }
297a1ba9ba4Schristos 
298a1ba9ba4Schristos   /* Since the target type was defaulted, check them all in the hope
299a1ba9ba4Schristos      that one will be uniquely recognized.  */
300a1ba9ba4Schristos   right_targ = NULL;
301a1ba9ba4Schristos   ar_right_targ = NULL;
302a1ba9ba4Schristos   match_targ = NULL;
303a1ba9ba4Schristos   best_match = 256;
304a1ba9ba4Schristos   best_count = 0;
305a1ba9ba4Schristos   match_count = 0;
306a1ba9ba4Schristos   ar_match_index = _bfd_target_vector_entries;
307a1ba9ba4Schristos 
308a1ba9ba4Schristos   for (target = bfd_target_vector; *target != NULL; target++)
309a1ba9ba4Schristos     {
310*184b2d41Schristos       void **high_water;
311a1ba9ba4Schristos 
312*184b2d41Schristos       /* The binary target matches anything, so don't return it when
313*184b2d41Schristos 	 searching.  Don't match the plugin target if we have another
314*184b2d41Schristos 	 alternative since we want to properly set the input format
315*184b2d41Schristos 	 before allowing a plugin to claim the file.  Also, don't
316*184b2d41Schristos 	 check the default target twice.  */
317a1ba9ba4Schristos       if (*target == &binary_vec
318*184b2d41Schristos #if BFD_SUPPORTS_PLUGINS
319*184b2d41Schristos 	  || (match_count != 0 && *target == &plugin_vec)
320*184b2d41Schristos #endif
321051580eeSchristos 	  || (!abfd->target_defaulted && *target == save_targ))
322a1ba9ba4Schristos 	continue;
323a1ba9ba4Schristos 
324a1ba9ba4Schristos       /* If we already tried a match, the bfd is modified and may
325a1ba9ba4Schristos 	 have sections attached, which will confuse the next
326a1ba9ba4Schristos 	 _bfd_check_format call.  */
327*184b2d41Schristos       bfd_reinit (abfd, initial_section_id, cleanup);
328*184b2d41Schristos       /* Free bfd_alloc memory too.  If we have matched and preserved
329*184b2d41Schristos 	 a target then the high water mark is that much higher.  */
330*184b2d41Schristos       if (preserve_match.marker)
331*184b2d41Schristos 	high_water = &preserve_match.marker;
332*184b2d41Schristos       else
333*184b2d41Schristos 	high_water = &preserve.marker;
334*184b2d41Schristos       bfd_release (abfd, *high_water);
335*184b2d41Schristos       *high_water = bfd_alloc (abfd, 1);
336a1ba9ba4Schristos 
337a1ba9ba4Schristos       /* Change BFD's target temporarily.  */
338a1ba9ba4Schristos       abfd->xvec = *target;
339a1ba9ba4Schristos 
340a1ba9ba4Schristos       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
341a1ba9ba4Schristos 	goto err_ret;
342a1ba9ba4Schristos 
343*184b2d41Schristos       cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
344*184b2d41Schristos       if (cleanup)
345a1ba9ba4Schristos 	{
346*184b2d41Schristos 	  int match_priority = abfd->xvec->match_priority;
347b2396a7bSchristos #if BFD_SUPPORTS_PLUGINS
348b2396a7bSchristos 	  /* If this object can be handled by a plugin, give that the
349b2396a7bSchristos 	     lowest priority; objects both handled by a plugin and
350b2396a7bSchristos 	     with an underlying object format will be claimed
351b2396a7bSchristos 	     separately by the plugin.  */
352b2396a7bSchristos 	  if (*target == &plugin_vec)
353b2396a7bSchristos 	    match_priority = (*target)->match_priority;
354b2396a7bSchristos #endif
355b2396a7bSchristos 
356a1ba9ba4Schristos 	  if (abfd->format != bfd_archive
357a1ba9ba4Schristos 	      || (bfd_has_map (abfd)
358a1ba9ba4Schristos 		  && bfd_get_error () != bfd_error_wrong_object_format))
359a1ba9ba4Schristos 	    {
360a1ba9ba4Schristos 	      /* If this is the default target, accept it, even if
361a1ba9ba4Schristos 		 other targets might match.  People who want those
362a1ba9ba4Schristos 		 other targets have to set the GNUTARGET variable.  */
363*184b2d41Schristos 	      if (abfd->xvec == bfd_default_vector[0])
364a1ba9ba4Schristos 		goto ok_ret;
365a1ba9ba4Schristos 
366a1ba9ba4Schristos 	      if (matching_vector)
367*184b2d41Schristos 		matching_vector[match_count] = abfd->xvec;
368a1ba9ba4Schristos 	      match_count++;
369a1ba9ba4Schristos 
370b2396a7bSchristos 	      if (match_priority < best_match)
371a1ba9ba4Schristos 		{
372b2396a7bSchristos 		  best_match = match_priority;
373a1ba9ba4Schristos 		  best_count = 0;
374a1ba9ba4Schristos 		}
375051580eeSchristos 	      if (match_priority <= best_match)
376051580eeSchristos 		{
377051580eeSchristos 		  /* This format checks out as ok!  */
378*184b2d41Schristos 		  right_targ = abfd->xvec;
379a1ba9ba4Schristos 		  best_count++;
380a1ba9ba4Schristos 		}
381051580eeSchristos 	    }
382a1ba9ba4Schristos 	  else
383a1ba9ba4Schristos 	    {
384a1ba9ba4Schristos 	      /* An archive with no armap or objects of the wrong
385a1ba9ba4Schristos 		 type.  We want this target to match if we get no
386a1ba9ba4Schristos 		 better matches.  */
387a1ba9ba4Schristos 	      if (ar_right_targ != bfd_default_vector[0])
388a1ba9ba4Schristos 		ar_right_targ = *target;
389a1ba9ba4Schristos 	      if (matching_vector)
390a1ba9ba4Schristos 		matching_vector[ar_match_index] = *target;
391a1ba9ba4Schristos 	      ar_match_index++;
392a1ba9ba4Schristos 	    }
393a1ba9ba4Schristos 
394*184b2d41Schristos 	  if (preserve_match.marker == NULL)
395*184b2d41Schristos 	    {
396*184b2d41Schristos 	      match_targ = abfd->xvec;
397*184b2d41Schristos 	      if (!bfd_preserve_save (abfd, &preserve_match, cleanup))
398a1ba9ba4Schristos 		goto err_ret;
399*184b2d41Schristos 	      cleanup = NULL;
400a1ba9ba4Schristos 	    }
401*184b2d41Schristos 	}
402a1ba9ba4Schristos     }
403a1ba9ba4Schristos 
404a1ba9ba4Schristos   if (best_count == 1)
405a1ba9ba4Schristos     match_count = 1;
406a1ba9ba4Schristos 
407a1ba9ba4Schristos   if (match_count == 0)
408a1ba9ba4Schristos     {
409a1ba9ba4Schristos       /* Try partial matches.  */
410a1ba9ba4Schristos       right_targ = ar_right_targ;
411a1ba9ba4Schristos 
412a1ba9ba4Schristos       if (right_targ == bfd_default_vector[0])
413a1ba9ba4Schristos 	{
414a1ba9ba4Schristos 	  match_count = 1;
415a1ba9ba4Schristos 	}
416a1ba9ba4Schristos       else
417a1ba9ba4Schristos 	{
418a1ba9ba4Schristos 	  match_count = ar_match_index - _bfd_target_vector_entries;
419a1ba9ba4Schristos 
420a1ba9ba4Schristos 	  if (matching_vector && match_count > 1)
421a1ba9ba4Schristos 	    memcpy (matching_vector,
422a1ba9ba4Schristos 		    matching_vector + _bfd_target_vector_entries,
423a1ba9ba4Schristos 		    sizeof (*matching_vector) * match_count);
424a1ba9ba4Schristos 	}
425a1ba9ba4Schristos     }
426a1ba9ba4Schristos 
427a1ba9ba4Schristos   /* We have more than one equally good match.  If any of the best
428a1ba9ba4Schristos      matches is a target in config.bfd targ_defvec or targ_selvecs,
429a1ba9ba4Schristos      choose it.  */
430a1ba9ba4Schristos   if (match_count > 1)
431a1ba9ba4Schristos     {
432a1ba9ba4Schristos       const bfd_target * const *assoc = bfd_associated_vector;
433a1ba9ba4Schristos 
434a1ba9ba4Schristos       while ((right_targ = *assoc++) != NULL)
435a1ba9ba4Schristos 	{
436a1ba9ba4Schristos 	  int i = match_count;
437a1ba9ba4Schristos 
438a1ba9ba4Schristos 	  while (--i >= 0)
439a1ba9ba4Schristos 	    if (matching_vector[i] == right_targ
440a1ba9ba4Schristos 		&& right_targ->match_priority <= best_match)
441a1ba9ba4Schristos 	      break;
442a1ba9ba4Schristos 
443a1ba9ba4Schristos 	  if (i >= 0)
444a1ba9ba4Schristos 	    {
445a1ba9ba4Schristos 	      match_count = 1;
446a1ba9ba4Schristos 	      break;
447a1ba9ba4Schristos 	    }
448a1ba9ba4Schristos 	}
449a1ba9ba4Schristos     }
450a1ba9ba4Schristos 
451a1ba9ba4Schristos   /* We still have more than one equally good match, and at least some
452a1ba9ba4Schristos      of the targets support match priority.  Choose the first of the
453a1ba9ba4Schristos      best matches.  */
454a1ba9ba4Schristos   if (matching_vector && match_count > 1 && best_count != match_count)
455a1ba9ba4Schristos     {
456a1ba9ba4Schristos       int i;
457a1ba9ba4Schristos 
458a1ba9ba4Schristos       for (i = 0; i < match_count; i++)
459a1ba9ba4Schristos 	{
460a1ba9ba4Schristos 	  right_targ = matching_vector[i];
461a1ba9ba4Schristos 	  if (right_targ->match_priority <= best_match)
462a1ba9ba4Schristos 	    break;
463a1ba9ba4Schristos 	}
464a1ba9ba4Schristos       match_count = 1;
465a1ba9ba4Schristos     }
466a1ba9ba4Schristos 
467a1ba9ba4Schristos   /* There is way too much undoing of half-known state here.  We
468a1ba9ba4Schristos      really shouldn't iterate on live bfd's.  Note that saving the
469a1ba9ba4Schristos      whole bfd and restoring it would be even worse; the first thing
470a1ba9ba4Schristos      you notice is that the cached bfd file position gets out of sync.  */
471*184b2d41Schristos   if (preserve_match.marker != NULL)
472*184b2d41Schristos     cleanup = bfd_preserve_restore (abfd, &preserve_match);
473a1ba9ba4Schristos 
474a1ba9ba4Schristos   if (match_count == 1)
475a1ba9ba4Schristos     {
476a1ba9ba4Schristos       abfd->xvec = right_targ;
477a1ba9ba4Schristos       /* If we come out of the loop knowing that the last target that
478a1ba9ba4Schristos 	 matched is the one we want, then ABFD should still be in a usable
479*184b2d41Schristos 	 state (except possibly for XVEC).  This is not just an
480*184b2d41Schristos 	 optimisation.  In the case of plugins a match against the
481*184b2d41Schristos 	 plugin target can result in the bfd being changed such that
482*184b2d41Schristos 	 it no longer matches the plugin target, nor will it match
483*184b2d41Schristos 	 RIGHT_TARG again.  */
484a1ba9ba4Schristos       if (match_targ != right_targ)
485a1ba9ba4Schristos 	{
486*184b2d41Schristos 	  bfd_reinit (abfd, initial_section_id, cleanup);
487*184b2d41Schristos 	  bfd_release (abfd, preserve.marker);
488a1ba9ba4Schristos 	  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
489a1ba9ba4Schristos 	    goto err_ret;
490*184b2d41Schristos 	  cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
491*184b2d41Schristos 	  BFD_ASSERT (cleanup != NULL);
492a1ba9ba4Schristos 	}
493a1ba9ba4Schristos 
494a1ba9ba4Schristos     ok_ret:
495a1ba9ba4Schristos       /* If the file was opened for update, then `output_has_begun'
496a1ba9ba4Schristos 	 some time ago when the file was created.  Do not recompute
497a1ba9ba4Schristos 	 sections sizes or alignments in _bfd_set_section_contents.
498a1ba9ba4Schristos 	 We can not set this flag until after checking the format,
499a1ba9ba4Schristos 	 because it will interfere with creation of BFD sections.  */
500a1ba9ba4Schristos       if (abfd->direction == both_direction)
501a1ba9ba4Schristos 	abfd->output_has_begun = TRUE;
502a1ba9ba4Schristos 
503a1ba9ba4Schristos       free (matching_vector);
504*184b2d41Schristos       if (preserve_match.marker != NULL)
505*184b2d41Schristos 	bfd_preserve_finish (abfd, &preserve_match);
506*184b2d41Schristos       bfd_preserve_finish (abfd, &preserve);
507a1ba9ba4Schristos 
508a1ba9ba4Schristos       /* File position has moved, BTW.  */
509a1ba9ba4Schristos       return TRUE;
510a1ba9ba4Schristos     }
511a1ba9ba4Schristos 
512a1ba9ba4Schristos   if (match_count == 0)
513a1ba9ba4Schristos     {
514a1ba9ba4Schristos     err_unrecog:
515a1ba9ba4Schristos       bfd_set_error (bfd_error_file_not_recognized);
516a1ba9ba4Schristos     err_ret:
517*184b2d41Schristos       if (cleanup)
518*184b2d41Schristos 	cleanup (abfd);
519a1ba9ba4Schristos       abfd->xvec = save_targ;
520a1ba9ba4Schristos       abfd->format = bfd_unknown;
521a1ba9ba4Schristos       free (matching_vector);
522*184b2d41Schristos       if (preserve_match.marker != NULL)
523*184b2d41Schristos 	bfd_preserve_finish (abfd, &preserve_match);
524a1ba9ba4Schristos       bfd_preserve_restore (abfd, &preserve);
525a1ba9ba4Schristos       return FALSE;
526a1ba9ba4Schristos     }
527a1ba9ba4Schristos 
528a1ba9ba4Schristos   /* Restore original target type and format.  */
529a1ba9ba4Schristos   abfd->xvec = save_targ;
530a1ba9ba4Schristos   abfd->format = bfd_unknown;
531a1ba9ba4Schristos   bfd_set_error (bfd_error_file_ambiguously_recognized);
532a1ba9ba4Schristos 
533a1ba9ba4Schristos   if (matching)
534a1ba9ba4Schristos     {
535a1ba9ba4Schristos       *matching = (char **) matching_vector;
536a1ba9ba4Schristos       matching_vector[match_count] = NULL;
537a1ba9ba4Schristos       /* Return target names.  This is a little nasty.  Maybe we
538a1ba9ba4Schristos 	 should do another bfd_malloc?  */
539a1ba9ba4Schristos       while (--match_count >= 0)
540a1ba9ba4Schristos 	{
541a1ba9ba4Schristos 	  const char *name = matching_vector[match_count]->name;
542a1ba9ba4Schristos 	  *(const char **) &matching_vector[match_count] = name;
543a1ba9ba4Schristos 	}
544a1ba9ba4Schristos     }
545*184b2d41Schristos   else
546*184b2d41Schristos     free (matching_vector);
547*184b2d41Schristos   if (cleanup)
548*184b2d41Schristos     cleanup (abfd);
549*184b2d41Schristos   if (preserve_match.marker != NULL)
550*184b2d41Schristos     bfd_preserve_finish (abfd, &preserve_match);
551*184b2d41Schristos   bfd_preserve_restore (abfd, &preserve);
552a1ba9ba4Schristos   return FALSE;
553a1ba9ba4Schristos }
554a1ba9ba4Schristos 
555a1ba9ba4Schristos /*
556a1ba9ba4Schristos FUNCTION
557a1ba9ba4Schristos 	bfd_set_format
558a1ba9ba4Schristos 
559a1ba9ba4Schristos SYNOPSIS
560a1ba9ba4Schristos 	bfd_boolean bfd_set_format (bfd *abfd, bfd_format format);
561a1ba9ba4Schristos 
562a1ba9ba4Schristos DESCRIPTION
563a1ba9ba4Schristos 	This function sets the file format of the BFD @var{abfd} to the
564a1ba9ba4Schristos 	format @var{format}. If the target set in the BFD does not
565a1ba9ba4Schristos 	support the format requested, the format is invalid, or the BFD
566a1ba9ba4Schristos 	is not open for writing, then an error occurs.
567a1ba9ba4Schristos */
568a1ba9ba4Schristos 
569a1ba9ba4Schristos bfd_boolean
bfd_set_format(bfd * abfd,bfd_format format)570a1ba9ba4Schristos bfd_set_format (bfd *abfd, bfd_format format)
571a1ba9ba4Schristos {
572a1ba9ba4Schristos   if (bfd_read_p (abfd)
573a1ba9ba4Schristos       || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
574a1ba9ba4Schristos     {
575a1ba9ba4Schristos       bfd_set_error (bfd_error_invalid_operation);
576a1ba9ba4Schristos       return FALSE;
577a1ba9ba4Schristos     }
578a1ba9ba4Schristos 
579a1ba9ba4Schristos   if (abfd->format != bfd_unknown)
580a1ba9ba4Schristos     return abfd->format == format;
581a1ba9ba4Schristos 
582a1ba9ba4Schristos   /* Presume the answer is yes.  */
583a1ba9ba4Schristos   abfd->format = format;
584a1ba9ba4Schristos 
585a1ba9ba4Schristos   if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
586a1ba9ba4Schristos     {
587a1ba9ba4Schristos       abfd->format = bfd_unknown;
588a1ba9ba4Schristos       return FALSE;
589a1ba9ba4Schristos     }
590a1ba9ba4Schristos 
591a1ba9ba4Schristos   return TRUE;
592a1ba9ba4Schristos }
593a1ba9ba4Schristos 
594a1ba9ba4Schristos /*
595a1ba9ba4Schristos FUNCTION
596a1ba9ba4Schristos 	bfd_format_string
597a1ba9ba4Schristos 
598a1ba9ba4Schristos SYNOPSIS
599a1ba9ba4Schristos 	const char *bfd_format_string (bfd_format format);
600a1ba9ba4Schristos 
601a1ba9ba4Schristos DESCRIPTION
602a1ba9ba4Schristos 	Return a pointer to a const string
603a1ba9ba4Schristos 	<<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
604a1ba9ba4Schristos 	depending upon the value of @var{format}.
605a1ba9ba4Schristos */
606a1ba9ba4Schristos 
607a1ba9ba4Schristos const char *
bfd_format_string(bfd_format format)608a1ba9ba4Schristos bfd_format_string (bfd_format format)
609a1ba9ba4Schristos {
610a1ba9ba4Schristos   if (((int) format < (int) bfd_unknown)
611a1ba9ba4Schristos       || ((int) format >= (int) bfd_type_end))
612a1ba9ba4Schristos     return "invalid";
613a1ba9ba4Schristos 
614a1ba9ba4Schristos   switch (format)
615a1ba9ba4Schristos     {
616a1ba9ba4Schristos     case bfd_object:
617a1ba9ba4Schristos       return "object";		/* Linker/assembler/compiler output.  */
618a1ba9ba4Schristos     case bfd_archive:
619a1ba9ba4Schristos       return "archive";		/* Object archive file.  */
620a1ba9ba4Schristos     case bfd_core:
621a1ba9ba4Schristos       return "core";		/* Core dump.  */
622a1ba9ba4Schristos     default:
623a1ba9ba4Schristos       return "unknown";
624a1ba9ba4Schristos     }
625a1ba9ba4Schristos }
626