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