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