Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Generic BFD support for file formats. |
b3adc24a | 2 | Copyright (C) 1990-2020 Free Software Foundation, Inc. |
252b5132 RH |
3 | Written by Cygnus Support. |
4 | ||
ed781d5d | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
ed781d5d NC |
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 | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
ed781d5d | 10 | (at your option) any later version. |
252b5132 | 11 | |
ed781d5d NC |
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. | |
252b5132 | 16 | |
ed781d5d NC |
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 | |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
21 | ||
252b5132 RH |
22 | |
23 | /* | |
24 | SECTION | |
25 | File formats | |
26 | ||
27 | A format is a BFD concept of high level file contents type. The | |
3fde5a36 | 28 | formats supported by BFD are: |
252b5132 RH |
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 | ||
1b74d094 BW |
42 | SUBSECTION |
43 | File format functions | |
252b5132 RH |
44 | */ |
45 | ||
252b5132 | 46 | #include "sysdep.h" |
3db64b00 | 47 | #include "bfd.h" |
252b5132 RH |
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 | |
ed781d5d | 58 | bfd_boolean bfd_check_format (bfd *abfd, bfd_format format); |
252b5132 RH |
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 | ||
b34976b6 | 73 | The function returns <<TRUE>> on success, otherwise <<FALSE>> |
3fde5a36 | 74 | with one of the following error codes: |
252b5132 RH |
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 | ||
b34976b6 | 91 | bfd_boolean |
c58b9523 | 92 | bfd_check_format (bfd *abfd, bfd_format format) |
252b5132 RH |
93 | { |
94 | return bfd_check_format_matches (abfd, format, NULL); | |
95 | } | |
96 | ||
c9ba0c87 AM |
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; | |
7cf7fcc8 | 106 | unsigned int section_id; |
c9ba0c87 | 107 | struct bfd_hash_table section_htab; |
5d9bbb73 | 108 | const struct bfd_build_id *build_id; |
c9ba0c87 AM |
109 | }; |
110 | ||
111 | /* When testing an object for compatibility with a particular target | |
112 | back-end, the back-end object_p function needs to set up certain | |
113 | fields in the bfd on successfully recognizing the object. This | |
114 | typically happens in a piecemeal fashion, with failures possible at | |
115 | many points. On failure, the bfd is supposed to be restored to its | |
116 | initial state, which is virtually impossible. However, restoring a | |
117 | subset of the bfd state works in practice. This function stores | |
118 | the subset. */ | |
119 | ||
120 | static bfd_boolean | |
121 | bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve) | |
122 | { | |
123 | preserve->tdata = abfd->tdata.any; | |
124 | preserve->arch_info = abfd->arch_info; | |
125 | preserve->flags = abfd->flags; | |
126 | preserve->sections = abfd->sections; | |
127 | preserve->section_last = abfd->section_last; | |
128 | preserve->section_count = abfd->section_count; | |
7cf7fcc8 | 129 | preserve->section_id = _bfd_section_id; |
c9ba0c87 AM |
130 | preserve->section_htab = abfd->section_htab; |
131 | preserve->marker = bfd_alloc (abfd, 1); | |
5d9bbb73 | 132 | preserve->build_id = abfd->build_id; |
c9ba0c87 AM |
133 | if (preserve->marker == NULL) |
134 | return FALSE; | |
135 | ||
136 | return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc, | |
137 | sizeof (struct section_hash_entry)); | |
138 | } | |
139 | ||
140 | /* Clear out a subset of BFD state. */ | |
141 | ||
142 | static void | |
cb001c0d | 143 | bfd_reinit (bfd *abfd, unsigned int section_id, bfd_cleanup cleanup) |
c9ba0c87 | 144 | { |
cb001c0d AM |
145 | _bfd_section_id = section_id; |
146 | if (cleanup) | |
147 | cleanup (abfd); | |
c9ba0c87 AM |
148 | abfd->tdata.any = NULL; |
149 | abfd->arch_info = &bfd_default_arch_struct; | |
150 | abfd->flags &= BFD_FLAGS_SAVED; | |
151 | bfd_section_list_clear (abfd); | |
152 | } | |
153 | ||
154 | /* Restores bfd state saved by bfd_preserve_save. */ | |
155 | ||
156 | static void | |
157 | bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve) | |
158 | { | |
159 | bfd_hash_table_free (&abfd->section_htab); | |
160 | ||
161 | abfd->tdata.any = preserve->tdata; | |
162 | abfd->arch_info = preserve->arch_info; | |
163 | abfd->flags = preserve->flags; | |
164 | abfd->section_htab = preserve->section_htab; | |
165 | abfd->sections = preserve->sections; | |
166 | abfd->section_last = preserve->section_last; | |
167 | abfd->section_count = preserve->section_count; | |
7cf7fcc8 | 168 | _bfd_section_id = preserve->section_id; |
5d9bbb73 | 169 | abfd->build_id = preserve->build_id; |
c9ba0c87 AM |
170 | |
171 | /* bfd_release frees all memory more recently bfd_alloc'd than | |
172 | its arg, as well as its arg. */ | |
173 | bfd_release (abfd, preserve->marker); | |
174 | preserve->marker = NULL; | |
175 | } | |
176 | ||
177 | /* Called when the bfd state saved by bfd_preserve_save is no longer | |
178 | needed. */ | |
179 | ||
180 | static void | |
181 | bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve) | |
182 | { | |
183 | /* It would be nice to be able to free more memory here, eg. old | |
184 | tdata, but that's not possible since these blocks are sitting | |
185 | inside bfd_alloc'd memory. The section hash is on a separate | |
186 | objalloc. */ | |
187 | bfd_hash_table_free (&preserve->section_htab); | |
188 | preserve->marker = NULL; | |
189 | } | |
190 | ||
252b5132 RH |
191 | /* |
192 | FUNCTION | |
193 | bfd_check_format_matches | |
194 | ||
195 | SYNOPSIS | |
c58b9523 AM |
196 | bfd_boolean bfd_check_format_matches |
197 | (bfd *abfd, bfd_format format, char ***matching); | |
252b5132 RH |
198 | |
199 | DESCRIPTION | |
b34976b6 | 200 | Like <<bfd_check_format>>, except when it returns FALSE with |
252b5132 RH |
201 | <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that |
202 | case, if @var{matching} is not NULL, it will be filled in with | |
203 | a NULL-terminated list of the names of the formats that matched, | |
204 | allocated with <<malloc>>. | |
205 | Then the user may choose a format and try again. | |
206 | ||
207 | When done with the list that @var{matching} points to, the caller | |
3fde5a36 | 208 | should free it. |
252b5132 RH |
209 | */ |
210 | ||
b34976b6 | 211 | bfd_boolean |
c58b9523 | 212 | bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching) |
252b5132 RH |
213 | { |
214 | extern const bfd_target binary_vec; | |
64bfc258 JM |
215 | #if BFD_SUPPORTS_PLUGINS |
216 | extern const bfd_target plugin_vec; | |
217 | #endif | |
08f74004 AM |
218 | const bfd_target * const *target; |
219 | const bfd_target **matching_vector = NULL; | |
0aabe54e AM |
220 | const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ; |
221 | int match_count, best_count, best_match; | |
3619ad04 | 222 | int ar_match_index; |
7cf7fcc8 | 223 | unsigned int initial_section_id = _bfd_section_id; |
ea933f17 | 224 | struct bfd_preserve preserve, preserve_match; |
cb001c0d | 225 | bfd_cleanup cleanup = NULL; |
252b5132 | 226 | |
5a1dcb7e AM |
227 | if (matching != NULL) |
228 | *matching = NULL; | |
229 | ||
3619ad04 | 230 | if (!bfd_read_p (abfd) |
cea4409c | 231 | || (unsigned int) abfd->format >= (unsigned int) bfd_type_end) |
1d713d9e NC |
232 | { |
233 | bfd_set_error (bfd_error_invalid_operation); | |
b34976b6 | 234 | return FALSE; |
1d713d9e | 235 | } |
252b5132 RH |
236 | |
237 | if (abfd->format != bfd_unknown) | |
b34976b6 | 238 | return abfd->format == format; |
252b5132 | 239 | |
5a1dcb7e | 240 | if (matching != NULL || *bfd_associated_vector != NULL) |
252b5132 | 241 | { |
986f0783 | 242 | size_t amt; |
dc810e39 | 243 | |
08f74004 | 244 | amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries; |
a50b1753 | 245 | matching_vector = (const bfd_target **) bfd_malloc (amt); |
252b5132 | 246 | if (!matching_vector) |
b34976b6 | 247 | return FALSE; |
252b5132 | 248 | } |
3fde5a36 | 249 | |
1d713d9e | 250 | /* Presume the answer is yes. */ |
252b5132 | 251 | abfd->format = format; |
c9ba0c87 | 252 | save_targ = abfd->xvec; |
ea933f17 AM |
253 | |
254 | preserve_match.marker = NULL; | |
255 | if (!bfd_preserve_save (abfd, &preserve)) | |
256 | goto err_ret; | |
252b5132 RH |
257 | |
258 | /* If the target type was explicitly specified, just check that target. */ | |
1d713d9e NC |
259 | if (!abfd->target_defaulted) |
260 | { | |
dc810e39 | 261 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) /* rewind! */ |
5a1dcb7e | 262 | goto err_ret; |
3fde5a36 | 263 | |
cb001c0d | 264 | cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); |
3fde5a36 | 265 | |
cb001c0d | 266 | if (cleanup) |
5a1dcb7e | 267 | goto ok_ret; |
1d713d9e NC |
268 | |
269 | /* For a long time the code has dropped through to check all | |
270 | targets if the specified target was wrong. I don't know why, | |
271 | and I'm reluctant to change it. However, in the case of an | |
272 | archive, it can cause problems. If the specified target does | |
273 | not permit archives (e.g., the binary target), then we should | |
274 | not allow some other target to recognize it as an archive, but | |
275 | should instead allow the specified target to recognize it as an | |
276 | object. When I first made this change, it broke the PE target, | |
277 | because the specified pei-i386 target did not recognize the | |
278 | actual pe-i386 archive. Since there may be other problems of | |
279 | this sort, I changed this test to check only for the binary | |
280 | target. */ | |
281 | if (format == bfd_archive && save_targ == &binary_vec) | |
5a1dcb7e | 282 | goto err_unrecog; |
1d713d9e NC |
283 | } |
284 | ||
c9ba0c87 AM |
285 | /* Since the target type was defaulted, check them all in the hope |
286 | that one will be uniquely recognized. */ | |
287 | right_targ = NULL; | |
288 | ar_right_targ = NULL; | |
289 | match_targ = NULL; | |
290 | best_match = 256; | |
291 | best_count = 0; | |
292 | match_count = 0; | |
293 | ar_match_index = _bfd_target_vector_entries; | |
294 | ||
1d713d9e NC |
295 | for (target = bfd_target_vector; *target != NULL; target++) |
296 | { | |
ea933f17 | 297 | void **high_water; |
3fde5a36 | 298 | |
999d6dff AM |
299 | /* The binary target matches anything, so don't return it when |
300 | searching. Don't match the plugin target if we have another | |
301 | alternative since we want to properly set the input format | |
302 | before allowing a plugin to claim the file. Also, don't | |
303 | check the default target twice. */ | |
25bbc984 | 304 | if (*target == &binary_vec |
999d6dff AM |
305 | #if BFD_SUPPORTS_PLUGINS |
306 | || (match_count != 0 && *target == &plugin_vec) | |
307 | #endif | |
7cf7fcc8 | 308 | || (!abfd->target_defaulted && *target == save_targ)) |
1d713d9e | 309 | continue; |
3fde5a36 | 310 | |
c9ba0c87 AM |
311 | /* If we already tried a match, the bfd is modified and may |
312 | have sections attached, which will confuse the next | |
313 | _bfd_check_format call. */ | |
cb001c0d | 314 | bfd_reinit (abfd, initial_section_id, cleanup); |
ea933f17 AM |
315 | /* Free bfd_alloc memory too. If we have matched and preserved |
316 | a target then the high water mark is that much higher. */ | |
317 | if (preserve_match.marker) | |
318 | high_water = &preserve_match.marker; | |
319 | else | |
320 | high_water = &preserve.marker; | |
321 | bfd_release (abfd, *high_water); | |
322 | *high_water = bfd_alloc (abfd, 1); | |
c9ba0c87 AM |
323 | |
324 | /* Change BFD's target temporarily. */ | |
325 | abfd->xvec = *target; | |
3fde5a36 | 326 | |
dc810e39 | 327 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) |
5a1dcb7e | 328 | goto err_ret; |
3fde5a36 | 329 | |
cb001c0d AM |
330 | cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); |
331 | if (cleanup) | |
c9ba0c87 | 332 | { |
cb001c0d | 333 | int match_priority = abfd->xvec->match_priority; |
64bfc258 JM |
334 | #if BFD_SUPPORTS_PLUGINS |
335 | /* If this object can be handled by a plugin, give that the | |
336 | lowest priority; objects both handled by a plugin and | |
337 | with an underlying object format will be claimed | |
338 | separately by the plugin. */ | |
339 | if (*target == &plugin_vec) | |
340 | match_priority = (*target)->match_priority; | |
341 | #endif | |
342 | ||
89d7b8aa AM |
343 | if (abfd->format != bfd_archive |
344 | || (bfd_has_map (abfd) | |
345 | && bfd_get_error () != bfd_error_wrong_object_format)) | |
0aabe54e | 346 | { |
89d7b8aa AM |
347 | /* If this is the default target, accept it, even if |
348 | other targets might match. People who want those | |
349 | other targets have to set the GNUTARGET variable. */ | |
cb001c0d | 350 | if (abfd->xvec == bfd_default_vector[0]) |
89d7b8aa AM |
351 | goto ok_ret; |
352 | ||
353 | if (matching_vector) | |
cb001c0d | 354 | matching_vector[match_count] = abfd->xvec; |
89d7b8aa AM |
355 | match_count++; |
356 | ||
64bfc258 | 357 | if (match_priority < best_match) |
89d7b8aa | 358 | { |
64bfc258 | 359 | best_match = match_priority; |
89d7b8aa AM |
360 | best_count = 0; |
361 | } | |
7cf7fcc8 AM |
362 | if (match_priority <= best_match) |
363 | { | |
364 | /* This format checks out as ok! */ | |
cb001c0d | 365 | right_targ = abfd->xvec; |
7cf7fcc8 AM |
366 | best_count++; |
367 | } | |
89d7b8aa AM |
368 | } |
369 | else | |
370 | { | |
371 | /* An archive with no armap or objects of the wrong | |
372 | type. We want this target to match if we get no | |
373 | better matches. */ | |
374 | if (ar_right_targ != bfd_default_vector[0]) | |
375 | ar_right_targ = *target; | |
376 | if (matching_vector) | |
377 | matching_vector[ar_match_index] = *target; | |
378 | ar_match_index++; | |
0aabe54e | 379 | } |
c9ba0c87 | 380 | |
ea933f17 AM |
381 | if (preserve_match.marker == NULL) |
382 | { | |
cb001c0d | 383 | match_targ = abfd->xvec; |
ea933f17 AM |
384 | if (!bfd_preserve_save (abfd, &preserve_match)) |
385 | goto err_ret; | |
386 | } | |
89d7b8aa | 387 | } |
1d713d9e | 388 | } |
3fde5a36 | 389 | |
0aabe54e AM |
390 | if (best_count == 1) |
391 | match_count = 1; | |
392 | ||
3619ad04 AM |
393 | if (match_count == 0) |
394 | { | |
395 | /* Try partial matches. */ | |
396 | right_targ = ar_right_targ; | |
ed781d5d | 397 | |
3619ad04 AM |
398 | if (right_targ == bfd_default_vector[0]) |
399 | { | |
400 | match_count = 1; | |
401 | } | |
402 | else | |
403 | { | |
404 | match_count = ar_match_index - _bfd_target_vector_entries; | |
ed781d5d | 405 | |
5a1dcb7e | 406 | if (matching_vector && match_count > 1) |
ed781d5d NC |
407 | memcpy (matching_vector, |
408 | matching_vector + _bfd_target_vector_entries, | |
409 | sizeof (*matching_vector) * match_count); | |
08f74004 AM |
410 | } |
411 | } | |
412 | ||
03ae2d5e AM |
413 | /* We have more than one equally good match. If any of the best |
414 | matches is a target in config.bfd targ_defvec or targ_selvecs, | |
415 | choose it. */ | |
5a1dcb7e | 416 | if (match_count > 1) |
08f74004 AM |
417 | { |
418 | const bfd_target * const *assoc = bfd_associated_vector; | |
419 | ||
420 | while ((right_targ = *assoc++) != NULL) | |
421 | { | |
422 | int i = match_count; | |
423 | ||
424 | while (--i >= 0) | |
03ae2d5e AM |
425 | if (matching_vector[i] == right_targ |
426 | && right_targ->match_priority <= best_match) | |
08f74004 AM |
427 | break; |
428 | ||
429 | if (i >= 0) | |
430 | { | |
431 | match_count = 1; | |
432 | break; | |
3619ad04 AM |
433 | } |
434 | } | |
435 | } | |
436 | ||
03ae2d5e AM |
437 | /* We still have more than one equally good match, and at least some |
438 | of the targets support match priority. Choose the first of the | |
439 | best matches. */ | |
033539e2 | 440 | if (matching_vector && match_count > 1 && best_count != match_count) |
03ae2d5e AM |
441 | { |
442 | int i; | |
443 | ||
444 | for (i = 0; i < match_count; i++) | |
445 | { | |
446 | right_targ = matching_vector[i]; | |
447 | if (right_targ->match_priority <= best_match) | |
448 | break; | |
449 | } | |
450 | match_count = 1; | |
451 | } | |
452 | ||
c9ba0c87 AM |
453 | /* There is way too much undoing of half-known state here. We |
454 | really shouldn't iterate on live bfd's. Note that saving the | |
455 | whole bfd and restoring it would be even worse; the first thing | |
456 | you notice is that the cached bfd file position gets out of sync. */ | |
ea933f17 AM |
457 | if (preserve_match.marker != NULL) |
458 | bfd_preserve_restore (abfd, &preserve_match); | |
c9ba0c87 | 459 | |
1d713d9e NC |
460 | if (match_count == 1) |
461 | { | |
0aabe54e AM |
462 | abfd->xvec = right_targ; |
463 | /* If we come out of the loop knowing that the last target that | |
464 | matched is the one we want, then ABFD should still be in a usable | |
ea933f17 AM |
465 | state (except possibly for XVEC). This is not just an |
466 | optimisation. In the case of plugins a match against the | |
467 | plugin target can result in the bfd being changed such that | |
468 | it no longer matches the plugin target, nor will it match | |
469 | RIGHT_TARG again. */ | |
0aabe54e AM |
470 | if (match_targ != right_targ) |
471 | { | |
cb001c0d | 472 | bfd_reinit (abfd, initial_section_id, cleanup); |
ea933f17 | 473 | bfd_release (abfd, preserve.marker); |
0aabe54e AM |
474 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) |
475 | goto err_ret; | |
cb001c0d AM |
476 | cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); |
477 | BFD_ASSERT (cleanup != NULL); | |
0aabe54e | 478 | } |
3fde5a36 | 479 | |
0aabe54e | 480 | ok_ret: |
26ae6d5e DJ |
481 | /* If the file was opened for update, then `output_has_begun' |
482 | some time ago when the file was created. Do not recompute | |
483 | sections sizes or alignments in _bfd_set_section_contents. | |
484 | We can not set this flag until after checking the format, | |
485 | because it will interfere with creation of BFD sections. */ | |
486 | if (abfd->direction == both_direction) | |
487 | abfd->output_has_begun = TRUE; | |
488 | ||
5a1dcb7e AM |
489 | if (matching_vector) |
490 | free (matching_vector); | |
ea933f17 AM |
491 | if (preserve_match.marker != NULL) |
492 | bfd_preserve_finish (abfd, &preserve_match); | |
493 | bfd_preserve_finish (abfd, &preserve); | |
c9ba0c87 AM |
494 | |
495 | /* File position has moved, BTW. */ | |
496 | return TRUE; | |
252b5132 | 497 | } |
252b5132 | 498 | |
252b5132 RH |
499 | if (match_count == 0) |
500 | { | |
5a1dcb7e | 501 | err_unrecog: |
252b5132 | 502 | bfd_set_error (bfd_error_file_not_recognized); |
5a1dcb7e AM |
503 | err_ret: |
504 | abfd->xvec = save_targ; | |
505 | abfd->format = bfd_unknown; | |
506 | if (matching_vector) | |
c58b9523 | 507 | free (matching_vector); |
ea933f17 AM |
508 | if (preserve_match.marker != NULL) |
509 | bfd_preserve_finish (abfd, &preserve_match); | |
510 | bfd_preserve_restore (abfd, &preserve); | |
5a1dcb7e | 511 | return FALSE; |
252b5132 | 512 | } |
3619ad04 | 513 | |
c9ba0c87 AM |
514 | /* Restore original target type and format. */ |
515 | abfd->xvec = save_targ; | |
516 | abfd->format = bfd_unknown; | |
5a1dcb7e AM |
517 | bfd_set_error (bfd_error_file_ambiguously_recognized); |
518 | ||
519 | if (matching) | |
520 | { | |
521 | *matching = (char **) matching_vector; | |
522 | matching_vector[match_count] = NULL; | |
523 | /* Return target names. This is a little nasty. Maybe we | |
524 | should do another bfd_malloc? */ | |
525 | while (--match_count >= 0) | |
3619ad04 | 526 | { |
5a1dcb7e AM |
527 | const char *name = matching_vector[match_count]->name; |
528 | *(const char **) &matching_vector[match_count] = name; | |
3619ad04 AM |
529 | } |
530 | } | |
9d78076e AM |
531 | else if (matching_vector) |
532 | free (matching_vector); | |
ea933f17 AM |
533 | if (preserve_match.marker != NULL) |
534 | bfd_preserve_finish (abfd, &preserve_match); | |
535 | bfd_preserve_restore (abfd, &preserve); | |
b34976b6 | 536 | return FALSE; |
252b5132 RH |
537 | } |
538 | ||
539 | /* | |
540 | FUNCTION | |
541 | bfd_set_format | |
542 | ||
543 | SYNOPSIS | |
ed781d5d | 544 | bfd_boolean bfd_set_format (bfd *abfd, bfd_format format); |
252b5132 RH |
545 | |
546 | DESCRIPTION | |
547 | This function sets the file format of the BFD @var{abfd} to the | |
548 | format @var{format}. If the target set in the BFD does not | |
549 | support the format requested, the format is invalid, or the BFD | |
550 | is not open for writing, then an error occurs. | |
252b5132 RH |
551 | */ |
552 | ||
b34976b6 | 553 | bfd_boolean |
c58b9523 | 554 | bfd_set_format (bfd *abfd, bfd_format format) |
252b5132 | 555 | { |
3619ad04 | 556 | if (bfd_read_p (abfd) |
cea4409c | 557 | || (unsigned int) abfd->format >= (unsigned int) bfd_type_end) |
1d713d9e NC |
558 | { |
559 | bfd_set_error (bfd_error_invalid_operation); | |
b34976b6 | 560 | return FALSE; |
1d713d9e | 561 | } |
252b5132 RH |
562 | |
563 | if (abfd->format != bfd_unknown) | |
b34976b6 | 564 | return abfd->format == format; |
252b5132 | 565 | |
1d713d9e | 566 | /* Presume the answer is yes. */ |
252b5132 RH |
567 | abfd->format = format; |
568 | ||
1d713d9e NC |
569 | if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) |
570 | { | |
571 | abfd->format = bfd_unknown; | |
b34976b6 | 572 | return FALSE; |
1d713d9e | 573 | } |
252b5132 | 574 | |
b34976b6 | 575 | return TRUE; |
252b5132 RH |
576 | } |
577 | ||
252b5132 RH |
578 | /* |
579 | FUNCTION | |
580 | bfd_format_string | |
581 | ||
582 | SYNOPSIS | |
ed781d5d | 583 | const char *bfd_format_string (bfd_format format); |
252b5132 RH |
584 | |
585 | DESCRIPTION | |
586 | Return a pointer to a const string | |
587 | <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>, | |
588 | depending upon the value of @var{format}. | |
589 | */ | |
590 | ||
3619ad04 | 591 | const char * |
c58b9523 | 592 | bfd_format_string (bfd_format format) |
252b5132 | 593 | { |
c58b9523 AM |
594 | if (((int) format < (int) bfd_unknown) |
595 | || ((int) format >= (int) bfd_type_end)) | |
252b5132 | 596 | return "invalid"; |
3fde5a36 | 597 | |
1d713d9e NC |
598 | switch (format) |
599 | { | |
600 | case bfd_object: | |
7dee875e | 601 | return "object"; /* Linker/assembler/compiler output. */ |
3fde5a36 | 602 | case bfd_archive: |
1d713d9e | 603 | return "archive"; /* Object archive file. */ |
3fde5a36 | 604 | case bfd_core: |
1d713d9e | 605 | return "core"; /* Core dump. */ |
3fde5a36 | 606 | default: |
1d713d9e NC |
607 | return "unknown"; |
608 | } | |
252b5132 | 609 | } |