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