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