* bfd.c (struct bfd_preserve, bfd_preserve_save, bfd_preserve_restore,
[external/binutils.git] / bfd / format.c
1 /* Generic BFD support for file formats.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001, 2002,
3    2003, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4    Written by Cygnus Support.
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23
24 /*
25 SECTION
26         File formats
27
28         A format is a BFD concept of high level file contents type. The
29         formats supported by BFD are:
30
31         o <<bfd_object>>
32
33         The BFD may contain data, symbols, relocations and debug info.
34
35         o <<bfd_archive>>
36
37         The BFD contains other BFDs and an optional index.
38
39         o <<bfd_core>>
40
41         The BFD contains the result of an executable core dump.
42
43 SUBSECTION
44         File format functions
45 */
46
47 #include "sysdep.h"
48 #include "bfd.h"
49 #include "libbfd.h"
50
51 /* IMPORT from targets.c.  */
52 extern const size_t _bfd_target_vector_entries;
53
54 /*
55 FUNCTION
56         bfd_check_format
57
58 SYNOPSIS
59         bfd_boolean bfd_check_format (bfd *abfd, bfd_format format);
60
61 DESCRIPTION
62         Verify if the file attached to the BFD @var{abfd} is compatible
63         with the format @var{format} (i.e., one of <<bfd_object>>,
64         <<bfd_archive>> or <<bfd_core>>).
65
66         If the BFD has been set to a specific target before the
67         call, only the named target and format combination is
68         checked. If the target has not been set, or has been set to
69         <<default>>, then all the known target backends is
70         interrogated to determine a match.  If the default target
71         matches, it is used.  If not, exactly one target must recognize
72         the file, or an error results.
73
74         The function returns <<TRUE>> on success, otherwise <<FALSE>>
75         with one of the following error codes:
76
77         o <<bfd_error_invalid_operation>> -
78         if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
79         <<bfd_core>>.
80
81         o <<bfd_error_system_call>> -
82         if an error occured during a read - even some file mismatches
83         can cause bfd_error_system_calls.
84
85         o <<file_not_recognised>> -
86         none of the backends recognised the file format.
87
88         o <<bfd_error_file_ambiguously_recognized>> -
89         more than one backend recognised the file format.
90 */
91
92 bfd_boolean
93 bfd_check_format (bfd *abfd, bfd_format format)
94 {
95   return bfd_check_format_matches (abfd, format, NULL);
96 }
97
98 struct bfd_preserve
99 {
100   void *marker;
101   void *tdata;
102   flagword flags;
103   const struct bfd_arch_info *arch_info;
104   struct bfd_section *sections;
105   struct bfd_section *section_last;
106   unsigned int section_count;
107   struct bfd_hash_table section_htab;
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
119 static bfd_boolean
120 bfd_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);
130   if (preserve->marker == NULL)
131     return FALSE;
132
133   return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
134                               sizeof (struct section_hash_entry));
135 }
136
137 /* Clear out a subset of BFD state.  */
138
139 static void
140 bfd_reinit (bfd *abfd)
141 {
142   abfd->tdata.any = NULL;
143   abfd->arch_info = &bfd_default_arch_struct;
144   abfd->flags &= BFD_FLAGS_SAVED;
145   bfd_section_list_clear (abfd);
146 }
147
148 /* Restores bfd state saved by bfd_preserve_save.  */
149
150 static void
151 bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
152 {
153   bfd_hash_table_free (&abfd->section_htab);
154
155   abfd->tdata.any = preserve->tdata;
156   abfd->arch_info = preserve->arch_info;
157   abfd->flags = preserve->flags;
158   abfd->section_htab = preserve->section_htab;
159   abfd->sections = preserve->sections;
160   abfd->section_last = preserve->section_last;
161   abfd->section_count = preserve->section_count;
162
163   /* bfd_release frees all memory more recently bfd_alloc'd than
164      its arg, as well as its arg.  */
165   bfd_release (abfd, preserve->marker);
166   preserve->marker = NULL;
167 }
168
169 /* Called when the bfd state saved by bfd_preserve_save is no longer
170    needed.  */
171
172 static void
173 bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
174 {
175   /* It would be nice to be able to free more memory here, eg. old
176      tdata, but that's not possible since these blocks are sitting
177      inside bfd_alloc'd memory.  The section hash is on a separate
178      objalloc.  */
179   bfd_hash_table_free (&preserve->section_htab);
180   preserve->marker = NULL;
181 }
182
183 /*
184 FUNCTION
185         bfd_check_format_matches
186
187 SYNOPSIS
188         bfd_boolean bfd_check_format_matches
189           (bfd *abfd, bfd_format format, char ***matching);
190
191 DESCRIPTION
192         Like <<bfd_check_format>>, except when it returns FALSE with
193         <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>.  In that
194         case, if @var{matching} is not NULL, it will be filled in with
195         a NULL-terminated list of the names of the formats that matched,
196         allocated with <<malloc>>.
197         Then the user may choose a format and try again.
198
199         When done with the list that @var{matching} points to, the caller
200         should free it.
201 */
202
203 bfd_boolean
204 bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
205 {
206   extern const bfd_target binary_vec;
207   const bfd_target * const *target;
208   const bfd_target **matching_vector = NULL;
209   const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
210   int match_count, best_count, best_match;
211   int ar_match_index;
212   struct bfd_preserve preserve;
213
214   if (matching != NULL)
215     *matching = NULL;
216
217   if (!bfd_read_p (abfd)
218       || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
219     {
220       bfd_set_error (bfd_error_invalid_operation);
221       return FALSE;
222     }
223
224   if (abfd->format != bfd_unknown)
225     return abfd->format == format;
226
227   if (matching != NULL || *bfd_associated_vector != NULL)
228     {
229       bfd_size_type amt;
230
231       amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
232       matching_vector = (const bfd_target **) bfd_malloc (amt);
233       if (!matching_vector)
234         return FALSE;
235     }
236
237   /* Presume the answer is yes.  */
238   abfd->format = format;
239   save_targ = abfd->xvec;
240   preserve.marker = NULL;
241
242   /* If the target type was explicitly specified, just check that target.  */
243   if (!abfd->target_defaulted)
244     {
245       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) /* rewind! */
246         goto err_ret;
247
248       right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
249
250       if (right_targ)
251         goto ok_ret;
252
253       /* For a long time the code has dropped through to check all
254          targets if the specified target was wrong.  I don't know why,
255          and I'm reluctant to change it.  However, in the case of an
256          archive, it can cause problems.  If the specified target does
257          not permit archives (e.g., the binary target), then we should
258          not allow some other target to recognize it as an archive, but
259          should instead allow the specified target to recognize it as an
260          object.  When I first made this change, it broke the PE target,
261          because the specified pei-i386 target did not recognize the
262          actual pe-i386 archive.  Since there may be other problems of
263          this sort, I changed this test to check only for the binary
264          target.  */
265       if (format == bfd_archive && save_targ == &binary_vec)
266         goto err_unrecog;
267     }
268
269   /* Since the target type was defaulted, check them all in the hope
270      that one will be uniquely recognized.  */
271   right_targ = NULL;
272   ar_right_targ = NULL;
273   match_targ = NULL;
274   best_match = 256;
275   best_count = 0;
276   match_count = 0;
277   ar_match_index = _bfd_target_vector_entries;
278
279   for (target = bfd_target_vector; *target != NULL; target++)
280     {
281       const bfd_target *temp;
282       bfd_error_type err;
283
284       /* Don't check the default target twice.  */
285       if (*target == &binary_vec
286           || (!abfd->target_defaulted && *target == save_targ)
287           || (*target)->match_priority > best_match)
288         continue;
289
290       /* If we already tried a match, the bfd is modified and may
291          have sections attached, which will confuse the next
292          _bfd_check_format call.  */
293       bfd_reinit (abfd);
294
295       /* Change BFD's target temporarily.  */
296       abfd->xvec = *target;
297
298       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
299         goto err_ret;
300
301       /* If _bfd_check_format neglects to set bfd_error, assume
302          bfd_error_wrong_format.  We didn't used to even pay any
303          attention to bfd_error, so I suspect that some
304          _bfd_check_format might have this problem.  */
305       bfd_set_error (bfd_error_wrong_format);
306
307       temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
308       if (temp)
309         {
310           match_targ = temp;
311           if (preserve.marker != NULL)
312             bfd_preserve_finish (abfd, &preserve);
313         }
314
315       if (temp && (abfd->format != bfd_archive || bfd_has_map (abfd)))
316         {
317           /* This format checks out as ok!  */
318           right_targ = temp;
319
320           /* If this is the default target, accept it, even if other
321              targets might match.  People who want those other targets
322              have to set the GNUTARGET variable.  */
323           if (temp == bfd_default_vector[0])
324             goto ok_ret;
325
326           if (matching_vector)
327             matching_vector[match_count] = temp;
328           match_count++;
329
330           if (temp->match_priority < best_match)
331             {
332               best_match = temp->match_priority;
333               best_count = 0;
334             }
335           best_count++;
336         }
337       else if (temp
338                || (err = bfd_get_error ()) == bfd_error_wrong_object_format
339                || err == bfd_error_file_ambiguously_recognized)
340         {
341           /* An archive with no armap or objects of the wrong type,
342              or an ambiguous match.  We want this target to match
343              if we get no better matches.  */
344           if (ar_right_targ != bfd_default_vector[0])
345             ar_right_targ = *target;
346           if (matching_vector)
347             matching_vector[ar_match_index] = *target;
348           ar_match_index++;
349         }
350       else if (err != bfd_error_wrong_format)
351         goto err_ret;
352
353       if (temp && !bfd_preserve_save (abfd, &preserve))
354         goto err_ret;
355     }
356
357   if (best_count == 1)
358     match_count = 1;
359
360   if (match_count == 0)
361     {
362       /* Try partial matches.  */
363       right_targ = ar_right_targ;
364
365       if (right_targ == bfd_default_vector[0])
366         {
367           match_count = 1;
368         }
369       else
370         {
371           match_count = ar_match_index - _bfd_target_vector_entries;
372
373           if (matching_vector && match_count > 1)
374             memcpy (matching_vector,
375                     matching_vector + _bfd_target_vector_entries,
376                     sizeof (*matching_vector) * match_count);
377         }
378     }
379
380   if (match_count > 1)
381     {
382       const bfd_target * const *assoc = bfd_associated_vector;
383
384       while ((right_targ = *assoc++) != NULL)
385         {
386           int i = match_count;
387
388           while (--i >= 0)
389             if (matching_vector[i] == right_targ)
390               break;
391
392           if (i >= 0)
393             {
394               match_count = 1;
395               break;
396             }
397         }
398     }
399
400   /* There is way too much undoing of half-known state here.  We
401      really shouldn't iterate on live bfd's.  Note that saving the
402      whole bfd and restoring it would be even worse; the first thing
403      you notice is that the cached bfd file position gets out of sync.  */
404   if (preserve.marker != NULL)
405     bfd_preserve_restore (abfd, &preserve);
406
407   if (match_count == 1)
408     {
409       abfd->xvec = right_targ;
410       /* If we come out of the loop knowing that the last target that
411          matched is the one we want, then ABFD should still be in a usable
412          state (except possibly for XVEC).  */
413       if (match_targ != right_targ)
414         {
415           bfd_reinit (abfd);
416           if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
417             goto err_ret;
418           match_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
419           BFD_ASSERT (match_targ != NULL);
420         }
421
422     ok_ret:
423       /* If the file was opened for update, then `output_has_begun'
424          some time ago when the file was created.  Do not recompute
425          sections sizes or alignments in _bfd_set_section_contents.
426          We can not set this flag until after checking the format,
427          because it will interfere with creation of BFD sections.  */
428       if (abfd->direction == both_direction)
429         abfd->output_has_begun = TRUE;
430
431       if (matching_vector)
432         free (matching_vector);
433
434       /* File position has moved, BTW.  */
435       return TRUE;
436     }
437
438   if (match_count == 0)
439     {
440     err_unrecog:
441       bfd_set_error (bfd_error_file_not_recognized);
442     err_ret:
443       abfd->xvec = save_targ;
444       abfd->format = bfd_unknown;
445       if (matching_vector)
446         free (matching_vector);
447       if (preserve.marker != NULL)
448         bfd_preserve_restore (abfd, &preserve);
449       return FALSE;
450     }
451
452   /* Restore original target type and format.  */
453   abfd->xvec = save_targ;
454   abfd->format = bfd_unknown;
455   bfd_set_error (bfd_error_file_ambiguously_recognized);
456
457   if (matching)
458     {
459       *matching = (char **) matching_vector;
460       matching_vector[match_count] = NULL;
461       /* Return target names.  This is a little nasty.  Maybe we
462          should do another bfd_malloc?  */
463       while (--match_count >= 0)
464         {
465           const char *name = matching_vector[match_count]->name;
466           *(const char **) &matching_vector[match_count] = name;
467         }
468     }
469   return FALSE;
470 }
471
472 /*
473 FUNCTION
474         bfd_set_format
475
476 SYNOPSIS
477         bfd_boolean bfd_set_format (bfd *abfd, bfd_format format);
478
479 DESCRIPTION
480         This function sets the file format of the BFD @var{abfd} to the
481         format @var{format}. If the target set in the BFD does not
482         support the format requested, the format is invalid, or the BFD
483         is not open for writing, then an error occurs.
484 */
485
486 bfd_boolean
487 bfd_set_format (bfd *abfd, bfd_format format)
488 {
489   if (bfd_read_p (abfd)
490       || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
491     {
492       bfd_set_error (bfd_error_invalid_operation);
493       return FALSE;
494     }
495
496   if (abfd->format != bfd_unknown)
497     return abfd->format == format;
498
499   /* Presume the answer is yes.  */
500   abfd->format = format;
501
502   if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
503     {
504       abfd->format = bfd_unknown;
505       return FALSE;
506     }
507
508   return TRUE;
509 }
510
511 /*
512 FUNCTION
513         bfd_format_string
514
515 SYNOPSIS
516         const char *bfd_format_string (bfd_format format);
517
518 DESCRIPTION
519         Return a pointer to a const string
520         <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
521         depending upon the value of @var{format}.
522 */
523
524 const char *
525 bfd_format_string (bfd_format format)
526 {
527   if (((int) format < (int) bfd_unknown)
528       || ((int) format >= (int) bfd_type_end))
529     return "invalid";
530
531   switch (format)
532     {
533     case bfd_object:
534       return "object";          /* Linker/assembler/compiler output.  */
535     case bfd_archive:
536       return "archive";         /* Object archive file.  */
537     case bfd_core:
538       return "core";            /* Core dump.  */
539     default:
540       return "unknown";
541     }
542 }