1 /* BFD library support routines for architectures.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Hacked by John Gilmore and Steve Chamberlain of Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 @section Architectures
24 BFD's idea of an architecture is implimented in @code{archures.c}. BFD
25 keeps one atom in a BFD describing the architecture of the data
26 attached to the BFD; a pointer to a @code{bfd_arch_info_type}.
28 Pointers to structures can be requested independently of a bfd so that
29 an architecture's information can be interrogated without access to an
32 The arch information is provided by each architecture package. The
33 set of default architectures is selected by the #define
34 @code{SELECT_ARCHITECTURES}. This is normally set up in the
35 @code{config\/h\-} file of your choice. If the name is not defined,
36 then all the architectures supported are included.
38 When BFD starts up, all the architectures are called with an
39 initialize method. It is up to the architecture back end to insert as
40 many items into the list of arches as it wants to, generally this
41 would be one for each machine and one for the default case (an item
42 with a machine field of 0).
46 /*proto* bfd_architecture
47 This enum gives the object file's CPU
48 architecture, in a global sense. E.g. what processor family does it
49 belong to? There is another field, which indicates what processor
50 within the family is in use. The machine gives a number which
51 distingushes different versions of the architecture, containing for
52 example 2 and 3 for Intel i960 KA and i960 KB, and 68020 and 68030 for
53 Motorola 68020 and 68030.
58 bfd_arch_unknown, {* File arch not known *}
59 bfd_arch_obscure, {* Arch known, not one of these *}
60 bfd_arch_m68k, {* Motorola 68xxx *}
61 bfd_arch_vax, {* DEC Vax *}
62 bfd_arch_i960, {* Intel 960 *}
63 {* The order of the following is important.
64 lower number indicates a machine type that
65 only accepts a subset of the instructions
66 available to machines with higher numbers.
67 The exception is the "ca", which is
68 incompatible with all other machines except
71 #define bfd_mach_i960_core 1
72 #define bfd_mach_i960_ka_sa 2
73 #define bfd_mach_i960_kb_sb 3
74 #define bfd_mach_i960_mc 4
75 #define bfd_mach_i960_xa 5
76 #define bfd_mach_i960_ca 6
78 bfd_arch_a29k, {* AMD 29000 *}
79 bfd_arch_sparc, {* SPARC *}
80 bfd_arch_mips, {* MIPS Rxxxx *}
81 bfd_arch_i386, {* Intel 386 *}
82 bfd_arch_ns32k, {* National Semiconductor 32xxx *}
83 bfd_arch_tahoe, {* CCI/Harris Tahoe *}
84 bfd_arch_i860, {* Intel 860 *}
85 bfd_arch_romp, {* IBM ROMP PC/RT *}
86 bfd_arch_alliant, {* Alliant *}
87 bfd_arch_convex, {* Convex *}
88 bfd_arch_m88k, {* Motorola 88xxx *}
89 bfd_arch_pyramid, {* Pyramid Technology *}
90 bfd_arch_h8300, {* Hitachi H8/300 *}
91 bfd_arch_rs6000, {* IBM RS/6000 *}
108 /*proto* bfd_arch_info
109 This structure contains information on architectures.
111 typedef int bfd_reloc_code_type;
113 typedef struct bfd_arch_info
116 int bits_per_address;
118 enum bfd_architecture arch;
121 CONST char *printable_name;
122 {* true if this is the default machine for the architecture *}
124 CONST struct bfd_arch_info * EXFUN((*compatible),(CONST struct bfd_arch_info *a,
125 CONST struct bfd_arch_info *b));
127 boolean EXFUN((*scan),(CONST struct bfd_arch_info *,CONST char *));
128 unsigned int EXFUN((*disassemble),(bfd_vma addr, CONST char *data,
130 CONST struct reloc_howto_struct *EXFUN((*reloc_type_lookup), (CONST struct
132 bfd_reloc_code_type code));
134 struct bfd_arch_info *next;
136 } bfd_arch_info_type;
142 bfd_arch_info_type *bfd_arch_info_list;
145 /*proto* bfd_printable_name
147 Return a printable string representing the architecture and machine
148 from the pointer to the arch info structure
150 *; CONST char *EXFUN(bfd_printable_name,(bfd *abfd));
155 DEFUN(bfd_printable_name, (abfd),
158 return abfd->arch_info->printable_name;
165 This routine is provided with a string and tries to work out if bfd
166 supports any cpu which could be described with the name provided. The
167 routine returns a pointer to an arch_info structure if a machine is
168 found, otherwise NULL.
170 *; bfd_arch_info_type *EXFUN(bfd_scan_arch,(CONST char *));
174 DEFUN(bfd_scan_arch,(string),
177 struct bfd_arch_info *ap;
179 /* Look through all the installed architectures */
180 for (ap = bfd_arch_info_list;
181 ap != (bfd_arch_info_type *)NULL;
184 if (ap->scan(ap, string))
187 return (bfd_arch_info_type *)NULL;
192 /*proto* bfd_arch_get_compatible
193 This routine is used to determine whether two BFDs' architectures and
194 machine types are compatible. It calculates the lowest common
195 denominator between the two architectures and machine types implied by
196 the BFDs and returns a pointer to an arch_info structure describing
197 the compatible machine.
199 *; CONST bfd_arch_info_type *EXFUN(bfd_arch_get_compatible,
204 CONST bfd_arch_info_type *
205 DEFUN(bfd_arch_get_compatible,(abfd, bbfd),
210 return abfd->arch_info->compatible(abfd->arch_info,bbfd->arch_info);
214 /*proto-internal* bfd_default_arch_struct
216 What bfds are seeded with
219 extern bfd_arch_info_type bfd_default_arch_struct;
223 bfd_arch_info_type bfd_default_arch_struct =
225 32,32,8,bfd_arch_unknown,0,"unknown","unknown",true,
226 bfd_default_compatible, bfd_default_scan,
230 /*proto* bfd_set_arch_info
232 *; void EXFUN(bfd_set_arch_info,(bfd *, bfd_arch_info_type *));
236 void DEFUN(bfd_set_arch_info,(abfd, arg),
238 bfd_arch_info_type *arg)
240 abfd->arch_info = arg;
243 /*proto-internal* bfd_default_set_arch_mach
245 Set the architecture and machine type in a bfd. This finds the correct
246 pointer to structure and inserts it into the arch_info pointer.
248 *; boolean EXFUN(bfd_default_set_arch_mach,(bfd *abfd,
249 enum bfd_architecture arch,
250 unsigned long mach));
254 boolean DEFUN(bfd_default_set_arch_mach,(abfd, arch, mach),
256 enum bfd_architecture arch AND
259 static struct bfd_arch_info *old_ptr = &bfd_default_arch_struct;
260 boolean found = false;
261 /* run through the table to find the one we want, we keep a little
262 cache to speed things up */
263 if (old_ptr == 0 || arch != old_ptr->arch || mach != old_ptr->mach) {
264 bfd_arch_info_type *ptr;
265 old_ptr = (bfd_arch_info_type *)NULL;
266 for (ptr = bfd_arch_info_list;
267 ptr != (bfd_arch_info_type *)NULL;
269 if (ptr->arch == arch &&
270 ((ptr->mach == mach) || (ptr->the_default && mach == 0))) {
277 /*looked for it and it wasn't there, so put in the default */
278 old_ptr = &bfd_default_arch_struct;
283 /* it was in the cache */
287 abfd->arch_info = old_ptr;
296 /*proto* bfd_get_arch
298 Returns the enumerated type which describes the supplied bfd's
301 *; enum bfd_architecture EXFUN(bfd_get_arch, (bfd *abfd));
304 enum bfd_architecture DEFUN(bfd_get_arch, (abfd), bfd *abfd)
306 return abfd->arch_info->arch;
311 /*proto* bfd_get_mach
313 Returns the long type which describes the supplied bfd's
316 *; unsigned long EXFUN(bfd_get_mach, (bfd *abfd));
319 unsigned long DEFUN(bfd_get_mach, (abfd), bfd *abfd)
321 return abfd->arch_info->mach;
324 /*proto* bfd_arch_bits_per_byte
326 Returns the number of bits in one of the architectures bytes
328 *; unsigned int EXFUN(bfd_arch_bits_per_byte, (bfd *abfd));
331 unsigned int DEFUN(bfd_arch_bits_per_byte, (abfd), bfd *abfd)
333 return abfd->arch_info->bits_per_byte;
336 /*proto* bfd_arch_bits_per_address
338 Returns the number of bits in one of the architectures addresses
340 *; unsigned int EXFUN(bfd_arch_bits_per_address, (bfd *abfd));
343 unsigned int DEFUN(bfd_arch_bits_per_address, (abfd), bfd *abfd)
345 return abfd->arch_info->bits_per_address;
350 extern void EXFUN(bfd_h8300_arch,(void));
351 extern void EXFUN(bfd_i960_arch,(void));
352 extern void EXFUN(bfd_empty_arch,(void));
353 extern void EXFUN(bfd_sparc_arch,(void));
354 extern void EXFUN(bfd_m88k_arch,(void));
355 extern void EXFUN(bfd_m68k_arch,(void));
356 extern void EXFUN(bfd_vax_arch,(void));
357 extern void EXFUN(bfd_a29k_arch,(void));
358 extern void EXFUN(bfd_mips_arch,(void));
359 extern void EXFUN(bfd_i386_arch,(void));
360 extern void EXFUN(bfd_rs6000_arch,(void));
364 static void EXFUN((*archures_init_table[]),()) =
366 #ifdef SELECT_ARCHITECTURES
367 SELECT_ARCHITECTURES,
387 This routine initializes the architecture dispatch table by calling
388 all installed architecture packages and getting them to poke around.
390 *; PROTO(void, bfd_arch_init,(void));
395 DEFUN_VOID(bfd_arch_init)
397 void EXFUN((**ptable),());
398 for (ptable = archures_init_table;
407 /*proto-internal* bfd_arch_linkin
409 Link the provided arch info structure into the list
411 *; void EXFUN(bfd_arch_linkin,(bfd_arch_info_type *));
415 void DEFUN(bfd_arch_linkin,(ptr),
416 bfd_arch_info_type *ptr)
418 ptr->next = bfd_arch_info_list;
419 bfd_arch_info_list = ptr;
423 /*proto-internal* bfd_default_compatible
425 The default function for testing for compatibility
427 *; CONST bfd_arch_info_type *EXFUN(bfd_default_compatible,
428 (CONST bfd_arch_info_type *a,
429 CONST bfd_arch_info_type *b));
432 CONST bfd_arch_info_type *
433 DEFUN(bfd_default_compatible,(a,b),
434 CONST bfd_arch_info_type *a AND
435 CONST bfd_arch_info_type *b)
437 if(a->arch != b->arch) return NULL;
439 if (a->mach > b->mach) {
442 if (b->mach > a->mach) {
448 /*proto-internal* bfd_default_scan
449 The default function for working out whether this is an architecture
450 hit and a machine hit
452 *; boolean EXFUN(bfd_default_scan,(CONST struct bfd_arch_info *, CONST char *));
457 DEFUN(bfd_default_scan,(info, string),
458 CONST struct bfd_arch_info *info AND
463 unsigned long number;
464 enum bfd_architecture arch;
465 /* First test for an exact match */
466 if (strcmp(string, info->printable_name) == 0) return true;
468 /* See how much of the supplied string matches with the
469 architecture, eg the string m68k:68020 would match the 68k entry
470 up to the :, then we get left with the machine number */
472 for (ptr_src = string,
473 ptr_tst = info->arch_name;
474 *ptr_src && *ptr_tst;
478 if (*ptr_src != *ptr_tst) break;
481 /* Chewed up as much of the architecture as will match, skip any
483 if (*ptr_src == ':') ptr_src++;
486 /* nothing more, then only keep this one if it is the default
487 machine for this architecture */
488 return info->the_default;
491 while (isdigit(*ptr_src)) {
492 number = number * 10 + *ptr_src - '0';
504 arch = bfd_arch_m68k;
509 arch = bfd_arch_i386;
512 arch = bfd_arch_a29k;
523 arch = bfd_arch_ns32k;
528 arch = bfd_arch_i860;
532 arch = bfd_arch_rs6000;
538 if (arch != info->arch)
541 if (number != info->mach)
550 /*proto* bfd_get_arch_info
552 *; bfd_arch_info_type * EXFUN(bfd_get_arch_info,(bfd *));
557 DEFUN(bfd_get_arch_info,(abfd),
560 return abfd->arch_info;
564 /*proto* bfd_lookup_arch
566 *; bfd_arch_info_type * EXFUN(bfd_lookup_arch,(enum
567 bfd_architecture arch,long machine));
569 Look for the architecure info struct which matches the arguments
570 given. A machine of 0 will match the machine/architecture structure which
571 marks itself as the default.
576 DEFUN(bfd_lookup_arch,(arch, machine),
577 enum bfd_architecture arch AND
580 bfd_arch_info_type *ap;
582 for (ap = bfd_arch_info_list;
583 ap != (bfd_arch_info_type *)NULL;
585 if (ap->arch == arch &&
586 ((ap->mach == machine) || (ap->the_default && machine == 0))) {
590 return (bfd_arch_info_type *)NULL;
595 /*proto* bfd_printable_arch_mach
596 Return a printable string representing the architecture and machine
599 NB. The use of this routine is depreciated.
601 *; PROTO(CONST char *,bfd_printable_arch_mach,
602 (enum bfd_architecture arch, unsigned long machine));
606 DEFUN(bfd_printable_arch_mach,(arch, machine),
607 enum bfd_architecture arch AND
608 unsigned long machine)
610 bfd_arch_info_type *ap = bfd_lookup_arch(arch, machine);
611 if(ap) return ap->printable_name;