xref: /openbsd-src/gnu/usr.bin/binutils/bfd/format.c (revision 007c2a4539b8b8aaa95c5e73e77620090abe113b)
12159047fSniklas /* Generic BFD support for file formats.
2c074d1c9Sdrahn    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2003
3b55d4692Sfgsch    Free Software Foundation, Inc.
42159047fSniklas    Written by Cygnus Support.
52159047fSniklas 
62159047fSniklas    This file is part of BFD, the Binary File Descriptor library.
72159047fSniklas 
82159047fSniklas    This program is free software; you can redistribute it and/or modify
92159047fSniklas    it under the terms of the GNU General Public License as published by
102159047fSniklas    the Free Software Foundation; either version 2 of the License, or
112159047fSniklas    (at your option) any later version.
122159047fSniklas 
132159047fSniklas    This program is distributed in the hope that it will be useful,
142159047fSniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
152159047fSniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
162159047fSniklas    GNU General Public License for more details.
172159047fSniklas 
182159047fSniklas    You should have received a copy of the GNU General Public License
192159047fSniklas    along with this program; if not, write to the Free Software
202159047fSniklas    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
212159047fSniklas 
222159047fSniklas /*
232159047fSniklas SECTION
242159047fSniklas 	File formats
252159047fSniklas 
262159047fSniklas 	A format is a BFD concept of high level file contents type. The
272159047fSniklas 	formats supported by BFD are:
282159047fSniklas 
292159047fSniklas 	o <<bfd_object>>
302159047fSniklas 
312159047fSniklas 	The BFD may contain data, symbols, relocations and debug info.
322159047fSniklas 
332159047fSniklas 	o <<bfd_archive>>
342159047fSniklas 
352159047fSniklas 	The BFD contains other BFDs and an optional index.
362159047fSniklas 
372159047fSniklas 	o <<bfd_core>>
382159047fSniklas 
392159047fSniklas 	The BFD contains the result of an executable core dump.
402159047fSniklas 
412159047fSniklas */
422159047fSniklas 
432159047fSniklas #include "bfd.h"
442159047fSniklas #include "sysdep.h"
452159047fSniklas #include "libbfd.h"
462159047fSniklas 
472159047fSniklas /* IMPORT from targets.c.  */
482159047fSniklas extern const size_t _bfd_target_vector_entries;
492159047fSniklas 
502159047fSniklas /*
512159047fSniklas FUNCTION
522159047fSniklas 	bfd_check_format
532159047fSniklas 
542159047fSniklas SYNOPSIS
55c074d1c9Sdrahn 	bfd_boolean bfd_check_format (bfd *abfd, bfd_format format);
562159047fSniklas 
572159047fSniklas DESCRIPTION
582159047fSniklas 	Verify if the file attached to the BFD @var{abfd} is compatible
592159047fSniklas 	with the format @var{format} (i.e., one of <<bfd_object>>,
602159047fSniklas 	<<bfd_archive>> or <<bfd_core>>).
612159047fSniklas 
622159047fSniklas 	If the BFD has been set to a specific target before the
632159047fSniklas 	call, only the named target and format combination is
642159047fSniklas 	checked. If the target has not been set, or has been set to
652159047fSniklas 	<<default>>, then all the known target backends is
662159047fSniklas 	interrogated to determine a match.  If the default target
672159047fSniklas 	matches, it is used.  If not, exactly one target must recognize
682159047fSniklas 	the file, or an error results.
692159047fSniklas 
70c074d1c9Sdrahn 	The function returns <<TRUE>> on success, otherwise <<FALSE>>
712159047fSniklas 	with one of the following error codes:
722159047fSniklas 
732159047fSniklas 	o <<bfd_error_invalid_operation>> -
742159047fSniklas 	if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
752159047fSniklas 	<<bfd_core>>.
762159047fSniklas 
772159047fSniklas 	o <<bfd_error_system_call>> -
782159047fSniklas 	if an error occured during a read - even some file mismatches
792159047fSniklas 	can cause bfd_error_system_calls.
802159047fSniklas 
812159047fSniklas 	o <<file_not_recognised>> -
822159047fSniklas 	none of the backends recognised the file format.
832159047fSniklas 
842159047fSniklas 	o <<bfd_error_file_ambiguously_recognized>> -
852159047fSniklas 	more than one backend recognised the file format.
862159047fSniklas */
872159047fSniklas 
88c074d1c9Sdrahn bfd_boolean
bfd_check_format(bfd * abfd,bfd_format format)89*007c2a45Smiod bfd_check_format (bfd *abfd, bfd_format format)
902159047fSniklas {
912159047fSniklas   return bfd_check_format_matches (abfd, format, NULL);
922159047fSniklas }
932159047fSniklas 
942159047fSniklas /*
952159047fSniklas FUNCTION
962159047fSniklas 	bfd_check_format_matches
972159047fSniklas 
982159047fSniklas SYNOPSIS
99*007c2a45Smiod 	bfd_boolean bfd_check_format_matches
100*007c2a45Smiod 	  (bfd *abfd, bfd_format format, char ***matching);
1012159047fSniklas 
1022159047fSniklas DESCRIPTION
103c074d1c9Sdrahn 	Like <<bfd_check_format>>, except when it returns FALSE with
1042159047fSniklas 	<<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>.  In that
1052159047fSniklas 	case, if @var{matching} is not NULL, it will be filled in with
1062159047fSniklas 	a NULL-terminated list of the names of the formats that matched,
1072159047fSniklas 	allocated with <<malloc>>.
1082159047fSniklas 	Then the user may choose a format and try again.
1092159047fSniklas 
1102159047fSniklas 	When done with the list that @var{matching} points to, the caller
1112159047fSniklas 	should free it.
1122159047fSniklas */
1132159047fSniklas 
114c074d1c9Sdrahn bfd_boolean
bfd_check_format_matches(bfd * abfd,bfd_format format,char *** matching)115*007c2a45Smiod bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
1162159047fSniklas {
117b305b0f1Sespie   extern const bfd_target binary_vec;
118c074d1c9Sdrahn   const bfd_target * const *target;
119c074d1c9Sdrahn   const bfd_target **matching_vector = NULL;
120c074d1c9Sdrahn   const bfd_target *save_targ, *right_targ, *ar_right_targ;
1212159047fSniklas   int match_count;
122c074d1c9Sdrahn   int ar_match_index;
1232159047fSniklas 
124c074d1c9Sdrahn   if (!bfd_read_p (abfd)
125c074d1c9Sdrahn       || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
126b55d4692Sfgsch     {
1272159047fSniklas       bfd_set_error (bfd_error_invalid_operation);
128c074d1c9Sdrahn       return FALSE;
1292159047fSniklas     }
1302159047fSniklas 
1312159047fSniklas   if (abfd->format != bfd_unknown)
132c074d1c9Sdrahn     return abfd->format == format;
1332159047fSniklas 
1342159047fSniklas   /* Since the target type was defaulted, check them
1352159047fSniklas      all in the hope that one will be uniquely recognized.  */
1362159047fSniklas   save_targ = abfd->xvec;
1372159047fSniklas   match_count = 0;
138c074d1c9Sdrahn   ar_match_index = _bfd_target_vector_entries;
139b55d4692Sfgsch 
1402159047fSniklas   if (matching)
1412159047fSniklas     {
142c074d1c9Sdrahn       bfd_size_type amt;
143c074d1c9Sdrahn 
144c074d1c9Sdrahn       *matching = NULL;
145c074d1c9Sdrahn       amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
146*007c2a45Smiod       matching_vector = bfd_malloc (amt);
1472159047fSniklas       if (!matching_vector)
148c074d1c9Sdrahn 	return FALSE;
1492159047fSniklas     }
150b55d4692Sfgsch 
1512159047fSniklas   right_targ = 0;
152c074d1c9Sdrahn   ar_right_targ = 0;
1532159047fSniklas 
154b55d4692Sfgsch   /* Presume the answer is yes.  */
1552159047fSniklas   abfd->format = format;
1562159047fSniklas 
1572159047fSniklas   /* If the target type was explicitly specified, just check that target.  */
158b55d4692Sfgsch   if (!abfd->target_defaulted)
159b55d4692Sfgsch     {
1602159047fSniklas       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)	/* rewind! */
161c074d1c9Sdrahn 	{
162c074d1c9Sdrahn 	  if (matching)
163*007c2a45Smiod 	    free (matching_vector);
164c074d1c9Sdrahn 	  return FALSE;
165c074d1c9Sdrahn 	}
166b55d4692Sfgsch 
1672159047fSniklas       right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
168b55d4692Sfgsch 
169b55d4692Sfgsch       if (right_targ)
170b55d4692Sfgsch 	{
171b55d4692Sfgsch 	  abfd->xvec = right_targ;	/* Set the target as returned.  */
172b55d4692Sfgsch 
1732159047fSniklas 	  if (matching)
174*007c2a45Smiod 	    free (matching_vector);
175b55d4692Sfgsch 
176c074d1c9Sdrahn 	  return TRUE;			/* File position has moved, BTW.  */
1772159047fSniklas 	}
178b305b0f1Sespie 
179b305b0f1Sespie       /* For a long time the code has dropped through to check all
180b305b0f1Sespie 	 targets if the specified target was wrong.  I don't know why,
181b305b0f1Sespie 	 and I'm reluctant to change it.  However, in the case of an
182b305b0f1Sespie 	 archive, it can cause problems.  If the specified target does
183b305b0f1Sespie 	 not permit archives (e.g., the binary target), then we should
184b305b0f1Sespie 	 not allow some other target to recognize it as an archive, but
185b305b0f1Sespie 	 should instead allow the specified target to recognize it as an
186b305b0f1Sespie 	 object.  When I first made this change, it broke the PE target,
187b305b0f1Sespie 	 because the specified pei-i386 target did not recognize the
188b305b0f1Sespie 	 actual pe-i386 archive.  Since there may be other problems of
189b305b0f1Sespie 	 this sort, I changed this test to check only for the binary
190b305b0f1Sespie 	 target.  */
191b305b0f1Sespie       if (format == bfd_archive && save_targ == &binary_vec)
192b305b0f1Sespie 	{
193b305b0f1Sespie 	  abfd->xvec = save_targ;
194b305b0f1Sespie 	  abfd->format = bfd_unknown;
195b55d4692Sfgsch 
196b305b0f1Sespie 	  if (matching)
197*007c2a45Smiod 	    free (matching_vector);
198b55d4692Sfgsch 
199b305b0f1Sespie 	  bfd_set_error (bfd_error_file_not_recognized);
200b55d4692Sfgsch 
201c074d1c9Sdrahn 	  return FALSE;
202b305b0f1Sespie 	}
2032159047fSniklas     }
2042159047fSniklas 
205b55d4692Sfgsch   for (target = bfd_target_vector; *target != NULL; target++)
206b55d4692Sfgsch     {
2072159047fSniklas       const bfd_target *temp;
208c074d1c9Sdrahn       bfd_error_type err;
2092159047fSniklas 
2102159047fSniklas       if (*target == &binary_vec)
2112159047fSniklas 	continue;
2122159047fSniklas 
213c074d1c9Sdrahn       abfd->xvec = *target;	/* Change BFD's target temporarily.  */
214b55d4692Sfgsch 
2152159047fSniklas       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
216c074d1c9Sdrahn 	{
217c074d1c9Sdrahn 	  if (matching)
218*007c2a45Smiod 	    free (matching_vector);
219c074d1c9Sdrahn 	  return FALSE;
220c074d1c9Sdrahn 	}
221b55d4692Sfgsch 
222b55d4692Sfgsch       /* If _bfd_check_format neglects to set bfd_error, assume
223b55d4692Sfgsch 	 bfd_error_wrong_format.  We didn't used to even pay any
224b55d4692Sfgsch 	 attention to bfd_error, so I suspect that some
225b55d4692Sfgsch 	 _bfd_check_format might have this problem.  */
2262159047fSniklas       bfd_set_error (bfd_error_wrong_format);
227b55d4692Sfgsch 
2282159047fSniklas       temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
229b55d4692Sfgsch 
230b55d4692Sfgsch       if (temp)
2312159047fSniklas 	{
232c074d1c9Sdrahn 	  /* This format checks out as ok!  */
233c074d1c9Sdrahn 	  right_targ = temp;
234b55d4692Sfgsch 
235b55d4692Sfgsch 	  /* If this is the default target, accept it, even if other
236b55d4692Sfgsch 	     targets might match.  People who want those other targets
237b55d4692Sfgsch 	     have to set the GNUTARGET variable.  */
2382159047fSniklas 	  if (temp == bfd_default_vector[0])
2392159047fSniklas 	    {
2402159047fSniklas 	      match_count = 1;
2412159047fSniklas 	      break;
2422159047fSniklas 	    }
243c074d1c9Sdrahn 
244c074d1c9Sdrahn 	  if (matching)
245c074d1c9Sdrahn 	    matching_vector[match_count] = temp;
246c074d1c9Sdrahn 
247c074d1c9Sdrahn 	  match_count++;
248c074d1c9Sdrahn 
2492159047fSniklas #ifdef GNU960
250b55d4692Sfgsch 	  /* Big- and little-endian b.out archives look the same, but it
251b55d4692Sfgsch 	     doesn't matter: there is no difference in their headers, and
252b55d4692Sfgsch 	     member file byte orders will (I hope) be handled appropriately
253b55d4692Sfgsch 	     by bfd.  Ditto for big and little coff archives.  And the 4
254b55d4692Sfgsch 	     coff/b.out object formats are unambiguous.  So accept the
255b55d4692Sfgsch 	     first match we find.  */
2562159047fSniklas 	  break;
2572159047fSniklas #endif
258b55d4692Sfgsch 	}
259c074d1c9Sdrahn       else if ((err = bfd_get_error ()) == bfd_error_wrong_object_format
260c074d1c9Sdrahn 	       || err == bfd_error_file_ambiguously_recognized)
261c074d1c9Sdrahn 	{
262c074d1c9Sdrahn 	  /* An archive with objects of the wrong type, or an
263c074d1c9Sdrahn 	     ambiguous match.  We want this target to match if we get
264c074d1c9Sdrahn 	     no better matches.  */
265c074d1c9Sdrahn 	  if (ar_right_targ != bfd_default_vector[0])
266c074d1c9Sdrahn 	    ar_right_targ = *target;
267c074d1c9Sdrahn 	  if (matching)
268c074d1c9Sdrahn 	    matching_vector[ar_match_index] = *target;
269c074d1c9Sdrahn 	  ar_match_index++;
270c074d1c9Sdrahn 	}
271c074d1c9Sdrahn       else if (err != bfd_error_wrong_format)
272b55d4692Sfgsch 	{
2732159047fSniklas 	  abfd->xvec = save_targ;
2742159047fSniklas 	  abfd->format = bfd_unknown;
275b55d4692Sfgsch 
276c074d1c9Sdrahn 	  if (matching)
277*007c2a45Smiod 	    free (matching_vector);
278b55d4692Sfgsch 
279c074d1c9Sdrahn 	  return FALSE;
280c074d1c9Sdrahn 	}
281c074d1c9Sdrahn     }
282c074d1c9Sdrahn 
283c074d1c9Sdrahn   if (match_count == 0)
284c074d1c9Sdrahn     {
285c074d1c9Sdrahn       /* Try partial matches.  */
286c074d1c9Sdrahn       right_targ = ar_right_targ;
287c074d1c9Sdrahn 
288c074d1c9Sdrahn       if (right_targ == bfd_default_vector[0])
289c074d1c9Sdrahn 	{
290c074d1c9Sdrahn 	  match_count = 1;
291c074d1c9Sdrahn 	}
292c074d1c9Sdrahn       else
293c074d1c9Sdrahn 	{
294c074d1c9Sdrahn 	  match_count = ar_match_index - _bfd_target_vector_entries;
295c074d1c9Sdrahn 
296c074d1c9Sdrahn 	  if (matching && match_count > 1)
297c074d1c9Sdrahn 	    memcpy (matching_vector,
298c074d1c9Sdrahn 		    matching_vector + _bfd_target_vector_entries,
299c074d1c9Sdrahn 		    sizeof (*matching_vector) * match_count);
300c074d1c9Sdrahn 	}
301c074d1c9Sdrahn     }
302c074d1c9Sdrahn 
303c074d1c9Sdrahn   if (match_count > 1
304c074d1c9Sdrahn       && bfd_associated_vector != NULL
305c074d1c9Sdrahn       && matching)
306c074d1c9Sdrahn     {
307c074d1c9Sdrahn       const bfd_target * const *assoc = bfd_associated_vector;
308c074d1c9Sdrahn 
309c074d1c9Sdrahn       while ((right_targ = *assoc++) != NULL)
310c074d1c9Sdrahn 	{
311c074d1c9Sdrahn 	  int i = match_count;
312c074d1c9Sdrahn 
313c074d1c9Sdrahn 	  while (--i >= 0)
314c074d1c9Sdrahn 	    if (matching_vector[i] == right_targ)
315c074d1c9Sdrahn 	      break;
316c074d1c9Sdrahn 
317c074d1c9Sdrahn 	  if (i >= 0)
318c074d1c9Sdrahn 	    {
319c074d1c9Sdrahn 	      match_count = 1;
320c074d1c9Sdrahn 	      break;
321c074d1c9Sdrahn 	    }
3222159047fSniklas 	}
3232159047fSniklas     }
3242159047fSniklas 
325b55d4692Sfgsch   if (match_count == 1)
326b55d4692Sfgsch     {
327b55d4692Sfgsch       abfd->xvec = right_targ;		/* Change BFD's target permanently.  */
328b55d4692Sfgsch 
3292159047fSniklas       if (matching)
330*007c2a45Smiod 	free (matching_vector);
331b55d4692Sfgsch 
332c074d1c9Sdrahn       return TRUE;			/* File position has moved, BTW.  */
3332159047fSniklas     }
3342159047fSniklas 
335b55d4692Sfgsch   abfd->xvec = save_targ;		/* Restore original target type.  */
336b55d4692Sfgsch   abfd->format = bfd_unknown;		/* Restore original format.  */
337b55d4692Sfgsch 
3382159047fSniklas   if (match_count == 0)
3392159047fSniklas     {
3402159047fSniklas       bfd_set_error (bfd_error_file_not_recognized);
341b55d4692Sfgsch 
3422159047fSniklas       if (matching)
343*007c2a45Smiod 	free (matching_vector);
3442159047fSniklas     }
3452159047fSniklas   else
346c074d1c9Sdrahn     {
3472159047fSniklas       bfd_set_error (bfd_error_file_ambiguously_recognized);
348b55d4692Sfgsch 
349c074d1c9Sdrahn       if (matching)
350c074d1c9Sdrahn 	{
351c074d1c9Sdrahn 	  *matching = (char **) matching_vector;
352c074d1c9Sdrahn 	  matching_vector[match_count] = NULL;
353c074d1c9Sdrahn 	  /* Return target names.  This is a little nasty.  Maybe we
354c074d1c9Sdrahn 	     should do another bfd_malloc?  */
355c074d1c9Sdrahn 	  while (--match_count >= 0)
356c074d1c9Sdrahn 	    {
357c074d1c9Sdrahn 	      const char *name = matching_vector[match_count]->name;
358c074d1c9Sdrahn 	      *(const char **) &matching_vector[match_count] = name;
359c074d1c9Sdrahn 	    }
360c074d1c9Sdrahn 	}
361c074d1c9Sdrahn     }
362c074d1c9Sdrahn 
363c074d1c9Sdrahn   return FALSE;
3642159047fSniklas }
3652159047fSniklas 
3662159047fSniklas /*
3672159047fSniklas FUNCTION
3682159047fSniklas 	bfd_set_format
3692159047fSniklas 
3702159047fSniklas SYNOPSIS
371c074d1c9Sdrahn 	bfd_boolean bfd_set_format (bfd *abfd, bfd_format format);
3722159047fSniklas 
3732159047fSniklas DESCRIPTION
3742159047fSniklas 	This function sets the file format of the BFD @var{abfd} to the
3752159047fSniklas 	format @var{format}. If the target set in the BFD does not
3762159047fSniklas 	support the format requested, the format is invalid, or the BFD
3772159047fSniklas 	is not open for writing, then an error occurs.
3782159047fSniklas */
3792159047fSniklas 
380c074d1c9Sdrahn bfd_boolean
bfd_set_format(bfd * abfd,bfd_format format)381*007c2a45Smiod bfd_set_format (bfd *abfd, bfd_format format)
3822159047fSniklas {
383c074d1c9Sdrahn   if (bfd_read_p (abfd)
384c074d1c9Sdrahn       || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
385b55d4692Sfgsch     {
3862159047fSniklas       bfd_set_error (bfd_error_invalid_operation);
387c074d1c9Sdrahn       return FALSE;
3882159047fSniklas     }
3892159047fSniklas 
3902159047fSniklas   if (abfd->format != bfd_unknown)
391c074d1c9Sdrahn     return abfd->format == format;
3922159047fSniklas 
393b55d4692Sfgsch   /* Presume the answer is yes.  */
3942159047fSniklas   abfd->format = format;
3952159047fSniklas 
396b55d4692Sfgsch   if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
397b55d4692Sfgsch     {
3982159047fSniklas       abfd->format = bfd_unknown;
399c074d1c9Sdrahn       return FALSE;
4002159047fSniklas     }
4012159047fSniklas 
402c074d1c9Sdrahn   return TRUE;
4032159047fSniklas }
4042159047fSniklas 
4052159047fSniklas /*
4062159047fSniklas FUNCTION
4072159047fSniklas 	bfd_format_string
4082159047fSniklas 
4092159047fSniklas SYNOPSIS
410c074d1c9Sdrahn 	const char *bfd_format_string (bfd_format format);
4112159047fSniklas 
4122159047fSniklas DESCRIPTION
4132159047fSniklas 	Return a pointer to a const string
4142159047fSniklas 	<<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
4152159047fSniklas 	depending upon the value of @var{format}.
4162159047fSniklas */
4172159047fSniklas 
418c074d1c9Sdrahn const char *
bfd_format_string(bfd_format format)419*007c2a45Smiod bfd_format_string (bfd_format format)
4202159047fSniklas {
4212159047fSniklas   if (((int) format < (int) bfd_unknown)
4222159047fSniklas       || ((int) format >= (int) bfd_type_end))
4232159047fSniklas     return "invalid";
4242159047fSniklas 
425b55d4692Sfgsch   switch (format)
426b55d4692Sfgsch     {
4272159047fSniklas     case bfd_object:
428*007c2a45Smiod       return "object";		/* Linker/assembler/compiler output.  */
4292159047fSniklas     case bfd_archive:
430b55d4692Sfgsch       return "archive";		/* Object archive file.  */
4312159047fSniklas     case bfd_core:
432b55d4692Sfgsch       return "core";		/* Core dump.  */
4332159047fSniklas     default:
4342159047fSniklas       return "unknown";
4352159047fSniklas     }
4362159047fSniklas }
437