* hosts/h-*.h: Configure fopen using ../include/fopen-*.h
[deliverable/binutils-gdb.git] / bfd / archures.c
CommitLineData
c618de01
SC
1/* BFD library support routines for architectures.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
4e6f9223
SC
3 Hacked by John Gilmore and Steve Chamberlain of Cygnus Support.
4
4a81b561 5
c618de01 6This file is part of BFD, the Binary File Descriptor library.
4a81b561 7
c618de01 8This program is free software; you can redistribute it and/or modify
4a81b561 9it under the terms of the GNU General Public License as published by
c618de01
SC
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
4a81b561 12
c618de01 13This program is distributed in the hope that it will be useful,
4a81b561
DHW
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
c618de01
SC
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
9fda1a39 22/*
4e6f9223 23
9fda1a39
SC
24SECTION
25 Architectures
26
27DESCRIPTION
28
29 BFD's idea of an architecture is implimented in
30 <<archures.c>>. BFD keeps one atom in a BFD describing the
31 architecture of the data attached to the BFD; a pointer to a
32 <<bfd_arch_info_type>>.
33
34 Pointers to structures can be requested independently of a bfd
35 so that an architecture's information can be interrogated
36 without access to an open bfd.
37
38 The arch information is provided by each architecture package.
39 The set of default architectures is selected by the #define
40 <<SELECT_ARCHITECTURES>>. This is normally set up in the
41 <<config\/h\->> file of your choice. If the name is not
42 defined, then all the architectures supported are included.
43
44 When BFD starts up, all the architectures are called with an
45 initialize method. It is up to the architecture back end to
46 insert as many items into the list of arches as it wants to,
47 generally this would be one for each machine and one for the
48 default case (an item with a machine field of 0).
c618de01
SC
49*/
50
9fda1a39
SC
51/*
52
53SUBSECTION
54 bfd_architecture
55
56DESCRIPTION
57 This enum gives the object file's CPU architecture, in a
58 global sense. E.g. what processor family does it belong to?
59 There is another field, which indicates what processor within
60 the family is in use. The machine gives a number which
61 distingushes different versions of the architecture,
62 containing for example 2 and 3 for Intel i960 KA and i960 KB,
63 and 68020 and 68030 for Motorola 68020 and 68030.
64
65.enum bfd_architecture
66.{
67. bfd_arch_unknown, {* File arch not known *}
68. bfd_arch_obscure, {* Arch known, not one of these *}
69. bfd_arch_m68k, {* Motorola 68xxx *}
70. bfd_arch_vax, {* DEC Vax *}
71. bfd_arch_i960, {* Intel 960 *}
72. {* The order of the following is important.
73. lower number indicates a machine type that
74. only accepts a subset of the instructions
75. available to machines with higher numbers.
76. The exception is the "ca", which is
77. incompatible with all other machines except
78. "core". *}
79.
80.#define bfd_mach_i960_core 1
81.#define bfd_mach_i960_ka_sa 2
82.#define bfd_mach_i960_kb_sb 3
83.#define bfd_mach_i960_mc 4
84.#define bfd_mach_i960_xa 5
85.#define bfd_mach_i960_ca 6
86.
87. bfd_arch_a29k, {* AMD 29000 *}
88. bfd_arch_sparc, {* SPARC *}
89. bfd_arch_mips, {* MIPS Rxxxx *}
90. bfd_arch_i386, {* Intel 386 *}
91. bfd_arch_ns32k, {* National Semiconductor 32xxx *}
92. bfd_arch_tahoe, {* CCI/Harris Tahoe *}
93. bfd_arch_i860, {* Intel 860 *}
94. bfd_arch_romp, {* IBM ROMP PC/RT *}
95. bfd_arch_alliant, {* Alliant *}
96. bfd_arch_convex, {* Convex *}
97. bfd_arch_m88k, {* Motorola 88xxx *}
98. bfd_arch_pyramid, {* Pyramid Technology *}
99. bfd_arch_h8300, {* Hitachi H8/300 *}
100. bfd_arch_rs6000, {* IBM RS/6000 *}
101. bfd_arch_last
102. };
c618de01 103
c618de01
SC
104
105*/
106
107
4a81b561
DHW
108
109/* $Id$ */
110
4a81b561 111#include "bfd.h"
cbdc7909 112#include "sysdep.h"
4e6f9223
SC
113#include "libbfd.h"
114
9fda1a39
SC
115/*
116
117SUBSECTION
118 bfd_arch_info
119
120DESCRIPTION
121 This structure contains information on architectures for use
122 within BFD.
123
124.typedef int bfd_reloc_code_type;
125.
126.typedef struct bfd_arch_info
127.{
128. int bits_per_word;
129. int bits_per_address;
130. int bits_per_byte;
131. enum bfd_architecture arch;
132. long mach;
133. char *arch_name;
134. CONST char *printable_name;
135.{* true if this is the default machine for the architecture *}
136. boolean the_default;
137. CONST struct bfd_arch_info * EXFUN((*compatible),
138. (CONST struct bfd_arch_info *a,
139. CONST struct bfd_arch_info *b));
140.
141. boolean EXFUN((*scan),(CONST struct bfd_arch_info *,CONST char *));
142. unsigned int EXFUN((*disassemble),(bfd_vma addr, CONST char *data,
143. PTR stream));
144. CONST struct reloc_howto_struct *EXFUN((*reloc_type_lookup),
145. (CONST struct bfd_arch_info *,
146. bfd_reloc_code_type code));
147.
148. struct bfd_arch_info *next;
149.
150.} bfd_arch_info_type;
4e6f9223
SC
151*/
152
cbdc7909 153bfd_arch_info_type *bfd_arch_info_list;
4a81b561 154
4a81b561 155
9fda1a39
SC
156/*
157
158FUNCTION
159 bfd_printable_name
4e6f9223 160
9fda1a39
SC
161DESCRIPTION
162 Return a printable string representing the architecture and machine
163 from the pointer to the arch info structure
4e6f9223 164
9fda1a39
SC
165SYNOPSIS
166 CONST char *bfd_printable_name(bfd *abfd);
4e6f9223
SC
167
168*/
169
170CONST char *
171DEFUN(bfd_printable_name, (abfd),
172 bfd *abfd)
173{
174 return abfd->arch_info->printable_name;
4a81b561
DHW
175}
176
4e6f9223
SC
177
178
9fda1a39
SC
179/*
180FUNCTION
181 bfd_scan_arch
4e6f9223 182
9fda1a39
SC
183DESCRIPTION
184 This routine is provided with a string and tries to work out
185 if bfd supports any cpu which could be described with the name
186 provided. The routine returns a pointer to an arch_info
187 structure if a machine is found, otherwise NULL.
188
189SYNOPSIS
190 bfd_arch_info_type *bfd_scan_arch(CONST char *);
c618de01 191*/
4a81b561 192
cbdc7909 193bfd_arch_info_type *
4e6f9223
SC
194DEFUN(bfd_scan_arch,(string),
195 CONST char *string)
4a81b561 196{
cbdc7909 197 struct bfd_arch_info *ap;
4e6f9223
SC
198
199 /* Look through all the installed architectures */
200 for (ap = bfd_arch_info_list;
cbdc7909 201 ap != (bfd_arch_info_type *)NULL;
4e6f9223 202 ap = ap->next) {
cbdc7909 203
4e6f9223
SC
204 if (ap->scan(ap, string))
205 return ap;
4a81b561 206 }
cbdc7909 207 return (bfd_arch_info_type *)NULL;
4e6f9223
SC
208}
209
4a81b561 210
4a81b561 211
9fda1a39
SC
212/*
213FUNCTION
214 bfd_arch_get_compatible
215
216DESCRIPTION
4e6f9223 217
9fda1a39
SC
218 This routine is used to determine whether two BFDs'
219 architectures and achine types are compatible. It calculates
220 the lowest common denominator between the two architectures
221 and machine types implied by the BFDs and returns a pointer to
222 an arch_info structure describing the compatible machine.
223
224
225SYNOPSIS
226 CONST bfd_arch_info_type *bfd_arch_get_compatible(
227 CONST bfd *abfd,
228 CONST bfd *bbfd);
4e6f9223
SC
229*/
230
cbdc7909 231CONST bfd_arch_info_type *
4e6f9223
SC
232DEFUN(bfd_arch_get_compatible,(abfd, bbfd),
233CONST bfd *abfd AND
234CONST bfd *bbfd)
235
236{
237 return abfd->arch_info->compatible(abfd->arch_info,bbfd->arch_info);
4a81b561
DHW
238}
239
4e6f9223 240
9fda1a39
SC
241/*
242INTERNAL
243
244SUBSECTION
245 bfd_default_arch_struct
4e6f9223 246
9fda1a39
SC
247DESCRIPTION
248 What bfds are seeded with
249
250
251.extern bfd_arch_info_type bfd_default_arch_struct;
4e6f9223 252
4e6f9223
SC
253*/
254
cbdc7909 255bfd_arch_info_type bfd_default_arch_struct =
9fda1a39 256{
4e6f9223 257 32,32,8,bfd_arch_unknown,0,"unknown","unknown",true,
9fda1a39
SC
258 bfd_default_compatible,
259 bfd_default_scan,
260 0,
261 bfd_default_reloc_type_lookup
4e6f9223 262
9fda1a39 263};
4e6f9223 264
9fda1a39
SC
265/*
266FUNCTION
267 bfd_set_arch_info
4e6f9223 268
9fda1a39
SC
269SYNOPSIS
270 void bfd_set_arch_info(bfd *, bfd_arch_info_type *);
4e6f9223
SC
271
272*/
273
274void DEFUN(bfd_set_arch_info,(abfd, arg),
275bfd *abfd AND
cbdc7909 276bfd_arch_info_type *arg)
4a81b561 277{
4e6f9223
SC
278 abfd->arch_info = arg;
279}
280
9fda1a39
SC
281/*
282INTERNAL FUNCTION
283 bfd_default_set_arch_mach
284
285DESCRIPTION
286 Set the architecture and machine type in a bfd. This finds the
287 correct pointer to structure and inserts it into the arch_info
288 pointer.
4e6f9223 289
4e6f9223 290
9fda1a39
SC
291SYNOPSIS
292 boolean bfd_default_set_arch_mach(bfd *abfd,
293 enum bfd_architecture arch,
294 unsigned long mach);
4e6f9223
SC
295
296*/
297
298boolean DEFUN(bfd_default_set_arch_mach,(abfd, arch, mach),
299 bfd *abfd AND
300 enum bfd_architecture arch AND
301 unsigned long mach)
302{
cbdc7909 303 static struct bfd_arch_info *old_ptr = &bfd_default_arch_struct;
4e6f9223
SC
304 boolean found = false;
305 /* run through the table to find the one we want, we keep a little
306 cache to speed things up */
307 if (old_ptr == 0 || arch != old_ptr->arch || mach != old_ptr->mach) {
cbdc7909
JG
308 bfd_arch_info_type *ptr;
309 old_ptr = (bfd_arch_info_type *)NULL;
4e6f9223 310 for (ptr = bfd_arch_info_list;
cbdc7909 311 ptr != (bfd_arch_info_type *)NULL;
4e6f9223
SC
312 ptr= ptr->next) {
313 if (ptr->arch == arch &&
314 ((ptr->mach == mach) || (ptr->the_default && mach == 0))) {
315 old_ptr = ptr;
316 found = true;
317 break;
318 }
4a81b561 319 }
4e6f9223
SC
320 if (found==false) {
321 /*looked for it and it wasn't there, so put in the default */
322 old_ptr = &bfd_default_arch_struct;
323
324 }
325 }
326 else {
327 /* it was in the cache */
328 found = true;
4a81b561
DHW
329 }
330
4e6f9223
SC
331 abfd->arch_info = old_ptr;
332
333 return found;
4a81b561 334}
4a81b561 335
4e6f9223
SC
336
337
338
339
9fda1a39
SC
340/*
341FUNCTION
342 bfd_get_arch
4e6f9223 343
9fda1a39
SC
344DESCRIPTION
345 Returns the enumerated type which describes the supplied bfd's
346 architecture
4e6f9223 347
9fda1a39
SC
348SYNOPSIS
349 enum bfd_architecture bfd_get_arch(bfd *abfd);
4e6f9223
SC
350*/
351
9fda1a39
SC
352enum bfd_architecture DEFUN(bfd_get_arch, (abfd), bfd *abfd)
353{
4e6f9223 354 return abfd->arch_info->arch;
9fda1a39 355}
4e6f9223 356
9fda1a39
SC
357/*
358FUNCTION
359 bfd_get_mach
4e6f9223 360
9fda1a39
SC
361DESCRIPTION
362 Returns the long type which describes the supplied bfd's
363 machine
4e6f9223 364
9fda1a39
SC
365SYNOPSIS
366 unsigned long bfd_get_mach(bfd *abfd);
4e6f9223
SC
367*/
368
9fda1a39
SC
369unsigned long
370DEFUN(bfd_get_mach, (abfd), bfd *abfd)
4a81b561 371{
4e6f9223 372 return abfd->arch_info->mach;
9fda1a39 373}
4e6f9223 374
9fda1a39
SC
375/*
376FUNCTION
377 bfd_arch_bits_per_byte
4e6f9223 378
9fda1a39
SC
379DESCRIPTION
380 Returns the number of bits in one of the architectures bytes
4e6f9223 381
9fda1a39
SC
382SYNOPSIS
383 unsigned int bfd_arch_bits_per_byte(bfd *abfd);
4e6f9223
SC
384*/
385
386unsigned int DEFUN(bfd_arch_bits_per_byte, (abfd), bfd *abfd)
387 {
388 return abfd->arch_info->bits_per_byte;
389 }
390
9fda1a39
SC
391/*
392FUNCTION
393 bfd_arch_bits_per_address
4e6f9223 394
9fda1a39
SC
395DESCRIPTION
396 Returns the number of bits in one of the architectures addresses
4e6f9223 397
9fda1a39
SC
398SYNOPSIS
399 unsigned int bfd_arch_bits_per_address(bfd *abfd);
4e6f9223
SC
400*/
401
402unsigned int DEFUN(bfd_arch_bits_per_address, (abfd), bfd *abfd)
403 {
404 return abfd->arch_info->bits_per_address;
4a81b561 405 }
4e6f9223
SC
406
407
408
409extern void EXFUN(bfd_h8300_arch,(void));
410extern void EXFUN(bfd_i960_arch,(void));
411extern void EXFUN(bfd_empty_arch,(void));
412extern void EXFUN(bfd_sparc_arch,(void));
413extern void EXFUN(bfd_m88k_arch,(void));
414extern void EXFUN(bfd_m68k_arch,(void));
415extern void EXFUN(bfd_vax_arch,(void));
416extern void EXFUN(bfd_a29k_arch,(void));
417extern void EXFUN(bfd_mips_arch,(void));
418extern void EXFUN(bfd_i386_arch,(void));
cbdc7909 419extern void EXFUN(bfd_rs6000_arch,(void));
4e6f9223
SC
420
421
422
423static void EXFUN((*archures_init_table[]),()) =
424{
425#ifdef SELECT_ARCHITECTURES
426 SELECT_ARCHITECTURES,
427#else
428 bfd_sparc_arch,
429 bfd_a29k_arch,
430 bfd_mips_arch,
431 bfd_h8300_arch,
432 bfd_i386_arch,
433 bfd_m88k_arch,
434 bfd_i960_arch,
435 bfd_m68k_arch,
436 bfd_vax_arch,
cbdc7909 437 bfd_rs6000_arch,
4e6f9223
SC
438#endif
439 0
440 };
441
442
443
9fda1a39
SC
444/*
445INTERNAL FUNCTION
446 bfd_arch_init
4e6f9223 447
9fda1a39
SC
448DESCRIPTION
449 This routine initializes the architecture dispatch table by
450 calling all installed architecture packages and getting them
451 to poke around.
4e6f9223 452
9fda1a39
SC
453SYNOPSIS
454 void bfd_arch_init(void);
4e6f9223
SC
455
456*/
457
458void
459DEFUN_VOID(bfd_arch_init)
460{
9fda1a39
SC
461 void EXFUN((**ptable),());
462 for (ptable = archures_init_table;
463 *ptable ;
464 ptable++)
465 {
4e6f9223 466 (*ptable)();
9fda1a39 467 }
4a81b561
DHW
468}
469
4e6f9223 470
9fda1a39
SC
471/*
472INTERNAL FUNCTION
473 bfd_arch_linkin
4e6f9223 474
9fda1a39
SC
475DESCRIPTION
476 Link the provided arch info structure into the list
4e6f9223 477
9fda1a39
SC
478SYNOPSIS
479 void bfd_arch_linkin(bfd_arch_info_type *);
4e6f9223
SC
480
481*/
482
483void DEFUN(bfd_arch_linkin,(ptr),
cbdc7909 484 bfd_arch_info_type *ptr)
4a81b561 485{
4e6f9223
SC
486 ptr->next = bfd_arch_info_list;
487 bfd_arch_info_list = ptr;
488}
4a81b561 489
4a81b561 490
9fda1a39
SC
491/*
492INTERNAL FUNCTION
493 bfd_default_compatible
4e6f9223 494
9fda1a39
SC
495DESCRIPTION
496 The default function for testing for compatibility.
4e6f9223 497
9fda1a39
SC
498SYNOPSIS
499 CONST bfd_arch_info_type *bfd_default_compatible
500 (CONST bfd_arch_info_type *a,
501 CONST bfd_arch_info_type *b);
4e6f9223
SC
502*/
503
cbdc7909 504CONST bfd_arch_info_type *
4e6f9223 505DEFUN(bfd_default_compatible,(a,b),
cbdc7909
JG
506 CONST bfd_arch_info_type *a AND
507 CONST bfd_arch_info_type *b)
4e6f9223 508{
cbdc7909 509 if(a->arch != b->arch) return NULL;
4e6f9223
SC
510
511 if (a->mach > b->mach) {
512 return a;
513 }
514 if (b->mach > a->mach) {
515 return b;
516 }
517 return a;
4a81b561
DHW
518}
519
520
9fda1a39
SC
521/*
522INTERNAL FUNCTION
523 bfd_default_scan
524
525DESCRIPTION
526 The default function for working out whether this is an
527 architecture hit and a machine hit.
528
529SYNOPSIS
530 boolean bfd_default_scan(CONST struct bfd_arch_info *, CONST char *);
4e6f9223
SC
531
532*/
533
534boolean
535DEFUN(bfd_default_scan,(info, string),
cbdc7909 536CONST struct bfd_arch_info *info AND
4e6f9223 537CONST char *string)
4a81b561 538{
9fda1a39
SC
539 CONST char *ptr_src;
540 CONST char *ptr_tst;
541 unsigned long number;
542 enum bfd_architecture arch;
543 /* First test for an exact match */
544 if (strcmp(string, info->printable_name) == 0) return true;
545
546 /* See how much of the supplied string matches with the
547 architecture, eg the string m68k:68020 would match the 68k entry
548 up to the :, then we get left with the machine number */
549
550 for (ptr_src = string,
551 ptr_tst = info->arch_name;
552 *ptr_src && *ptr_tst;
553 ptr_src++,
554 ptr_tst++)
555 {
4e6f9223 556 if (*ptr_src != *ptr_tst) break;
9fda1a39 557 }
4e6f9223 558
9fda1a39
SC
559 /* Chewed up as much of the architecture as will match, skip any
560 colons */
561 if (*ptr_src == ':') ptr_src++;
4e6f9223 562
9fda1a39
SC
563 if (*ptr_src == 0) {
564 /* nothing more, then only keep this one if it is the default
565 machine for this architecture */
566 return info->the_default;
567 }
568 number = 0;
569 while (isdigit(*ptr_src)) {
570 number = number * 10 + *ptr_src - '0';
571 ptr_src++;
572 }
573
574 switch (number)
575 {
576 case 68010:
577 case 68020:
578 case 68030:
579 case 68040:
580 case 68332:
581 case 68050:
582 case 68000:
583 arch = bfd_arch_m68k;
584 break;
585 case 386:
586 case 80386:
587 case 486:
588 arch = bfd_arch_i386;
589 break;
590 case 29000:
591 arch = bfd_arch_a29k;
592 break;
4a81b561 593
9fda1a39
SC
594 case 32016:
595 case 32032:
596 case 32132:
597 case 32232:
598 case 32332:
599 case 32432:
600 case 32532:
601 case 32000:
602 arch = bfd_arch_ns32k;
603 break;
4e6f9223 604
9fda1a39
SC
605 case 860:
606 case 80860:
607 arch = bfd_arch_i860;
608 break;
4a81b561 609
9fda1a39
SC
610 case 6000:
611 arch = bfd_arch_rs6000;
612 break;
613
614 default:
615 return false;
616 }
617 if (arch != info->arch)
618 return false;
619
620 if (number != info->mach)
621 return false;
622
623 return true;
4a81b561 624}
c618de01
SC
625
626
c618de01
SC
627
628
9fda1a39
SC
629/*
630FUNCTION
631 bfd_get_arch_info
c618de01 632
9fda1a39
SC
633
634SYNOPSIS
635 bfd_arch_info_type * bfd_get_arch_info(bfd *);
c618de01 636
4e6f9223 637*/
c618de01 638
cbdc7909 639bfd_arch_info_type *
4e6f9223
SC
640DEFUN(bfd_get_arch_info,(abfd),
641bfd *abfd)
642{
643 return abfd->arch_info;
644}
cbdc7909
JG
645
646
9fda1a39
SC
647/*
648FUNCTION
649 bfd_lookup_arch
650
651DESCRIPTION
652
653 Look for the architecure info struct which matches the
654 arguments given. A machine of 0 will match the
655 machine/architecture structure which marks itself as the
656 default.
657
cbdc7909 658
9fda1a39
SC
659SYNOPSIS
660 bfd_arch_info_type *bfd_lookup_arch
661 (enum bfd_architecture
662 arch,
663 long machine);
cbdc7909 664
cbdc7909
JG
665
666*/
667
668bfd_arch_info_type *
669DEFUN(bfd_lookup_arch,(arch, machine),
670enum bfd_architecture arch AND
671long machine)
672{
9fda1a39
SC
673 bfd_arch_info_type *ap;
674 bfd_check_init();
675 for (ap = bfd_arch_info_list;
676 ap != (bfd_arch_info_type *)NULL;
677 ap = ap->next) {
678 if (ap->arch == arch &&
679 ((ap->mach == machine)
680 || (ap->the_default && machine == 0))) {
681 return ap;
682 }
683 }
684 return (bfd_arch_info_type *)NULL;
cbdc7909
JG
685}
686
687
688
9fda1a39
SC
689/*
690FUNCTION
691 bfd_printable_arch_mach
692
693DESCRIPTION
694 Return a printable string representing the architecture and
695 machine type.
cbdc7909 696
9fda1a39 697 NB. The use of this routine is depreciated.
cbdc7909 698
9fda1a39
SC
699SYNOPSIS
700 CONST char * bfd_printable_arch_mach
701 (enum bfd_architecture arch, unsigned long machine);
cbdc7909
JG
702*/
703
704CONST char *
705DEFUN(bfd_printable_arch_mach,(arch, machine),
706 enum bfd_architecture arch AND
707 unsigned long machine)
708{
9fda1a39
SC
709 bfd_arch_info_type *ap = bfd_lookup_arch(arch, machine);
710 if(ap) return ap->printable_name;
711 return "UNKNOWN!";
cbdc7909 712}
This page took 0.07032 seconds and 4 git commands to generate.