Coverage Report

Created: 2025-07-08 11:15

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