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