f8d7418a63901e2886edf058a350a3090856ba11
[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 atoms in a BFD describing the architecture of the data
26 attached to the BFD;  a pointer to a @code{bfd_arch_info_struct}. 
27
28 Pointers to structures can be requested independently of a bfd so that
29 an archictectures 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 RS/6000 *}
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_last
92   };
93 *-
94
95 stuff
96
97 */
98
99
100
101 /* $Id$ */
102
103 #include <sysdep.h>
104 #include "bfd.h"
105 #include "libbfd.h"
106
107 /*proto* bfd_arch_info_struct
108 This structure contains information on architectures.
109 *+
110 typedef int bfd_reloc_code_enum_type;
111
112 typedef struct bfd_arch_info_struct 
113 {
114   int bits_per_word;
115   int bits_per_address;
116   int bits_per_byte;
117   enum bfd_architecture arch;
118   long mach;
119   char *arch_name;
120   CONST  char *printable_name;
121 {* true if this is the default machine for the architecture *}
122   boolean the_default;  
123   CONST struct bfd_arch_info_struct * EXFUN((*compatible),(CONST struct bfd_arch_info_struct *a,
124                                                      CONST struct bfd_arch_info_struct *b));
125
126                      
127   boolean EXFUN((*scan),(CONST struct bfd_arch_info_struct *,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), (bfd_reloc_code_enum_type  code));
131
132   struct bfd_arch_info_struct *next;
133
134 } bfd_arch_info_struct_type;
135
136
137 *-
138 */
139
140 bfd_arch_info_struct_type   *bfd_arch_info_list;
141
142 /*proto* bfd_printable_arch_mach
143 Return a printable string representing the architecture and machine
144 type. 
145
146 NB. The use of this routine is depreciated.
147
148 *; PROTO(CONST char *,bfd_printable_arch_mach,
149     (enum bfd_architecture arch, unsigned long machine));
150 */
151
152 CONST char *
153 DEFUN(bfd_printable_arch_mach,(arch, machine),
154       enum bfd_architecture arch AND
155       unsigned long machine)
156 {
157   bfd_arch_info_struct_type *ap;
158   bfd_check_init();  
159   for (ap = bfd_arch_info_list; 
160        ap !=  (bfd_arch_info_struct_type *)NULL;
161        ap = ap->next) {
162     if (ap->arch == arch &&
163         ((ap->mach == machine) || (ap->the_default && machine == 0))) {
164       return ap->printable_name;
165     }
166   }
167   return "UNKNOWN!";
168 }
169
170
171 /*proto* bfd_printable_name
172
173 Return a printable string representing the architecture and machine
174 from the pointer to the arch info structure 
175
176 *; CONST char *EXFUN(bfd_printable_name,(bfd *abfd));
177
178 */
179
180 CONST char *
181 DEFUN(bfd_printable_name, (abfd),
182       bfd *abfd) 
183 {
184   return abfd->arch_info->printable_name;
185 }
186
187
188
189 /*proto*
190 *i bfd_scan_arch
191 This routine is provided with a string and tries to work out if bfd
192 supports any cpu which could be described with the name provided.  The
193 routine returns a pointer to an arch_info structure if a machine is
194 found, otherwise NULL.
195
196 *; bfd_arch_info_struct_type *EXFUN(bfd_scan_arch,(CONST char *));
197 */
198
199 bfd_arch_info_struct_type *
200 DEFUN(bfd_scan_arch,(string),
201       CONST char *string)
202 {
203   struct bfd_arch_info_struct *ap;
204
205   /* Look through all the installed architectures */
206   for (ap = bfd_arch_info_list;
207        ap != (bfd_arch_info_struct_type *)NULL;
208        ap = ap->next) {
209     /* Don't bother with anything if the first chars don't match */
210     if (ap->arch_name[0] != string[0])
211       continue;
212     if (ap->scan(ap, string)) 
213       return ap;
214   }
215   return (bfd_arch_info_struct_type *)NULL;
216 }
217
218
219
220 /*proto* bfd_arch_get_compatible
221 This routine is used to determine whether two BFDs' architectures and
222 machine types are compatible.  It calculates the lowest common
223 denominator between the two architectures and machine types implied by
224 the BFDs and returns a pointer to an arch_info structure describing
225 the compatible machine.
226
227 *; CONST bfd_arch_info_struct_type *EXFUN(bfd_arch_get_compatible,
228      (CONST bfd *abfd,
229      CONST bfd *bbfd));
230 */
231
232 CONST bfd_arch_info_struct_type *
233 DEFUN(bfd_arch_get_compatible,(abfd, bbfd),
234 CONST    bfd *abfd AND
235 CONST    bfd *bbfd)
236
237 {
238   return  abfd->arch_info->compatible(abfd->arch_info,bbfd->arch_info);
239 }
240
241
242 /*proto-internal* bfd_default_arch_struct
243
244 What bfds are seeded with 
245
246 *+
247 extern bfd_arch_info_struct_type bfd_default_arch_struct;
248 *-
249 */
250
251 bfd_arch_info_struct_type bfd_default_arch_struct =
252   {
253     32,32,8,bfd_arch_unknown,0,"unknown","unknown",true,
254     bfd_default_compatible, bfd_default_scan,
255
256   };
257
258 /*proto* bfd_set_arch_info
259
260 *; void EXFUN(bfd_set_arch_info,(bfd *, bfd_arch_info_struct_type *));
261
262 */
263
264 void DEFUN(bfd_set_arch_info,(abfd, arg),
265 bfd *abfd AND
266 bfd_arch_info_struct_type *arg)
267 {
268   abfd->arch_info = arg;
269 }
270
271 /*proto-internal* bfd_default_set_arch_mach
272
273 Set the architecture and machine type in a bfd. This finds the correct
274 pointer to structure and inserts it into the arch_info pointer. 
275
276 *;  boolean EXFUN(bfd_default_set_arch_mach,(bfd *abfd,
277           enum bfd_architecture arch,
278          unsigned long mach));
279
280 */
281
282 boolean DEFUN(bfd_default_set_arch_mach,(abfd, arch, mach),
283               bfd *abfd AND
284               enum bfd_architecture arch AND
285               unsigned    long mach)
286 {
287   static struct bfd_arch_info_struct *old_ptr = &bfd_default_arch_struct;
288   boolean found = false;
289   /* run through the table to find the one we want, we keep a little
290      cache to speed things up */
291   if (old_ptr == 0 || arch != old_ptr->arch || mach != old_ptr->mach) {
292     bfd_arch_info_struct_type *ptr;
293     old_ptr = (bfd_arch_info_struct_type *)NULL;
294     for (ptr = bfd_arch_info_list;
295          ptr != (bfd_arch_info_struct_type *)NULL;
296          ptr= ptr->next) {
297       if (ptr->arch == arch &&
298           ((ptr->mach == mach) || (ptr->the_default && mach == 0))) {
299         old_ptr = ptr;
300         found = true;
301         break;
302       }
303     }
304     if (found==false) {
305       /*looked for it and it wasn't there, so put in the default */
306       old_ptr = &bfd_default_arch_struct;
307
308     }
309   }
310   else {
311     /* it was in the cache */
312     found = true;
313   }
314
315   abfd->arch_info = old_ptr;
316
317   return found;
318 }
319
320
321
322
323
324 /*proto* bfd_get_arch
325
326 Returns the enumerated type which describes the supplied bfd's
327 architecture
328
329 *; enum bfd_architecture EXFUN(bfd_get_arch, (bfd *abfd));
330 */
331
332  enum bfd_architecture DEFUN(bfd_get_arch, (abfd), bfd *abfd)
333   {
334     return abfd->arch_info->arch;
335
336
337   }
338
339 /*proto* bfd_get_mach
340
341 Returns the long type which describes the supplied bfd's
342 machine
343
344 *; unsigned long EXFUN(bfd_get_mach, (bfd *abfd));
345 */
346
347 unsigned long  DEFUN(bfd_get_mach, (abfd), bfd *abfd)
348 {
349     return abfd->arch_info->mach;
350   }
351
352 /*proto* bfd_arch_bits_per_byte
353
354 Returns the number of bits in one of the architectures bytes
355
356 *; unsigned int EXFUN(bfd_arch_bits_per_byte, (bfd *abfd));
357 */
358
359 unsigned int DEFUN(bfd_arch_bits_per_byte, (abfd), bfd *abfd)
360   {
361     return abfd->arch_info->bits_per_byte;
362   }
363
364 /*proto* bfd_arch_bits_per_address
365
366 Returns the number of bits in one of the architectures addresses
367
368 *; unsigned int EXFUN(bfd_arch_bits_per_address, (bfd *abfd));
369 */
370
371 unsigned int DEFUN(bfd_arch_bits_per_address, (abfd), bfd *abfd)
372   {
373     return abfd->arch_info->bits_per_address;
374   }
375
376
377
378 extern void EXFUN(bfd_h8300_arch,(void));
379 extern void EXFUN(bfd_i960_arch,(void));
380 extern void EXFUN(bfd_empty_arch,(void));
381 extern void EXFUN(bfd_sparc_arch,(void));
382 extern void EXFUN(bfd_m88k_arch,(void));
383 extern void EXFUN(bfd_m68k_arch,(void));
384 extern void EXFUN(bfd_vax_arch,(void));
385 extern void EXFUN(bfd_a29k_arch,(void));
386 extern void EXFUN(bfd_mips_arch,(void));
387 extern void EXFUN(bfd_i386_arch,(void));
388
389
390
391 static void EXFUN((*archures_init_table[]),()) = 
392 {
393 #ifdef SELECT_ARCHITECTURES
394   SELECT_ARCHITECTURES,
395 #else
396   bfd_sparc_arch,
397   bfd_a29k_arch,
398   bfd_mips_arch,
399   bfd_h8300_arch,
400   bfd_i386_arch,
401   bfd_m88k_arch,
402   bfd_i960_arch,
403   bfd_m68k_arch,
404   bfd_vax_arch,
405 #endif
406   0
407   };
408
409
410
411 /*proto-internal*
412
413 This routine initializes the architecture dispatch table by calling
414 all installed architecture packages and getting them to poke around.
415
416 *; PROTO(void, bfd_arch_init,(void));
417
418 */
419
420 void
421 DEFUN_VOID(bfd_arch_init)
422 {
423   void EXFUN((**ptable),());
424   for (ptable = archures_init_table; 
425        *ptable ;
426        ptable++)
427       {
428         (*ptable)();
429       }
430 }
431
432
433 /*proto-internal* bfd_arch_linkin
434
435 Link the provided arch info structure into the list
436
437 *; void EXFUN(bfd_arch_linkin,(bfd_arch_info_struct_type *));
438
439 */
440
441 void DEFUN(bfd_arch_linkin,(ptr),
442            bfd_arch_info_struct_type *ptr)
443 {
444   ptr->next = bfd_arch_info_list;
445   bfd_arch_info_list = ptr;
446 }
447
448
449 /*proto-internal* bfd_default_compatible
450
451 The default function for testing for compatibility 
452
453 *; CONST bfd_arch_info_struct_type *EXFUN(bfd_default_compatible,
454      (CONST bfd_arch_info_struct_type *a,
455      CONST bfd_arch_info_struct_type *b));
456 */
457
458 CONST bfd_arch_info_struct_type *
459 DEFUN(bfd_default_compatible,(a,b),
460       CONST bfd_arch_info_struct_type *a AND
461       CONST bfd_arch_info_struct_type *b)
462 {
463   if(a->arch != b->arch) return (bfd_arch_info_struct_type *)NULL;
464
465   if (a->mach > b->mach) {
466     return a;
467   }
468   if (b->mach > a->mach) {
469     return b;
470   }
471   return a;
472 }
473
474 /*proto-internal* bfd_default_scan
475 The default function for working out whether this is an architecture
476 hit and a machine hit 
477
478 *; boolean EXFUN(bfd_default_scan,(CONST struct bfd_arch_info_struct *, CONST char *));
479
480 */
481
482 boolean 
483 DEFUN(bfd_default_scan,(info, string),
484 CONST struct bfd_arch_info_struct *info AND
485 CONST char *string)
486 {
487   CONST  char *ptr_src;
488   CONST   char *ptr_tst;
489   unsigned long number;
490   enum bfd_architecture arch;
491   /* First test for an exact match */
492   if (strcmp(string, info->printable_name) == 0) return true;
493
494   /* See how much of the supplied string matches with the
495      architecture, eg the string m68k:68020 would match the 68k entry
496      up to the :, then we get left with the machine number */
497
498   for (ptr_src = string,
499        ptr_tst = info->arch_name; 
500        *ptr_src && *ptr_tst;
501        ptr_src++,
502        ptr_tst++) 
503       {
504         if (*ptr_src != *ptr_tst) break;
505       }
506
507   /* Chewed up as much of the architecture as will match, skip any
508      colons */
509   if (*ptr_src == ':') ptr_src++;
510   
511   if (*ptr_src == 0) {
512     /* nothing more, then only keep this one if it is the default
513        machine for this architecture */
514     return info->the_default;
515   }
516   number = 0;
517   while (isdigit(*ptr_src)) {
518     number = number * 10 + *ptr_src  - '0';
519     ptr_src++;
520   }
521
522   switch (number) {
523   case 68010:
524   case 68020:
525   case 68030:
526   case 68040:
527   case 68332:
528   case 68050:        
529   case 68000: 
530     arch = bfd_arch_m68k; 
531     break;
532   case 386: 
533   case 80386:
534   case 486:
535     arch = bfd_arch_i386;
536     break;
537   case 29000: 
538     arch = bfd_arch_a29k;
539     break;
540
541   case 32016:
542   case 32032:
543   case 32132:
544   case 32232:
545   case 32332:
546   case 32432:
547   case 32532:  
548   case 32000: 
549     arch = bfd_arch_ns32k; 
550     break;
551
552   case 860:
553   case 80860: 
554     arch = bfd_arch_i860; 
555     break;
556
557   default:  
558     return false;
559   }
560   if (arch != info->arch) 
561     return false;
562
563   if (number != info->mach)
564     return false;
565
566   return true;
567 }
568
569
570
571
572 /*proto* bfd_get_arch_info
573
574 *; bfd_arch_info_struct_type * EXFUN(bfd_get_arch_info,(bfd *));
575
576 */
577
578 bfd_arch_info_struct_type *
579 DEFUN(bfd_get_arch_info,(abfd),
580 bfd *abfd)
581 {
582   return  abfd->arch_info;
583 }