* Makefile.in: added coff-h8300
[deliverable/binutils-gdb.git] / bfd / targets.c
CommitLineData
4e6f9223
SC
1/* Generic target-file-type support for the BFD library.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4a81b561 4
4e6f9223 5This file is part of BFD, the Binary File Descriptor library.
4a81b561 6
4e6f9223 7This program is free software; you can redistribute it and/or modify
4a81b561 8it under the terms of the GNU General Public License as published by
4e6f9223
SC
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
4a81b561 11
4e6f9223 12This program is distributed in the hope that it will be useful,
4a81b561
DHW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
4e6f9223
SC
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
4a81b561
DHW
20
21/* $Id$ */
22
4a81b561 23#include "bfd.h"
bbc8d484 24#include "sysdep.h"
4a81b561
DHW
25#include "libbfd.h"
26
6f715d66
SC
27/*doc*
28@section Targets
29Each port of BFD to a different machine requries the creation of a
4e6f9223 30target back end. All the back end provides to the root part of BFD is
6f715d66
SC
31a structure containing pointers to functions which perform certain low
32level operations on files. BFD translates the applications's requests
33through a pointer into calls to the back end routines.
34
35When a file is opened with @code{bfd_openr}, its format and target are
36unknown. BFD uses various mechanisms to determine how to interpret the
3f85ebce 37file. The operations performed are:
6f715d66
SC
38@itemize @bullet
39@item
4e6f9223 40First a BFD is created by calling the internal routine
6f715d66 41@code{new_bfd}, then @code{bfd_find_target} is called with the target
4e6f9223 42string supplied to @code{bfd_openr} and the new BFD pointer.
6f715d66
SC
43@item
44If 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.
47@item
48If the target string is still NULL, or the target string
49is @code{default}, then the first item in the target vector is used as
4e6f9223 50the target type. @xref{bfd_target}.
6f715d66
SC
51@item
52Otherwise, the elements in the target vector are
53inspected one by one, until a match on target name is found. When
54found, that is used.
55@item
56Otherwise the error @code{invalid_target} is returned to
57@code{bfd_openr}.
58@item
59@code{bfd_openr} attempts to open the file using
4e6f9223 60@code{bfd_open_file}, and returns the BFD.
6f715d66 61@end itemize
4e6f9223 62Once the BFD has been opened and the target selected, the file format
6f715d66 63may be determined. This is done by calling @code{bfd_check_format} on
4e6f9223 64the BFD with a suggested format. The routine returns @code{true} when
6f715d66 65the application guesses right.
92c78ee6
RP
66
67@menu
68* bfd_target::
69@end menu
6f715d66
SC
70*/
71
72
73/*proto* bfd_target
92c78ee6 74@node bfd_target, , Targets, Targets
6f715d66
SC
75@subsection bfd_target
76This structure contains everything that BFD knows about a target.
77It includes things like its byte order, name, what routines to call
78to do various operations, etc.
79
80Every BFD points to a target structure with its "xvec" member.
81
82
83Shortcut for declaring fields which are prototyped function pointers,
84while avoiding anguish on compilers that don't support protos.
85
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)
90
91These macros are used to dispatch to functions through the bfd_target
4e6f9223
SC
92vector. They are used in a number of macros further down in @file{bfd.h}, and
93are also used when calling various routines by hand inside the BFD
6f715d66
SC
94implementation. The "arglist" argument must be parenthesized; it
95contains all the arguments to the called function.
96
97$#define BFD_SEND(bfd, message, arglist) \
98$ ((*((bfd)->xvec->message)) arglist)
99
4e6f9223 100For operations which index on the BFD format
6f715d66
SC
101
102$#define BFD_SEND_FMT(bfd, message, arglist) \
103$ (((bfd)->xvec->message[(int)((bfd)->format)]) arglist)
104
105This is the struct which defines the type of BFD this is. The
4e6f9223 106"xvec" member of the struct @code{bfd} itself points here. Each module
6f715d66
SC
107that implements access to a different target under BFD, defines
108one of these.
109
110FIXME, these names should be rationalised with the names of the
111entry points which call them. Too bad we can't have one macro to
112define them both!
113
114*+++
115
116$typedef struct bfd_target
117${
118
119identifies the kind of target, eg SunOS4, Ultrix, etc
120
121$ char *name;
122
123The "flavour" of a back end is a general indication about the contents
124of a file.
125
92c78ee6
RP
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;
6f715d66
SC
134
135The order of bytes within the data area of a file.
136
137$ boolean byteorder_big_p;
138
139The order of bytes within the header parts of a file.
140
141$ boolean header_byteorder_big_p;
142
143This is a mask of all the flags which an executable may have set -
144from the set @code{NO_FLAGS}, @code{HAS_RELOC}, ...@code{D_PAGED}.
145
146$ flagword object_flags;
147
148This is a mask of all the flags which a section may have set - from
149the set @code{SEC_NO_FLAGS}, @code{SEC_ALLOC}, ...@code{SET_NEVER_LOAD}.
150
151$ flagword section_flags;
152
153The pad character for filenames within an archive header.
154
155$ char ar_pad_char;
156
157The maximum number of characters in an archive header.
158
159$ unsigned short ar_max_namelen;
160
161The minimum alignment restriction for any section.
162
163$ unsigned int align_power_min;
164
165Entries for byte swapping for data. These are different to the other
4e6f9223 166entry points, since they don't take BFD as first arg. Certain other handlers
6f715d66
SC
167could do the same.
168
81f3996f
SC
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 *));
6f715d66
SC
175
176Byte swapping for the headers
177
4e6f9223 178$ SDEF (bfd_vma, bfd_h_getx64, (bfd_byte *));
81f3996f
SC
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 *));
6f715d66
SC
184
185Format dependent routines, these turn into vectors of entry points
186within the target vector structure; one for each format to check.
187
188Check the format of a file being read. Return bfd_target * or zero.
189
190$ SDEF_FMT (struct bfd_target *, _bfd_check_format, (bfd *));
191
192Set the format of a file being written.
193
194$ SDEF_FMT (boolean, _bfd_set_format, (bfd *));
195
196Write cached information into a file being written, at bfd_close.
197
198$ SDEF_FMT (boolean, _bfd_write_contents, (bfd *));
199
200The following functions are defined in @code{JUMP_TABLE}. The idea is
201that the back end writer of @code{foo} names all the routines
202@code{foo_}@var{entry_point}, @code{JUMP_TABLE} will built the entries
203in this structure in the right order.
204
205Core file entry points
206
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 *));
210
211Archive entry points
212
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,
218$ struct orl *map,
f58809fd 219$ unsigned int orl_count,
6f715d66
SC
220$ int stridx));
221
222Standard stuff.
223
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));
230
231Symbols and reloctions
232
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 *,
92c78ee6 241$ bfd_print_symbol_type));
6f715d66
SC
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 *));
244$
245$ SDEF (boolean, _bfd_set_arch_mach, (bfd *, enum bfd_architecture,
246$ unsigned long));
247$
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 *));
254$
255$ SDEF (int, _bfd_sizeof_headers, (bfd *, boolean));
256$
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 *));
260
261Special entry points for gdb to swap in coff symbol table parts
262
263$ SDEF(void, _bfd_coff_swap_aux_in,(
264$ bfd *abfd ,
265$ PTR ext,
266$ int type,
267$ int class ,
268$ PTR in));
269$
270$ SDEF(void, _bfd_coff_swap_sym_in,(
271$ bfd *abfd ,
272$ PTR ext,
273$ PTR in));
274$
275$ SDEF(void, _bfd_coff_swap_lineno_in, (
276$ bfd *abfd,
277$ PTR ext,
278$ PTR in));
279$
0d740984
SC
280
281Special entry points for gas to swap coff parts
282
283$ SDEF(unsigned int, _bfd_coff_swap_aux_out,(
284$ bfd *abfd,
285$ PTR in,
286$ int type,
287$ int class,
288$ PTR ext));
289$
290$ SDEF(unsigned int, _bfd_coff_swap_sym_out,(
291$ bfd *abfd,
292$ PTR in,
293$ PTR ext));
294$
295$ SDEF(unsigned int, _bfd_coff_swap_lineno_out,(
296$ bfd *abfd,
297$ PTR in,
298$ PTR ext));
299$
300$ SDEF(unsigned int, _bfd_coff_swap_reloc_out,(
301$ bfd *abfd,
302$ PTR src,
303$ PTR dst));
304$
305$ SDEF(unsigned int, _bfd_coff_swap_filehdr_out,(
306$ bfd *abfd,
307$ PTR in,
308$ PTR out));
309$
310$ SDEF(unsigned int, _bfd_coff_swap_aouthdr_out,(
311$ bfd *abfd,
312$ PTR in,
313$ PTR out));
314$
315$ SDEF(unsigned int, _bfd_coff_swap_scnhdr_out,(
316$ bfd *abfd,
317$ PTR in,
318$ PTR out));
319$
6f715d66
SC
320$} bfd_target;
321
322*---
323
324*/
f8adc62d
JG
325
326/* The default is to define a target_vector containing just the
327 DEFAULT_TARGET. (This is to save space in the executables.)
328 You can override this by giving an explicit target_vector using
329 the SELECT_VECTOR macro.
330 Or define ALL_TARGETS macro to get all of the available targets. */
331
332
333#if MINIMIZE && defined(DEFAULT_VECTOR) && !defined(SELECT_VECS)
334#ifdef TRAD_CORE
335#define SELECT_VECS &DEFAULT_VECTOR,&trad_core_vec
336#else
337#define SELECT_VECS &DEFAULT_VECTOR
338#endif
339#endif
340
23b0b558
JG
341extern bfd_target ecoff_little_vec;
342extern bfd_target ecoff_big_vec;
2b1d8a50 343extern bfd_target sunos_big_vec;
7ed4093a 344extern bfd_target demo_64_vec;
4a81b561
DHW
345extern bfd_target srec_vec;
346extern bfd_target b_out_vec_little_host;
347extern bfd_target b_out_vec_big_host;
348extern bfd_target icoff_little_vec;
349extern bfd_target icoff_big_vec;
3f85ebce
JG
350extern bfd_target elf_little_vec;
351extern bfd_target elf_big_vec;
aacf30e3
DHW
352extern bfd_target ieee_vec;
353extern bfd_target oasys_vec;
f8adc62d 354extern bfd_target m88kbcs_vec;
c407897e 355extern bfd_target m68kcoff_vec;
20fdc627 356extern bfd_target i386coff_vec;
bbc8d484 357extern bfd_target i386aout_vec;
41f50af0 358extern bfd_target a29kcoff_big_vec;
f58809fd 359extern bfd_target trad_core_vec;
cbdc7909 360extern bfd_target rs6000coff_vec;
3b4f1a5d 361extern bfd_target h8300coff_vec;
4e6f9223 362
f8adc62d
JG
363#ifdef DEFAULT_VECTOR
364extern bfd_target DEFAULT_VECTOR;
365#endif
366
4e6f9223
SC
367#ifdef SELECT_VECS
368
369bfd_target *target_vector[] = {
370SELECT_VECS,
3710
372
373};
374#else
c0e5039e 375
7d774e01 376#ifdef GNU960
6f715d66
SC
377#define ICOFF_LITTLE_VEC icoff_little_vec
378#define ICOFF_BIG_VEC icoff_big_vec
379#define B_OUT_VEC_LITTLE_HOST b_out_vec_little_host
380#define B_OUT_VEC_BIG_HOST b_out_vec_big_host
7d774e01 381#endif /* GNU960 */
9846338e 382
9872a49c 383#ifndef RESTRICTED
6f715d66
SC
384#define ECOFF_LITTLE_VEC ecoff_little_vec
385#define ECOFF_BIG_VEC ecoff_big_vec
386#define ICOFF_LITTLE_VEC icoff_little_vec
387#define ICOFF_BIG_VEC icoff_big_vec
3f85ebce
JG
388#define ELF_LITTLE_VEC elf_little_vec
389#define ELF_BIG_VEC elf_big_vec
6f715d66
SC
390#define ZB_OUT_VEC_LITTLE_HOST b_out_vec_little_host
391#define ZB_OUT_VEC_BIG_HOST b_out_vec_big_host
392#define SUNOS_VEC_BIG_HOST sunos_big_vec
393#define DEMO_64_VEC demo_64_vec
92c78ee6
RP
394
395/* We have no oasys tools anymore, so we can't test any of this
396 anymore. If you want to test the stuff yourself, go ahead...
397 steve@cygnus.com */
398#if 0
6f715d66 399#define OASYS_VEC oasys_vec
92c78ee6
RP
400#endif
401
6f715d66 402#define IEEE_VEC ieee_vec
f8adc62d 403#define M88KBCS_VEC m88kbcs_vec
6f715d66
SC
404#define SREC_VEC srec_vec
405#define M68KCOFF_VEC m68kcoff_vec
406#define I386COFF_VEC i386coff_vec
bbc8d484 407#define I386AOUT_VEC i386aout_vec
41f50af0 408#define A29KCOFF_BIG_VEC a29kcoff_big_vec
cbdc7909 409#define RS6000COFF_VEC rs6000coff_vec
3b4f1a5d 410#define H8300COFF_VEC h8300coff_vec
9872a49c 411#endif
4e6f9223 412
7d774e01 413bfd_target *target_vector[] = {
9846338e 414
7d774e01 415#ifdef DEFAULT_VECTOR
6f715d66 416 &DEFAULT_VECTOR,
92c78ee6 417#endif
7d774e01 418
6f715d66
SC
419#ifdef I386COFF_VEC
420 &I386COFF_VEC,
92c78ee6 421#endif
20fdc627 422
bbc8d484
JG
423#ifdef I386AOUT_VEC
424 &I386AOUT_VEC,
425#endif
426
23b0b558 427#ifdef ECOFF_LITTLE_VEC
6f715d66 428 &ECOFF_LITTLE_VEC,
23b0b558
JG
429#endif
430
431#ifdef ECOFF_BIG_VEC
6f715d66 432 &ECOFF_BIG_VEC,
23b0b558 433#endif
92c78ee6 434
7d774e01 435#ifdef IEEE_VEC
6f715d66 436 &IEEE_VEC,
92c78ee6 437#endif
7d774e01
RP
438
439#ifdef OASYS_VEC
6f715d66 440 &OASYS_VEC,
92c78ee6 441#endif
7d774e01 442
2b1d8a50 443#ifdef SUNOS_VEC_BIG_HOST
6f715d66 444 &SUNOS_VEC_BIG_HOST,
92c78ee6 445#endif
7ed4093a
SC
446
447#ifdef HOST_64_BIT
448#ifdef DEMO_64_VEC
6f715d66 449 &DEMO_64_VEC,
7ed4093a
SC
450#endif
451#endif
452
3b4f1a5d
SC
453#ifdef H300COFF_VEC
454 &h8300coff_vec,
455#endif
f8adc62d
JG
456#ifdef M88KBCS_VEC
457 &M88KBCS_VEC,
92c78ee6 458#endif
7d774e01
RP
459
460#ifdef SREC_VEC
6f715d66 461 &SREC_VEC,
92c78ee6 462#endif
6f715d66 463
7d774e01 464#ifdef ICOFF_LITTLE_VEC
6f715d66 465 &ICOFF_LITTLE_VEC,
92c78ee6 466#endif
7d774e01
RP
467
468#ifdef ICOFF_BIG_VEC
6f715d66 469 &ICOFF_BIG_VEC,
92c78ee6 470#endif
7d774e01 471
3f85ebce
JG
472#ifdef ELF_LITTLE_VEC
473 &ELF_LITTLE_VEC,
92c78ee6 474#endif
3f85ebce
JG
475
476#ifdef ELF_BIG_VEC
477 &ELF_BIG_VEC,
92c78ee6 478#endif
3f85ebce 479
7d774e01 480#ifdef B_OUT_VEC_LITTLE_HOST
6f715d66 481 &B_OUT_VEC_LITTLE_HOST,
92c78ee6 482#endif
9846338e 483
7d774e01 484#ifdef B_OUT_VEC_BIG_HOST
6f715d66 485 &B_OUT_VEC_BIG_HOST,
92c78ee6 486#endif
9846338e 487
6f715d66
SC
488#ifdef M68KCOFF_VEC
489 &M68KCOFF_VEC,
92c78ee6 490#endif
20fdc627 491
41f50af0
SC
492#ifdef A29KCOFF_BIG_VEC
493 &A29KCOFF_BIG_VEC,
92c78ee6
RP
494#endif
495
496#ifdef TRAD_CORE
f58809fd 497 &trad_core_vec,
92c78ee6 498#endif
41f50af0 499
cbdc7909
JG
500#ifdef RS6000COFF_VEC
501 &RS6000COFF_VEC,
502#endif
503
6f715d66 504 NULL, /* end of list marker */
7d774e01 505};
c0e5039e 506
4e6f9223 507#endif
c0e5039e
JG
508
509/* default_vector[0] contains either the address of the default vector,
510 if there is one, or zero if there isn't. */
511
512bfd_target *default_vector[] = {
513#ifdef DEFAULT_VECTOR
6f715d66 514 &DEFAULT_VECTOR,
c0e5039e 515#endif
6f715d66 516 0,
c0e5039e 517};
6f715d66
SC
518
519
520
521
522/*proto*
523*i bfd_find_target
524Returns a pointer to the transfer vector for the object target
525named target_name. If target_name is NULL, chooses the one in the
526environment variable GNUTARGET; if that is null or not defined then
527the first entry in the target list is chosen. Passing in the
528string "default" or setting the environment variable to "default"
529will cause the first entry in the target list to be returned,
4e6f9223
SC
530and "target_defaulted" will be set in the BFD. This causes
531@code{bfd_check_format} to loop over all the targets to find the one
6f715d66
SC
532that matches the file being read.
533*; PROTO(bfd_target *, bfd_find_target,(CONST char *, bfd *));
534*-*/
535
536bfd_target *
537DEFUN(bfd_find_target,(target_name, abfd),
538 CONST char *target_name AND
539 bfd *abfd)
540{
541 bfd_target **target;
542 extern char *getenv ();
f8adc62d
JG
543 CONST char *targname = (target_name ? target_name :
544 (CONST char *) getenv ("GNUTARGET"));
6f715d66
SC
545
546 /* This is safe; the vector cannot be null */
547 if (targname == NULL || !strcmp (targname, "default")) {
548 abfd->target_defaulted = true;
549 return abfd->xvec = target_vector[0];
550 }
551
552 abfd->target_defaulted = false;
553
554 for (target = &target_vector[0]; *target != NULL; target++) {
555 if (!strcmp (targname, (*target)->name))
556 return abfd->xvec = *target;
557 }
558
559 bfd_error = invalid_target;
560 return NULL;
561}
562
563
564/*proto*
565*i bfd_target_list
566This function returns a freshly malloced NULL-terminated vector of the
4e6f9223 567names of all the valid BFD targets. Do not modify the names
6f715d66
SC
568*; PROTO(CONST char **,bfd_target_list,());
569
570*-*/
571
572CONST char **
573DEFUN_VOID(bfd_target_list)
574{
575 int vec_length= 0;
576 bfd_target **target;
4e6f9223 577 CONST char **name_list, **name_ptr;
6f715d66
SC
578
579 for (target = &target_vector[0]; *target != NULL; target++)
580 vec_length++;
581
582 name_ptr =
583 name_list = (CONST char **) zalloc ((vec_length + 1) * sizeof (char **));
584
585 if (name_list == NULL) {
586 bfd_error = no_memory;
587 return NULL;
588 }
589
6f715d66
SC
590 for (target = &target_vector[0]; *target != NULL; target++)
591 *(name_ptr++) = (*target)->name;
592
593 return name_list;
594}
This page took 0.063943 seconds and 4 git commands to generate.