1 /* Generic BFD support for file formats.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
23 A format is a BFD concept of high level file contents. The
24 formats supported by BFD are:
27 The BFD may contain data, symbols, relocations and debug info.
29 The BFD contains other BFDs and an optional index.
31 The BFD contains the result of an executable core dump.
39 extern bfd_target *target_vector[];
40 extern bfd_target *default_vector[];
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
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.
55 The function returns @code{true} on success, otherwise @code{false}
56 with one of the following error codes:
60 if @code{format} is not one of @code{bfd_object}, @code{bfd_archive}
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.
70 *; PROTO(boolean, bfd_check_format, (bfd *abfd, bfd_format format));
74 DEFUN(bfd_check_format,(abfd, format),
78 bfd_target **target, *save_targ, *right_targ;
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;
88 if (abfd->format != bfd_unknown)
89 return (abfd->format == format)? true: false;
91 /* presume the answer is yes */
92 abfd->format = format;
94 /* If the target type was explicitly specified, just check that target. */
96 if (!abfd->target_defaulted) {
97 bfd_seek (abfd, (file_ptr)0, SEEK_SET); /* rewind! */
99 right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
101 abfd->xvec = right_targ; /* Set the target as returned */
102 return true; /* File position has moved, BTW */
104 abfd->format = bfd_unknown;
105 return false; /* Specified target is not right */
108 /* Since the target type was defaulted, check them
109 all in the hope that one will be uniquely recognized. */
111 save_targ = abfd->xvec;
115 for (target = target_vector; *target != NULL; target++) {
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! */
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])
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.
141 if (match_count == 1) {
142 abfd->xvec = right_targ; /* Change BFD's target permanently */
143 return true; /* File position has moved, BTW */
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);
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));
161 DEFUN(bfd_set_format,(abfd, format),
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;
173 if (abfd->format != bfd_unknown)
174 return (abfd->format == format) ? true:false;
176 /* presume the answer is yes */
177 abfd->format = format;
179 if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) {
180 abfd->format = bfd_unknown;
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));
197 DEFUN(bfd_format_string,(format),
200 if (((int)format <(int) bfd_unknown)
201 || ((int)format >=(int) bfd_type_end))
206 return "object"; /* linker/assember/compiler output */
208 return "archive"; /* object archive file */
210 return "core"; /* core dump */