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