]>
Commit | Line | Data |
---|---|---|
6724ff46 RP |
1 | /* Generic BFD support for file formats. |
2 | Copyright (C) 1990-1991 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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | /*doc* | |
22 | @section File Formats | |
23 | A format is a BFD concept of high level file contents. The | |
24 | formats supported by BFD are: | |
25 | @table @code | |
26 | @item bfd_object | |
27 | The BFD may contain data, symbols, relocations and debug info. | |
28 | @item bfd_archive | |
29 | The BFD contains other BFDs and an optional index. | |
30 | @item bfd_core | |
31 | The BFD contains the result of an executable core dump. | |
32 | @end table | |
33 | */ | |
34 | #include "sysdep.h" | |
35 | #include "bfd.h" | |
36 | #include "libbfd.h" | |
37 | ||
38 | ||
39 | extern bfd_target *target_vector[]; | |
40 | extern bfd_target *default_vector[]; | |
41 | ||
42 | ||
43 | /*proto* | |
44 | *i bfd_check_format | |
45 | This routine is supplied a BFD and a format. It attempts to verify if | |
46 | the file attatched to the BFD is indeed compatible with the format | |
47 | specified (ie, one of @code{bfd_object}, @code{bfd_archive} or | |
48 | @code{bfd_core}). | |
49 | ||
50 | If the BFD has been set to a specific @var{target} before the call, | |
51 | only the named target and format combination will be checked. If the | |
52 | target has not been set, or has been set to @code{default} then all | |
53 | the known target backends will be interrogated to determine a match. | |
54 | ||
55 | The function returns @code{true} on success, otherwise @code{false} | |
56 | with one of the following error codes: | |
57 | @table @code | |
58 | @item | |
59 | invalid_operation | |
60 | if @code{format} is not one of @code{bfd_object}, @code{bfd_archive} | |
61 | or @code{bfd_core}. | |
62 | @item system_call_error | |
63 | if an error occured during a read - even some file mismatches can | |
64 | cause system_call_errros | |
65 | @item file_not_recognised | |
66 | none of the backends recognised the file format | |
67 | @item file_ambiguously_recognized | |
68 | more than one backend recognised the file format. | |
69 | @end table | |
70 | *; PROTO(boolean, bfd_check_format, (bfd *abfd, bfd_format format)); | |
71 | *-*/ | |
72 | ||
73 | boolean | |
74 | DEFUN(bfd_check_format,(abfd, format), | |
75 | bfd *abfd AND | |
76 | bfd_format format) | |
77 | { | |
78 | bfd_target **target, *save_targ, *right_targ; | |
79 | int match_count; | |
80 | ||
81 | if (!bfd_read_p (abfd) || | |
82 | ((int)(abfd->format) < (int)bfd_unknown) || | |
83 | ((int)(abfd->format) >= (int)bfd_type_end)) { | |
84 | bfd_error = invalid_operation; | |
85 | return false; | |
86 | } | |
87 | ||
88 | if (abfd->format != bfd_unknown) | |
89 | return (abfd->format == format)? true: false; | |
90 | ||
91 | /* presume the answer is yes */ | |
92 | abfd->format = format; | |
93 | ||
94 | /* If the target type was explicitly specified, just check that target. */ | |
95 | ||
96 | if (!abfd->target_defaulted) { | |
97 | bfd_seek (abfd, (file_ptr)0, SEEK_SET); /* rewind! */ | |
98 | ||
99 | right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); | |
100 | if (right_targ) { | |
101 | abfd->xvec = right_targ; /* Set the target as returned */ | |
102 | return true; /* File position has moved, BTW */ | |
103 | } | |
104 | abfd->format = bfd_unknown; | |
105 | return false; /* Specified target is not right */ | |
106 | } | |
107 | ||
108 | /* Since the target type was defaulted, check them | |
109 | all in the hope that one will be uniquely recognized. */ | |
110 | ||
111 | save_targ = abfd->xvec; | |
112 | match_count = 0; | |
113 | right_targ = 0; | |
114 | ||
115 | for (target = target_vector; *target != NULL; target++) { | |
116 | bfd_target *temp; | |
117 | ||
118 | abfd->xvec = *target; /* Change BFD's target temporarily */ | |
119 | bfd_seek (abfd, (file_ptr)0, SEEK_SET); | |
120 | temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); | |
121 | if (temp) { /* This format checks out as ok! */ | |
122 | right_targ = temp; | |
123 | match_count++; | |
124 | /* If this is the default target, accept it, even if other targets | |
125 | might match. People who want those other targets have to set | |
126 | the GNUTARGET variable. */ | |
127 | if (temp == default_vector[0]) | |
128 | break; | |
129 | #ifdef GNU960 | |
130 | /* Big- and little-endian b.out archives look the same, but it doesn't | |
131 | * matter: there is no difference in their headers, and member file byte | |
132 | * orders will (I hope) be handled appropriately by bfd. Ditto for big | |
133 | * and little coff archives. And the 4 coff/b.out object formats are | |
134 | * unambiguous. So accept the first match we find. | |
135 | */ | |
136 | break; | |
137 | #endif | |
138 | } | |
139 | } | |
140 | ||
141 | if (match_count == 1) { | |
142 | abfd->xvec = right_targ; /* Change BFD's target permanently */ | |
143 | return true; /* File position has moved, BTW */ | |
144 | } | |
145 | ||
146 | abfd->xvec = save_targ; /* Restore original target type */ | |
147 | abfd->format = bfd_unknown; /* Restore original format */ | |
148 | bfd_error = ((match_count == 0) ? file_not_recognized : | |
149 | file_ambiguously_recognized); | |
150 | return false; | |
151 | } | |
152 | /*proto* | |
153 | *i bfd_set_format | |
154 | This function sets the file format of the supplied BFD to the format | |
155 | requested. If the target set in the BFD does not support the format | |
156 | requested, the format is illegal or the BFD is not open for writing | |
157 | than an error occurs. | |
158 | *; PROTO(boolean,bfd_set_format,(bfd *, bfd_format)); | |
159 | *-*/ | |
160 | boolean | |
161 | DEFUN(bfd_set_format,(abfd, format), | |
162 | bfd *abfd AND | |
163 | bfd_format format) | |
164 | { | |
165 | ||
166 | if (bfd_read_p (abfd) || | |
167 | ((int)abfd->format < (int)bfd_unknown) || | |
168 | ((int)abfd->format >= (int)bfd_type_end)) { | |
169 | bfd_error = invalid_operation; | |
170 | return false; | |
171 | } | |
172 | ||
173 | if (abfd->format != bfd_unknown) | |
174 | return (abfd->format == format) ? true:false; | |
175 | ||
176 | /* presume the answer is yes */ | |
177 | abfd->format = format; | |
178 | ||
179 | if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) { | |
180 | abfd->format = bfd_unknown; | |
181 | return false; | |
182 | } | |
183 | ||
184 | return true; | |
185 | } | |
186 | ||
187 | ||
188 | /*proto* | |
189 | *i bfd_format_string | |
190 | This function takes one argument, and enumerated type (bfd_format) and | |
191 | returns a pointer to a const string "invalid", "object", "archive", | |
192 | "core" or "unknown" depending upon the value of the enumeration. | |
193 | *; PROTO(CONST char *, bfd_format_string, (bfd_format)); | |
194 | *-*/ | |
195 | ||
196 | CONST char * | |
197 | DEFUN(bfd_format_string,(format), | |
198 | bfd_format format) | |
199 | { | |
200 | if (((int)format <(int) bfd_unknown) | |
201 | || ((int)format >=(int) bfd_type_end)) | |
202 | return "invalid"; | |
203 | ||
204 | switch (format) { | |
205 | case bfd_object: | |
206 | return "object"; /* linker/assember/compiler output */ | |
207 | case bfd_archive: | |
208 | return "archive"; /* object archive file */ | |
209 | case bfd_core: | |
210 | return "core"; /* core dump */ | |
211 | default: | |
212 | return "unknown"; | |
213 | } | |
214 | } |