xref: /openbsd-src/gnu/usr.bin/binutils-2.17/bfd/cpu-h8300.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* BFD library support routines for the Renesas H8/300 architecture.
2*3d8817e4Smiod    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 2000, 2001, 2002,
3*3d8817e4Smiod    2003, 2004 Free Software Foundation, Inc.
4*3d8817e4Smiod    Hacked by Steve Chamberlain of Cygnus Support.
5*3d8817e4Smiod 
6*3d8817e4Smiod    This file is part of BFD, the Binary File Descriptor library.
7*3d8817e4Smiod 
8*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
9*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
10*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
11*3d8817e4Smiod    (at your option) any later version.
12*3d8817e4Smiod 
13*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
14*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*3d8817e4Smiod    GNU General Public License for more details.
17*3d8817e4Smiod 
18*3d8817e4Smiod    You should have received a copy of the GNU General Public License
19*3d8817e4Smiod    along with this program; if not, write to the Free Software
20*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21*3d8817e4Smiod 
22*3d8817e4Smiod #include "bfd.h"
23*3d8817e4Smiod #include "sysdep.h"
24*3d8817e4Smiod #include "libbfd.h"
25*3d8817e4Smiod 
26*3d8817e4Smiod static bfd_boolean
h8300_scan(const struct bfd_arch_info * info,const char * string)27*3d8817e4Smiod h8300_scan (const struct bfd_arch_info *info, const char *string)
28*3d8817e4Smiod {
29*3d8817e4Smiod   if (*string != 'h' && *string != 'H')
30*3d8817e4Smiod     return FALSE;
31*3d8817e4Smiod 
32*3d8817e4Smiod   string++;
33*3d8817e4Smiod   if (*string != '8')
34*3d8817e4Smiod     return FALSE;
35*3d8817e4Smiod 
36*3d8817e4Smiod   string++;
37*3d8817e4Smiod   if (*string == '/')
38*3d8817e4Smiod     string++;
39*3d8817e4Smiod 
40*3d8817e4Smiod   if (*string != '3')
41*3d8817e4Smiod     return FALSE;
42*3d8817e4Smiod   string++;
43*3d8817e4Smiod   if (*string != '0')
44*3d8817e4Smiod     return FALSE;
45*3d8817e4Smiod   string++;
46*3d8817e4Smiod   if (*string != '0')
47*3d8817e4Smiod     return FALSE;
48*3d8817e4Smiod   string++;
49*3d8817e4Smiod   if (*string == '-')
50*3d8817e4Smiod     string++;
51*3d8817e4Smiod 
52*3d8817e4Smiod   /* In ELF linker scripts, we typically express the architecture/machine
53*3d8817e4Smiod      as architecture:machine.
54*3d8817e4Smiod 
55*3d8817e4Smiod      So if we've matched so far and encounter a colon, try to match the
56*3d8817e4Smiod      string following the colon.  */
57*3d8817e4Smiod   if (*string == ':')
58*3d8817e4Smiod     {
59*3d8817e4Smiod       string++;
60*3d8817e4Smiod       return h8300_scan (info, string);
61*3d8817e4Smiod     }
62*3d8817e4Smiod 
63*3d8817e4Smiod   if (*string == 'h' || *string == 'H')
64*3d8817e4Smiod     {
65*3d8817e4Smiod       string++;
66*3d8817e4Smiod       if (*string == 'n' || *string == 'N')
67*3d8817e4Smiod 	return (info->mach == bfd_mach_h8300hn);
68*3d8817e4Smiod 
69*3d8817e4Smiod       return (info->mach == bfd_mach_h8300h);
70*3d8817e4Smiod     }
71*3d8817e4Smiod   else if (*string == 's' || *string == 'S')
72*3d8817e4Smiod     {
73*3d8817e4Smiod       string++;
74*3d8817e4Smiod       if (*string == 'n' || *string == 'N')
75*3d8817e4Smiod 	return (info->mach == bfd_mach_h8300sn);
76*3d8817e4Smiod 
77*3d8817e4Smiod       if (*string == 'x' || *string == 'X')
78*3d8817e4Smiod 	{
79*3d8817e4Smiod 	  string++;
80*3d8817e4Smiod 	  if (*string == 'n' || *string == 'N')
81*3d8817e4Smiod 	    return (info->mach == bfd_mach_h8300sxn);
82*3d8817e4Smiod 
83*3d8817e4Smiod 	  return (info->mach == bfd_mach_h8300sx);
84*3d8817e4Smiod 	}
85*3d8817e4Smiod 
86*3d8817e4Smiod       return (info->mach == bfd_mach_h8300s);
87*3d8817e4Smiod     }
88*3d8817e4Smiod   else
89*3d8817e4Smiod     return info->mach == bfd_mach_h8300;
90*3d8817e4Smiod }
91*3d8817e4Smiod 
92*3d8817e4Smiod /* This routine is provided two arch_infos and works out the machine
93*3d8817e4Smiod    which would be compatible with both and returns a pointer to its
94*3d8817e4Smiod    info structure.  */
95*3d8817e4Smiod 
96*3d8817e4Smiod static const bfd_arch_info_type *
compatible(const bfd_arch_info_type * in,const bfd_arch_info_type * out)97*3d8817e4Smiod compatible (const bfd_arch_info_type *in, const bfd_arch_info_type *out)
98*3d8817e4Smiod {
99*3d8817e4Smiod   if (in->arch != out->arch)
100*3d8817e4Smiod     return 0;
101*3d8817e4Smiod   if (in->mach == bfd_mach_h8300sx && out->mach == bfd_mach_h8300s)
102*3d8817e4Smiod     return in;
103*3d8817e4Smiod   if (in->mach == bfd_mach_h8300s && out->mach == bfd_mach_h8300sx)
104*3d8817e4Smiod     return out;
105*3d8817e4Smiod   if (in->mach == bfd_mach_h8300sxn && out->mach == bfd_mach_h8300sn)
106*3d8817e4Smiod     return in;
107*3d8817e4Smiod   if (in->mach == bfd_mach_h8300sn && out->mach == bfd_mach_h8300sxn)
108*3d8817e4Smiod     return out;
109*3d8817e4Smiod   /* It's really not a good idea to mix and match modes.  */
110*3d8817e4Smiod   if (in->mach != out->mach)
111*3d8817e4Smiod     return 0;
112*3d8817e4Smiod   else
113*3d8817e4Smiod     return in;
114*3d8817e4Smiod }
115*3d8817e4Smiod 
116*3d8817e4Smiod static const bfd_arch_info_type h8300sxn_info_struct =
117*3d8817e4Smiod {
118*3d8817e4Smiod   32,				/* 32 bits in a word */
119*3d8817e4Smiod   16,				/* 16 bits in an address */
120*3d8817e4Smiod   8,				/* 8 bits in a byte */
121*3d8817e4Smiod   bfd_arch_h8300,
122*3d8817e4Smiod   bfd_mach_h8300sxn,
123*3d8817e4Smiod   "h8300sxn",			/* arch_name  */
124*3d8817e4Smiod   "h8300sxn",			/* printable name */
125*3d8817e4Smiod   1,
126*3d8817e4Smiod   FALSE,			/* the default machine */
127*3d8817e4Smiod   compatible,
128*3d8817e4Smiod   h8300_scan,
129*3d8817e4Smiod   0
130*3d8817e4Smiod };
131*3d8817e4Smiod 
132*3d8817e4Smiod static const bfd_arch_info_type h8300sx_info_struct =
133*3d8817e4Smiod {
134*3d8817e4Smiod   32,				/* 32 bits in a word */
135*3d8817e4Smiod   32,				/* 32 bits in an address */
136*3d8817e4Smiod   8,				/* 8 bits in a byte */
137*3d8817e4Smiod   bfd_arch_h8300,
138*3d8817e4Smiod   bfd_mach_h8300sx,
139*3d8817e4Smiod   "h8300sx",			/* arch_name  */
140*3d8817e4Smiod   "h8300sx",			/* printable name */
141*3d8817e4Smiod   1,
142*3d8817e4Smiod   FALSE,			/* the default machine */
143*3d8817e4Smiod   compatible,
144*3d8817e4Smiod   h8300_scan,
145*3d8817e4Smiod   &h8300sxn_info_struct
146*3d8817e4Smiod };
147*3d8817e4Smiod 
148*3d8817e4Smiod static const bfd_arch_info_type h8300sn_info_struct =
149*3d8817e4Smiod {
150*3d8817e4Smiod   32,				/* 32 bits in a word.  */
151*3d8817e4Smiod   16,				/* 16 bits in an address.  */
152*3d8817e4Smiod   8,				/* 8 bits in a byte.  */
153*3d8817e4Smiod   bfd_arch_h8300,
154*3d8817e4Smiod   bfd_mach_h8300sn,
155*3d8817e4Smiod   "h8300sn",			/* Architecture name.  */
156*3d8817e4Smiod   "h8300sn",			/* Printable name.  */
157*3d8817e4Smiod   1,
158*3d8817e4Smiod   FALSE,			/* The default machine.  */
159*3d8817e4Smiod   compatible,
160*3d8817e4Smiod   h8300_scan,
161*3d8817e4Smiod   &h8300sx_info_struct
162*3d8817e4Smiod };
163*3d8817e4Smiod 
164*3d8817e4Smiod static const bfd_arch_info_type h8300hn_info_struct =
165*3d8817e4Smiod {
166*3d8817e4Smiod   32,				/* 32 bits in a word.  */
167*3d8817e4Smiod   16,				/* 16 bits in an address.  */
168*3d8817e4Smiod   8,				/* 8 bits in a byte.  */
169*3d8817e4Smiod   bfd_arch_h8300,
170*3d8817e4Smiod   bfd_mach_h8300hn,
171*3d8817e4Smiod   "h8300hn",			/* Architecture name.  */
172*3d8817e4Smiod   "h8300hn",			/* Printable name.  */
173*3d8817e4Smiod   1,
174*3d8817e4Smiod   FALSE,			/* The default machine.  */
175*3d8817e4Smiod   compatible,
176*3d8817e4Smiod   h8300_scan,
177*3d8817e4Smiod   &h8300sn_info_struct
178*3d8817e4Smiod };
179*3d8817e4Smiod 
180*3d8817e4Smiod static const bfd_arch_info_type h8300s_info_struct =
181*3d8817e4Smiod {
182*3d8817e4Smiod   32,				/* 32 bits in a word.  */
183*3d8817e4Smiod   32,				/* 32 bits in an address.  */
184*3d8817e4Smiod   8,				/* 8 bits in a byte.  */
185*3d8817e4Smiod   bfd_arch_h8300,
186*3d8817e4Smiod   bfd_mach_h8300s,
187*3d8817e4Smiod   "h8300s",			/* Architecture name.  */
188*3d8817e4Smiod   "h8300s",			/* Printable name.  */
189*3d8817e4Smiod   1,
190*3d8817e4Smiod   FALSE,			/* The default machine.  */
191*3d8817e4Smiod   compatible,
192*3d8817e4Smiod   h8300_scan,
193*3d8817e4Smiod   & h8300hn_info_struct
194*3d8817e4Smiod };
195*3d8817e4Smiod 
196*3d8817e4Smiod static const bfd_arch_info_type h8300h_info_struct =
197*3d8817e4Smiod {
198*3d8817e4Smiod   32,				/* 32 bits in a word.  */
199*3d8817e4Smiod   32,				/* 32 bits in an address.  */
200*3d8817e4Smiod   8,				/* 8 bits in a byte.  */
201*3d8817e4Smiod   bfd_arch_h8300,
202*3d8817e4Smiod   bfd_mach_h8300h,
203*3d8817e4Smiod   "h8300h",			/* Architecture name.  */
204*3d8817e4Smiod   "h8300h",			/* Printable name.  */
205*3d8817e4Smiod   1,
206*3d8817e4Smiod   FALSE,			/* The default machine.  */
207*3d8817e4Smiod   compatible,
208*3d8817e4Smiod   h8300_scan,
209*3d8817e4Smiod   &h8300s_info_struct
210*3d8817e4Smiod };
211*3d8817e4Smiod 
212*3d8817e4Smiod const bfd_arch_info_type bfd_h8300_arch =
213*3d8817e4Smiod {
214*3d8817e4Smiod   16,				/* 16 bits in a word.  */
215*3d8817e4Smiod   16,				/* 16 bits in an address.  */
216*3d8817e4Smiod   8,				/* 8 bits in a byte.  */
217*3d8817e4Smiod   bfd_arch_h8300,
218*3d8817e4Smiod   bfd_mach_h8300,
219*3d8817e4Smiod   "h8300",			/* Architecture name.  */
220*3d8817e4Smiod   "h8300",			/* Printable name.  */
221*3d8817e4Smiod   1,
222*3d8817e4Smiod   TRUE,				/* The default machine.  */
223*3d8817e4Smiod   compatible,
224*3d8817e4Smiod   h8300_scan,
225*3d8817e4Smiod   &h8300h_info_struct
226*3d8817e4Smiod };
227*3d8817e4Smiod 
228*3d8817e4Smiod /* Pad the given address to 32 bits, converting 16-bit and 24-bit
229*3d8817e4Smiod    addresses into the values they would have had on a h8s target.  */
230*3d8817e4Smiod 
231*3d8817e4Smiod bfd_vma
bfd_h8300_pad_address(bfd * abfd,bfd_vma address)232*3d8817e4Smiod bfd_h8300_pad_address (bfd *abfd, bfd_vma address)
233*3d8817e4Smiod {
234*3d8817e4Smiod   /* Cope with bfd_vma's larger than 32 bits.  */
235*3d8817e4Smiod   address &= 0xffffffffu;
236*3d8817e4Smiod 
237*3d8817e4Smiod   switch (bfd_get_mach (abfd))
238*3d8817e4Smiod     {
239*3d8817e4Smiod     case bfd_mach_h8300:
240*3d8817e4Smiod     case bfd_mach_h8300hn:
241*3d8817e4Smiod     case bfd_mach_h8300sn:
242*3d8817e4Smiod     case bfd_mach_h8300sxn:
243*3d8817e4Smiod       /* Sign extend a 16-bit address.  */
244*3d8817e4Smiod       if (address >= 0x8000)
245*3d8817e4Smiod 	return address | 0xffff0000u;
246*3d8817e4Smiod       return address;
247*3d8817e4Smiod 
248*3d8817e4Smiod     case bfd_mach_h8300h:
249*3d8817e4Smiod       /* Sign extend a 24-bit address.  */
250*3d8817e4Smiod       if (address >= 0x800000)
251*3d8817e4Smiod 	return address | 0xff000000u;
252*3d8817e4Smiod       return address;
253*3d8817e4Smiod 
254*3d8817e4Smiod     case bfd_mach_h8300s:
255*3d8817e4Smiod     case bfd_mach_h8300sx:
256*3d8817e4Smiod       return address;
257*3d8817e4Smiod 
258*3d8817e4Smiod     default:
259*3d8817e4Smiod       abort ();
260*3d8817e4Smiod     }
261*3d8817e4Smiod }
262