Coverage Report

Created: 2025-06-24 06:45

/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
3.49M
{
105
3.49M
  return bfd_check_format_matches (abfd, format, NULL);
106
3.49M
}
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
6.46M
{
141
6.46M
  preserve->tdata = abfd->tdata.any;
142
6.46M
  preserve->arch_info = abfd->arch_info;
143
6.46M
  preserve->flags = abfd->flags;
144
6.46M
  preserve->iovec = abfd->iovec;
145
6.46M
  preserve->iostream = abfd->iostream;
146
6.46M
  preserve->sections = abfd->sections;
147
6.46M
  preserve->section_last = abfd->section_last;
148
6.46M
  preserve->section_count = abfd->section_count;
149
6.46M
  preserve->section_id = _bfd_section_id;
150
6.46M
  preserve->symcount = abfd->symcount;
151
6.46M
  preserve->read_only = abfd->read_only;
152
6.46M
  preserve->start_address = abfd->start_address;
153
6.46M
  preserve->section_htab = abfd->section_htab;
154
6.46M
  preserve->marker = bfd_alloc (abfd, 1);
155
6.46M
  preserve->build_id = abfd->build_id;
156
6.46M
  preserve->cleanup = cleanup;
157
6.46M
  if (preserve->marker == NULL)
158
0
    return false;
159
160
6.46M
  return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
161
6.46M
            sizeof (struct section_hash_entry));
162
6.46M
}
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
927M
{
174
927M
  if (abfd->iovec != preserve->iovec)
175
129k
    {
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
129k
      bfd_cache_close (abfd);
184
129k
      abfd->iovec = preserve->iovec;
185
129k
      abfd->iostream = preserve->iostream;
186
187
      /* Handle in-memory to file backed transition.  */
188
129k
      if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0
189
129k
    && (abfd->flags & BFD_IN_MEMORY) != 0
190
129k
    && (preserve->flags & BFD_CLOSED_BY_CACHE) == 0
191
129k
    && (preserve->flags & BFD_IN_MEMORY) == 0)
192
328
  bfd_open_file (abfd);
193
129k
    }
194
927M
  abfd->flags = preserve->flags;
195
927M
}
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
923M
{
203
923M
  _bfd_section_id = section_id;
204
923M
  if (cleanup)
205
7.10M
    cleanup (abfd);
206
923M
  abfd->tdata.any = NULL;
207
923M
  abfd->arch_info = &bfd_default_arch_struct;
208
923M
  io_reinit (abfd, preserve);
209
923M
  abfd->symcount = 0;
210
923M
  abfd->read_only = 0;
211
923M
  abfd->start_address = 0;
212
923M
  abfd->build_id = NULL;
213
923M
  bfd_section_list_clear (abfd);
214
923M
}
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.75M
{
221
3.75M
  bfd_hash_table_free (&abfd->section_htab);
222
223
3.75M
  abfd->tdata.any = preserve->tdata;
224
3.75M
  abfd->arch_info = preserve->arch_info;
225
3.75M
  io_reinit (abfd, preserve);
226
3.75M
  abfd->section_htab = preserve->section_htab;
227
3.75M
  abfd->sections = preserve->sections;
228
3.75M
  abfd->section_last = preserve->section_last;
229
3.75M
  abfd->section_count = preserve->section_count;
230
3.75M
  _bfd_section_id = preserve->section_id;
231
3.75M
  abfd->symcount = preserve->symcount;
232
3.75M
  abfd->read_only = preserve->read_only;
233
3.75M
  abfd->start_address = preserve->start_address;
234
3.75M
  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.75M
  bfd_release (abfd, preserve->marker);
239
3.75M
  preserve->marker = NULL;
240
3.75M
  return preserve->cleanup;
241
3.75M
}
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.70M
{
249
2.70M
  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.70M
  bfd_hash_table_free (&preserve->section_htab);
263
2.70M
  preserve->marker = NULL;
264
2.70M
}
265
266
static void
267
print_warnmsg (struct per_xvec_message **list)
268
947k
{
269
1.12M
  for (struct per_xvec_message *warn = *list; warn; warn = warn->next)
270
177k
    _bfd_error_handler ("%s", warn->message);
271
947k
}
272
273
static void
274
clear_warnmsg (struct per_xvec_message **list)
275
3.66M
{
276
3.66M
  struct per_xvec_message *warn = *list;
277
3.98M
  while (warn)
278
312k
    {
279
312k
      struct per_xvec_message *next = warn->next;
280
312k
      free (warn);
281
312k
      warn = next;
282
312k
    }
283
3.66M
  *list = NULL;
284
3.66M
}
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
3.62M
{
298
3.62M
  struct per_xvec_messages *iter;
299
300
3.62M
  if (targ == PER_XVEC_NO_TARGET)
301
918k
    {
302
918k
      iter = list->next;
303
935k
      while (iter != NULL)
304
20.4k
  {
305
20.4k
    struct per_xvec_message *msg1 = list->messages;
306
20.4k
    struct per_xvec_message *msg2 = iter->messages;
307
20.4k
    do
308
33.6k
      {
309
33.6k
        if (strcmp (msg1->message, msg2->message))
310
2.34k
    break;
311
31.2k
        msg1 = msg1->next;
312
31.2k
        msg2 = msg2->next;
313
31.2k
      } while (msg1 && msg2);
314
20.4k
    if (msg1 || msg2)
315
2.72k
      break;
316
17.7k
    iter = iter->next;
317
17.7k
  }
318
918k
      if (iter == NULL)
319
915k
  targ = list->targ;
320
918k
    }
321
322
3.62M
  iter = list;
323
7.28M
  while (iter != NULL)
324
3.66M
    {
325
3.66M
      struct per_xvec_messages *next = iter->next;
326
327
3.66M
      if (iter->targ == targ)
328
947k
  print_warnmsg (&iter->messages);
329
3.66M
      clear_warnmsg (&iter->messages);
330
3.66M
      if (iter != list)
331
43.3k
  free (iter);
332
3.66M
      iter = next;
333
3.66M
    }
334
335
  /* Don't retain a pointer to free'd memory.  */
336
3.62M
  list->next = NULL;
337
3.62M
}
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
922M
{
346
922M
  struct per_xvec_messages *iter;
347
348
1.85G
  for (iter = list; iter != NULL; iter = iter->next)
349
928M
    {
350
928M
      if (iter->targ == targ)
351
1.76k
  clear_warnmsg (&iter->messages);
352
928M
    }
353
922M
}
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.70M
{
372
2.70M
#if BFD_SUPPORTS_PLUGINS
373
2.70M
  if (abfd->format == bfd_object
374
2.70M
      && abfd->lto_type == lto_non_object
375
2.70M
      && (abfd->flags
376
2.63M
    & (DYNAMIC
377
2.63M
       | (bfd_get_flavour (abfd) == bfd_target_elf_flavour
378
2.63M
    ? EXEC_P : 0))) == 0)
379
2.48M
    {
380
2.48M
      asection *sec;
381
2.48M
      enum bfd_lto_object_type type = lto_non_ir_object;
382
2.48M
      struct lto_section lsection = { 0, 0, 0, 0 };
383
      /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
384
   section.  */
385
100M
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
386
98.3M
  if (strcmp (sec->name, GNU_OBJECT_ONLY_SECTION_NAME) == 0)
387
1.07k
    {
388
1.07k
      type = lto_mixed_object;
389
1.07k
      abfd->object_only_section = sec;
390
1.07k
      break;
391
1.07k
    }
392
98.3M
  else if (lsection.major_version == 0
393
98.3M
     && startswith (sec->name, ".gnu.lto_.lto.")
394
98.3M
     && bfd_get_section_contents (abfd, sec, &lsection, 0,
395
9.86k
                sizeof (struct lto_section)))
396
5.63k
    {
397
5.63k
      if (lsection.slim_object)
398
2.04k
        type = lto_slim_ir_object;
399
3.58k
      else
400
3.58k
        type = lto_fat_ir_object;
401
5.63k
    }
402
403
2.48M
      abfd->lto_type = type;
404
2.48M
    }
405
2.70M
#endif
406
2.70M
}
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
3.62M
{
431
3.62M
  extern const bfd_target binary_vec;
432
3.62M
  const bfd_target * const *target;
433
3.62M
  const bfd_target **matching_vector = NULL;
434
3.62M
  const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
435
3.62M
  int match_count, best_count, best_match;
436
3.62M
  int ar_match_index;
437
3.62M
  unsigned int initial_section_id = _bfd_section_id;
438
3.62M
  struct bfd_preserve preserve, preserve_match;
439
3.62M
  bfd_cleanup cleanup = NULL;
440
3.62M
  struct per_xvec_messages messages = { abfd, PER_XVEC_NO_TARGET, NULL, NULL };
441
3.62M
  struct per_xvec_messages *orig_messages;
442
3.62M
  bool old_in_format_matches;
443
444
3.62M
  if (matching != NULL)
445
131k
    *matching = NULL;
446
447
3.62M
  if (!bfd_read_p (abfd)
448
3.62M
      || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
449
1.80k
    {
450
1.80k
      bfd_set_error (bfd_error_invalid_operation);
451
1.80k
      return false;
452
1.80k
    }
453
454
3.62M
  if (abfd->format != bfd_unknown)
455
43
    {
456
43
      bfd_set_lto_type (abfd);
457
43
      return abfd->format == format;
458
43
    }
459
460
3.62M
  if (matching != NULL || *bfd_associated_vector != NULL)
461
3.62M
    {
462
3.62M
      size_t amt;
463
464
3.62M
      amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
465
3.62M
      matching_vector = (const bfd_target **) bfd_malloc (amt);
466
3.62M
      if (!matching_vector)
467
0
  return false;
468
3.62M
    }
469
470
  /* Avoid clashes with bfd_cache_close_all running in another
471
     thread.  */
472
3.62M
  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
3.62M
  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
3.62M
  abfd->format = format;
488
3.62M
  save_targ = abfd->xvec;
489
490
  /* Don't report errors on recursive calls checking the first element
491
     of an archive.  */
492
3.62M
  orig_messages = _bfd_set_error_handler_caching (&messages);
493
494
3.62M
  preserve_match.marker = NULL;
495
3.62M
  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
3.62M
  if (!abfd->target_defaulted)
500
3.35M
    {
501
3.35M
      if (bfd_seek (abfd, 0, SEEK_SET) != 0)  /* rewind! */
502
0
  goto err_ret;
503
504
3.35M
      cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
505
506
3.35M
      if (cleanup)
507
16.7k
  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
3.33M
      if (format == bfd_archive && save_targ == &binary_vec)
522
0
  goto err_unrecog;
523
3.33M
    }
524
525
  /* Since the target type was defaulted, check them all in the hope
526
     that one will be uniquely recognized.  */
527
3.60M
  right_targ = NULL;
528
3.60M
  ar_right_targ = NULL;
529
3.60M
  match_targ = NULL;
530
3.60M
  best_match = 256;
531
3.60M
  best_count = 0;
532
3.60M
  match_count = 0;
533
3.60M
  ar_match_index = _bfd_target_vector_entries;
534
535
936M
  for (target = bfd_target_vector; *target != NULL; target++)
536
932M
    {
537
932M
      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
932M
      if (*target == &binary_vec
545
932M
#if BFD_SUPPORTS_PLUGINS
546
932M
    || (match_count != 0 && bfd_plugin_target_p (*target))
547
932M
#endif
548
932M
    || (!abfd->target_defaulted && *target == save_targ))
549
9.75M
  continue;
550
551
922M
#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
922M
      if (bfd_plugin_specified_p () && bfd_plugin_target_p (*target))
555
0
  continue;
556
922M
#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
922M
      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
922M
      if (preserve_match.marker)
565
390M
  high_water = &preserve_match.marker;
566
532M
      else
567
532M
  high_water = &preserve.marker;
568
922M
      bfd_release (abfd, *high_water);
569
922M
      *high_water = bfd_alloc (abfd, 1);
570
571
      /* Change BFD's target temporarily.  */
572
922M
      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
922M
      clear_messages (&messages, abfd->xvec);
579
580
922M
      if (bfd_seek (abfd, 0, SEEK_SET) != 0)
581
0
  goto err_ret;
582
583
922M
      cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
584
922M
      if (cleanup)
585
9.47M
  {
586
9.47M
    int match_priority = abfd->xvec->match_priority;
587
588
9.47M
    if (abfd->format != bfd_archive
589
9.47M
        || (bfd_has_map (abfd)
590
5.68M
      && bfd_get_error () != bfd_error_wrong_object_format))
591
4.53M
      {
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
4.53M
        if (abfd->xvec == bfd_default_vector[0])
596
19.0k
    goto ok_ret;
597
598
4.51M
        if (matching_vector)
599
4.51M
    matching_vector[match_count] = abfd->xvec;
600
4.51M
        match_count++;
601
602
4.51M
        if (match_priority < best_match)
603
3.21M
    {
604
3.21M
      best_match = match_priority;
605
3.21M
      best_count = 0;
606
3.21M
    }
607
4.51M
        if (match_priority <= best_match)
608
3.93M
    {
609
      /* This format checks out as ok!  */
610
3.93M
      right_targ = abfd->xvec;
611
3.93M
      best_count++;
612
3.93M
    }
613
4.51M
      }
614
4.94M
    else
615
4.94M
      {
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.94M
        if (ar_right_targ != bfd_default_vector[0])
620
418k
    ar_right_targ = *target;
621
4.94M
        if (matching_vector)
622
4.94M
    matching_vector[ar_match_index] = *target;
623
4.94M
        ar_match_index++;
624
4.94M
      }
625
626
9.45M
    if (preserve_match.marker == NULL)
627
2.83M
      {
628
2.83M
        match_targ = abfd->xvec;
629
2.83M
        if (!bfd_preserve_save (abfd, &preserve_match, cleanup))
630
0
    goto err_ret;
631
2.83M
        cleanup = NULL;
632
2.83M
      }
633
9.45M
  }
634
922M
    }
635
636
3.58M
  if (best_count == 1)
637
2.42M
    match_count = 1;
638
639
3.58M
  if (match_count == 0)
640
771k
    {
641
      /* Try partial matches.  */
642
771k
      right_targ = ar_right_targ;
643
644
771k
      if (right_targ == bfd_default_vector[0])
645
12.5k
  {
646
12.5k
    match_count = 1;
647
12.5k
  }
648
759k
      else
649
759k
  {
650
759k
    match_count = ar_match_index - _bfd_target_vector_entries;
651
652
759k
    if (matching_vector && match_count > 1)
653
8.02k
      memcpy (matching_vector,
654
8.02k
        matching_vector + _bfd_target_vector_entries,
655
8.02k
        sizeof (*matching_vector) * match_count);
656
759k
  }
657
771k
    }
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
3.58M
  if (match_count > 1)
663
393k
    {
664
393k
      const bfd_target * const *assoc = bfd_associated_vector;
665
666
3.77M
      while ((right_targ = *assoc++) != NULL)
667
3.53M
  {
668
3.53M
    int i = match_count;
669
670
22.7M
    while (--i >= 0)
671
19.3M
      if (matching_vector[i] == right_targ
672
19.3M
    && right_targ->match_priority <= best_match)
673
155k
        break;
674
675
3.53M
    if (i >= 0)
676
155k
      {
677
155k
        match_count = 1;
678
155k
        break;
679
155k
      }
680
3.53M
  }
681
393k
    }
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
3.58M
  if (matching_vector && match_count > 1 && best_count != match_count)
687
69.8k
    {
688
69.8k
      int i;
689
690
136k
      for (i = 0; i < match_count; i++)
691
136k
  {
692
136k
    right_targ = matching_vector[i];
693
136k
    if (right_targ->match_priority <= best_match)
694
69.8k
      break;
695
136k
  }
696
69.8k
      match_count = 1;
697
69.8k
    }
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
3.58M
  if (preserve_match.marker != NULL)
704
2.83M
    cleanup = bfd_preserve_restore (abfd, &preserve_match);
705
706
3.58M
  if (match_count == 1)
707
2.66M
    {
708
2.66M
      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.66M
      if (match_targ != right_targ)
717
494k
  {
718
494k
    bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
719
494k
    bfd_release (abfd, preserve.marker);
720
494k
    if (bfd_seek (abfd, 0, SEEK_SET) != 0)
721
0
      goto err_ret;
722
494k
    cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
723
494k
    BFD_ASSERT (cleanup != NULL);
724
494k
  }
725
726
2.70M
    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.70M
      if (abfd->direction == both_direction)
733
23.0k
  abfd->output_has_begun = true;
734
735
2.70M
      free (matching_vector);
736
2.70M
      if (preserve_match.marker != NULL)
737
0
  bfd_preserve_finish (abfd, &preserve_match);
738
2.70M
      bfd_preserve_finish (abfd, &preserve);
739
2.70M
      _bfd_restore_error_handler_caching (orig_messages);
740
741
2.70M
      print_and_clear_messages (&messages, abfd->xvec);
742
743
2.70M
      bfd_set_lto_type (abfd);
744
745
      /* File position has moved, BTW.  */
746
2.70M
      bool ret = bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL);
747
2.70M
      if (!bfd_unlock ())
748
0
  return false;
749
2.70M
      return ret;
750
2.70M
    }
751
752
918k
  if (match_count == 0)
753
749k
    {
754
749k
    err_unrecog:
755
749k
      bfd_set_error (bfd_error_file_not_recognized);
756
749k
    err_ret:
757
749k
      if (cleanup)
758
0
  cleanup (abfd);
759
749k
      abfd->xvec = save_targ;
760
749k
      abfd->format = bfd_unknown;
761
749k
      free (matching_vector);
762
749k
      goto out;
763
749k
    }
764
765
  /* Restore original target type and format.  */
766
168k
  abfd->xvec = save_targ;
767
168k
  abfd->format = bfd_unknown;
768
168k
  bfd_set_error (bfd_error_file_ambiguously_recognized);
769
770
168k
  if (matching)
771
5.47k
    {
772
5.47k
      *matching = (char **) matching_vector;
773
5.47k
      matching_vector[match_count] = NULL;
774
      /* Return target names.  This is a little nasty.  Maybe we
775
   should do another bfd_malloc?  */
776
21.1k
      while (--match_count >= 0)
777
15.7k
  {
778
15.7k
    const char *name = matching_vector[match_count]->name;
779
15.7k
    *(const char **) &matching_vector[match_count] = name;
780
15.7k
  }
781
5.47k
    }
782
162k
  else
783
162k
    free (matching_vector);
784
168k
  if (cleanup)
785
168k
    cleanup (abfd);
786
918k
 out:
787
918k
  if (preserve_match.marker != NULL)
788
0
    bfd_preserve_finish (abfd, &preserve_match);
789
918k
  if (preserve.marker != NULL)
790
918k
    bfd_preserve_restore (abfd, &preserve);
791
918k
  _bfd_restore_error_handler_caching (orig_messages);
792
918k
  print_and_clear_messages (&messages, PER_XVEC_NO_TARGET);
793
918k
  bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL);
794
918k
  bfd_unlock ();
795
918k
  return false;
796
168k
}
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
8.51k
{
815
8.51k
  if (bfd_read_p (abfd)
816
8.51k
      || (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
8.51k
  if (abfd->format != bfd_unknown)
823
3.01k
    return abfd->format == format;
824
825
  /* Presume the answer is yes.  */
826
5.50k
  abfd->format = format;
827
828
5.50k
  if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
829
1.74k
    {
830
1.74k
      abfd->format = bfd_unknown;
831
1.74k
      return false;
832
1.74k
    }
833
834
3.75k
  return true;
835
5.50k
}
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
}