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