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