* hosts/h-*.h: Configure fopen using ../include/fopen-*.h
[deliverable/binutils-gdb.git] / bfd / archures.c
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
6 This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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. */
21
22 /*
23
24 SECTION
25 Architectures
26
27 DESCRIPTION
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).
49 */
50
51 /*
52
53 SUBSECTION
54 bfd_architecture
55
56 DESCRIPTION
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 . };
103
104
105 */
106
107
108
109 /* $Id$ */
110
111 #include "bfd.h"
112 #include "sysdep.h"
113 #include "libbfd.h"
114
115 /*
116
117 SUBSECTION
118 bfd_arch_info
119
120 DESCRIPTION
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;
151 */
152
153 bfd_arch_info_type *bfd_arch_info_list;
154
155
156 /*
157
158 FUNCTION
159 bfd_printable_name
160
161 DESCRIPTION
162 Return a printable string representing the architecture and machine
163 from the pointer to the arch info structure
164
165 SYNOPSIS
166 CONST char *bfd_printable_name(bfd *abfd);
167
168 */
169
170 CONST char *
171 DEFUN(bfd_printable_name, (abfd),
172 bfd *abfd)
173 {
174 return abfd->arch_info->printable_name;
175 }
176
177
178
179 /*
180 FUNCTION
181 bfd_scan_arch
182
183 DESCRIPTION
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
189 SYNOPSIS
190 bfd_arch_info_type *bfd_scan_arch(CONST char *);
191 */
192
193 bfd_arch_info_type *
194 DEFUN(bfd_scan_arch,(string),
195 CONST char *string)
196 {
197 struct bfd_arch_info *ap;
198
199 /* Look through all the installed architectures */
200 for (ap = bfd_arch_info_list;
201 ap != (bfd_arch_info_type *)NULL;
202 ap = ap->next) {
203
204 if (ap->scan(ap, string))
205 return ap;
206 }
207 return (bfd_arch_info_type *)NULL;
208 }
209
210
211
212 /*
213 FUNCTION
214 bfd_arch_get_compatible
215
216 DESCRIPTION
217
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
225 SYNOPSIS
226 CONST bfd_arch_info_type *bfd_arch_get_compatible(
227 CONST bfd *abfd,
228 CONST bfd *bbfd);
229 */
230
231 CONST bfd_arch_info_type *
232 DEFUN(bfd_arch_get_compatible,(abfd, bbfd),
233 CONST bfd *abfd AND
234 CONST bfd *bbfd)
235
236 {
237 return abfd->arch_info->compatible(abfd->arch_info,bbfd->arch_info);
238 }
239
240
241 /*
242 INTERNAL
243
244 SUBSECTION
245 bfd_default_arch_struct
246
247 DESCRIPTION
248 What bfds are seeded with
249
250
251 .extern bfd_arch_info_type bfd_default_arch_struct;
252
253 */
254
255 bfd_arch_info_type bfd_default_arch_struct =
256 {
257 32,32,8,bfd_arch_unknown,0,"unknown","unknown",true,
258 bfd_default_compatible,
259 bfd_default_scan,
260 0,
261 bfd_default_reloc_type_lookup
262
263 };
264
265 /*
266 FUNCTION
267 bfd_set_arch_info
268
269 SYNOPSIS
270 void bfd_set_arch_info(bfd *, bfd_arch_info_type *);
271
272 */
273
274 void DEFUN(bfd_set_arch_info,(abfd, arg),
275 bfd *abfd AND
276 bfd_arch_info_type *arg)
277 {
278 abfd->arch_info = arg;
279 }
280
281 /*
282 INTERNAL FUNCTION
283 bfd_default_set_arch_mach
284
285 DESCRIPTION
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.
289
290
291 SYNOPSIS
292 boolean bfd_default_set_arch_mach(bfd *abfd,
293 enum bfd_architecture arch,
294 unsigned long mach);
295
296 */
297
298 boolean DEFUN(bfd_default_set_arch_mach,(abfd, arch, mach),
299 bfd *abfd AND
300 enum bfd_architecture arch AND
301 unsigned long mach)
302 {
303 static struct bfd_arch_info *old_ptr = &bfd_default_arch_struct;
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) {
308 bfd_arch_info_type *ptr;
309 old_ptr = (bfd_arch_info_type *)NULL;
310 for (ptr = bfd_arch_info_list;
311 ptr != (bfd_arch_info_type *)NULL;
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 }
319 }
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;
329 }
330
331 abfd->arch_info = old_ptr;
332
333 return found;
334 }
335
336
337
338
339
340 /*
341 FUNCTION
342 bfd_get_arch
343
344 DESCRIPTION
345 Returns the enumerated type which describes the supplied bfd's
346 architecture
347
348 SYNOPSIS
349 enum bfd_architecture bfd_get_arch(bfd *abfd);
350 */
351
352 enum bfd_architecture DEFUN(bfd_get_arch, (abfd), bfd *abfd)
353 {
354 return abfd->arch_info->arch;
355 }
356
357 /*
358 FUNCTION
359 bfd_get_mach
360
361 DESCRIPTION
362 Returns the long type which describes the supplied bfd's
363 machine
364
365 SYNOPSIS
366 unsigned long bfd_get_mach(bfd *abfd);
367 */
368
369 unsigned long
370 DEFUN(bfd_get_mach, (abfd), bfd *abfd)
371 {
372 return abfd->arch_info->mach;
373 }
374
375 /*
376 FUNCTION
377 bfd_arch_bits_per_byte
378
379 DESCRIPTION
380 Returns the number of bits in one of the architectures bytes
381
382 SYNOPSIS
383 unsigned int bfd_arch_bits_per_byte(bfd *abfd);
384 */
385
386 unsigned int DEFUN(bfd_arch_bits_per_byte, (abfd), bfd *abfd)
387 {
388 return abfd->arch_info->bits_per_byte;
389 }
390
391 /*
392 FUNCTION
393 bfd_arch_bits_per_address
394
395 DESCRIPTION
396 Returns the number of bits in one of the architectures addresses
397
398 SYNOPSIS
399 unsigned int bfd_arch_bits_per_address(bfd *abfd);
400 */
401
402 unsigned int DEFUN(bfd_arch_bits_per_address, (abfd), bfd *abfd)
403 {
404 return abfd->arch_info->bits_per_address;
405 }
406
407
408
409 extern void EXFUN(bfd_h8300_arch,(void));
410 extern void EXFUN(bfd_i960_arch,(void));
411 extern void EXFUN(bfd_empty_arch,(void));
412 extern void EXFUN(bfd_sparc_arch,(void));
413 extern void EXFUN(bfd_m88k_arch,(void));
414 extern void EXFUN(bfd_m68k_arch,(void));
415 extern void EXFUN(bfd_vax_arch,(void));
416 extern void EXFUN(bfd_a29k_arch,(void));
417 extern void EXFUN(bfd_mips_arch,(void));
418 extern void EXFUN(bfd_i386_arch,(void));
419 extern void EXFUN(bfd_rs6000_arch,(void));
420
421
422
423 static 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,
437 bfd_rs6000_arch,
438 #endif
439 0
440 };
441
442
443
444 /*
445 INTERNAL FUNCTION
446 bfd_arch_init
447
448 DESCRIPTION
449 This routine initializes the architecture dispatch table by
450 calling all installed architecture packages and getting them
451 to poke around.
452
453 SYNOPSIS
454 void bfd_arch_init(void);
455
456 */
457
458 void
459 DEFUN_VOID(bfd_arch_init)
460 {
461 void EXFUN((**ptable),());
462 for (ptable = archures_init_table;
463 *ptable ;
464 ptable++)
465 {
466 (*ptable)();
467 }
468 }
469
470
471 /*
472 INTERNAL FUNCTION
473 bfd_arch_linkin
474
475 DESCRIPTION
476 Link the provided arch info structure into the list
477
478 SYNOPSIS
479 void bfd_arch_linkin(bfd_arch_info_type *);
480
481 */
482
483 void DEFUN(bfd_arch_linkin,(ptr),
484 bfd_arch_info_type *ptr)
485 {
486 ptr->next = bfd_arch_info_list;
487 bfd_arch_info_list = ptr;
488 }
489
490
491 /*
492 INTERNAL FUNCTION
493 bfd_default_compatible
494
495 DESCRIPTION
496 The default function for testing for compatibility.
497
498 SYNOPSIS
499 CONST bfd_arch_info_type *bfd_default_compatible
500 (CONST bfd_arch_info_type *a,
501 CONST bfd_arch_info_type *b);
502 */
503
504 CONST bfd_arch_info_type *
505 DEFUN(bfd_default_compatible,(a,b),
506 CONST bfd_arch_info_type *a AND
507 CONST bfd_arch_info_type *b)
508 {
509 if(a->arch != b->arch) return NULL;
510
511 if (a->mach > b->mach) {
512 return a;
513 }
514 if (b->mach > a->mach) {
515 return b;
516 }
517 return a;
518 }
519
520
521 /*
522 INTERNAL FUNCTION
523 bfd_default_scan
524
525 DESCRIPTION
526 The default function for working out whether this is an
527 architecture hit and a machine hit.
528
529 SYNOPSIS
530 boolean bfd_default_scan(CONST struct bfd_arch_info *, CONST char *);
531
532 */
533
534 boolean
535 DEFUN(bfd_default_scan,(info, string),
536 CONST struct bfd_arch_info *info AND
537 CONST char *string)
538 {
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 {
556 if (*ptr_src != *ptr_tst) break;
557 }
558
559 /* Chewed up as much of the architecture as will match, skip any
560 colons */
561 if (*ptr_src == ':') ptr_src++;
562
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;
593
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;
604
605 case 860:
606 case 80860:
607 arch = bfd_arch_i860;
608 break;
609
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;
624 }
625
626
627
628
629 /*
630 FUNCTION
631 bfd_get_arch_info
632
633
634 SYNOPSIS
635 bfd_arch_info_type * bfd_get_arch_info(bfd *);
636
637 */
638
639 bfd_arch_info_type *
640 DEFUN(bfd_get_arch_info,(abfd),
641 bfd *abfd)
642 {
643 return abfd->arch_info;
644 }
645
646
647 /*
648 FUNCTION
649 bfd_lookup_arch
650
651 DESCRIPTION
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
658
659 SYNOPSIS
660 bfd_arch_info_type *bfd_lookup_arch
661 (enum bfd_architecture
662 arch,
663 long machine);
664
665
666 */
667
668 bfd_arch_info_type *
669 DEFUN(bfd_lookup_arch,(arch, machine),
670 enum bfd_architecture arch AND
671 long machine)
672 {
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;
685 }
686
687
688
689 /*
690 FUNCTION
691 bfd_printable_arch_mach
692
693 DESCRIPTION
694 Return a printable string representing the architecture and
695 machine type.
696
697 NB. The use of this routine is depreciated.
698
699 SYNOPSIS
700 CONST char * bfd_printable_arch_mach
701 (enum bfd_architecture arch, unsigned long machine);
702 */
703
704 CONST char *
705 DEFUN(bfd_printable_arch_mach,(arch, machine),
706 enum bfd_architecture arch AND
707 unsigned long machine)
708 {
709 bfd_arch_info_type *ap = bfd_lookup_arch(arch, machine);
710 if(ap) return ap->printable_name;
711 return "UNKNOWN!";
712 }
This page took 0.060823 seconds and 4 git commands to generate.