xref: /openbsd/gnu/usr.bin/binutils/bfd/archures.c (revision 133306f0)
1 /* BFD library support routines for architectures.
2    Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999, 2000
3    Free Software Foundation, Inc.
4    Hacked by John Gilmore and Steve Chamberlain of Cygnus Support.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21 
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include <ctype.h>
26 
27 /*
28 
29 SECTION
30 	Architectures
31 
32 	BFD keeps one atom in a BFD describing the
33 	architecture of the data attached to the BFD: a pointer to a
34 	<<bfd_arch_info_type>>.
35 
36 	Pointers to structures can be requested independently of a BFD
37 	so that an architecture's information can be interrogated
38 	without access to an open BFD.
39 
40 	The architecture information is provided by each architecture package.
41 	The set of default architectures is selected by the macro
42 	<<SELECT_ARCHITECTURES>>.  This is normally set up in the
43 	@file{config/@var{target}.mt} file of your choice.  If the name is not
44 	defined, then all the architectures supported are included.
45 
46 	When BFD starts up, all the architectures are called with an
47 	initialize method.  It is up to the architecture back end to
48 	insert as many items into the list of architectures as it wants to;
49 	generally this would be one for each machine and one for the
50 	default case (an item with a machine field of 0).
51 
52 	BFD's idea of an architecture is implemented in	@file{archures.c}.
53 */
54 
55 /*
56 
57 SUBSECTION
58 	bfd_architecture
59 
60 DESCRIPTION
61 	This enum gives the object file's CPU architecture, in a
62 	global sense---i.e., what processor family does it belong to?
63 	Another field indicates which processor within
64 	the family is in use.  The machine gives a number which
65 	distinguishes different versions of the architecture,
66 	containing, for example, 2 and 3 for Intel i960 KA and i960 KB,
67 	and 68020 and 68030 for Motorola 68020 and 68030.
68 
69 .enum bfd_architecture
70 .{
71 .  bfd_arch_unknown,   {* File arch not known *}
72 .  bfd_arch_obscure,   {* Arch known, not one of these *}
73 .  bfd_arch_m68k,      {* Motorola 68xxx *}
74 .#define bfd_mach_m68000 1
75 .#define bfd_mach_m68008 2
76 .#define bfd_mach_m68010 3
77 .#define bfd_mach_m68020 4
78 .#define bfd_mach_m68030 5
79 .#define bfd_mach_m68040 6
80 .#define bfd_mach_m68060 7
81 .#define bfd_mach_cpu32  8
82 .  bfd_arch_vax,       {* DEC Vax *}
83 .  bfd_arch_i960,      {* Intel 960 *}
84 .    {* The order of the following is important.
85 .       lower number indicates a machine type that
86 .       only accepts a subset of the instructions
87 .       available to machines with higher numbers.
88 .       The exception is the "ca", which is
89 .       incompatible with all other machines except
90 .       "core". *}
91 .
92 .#define bfd_mach_i960_core      1
93 .#define bfd_mach_i960_ka_sa     2
94 .#define bfd_mach_i960_kb_sb     3
95 .#define bfd_mach_i960_mc        4
96 .#define bfd_mach_i960_xa        5
97 .#define bfd_mach_i960_ca        6
98 .#define bfd_mach_i960_jx	 7
99 .#define bfd_mach_i960_hx        8
100 .
101 .  bfd_arch_a29k,      {* AMD 29000 *}
102 .  bfd_arch_sparc,     {* SPARC *}
103 .#define bfd_mach_sparc			1
104 .{* The difference between v8plus and v9 is that v9 is a true 64 bit env.  *}
105 .#define bfd_mach_sparc_sparclet	2
106 .#define bfd_mach_sparc_sparclite	3
107 .#define bfd_mach_sparc_v8plus		4
108 .#define bfd_mach_sparc_v8plusa		5 {* with ultrasparc add'ns *}
109 .#define bfd_mach_sparc_sparclite_le	6
110 .#define bfd_mach_sparc_v9		7
111 .#define bfd_mach_sparc_v9a		8 {* with ultrasparc add'ns *}
112 .{* Nonzero if MACH has the v9 instruction set.  *}
113 .#define bfd_mach_sparc_v9_p(mach) \
114 .  ((mach) >= bfd_mach_sparc_v8plus && (mach) <= bfd_mach_sparc_v9a)
115 .  bfd_arch_mips,      {* MIPS Rxxxx *}
116 .#define bfd_mach_mips3000		3000
117 .#define bfd_mach_mips3900		3900
118 .#define bfd_mach_mips4000		4000
119 .#define bfd_mach_mips4010		4010
120 .#define bfd_mach_mips4100		4100
121 .#define bfd_mach_mips4111		4111
122 .#define bfd_mach_mips4300		4300
123 .#define bfd_mach_mips4400		4400
124 .#define bfd_mach_mips4600		4600
125 .#define bfd_mach_mips4650		4650
126 .#define bfd_mach_mips5000		5000
127 .#define bfd_mach_mips6000		6000
128 .#define bfd_mach_mips8000		8000
129 .#define bfd_mach_mips10000		10000
130 .#define bfd_mach_mips16		16
131 .  bfd_arch_i386,      {* Intel 386 *}
132 .#define bfd_mach_i386_i386 0
133 .#define bfd_mach_i386_i8086 1
134 .#define bfd_mach_i386_i386_intel_syntax 2
135 .  bfd_arch_we32k,     {* AT&T WE32xxx *}
136 .  bfd_arch_tahoe,     {* CCI/Harris Tahoe *}
137 .  bfd_arch_i860,      {* Intel 860 *}
138 .  bfd_arch_i370,      {* IBM 360/370 Mainframes *}
139 .  bfd_arch_romp,      {* IBM ROMP PC/RT *}
140 .  bfd_arch_alliant,   {* Alliant *}
141 .  bfd_arch_convex,    {* Convex *}
142 .  bfd_arch_m88k,      {* Motorola 88xxx *}
143 .  bfd_arch_pyramid,   {* Pyramid Technology *}
144 .  bfd_arch_h8300,     {* Hitachi H8/300 *}
145 .#define bfd_mach_h8300   1
146 .#define bfd_mach_h8300h  2
147 .#define bfd_mach_h8300s  3
148 .  bfd_arch_powerpc,   {* PowerPC *}
149 .  bfd_arch_rs6000,    {* IBM RS/6000 *}
150 .  bfd_arch_hppa,      {* HP PA RISC *}
151 .  bfd_arch_d10v,      {* Mitsubishi D10V *}
152 .#define bfd_mach_d10v		0
153 .#define bfd_mach_d10v_ts2	2
154 .#define bfd_mach_d10v_ts3	3
155 .  bfd_arch_d30v,      {* Mitsubishi D30V *}
156 .  bfd_arch_z8k,       {* Zilog Z8000 *}
157 .#define bfd_mach_z8001		1
158 .#define bfd_mach_z8002		2
159 .  bfd_arch_h8500,     {* Hitachi H8/500 *}
160 .  bfd_arch_sh,        {* Hitachi SH *}
161 .#define bfd_mach_sh            0
162 .#define bfd_mach_sh2        0x20
163 .#define bfd_mach_sh_dsp     0x2d
164 .#define bfd_mach_sh3        0x30
165 .#define bfd_mach_sh3_dsp    0x3d
166 .#define bfd_mach_sh3e       0x3e
167 .#define bfd_mach_sh4        0x40
168 .  bfd_arch_alpha,     {* Dec Alpha *}
169 .#define bfd_mach_alpha_ev4  0x10
170 .#define bfd_mach_alpha_ev5  0x20
171 .#define bfd_mach_alpha_ev6  0x30
172 .  bfd_arch_arm,       {* Advanced Risc Machines ARM *}
173 .#define bfd_mach_arm_2		1
174 .#define bfd_mach_arm_2a	2
175 .#define bfd_mach_arm_3		3
176 .#define bfd_mach_arm_3M 	4
177 .#define bfd_mach_arm_4 	5
178 .#define bfd_mach_arm_4T 	6
179 .#define bfd_mach_arm_5 	7
180 .#define bfd_mach_arm_5T	8
181 .  bfd_arch_ns32k,     {* National Semiconductors ns32000 *}
182 .  bfd_arch_w65,       {* WDC 65816 *}
183 .  bfd_arch_tic30,     {* Texas Instruments TMS320C30 *}
184 .  bfd_arch_tic80,     {* TI TMS320c80 (MVP) *}
185 .  bfd_arch_v850,      {* NEC V850 *}
186 .#define bfd_mach_v850          0
187 .#define bfd_mach_v850e 	'E'
188 .#define bfd_mach_v850ea	'A'
189 .  bfd_arch_arc,       {* Argonaut RISC Core *}
190 .#define bfd_mach_arc_base 0
191 .  bfd_arch_m32r,      {* Mitsubishi M32R/D *}
192 .#define bfd_mach_m32r		0 {* backwards compatibility *}
193 .#define bfd_mach_m32rx		'x'
194 .  bfd_arch_mn10200,   {* Matsushita MN10200 *}
195 .  bfd_arch_mn10300,   {* Matsushita MN10300 *}
196 .#define bfd_mach_mn10300		300
197 .#define bfd_mach_am33		330
198 .  bfd_arch_fr30,
199 .#define bfd_mach_fr30		0x46523330
200 .  bfd_arch_mcore,
201 .  bfd_arch_pj,
202 .  bfd_arch_avr,       {* Atmel AVR microcontrollers *}
203 .#define bfd_mach_avr1		1
204 .#define bfd_mach_avr2		2
205 .#define bfd_mach_avr3		3
206 .#define bfd_mach_avr4		4
207 .  bfd_arch_last
208 .  };
209 
210 
211 */
212 
213 /*
214 
215 SUBSECTION
216 	bfd_arch_info
217 
218 DESCRIPTION
219 	This structure contains information on architectures for use
220 	within BFD.
221 
222 .
223 .typedef struct bfd_arch_info
224 .{
225 .  int bits_per_word;
226 .  int bits_per_address;
227 .  int bits_per_byte;
228 .  enum bfd_architecture arch;
229 .  unsigned long mach;
230 .  const char *arch_name;
231 .  const char *printable_name;
232 .  unsigned int section_align_power;
233 . {* true if this is the default machine for the architecture *}
234 .  boolean the_default;
235 .  const struct bfd_arch_info * (*compatible)
236 .	PARAMS ((const struct bfd_arch_info *a,
237 .	         const struct bfd_arch_info *b));
238 .
239 .  boolean (*scan) PARAMS ((const struct bfd_arch_info *, const char *));
240 .
241 .  const struct bfd_arch_info *next;
242 .} bfd_arch_info_type;
243 */
244 
245 extern const bfd_arch_info_type bfd_a29k_arch;
246 extern const bfd_arch_info_type bfd_alpha_arch;
247 extern const bfd_arch_info_type bfd_arc_arch;
248 extern const bfd_arch_info_type bfd_arm_arch;
249 extern const bfd_arch_info_type bfd_d10v_arch;
250 extern const bfd_arch_info_type bfd_d30v_arch;
251 extern const bfd_arch_info_type bfd_h8300_arch;
252 extern const bfd_arch_info_type bfd_h8500_arch;
253 extern const bfd_arch_info_type bfd_hppa_arch;
254 extern const bfd_arch_info_type bfd_i370_arch;
255 extern const bfd_arch_info_type bfd_i386_arch;
256 extern const bfd_arch_info_type bfd_i860_arch;
257 extern const bfd_arch_info_type bfd_i960_arch;
258 extern const bfd_arch_info_type bfd_m32r_arch;
259 extern const bfd_arch_info_type bfd_m68k_arch;
260 extern const bfd_arch_info_type bfd_m88k_arch;
261 extern const bfd_arch_info_type bfd_mips_arch;
262 extern const bfd_arch_info_type bfd_mn10200_arch;
263 extern const bfd_arch_info_type bfd_mn10300_arch;
264 extern const bfd_arch_info_type bfd_powerpc_arch;
265 extern const bfd_arch_info_type bfd_rs6000_arch;
266 extern const bfd_arch_info_type bfd_pj_arch;
267 extern const bfd_arch_info_type bfd_sh_arch;
268 extern const bfd_arch_info_type bfd_sparc_arch;
269 extern const bfd_arch_info_type bfd_tic30_arch;
270 extern const bfd_arch_info_type bfd_tic80_arch;
271 extern const bfd_arch_info_type bfd_vax_arch;
272 extern const bfd_arch_info_type bfd_we32k_arch;
273 extern const bfd_arch_info_type bfd_z8k_arch;
274 extern const bfd_arch_info_type bfd_ns32k_arch;
275 extern const bfd_arch_info_type bfd_w65_arch;
276 extern const bfd_arch_info_type bfd_v850_arch;
277 extern const bfd_arch_info_type bfd_fr30_arch;
278 extern const bfd_arch_info_type bfd_mcore_arch;
279 extern const bfd_arch_info_type bfd_avr_arch;
280 
281 static const bfd_arch_info_type * const bfd_archures_list[] =
282 {
283 #ifdef SELECT_ARCHITECTURES
284   SELECT_ARCHITECTURES,
285 #else
286   &bfd_a29k_arch,
287   &bfd_alpha_arch,
288   &bfd_arc_arch,
289   &bfd_arm_arch,
290   &bfd_d10v_arch,
291   &bfd_d30v_arch,
292   &bfd_h8300_arch,
293   &bfd_h8500_arch,
294   &bfd_hppa_arch,
295   &bfd_i370_arch,
296   &bfd_i386_arch,
297   &bfd_i860_arch,
298   &bfd_i960_arch,
299   &bfd_m32r_arch,
300   &bfd_m68k_arch,
301   &bfd_m88k_arch,
302   &bfd_mips_arch,
303   &bfd_mn10200_arch,
304   &bfd_mn10300_arch,
305   &bfd_powerpc_arch,
306   &bfd_rs6000_arch,
307   &bfd_sh_arch,
308   &bfd_sparc_arch,
309   &bfd_tic30_arch,
310   &bfd_tic80_arch,
311   &bfd_vax_arch,
312   &bfd_we32k_arch,
313   &bfd_z8k_arch,
314   &bfd_ns32k_arch,
315   &bfd_w65_arch,
316   &bfd_v850_arch,
317   &bfd_fr30_arch,
318   &bfd_mcore_arch,
319   &bfd_avr_arch,
320 #endif
321   0
322 };
323 
324 /*
325 FUNCTION
326 	bfd_printable_name
327 
328 SYNOPSIS
329 	const char *bfd_printable_name(bfd *abfd);
330 
331 DESCRIPTION
332 	Return a printable string representing the architecture and machine
333 	from the pointer to the architecture info structure.
334 
335 */
336 
337 const char *
338 bfd_printable_name (abfd)
339      bfd *abfd;
340 {
341   return abfd->arch_info->printable_name;
342 }
343 
344 
345 
346 /*
347 FUNCTION
348 	bfd_scan_arch
349 
350 SYNOPSIS
351 	const bfd_arch_info_type *bfd_scan_arch(const char *string);
352 
353 DESCRIPTION
354 	Figure out if BFD supports any cpu which could be described with
355 	the name @var{string}.  Return a pointer to an <<arch_info>>
356 	structure if a machine is found, otherwise NULL.
357 
358 */
359 
360 const bfd_arch_info_type *
361 bfd_scan_arch (string)
362      const char *string;
363 {
364   const bfd_arch_info_type * const *app, *ap;
365 
366   /* Look through all the installed architectures */
367   for (app = bfd_archures_list; *app != NULL; app++)
368     {
369       for (ap = *app; ap != NULL; ap = ap->next)
370 	{
371 	  if (ap->scan (ap, string))
372 	    return ap;
373 	}
374     }
375 
376   return NULL;
377 }
378 
379 
380 
381 /*
382 FUNCTION
383 	bfd_arch_list
384 
385 SYNOPSIS
386 	const char **bfd_arch_list(void);
387 
388 DESCRIPTION
389 	Return a freshly malloced NULL-terminated vector of the names
390 	of all the valid BFD architectures.  Do not modify the names.
391 
392 */
393 
394 const char **
395 bfd_arch_list ()
396 {
397   int vec_length = 0;
398   const char **name_ptr;
399   const char **name_list;
400   const bfd_arch_info_type * const *app;
401 
402   /* Determine the number of architectures */
403   vec_length = 0;
404   for (app = bfd_archures_list; *app != NULL; app++)
405     {
406       const bfd_arch_info_type *ap;
407       for (ap = *app; ap != NULL; ap = ap->next)
408 	{
409 	  vec_length++;
410 	}
411     }
412 
413   name_list = (CONST char **)
414     bfd_malloc ((vec_length + 1) * sizeof (char **));
415   if (name_list == NULL)
416     return NULL;
417 
418   /* Point the list at each of the names */
419   name_ptr = name_list;
420   for (app = bfd_archures_list; *app != NULL; app++)
421     {
422       const bfd_arch_info_type *ap;
423       for (ap = *app; ap != NULL; ap = ap->next)
424 	{
425 	  *name_ptr = ap->printable_name;
426 	  name_ptr++;
427 	}
428     }
429   *name_ptr = NULL;
430 
431   return name_list;
432 }
433 
434 
435 
436 /*
437 FUNCTION
438 	bfd_arch_get_compatible
439 
440 SYNOPSIS
441 	const bfd_arch_info_type *bfd_arch_get_compatible(
442 		const bfd *abfd,
443 	        const bfd *bbfd);
444 
445 DESCRIPTION
446 	Determine whether two BFDs'
447 	architectures and machine types are compatible.  Calculates
448 	the lowest common denominator between the two architectures
449 	and machine types implied by the BFDs and returns a pointer to
450 	an <<arch_info>> structure describing the compatible machine.
451 */
452 
453 const bfd_arch_info_type *
454 bfd_arch_get_compatible (abfd, bbfd)
455      const bfd *abfd;
456      const bfd *bbfd;
457 {
458   /* If either architecture is unknown, then all we can do is assume
459      the user knows what he's doing.  */
460   if (abfd->arch_info->arch == bfd_arch_unknown)
461   	return bbfd->arch_info;
462   if (bbfd->arch_info->arch == bfd_arch_unknown)
463   	return abfd->arch_info;
464 
465   /* Otherwise architecture-specific code has to decide.  */
466   return abfd->arch_info->compatible (abfd->arch_info, bbfd->arch_info);
467 }
468 
469 
470 /*
471 INTERNAL_DEFINITION
472 	bfd_default_arch_struct
473 
474 DESCRIPTION
475 	The <<bfd_default_arch_struct>> is an item of
476 	<<bfd_arch_info_type>> which has been initialized to a fairly
477 	generic state.  A BFD starts life by pointing to this
478 	structure, until the correct back end has determined the real
479 	architecture of the file.
480 
481 .extern const bfd_arch_info_type bfd_default_arch_struct;
482 
483 */
484 
485 const bfd_arch_info_type bfd_default_arch_struct =
486 {
487     32,32,8,bfd_arch_unknown,0,"unknown","unknown",2,true,
488     bfd_default_compatible,
489     bfd_default_scan,
490     0,
491 };
492 
493 /*
494 FUNCTION
495 	bfd_set_arch_info
496 
497 SYNOPSIS
498 	void bfd_set_arch_info(bfd *abfd, const bfd_arch_info_type *arg);
499 
500 DESCRIPTION
501 	Set the architecture info of @var{abfd} to @var{arg}.
502 */
503 
504 void
505 bfd_set_arch_info (abfd, arg)
506      bfd *abfd;
507      const bfd_arch_info_type *arg;
508 {
509   abfd->arch_info = arg;
510 }
511 
512 /*
513 INTERNAL_FUNCTION
514 	bfd_default_set_arch_mach
515 
516 SYNOPSIS
517 	boolean bfd_default_set_arch_mach(bfd *abfd,
518 		enum bfd_architecture arch,
519 		unsigned long mach);
520 
521 DESCRIPTION
522 	Set the architecture and machine type in BFD @var{abfd}
523 	to @var{arch} and @var{mach}.  Find the correct
524 	pointer to a structure and insert it into the <<arch_info>>
525 	pointer.
526 */
527 
528 boolean
529 bfd_default_set_arch_mach (abfd, arch, mach)
530      bfd *abfd;
531      enum bfd_architecture arch;
532      unsigned long mach;
533 {
534   const bfd_arch_info_type * const *app, *ap;
535 
536   for (app = bfd_archures_list; *app != NULL; app++)
537     {
538       for (ap = *app; ap != NULL; ap = ap->next)
539 	{
540 	  if (ap->arch == arch
541 	      && (ap->mach == mach
542 		  || (mach == 0 && ap->the_default)))
543 	    {
544 	      abfd->arch_info = ap;
545 	      return true;
546 	    }
547 	}
548     }
549 
550   abfd->arch_info = &bfd_default_arch_struct;
551   bfd_set_error (bfd_error_bad_value);
552   return false;
553 }
554 
555 
556 /*
557 FUNCTION
558 	bfd_get_arch
559 
560 SYNOPSIS
561 	enum bfd_architecture bfd_get_arch(bfd *abfd);
562 
563 DESCRIPTION
564 	Return the enumerated type which describes the BFD @var{abfd}'s
565 	architecture.
566 
567 */
568 
569 enum bfd_architecture
570 bfd_get_arch (abfd)
571      bfd *abfd;
572 {
573     return abfd->arch_info->arch;
574 }
575 
576 /*
577 FUNCTION
578 	bfd_get_mach
579 
580 SYNOPSIS
581 	unsigned long bfd_get_mach(bfd *abfd);
582 
583 DESCRIPTION
584 	Return the long type which describes the BFD @var{abfd}'s
585 	machine.
586 */
587 
588 unsigned long
589 bfd_get_mach (abfd)
590      bfd *abfd;
591 {
592     return abfd->arch_info->mach;
593 }
594 
595 /*
596 FUNCTION
597 	bfd_arch_bits_per_byte
598 
599 SYNOPSIS
600 	unsigned int bfd_arch_bits_per_byte(bfd *abfd);
601 
602 DESCRIPTION
603 	Return the number of bits in one of the BFD @var{abfd}'s
604 	architecture's bytes.
605 
606 */
607 
608 unsigned int
609 bfd_arch_bits_per_byte (abfd)
610      bfd *abfd;
611 {
612   return abfd->arch_info->bits_per_byte;
613 }
614 
615 /*
616 FUNCTION
617 	bfd_arch_bits_per_address
618 
619 SYNOPSIS
620 	unsigned int bfd_arch_bits_per_address(bfd *abfd);
621 
622 DESCRIPTION
623 	Return the number of bits in one of the BFD @var{abfd}'s
624 	architecture's addresses.
625 */
626 
627 unsigned int
628 bfd_arch_bits_per_address (abfd)
629      bfd *abfd;
630 {
631   return abfd->arch_info->bits_per_address;
632 }
633 
634 
635 /*
636 INTERNAL_FUNCTION
637 	bfd_default_compatible
638 
639 SYNOPSIS
640 	const bfd_arch_info_type *bfd_default_compatible
641 	(const bfd_arch_info_type *a,
642 	const bfd_arch_info_type *b);
643 
644 DESCRIPTION
645 	The default function for testing for compatibility.
646 */
647 
648 const bfd_arch_info_type *
649 bfd_default_compatible (a,b)
650      const bfd_arch_info_type *a;
651      const bfd_arch_info_type *b;
652 {
653   if (a->arch != b->arch)
654     return NULL;
655 
656   if (a->mach > b->mach)
657     return a;
658 
659   if (b->mach > a->mach)
660     return b;
661 
662   return a;
663 }
664 
665 
666 /*
667 INTERNAL_FUNCTION
668 	bfd_default_scan
669 
670 SYNOPSIS
671 	boolean bfd_default_scan(const struct bfd_arch_info *info, const char *string);
672 
673 DESCRIPTION
674 	The default function for working out whether this is an
675 	architecture hit and a machine hit.
676 */
677 
678 boolean
679 bfd_default_scan (info, string)
680      const struct bfd_arch_info *info;
681      const char *string;
682 {
683   const char *ptr_src;
684   const char *ptr_tst;
685   unsigned long number;
686   enum bfd_architecture arch;
687   const char *printable_name_colon;
688 
689   /* Exact match of the architecture name (ARCH_NAME) and also the
690      default architecture? */
691   if (strcasecmp (string, info->arch_name) == 0
692       && info->the_default)
693     return true;
694 
695   /* Exact match of the machine name (PRINTABLE_NAME)? */
696   if (strcasecmp (string, info->printable_name) == 0)
697     return true;
698 
699   /* Given that printable_name contains no colon, attempt to match:
700      ARCH_NAME [ ":" ] PRINTABLE_NAME? */
701   printable_name_colon = strchr (info->printable_name, ':');
702   if (printable_name_colon == NULL)
703     {
704       int strlen_arch_name = strlen (info->arch_name);
705       if (strncasecmp (string, info->arch_name, strlen_arch_name) == 0)
706 	{
707 	  if (string[strlen_arch_name] == ':')
708 	    {
709 	      if (strcasecmp (string + strlen_arch_name + 1,
710 			      info->printable_name) == 0)
711 		return true;
712 	    }
713 	  else
714 	    {
715 	      if (strcasecmp (string + strlen_arch_name,
716 			      info->printable_name) == 0)
717 		return true;
718 	    }
719 	}
720     }
721 
722   /* Given that PRINTABLE_NAME has the form: <arch> ":" <mach>;
723      Attempt to match: <arch> <mach>? */
724   if (printable_name_colon != NULL)
725     {
726       int colon_index = printable_name_colon - info->printable_name;
727       if (strncasecmp (string, info->printable_name, colon_index) == 0
728 	  && strcasecmp (string + colon_index,
729 			 info->printable_name + colon_index + 1) == 0)
730 	return true;
731     }
732 
733   /* Given that PRINTABLE_NAME has the form: <arch> ":" <mach>; Do not
734      attempt to match just <mach>, it could be ambigious.  This test
735      is left until later. */
736 
737   /* NOTE: The below is retained for compatibility only. Please do not
738      add to this code */
739 
740   /* See how much of the supplied string matches with the
741      architecture, eg the string m68k:68020 would match the 68k entry
742      up to the :, then we get left with the machine number */
743 
744   for (ptr_src = string, ptr_tst = info->arch_name;
745        *ptr_src && *ptr_tst;
746        ptr_src++, ptr_tst++)
747     {
748       if (*ptr_src != *ptr_tst) break;
749     }
750 
751   /* Chewed up as much of the architecture as will match, skip any
752      colons */
753   if (*ptr_src == ':')
754     ptr_src++;
755 
756   if (*ptr_src == 0)
757     {
758       /* nothing more, then only keep this one if it is the default
759 	 machine for this architecture */
760       return info->the_default;
761     }
762 
763   number = 0;
764   while (isdigit ((unsigned char) *ptr_src))
765     {
766       number = number * 10 + *ptr_src  - '0';
767       ptr_src++;
768     }
769 
770   /* NOTE: The below is retained for compatibility only.
771      PLEASE DO NOT ADD TO THIS CODE. */
772 
773   switch (number)
774     {
775       /* FIXME: These are needed to parse IEEE objects.  */
776     case 68000:
777       arch = bfd_arch_m68k;
778       number = bfd_mach_m68000;
779       break;
780     case 68010:
781       arch = bfd_arch_m68k;
782       number = bfd_mach_m68010;
783       break;
784     case 68020:
785       arch = bfd_arch_m68k;
786       number = bfd_mach_m68020;
787       break;
788     case 68030:
789       arch = bfd_arch_m68k;
790       number = bfd_mach_m68030;
791       break;
792     case 68040:
793       arch = bfd_arch_m68k;
794       number = bfd_mach_m68040;
795       break;
796     case 68060:
797       arch = bfd_arch_m68k;
798       number = bfd_mach_m68060;
799       break;
800     case 68332:
801       arch = bfd_arch_m68k;
802       number = bfd_mach_cpu32;
803       break;
804 
805     case 32000:
806       arch = bfd_arch_we32k;
807       break;
808 
809     case 3000:
810       arch = bfd_arch_mips;
811       number = bfd_mach_mips3000;
812       break;
813 
814     case 4000:
815       arch = bfd_arch_mips;
816       number = bfd_mach_mips4000;
817       break;
818 
819     case 6000:
820       arch = bfd_arch_rs6000;
821       break;
822 
823     case 7410:
824       arch = bfd_arch_sh;
825       number = bfd_mach_sh_dsp;
826       break;
827 
828     case 7708:
829       arch = bfd_arch_sh;
830       number = bfd_mach_sh3;
831       break;
832 
833     case 7729:
834       arch = bfd_arch_sh;
835       number = bfd_mach_sh3_dsp;
836       break;
837 
838     case 7750:
839       arch = bfd_arch_sh;
840       number = bfd_mach_sh4;
841       break;
842 
843     default:
844       return false;
845     }
846 
847   if (arch != info->arch)
848     return false;
849 
850   if (number != info->mach)
851     return false;
852 
853   return true;
854 }
855 
856 
857 /*
858 FUNCTION
859 	bfd_get_arch_info
860 
861 SYNOPSIS
862 	const bfd_arch_info_type * bfd_get_arch_info(bfd *abfd);
863 
864 DESCRIPTION
865 	Return the architecture info struct in @var{abfd}.
866 */
867 
868 const bfd_arch_info_type *
869 bfd_get_arch_info (abfd)
870      bfd *abfd;
871 {
872   return abfd->arch_info;
873 }
874 
875 
876 /*
877 FUNCTION
878 	bfd_lookup_arch
879 
880 SYNOPSIS
881 	const bfd_arch_info_type *bfd_lookup_arch
882 		(enum bfd_architecture
883 		arch,
884 		unsigned long machine);
885 
886 DESCRIPTION
887 	Look for the architecure info structure which matches the
888 	arguments @var{arch} and @var{machine}. A machine of 0 matches the
889 	machine/architecture structure which marks itself as the
890 	default.
891 */
892 
893 const bfd_arch_info_type *
894 bfd_lookup_arch (arch, machine)
895      enum bfd_architecture arch;
896      unsigned long machine;
897 {
898   const bfd_arch_info_type * const *app, *ap;
899 
900   for (app = bfd_archures_list; *app != NULL; app++)
901     {
902       for (ap = *app; ap != NULL; ap = ap->next)
903 	{
904 	  if (ap->arch == arch
905 	      && (ap->mach == machine
906 		  || (machine == 0 && ap->the_default)))
907 	    return ap;
908 	}
909     }
910 
911   return NULL;
912 }
913 
914 
915 /*
916 FUNCTION
917 	bfd_printable_arch_mach
918 
919 SYNOPSIS
920 	const char *bfd_printable_arch_mach
921 		(enum bfd_architecture arch, unsigned long machine);
922 
923 DESCRIPTION
924 	Return a printable string representing the architecture and
925 	machine type.
926 
927 	This routine is depreciated.
928 */
929 
930 const char *
931 bfd_printable_arch_mach (arch, machine)
932      enum bfd_architecture arch;
933      unsigned long machine;
934 {
935     const bfd_arch_info_type * ap = bfd_lookup_arch (arch, machine);
936 
937     if (ap)
938       return ap->printable_name;
939     return "UNKNOWN!";
940 }
941 
942 /*
943 FUNCTION
944 	bfd_octets_per_byte
945 
946 SYNOPSIS
947 	unsigned int bfd_octets_per_byte(bfd *abfd);
948 
949 DESCRIPTION
950 	Return the number of octets (8-bit quantities) per target byte
951         (minimum addressable unit).  In most cases, this will be one, but some
952         DSP targets have 16, 32, or even 48 bits per byte.
953 
954 */
955 
956 unsigned int
957 bfd_octets_per_byte (abfd)
958      bfd * abfd;
959 {
960     return bfd_arch_mach_octets_per_byte (bfd_get_arch (abfd),
961                                           bfd_get_mach (abfd));
962 }
963 
964 /*
965 FUNCTION
966 	bfd_arch_mach_octets_per_byte
967 
968 SYNOPSIS
969 	unsigned int bfd_arch_mach_octets_per_byte(enum bfd_architecture arch,
970                                                    unsigned long machine);
971 
972 DESCRIPTION
973 	See bfd_octets_per_byte.
974 
975         This routine is provided for those cases where a bfd * is not
976         available
977 */
978 
979 unsigned int
980 bfd_arch_mach_octets_per_byte (arch, mach)
981     enum bfd_architecture arch;
982     unsigned long mach;
983 {
984     const bfd_arch_info_type * ap = bfd_lookup_arch (arch, mach);
985 
986     if (ap)
987       return ap->bits_per_byte / 8;
988     return 1;
989 }
990