1*3d8817e4Smiod /* Generic BFD support for file formats.
2*3d8817e4Smiod Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001, 2002,
3*3d8817e4Smiod 2003, 2005 Free Software Foundation, Inc.
4*3d8817e4Smiod Written by Cygnus Support.
5*3d8817e4Smiod
6*3d8817e4Smiod This file is part of BFD, the Binary File Descriptor library.
7*3d8817e4Smiod
8*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
9*3d8817e4Smiod it under the terms of the GNU General Public License as published by
10*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
11*3d8817e4Smiod (at your option) any later version.
12*3d8817e4Smiod
13*3d8817e4Smiod This program is distributed in the hope that it will be useful,
14*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
15*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*3d8817e4Smiod GNU General Public License for more details.
17*3d8817e4Smiod
18*3d8817e4Smiod You should have received a copy of the GNU General Public License
19*3d8817e4Smiod along with this program; if not, write to the Free Software
20*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21*3d8817e4Smiod
22*3d8817e4Smiod /*
23*3d8817e4Smiod SECTION
24*3d8817e4Smiod File formats
25*3d8817e4Smiod
26*3d8817e4Smiod A format is a BFD concept of high level file contents type. The
27*3d8817e4Smiod formats supported by BFD are:
28*3d8817e4Smiod
29*3d8817e4Smiod o <<bfd_object>>
30*3d8817e4Smiod
31*3d8817e4Smiod The BFD may contain data, symbols, relocations and debug info.
32*3d8817e4Smiod
33*3d8817e4Smiod o <<bfd_archive>>
34*3d8817e4Smiod
35*3d8817e4Smiod The BFD contains other BFDs and an optional index.
36*3d8817e4Smiod
37*3d8817e4Smiod o <<bfd_core>>
38*3d8817e4Smiod
39*3d8817e4Smiod The BFD contains the result of an executable core dump.
40*3d8817e4Smiod
41*3d8817e4Smiod SUBSECTION
42*3d8817e4Smiod File format functions
43*3d8817e4Smiod */
44*3d8817e4Smiod
45*3d8817e4Smiod #include "bfd.h"
46*3d8817e4Smiod #include "sysdep.h"
47*3d8817e4Smiod #include "libbfd.h"
48*3d8817e4Smiod
49*3d8817e4Smiod /* IMPORT from targets.c. */
50*3d8817e4Smiod extern const size_t _bfd_target_vector_entries;
51*3d8817e4Smiod
52*3d8817e4Smiod /*
53*3d8817e4Smiod FUNCTION
54*3d8817e4Smiod bfd_check_format
55*3d8817e4Smiod
56*3d8817e4Smiod SYNOPSIS
57*3d8817e4Smiod bfd_boolean bfd_check_format (bfd *abfd, bfd_format format);
58*3d8817e4Smiod
59*3d8817e4Smiod DESCRIPTION
60*3d8817e4Smiod Verify if the file attached to the BFD @var{abfd} is compatible
61*3d8817e4Smiod with the format @var{format} (i.e., one of <<bfd_object>>,
62*3d8817e4Smiod <<bfd_archive>> or <<bfd_core>>).
63*3d8817e4Smiod
64*3d8817e4Smiod If the BFD has been set to a specific target before the
65*3d8817e4Smiod call, only the named target and format combination is
66*3d8817e4Smiod checked. If the target has not been set, or has been set to
67*3d8817e4Smiod <<default>>, then all the known target backends is
68*3d8817e4Smiod interrogated to determine a match. If the default target
69*3d8817e4Smiod matches, it is used. If not, exactly one target must recognize
70*3d8817e4Smiod the file, or an error results.
71*3d8817e4Smiod
72*3d8817e4Smiod The function returns <<TRUE>> on success, otherwise <<FALSE>>
73*3d8817e4Smiod with one of the following error codes:
74*3d8817e4Smiod
75*3d8817e4Smiod o <<bfd_error_invalid_operation>> -
76*3d8817e4Smiod if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
77*3d8817e4Smiod <<bfd_core>>.
78*3d8817e4Smiod
79*3d8817e4Smiod o <<bfd_error_system_call>> -
80*3d8817e4Smiod if an error occured during a read - even some file mismatches
81*3d8817e4Smiod can cause bfd_error_system_calls.
82*3d8817e4Smiod
83*3d8817e4Smiod o <<file_not_recognised>> -
84*3d8817e4Smiod none of the backends recognised the file format.
85*3d8817e4Smiod
86*3d8817e4Smiod o <<bfd_error_file_ambiguously_recognized>> -
87*3d8817e4Smiod more than one backend recognised the file format.
88*3d8817e4Smiod */
89*3d8817e4Smiod
90*3d8817e4Smiod bfd_boolean
bfd_check_format(bfd * abfd,bfd_format format)91*3d8817e4Smiod bfd_check_format (bfd *abfd, bfd_format format)
92*3d8817e4Smiod {
93*3d8817e4Smiod return bfd_check_format_matches (abfd, format, NULL);
94*3d8817e4Smiod }
95*3d8817e4Smiod
96*3d8817e4Smiod /*
97*3d8817e4Smiod FUNCTION
98*3d8817e4Smiod bfd_check_format_matches
99*3d8817e4Smiod
100*3d8817e4Smiod SYNOPSIS
101*3d8817e4Smiod bfd_boolean bfd_check_format_matches
102*3d8817e4Smiod (bfd *abfd, bfd_format format, char ***matching);
103*3d8817e4Smiod
104*3d8817e4Smiod DESCRIPTION
105*3d8817e4Smiod Like <<bfd_check_format>>, except when it returns FALSE with
106*3d8817e4Smiod <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that
107*3d8817e4Smiod case, if @var{matching} is not NULL, it will be filled in with
108*3d8817e4Smiod a NULL-terminated list of the names of the formats that matched,
109*3d8817e4Smiod allocated with <<malloc>>.
110*3d8817e4Smiod Then the user may choose a format and try again.
111*3d8817e4Smiod
112*3d8817e4Smiod When done with the list that @var{matching} points to, the caller
113*3d8817e4Smiod should free it.
114*3d8817e4Smiod */
115*3d8817e4Smiod
116*3d8817e4Smiod bfd_boolean
bfd_check_format_matches(bfd * abfd,bfd_format format,char *** matching)117*3d8817e4Smiod bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
118*3d8817e4Smiod {
119*3d8817e4Smiod extern const bfd_target binary_vec;
120*3d8817e4Smiod const bfd_target * const *target;
121*3d8817e4Smiod const bfd_target **matching_vector = NULL;
122*3d8817e4Smiod const bfd_target *save_targ, *right_targ, *ar_right_targ;
123*3d8817e4Smiod int match_count;
124*3d8817e4Smiod int ar_match_index;
125*3d8817e4Smiod
126*3d8817e4Smiod if (!bfd_read_p (abfd)
127*3d8817e4Smiod || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
128*3d8817e4Smiod {
129*3d8817e4Smiod bfd_set_error (bfd_error_invalid_operation);
130*3d8817e4Smiod return FALSE;
131*3d8817e4Smiod }
132*3d8817e4Smiod
133*3d8817e4Smiod if (abfd->format != bfd_unknown)
134*3d8817e4Smiod return abfd->format == format;
135*3d8817e4Smiod
136*3d8817e4Smiod /* Since the target type was defaulted, check them
137*3d8817e4Smiod all in the hope that one will be uniquely recognized. */
138*3d8817e4Smiod save_targ = abfd->xvec;
139*3d8817e4Smiod match_count = 0;
140*3d8817e4Smiod ar_match_index = _bfd_target_vector_entries;
141*3d8817e4Smiod
142*3d8817e4Smiod if (matching)
143*3d8817e4Smiod {
144*3d8817e4Smiod bfd_size_type amt;
145*3d8817e4Smiod
146*3d8817e4Smiod *matching = NULL;
147*3d8817e4Smiod amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
148*3d8817e4Smiod matching_vector = bfd_malloc (amt);
149*3d8817e4Smiod if (!matching_vector)
150*3d8817e4Smiod return FALSE;
151*3d8817e4Smiod }
152*3d8817e4Smiod
153*3d8817e4Smiod right_targ = 0;
154*3d8817e4Smiod ar_right_targ = 0;
155*3d8817e4Smiod
156*3d8817e4Smiod /* Presume the answer is yes. */
157*3d8817e4Smiod abfd->format = format;
158*3d8817e4Smiod
159*3d8817e4Smiod /* If the target type was explicitly specified, just check that target. */
160*3d8817e4Smiod if (!abfd->target_defaulted)
161*3d8817e4Smiod {
162*3d8817e4Smiod if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) /* rewind! */
163*3d8817e4Smiod {
164*3d8817e4Smiod if (matching)
165*3d8817e4Smiod free (matching_vector);
166*3d8817e4Smiod return FALSE;
167*3d8817e4Smiod }
168*3d8817e4Smiod
169*3d8817e4Smiod right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
170*3d8817e4Smiod
171*3d8817e4Smiod if (right_targ)
172*3d8817e4Smiod {
173*3d8817e4Smiod abfd->xvec = right_targ; /* Set the target as returned. */
174*3d8817e4Smiod
175*3d8817e4Smiod if (matching)
176*3d8817e4Smiod free (matching_vector);
177*3d8817e4Smiod
178*3d8817e4Smiod /* If the file was opened for update, then `output_has_begun'
179*3d8817e4Smiod some time ago when the file was created. Do not recompute
180*3d8817e4Smiod sections sizes or alignments in _bfd_set_section_contents.
181*3d8817e4Smiod We can not set this flag until after checking the format,
182*3d8817e4Smiod because it will interfere with creation of BFD sections. */
183*3d8817e4Smiod if (abfd->direction == both_direction)
184*3d8817e4Smiod abfd->output_has_begun = TRUE;
185*3d8817e4Smiod
186*3d8817e4Smiod return TRUE; /* File position has moved, BTW. */
187*3d8817e4Smiod }
188*3d8817e4Smiod
189*3d8817e4Smiod /* For a long time the code has dropped through to check all
190*3d8817e4Smiod targets if the specified target was wrong. I don't know why,
191*3d8817e4Smiod and I'm reluctant to change it. However, in the case of an
192*3d8817e4Smiod archive, it can cause problems. If the specified target does
193*3d8817e4Smiod not permit archives (e.g., the binary target), then we should
194*3d8817e4Smiod not allow some other target to recognize it as an archive, but
195*3d8817e4Smiod should instead allow the specified target to recognize it as an
196*3d8817e4Smiod object. When I first made this change, it broke the PE target,
197*3d8817e4Smiod because the specified pei-i386 target did not recognize the
198*3d8817e4Smiod actual pe-i386 archive. Since there may be other problems of
199*3d8817e4Smiod this sort, I changed this test to check only for the binary
200*3d8817e4Smiod target. */
201*3d8817e4Smiod if (format == bfd_archive && save_targ == &binary_vec)
202*3d8817e4Smiod {
203*3d8817e4Smiod abfd->xvec = save_targ;
204*3d8817e4Smiod abfd->format = bfd_unknown;
205*3d8817e4Smiod
206*3d8817e4Smiod if (matching)
207*3d8817e4Smiod free (matching_vector);
208*3d8817e4Smiod
209*3d8817e4Smiod bfd_set_error (bfd_error_file_not_recognized);
210*3d8817e4Smiod
211*3d8817e4Smiod return FALSE;
212*3d8817e4Smiod }
213*3d8817e4Smiod }
214*3d8817e4Smiod
215*3d8817e4Smiod for (target = bfd_target_vector; *target != NULL; target++)
216*3d8817e4Smiod {
217*3d8817e4Smiod const bfd_target *temp;
218*3d8817e4Smiod bfd_error_type err;
219*3d8817e4Smiod
220*3d8817e4Smiod /* Don't check the default target twice. */
221*3d8817e4Smiod if (*target == &binary_vec
222*3d8817e4Smiod || (!abfd->target_defaulted && *target == save_targ))
223*3d8817e4Smiod continue;
224*3d8817e4Smiod
225*3d8817e4Smiod abfd->xvec = *target; /* Change BFD's target temporarily. */
226*3d8817e4Smiod
227*3d8817e4Smiod if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
228*3d8817e4Smiod {
229*3d8817e4Smiod if (matching)
230*3d8817e4Smiod free (matching_vector);
231*3d8817e4Smiod return FALSE;
232*3d8817e4Smiod }
233*3d8817e4Smiod
234*3d8817e4Smiod /* If _bfd_check_format neglects to set bfd_error, assume
235*3d8817e4Smiod bfd_error_wrong_format. We didn't used to even pay any
236*3d8817e4Smiod attention to bfd_error, so I suspect that some
237*3d8817e4Smiod _bfd_check_format might have this problem. */
238*3d8817e4Smiod bfd_set_error (bfd_error_wrong_format);
239*3d8817e4Smiod
240*3d8817e4Smiod temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
241*3d8817e4Smiod
242*3d8817e4Smiod if (temp)
243*3d8817e4Smiod {
244*3d8817e4Smiod /* This format checks out as ok! */
245*3d8817e4Smiod right_targ = temp;
246*3d8817e4Smiod
247*3d8817e4Smiod /* If this is the default target, accept it, even if other
248*3d8817e4Smiod targets might match. People who want those other targets
249*3d8817e4Smiod have to set the GNUTARGET variable. */
250*3d8817e4Smiod if (temp == bfd_default_vector[0])
251*3d8817e4Smiod {
252*3d8817e4Smiod match_count = 1;
253*3d8817e4Smiod break;
254*3d8817e4Smiod }
255*3d8817e4Smiod
256*3d8817e4Smiod if (matching)
257*3d8817e4Smiod matching_vector[match_count] = temp;
258*3d8817e4Smiod
259*3d8817e4Smiod match_count++;
260*3d8817e4Smiod }
261*3d8817e4Smiod else if ((err = bfd_get_error ()) == bfd_error_wrong_object_format
262*3d8817e4Smiod || err == bfd_error_file_ambiguously_recognized)
263*3d8817e4Smiod {
264*3d8817e4Smiod /* An archive with objects of the wrong type, or an
265*3d8817e4Smiod ambiguous match. We want this target to match if we get
266*3d8817e4Smiod no better matches. */
267*3d8817e4Smiod if (ar_right_targ != bfd_default_vector[0])
268*3d8817e4Smiod ar_right_targ = *target;
269*3d8817e4Smiod if (matching)
270*3d8817e4Smiod matching_vector[ar_match_index] = *target;
271*3d8817e4Smiod ar_match_index++;
272*3d8817e4Smiod }
273*3d8817e4Smiod else if (err != bfd_error_wrong_format)
274*3d8817e4Smiod {
275*3d8817e4Smiod abfd->xvec = save_targ;
276*3d8817e4Smiod abfd->format = bfd_unknown;
277*3d8817e4Smiod
278*3d8817e4Smiod if (matching)
279*3d8817e4Smiod free (matching_vector);
280*3d8817e4Smiod
281*3d8817e4Smiod return FALSE;
282*3d8817e4Smiod }
283*3d8817e4Smiod }
284*3d8817e4Smiod
285*3d8817e4Smiod if (match_count == 0)
286*3d8817e4Smiod {
287*3d8817e4Smiod /* Try partial matches. */
288*3d8817e4Smiod right_targ = ar_right_targ;
289*3d8817e4Smiod
290*3d8817e4Smiod if (right_targ == bfd_default_vector[0])
291*3d8817e4Smiod {
292*3d8817e4Smiod match_count = 1;
293*3d8817e4Smiod }
294*3d8817e4Smiod else
295*3d8817e4Smiod {
296*3d8817e4Smiod match_count = ar_match_index - _bfd_target_vector_entries;
297*3d8817e4Smiod
298*3d8817e4Smiod if (matching && match_count > 1)
299*3d8817e4Smiod memcpy (matching_vector,
300*3d8817e4Smiod matching_vector + _bfd_target_vector_entries,
301*3d8817e4Smiod sizeof (*matching_vector) * match_count);
302*3d8817e4Smiod }
303*3d8817e4Smiod }
304*3d8817e4Smiod
305*3d8817e4Smiod if (match_count > 1
306*3d8817e4Smiod && bfd_associated_vector != NULL
307*3d8817e4Smiod && matching)
308*3d8817e4Smiod {
309*3d8817e4Smiod const bfd_target * const *assoc = bfd_associated_vector;
310*3d8817e4Smiod
311*3d8817e4Smiod while ((right_targ = *assoc++) != NULL)
312*3d8817e4Smiod {
313*3d8817e4Smiod int i = match_count;
314*3d8817e4Smiod
315*3d8817e4Smiod while (--i >= 0)
316*3d8817e4Smiod if (matching_vector[i] == right_targ)
317*3d8817e4Smiod break;
318*3d8817e4Smiod
319*3d8817e4Smiod if (i >= 0)
320*3d8817e4Smiod {
321*3d8817e4Smiod match_count = 1;
322*3d8817e4Smiod break;
323*3d8817e4Smiod }
324*3d8817e4Smiod }
325*3d8817e4Smiod }
326*3d8817e4Smiod
327*3d8817e4Smiod if (match_count == 1)
328*3d8817e4Smiod {
329*3d8817e4Smiod abfd->xvec = right_targ; /* Change BFD's target permanently. */
330*3d8817e4Smiod
331*3d8817e4Smiod if (matching)
332*3d8817e4Smiod free (matching_vector);
333*3d8817e4Smiod
334*3d8817e4Smiod /* If the file was opened for update, then `output_has_begun'
335*3d8817e4Smiod some time ago when the file was created. Do not recompute
336*3d8817e4Smiod sections sizes or alignments in _bfd_set_section_contents.
337*3d8817e4Smiod We can not set this flag until after checking the format,
338*3d8817e4Smiod because it will interfere with creation of BFD sections. */
339*3d8817e4Smiod if (abfd->direction == both_direction)
340*3d8817e4Smiod abfd->output_has_begun = TRUE;
341*3d8817e4Smiod
342*3d8817e4Smiod return TRUE; /* File position has moved, BTW. */
343*3d8817e4Smiod }
344*3d8817e4Smiod
345*3d8817e4Smiod abfd->xvec = save_targ; /* Restore original target type. */
346*3d8817e4Smiod abfd->format = bfd_unknown; /* Restore original format. */
347*3d8817e4Smiod
348*3d8817e4Smiod if (match_count == 0)
349*3d8817e4Smiod {
350*3d8817e4Smiod bfd_set_error (bfd_error_file_not_recognized);
351*3d8817e4Smiod
352*3d8817e4Smiod if (matching)
353*3d8817e4Smiod free (matching_vector);
354*3d8817e4Smiod }
355*3d8817e4Smiod else
356*3d8817e4Smiod {
357*3d8817e4Smiod bfd_set_error (bfd_error_file_ambiguously_recognized);
358*3d8817e4Smiod
359*3d8817e4Smiod if (matching)
360*3d8817e4Smiod {
361*3d8817e4Smiod *matching = (char **) matching_vector;
362*3d8817e4Smiod matching_vector[match_count] = NULL;
363*3d8817e4Smiod /* Return target names. This is a little nasty. Maybe we
364*3d8817e4Smiod should do another bfd_malloc? */
365*3d8817e4Smiod while (--match_count >= 0)
366*3d8817e4Smiod {
367*3d8817e4Smiod const char *name = matching_vector[match_count]->name;
368*3d8817e4Smiod *(const char **) &matching_vector[match_count] = name;
369*3d8817e4Smiod }
370*3d8817e4Smiod }
371*3d8817e4Smiod }
372*3d8817e4Smiod
373*3d8817e4Smiod return FALSE;
374*3d8817e4Smiod }
375*3d8817e4Smiod
376*3d8817e4Smiod /*
377*3d8817e4Smiod FUNCTION
378*3d8817e4Smiod bfd_set_format
379*3d8817e4Smiod
380*3d8817e4Smiod SYNOPSIS
381*3d8817e4Smiod bfd_boolean bfd_set_format (bfd *abfd, bfd_format format);
382*3d8817e4Smiod
383*3d8817e4Smiod DESCRIPTION
384*3d8817e4Smiod This function sets the file format of the BFD @var{abfd} to the
385*3d8817e4Smiod format @var{format}. If the target set in the BFD does not
386*3d8817e4Smiod support the format requested, the format is invalid, or the BFD
387*3d8817e4Smiod is not open for writing, then an error occurs.
388*3d8817e4Smiod */
389*3d8817e4Smiod
390*3d8817e4Smiod bfd_boolean
bfd_set_format(bfd * abfd,bfd_format format)391*3d8817e4Smiod bfd_set_format (bfd *abfd, bfd_format format)
392*3d8817e4Smiod {
393*3d8817e4Smiod if (bfd_read_p (abfd)
394*3d8817e4Smiod || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
395*3d8817e4Smiod {
396*3d8817e4Smiod bfd_set_error (bfd_error_invalid_operation);
397*3d8817e4Smiod return FALSE;
398*3d8817e4Smiod }
399*3d8817e4Smiod
400*3d8817e4Smiod if (abfd->format != bfd_unknown)
401*3d8817e4Smiod return abfd->format == format;
402*3d8817e4Smiod
403*3d8817e4Smiod /* Presume the answer is yes. */
404*3d8817e4Smiod abfd->format = format;
405*3d8817e4Smiod
406*3d8817e4Smiod if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
407*3d8817e4Smiod {
408*3d8817e4Smiod abfd->format = bfd_unknown;
409*3d8817e4Smiod return FALSE;
410*3d8817e4Smiod }
411*3d8817e4Smiod
412*3d8817e4Smiod return TRUE;
413*3d8817e4Smiod }
414*3d8817e4Smiod
415*3d8817e4Smiod /*
416*3d8817e4Smiod FUNCTION
417*3d8817e4Smiod bfd_format_string
418*3d8817e4Smiod
419*3d8817e4Smiod SYNOPSIS
420*3d8817e4Smiod const char *bfd_format_string (bfd_format format);
421*3d8817e4Smiod
422*3d8817e4Smiod DESCRIPTION
423*3d8817e4Smiod Return a pointer to a const string
424*3d8817e4Smiod <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
425*3d8817e4Smiod depending upon the value of @var{format}.
426*3d8817e4Smiod */
427*3d8817e4Smiod
428*3d8817e4Smiod const char *
bfd_format_string(bfd_format format)429*3d8817e4Smiod bfd_format_string (bfd_format format)
430*3d8817e4Smiod {
431*3d8817e4Smiod if (((int) format < (int) bfd_unknown)
432*3d8817e4Smiod || ((int) format >= (int) bfd_type_end))
433*3d8817e4Smiod return "invalid";
434*3d8817e4Smiod
435*3d8817e4Smiod switch (format)
436*3d8817e4Smiod {
437*3d8817e4Smiod case bfd_object:
438*3d8817e4Smiod return "object"; /* Linker/assembler/compiler output. */
439*3d8817e4Smiod case bfd_archive:
440*3d8817e4Smiod return "archive"; /* Object archive file. */
441*3d8817e4Smiod case bfd_core:
442*3d8817e4Smiod return "core"; /* Core dump. */
443*3d8817e4Smiod default:
444*3d8817e4Smiod return "unknown";
445*3d8817e4Smiod }
446*3d8817e4Smiod }
447