1 /* Generic target-file-type support for the BFD library.
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. */
29 Each port of BFD to a different machine requries the creation of a
30 target back end. All the back end provides to the root part of BFD is
31 a structure containing pointers to functions which perform certain low
32 level operations on files. BFD translates the applications's requests
33 through a pointer into calls to the back end routines.
35 When a file is opened with @code{bfd_openr}, its format and target are
36 unknown. BFD uses various mechanisms to determine how to interpret the
37 file. The operations performed are:
40 First a BFD is created by calling the internal routine
41 @code{new_bfd}, then @code{bfd_find_target} is called with the target
42 string supplied to @code{bfd_openr} and the new BFD pointer.
44 If a null target string was provided to
45 @code{bfd_find_target}, it looks up the environment variable
46 @code{GNUTARGET} and uses that as the target string.
48 If the target string is still NULL, or the target string
49 is @code{default}, then the first item in the target vector is used as
50 the target type. @xref{bfd_target}.
52 Otherwise, the elements in the target vector are
53 inspected one by one, until a match on target name is found. When
56 Otherwise the error @code{invalid_target} is returned to
59 @code{bfd_openr} attempts to open the file using
60 @code{bfd_open_file}, and returns the BFD.
62 Once the BFD has been opened and the target selected, the file format
63 may be determined. This is done by calling @code{bfd_check_format} on
64 the BFD with a suggested format. The routine returns @code{true} when
65 the application guesses right.
74 @node bfd_target, , Targets, Targets
75 @subsection bfd_target
76 This structure contains everything that BFD knows about a target.
77 It includes things like its byte order, name, what routines to call
78 to do various operations, etc.
80 Every BFD points to a target structure with its "xvec" member.
83 Shortcut for declaring fields which are prototyped function pointers,
84 while avoiding anguish on compilers that don't support protos.
86 $#define SDEF(ret, name, arglist) \
87 $ PROTO(ret,(*name),arglist)
88 $#define SDEF_FMT(ret, name, arglist) \
89 $ PROTO(ret,(*name[bfd_type_end]),arglist)
91 These macros are used to dispatch to functions through the bfd_target
92 vector. They are used in a number of macros further down in @file{bfd.h}, and
93 are also used when calling various routines by hand inside the BFD
94 implementation. The "arglist" argument must be parenthesized; it
95 contains all the arguments to the called function.
97 $#define BFD_SEND(bfd, message, arglist) \
98 $ ((*((bfd)->xvec->message)) arglist)
100 For operations which index on the BFD format
102 $#define BFD_SEND_FMT(bfd, message, arglist) \
103 $ (((bfd)->xvec->message[(int)((bfd)->format)]) arglist)
105 This is the struct which defines the type of BFD this is. The
106 "xvec" member of the struct @code{bfd} itself points here. Each module
107 that implements access to a different target under BFD, defines
110 FIXME, these names should be rationalised with the names of the
111 entry points which call them. Too bad we can't have one macro to
116 $typedef struct bfd_target
119 identifies the kind of target, eg SunOS4, Ultrix, etc
123 The "flavour" of a back end is a general indication about the contents
126 $ enum target_flavour {
127 $ bfd_target_unknown_flavour,
128 $ bfd_target_aout_flavour,
129 $ bfd_target_coff_flavour,
130 $ bfd_target_elf_flavour,
131 $ bfd_target_ieee_flavour,
132 $ bfd_target_oasys_flavour,
133 $ bfd_target_srec_flavour} flavour;
135 The order of bytes within the data area of a file.
137 $ boolean byteorder_big_p;
139 The order of bytes within the header parts of a file.
141 $ boolean header_byteorder_big_p;
143 This is a mask of all the flags which an executable may have set -
144 from the set @code{NO_FLAGS}, @code{HAS_RELOC}, ...@code{D_PAGED}.
146 $ flagword object_flags;
148 This is a mask of all the flags which a section may have set - from
149 the set @code{SEC_NO_FLAGS}, @code{SEC_ALLOC}, ...@code{SET_NEVER_LOAD}.
151 $ flagword section_flags;
153 The pad character for filenames within an archive header.
157 The maximum number of characters in an archive header.
159 $ unsigned short ar_max_namelen;
161 The minimum alignment restriction for any section.
163 $ unsigned int align_power_min;
165 Entries for byte swapping for data. These are different to the other
166 entry points, since they don't take BFD as first arg. Certain other handlers
169 $ SDEF (bfd_vma, bfd_getx64, (bfd_byte *));
170 $ SDEF (void, bfd_putx64, (bfd_vma, bfd_byte *));
171 $ SDEF (bfd_vma, bfd_getx32, (bfd_byte *));
172 $ SDEF (void, bfd_putx32, (bfd_vma, bfd_byte *));
173 $ SDEF (bfd_vma, bfd_getx16, (bfd_byte *));
174 $ SDEF (void, bfd_putx16, (bfd_vma, bfd_byte *));
176 Byte swapping for the headers
178 $ SDEF (bfd_vma, bfd_h_getx64, (bfd_byte *));
179 $ SDEF (void, bfd_h_putx64, (bfd_vma, bfd_byte *));
180 $ SDEF (bfd_vma, bfd_h_getx32, (bfd_byte *));
181 $ SDEF (void, bfd_h_putx32, (bfd_vma, bfd_byte *));
182 $ SDEF (bfd_vma, bfd_h_getx16, (bfd_byte *));
183 $ SDEF (void, bfd_h_putx16, (bfd_vma, bfd_byte *));
185 Format dependent routines, these turn into vectors of entry points
186 within the target vector structure; one for each format to check.
188 Check the format of a file being read. Return bfd_target * or zero.
190 $ SDEF_FMT (struct bfd_target *, _bfd_check_format, (bfd *));
192 Set the format of a file being written.
194 $ SDEF_FMT (boolean, _bfd_set_format, (bfd *));
196 Write cached information into a file being written, at bfd_close.
198 $ SDEF_FMT (boolean, _bfd_write_contents, (bfd *));
200 The following functions are defined in @code{JUMP_TABLE}. The idea is
201 that the back end writer of @code{foo} names all the routines
202 @code{foo_}@var{entry_point}, @code{JUMP_TABLE} will built the entries
203 in this structure in the right order.
205 Core file entry points
207 $ SDEF (char *, _core_file_failing_command, (bfd *));
208 $ SDEF (int, _core_file_failing_signal, (bfd *));
209 $ SDEF (boolean, _core_file_matches_executable_p, (bfd *, bfd *));
213 $ SDEF (boolean, _bfd_slurp_armap, (bfd *));
214 $ SDEF (boolean, _bfd_slurp_extended_name_table, (bfd *));
215 $ SDEF (void, _bfd_truncate_arname, (bfd *, CONST char *, char *));
216 $ SDEF (boolean, write_armap, (bfd *arch,
217 $ unsigned int elength,
219 $ unsigned int orl_count,
224 $ SDEF (boolean, _close_and_cleanup, (bfd *));
225 $ SDEF (boolean, _bfd_set_section_contents, (bfd *, sec_ptr, PTR,
226 $ file_ptr, bfd_size_type));
227 $ SDEF (boolean, _bfd_get_section_contents, (bfd *, sec_ptr, PTR,
228 $ file_ptr, bfd_size_type));
229 $ SDEF (boolean, _new_section_hook, (bfd *, sec_ptr));
231 Symbols and reloctions
233 $ SDEF (unsigned int, _get_symtab_upper_bound, (bfd *));
234 $ SDEF (unsigned int, _bfd_canonicalize_symtab,
235 $ (bfd *, struct symbol_cache_entry **));
236 $ SDEF (unsigned int, _get_reloc_upper_bound, (bfd *, sec_ptr));
237 $ SDEF (unsigned int, _bfd_canonicalize_reloc, (bfd *, sec_ptr, arelent **,
238 $ struct symbol_cache_entry**));
239 $ SDEF (struct symbol_cache_entry *, _bfd_make_empty_symbol, (bfd *));
240 $ SDEF (void, _bfd_print_symbol, (bfd *, PTR, struct symbol_cache_entry *,
241 $ bfd_print_symbol_type));
242 $#define bfd_print_symbol(b,p,s,e) BFD_SEND(b, _bfd_print_symbol, (b,p,s,e))
243 $ SDEF (alent *, _get_lineno, (bfd *, struct symbol_cache_entry *));
245 $ SDEF (boolean, _bfd_set_arch_mach, (bfd *, enum bfd_architecture,
248 $ SDEF (bfd *, openr_next_archived_file, (bfd *arch, bfd *prev));
249 $ SDEF (boolean, _bfd_find_nearest_line,
250 $ (bfd *abfd, struct sec *section,
251 $ struct symbol_cache_entry **symbols,bfd_vma offset,
252 $ CONST char **file, CONST char **func, unsigned int *line));
253 $ SDEF (int, _bfd_stat_arch_elt, (bfd *, struct stat *));
255 $ SDEF (int, _bfd_sizeof_headers, (bfd *, boolean));
257 $ SDEF (void, _bfd_debug_info_start, (bfd *));
258 $ SDEF (void, _bfd_debug_info_end, (bfd *));
259 $ SDEF (void, _bfd_debug_info_accumulate, (bfd *, struct sec *));
261 Special entry points for gdb to swap in coff symbol table parts
263 $ SDEF(void, _bfd_coff_swap_aux_in,(
270 $ SDEF(void, _bfd_coff_swap_sym_in,(
275 $ SDEF(void, _bfd_coff_swap_lineno_in, (
281 Special entry points for gas to swap coff parts
283 $ SDEF(unsigned int, _bfd_coff_swap_aux_out,(
290 $ SDEF(unsigned int, _bfd_coff_swap_sym_out,(
295 $ SDEF(unsigned int, _bfd_coff_swap_lineno_out,(
300 $ SDEF(unsigned int, _bfd_coff_swap_reloc_out,(
305 $ SDEF(unsigned int, _bfd_coff_swap_filehdr_out,(
310 $ SDEF(unsigned int, _bfd_coff_swap_aouthdr_out,(
315 $ SDEF(unsigned int, _bfd_coff_swap_scnhdr_out,(
325 extern bfd_target ecoff_little_vec;
326 extern bfd_target ecoff_big_vec;
327 extern bfd_target sunos_big_vec;
328 extern bfd_target demo_64_vec;
329 extern bfd_target srec_vec;
330 extern bfd_target b_out_vec_little_host;
331 extern bfd_target b_out_vec_big_host;
332 extern bfd_target icoff_little_vec;
333 extern bfd_target icoff_big_vec;
334 extern bfd_target elf_little_vec;
335 extern bfd_target elf_big_vec;
336 extern bfd_target ieee_vec;
337 extern bfd_target oasys_vec;
338 extern bfd_target m88k_bcs_vec;
339 extern bfd_target m68kcoff_vec;
340 extern bfd_target i386coff_vec;
341 extern bfd_target i386aout_vec;
342 extern bfd_target a29kcoff_big_vec;
343 extern bfd_target trad_core_vec;
347 bfd_target *target_vector[] = {
353 #ifdef DEFAULT_VECTOR
354 extern bfd_target DEFAULT_VECTOR;
358 #define ICOFF_LITTLE_VEC icoff_little_vec
359 #define ICOFF_BIG_VEC icoff_big_vec
360 #define B_OUT_VEC_LITTLE_HOST b_out_vec_little_host
361 #define B_OUT_VEC_BIG_HOST b_out_vec_big_host
365 #define ECOFF_LITTLE_VEC ecoff_little_vec
366 #define ECOFF_BIG_VEC ecoff_big_vec
367 #define ICOFF_LITTLE_VEC icoff_little_vec
368 #define ICOFF_BIG_VEC icoff_big_vec
369 #define ELF_LITTLE_VEC elf_little_vec
370 #define ELF_BIG_VEC elf_big_vec
371 #define ZB_OUT_VEC_LITTLE_HOST b_out_vec_little_host
372 #define ZB_OUT_VEC_BIG_HOST b_out_vec_big_host
373 #define SUNOS_VEC_BIG_HOST sunos_big_vec
374 #define DEMO_64_VEC demo_64_vec
376 /* We have no oasys tools anymore, so we can't test any of this
377 anymore. If you want to test the stuff yourself, go ahead...
380 #define OASYS_VEC oasys_vec
383 #define IEEE_VEC ieee_vec
384 #define M88K_BCS_VEC m88k_bcs_vec
385 #define SREC_VEC srec_vec
386 #define M68KCOFF_VEC m68kcoff_vec
387 #define I386COFF_VEC i386coff_vec
388 #define I386AOUT_VEC i386aout_vec
389 #define A29KCOFF_BIG_VEC a29kcoff_big_vec
392 bfd_target *target_vector[] = {
394 #ifdef DEFAULT_VECTOR
406 #ifdef ECOFF_LITTLE_VEC
422 #ifdef SUNOS_VEC_BIG_HOST
440 #ifdef ICOFF_LITTLE_VEC
448 #ifdef ELF_LITTLE_VEC
456 #ifdef B_OUT_VEC_LITTLE_HOST
457 &B_OUT_VEC_LITTLE_HOST,
460 #ifdef B_OUT_VEC_BIG_HOST
468 #ifdef A29KCOFF_BIG_VEC
476 NULL, /* end of list marker */
481 /* default_vector[0] contains either the address of the default vector,
482 if there is one, or zero if there isn't. */
484 bfd_target *default_vector[] = {
485 #ifdef DEFAULT_VECTOR
496 Returns a pointer to the transfer vector for the object target
497 named target_name. If target_name is NULL, chooses the one in the
498 environment variable GNUTARGET; if that is null or not defined then
499 the first entry in the target list is chosen. Passing in the
500 string "default" or setting the environment variable to "default"
501 will cause the first entry in the target list to be returned,
502 and "target_defaulted" will be set in the BFD. This causes
503 @code{bfd_check_format} to loop over all the targets to find the one
504 that matches the file being read.
505 *; PROTO(bfd_target *, bfd_find_target,(CONST char *, bfd *));
509 DEFUN(bfd_find_target,(target_name, abfd),
510 CONST char *target_name AND
514 extern char *getenv ();
515 CONST char *targname = (target_name ? target_name : getenv ("GNUTARGET"));
517 /* This is safe; the vector cannot be null */
518 if (targname == NULL || !strcmp (targname, "default")) {
519 abfd->target_defaulted = true;
520 return abfd->xvec = target_vector[0];
523 abfd->target_defaulted = false;
525 for (target = &target_vector[0]; *target != NULL; target++) {
526 if (!strcmp (targname, (*target)->name))
527 return abfd->xvec = *target;
530 bfd_error = invalid_target;
537 This function returns a freshly malloced NULL-terminated vector of the
538 names of all the valid BFD targets. Do not modify the names
539 *; PROTO(CONST char **,bfd_target_list,());
544 DEFUN_VOID(bfd_target_list)
548 CONST char **name_list, **name_ptr;
550 for (target = &target_vector[0]; *target != NULL; target++)
554 name_list = (CONST char **) zalloc ((vec_length + 1) * sizeof (char **));
556 if (name_list == NULL) {
557 bfd_error = no_memory;
561 for (target = &target_vector[0]; *target != NULL; target++)
562 *(name_ptr++) = (*target)->name;