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