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