Basic error checking for mach-o
[deliverable/binutils-gdb.git] / bfd / format.c
CommitLineData
252b5132 1/* Generic BFD support for file formats.
b3adc24a 2 Copyright (C) 1990-2020 Free Software Foundation, Inc.
252b5132
RH
3 Written by Cygnus Support.
4
ed781d5d 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
ed781d5d
NC
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
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
ed781d5d 10 (at your option) any later version.
252b5132 11
ed781d5d
NC
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.
252b5132 16
ed781d5d
NC
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
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
252b5132
RH
22
23/*
24SECTION
25 File formats
26
27 A format is a BFD concept of high level file contents type. The
3fde5a36 28 formats supported by BFD are:
252b5132
RH
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
1b74d094
BW
42SUBSECTION
43 File format functions
252b5132
RH
44*/
45
252b5132 46#include "sysdep.h"
3db64b00 47#include "bfd.h"
252b5132
RH
48#include "libbfd.h"
49
50/* IMPORT from targets.c. */
51extern const size_t _bfd_target_vector_entries;
52
53/*
54FUNCTION
55 bfd_check_format
56
57SYNOPSIS
ed781d5d 58 bfd_boolean bfd_check_format (bfd *abfd, bfd_format format);
252b5132
RH
59
60DESCRIPTION
61 Verify if the file attached to the BFD @var{abfd} is compatible
62 with the format @var{format} (i.e., one of <<bfd_object>>,
63 <<bfd_archive>> or <<bfd_core>>).
64
65 If the BFD has been set to a specific target before the
66 call, only the named target and format combination is
67 checked. If the target has not been set, or has been set to
68 <<default>>, then all the known target backends is
69 interrogated to determine a match. If the default target
70 matches, it is used. If not, exactly one target must recognize
71 the file, or an error results.
72
b34976b6 73 The function returns <<TRUE>> on success, otherwise <<FALSE>>
3fde5a36 74 with one of the following error codes:
252b5132
RH
75
76 o <<bfd_error_invalid_operation>> -
77 if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
78 <<bfd_core>>.
79
80 o <<bfd_error_system_call>> -
81 if an error occured during a read - even some file mismatches
82 can cause bfd_error_system_calls.
83
84 o <<file_not_recognised>> -
85 none of the backends recognised the file format.
86
87 o <<bfd_error_file_ambiguously_recognized>> -
88 more than one backend recognised the file format.
89*/
90
b34976b6 91bfd_boolean
c58b9523 92bfd_check_format (bfd *abfd, bfd_format format)
252b5132
RH
93{
94 return bfd_check_format_matches (abfd, format, NULL);
95}
96
c9ba0c87
AM
97struct bfd_preserve
98{
99 void *marker;
100 void *tdata;
101 flagword flags;
102 const struct bfd_arch_info *arch_info;
103 struct bfd_section *sections;
104 struct bfd_section *section_last;
105 unsigned int section_count;
7cf7fcc8 106 unsigned int section_id;
c9ba0c87 107 struct bfd_hash_table section_htab;
5d9bbb73 108 const struct bfd_build_id *build_id;
c9ba0c87
AM
109};
110
111/* When testing an object for compatibility with a particular target
112 back-end, the back-end object_p function needs to set up certain
113 fields in the bfd on successfully recognizing the object. This
114 typically happens in a piecemeal fashion, with failures possible at
115 many points. On failure, the bfd is supposed to be restored to its
116 initial state, which is virtually impossible. However, restoring a
117 subset of the bfd state works in practice. This function stores
118 the subset. */
119
120static bfd_boolean
121bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve)
122{
123 preserve->tdata = abfd->tdata.any;
124 preserve->arch_info = abfd->arch_info;
125 preserve->flags = abfd->flags;
126 preserve->sections = abfd->sections;
127 preserve->section_last = abfd->section_last;
128 preserve->section_count = abfd->section_count;
7cf7fcc8 129 preserve->section_id = _bfd_section_id;
c9ba0c87
AM
130 preserve->section_htab = abfd->section_htab;
131 preserve->marker = bfd_alloc (abfd, 1);
5d9bbb73 132 preserve->build_id = abfd->build_id;
c9ba0c87
AM
133 if (preserve->marker == NULL)
134 return FALSE;
135
136 return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
137 sizeof (struct section_hash_entry));
138}
139
140/* Clear out a subset of BFD state. */
141
142static void
7cf7fcc8 143bfd_reinit (bfd *abfd, unsigned int section_id)
c9ba0c87
AM
144{
145 abfd->tdata.any = NULL;
146 abfd->arch_info = &bfd_default_arch_struct;
147 abfd->flags &= BFD_FLAGS_SAVED;
148 bfd_section_list_clear (abfd);
7cf7fcc8 149 _bfd_section_id = section_id;
c9ba0c87
AM
150}
151
152/* Restores bfd state saved by bfd_preserve_save. */
153
154static void
155bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
156{
157 bfd_hash_table_free (&abfd->section_htab);
158
159 abfd->tdata.any = preserve->tdata;
160 abfd->arch_info = preserve->arch_info;
161 abfd->flags = preserve->flags;
162 abfd->section_htab = preserve->section_htab;
163 abfd->sections = preserve->sections;
164 abfd->section_last = preserve->section_last;
165 abfd->section_count = preserve->section_count;
7cf7fcc8 166 _bfd_section_id = preserve->section_id;
5d9bbb73 167 abfd->build_id = preserve->build_id;
c9ba0c87
AM
168
169 /* bfd_release frees all memory more recently bfd_alloc'd than
170 its arg, as well as its arg. */
171 bfd_release (abfd, preserve->marker);
172 preserve->marker = NULL;
173}
174
175/* Called when the bfd state saved by bfd_preserve_save is no longer
176 needed. */
177
178static void
179bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
180{
181 /* It would be nice to be able to free more memory here, eg. old
182 tdata, but that's not possible since these blocks are sitting
183 inside bfd_alloc'd memory. The section hash is on a separate
184 objalloc. */
185 bfd_hash_table_free (&preserve->section_htab);
186 preserve->marker = NULL;
187}
188
252b5132
RH
189/*
190FUNCTION
191 bfd_check_format_matches
192
193SYNOPSIS
c58b9523
AM
194 bfd_boolean bfd_check_format_matches
195 (bfd *abfd, bfd_format format, char ***matching);
252b5132
RH
196
197DESCRIPTION
b34976b6 198 Like <<bfd_check_format>>, except when it returns FALSE with
252b5132
RH
199 <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that
200 case, if @var{matching} is not NULL, it will be filled in with
201 a NULL-terminated list of the names of the formats that matched,
202 allocated with <<malloc>>.
203 Then the user may choose a format and try again.
204
205 When done with the list that @var{matching} points to, the caller
3fde5a36 206 should free it.
252b5132
RH
207*/
208
b34976b6 209bfd_boolean
c58b9523 210bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
252b5132
RH
211{
212 extern const bfd_target binary_vec;
64bfc258
JM
213#if BFD_SUPPORTS_PLUGINS
214 extern const bfd_target plugin_vec;
215#endif
08f74004
AM
216 const bfd_target * const *target;
217 const bfd_target **matching_vector = NULL;
0aabe54e
AM
218 const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
219 int match_count, best_count, best_match;
3619ad04 220 int ar_match_index;
7cf7fcc8 221 unsigned int initial_section_id = _bfd_section_id;
ea933f17 222 struct bfd_preserve preserve, preserve_match;
252b5132 223
5a1dcb7e
AM
224 if (matching != NULL)
225 *matching = NULL;
226
3619ad04 227 if (!bfd_read_p (abfd)
cea4409c 228 || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
1d713d9e
NC
229 {
230 bfd_set_error (bfd_error_invalid_operation);
b34976b6 231 return FALSE;
1d713d9e 232 }
252b5132
RH
233
234 if (abfd->format != bfd_unknown)
b34976b6 235 return abfd->format == format;
252b5132 236
5a1dcb7e 237 if (matching != NULL || *bfd_associated_vector != NULL)
252b5132 238 {
dc810e39
AM
239 bfd_size_type amt;
240
08f74004 241 amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
a50b1753 242 matching_vector = (const bfd_target **) bfd_malloc (amt);
252b5132 243 if (!matching_vector)
b34976b6 244 return FALSE;
252b5132 245 }
3fde5a36 246
1d713d9e 247 /* Presume the answer is yes. */
252b5132 248 abfd->format = format;
c9ba0c87 249 save_targ = abfd->xvec;
ea933f17
AM
250
251 preserve_match.marker = NULL;
252 if (!bfd_preserve_save (abfd, &preserve))
253 goto err_ret;
252b5132
RH
254
255 /* If the target type was explicitly specified, just check that target. */
1d713d9e
NC
256 if (!abfd->target_defaulted)
257 {
dc810e39 258 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) /* rewind! */
5a1dcb7e 259 goto err_ret;
3fde5a36 260
1d713d9e 261 right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
3fde5a36 262
1d713d9e 263 if (right_targ)
5a1dcb7e 264 goto ok_ret;
1d713d9e
NC
265
266 /* For a long time the code has dropped through to check all
267 targets if the specified target was wrong. I don't know why,
268 and I'm reluctant to change it. However, in the case of an
269 archive, it can cause problems. If the specified target does
270 not permit archives (e.g., the binary target), then we should
271 not allow some other target to recognize it as an archive, but
272 should instead allow the specified target to recognize it as an
273 object. When I first made this change, it broke the PE target,
274 because the specified pei-i386 target did not recognize the
275 actual pe-i386 archive. Since there may be other problems of
276 this sort, I changed this test to check only for the binary
277 target. */
278 if (format == bfd_archive && save_targ == &binary_vec)
5a1dcb7e 279 goto err_unrecog;
1d713d9e
NC
280 }
281
c9ba0c87
AM
282 /* Since the target type was defaulted, check them all in the hope
283 that one will be uniquely recognized. */
284 right_targ = NULL;
285 ar_right_targ = NULL;
286 match_targ = NULL;
287 best_match = 256;
288 best_count = 0;
289 match_count = 0;
290 ar_match_index = _bfd_target_vector_entries;
291
1d713d9e
NC
292 for (target = bfd_target_vector; *target != NULL; target++)
293 {
294 const bfd_target *temp;
ea933f17 295 void **high_water;
3fde5a36 296
999d6dff
AM
297 /* The binary target matches anything, so don't return it when
298 searching. Don't match the plugin target if we have another
299 alternative since we want to properly set the input format
300 before allowing a plugin to claim the file. Also, don't
301 check the default target twice. */
25bbc984 302 if (*target == &binary_vec
999d6dff
AM
303#if BFD_SUPPORTS_PLUGINS
304 || (match_count != 0 && *target == &plugin_vec)
305#endif
7cf7fcc8 306 || (!abfd->target_defaulted && *target == save_targ))
1d713d9e 307 continue;
3fde5a36 308
c9ba0c87
AM
309 /* If we already tried a match, the bfd is modified and may
310 have sections attached, which will confuse the next
311 _bfd_check_format call. */
7cf7fcc8 312 bfd_reinit (abfd, initial_section_id);
ea933f17
AM
313 /* Free bfd_alloc memory too. If we have matched and preserved
314 a target then the high water mark is that much higher. */
315 if (preserve_match.marker)
316 high_water = &preserve_match.marker;
317 else
318 high_water = &preserve.marker;
319 bfd_release (abfd, *high_water);
320 *high_water = bfd_alloc (abfd, 1);
c9ba0c87
AM
321
322 /* Change BFD's target temporarily. */
323 abfd->xvec = *target;
3fde5a36 324
dc810e39 325 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
5a1dcb7e 326 goto err_ret;
3fde5a36 327
1d713d9e
NC
328 /* If _bfd_check_format neglects to set bfd_error, assume
329 bfd_error_wrong_format. We didn't used to even pay any
330 attention to bfd_error, so I suspect that some
331 _bfd_check_format might have this problem. */
332 bfd_set_error (bfd_error_wrong_format);
333
334 temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
0aabe54e 335 if (temp)
c9ba0c87 336 {
64bfc258
JM
337 int match_priority = temp->match_priority;
338#if BFD_SUPPORTS_PLUGINS
339 /* If this object can be handled by a plugin, give that the
340 lowest priority; objects both handled by a plugin and
341 with an underlying object format will be claimed
342 separately by the plugin. */
343 if (*target == &plugin_vec)
344 match_priority = (*target)->match_priority;
345#endif
346
89d7b8aa
AM
347 if (abfd->format != bfd_archive
348 || (bfd_has_map (abfd)
349 && bfd_get_error () != bfd_error_wrong_object_format))
0aabe54e 350 {
89d7b8aa
AM
351 /* If this is the default target, accept it, even if
352 other targets might match. People who want those
353 other targets have to set the GNUTARGET variable. */
354 if (temp == bfd_default_vector[0])
355 goto ok_ret;
356
357 if (matching_vector)
358 matching_vector[match_count] = temp;
359 match_count++;
360
64bfc258 361 if (match_priority < best_match)
89d7b8aa 362 {
64bfc258 363 best_match = match_priority;
89d7b8aa
AM
364 best_count = 0;
365 }
7cf7fcc8
AM
366 if (match_priority <= best_match)
367 {
368 /* This format checks out as ok! */
369 right_targ = temp;
370 best_count++;
371 }
89d7b8aa
AM
372 }
373 else
374 {
375 /* An archive with no armap or objects of the wrong
376 type. We want this target to match if we get no
377 better matches. */
378 if (ar_right_targ != bfd_default_vector[0])
379 ar_right_targ = *target;
380 if (matching_vector)
381 matching_vector[ar_match_index] = *target;
382 ar_match_index++;
0aabe54e 383 }
c9ba0c87 384
ea933f17
AM
385 if (preserve_match.marker == NULL)
386 {
387 match_targ = temp;
388 if (!bfd_preserve_save (abfd, &preserve_match))
389 goto err_ret;
390 }
89d7b8aa
AM
391 }
392 else if (bfd_get_error () != bfd_error_wrong_format)
c9ba0c87 393 goto err_ret;
1d713d9e 394 }
3fde5a36 395
0aabe54e
AM
396 if (best_count == 1)
397 match_count = 1;
398
3619ad04
AM
399 if (match_count == 0)
400 {
401 /* Try partial matches. */
402 right_targ = ar_right_targ;
ed781d5d 403
3619ad04
AM
404 if (right_targ == bfd_default_vector[0])
405 {
406 match_count = 1;
407 }
408 else
409 {
410 match_count = ar_match_index - _bfd_target_vector_entries;
ed781d5d 411
5a1dcb7e 412 if (matching_vector && match_count > 1)
ed781d5d
NC
413 memcpy (matching_vector,
414 matching_vector + _bfd_target_vector_entries,
415 sizeof (*matching_vector) * match_count);
08f74004
AM
416 }
417 }
418
03ae2d5e
AM
419 /* We have more than one equally good match. If any of the best
420 matches is a target in config.bfd targ_defvec or targ_selvecs,
421 choose it. */
5a1dcb7e 422 if (match_count > 1)
08f74004
AM
423 {
424 const bfd_target * const *assoc = bfd_associated_vector;
425
426 while ((right_targ = *assoc++) != NULL)
427 {
428 int i = match_count;
429
430 while (--i >= 0)
03ae2d5e
AM
431 if (matching_vector[i] == right_targ
432 && right_targ->match_priority <= best_match)
08f74004
AM
433 break;
434
435 if (i >= 0)
436 {
437 match_count = 1;
438 break;
3619ad04
AM
439 }
440 }
441 }
442
03ae2d5e
AM
443 /* We still have more than one equally good match, and at least some
444 of the targets support match priority. Choose the first of the
445 best matches. */
033539e2 446 if (matching_vector && match_count > 1 && best_count != match_count)
03ae2d5e
AM
447 {
448 int i;
449
450 for (i = 0; i < match_count; i++)
451 {
452 right_targ = matching_vector[i];
453 if (right_targ->match_priority <= best_match)
454 break;
455 }
456 match_count = 1;
457 }
458
c9ba0c87
AM
459 /* There is way too much undoing of half-known state here. We
460 really shouldn't iterate on live bfd's. Note that saving the
461 whole bfd and restoring it would be even worse; the first thing
462 you notice is that the cached bfd file position gets out of sync. */
ea933f17
AM
463 if (preserve_match.marker != NULL)
464 bfd_preserve_restore (abfd, &preserve_match);
c9ba0c87 465
1d713d9e
NC
466 if (match_count == 1)
467 {
0aabe54e
AM
468 abfd->xvec = right_targ;
469 /* If we come out of the loop knowing that the last target that
470 matched is the one we want, then ABFD should still be in a usable
ea933f17
AM
471 state (except possibly for XVEC). This is not just an
472 optimisation. In the case of plugins a match against the
473 plugin target can result in the bfd being changed such that
474 it no longer matches the plugin target, nor will it match
475 RIGHT_TARG again. */
0aabe54e
AM
476 if (match_targ != right_targ)
477 {
7cf7fcc8 478 bfd_reinit (abfd, initial_section_id);
ea933f17 479 bfd_release (abfd, preserve.marker);
0aabe54e
AM
480 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
481 goto err_ret;
482 match_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
c9ba0c87 483 BFD_ASSERT (match_targ != NULL);
0aabe54e 484 }
3fde5a36 485
0aabe54e 486 ok_ret:
26ae6d5e
DJ
487 /* If the file was opened for update, then `output_has_begun'
488 some time ago when the file was created. Do not recompute
489 sections sizes or alignments in _bfd_set_section_contents.
490 We can not set this flag until after checking the format,
491 because it will interfere with creation of BFD sections. */
492 if (abfd->direction == both_direction)
493 abfd->output_has_begun = TRUE;
494
5a1dcb7e
AM
495 if (matching_vector)
496 free (matching_vector);
ea933f17
AM
497 if (preserve_match.marker != NULL)
498 bfd_preserve_finish (abfd, &preserve_match);
499 bfd_preserve_finish (abfd, &preserve);
c9ba0c87
AM
500
501 /* File position has moved, BTW. */
502 return TRUE;
252b5132 503 }
252b5132 504
252b5132
RH
505 if (match_count == 0)
506 {
5a1dcb7e 507 err_unrecog:
252b5132 508 bfd_set_error (bfd_error_file_not_recognized);
5a1dcb7e
AM
509 err_ret:
510 abfd->xvec = save_targ;
511 abfd->format = bfd_unknown;
512 if (matching_vector)
c58b9523 513 free (matching_vector);
ea933f17
AM
514 if (preserve_match.marker != NULL)
515 bfd_preserve_finish (abfd, &preserve_match);
516 bfd_preserve_restore (abfd, &preserve);
5a1dcb7e 517 return FALSE;
252b5132 518 }
3619ad04 519
c9ba0c87
AM
520 /* Restore original target type and format. */
521 abfd->xvec = save_targ;
522 abfd->format = bfd_unknown;
5a1dcb7e
AM
523 bfd_set_error (bfd_error_file_ambiguously_recognized);
524
525 if (matching)
526 {
527 *matching = (char **) matching_vector;
528 matching_vector[match_count] = NULL;
529 /* Return target names. This is a little nasty. Maybe we
530 should do another bfd_malloc? */
531 while (--match_count >= 0)
3619ad04 532 {
5a1dcb7e
AM
533 const char *name = matching_vector[match_count]->name;
534 *(const char **) &matching_vector[match_count] = name;
3619ad04
AM
535 }
536 }
9d78076e
AM
537 else if (matching_vector)
538 free (matching_vector);
ea933f17
AM
539 if (preserve_match.marker != NULL)
540 bfd_preserve_finish (abfd, &preserve_match);
541 bfd_preserve_restore (abfd, &preserve);
b34976b6 542 return FALSE;
252b5132
RH
543}
544
545/*
546FUNCTION
547 bfd_set_format
548
549SYNOPSIS
ed781d5d 550 bfd_boolean bfd_set_format (bfd *abfd, bfd_format format);
252b5132
RH
551
552DESCRIPTION
553 This function sets the file format of the BFD @var{abfd} to the
554 format @var{format}. If the target set in the BFD does not
555 support the format requested, the format is invalid, or the BFD
556 is not open for writing, then an error occurs.
252b5132
RH
557*/
558
b34976b6 559bfd_boolean
c58b9523 560bfd_set_format (bfd *abfd, bfd_format format)
252b5132 561{
3619ad04 562 if (bfd_read_p (abfd)
cea4409c 563 || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
1d713d9e
NC
564 {
565 bfd_set_error (bfd_error_invalid_operation);
b34976b6 566 return FALSE;
1d713d9e 567 }
252b5132
RH
568
569 if (abfd->format != bfd_unknown)
b34976b6 570 return abfd->format == format;
252b5132 571
1d713d9e 572 /* Presume the answer is yes. */
252b5132
RH
573 abfd->format = format;
574
1d713d9e
NC
575 if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
576 {
577 abfd->format = bfd_unknown;
b34976b6 578 return FALSE;
1d713d9e 579 }
252b5132 580
b34976b6 581 return TRUE;
252b5132
RH
582}
583
252b5132
RH
584/*
585FUNCTION
586 bfd_format_string
587
588SYNOPSIS
ed781d5d 589 const char *bfd_format_string (bfd_format format);
252b5132
RH
590
591DESCRIPTION
592 Return a pointer to a const string
593 <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
594 depending upon the value of @var{format}.
595*/
596
3619ad04 597const char *
c58b9523 598bfd_format_string (bfd_format format)
252b5132 599{
c58b9523
AM
600 if (((int) format < (int) bfd_unknown)
601 || ((int) format >= (int) bfd_type_end))
252b5132 602 return "invalid";
3fde5a36 603
1d713d9e
NC
604 switch (format)
605 {
606 case bfd_object:
7dee875e 607 return "object"; /* Linker/assembler/compiler output. */
3fde5a36 608 case bfd_archive:
1d713d9e 609 return "archive"; /* Object archive file. */
3fde5a36 610 case bfd_core:
1d713d9e 611 return "core"; /* Core dump. */
3fde5a36 612 default:
1d713d9e
NC
613 return "unknown";
614 }
252b5132 615}
This page took 1.0593 seconds and 4 git commands to generate.