Home | History | Annotate | Line # | Download | only in bfd
      1       1.1  christos /* Generic BFD support for file formats.
      2  1.1.1.12  christos    Copyright (C) 1990-2025 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.1.12  christos #include "plugin.h"
     50       1.1  christos 
     51       1.1  christos /* IMPORT from targets.c.  */
     52       1.1  christos extern const size_t _bfd_target_vector_entries;
     53       1.1  christos 
     54       1.1  christos /*
     55       1.1  christos FUNCTION
     56       1.1  christos 	bfd_check_format
     57       1.1  christos 
     58       1.1  christos SYNOPSIS
     59   1.1.1.9  christos 	bool bfd_check_format (bfd *abfd, bfd_format format);
     60       1.1  christos 
     61       1.1  christos DESCRIPTION
     62       1.1  christos 	Verify if the file attached to the BFD @var{abfd} is compatible
     63       1.1  christos 	with the format @var{format} (i.e., one of <<bfd_object>>,
     64       1.1  christos 	<<bfd_archive>> or <<bfd_core>>).
     65       1.1  christos 
     66       1.1  christos 	If the BFD has been set to a specific target before the
     67       1.1  christos 	call, only the named target and format combination is
     68       1.1  christos 	checked. If the target has not been set, or has been set to
     69       1.1  christos 	<<default>>, then all the known target backends is
     70       1.1  christos 	interrogated to determine a match.  If the default target
     71       1.1  christos 	matches, it is used.  If not, exactly one target must recognize
     72       1.1  christos 	the file, or an error results.
     73       1.1  christos 
     74       1.1  christos 	The function returns <<TRUE>> on success, otherwise <<FALSE>>
     75       1.1  christos 	with one of the following error codes:
     76       1.1  christos 
     77       1.1  christos 	o <<bfd_error_invalid_operation>> -
     78       1.1  christos 	if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
     79       1.1  christos 	<<bfd_core>>.
     80       1.1  christos 
     81       1.1  christos 	o <<bfd_error_system_call>> -
     82       1.1  christos 	if an error occured during a read - even some file mismatches
     83       1.1  christos 	can cause bfd_error_system_calls.
     84       1.1  christos 
     85       1.1  christos 	o <<file_not_recognised>> -
     86       1.1  christos 	none of the backends recognised the file format.
     87       1.1  christos 
     88       1.1  christos 	o <<bfd_error_file_ambiguously_recognized>> -
     89       1.1  christos 	more than one backend recognised the file format.
     90  1.1.1.10  christos 
     91  1.1.1.10  christos 	When calling bfd_check_format (or bfd_check_format_matches),
     92  1.1.1.10  christos 	any underlying file descriptor will be kept open for the
     93  1.1.1.10  christos 	duration of the call.  This is done to avoid races when
     94  1.1.1.10  christos 	another thread calls bfd_cache_close_all.  In this scenario,
     95  1.1.1.10  christos 	the thread calling bfd_check_format must call bfd_cache_close
     96  1.1.1.10  christos 	itself.
     97       1.1  christos */
     98       1.1  christos 
     99   1.1.1.9  christos bool
    100       1.1  christos bfd_check_format (bfd *abfd, bfd_format format)
    101       1.1  christos {
    102       1.1  christos   return bfd_check_format_matches (abfd, format, NULL);
    103       1.1  christos }
    104       1.1  christos 
    105   1.1.1.2  christos struct bfd_preserve
    106   1.1.1.2  christos {
    107   1.1.1.2  christos   void *marker;
    108   1.1.1.2  christos   void *tdata;
    109   1.1.1.2  christos   flagword flags;
    110  1.1.1.10  christos   const struct bfd_iovec *iovec;
    111  1.1.1.10  christos   void *iostream;
    112   1.1.1.2  christos   const struct bfd_arch_info *arch_info;
    113  1.1.1.10  christos   const struct bfd_build_id *build_id;
    114  1.1.1.10  christos   bfd_cleanup cleanup;
    115   1.1.1.2  christos   struct bfd_section *sections;
    116   1.1.1.2  christos   struct bfd_section *section_last;
    117   1.1.1.2  christos   unsigned int section_count;
    118   1.1.1.7  christos   unsigned int section_id;
    119  1.1.1.10  christos   unsigned int symcount;
    120  1.1.1.10  christos   bool read_only;
    121  1.1.1.10  christos   bfd_vma start_address;
    122   1.1.1.2  christos   struct bfd_hash_table section_htab;
    123   1.1.1.2  christos };
    124   1.1.1.2  christos 
    125   1.1.1.2  christos /* When testing an object for compatibility with a particular target
    126   1.1.1.2  christos    back-end, the back-end object_p function needs to set up certain
    127   1.1.1.2  christos    fields in the bfd on successfully recognizing the object.  This
    128   1.1.1.2  christos    typically happens in a piecemeal fashion, with failures possible at
    129   1.1.1.2  christos    many points.  On failure, the bfd is supposed to be restored to its
    130   1.1.1.2  christos    initial state, which is virtually impossible.  However, restoring a
    131   1.1.1.2  christos    subset of the bfd state works in practice.  This function stores
    132   1.1.1.2  christos    the subset.  */
    133   1.1.1.2  christos 
    134   1.1.1.9  christos static bool
    135   1.1.1.8  christos bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve,
    136   1.1.1.8  christos 		   bfd_cleanup cleanup)
    137   1.1.1.2  christos {
    138   1.1.1.2  christos   preserve->tdata = abfd->tdata.any;
    139   1.1.1.2  christos   preserve->arch_info = abfd->arch_info;
    140   1.1.1.2  christos   preserve->flags = abfd->flags;
    141  1.1.1.10  christos   preserve->iovec = abfd->iovec;
    142  1.1.1.10  christos   preserve->iostream = abfd->iostream;
    143   1.1.1.2  christos   preserve->sections = abfd->sections;
    144   1.1.1.2  christos   preserve->section_last = abfd->section_last;
    145   1.1.1.2  christos   preserve->section_count = abfd->section_count;
    146   1.1.1.7  christos   preserve->section_id = _bfd_section_id;
    147  1.1.1.10  christos   preserve->symcount = abfd->symcount;
    148  1.1.1.10  christos   preserve->read_only = abfd->read_only;
    149  1.1.1.10  christos   preserve->start_address = abfd->start_address;
    150   1.1.1.2  christos   preserve->section_htab = abfd->section_htab;
    151   1.1.1.2  christos   preserve->marker = bfd_alloc (abfd, 1);
    152   1.1.1.6  christos   preserve->build_id = abfd->build_id;
    153   1.1.1.8  christos   preserve->cleanup = cleanup;
    154   1.1.1.2  christos   if (preserve->marker == NULL)
    155   1.1.1.9  christos     return false;
    156   1.1.1.2  christos 
    157   1.1.1.2  christos   return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
    158   1.1.1.2  christos 			      sizeof (struct section_hash_entry));
    159   1.1.1.2  christos }
    160   1.1.1.2  christos 
    161  1.1.1.10  christos /* A back-end object_p function may flip a bfd from file backed to
    162  1.1.1.10  christos    in-memory, eg. pe_ILF_object_p.  In that case to restore the
    163  1.1.1.10  christos    original IO state we need to reopen the file.  Conversely, if we
    164  1.1.1.10  christos    are restoring a previously matched pe ILF format and have been
    165  1.1.1.10  christos    checking further target matches using file IO then we need to close
    166  1.1.1.10  christos    the file and detach the bfd from the cache lru list.  */
    167  1.1.1.10  christos 
    168  1.1.1.10  christos static void
    169  1.1.1.10  christos io_reinit (bfd *abfd, struct bfd_preserve *preserve)
    170  1.1.1.10  christos {
    171  1.1.1.10  christos   if (abfd->iovec != preserve->iovec)
    172  1.1.1.10  christos     {
    173  1.1.1.10  christos       /* Handle file backed to in-memory transition.  bfd_cache_close
    174  1.1.1.10  christos 	 won't do anything unless abfd->iovec is the cache_iovec.
    175  1.1.1.10  christos 	 Don't be tempted to call iovec->bclose here.  We don't want
    176  1.1.1.10  christos 	 to call memory_bclose, which would free the bim.  The bim
    177  1.1.1.10  christos 	 must be kept if bfd_check_format_matches is going to decide
    178  1.1.1.10  christos 	 later that the PE format needing it is in fact the correct
    179  1.1.1.10  christos 	 target match.  */
    180  1.1.1.10  christos       bfd_cache_close (abfd);
    181  1.1.1.10  christos       abfd->iovec = preserve->iovec;
    182  1.1.1.10  christos       abfd->iostream = preserve->iostream;
    183  1.1.1.10  christos 
    184  1.1.1.10  christos       /* Handle in-memory to file backed transition.  */
    185  1.1.1.10  christos       if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0
    186  1.1.1.10  christos 	  && (abfd->flags & BFD_IN_MEMORY) != 0
    187  1.1.1.10  christos 	  && (preserve->flags & BFD_CLOSED_BY_CACHE) == 0
    188  1.1.1.10  christos 	  && (preserve->flags & BFD_IN_MEMORY) == 0)
    189  1.1.1.10  christos 	bfd_open_file (abfd);
    190  1.1.1.10  christos     }
    191  1.1.1.10  christos   abfd->flags = preserve->flags;
    192  1.1.1.10  christos }
    193  1.1.1.10  christos 
    194   1.1.1.2  christos /* Clear out a subset of BFD state.  */
    195   1.1.1.2  christos 
    196   1.1.1.2  christos static void
    197  1.1.1.10  christos bfd_reinit (bfd *abfd, unsigned int section_id,
    198  1.1.1.10  christos 	    struct bfd_preserve *preserve, bfd_cleanup cleanup)
    199   1.1.1.2  christos {
    200   1.1.1.8  christos   _bfd_section_id = section_id;
    201   1.1.1.8  christos   if (cleanup)
    202   1.1.1.8  christos     cleanup (abfd);
    203   1.1.1.2  christos   abfd->tdata.any = NULL;
    204   1.1.1.2  christos   abfd->arch_info = &bfd_default_arch_struct;
    205  1.1.1.10  christos   io_reinit (abfd, preserve);
    206  1.1.1.10  christos   abfd->symcount = 0;
    207  1.1.1.10  christos   abfd->read_only = 0;
    208  1.1.1.10  christos   abfd->start_address = 0;
    209   1.1.1.9  christos   abfd->build_id = NULL;
    210   1.1.1.2  christos   bfd_section_list_clear (abfd);
    211   1.1.1.2  christos }
    212   1.1.1.2  christos 
    213   1.1.1.2  christos /* Restores bfd state saved by bfd_preserve_save.  */
    214   1.1.1.2  christos 
    215   1.1.1.8  christos static bfd_cleanup
    216   1.1.1.2  christos bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
    217   1.1.1.2  christos {
    218   1.1.1.2  christos   bfd_hash_table_free (&abfd->section_htab);
    219   1.1.1.2  christos 
    220   1.1.1.2  christos   abfd->tdata.any = preserve->tdata;
    221   1.1.1.2  christos   abfd->arch_info = preserve->arch_info;
    222  1.1.1.10  christos   io_reinit (abfd, preserve);
    223   1.1.1.2  christos   abfd->section_htab = preserve->section_htab;
    224   1.1.1.2  christos   abfd->sections = preserve->sections;
    225   1.1.1.2  christos   abfd->section_last = preserve->section_last;
    226   1.1.1.2  christos   abfd->section_count = preserve->section_count;
    227   1.1.1.7  christos   _bfd_section_id = preserve->section_id;
    228  1.1.1.10  christos   abfd->symcount = preserve->symcount;
    229  1.1.1.10  christos   abfd->read_only = preserve->read_only;
    230  1.1.1.10  christos   abfd->start_address = preserve->start_address;
    231   1.1.1.6  christos   abfd->build_id = preserve->build_id;
    232   1.1.1.2  christos 
    233   1.1.1.2  christos   /* bfd_release frees all memory more recently bfd_alloc'd than
    234   1.1.1.2  christos      its arg, as well as its arg.  */
    235   1.1.1.2  christos   bfd_release (abfd, preserve->marker);
    236   1.1.1.2  christos   preserve->marker = NULL;
    237   1.1.1.8  christos   return preserve->cleanup;
    238   1.1.1.2  christos }
    239   1.1.1.2  christos 
    240   1.1.1.2  christos /* Called when the bfd state saved by bfd_preserve_save is no longer
    241   1.1.1.2  christos    needed.  */
    242   1.1.1.2  christos 
    243   1.1.1.2  christos static void
    244   1.1.1.2  christos bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
    245   1.1.1.2  christos {
    246   1.1.1.8  christos   if (preserve->cleanup)
    247   1.1.1.8  christos     {
    248   1.1.1.8  christos       /* Run the cleanup, assuming that all it will need is the
    249   1.1.1.8  christos 	 tdata at the time the cleanup was returned.  */
    250   1.1.1.8  christos       void *tdata = abfd->tdata.any;
    251   1.1.1.8  christos       abfd->tdata.any = preserve->tdata;
    252   1.1.1.8  christos       preserve->cleanup (abfd);
    253   1.1.1.8  christos       abfd->tdata.any = tdata;
    254   1.1.1.8  christos     }
    255   1.1.1.2  christos   /* It would be nice to be able to free more memory here, eg. old
    256   1.1.1.2  christos      tdata, but that's not possible since these blocks are sitting
    257   1.1.1.2  christos      inside bfd_alloc'd memory.  The section hash is on a separate
    258   1.1.1.2  christos      objalloc.  */
    259   1.1.1.2  christos   bfd_hash_table_free (&preserve->section_htab);
    260   1.1.1.2  christos   preserve->marker = NULL;
    261   1.1.1.2  christos }
    262   1.1.1.2  christos 
    263   1.1.1.9  christos static void
    264   1.1.1.9  christos print_warnmsg (struct per_xvec_message **list)
    265   1.1.1.9  christos {
    266   1.1.1.9  christos   for (struct per_xvec_message *warn = *list; warn; warn = warn->next)
    267  1.1.1.10  christos     _bfd_error_handler ("%s", warn->message);
    268   1.1.1.9  christos }
    269   1.1.1.9  christos 
    270   1.1.1.9  christos static void
    271   1.1.1.9  christos clear_warnmsg (struct per_xvec_message **list)
    272   1.1.1.9  christos {
    273   1.1.1.9  christos   struct per_xvec_message *warn = *list;
    274   1.1.1.9  christos   while (warn)
    275   1.1.1.9  christos     {
    276   1.1.1.9  christos       struct per_xvec_message *next = warn->next;
    277   1.1.1.9  christos       free (warn);
    278   1.1.1.9  christos       warn = next;
    279   1.1.1.9  christos     }
    280   1.1.1.9  christos   *list = NULL;
    281   1.1.1.9  christos }
    282   1.1.1.9  christos 
    283  1.1.1.10  christos /* Free all the storage in LIST.  Note that the first element of LIST
    284  1.1.1.10  christos    is special and is assumed to be stack-allocated.  TARG is used for
    285  1.1.1.10  christos    re-issuing warning messages.  If TARG is PER_XVEC_NO_TARGET, then
    286  1.1.1.11  christos    it acts like a sort of wildcard -- messages are reissued if all
    287  1.1.1.11  christos    targets with messages have identical messages.  One copy of the
    288  1.1.1.11  christos    messages are then reissued.  If TARG is anything else, then only
    289  1.1.1.11  christos    messages associated with TARG are emitted.  */
    290  1.1.1.10  christos 
    291   1.1.1.9  christos static void
    292  1.1.1.10  christos print_and_clear_messages (struct per_xvec_messages *list,
    293  1.1.1.10  christos 			  const bfd_target *targ)
    294   1.1.1.9  christos {
    295  1.1.1.11  christos   struct per_xvec_messages *iter;
    296  1.1.1.10  christos 
    297  1.1.1.11  christos   if (targ == PER_XVEC_NO_TARGET)
    298  1.1.1.11  christos     {
    299  1.1.1.11  christos       iter = list->next;
    300  1.1.1.11  christos       while (iter != NULL)
    301  1.1.1.11  christos 	{
    302  1.1.1.11  christos 	  struct per_xvec_message *msg1 = list->messages;
    303  1.1.1.11  christos 	  struct per_xvec_message *msg2 = iter->messages;
    304  1.1.1.11  christos 	  do
    305  1.1.1.11  christos 	    {
    306  1.1.1.11  christos 	      if (strcmp (msg1->message, msg2->message))
    307  1.1.1.11  christos 		break;
    308  1.1.1.11  christos 	      msg1 = msg1->next;
    309  1.1.1.11  christos 	      msg2 = msg2->next;
    310  1.1.1.11  christos 	    } while (msg1 && msg2);
    311  1.1.1.11  christos 	  if (msg1 || msg2)
    312  1.1.1.11  christos 	    break;
    313  1.1.1.11  christos 	  iter = iter->next;
    314  1.1.1.11  christos 	}
    315  1.1.1.11  christos       if (iter == NULL)
    316  1.1.1.11  christos 	targ = list->targ;
    317  1.1.1.11  christos     }
    318  1.1.1.10  christos 
    319  1.1.1.11  christos   iter = list;
    320  1.1.1.10  christos   while (iter != NULL)
    321  1.1.1.10  christos     {
    322  1.1.1.10  christos       struct per_xvec_messages *next = iter->next;
    323  1.1.1.10  christos 
    324  1.1.1.10  christos       if (iter->targ == targ)
    325  1.1.1.10  christos 	print_warnmsg (&iter->messages);
    326  1.1.1.10  christos       clear_warnmsg (&iter->messages);
    327  1.1.1.10  christos       if (iter != list)
    328  1.1.1.10  christos 	free (iter);
    329  1.1.1.10  christos       iter = next;
    330  1.1.1.10  christos     }
    331  1.1.1.12  christos 
    332  1.1.1.12  christos   /* Don't retain a pointer to free'd memory.  */
    333  1.1.1.12  christos   list->next = NULL;
    334  1.1.1.12  christos }
    335  1.1.1.12  christos 
    336  1.1.1.12  christos /* Discard all messages associated with TARG in LIST.  Unlike
    337  1.1.1.12  christos    print_and_clear_messages, PER_XVEC_NO_TARGET is not valid for TARG.  */
    338  1.1.1.12  christos 
    339  1.1.1.12  christos static void
    340  1.1.1.12  christos clear_messages (struct per_xvec_messages *list,
    341  1.1.1.12  christos 		const bfd_target *targ)
    342  1.1.1.12  christos {
    343  1.1.1.12  christos   struct per_xvec_messages *iter;
    344  1.1.1.12  christos 
    345  1.1.1.12  christos   for (iter = list; iter != NULL; iter = iter->next)
    346  1.1.1.12  christos     {
    347  1.1.1.12  christos       if (iter->targ == targ)
    348  1.1.1.12  christos 	clear_warnmsg (&iter->messages);
    349  1.1.1.12  christos     }
    350  1.1.1.10  christos }
    351  1.1.1.10  christos 
    352  1.1.1.10  christos /* This a copy of lto_section defined in GCC (lto-streamer.h).  */
    353  1.1.1.10  christos 
    354  1.1.1.10  christos struct lto_section
    355  1.1.1.10  christos {
    356  1.1.1.10  christos   int16_t major_version;
    357  1.1.1.10  christos   int16_t minor_version;
    358  1.1.1.10  christos   unsigned char slim_object;
    359  1.1.1.10  christos 
    360  1.1.1.10  christos   /* Flags is a private field that is not defined publicly.  */
    361  1.1.1.10  christos   uint16_t flags;
    362  1.1.1.10  christos };
    363  1.1.1.10  christos 
    364  1.1.1.10  christos /* Set lto_type in ABFD.  */
    365  1.1.1.10  christos 
    366  1.1.1.10  christos static void
    367  1.1.1.12  christos bfd_set_lto_type (bfd *abfd)
    368  1.1.1.10  christos {
    369  1.1.1.10  christos   if (abfd->format == bfd_object
    370  1.1.1.10  christos       && abfd->lto_type == lto_non_object
    371  1.1.1.12  christos       && (abfd->flags
    372  1.1.1.12  christos 	  & (DYNAMIC
    373  1.1.1.12  christos 	     | (bfd_get_flavour (abfd) == bfd_target_elf_flavour
    374  1.1.1.12  christos 		? EXEC_P : 0))) == 0)
    375  1.1.1.10  christos     {
    376  1.1.1.12  christos       asection *sec = abfd->sections;
    377  1.1.1.10  christos       enum bfd_lto_object_type type = lto_non_ir_object;
    378  1.1.1.12  christos       if (sec == NULL)
    379  1.1.1.12  christos 	{
    380  1.1.1.12  christos 	  /* If there are no sections, check for slim LLVM IR object whose
    381  1.1.1.12  christos 	     first 4 bytes are: 'B', 'C', 0xc0, 0xde.  */
    382  1.1.1.12  christos 	  bfd_byte llvm_ir_magic[4];
    383  1.1.1.12  christos 	  if (bfd_seek (abfd, 0, SEEK_SET) == 0
    384  1.1.1.12  christos 	      && bfd_read (llvm_ir_magic, 4, abfd) == 4
    385  1.1.1.12  christos 	      && llvm_ir_magic[0] == 'B'
    386  1.1.1.12  christos 	      && llvm_ir_magic[1] == 'C'
    387  1.1.1.12  christos 	      && llvm_ir_magic[2] == 0xc0
    388  1.1.1.12  christos 	      && llvm_ir_magic[3] == 0xde)
    389  1.1.1.12  christos 	    type = lto_slim_ir_object;
    390  1.1.1.12  christos 	}
    391  1.1.1.12  christos       else
    392  1.1.1.12  christos 	{
    393  1.1.1.12  christos 	  struct lto_section lsection = { 0, 0, 0, 0 };
    394  1.1.1.12  christos 	  /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode
    395  1.1.1.12  christos 	     information section.  */
    396  1.1.1.12  christos 	  for (; sec != NULL; sec = sec->next)
    397  1.1.1.12  christos 	    if (strcmp (sec->name, GNU_OBJECT_ONLY_SECTION_NAME) == 0)
    398  1.1.1.12  christos 	      {
    399  1.1.1.12  christos 		type = lto_mixed_object;
    400  1.1.1.12  christos 		abfd->object_only_section = sec;
    401  1.1.1.12  christos 		break;
    402  1.1.1.12  christos 	      }
    403  1.1.1.12  christos 	    else if (strcmp (sec->name, ".llvm.lto") == 0)
    404  1.1.1.12  christos 	      {
    405  1.1.1.12  christos 		type = lto_fat_ir_object;
    406  1.1.1.12  christos 		break;
    407  1.1.1.12  christos 	      }
    408  1.1.1.12  christos 	    else if (lsection.major_version == 0
    409  1.1.1.12  christos 		     && startswith (sec->name, ".gnu.lto_.lto.")
    410  1.1.1.12  christos 		     && bfd_get_section_contents (abfd, sec, &lsection, 0,
    411  1.1.1.12  christos 						  sizeof (struct lto_section)))
    412  1.1.1.12  christos 	      {
    413  1.1.1.12  christos 		if (lsection.slim_object)
    414  1.1.1.12  christos 		  type = lto_slim_ir_object;
    415  1.1.1.12  christos 		else
    416  1.1.1.12  christos 		  type = lto_fat_ir_object;
    417  1.1.1.10  christos 	  }
    418  1.1.1.12  christos 	}
    419  1.1.1.10  christos 
    420  1.1.1.10  christos       abfd->lto_type = type;
    421  1.1.1.10  christos     }
    422   1.1.1.9  christos }
    423   1.1.1.9  christos 
    424       1.1  christos /*
    425       1.1  christos FUNCTION
    426       1.1  christos 	bfd_check_format_matches
    427       1.1  christos 
    428       1.1  christos SYNOPSIS
    429   1.1.1.9  christos 	bool bfd_check_format_matches
    430       1.1  christos 	  (bfd *abfd, bfd_format format, char ***matching);
    431       1.1  christos 
    432       1.1  christos DESCRIPTION
    433       1.1  christos 	Like <<bfd_check_format>>, except when it returns FALSE with
    434  1.1.1.12  christos 	<<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>.
    435  1.1.1.12  christos 	In that case, if @var{matching} is not NULL, it will be filled
    436  1.1.1.12  christos 	in with a NULL-terminated list of the names of the formats
    437  1.1.1.12  christos 	that matched, allocated with <<malloc>>.
    438       1.1  christos 	Then the user may choose a format and try again.
    439       1.1  christos 
    440       1.1  christos 	When done with the list that @var{matching} points to, the caller
    441       1.1  christos 	should free it.
    442       1.1  christos */
    443       1.1  christos 
    444   1.1.1.9  christos bool
    445       1.1  christos bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
    446       1.1  christos {
    447       1.1  christos   extern const bfd_target binary_vec;
    448       1.1  christos   const bfd_target * const *target;
    449       1.1  christos   const bfd_target **matching_vector = NULL;
    450   1.1.1.2  christos   const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
    451  1.1.1.12  christos   const bfd_target *fail_targ;
    452   1.1.1.2  christos   int match_count, best_count, best_match;
    453       1.1  christos   int ar_match_index;
    454   1.1.1.7  christos   unsigned int initial_section_id = _bfd_section_id;
    455   1.1.1.8  christos   struct bfd_preserve preserve, preserve_match;
    456   1.1.1.8  christos   bfd_cleanup cleanup = NULL;
    457  1.1.1.10  christos   struct per_xvec_messages messages = { abfd, PER_XVEC_NO_TARGET, NULL, NULL };
    458  1.1.1.10  christos   struct per_xvec_messages *orig_messages;
    459  1.1.1.10  christos   bool old_in_format_matches;
    460       1.1  christos 
    461       1.1  christos   if (matching != NULL)
    462       1.1  christos     *matching = NULL;
    463       1.1  christos 
    464       1.1  christos   if (!bfd_read_p (abfd)
    465       1.1  christos       || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
    466       1.1  christos     {
    467       1.1  christos       bfd_set_error (bfd_error_invalid_operation);
    468   1.1.1.9  christos       return false;
    469       1.1  christos     }
    470       1.1  christos 
    471       1.1  christos   if (abfd->format != bfd_unknown)
    472  1.1.1.12  christos     return abfd->format == format;
    473       1.1  christos 
    474       1.1  christos   if (matching != NULL || *bfd_associated_vector != NULL)
    475       1.1  christos     {
    476   1.1.1.8  christos       size_t amt;
    477       1.1  christos 
    478       1.1  christos       amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
    479       1.1  christos       matching_vector = (const bfd_target **) bfd_malloc (amt);
    480       1.1  christos       if (!matching_vector)
    481   1.1.1.9  christos 	return false;
    482       1.1  christos     }
    483       1.1  christos 
    484  1.1.1.10  christos   /* Avoid clashes with bfd_cache_close_all running in another
    485  1.1.1.10  christos      thread.  */
    486  1.1.1.10  christos   if (!bfd_cache_set_uncloseable (abfd, true, &old_in_format_matches))
    487  1.1.1.12  christos     {
    488  1.1.1.12  christos       free (matching_vector);
    489  1.1.1.12  christos       return false;
    490  1.1.1.12  christos     }
    491  1.1.1.12  christos 
    492  1.1.1.12  christos   /* Locking is required here in order to manage _bfd_section_id.  */
    493  1.1.1.12  christos   if (!bfd_lock ())
    494  1.1.1.12  christos     {
    495  1.1.1.12  christos       bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL);
    496  1.1.1.12  christos       free (matching_vector);
    497  1.1.1.12  christos       return false;
    498  1.1.1.12  christos     }
    499  1.1.1.10  christos 
    500       1.1  christos   /* Presume the answer is yes.  */
    501       1.1  christos   abfd->format = format;
    502   1.1.1.2  christos   save_targ = abfd->xvec;
    503   1.1.1.8  christos 
    504   1.1.1.9  christos   /* Don't report errors on recursive calls checking the first element
    505   1.1.1.9  christos      of an archive.  */
    506  1.1.1.10  christos   orig_messages = _bfd_set_error_handler_caching (&messages);
    507   1.1.1.9  christos 
    508   1.1.1.8  christos   preserve_match.marker = NULL;
    509   1.1.1.8  christos   if (!bfd_preserve_save (abfd, &preserve, NULL))
    510   1.1.1.8  christos     goto err_ret;
    511       1.1  christos 
    512  1.1.1.12  christos   /* First try matching the plugin target if appropriate.  Next try
    513  1.1.1.12  christos      the current target.  The current target may have been set due to
    514  1.1.1.12  christos      a user option, or due to the linker trying optimistically to load
    515  1.1.1.12  christos      input files for the same target as the output.  Either will
    516  1.1.1.12  christos      have target_defaulted false.  Failing that, bfd_find_target will
    517  1.1.1.12  christos      have chosen a default target, and target_defaulted will be true.  */
    518  1.1.1.12  christos   fail_targ = NULL;
    519  1.1.1.12  christos   if (bfd_plugin_enabled ()
    520  1.1.1.12  christos       && abfd->format == bfd_object
    521  1.1.1.12  christos       && abfd->target_defaulted
    522  1.1.1.12  christos       && !abfd->is_linker_input
    523  1.1.1.12  christos       && abfd->plugin_format != bfd_plugin_no)
    524       1.1  christos     {
    525  1.1.1.12  christos       if (bfd_seek (abfd, 0, SEEK_SET) != 0)
    526       1.1  christos 	goto err_ret;
    527       1.1  christos 
    528  1.1.1.12  christos       BFD_ASSERT (save_targ != bfd_plugin_vec ());
    529  1.1.1.12  christos       abfd->xvec = bfd_plugin_vec ();
    530  1.1.1.12  christos       bfd_set_error (bfd_error_no_error);
    531   1.1.1.8  christos       cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
    532   1.1.1.8  christos       if (cleanup)
    533       1.1  christos 	goto ok_ret;
    534       1.1  christos 
    535  1.1.1.12  christos       bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
    536  1.1.1.12  christos       bfd_release (abfd, preserve.marker);
    537  1.1.1.12  christos       preserve.marker = bfd_alloc (abfd, 1);
    538  1.1.1.12  christos       abfd->xvec = save_targ;
    539       1.1  christos     }
    540       1.1  christos 
    541  1.1.1.12  christos   /* bfd_plugin_no excluding the plugin target is an optimisation.
    542  1.1.1.12  christos      The test can be removed if desired.  */
    543  1.1.1.12  christos   if (!(abfd->plugin_format == bfd_plugin_no
    544  1.1.1.12  christos 	&& bfd_plugin_target_p (save_targ)))
    545  1.1.1.12  christos     {
    546  1.1.1.12  christos       if (bfd_seek (abfd, 0, SEEK_SET) != 0)
    547  1.1.1.12  christos 	goto err_ret;
    548  1.1.1.12  christos 
    549  1.1.1.12  christos       bfd_set_error (bfd_error_no_error);
    550  1.1.1.12  christos       cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
    551  1.1.1.12  christos       if (cleanup)
    552  1.1.1.12  christos 	{
    553  1.1.1.12  christos 	  if (abfd->format != bfd_archive
    554  1.1.1.12  christos 	      /* An archive with object files matching the archive
    555  1.1.1.12  christos 		 target is OK.  Other archives should be further
    556  1.1.1.12  christos 		 tested.  */
    557  1.1.1.12  christos 	      || (bfd_has_map (abfd)
    558  1.1.1.12  christos 		  && bfd_get_error () != bfd_error_wrong_object_format)
    559  1.1.1.12  christos 	      /* Empty archives can match the current target.
    560  1.1.1.12  christos 		 Attempting to read the armap will result in a file
    561  1.1.1.12  christos 		 truncated error.  */
    562  1.1.1.12  christos 	      || (!bfd_has_map (abfd)
    563  1.1.1.12  christos 		  && bfd_get_error () == bfd_error_file_truncated))
    564  1.1.1.12  christos 	    goto ok_ret;
    565  1.1.1.12  christos 	}
    566  1.1.1.12  christos       else
    567  1.1.1.12  christos 	{
    568  1.1.1.12  christos 	  if (!abfd->target_defaulted && !abfd->is_linker_input)
    569  1.1.1.12  christos 	    goto err_unrecog;
    570  1.1.1.12  christos 	  fail_targ = save_targ;
    571  1.1.1.12  christos 	}
    572  1.1.1.12  christos     }
    573  1.1.1.12  christos 
    574  1.1.1.12  christos   /* Check all targets in the hope that one will be recognized.  */
    575   1.1.1.2  christos   right_targ = NULL;
    576   1.1.1.2  christos   ar_right_targ = NULL;
    577   1.1.1.2  christos   match_targ = NULL;
    578   1.1.1.2  christos   best_match = 256;
    579   1.1.1.2  christos   best_count = 0;
    580   1.1.1.2  christos   match_count = 0;
    581   1.1.1.2  christos   ar_match_index = _bfd_target_vector_entries;
    582   1.1.1.2  christos 
    583       1.1  christos   for (target = bfd_target_vector; *target != NULL; target++)
    584       1.1  christos     {
    585   1.1.1.8  christos       void **high_water;
    586       1.1  christos 
    587   1.1.1.8  christos       /* The binary target matches anything, so don't return it when
    588  1.1.1.12  christos 	 searching.  Also, don't check the current target twice when
    589  1.1.1.12  christos 	 it has failed already.
    590  1.1.1.12  christos 	 Don't match the plugin target during linking if we have
    591  1.1.1.12  christos 	 another alternative since we want to properly set the input
    592  1.1.1.12  christos 	 format before allowing a plugin to claim the file.
    593  1.1.1.12  christos 	 Also as an optimisation don't match the plugin target when
    594  1.1.1.12  christos 	 abfd->plugin_format is set to bfd_plugin_no.  (This occurs
    595  1.1.1.12  christos 	 when LTO sections have been stripped or when we have a
    596  1.1.1.12  christos 	 recursive call here from the plugin object_p via
    597  1.1.1.12  christos 	 bfd_plugin_get_symbols_in_object_only.)  */
    598       1.1  christos       if (*target == &binary_vec
    599  1.1.1.12  christos 	  || *target == fail_targ
    600  1.1.1.12  christos 	  || (((abfd->is_linker_input && match_count != 0)
    601  1.1.1.12  christos 	       || abfd->plugin_format == bfd_plugin_no)
    602  1.1.1.12  christos 	      && bfd_plugin_target_p (*target)))
    603       1.1  christos 	continue;
    604       1.1  christos 
    605   1.1.1.2  christos       /* If we already tried a match, the bfd is modified and may
    606   1.1.1.2  christos 	 have sections attached, which will confuse the next
    607   1.1.1.2  christos 	 _bfd_check_format call.  */
    608  1.1.1.10  christos       bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
    609   1.1.1.8  christos       /* Free bfd_alloc memory too.  If we have matched and preserved
    610   1.1.1.8  christos 	 a target then the high water mark is that much higher.  */
    611   1.1.1.8  christos       if (preserve_match.marker)
    612   1.1.1.8  christos 	high_water = &preserve_match.marker;
    613   1.1.1.8  christos       else
    614   1.1.1.8  christos 	high_water = &preserve.marker;
    615   1.1.1.8  christos       bfd_release (abfd, *high_water);
    616   1.1.1.8  christos       *high_water = bfd_alloc (abfd, 1);
    617   1.1.1.2  christos 
    618   1.1.1.2  christos       /* Change BFD's target temporarily.  */
    619   1.1.1.2  christos       abfd->xvec = *target;
    620       1.1  christos 
    621  1.1.1.12  christos       /* It is possible that targets appear multiple times in
    622  1.1.1.12  christos 	 bfd_target_vector.  If this is the case, then we want to avoid
    623  1.1.1.12  christos 	 accumulating duplicate messages for a target in MESSAGES, so
    624  1.1.1.12  christos 	 discard any previous messages associated with this target.  */
    625  1.1.1.12  christos       clear_messages (&messages, abfd->xvec);
    626  1.1.1.12  christos 
    627  1.1.1.10  christos       if (bfd_seek (abfd, 0, SEEK_SET) != 0)
    628       1.1  christos 	goto err_ret;
    629       1.1  christos 
    630  1.1.1.12  christos       bfd_set_error (bfd_error_no_error);
    631   1.1.1.8  christos       cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
    632   1.1.1.8  christos       if (cleanup)
    633       1.1  christos 	{
    634   1.1.1.8  christos 	  int match_priority = abfd->xvec->match_priority;
    635   1.1.1.5  christos 
    636   1.1.1.2  christos 	  if (abfd->format != bfd_archive
    637   1.1.1.2  christos 	      || (bfd_has_map (abfd)
    638   1.1.1.2  christos 		  && bfd_get_error () != bfd_error_wrong_object_format))
    639   1.1.1.2  christos 	    {
    640   1.1.1.2  christos 	      /* If this is the default target, accept it, even if
    641   1.1.1.2  christos 		 other targets might match.  People who want those
    642   1.1.1.2  christos 		 other targets have to set the GNUTARGET variable.  */
    643   1.1.1.8  christos 	      if (abfd->xvec == bfd_default_vector[0])
    644   1.1.1.2  christos 		goto ok_ret;
    645   1.1.1.2  christos 
    646   1.1.1.2  christos 	      if (matching_vector)
    647   1.1.1.8  christos 		matching_vector[match_count] = abfd->xvec;
    648   1.1.1.2  christos 	      match_count++;
    649   1.1.1.2  christos 
    650   1.1.1.5  christos 	      if (match_priority < best_match)
    651   1.1.1.2  christos 		{
    652   1.1.1.5  christos 		  best_match = match_priority;
    653   1.1.1.2  christos 		  best_count = 0;
    654   1.1.1.2  christos 		}
    655   1.1.1.7  christos 	      if (match_priority <= best_match)
    656   1.1.1.7  christos 		{
    657   1.1.1.7  christos 		  /* This format checks out as ok!  */
    658   1.1.1.8  christos 		  right_targ = abfd->xvec;
    659   1.1.1.7  christos 		  best_count++;
    660   1.1.1.7  christos 		}
    661   1.1.1.2  christos 	    }
    662   1.1.1.2  christos 	  else
    663       1.1  christos 	    {
    664   1.1.1.2  christos 	      /* An archive with no armap or objects of the wrong
    665   1.1.1.2  christos 		 type.  We want this target to match if we get no
    666   1.1.1.2  christos 		 better matches.  */
    667   1.1.1.2  christos 	      if (ar_right_targ != bfd_default_vector[0])
    668   1.1.1.2  christos 		ar_right_targ = *target;
    669   1.1.1.2  christos 	      if (matching_vector)
    670   1.1.1.2  christos 		matching_vector[ar_match_index] = *target;
    671   1.1.1.2  christos 	      ar_match_index++;
    672       1.1  christos 	    }
    673       1.1  christos 
    674   1.1.1.8  christos 	  if (preserve_match.marker == NULL)
    675   1.1.1.8  christos 	    {
    676   1.1.1.8  christos 	      match_targ = abfd->xvec;
    677   1.1.1.8  christos 	      if (!bfd_preserve_save (abfd, &preserve_match, cleanup))
    678   1.1.1.8  christos 		goto err_ret;
    679   1.1.1.8  christos 	      cleanup = NULL;
    680   1.1.1.8  christos 	    }
    681       1.1  christos 	}
    682       1.1  christos     }
    683       1.1  christos 
    684   1.1.1.2  christos   if (best_count == 1)
    685   1.1.1.2  christos     match_count = 1;
    686   1.1.1.2  christos 
    687       1.1  christos   if (match_count == 0)
    688       1.1  christos     {
    689       1.1  christos       /* Try partial matches.  */
    690       1.1  christos       right_targ = ar_right_targ;
    691       1.1  christos 
    692       1.1  christos       if (right_targ == bfd_default_vector[0])
    693       1.1  christos 	{
    694       1.1  christos 	  match_count = 1;
    695       1.1  christos 	}
    696       1.1  christos       else
    697       1.1  christos 	{
    698       1.1  christos 	  match_count = ar_match_index - _bfd_target_vector_entries;
    699       1.1  christos 
    700       1.1  christos 	  if (matching_vector && match_count > 1)
    701       1.1  christos 	    memcpy (matching_vector,
    702       1.1  christos 		    matching_vector + _bfd_target_vector_entries,
    703       1.1  christos 		    sizeof (*matching_vector) * match_count);
    704       1.1  christos 	}
    705       1.1  christos     }
    706       1.1  christos 
    707   1.1.1.3  christos   /* We have more than one equally good match.  If any of the best
    708   1.1.1.3  christos      matches is a target in config.bfd targ_defvec or targ_selvecs,
    709   1.1.1.3  christos      choose it.  */
    710       1.1  christos   if (match_count > 1)
    711       1.1  christos     {
    712       1.1  christos       const bfd_target * const *assoc = bfd_associated_vector;
    713       1.1  christos 
    714       1.1  christos       while ((right_targ = *assoc++) != NULL)
    715       1.1  christos 	{
    716       1.1  christos 	  int i = match_count;
    717       1.1  christos 
    718       1.1  christos 	  while (--i >= 0)
    719   1.1.1.3  christos 	    if (matching_vector[i] == right_targ
    720   1.1.1.3  christos 		&& right_targ->match_priority <= best_match)
    721       1.1  christos 	      break;
    722       1.1  christos 
    723       1.1  christos 	  if (i >= 0)
    724       1.1  christos 	    {
    725       1.1  christos 	      match_count = 1;
    726       1.1  christos 	      break;
    727       1.1  christos 	    }
    728       1.1  christos 	}
    729       1.1  christos     }
    730       1.1  christos 
    731   1.1.1.3  christos   /* We still have more than one equally good match, and at least some
    732   1.1.1.3  christos      of the targets support match priority.  Choose the first of the
    733   1.1.1.3  christos      best matches.  */
    734   1.1.1.4  christos   if (matching_vector && match_count > 1 && best_count != match_count)
    735   1.1.1.3  christos     {
    736   1.1.1.3  christos       int i;
    737   1.1.1.3  christos 
    738   1.1.1.3  christos       for (i = 0; i < match_count; i++)
    739   1.1.1.3  christos 	{
    740   1.1.1.3  christos 	  right_targ = matching_vector[i];
    741   1.1.1.3  christos 	  if (right_targ->match_priority <= best_match)
    742   1.1.1.3  christos 	    break;
    743   1.1.1.3  christos 	}
    744   1.1.1.3  christos       match_count = 1;
    745   1.1.1.3  christos     }
    746   1.1.1.3  christos 
    747   1.1.1.2  christos   /* There is way too much undoing of half-known state here.  We
    748   1.1.1.2  christos      really shouldn't iterate on live bfd's.  Note that saving the
    749   1.1.1.2  christos      whole bfd and restoring it would be even worse; the first thing
    750   1.1.1.2  christos      you notice is that the cached bfd file position gets out of sync.  */
    751   1.1.1.8  christos   if (preserve_match.marker != NULL)
    752   1.1.1.8  christos     cleanup = bfd_preserve_restore (abfd, &preserve_match);
    753   1.1.1.2  christos 
    754       1.1  christos   if (match_count == 1)
    755       1.1  christos     {
    756   1.1.1.2  christos       abfd->xvec = right_targ;
    757   1.1.1.2  christos       /* If we come out of the loop knowing that the last target that
    758   1.1.1.2  christos 	 matched is the one we want, then ABFD should still be in a usable
    759   1.1.1.8  christos 	 state (except possibly for XVEC).  This is not just an
    760   1.1.1.8  christos 	 optimisation.  In the case of plugins a match against the
    761   1.1.1.8  christos 	 plugin target can result in the bfd being changed such that
    762   1.1.1.8  christos 	 it no longer matches the plugin target, nor will it match
    763   1.1.1.8  christos 	 RIGHT_TARG again.  */
    764   1.1.1.2  christos       if (match_targ != right_targ)
    765   1.1.1.2  christos 	{
    766  1.1.1.10  christos 	  bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
    767   1.1.1.8  christos 	  bfd_release (abfd, preserve.marker);
    768  1.1.1.10  christos 	  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
    769   1.1.1.2  christos 	    goto err_ret;
    770   1.1.1.8  christos 	  cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
    771   1.1.1.8  christos 	  BFD_ASSERT (cleanup != NULL);
    772   1.1.1.2  christos 	}
    773       1.1  christos 
    774   1.1.1.2  christos     ok_ret:
    775       1.1  christos       /* If the file was opened for update, then `output_has_begun'
    776       1.1  christos 	 some time ago when the file was created.  Do not recompute
    777       1.1  christos 	 sections sizes or alignments in _bfd_set_section_contents.
    778       1.1  christos 	 We can not set this flag until after checking the format,
    779       1.1  christos 	 because it will interfere with creation of BFD sections.  */
    780       1.1  christos       if (abfd->direction == both_direction)
    781   1.1.1.9  christos 	abfd->output_has_begun = true;
    782       1.1  christos 
    783   1.1.1.8  christos       free (matching_vector);
    784   1.1.1.8  christos       if (preserve_match.marker != NULL)
    785   1.1.1.8  christos 	bfd_preserve_finish (abfd, &preserve_match);
    786   1.1.1.8  christos       bfd_preserve_finish (abfd, &preserve);
    787  1.1.1.10  christos       _bfd_restore_error_handler_caching (orig_messages);
    788   1.1.1.9  christos 
    789  1.1.1.10  christos       print_and_clear_messages (&messages, abfd->xvec);
    790  1.1.1.10  christos 
    791  1.1.1.10  christos       bfd_set_lto_type (abfd);
    792   1.1.1.2  christos 
    793   1.1.1.2  christos       /* File position has moved, BTW.  */
    794  1.1.1.11  christos       bool ret = bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL);
    795  1.1.1.11  christos       if (!bfd_unlock ())
    796  1.1.1.11  christos 	return false;
    797  1.1.1.11  christos       return ret;
    798       1.1  christos     }
    799       1.1  christos 
    800       1.1  christos   if (match_count == 0)
    801       1.1  christos     {
    802       1.1  christos     err_unrecog:
    803       1.1  christos       bfd_set_error (bfd_error_file_not_recognized);
    804       1.1  christos     err_ret:
    805   1.1.1.8  christos       if (cleanup)
    806   1.1.1.8  christos 	cleanup (abfd);
    807       1.1  christos       abfd->xvec = save_targ;
    808       1.1  christos       abfd->format = bfd_unknown;
    809   1.1.1.8  christos       free (matching_vector);
    810   1.1.1.9  christos       goto out;
    811       1.1  christos     }
    812       1.1  christos 
    813   1.1.1.2  christos   /* Restore original target type and format.  */
    814   1.1.1.2  christos   abfd->xvec = save_targ;
    815   1.1.1.2  christos   abfd->format = bfd_unknown;
    816       1.1  christos   bfd_set_error (bfd_error_file_ambiguously_recognized);
    817       1.1  christos 
    818       1.1  christos   if (matching)
    819       1.1  christos     {
    820       1.1  christos       *matching = (char **) matching_vector;
    821       1.1  christos       matching_vector[match_count] = NULL;
    822       1.1  christos       /* Return target names.  This is a little nasty.  Maybe we
    823       1.1  christos 	 should do another bfd_malloc?  */
    824       1.1  christos       while (--match_count >= 0)
    825       1.1  christos 	{
    826       1.1  christos 	  const char *name = matching_vector[match_count]->name;
    827       1.1  christos 	  *(const char **) &matching_vector[match_count] = name;
    828       1.1  christos 	}
    829       1.1  christos     }
    830   1.1.1.8  christos   else
    831   1.1.1.8  christos     free (matching_vector);
    832   1.1.1.8  christos   if (cleanup)
    833   1.1.1.8  christos     cleanup (abfd);
    834   1.1.1.9  christos  out:
    835   1.1.1.8  christos   if (preserve_match.marker != NULL)
    836   1.1.1.8  christos     bfd_preserve_finish (abfd, &preserve_match);
    837  1.1.1.12  christos   if (preserve.marker != NULL)
    838  1.1.1.12  christos     bfd_preserve_restore (abfd, &preserve);
    839  1.1.1.10  christos   _bfd_restore_error_handler_caching (orig_messages);
    840  1.1.1.10  christos   print_and_clear_messages (&messages, PER_XVEC_NO_TARGET);
    841  1.1.1.10  christos   bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL);
    842  1.1.1.11  christos   bfd_unlock ();
    843   1.1.1.9  christos   return false;
    844       1.1  christos }
    845       1.1  christos 
    846       1.1  christos /*
    847       1.1  christos FUNCTION
    848       1.1  christos 	bfd_set_format
    849       1.1  christos 
    850       1.1  christos SYNOPSIS
    851   1.1.1.9  christos 	bool bfd_set_format (bfd *abfd, bfd_format format);
    852       1.1  christos 
    853       1.1  christos DESCRIPTION
    854       1.1  christos 	This function sets the file format of the BFD @var{abfd} to the
    855       1.1  christos 	format @var{format}. If the target set in the BFD does not
    856       1.1  christos 	support the format requested, the format is invalid, or the BFD
    857       1.1  christos 	is not open for writing, then an error occurs.
    858       1.1  christos */
    859       1.1  christos 
    860   1.1.1.9  christos bool
    861       1.1  christos bfd_set_format (bfd *abfd, bfd_format format)
    862       1.1  christos {
    863       1.1  christos   if (bfd_read_p (abfd)
    864       1.1  christos       || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
    865       1.1  christos     {
    866       1.1  christos       bfd_set_error (bfd_error_invalid_operation);
    867   1.1.1.9  christos       return false;
    868       1.1  christos     }
    869       1.1  christos 
    870       1.1  christos   if (abfd->format != bfd_unknown)
    871       1.1  christos     return abfd->format == format;
    872       1.1  christos 
    873       1.1  christos   /* Presume the answer is yes.  */
    874       1.1  christos   abfd->format = format;
    875       1.1  christos 
    876       1.1  christos   if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
    877       1.1  christos     {
    878       1.1  christos       abfd->format = bfd_unknown;
    879   1.1.1.9  christos       return false;
    880       1.1  christos     }
    881       1.1  christos 
    882   1.1.1.9  christos   return true;
    883       1.1  christos }
    884       1.1  christos 
    885       1.1  christos /*
    886       1.1  christos FUNCTION
    887       1.1  christos 	bfd_format_string
    888       1.1  christos 
    889       1.1  christos SYNOPSIS
    890       1.1  christos 	const char *bfd_format_string (bfd_format format);
    891       1.1  christos 
    892       1.1  christos DESCRIPTION
    893       1.1  christos 	Return a pointer to a const string
    894       1.1  christos 	<<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
    895       1.1  christos 	depending upon the value of @var{format}.
    896       1.1  christos */
    897       1.1  christos 
    898       1.1  christos const char *
    899       1.1  christos bfd_format_string (bfd_format format)
    900       1.1  christos {
    901       1.1  christos   if (((int) format < (int) bfd_unknown)
    902       1.1  christos       || ((int) format >= (int) bfd_type_end))
    903       1.1  christos     return "invalid";
    904       1.1  christos 
    905       1.1  christos   switch (format)
    906       1.1  christos     {
    907       1.1  christos     case bfd_object:
    908       1.1  christos       return "object";		/* Linker/assembler/compiler output.  */
    909       1.1  christos     case bfd_archive:
    910       1.1  christos       return "archive";		/* Object archive file.  */
    911       1.1  christos     case bfd_core:
    912       1.1  christos       return "core";		/* Core dump.  */
    913       1.1  christos     default:
    914       1.1  christos       return "unknown";
    915       1.1  christos     }
    916       1.1  christos }
    917