Coverage Report

Created: 2026-03-10 08:46

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