packaging: Enable testing infrastructure
[external/binutils.git] / bfd / format.c
1 /* Generic BFD support for file formats.
2    Copyright (C) 1990-2019 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
50 /* IMPORT from targets.c.  */
51 extern const size_t _bfd_target_vector_entries;
52
53 /*
54 FUNCTION
55         bfd_check_format
56
57 SYNOPSIS
58         bfd_boolean bfd_check_format (bfd *abfd, bfd_format format);
59
60 DESCRIPTION
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
73         The function returns <<TRUE>> on success, otherwise <<FALSE>>
74         with one of the following error codes:
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
91 bfd_boolean
92 bfd_check_format (bfd *abfd, bfd_format format)
93 {
94   return bfd_check_format_matches (abfd, format, NULL);
95 }
96
97 struct 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   unsigned int section_id;
107   struct bfd_hash_table section_htab;
108   const struct bfd_build_id *build_id;
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
120 static bfd_boolean
121 bfd_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;
129   preserve->section_id = _bfd_section_id;
130   preserve->section_htab = abfd->section_htab;
131   preserve->marker = bfd_alloc (abfd, 1);
132   preserve->build_id = abfd->build_id;
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
142 static void
143 bfd_reinit (bfd *abfd, unsigned int section_id)
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);
149   _bfd_section_id = section_id;
150 }
151
152 /* Restores bfd state saved by bfd_preserve_save.  */
153
154 static void
155 bfd_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;
166   _bfd_section_id = preserve->section_id;
167   abfd->build_id = preserve->build_id;
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
178 static void
179 bfd_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
189 /*
190 FUNCTION
191         bfd_check_format_matches
192
193 SYNOPSIS
194         bfd_boolean bfd_check_format_matches
195           (bfd *abfd, bfd_format format, char ***matching);
196
197 DESCRIPTION
198         Like <<bfd_check_format>>, except when it returns FALSE with
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
206         should free it.
207 */
208
209 bfd_boolean
210 bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
211 {
212   extern const bfd_target binary_vec;
213 #if BFD_SUPPORTS_PLUGINS
214   extern const bfd_target plugin_vec;
215 #endif
216   const bfd_target * const *target;
217   const bfd_target **matching_vector = NULL;
218   const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
219   int match_count, best_count, best_match;
220   int ar_match_index;
221   unsigned int initial_section_id = _bfd_section_id;
222   struct bfd_preserve preserve;
223
224   if (matching != NULL)
225     *matching = NULL;
226
227   if (!bfd_read_p (abfd)
228       || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
229     {
230       bfd_set_error (bfd_error_invalid_operation);
231       return FALSE;
232     }
233
234   if (abfd->format != bfd_unknown)
235     return abfd->format == format;
236
237   if (matching != NULL || *bfd_associated_vector != NULL)
238     {
239       bfd_size_type amt;
240
241       amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
242       matching_vector = (const bfd_target **) bfd_malloc (amt);
243       if (!matching_vector)
244         return FALSE;
245     }
246
247   /* Presume the answer is yes.  */
248   abfd->format = format;
249   save_targ = abfd->xvec;
250   preserve.marker = NULL;
251
252   /* If the target type was explicitly specified, just check that target.  */
253   if (!abfd->target_defaulted)
254     {
255       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) /* rewind! */
256         goto err_ret;
257
258       right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
259
260       if (right_targ)
261         goto ok_ret;
262
263       /* For a long time the code has dropped through to check all
264          targets if the specified target was wrong.  I don't know why,
265          and I'm reluctant to change it.  However, in the case of an
266          archive, it can cause problems.  If the specified target does
267          not permit archives (e.g., the binary target), then we should
268          not allow some other target to recognize it as an archive, but
269          should instead allow the specified target to recognize it as an
270          object.  When I first made this change, it broke the PE target,
271          because the specified pei-i386 target did not recognize the
272          actual pe-i386 archive.  Since there may be other problems of
273          this sort, I changed this test to check only for the binary
274          target.  */
275       if (format == bfd_archive && save_targ == &binary_vec)
276         goto err_unrecog;
277     }
278
279   /* Since the target type was defaulted, check them all in the hope
280      that one will be uniquely recognized.  */
281   right_targ = NULL;
282   ar_right_targ = NULL;
283   match_targ = NULL;
284   best_match = 256;
285   best_count = 0;
286   match_count = 0;
287   ar_match_index = _bfd_target_vector_entries;
288
289   for (target = bfd_target_vector; *target != NULL; target++)
290     {
291       const bfd_target *temp;
292
293       /* The binary target matches anything, so don't return it when
294          searching.  Don't match the plugin target if we have another
295          alternative since we want to properly set the input format
296          before allowing a plugin to claim the file.  Also, don't
297          check the default target twice.  */
298       if (*target == &binary_vec
299 #if BFD_SUPPORTS_PLUGINS
300           || (match_count != 0 && *target == &plugin_vec)
301 #endif
302           || (!abfd->target_defaulted && *target == save_targ))
303         continue;
304
305       /* If we already tried a match, the bfd is modified and may
306          have sections attached, which will confuse the next
307          _bfd_check_format call.  */
308       bfd_reinit (abfd, initial_section_id);
309
310       /* Change BFD's target temporarily.  */
311       abfd->xvec = *target;
312
313       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
314         goto err_ret;
315
316       /* If _bfd_check_format neglects to set bfd_error, assume
317          bfd_error_wrong_format.  We didn't used to even pay any
318          attention to bfd_error, so I suspect that some
319          _bfd_check_format might have this problem.  */
320       bfd_set_error (bfd_error_wrong_format);
321
322       temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
323       if (temp)
324         {
325           int match_priority = temp->match_priority;
326 #if BFD_SUPPORTS_PLUGINS
327           /* If this object can be handled by a plugin, give that the
328              lowest priority; objects both handled by a plugin and
329              with an underlying object format will be claimed
330              separately by the plugin.  */
331           if (*target == &plugin_vec)
332             match_priority = (*target)->match_priority;
333 #endif
334
335           match_targ = temp;
336           if (preserve.marker != NULL)
337             bfd_preserve_finish (abfd, &preserve);
338
339           if (abfd->format != bfd_archive
340               || (bfd_has_map (abfd)
341                   && bfd_get_error () != bfd_error_wrong_object_format))
342             {
343               /* If this is the default target, accept it, even if
344                  other targets might match.  People who want those
345                  other targets have to set the GNUTARGET variable.  */
346               if (temp == bfd_default_vector[0])
347                 goto ok_ret;
348
349               if (matching_vector)
350                 matching_vector[match_count] = temp;
351               match_count++;
352
353               if (match_priority < best_match)
354                 {
355                   best_match = match_priority;
356                   best_count = 0;
357                 }
358               if (match_priority <= best_match)
359                 {
360                   /* This format checks out as ok!  */
361                   right_targ = temp;
362                   best_count++;
363                 }
364             }
365           else
366             {
367               /* An archive with no armap or objects of the wrong
368                  type.  We want this target to match if we get no
369                  better matches.  */
370               if (ar_right_targ != bfd_default_vector[0])
371                 ar_right_targ = *target;
372               if (matching_vector)
373                 matching_vector[ar_match_index] = *target;
374               ar_match_index++;
375             }
376
377           if (!bfd_preserve_save (abfd, &preserve))
378             goto err_ret;
379         }
380       else if (bfd_get_error () != bfd_error_wrong_format)
381         goto err_ret;
382     }
383
384   if (best_count == 1)
385     match_count = 1;
386
387   if (match_count == 0)
388     {
389       /* Try partial matches.  */
390       right_targ = ar_right_targ;
391
392       if (right_targ == bfd_default_vector[0])
393         {
394           match_count = 1;
395         }
396       else
397         {
398           match_count = ar_match_index - _bfd_target_vector_entries;
399
400           if (matching_vector && match_count > 1)
401             memcpy (matching_vector,
402                     matching_vector + _bfd_target_vector_entries,
403                     sizeof (*matching_vector) * match_count);
404         }
405     }
406
407   /* We have more than one equally good match.  If any of the best
408      matches is a target in config.bfd targ_defvec or targ_selvecs,
409      choose it.  */
410   if (match_count > 1)
411     {
412       const bfd_target * const *assoc = bfd_associated_vector;
413
414       while ((right_targ = *assoc++) != NULL)
415         {
416           int i = match_count;
417
418           while (--i >= 0)
419             if (matching_vector[i] == right_targ
420                 && right_targ->match_priority <= best_match)
421               break;
422
423           if (i >= 0)
424             {
425               match_count = 1;
426               break;
427             }
428         }
429     }
430
431   /* We still have more than one equally good match, and at least some
432      of the targets support match priority.  Choose the first of the
433      best matches.  */
434   if (matching_vector && match_count > 1 && best_count != match_count)
435     {
436       int i;
437
438       for (i = 0; i < match_count; i++)
439         {
440           right_targ = matching_vector[i];
441           if (right_targ->match_priority <= best_match)
442             break;
443         }
444       match_count = 1;
445     }
446
447   /* There is way too much undoing of half-known state here.  We
448      really shouldn't iterate on live bfd's.  Note that saving the
449      whole bfd and restoring it would be even worse; the first thing
450      you notice is that the cached bfd file position gets out of sync.  */
451   if (preserve.marker != NULL)
452     bfd_preserve_restore (abfd, &preserve);
453
454   if (match_count == 1)
455     {
456       abfd->xvec = right_targ;
457       /* If we come out of the loop knowing that the last target that
458          matched is the one we want, then ABFD should still be in a usable
459          state (except possibly for XVEC).  */
460       if (match_targ != right_targ)
461         {
462           bfd_reinit (abfd, initial_section_id);
463           if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
464             goto err_ret;
465           match_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
466           BFD_ASSERT (match_targ != NULL);
467         }
468
469     ok_ret:
470       /* If the file was opened for update, then `output_has_begun'
471          some time ago when the file was created.  Do not recompute
472          sections sizes or alignments in _bfd_set_section_contents.
473          We can not set this flag until after checking the format,
474          because it will interfere with creation of BFD sections.  */
475       if (abfd->direction == both_direction)
476         abfd->output_has_begun = TRUE;
477
478       if (matching_vector)
479         free (matching_vector);
480
481       /* File position has moved, BTW.  */
482       return TRUE;
483     }
484
485   if (match_count == 0)
486     {
487     err_unrecog:
488       bfd_set_error (bfd_error_file_not_recognized);
489     err_ret:
490       abfd->xvec = save_targ;
491       abfd->format = bfd_unknown;
492       if (matching_vector)
493         free (matching_vector);
494       if (preserve.marker != NULL)
495         bfd_preserve_restore (abfd, &preserve);
496       return FALSE;
497     }
498
499   /* Restore original target type and format.  */
500   abfd->xvec = save_targ;
501   abfd->format = bfd_unknown;
502   bfd_set_error (bfd_error_file_ambiguously_recognized);
503
504   if (matching)
505     {
506       *matching = (char **) matching_vector;
507       matching_vector[match_count] = NULL;
508       /* Return target names.  This is a little nasty.  Maybe we
509          should do another bfd_malloc?  */
510       while (--match_count >= 0)
511         {
512           const char *name = matching_vector[match_count]->name;
513           *(const char **) &matching_vector[match_count] = name;
514         }
515     }
516   return FALSE;
517 }
518
519 /*
520 FUNCTION
521         bfd_set_format
522
523 SYNOPSIS
524         bfd_boolean bfd_set_format (bfd *abfd, bfd_format format);
525
526 DESCRIPTION
527         This function sets the file format of the BFD @var{abfd} to the
528         format @var{format}. If the target set in the BFD does not
529         support the format requested, the format is invalid, or the BFD
530         is not open for writing, then an error occurs.
531 */
532
533 bfd_boolean
534 bfd_set_format (bfd *abfd, bfd_format format)
535 {
536   if (bfd_read_p (abfd)
537       || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
538     {
539       bfd_set_error (bfd_error_invalid_operation);
540       return FALSE;
541     }
542
543   if (abfd->format != bfd_unknown)
544     return abfd->format == format;
545
546   /* Presume the answer is yes.  */
547   abfd->format = format;
548
549   if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
550     {
551       abfd->format = bfd_unknown;
552       return FALSE;
553     }
554
555   return TRUE;
556 }
557
558 /*
559 FUNCTION
560         bfd_format_string
561
562 SYNOPSIS
563         const char *bfd_format_string (bfd_format format);
564
565 DESCRIPTION
566         Return a pointer to a const string
567         <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
568         depending upon the value of @var{format}.
569 */
570
571 const char *
572 bfd_format_string (bfd_format format)
573 {
574   if (((int) format < (int) bfd_unknown)
575       || ((int) format >= (int) bfd_type_end))
576     return "invalid";
577
578   switch (format)
579     {
580     case bfd_object:
581       return "object";          /* Linker/assembler/compiler output.  */
582     case bfd_archive:
583       return "archive";         /* Object archive file.  */
584     case bfd_core:
585       return "core";            /* Core dump.  */
586     default:
587       return "unknown";
588     }
589 }