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