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