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