15796c8dcSSimon Schubert /* Generic BFD support for file formats.
25796c8dcSSimon Schubert Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001, 2002,
35796c8dcSSimon Schubert 2003, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
45796c8dcSSimon Schubert Written by Cygnus Support.
55796c8dcSSimon Schubert
65796c8dcSSimon Schubert This file is part of BFD, the Binary File Descriptor library.
75796c8dcSSimon Schubert
85796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
95796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
105796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
115796c8dcSSimon Schubert (at your option) any later version.
125796c8dcSSimon Schubert
135796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
145796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
155796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
165796c8dcSSimon Schubert GNU General Public License for more details.
175796c8dcSSimon Schubert
185796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
195796c8dcSSimon Schubert along with this program; if not, write to the Free Software
205796c8dcSSimon Schubert Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
215796c8dcSSimon Schubert MA 02110-1301, USA. */
225796c8dcSSimon Schubert
235796c8dcSSimon Schubert
245796c8dcSSimon Schubert /*
255796c8dcSSimon Schubert SECTION
265796c8dcSSimon Schubert File formats
275796c8dcSSimon Schubert
285796c8dcSSimon Schubert A format is a BFD concept of high level file contents type. The
295796c8dcSSimon Schubert formats supported by BFD are:
305796c8dcSSimon Schubert
315796c8dcSSimon Schubert o <<bfd_object>>
325796c8dcSSimon Schubert
335796c8dcSSimon Schubert The BFD may contain data, symbols, relocations and debug info.
345796c8dcSSimon Schubert
355796c8dcSSimon Schubert o <<bfd_archive>>
365796c8dcSSimon Schubert
375796c8dcSSimon Schubert The BFD contains other BFDs and an optional index.
385796c8dcSSimon Schubert
395796c8dcSSimon Schubert o <<bfd_core>>
405796c8dcSSimon Schubert
415796c8dcSSimon Schubert The BFD contains the result of an executable core dump.
425796c8dcSSimon Schubert
435796c8dcSSimon Schubert SUBSECTION
445796c8dcSSimon Schubert File format functions
455796c8dcSSimon Schubert */
465796c8dcSSimon Schubert
475796c8dcSSimon Schubert #include "sysdep.h"
485796c8dcSSimon Schubert #include "bfd.h"
495796c8dcSSimon Schubert #include "libbfd.h"
505796c8dcSSimon Schubert
515796c8dcSSimon Schubert /* IMPORT from targets.c. */
525796c8dcSSimon Schubert extern const size_t _bfd_target_vector_entries;
535796c8dcSSimon Schubert
545796c8dcSSimon Schubert /*
555796c8dcSSimon Schubert FUNCTION
565796c8dcSSimon Schubert bfd_check_format
575796c8dcSSimon Schubert
585796c8dcSSimon Schubert SYNOPSIS
595796c8dcSSimon Schubert bfd_boolean bfd_check_format (bfd *abfd, bfd_format format);
605796c8dcSSimon Schubert
615796c8dcSSimon Schubert DESCRIPTION
625796c8dcSSimon Schubert Verify if the file attached to the BFD @var{abfd} is compatible
635796c8dcSSimon Schubert with the format @var{format} (i.e., one of <<bfd_object>>,
645796c8dcSSimon Schubert <<bfd_archive>> or <<bfd_core>>).
655796c8dcSSimon Schubert
665796c8dcSSimon Schubert If the BFD has been set to a specific target before the
675796c8dcSSimon Schubert call, only the named target and format combination is
685796c8dcSSimon Schubert checked. If the target has not been set, or has been set to
695796c8dcSSimon Schubert <<default>>, then all the known target backends is
705796c8dcSSimon Schubert interrogated to determine a match. If the default target
715796c8dcSSimon Schubert matches, it is used. If not, exactly one target must recognize
725796c8dcSSimon Schubert the file, or an error results.
735796c8dcSSimon Schubert
745796c8dcSSimon Schubert The function returns <<TRUE>> on success, otherwise <<FALSE>>
755796c8dcSSimon Schubert with one of the following error codes:
765796c8dcSSimon Schubert
775796c8dcSSimon Schubert o <<bfd_error_invalid_operation>> -
785796c8dcSSimon Schubert if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
795796c8dcSSimon Schubert <<bfd_core>>.
805796c8dcSSimon Schubert
815796c8dcSSimon Schubert o <<bfd_error_system_call>> -
825796c8dcSSimon Schubert if an error occured during a read - even some file mismatches
835796c8dcSSimon Schubert can cause bfd_error_system_calls.
845796c8dcSSimon Schubert
855796c8dcSSimon Schubert o <<file_not_recognised>> -
865796c8dcSSimon Schubert none of the backends recognised the file format.
875796c8dcSSimon Schubert
885796c8dcSSimon Schubert o <<bfd_error_file_ambiguously_recognized>> -
895796c8dcSSimon Schubert more than one backend recognised the file format.
905796c8dcSSimon Schubert */
915796c8dcSSimon Schubert
925796c8dcSSimon Schubert bfd_boolean
bfd_check_format(bfd * abfd,bfd_format format)935796c8dcSSimon Schubert bfd_check_format (bfd *abfd, bfd_format format)
945796c8dcSSimon Schubert {
955796c8dcSSimon Schubert return bfd_check_format_matches (abfd, format, NULL);
965796c8dcSSimon Schubert }
975796c8dcSSimon Schubert
98*ef5ccd6cSJohn Marino struct bfd_preserve
99*ef5ccd6cSJohn Marino {
100*ef5ccd6cSJohn Marino void *marker;
101*ef5ccd6cSJohn Marino void *tdata;
102*ef5ccd6cSJohn Marino flagword flags;
103*ef5ccd6cSJohn Marino const struct bfd_arch_info *arch_info;
104*ef5ccd6cSJohn Marino struct bfd_section *sections;
105*ef5ccd6cSJohn Marino struct bfd_section *section_last;
106*ef5ccd6cSJohn Marino unsigned int section_count;
107*ef5ccd6cSJohn Marino struct bfd_hash_table section_htab;
108*ef5ccd6cSJohn Marino };
109*ef5ccd6cSJohn Marino
110*ef5ccd6cSJohn Marino /* When testing an object for compatibility with a particular target
111*ef5ccd6cSJohn Marino back-end, the back-end object_p function needs to set up certain
112*ef5ccd6cSJohn Marino fields in the bfd on successfully recognizing the object. This
113*ef5ccd6cSJohn Marino typically happens in a piecemeal fashion, with failures possible at
114*ef5ccd6cSJohn Marino many points. On failure, the bfd is supposed to be restored to its
115*ef5ccd6cSJohn Marino initial state, which is virtually impossible. However, restoring a
116*ef5ccd6cSJohn Marino subset of the bfd state works in practice. This function stores
117*ef5ccd6cSJohn Marino the subset. */
118*ef5ccd6cSJohn Marino
119*ef5ccd6cSJohn Marino static bfd_boolean
bfd_preserve_save(bfd * abfd,struct bfd_preserve * preserve)120*ef5ccd6cSJohn Marino bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve)
121*ef5ccd6cSJohn Marino {
122*ef5ccd6cSJohn Marino preserve->tdata = abfd->tdata.any;
123*ef5ccd6cSJohn Marino preserve->arch_info = abfd->arch_info;
124*ef5ccd6cSJohn Marino preserve->flags = abfd->flags;
125*ef5ccd6cSJohn Marino preserve->sections = abfd->sections;
126*ef5ccd6cSJohn Marino preserve->section_last = abfd->section_last;
127*ef5ccd6cSJohn Marino preserve->section_count = abfd->section_count;
128*ef5ccd6cSJohn Marino preserve->section_htab = abfd->section_htab;
129*ef5ccd6cSJohn Marino preserve->marker = bfd_alloc (abfd, 1);
130*ef5ccd6cSJohn Marino if (preserve->marker == NULL)
131*ef5ccd6cSJohn Marino return FALSE;
132*ef5ccd6cSJohn Marino
133*ef5ccd6cSJohn Marino return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
134*ef5ccd6cSJohn Marino sizeof (struct section_hash_entry));
135*ef5ccd6cSJohn Marino }
136*ef5ccd6cSJohn Marino
137*ef5ccd6cSJohn Marino /* Clear out a subset of BFD state. */
138*ef5ccd6cSJohn Marino
139*ef5ccd6cSJohn Marino static void
bfd_reinit(bfd * abfd)140*ef5ccd6cSJohn Marino bfd_reinit (bfd *abfd)
141*ef5ccd6cSJohn Marino {
142*ef5ccd6cSJohn Marino abfd->tdata.any = NULL;
143*ef5ccd6cSJohn Marino abfd->arch_info = &bfd_default_arch_struct;
144*ef5ccd6cSJohn Marino abfd->flags &= BFD_FLAGS_SAVED;
145*ef5ccd6cSJohn Marino bfd_section_list_clear (abfd);
146*ef5ccd6cSJohn Marino }
147*ef5ccd6cSJohn Marino
148*ef5ccd6cSJohn Marino /* Restores bfd state saved by bfd_preserve_save. */
149*ef5ccd6cSJohn Marino
150*ef5ccd6cSJohn Marino static void
bfd_preserve_restore(bfd * abfd,struct bfd_preserve * preserve)151*ef5ccd6cSJohn Marino bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
152*ef5ccd6cSJohn Marino {
153*ef5ccd6cSJohn Marino bfd_hash_table_free (&abfd->section_htab);
154*ef5ccd6cSJohn Marino
155*ef5ccd6cSJohn Marino abfd->tdata.any = preserve->tdata;
156*ef5ccd6cSJohn Marino abfd->arch_info = preserve->arch_info;
157*ef5ccd6cSJohn Marino abfd->flags = preserve->flags;
158*ef5ccd6cSJohn Marino abfd->section_htab = preserve->section_htab;
159*ef5ccd6cSJohn Marino abfd->sections = preserve->sections;
160*ef5ccd6cSJohn Marino abfd->section_last = preserve->section_last;
161*ef5ccd6cSJohn Marino abfd->section_count = preserve->section_count;
162*ef5ccd6cSJohn Marino
163*ef5ccd6cSJohn Marino /* bfd_release frees all memory more recently bfd_alloc'd than
164*ef5ccd6cSJohn Marino its arg, as well as its arg. */
165*ef5ccd6cSJohn Marino bfd_release (abfd, preserve->marker);
166*ef5ccd6cSJohn Marino preserve->marker = NULL;
167*ef5ccd6cSJohn Marino }
168*ef5ccd6cSJohn Marino
169*ef5ccd6cSJohn Marino /* Called when the bfd state saved by bfd_preserve_save is no longer
170*ef5ccd6cSJohn Marino needed. */
171*ef5ccd6cSJohn Marino
172*ef5ccd6cSJohn Marino static void
bfd_preserve_finish(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_preserve * preserve)173*ef5ccd6cSJohn Marino bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
174*ef5ccd6cSJohn Marino {
175*ef5ccd6cSJohn Marino /* It would be nice to be able to free more memory here, eg. old
176*ef5ccd6cSJohn Marino tdata, but that's not possible since these blocks are sitting
177*ef5ccd6cSJohn Marino inside bfd_alloc'd memory. The section hash is on a separate
178*ef5ccd6cSJohn Marino objalloc. */
179*ef5ccd6cSJohn Marino bfd_hash_table_free (&preserve->section_htab);
180*ef5ccd6cSJohn Marino preserve->marker = NULL;
181*ef5ccd6cSJohn Marino }
182*ef5ccd6cSJohn Marino
1835796c8dcSSimon Schubert /*
1845796c8dcSSimon Schubert FUNCTION
1855796c8dcSSimon Schubert bfd_check_format_matches
1865796c8dcSSimon Schubert
1875796c8dcSSimon Schubert SYNOPSIS
1885796c8dcSSimon Schubert bfd_boolean bfd_check_format_matches
1895796c8dcSSimon Schubert (bfd *abfd, bfd_format format, char ***matching);
1905796c8dcSSimon Schubert
1915796c8dcSSimon Schubert DESCRIPTION
1925796c8dcSSimon Schubert Like <<bfd_check_format>>, except when it returns FALSE with
1935796c8dcSSimon Schubert <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that
1945796c8dcSSimon Schubert case, if @var{matching} is not NULL, it will be filled in with
1955796c8dcSSimon Schubert a NULL-terminated list of the names of the formats that matched,
1965796c8dcSSimon Schubert allocated with <<malloc>>.
1975796c8dcSSimon Schubert Then the user may choose a format and try again.
1985796c8dcSSimon Schubert
1995796c8dcSSimon Schubert When done with the list that @var{matching} points to, the caller
2005796c8dcSSimon Schubert should free it.
2015796c8dcSSimon Schubert */
2025796c8dcSSimon Schubert
2035796c8dcSSimon Schubert bfd_boolean
bfd_check_format_matches(bfd * abfd,bfd_format format,char *** matching)2045796c8dcSSimon Schubert bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
2055796c8dcSSimon Schubert {
2065796c8dcSSimon Schubert extern const bfd_target binary_vec;
2075796c8dcSSimon Schubert const bfd_target * const *target;
2085796c8dcSSimon Schubert const bfd_target **matching_vector = NULL;
209a45ae5f8SJohn Marino const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
210a45ae5f8SJohn Marino int match_count, best_count, best_match;
2115796c8dcSSimon Schubert int ar_match_index;
212*ef5ccd6cSJohn Marino struct bfd_preserve preserve;
2135796c8dcSSimon Schubert
2145796c8dcSSimon Schubert if (matching != NULL)
2155796c8dcSSimon Schubert *matching = NULL;
2165796c8dcSSimon Schubert
2175796c8dcSSimon Schubert if (!bfd_read_p (abfd)
2185796c8dcSSimon Schubert || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
2195796c8dcSSimon Schubert {
2205796c8dcSSimon Schubert bfd_set_error (bfd_error_invalid_operation);
2215796c8dcSSimon Schubert return FALSE;
2225796c8dcSSimon Schubert }
2235796c8dcSSimon Schubert
2245796c8dcSSimon Schubert if (abfd->format != bfd_unknown)
2255796c8dcSSimon Schubert return abfd->format == format;
2265796c8dcSSimon Schubert
2275796c8dcSSimon Schubert if (matching != NULL || *bfd_associated_vector != NULL)
2285796c8dcSSimon Schubert {
2295796c8dcSSimon Schubert bfd_size_type amt;
2305796c8dcSSimon Schubert
2315796c8dcSSimon Schubert amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
2325796c8dcSSimon Schubert matching_vector = (const bfd_target **) bfd_malloc (amt);
2335796c8dcSSimon Schubert if (!matching_vector)
2345796c8dcSSimon Schubert return FALSE;
2355796c8dcSSimon Schubert }
2365796c8dcSSimon Schubert
2375796c8dcSSimon Schubert /* Presume the answer is yes. */
2385796c8dcSSimon Schubert abfd->format = format;
239*ef5ccd6cSJohn Marino save_targ = abfd->xvec;
240*ef5ccd6cSJohn Marino preserve.marker = NULL;
2415796c8dcSSimon Schubert
2425796c8dcSSimon Schubert /* If the target type was explicitly specified, just check that target. */
2435796c8dcSSimon Schubert if (!abfd->target_defaulted)
2445796c8dcSSimon Schubert {
2455796c8dcSSimon Schubert if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) /* rewind! */
2465796c8dcSSimon Schubert goto err_ret;
2475796c8dcSSimon Schubert
2485796c8dcSSimon Schubert right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
2495796c8dcSSimon Schubert
2505796c8dcSSimon Schubert if (right_targ)
2515796c8dcSSimon Schubert goto ok_ret;
2525796c8dcSSimon Schubert
2535796c8dcSSimon Schubert /* For a long time the code has dropped through to check all
2545796c8dcSSimon Schubert targets if the specified target was wrong. I don't know why,
2555796c8dcSSimon Schubert and I'm reluctant to change it. However, in the case of an
2565796c8dcSSimon Schubert archive, it can cause problems. If the specified target does
2575796c8dcSSimon Schubert not permit archives (e.g., the binary target), then we should
2585796c8dcSSimon Schubert not allow some other target to recognize it as an archive, but
2595796c8dcSSimon Schubert should instead allow the specified target to recognize it as an
2605796c8dcSSimon Schubert object. When I first made this change, it broke the PE target,
2615796c8dcSSimon Schubert because the specified pei-i386 target did not recognize the
2625796c8dcSSimon Schubert actual pe-i386 archive. Since there may be other problems of
2635796c8dcSSimon Schubert this sort, I changed this test to check only for the binary
2645796c8dcSSimon Schubert target. */
2655796c8dcSSimon Schubert if (format == bfd_archive && save_targ == &binary_vec)
2665796c8dcSSimon Schubert goto err_unrecog;
2675796c8dcSSimon Schubert }
2685796c8dcSSimon Schubert
269*ef5ccd6cSJohn Marino /* Since the target type was defaulted, check them all in the hope
270*ef5ccd6cSJohn Marino that one will be uniquely recognized. */
271*ef5ccd6cSJohn Marino right_targ = NULL;
272*ef5ccd6cSJohn Marino ar_right_targ = NULL;
273*ef5ccd6cSJohn Marino match_targ = NULL;
274*ef5ccd6cSJohn Marino best_match = 256;
275*ef5ccd6cSJohn Marino best_count = 0;
276*ef5ccd6cSJohn Marino match_count = 0;
277*ef5ccd6cSJohn Marino ar_match_index = _bfd_target_vector_entries;
278*ef5ccd6cSJohn Marino
2795796c8dcSSimon Schubert for (target = bfd_target_vector; *target != NULL; target++)
2805796c8dcSSimon Schubert {
2815796c8dcSSimon Schubert const bfd_target *temp;
2825796c8dcSSimon Schubert
2835796c8dcSSimon Schubert /* Don't check the default target twice. */
2845796c8dcSSimon Schubert if (*target == &binary_vec
285a45ae5f8SJohn Marino || (!abfd->target_defaulted && *target == save_targ)
286a45ae5f8SJohn Marino || (*target)->match_priority > best_match)
2875796c8dcSSimon Schubert continue;
2885796c8dcSSimon Schubert
289*ef5ccd6cSJohn Marino /* If we already tried a match, the bfd is modified and may
290*ef5ccd6cSJohn Marino have sections attached, which will confuse the next
291*ef5ccd6cSJohn Marino _bfd_check_format call. */
292*ef5ccd6cSJohn Marino bfd_reinit (abfd);
293*ef5ccd6cSJohn Marino
294*ef5ccd6cSJohn Marino /* Change BFD's target temporarily. */
295*ef5ccd6cSJohn Marino abfd->xvec = *target;
2965796c8dcSSimon Schubert
2975796c8dcSSimon Schubert if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2985796c8dcSSimon Schubert goto err_ret;
2995796c8dcSSimon Schubert
3005796c8dcSSimon Schubert /* If _bfd_check_format neglects to set bfd_error, assume
3015796c8dcSSimon Schubert bfd_error_wrong_format. We didn't used to even pay any
3025796c8dcSSimon Schubert attention to bfd_error, so I suspect that some
3035796c8dcSSimon Schubert _bfd_check_format might have this problem. */
3045796c8dcSSimon Schubert bfd_set_error (bfd_error_wrong_format);
3055796c8dcSSimon Schubert
3065796c8dcSSimon Schubert temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
307a45ae5f8SJohn Marino if (temp)
308*ef5ccd6cSJohn Marino {
309a45ae5f8SJohn Marino match_targ = temp;
310*ef5ccd6cSJohn Marino if (preserve.marker != NULL)
311*ef5ccd6cSJohn Marino bfd_preserve_finish (abfd, &preserve);
3125796c8dcSSimon Schubert
313*ef5ccd6cSJohn Marino if (abfd->format != bfd_archive
314*ef5ccd6cSJohn Marino || (bfd_has_map (abfd)
315*ef5ccd6cSJohn Marino && bfd_get_error () != bfd_error_wrong_object_format))
3165796c8dcSSimon Schubert {
3175796c8dcSSimon Schubert /* This format checks out as ok! */
3185796c8dcSSimon Schubert right_targ = temp;
3195796c8dcSSimon Schubert
320*ef5ccd6cSJohn Marino /* If this is the default target, accept it, even if
321*ef5ccd6cSJohn Marino other targets might match. People who want those
322*ef5ccd6cSJohn Marino other targets have to set the GNUTARGET variable. */
3235796c8dcSSimon Schubert if (temp == bfd_default_vector[0])
324a45ae5f8SJohn Marino goto ok_ret;
3255796c8dcSSimon Schubert
3265796c8dcSSimon Schubert if (matching_vector)
3275796c8dcSSimon Schubert matching_vector[match_count] = temp;
3285796c8dcSSimon Schubert match_count++;
329a45ae5f8SJohn Marino
330a45ae5f8SJohn Marino if (temp->match_priority < best_match)
331a45ae5f8SJohn Marino {
332a45ae5f8SJohn Marino best_match = temp->match_priority;
333a45ae5f8SJohn Marino best_count = 0;
334a45ae5f8SJohn Marino }
335a45ae5f8SJohn Marino best_count++;
3365796c8dcSSimon Schubert }
337*ef5ccd6cSJohn Marino else
3385796c8dcSSimon Schubert {
339*ef5ccd6cSJohn Marino /* An archive with no armap or objects of the wrong
340*ef5ccd6cSJohn Marino type. We want this target to match if we get no
341*ef5ccd6cSJohn Marino better matches. */
3425796c8dcSSimon Schubert if (ar_right_targ != bfd_default_vector[0])
3435796c8dcSSimon Schubert ar_right_targ = *target;
3445796c8dcSSimon Schubert if (matching_vector)
3455796c8dcSSimon Schubert matching_vector[ar_match_index] = *target;
3465796c8dcSSimon Schubert ar_match_index++;
3475796c8dcSSimon Schubert }
348*ef5ccd6cSJohn Marino
349*ef5ccd6cSJohn Marino if (!bfd_preserve_save (abfd, &preserve))
350*ef5ccd6cSJohn Marino goto err_ret;
351*ef5ccd6cSJohn Marino }
352*ef5ccd6cSJohn Marino else if (bfd_get_error () != bfd_error_wrong_format)
3535796c8dcSSimon Schubert goto err_ret;
3545796c8dcSSimon Schubert }
3555796c8dcSSimon Schubert
356a45ae5f8SJohn Marino if (best_count == 1)
357a45ae5f8SJohn Marino match_count = 1;
358a45ae5f8SJohn Marino
3595796c8dcSSimon Schubert if (match_count == 0)
3605796c8dcSSimon Schubert {
3615796c8dcSSimon Schubert /* Try partial matches. */
3625796c8dcSSimon Schubert right_targ = ar_right_targ;
3635796c8dcSSimon Schubert
3645796c8dcSSimon Schubert if (right_targ == bfd_default_vector[0])
3655796c8dcSSimon Schubert {
3665796c8dcSSimon Schubert match_count = 1;
3675796c8dcSSimon Schubert }
3685796c8dcSSimon Schubert else
3695796c8dcSSimon Schubert {
3705796c8dcSSimon Schubert match_count = ar_match_index - _bfd_target_vector_entries;
3715796c8dcSSimon Schubert
3725796c8dcSSimon Schubert if (matching_vector && match_count > 1)
3735796c8dcSSimon Schubert memcpy (matching_vector,
3745796c8dcSSimon Schubert matching_vector + _bfd_target_vector_entries,
3755796c8dcSSimon Schubert sizeof (*matching_vector) * match_count);
3765796c8dcSSimon Schubert }
3775796c8dcSSimon Schubert }
3785796c8dcSSimon Schubert
3795796c8dcSSimon Schubert if (match_count > 1)
3805796c8dcSSimon Schubert {
3815796c8dcSSimon Schubert const bfd_target * const *assoc = bfd_associated_vector;
3825796c8dcSSimon Schubert
3835796c8dcSSimon Schubert while ((right_targ = *assoc++) != NULL)
3845796c8dcSSimon Schubert {
3855796c8dcSSimon Schubert int i = match_count;
3865796c8dcSSimon Schubert
3875796c8dcSSimon Schubert while (--i >= 0)
3885796c8dcSSimon Schubert if (matching_vector[i] == right_targ)
3895796c8dcSSimon Schubert break;
3905796c8dcSSimon Schubert
3915796c8dcSSimon Schubert if (i >= 0)
3925796c8dcSSimon Schubert {
3935796c8dcSSimon Schubert match_count = 1;
3945796c8dcSSimon Schubert break;
3955796c8dcSSimon Schubert }
3965796c8dcSSimon Schubert }
3975796c8dcSSimon Schubert }
3985796c8dcSSimon Schubert
399*ef5ccd6cSJohn Marino /* There is way too much undoing of half-known state here. We
400*ef5ccd6cSJohn Marino really shouldn't iterate on live bfd's. Note that saving the
401*ef5ccd6cSJohn Marino whole bfd and restoring it would be even worse; the first thing
402*ef5ccd6cSJohn Marino you notice is that the cached bfd file position gets out of sync. */
403*ef5ccd6cSJohn Marino if (preserve.marker != NULL)
404*ef5ccd6cSJohn Marino bfd_preserve_restore (abfd, &preserve);
405*ef5ccd6cSJohn Marino
4065796c8dcSSimon Schubert if (match_count == 1)
4075796c8dcSSimon Schubert {
408a45ae5f8SJohn Marino abfd->xvec = right_targ;
409a45ae5f8SJohn Marino /* If we come out of the loop knowing that the last target that
410a45ae5f8SJohn Marino matched is the one we want, then ABFD should still be in a usable
411a45ae5f8SJohn Marino state (except possibly for XVEC). */
412a45ae5f8SJohn Marino if (match_targ != right_targ)
413a45ae5f8SJohn Marino {
414*ef5ccd6cSJohn Marino bfd_reinit (abfd);
415a45ae5f8SJohn Marino if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
416a45ae5f8SJohn Marino goto err_ret;
417a45ae5f8SJohn Marino match_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
418*ef5ccd6cSJohn Marino BFD_ASSERT (match_targ != NULL);
419a45ae5f8SJohn Marino }
4205796c8dcSSimon Schubert
421a45ae5f8SJohn Marino ok_ret:
4225796c8dcSSimon Schubert /* If the file was opened for update, then `output_has_begun'
4235796c8dcSSimon Schubert some time ago when the file was created. Do not recompute
4245796c8dcSSimon Schubert sections sizes or alignments in _bfd_set_section_contents.
4255796c8dcSSimon Schubert We can not set this flag until after checking the format,
4265796c8dcSSimon Schubert because it will interfere with creation of BFD sections. */
4275796c8dcSSimon Schubert if (abfd->direction == both_direction)
4285796c8dcSSimon Schubert abfd->output_has_begun = TRUE;
4295796c8dcSSimon Schubert
4305796c8dcSSimon Schubert if (matching_vector)
4315796c8dcSSimon Schubert free (matching_vector);
432*ef5ccd6cSJohn Marino
433*ef5ccd6cSJohn Marino /* File position has moved, BTW. */
434*ef5ccd6cSJohn Marino return TRUE;
4355796c8dcSSimon Schubert }
4365796c8dcSSimon Schubert
4375796c8dcSSimon Schubert if (match_count == 0)
4385796c8dcSSimon Schubert {
4395796c8dcSSimon Schubert err_unrecog:
4405796c8dcSSimon Schubert bfd_set_error (bfd_error_file_not_recognized);
4415796c8dcSSimon Schubert err_ret:
4425796c8dcSSimon Schubert abfd->xvec = save_targ;
4435796c8dcSSimon Schubert abfd->format = bfd_unknown;
4445796c8dcSSimon Schubert if (matching_vector)
4455796c8dcSSimon Schubert free (matching_vector);
446*ef5ccd6cSJohn Marino if (preserve.marker != NULL)
447*ef5ccd6cSJohn Marino bfd_preserve_restore (abfd, &preserve);
4485796c8dcSSimon Schubert return FALSE;
4495796c8dcSSimon Schubert }
4505796c8dcSSimon Schubert
451*ef5ccd6cSJohn Marino /* Restore original target type and format. */
452*ef5ccd6cSJohn Marino abfd->xvec = save_targ;
453*ef5ccd6cSJohn Marino abfd->format = bfd_unknown;
4545796c8dcSSimon Schubert bfd_set_error (bfd_error_file_ambiguously_recognized);
4555796c8dcSSimon Schubert
4565796c8dcSSimon Schubert if (matching)
4575796c8dcSSimon Schubert {
4585796c8dcSSimon Schubert *matching = (char **) matching_vector;
4595796c8dcSSimon Schubert matching_vector[match_count] = NULL;
4605796c8dcSSimon Schubert /* Return target names. This is a little nasty. Maybe we
4615796c8dcSSimon Schubert should do another bfd_malloc? */
4625796c8dcSSimon Schubert while (--match_count >= 0)
4635796c8dcSSimon Schubert {
4645796c8dcSSimon Schubert const char *name = matching_vector[match_count]->name;
4655796c8dcSSimon Schubert *(const char **) &matching_vector[match_count] = name;
4665796c8dcSSimon Schubert }
4675796c8dcSSimon Schubert }
4685796c8dcSSimon Schubert return FALSE;
4695796c8dcSSimon Schubert }
4705796c8dcSSimon Schubert
4715796c8dcSSimon Schubert /*
4725796c8dcSSimon Schubert FUNCTION
4735796c8dcSSimon Schubert bfd_set_format
4745796c8dcSSimon Schubert
4755796c8dcSSimon Schubert SYNOPSIS
4765796c8dcSSimon Schubert bfd_boolean bfd_set_format (bfd *abfd, bfd_format format);
4775796c8dcSSimon Schubert
4785796c8dcSSimon Schubert DESCRIPTION
4795796c8dcSSimon Schubert This function sets the file format of the BFD @var{abfd} to the
4805796c8dcSSimon Schubert format @var{format}. If the target set in the BFD does not
4815796c8dcSSimon Schubert support the format requested, the format is invalid, or the BFD
4825796c8dcSSimon Schubert is not open for writing, then an error occurs.
4835796c8dcSSimon Schubert */
4845796c8dcSSimon Schubert
4855796c8dcSSimon Schubert bfd_boolean
bfd_set_format(bfd * abfd,bfd_format format)4865796c8dcSSimon Schubert bfd_set_format (bfd *abfd, bfd_format format)
4875796c8dcSSimon Schubert {
4885796c8dcSSimon Schubert if (bfd_read_p (abfd)
4895796c8dcSSimon Schubert || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
4905796c8dcSSimon Schubert {
4915796c8dcSSimon Schubert bfd_set_error (bfd_error_invalid_operation);
4925796c8dcSSimon Schubert return FALSE;
4935796c8dcSSimon Schubert }
4945796c8dcSSimon Schubert
4955796c8dcSSimon Schubert if (abfd->format != bfd_unknown)
4965796c8dcSSimon Schubert return abfd->format == format;
4975796c8dcSSimon Schubert
4985796c8dcSSimon Schubert /* Presume the answer is yes. */
4995796c8dcSSimon Schubert abfd->format = format;
5005796c8dcSSimon Schubert
5015796c8dcSSimon Schubert if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
5025796c8dcSSimon Schubert {
5035796c8dcSSimon Schubert abfd->format = bfd_unknown;
5045796c8dcSSimon Schubert return FALSE;
5055796c8dcSSimon Schubert }
5065796c8dcSSimon Schubert
5075796c8dcSSimon Schubert return TRUE;
5085796c8dcSSimon Schubert }
5095796c8dcSSimon Schubert
5105796c8dcSSimon Schubert /*
5115796c8dcSSimon Schubert FUNCTION
5125796c8dcSSimon Schubert bfd_format_string
5135796c8dcSSimon Schubert
5145796c8dcSSimon Schubert SYNOPSIS
5155796c8dcSSimon Schubert const char *bfd_format_string (bfd_format format);
5165796c8dcSSimon Schubert
5175796c8dcSSimon Schubert DESCRIPTION
5185796c8dcSSimon Schubert Return a pointer to a const string
5195796c8dcSSimon Schubert <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
5205796c8dcSSimon Schubert depending upon the value of @var{format}.
5215796c8dcSSimon Schubert */
5225796c8dcSSimon Schubert
5235796c8dcSSimon Schubert const char *
bfd_format_string(bfd_format format)5245796c8dcSSimon Schubert bfd_format_string (bfd_format format)
5255796c8dcSSimon Schubert {
5265796c8dcSSimon Schubert if (((int) format < (int) bfd_unknown)
5275796c8dcSSimon Schubert || ((int) format >= (int) bfd_type_end))
5285796c8dcSSimon Schubert return "invalid";
5295796c8dcSSimon Schubert
5305796c8dcSSimon Schubert switch (format)
5315796c8dcSSimon Schubert {
5325796c8dcSSimon Schubert case bfd_object:
5335796c8dcSSimon Schubert return "object"; /* Linker/assembler/compiler output. */
5345796c8dcSSimon Schubert case bfd_archive:
5355796c8dcSSimon Schubert return "archive"; /* Object archive file. */
5365796c8dcSSimon Schubert case bfd_core:
5375796c8dcSSimon Schubert return "core"; /* Core dump. */
5385796c8dcSSimon Schubert default:
5395796c8dcSSimon Schubert return "unknown";
5405796c8dcSSimon Schubert }
5415796c8dcSSimon Schubert }
542