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