Coverage Report

Created: 2024-05-21 06:29

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