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