| 1 | /* BFD library support routines for the Renesas H8/300 architecture. |
| 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 2000, 2001, 2002, 2003 |
| 3 | Free Software Foundation, Inc. |
| 4 | Hacked by 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 | |
| 26 | static bfd_boolean h8300_scan |
| 27 | (const struct bfd_arch_info *, const char *); |
| 28 | static const bfd_arch_info_type * compatible |
| 29 | (const bfd_arch_info_type *, const bfd_arch_info_type *); |
| 30 | |
| 31 | static bfd_boolean |
| 32 | h8300_scan (const struct bfd_arch_info *info, const char *string) |
| 33 | { |
| 34 | if (*string != 'h' && *string != 'H') |
| 35 | return FALSE; |
| 36 | |
| 37 | string++; |
| 38 | if (*string != '8') |
| 39 | return FALSE; |
| 40 | |
| 41 | string++; |
| 42 | if (*string == '/') |
| 43 | string++; |
| 44 | |
| 45 | if (*string != '3') |
| 46 | return FALSE; |
| 47 | string++; |
| 48 | if (*string != '0') |
| 49 | return FALSE; |
| 50 | string++; |
| 51 | if (*string != '0') |
| 52 | return FALSE; |
| 53 | string++; |
| 54 | if (*string == '-') |
| 55 | string++; |
| 56 | |
| 57 | /* In ELF linker scripts, we typically express the architecture/machine |
| 58 | as architecture:machine. |
| 59 | |
| 60 | So if we've matched so far and encounter a colon, try to match the |
| 61 | string following the colon. */ |
| 62 | if (*string == ':') |
| 63 | { |
| 64 | string++; |
| 65 | return h8300_scan (info, string); |
| 66 | } |
| 67 | |
| 68 | if (*string == 'h' || *string == 'H') |
| 69 | { |
| 70 | string++; |
| 71 | if (*string == 'n' || *string == 'N') |
| 72 | return (info->mach == bfd_mach_h8300hn); |
| 73 | |
| 74 | return (info->mach == bfd_mach_h8300h); |
| 75 | } |
| 76 | else if (*string == 's' || *string == 'S') |
| 77 | { |
| 78 | string++; |
| 79 | if (*string == 'n' || *string == 'N') |
| 80 | return (info->mach == bfd_mach_h8300sn); |
| 81 | |
| 82 | if (*string == 'x' || *string == 'X') |
| 83 | { |
| 84 | string++; |
| 85 | if (*string == 'n' || *string == 'N') |
| 86 | return (info->mach == bfd_mach_h8300sxn); |
| 87 | |
| 88 | return (info->mach == bfd_mach_h8300sx); |
| 89 | } |
| 90 | |
| 91 | return (info->mach == bfd_mach_h8300s); |
| 92 | } |
| 93 | else |
| 94 | return info->mach == bfd_mach_h8300; |
| 95 | } |
| 96 | |
| 97 | /* This routine is provided two arch_infos and works out the machine |
| 98 | which would be compatible with both and returns a pointer to its |
| 99 | info structure. */ |
| 100 | |
| 101 | static const bfd_arch_info_type * |
| 102 | compatible (const bfd_arch_info_type *in, const bfd_arch_info_type *out) |
| 103 | { |
| 104 | /* It's really not a good idea to mix and match modes. */ |
| 105 | if (in->arch != out->arch || in->mach != out->mach) |
| 106 | return 0; |
| 107 | else |
| 108 | return in; |
| 109 | } |
| 110 | |
| 111 | static const bfd_arch_info_type h8300sxn_info_struct = |
| 112 | { |
| 113 | 32, /* 32 bits in a word */ |
| 114 | 32, /* 32 bits in an address */ |
| 115 | 8, /* 8 bits in a byte */ |
| 116 | bfd_arch_h8300, |
| 117 | bfd_mach_h8300sxn, |
| 118 | "h8300sxn", /* arch_name */ |
| 119 | "h8300sxn", /* printable name */ |
| 120 | 1, |
| 121 | FALSE, /* the default machine */ |
| 122 | compatible, |
| 123 | h8300_scan, |
| 124 | 0 |
| 125 | }; |
| 126 | |
| 127 | static const bfd_arch_info_type h8300sx_info_struct = |
| 128 | { |
| 129 | 32, /* 32 bits in a word */ |
| 130 | 32, /* 32 bits in an address */ |
| 131 | 8, /* 8 bits in a byte */ |
| 132 | bfd_arch_h8300, |
| 133 | bfd_mach_h8300sx, |
| 134 | "h8300sx", /* arch_name */ |
| 135 | "h8300sx", /* printable name */ |
| 136 | 1, |
| 137 | FALSE, /* the default machine */ |
| 138 | compatible, |
| 139 | h8300_scan, |
| 140 | &h8300sxn_info_struct |
| 141 | }; |
| 142 | |
| 143 | static const bfd_arch_info_type h8300sn_info_struct = |
| 144 | { |
| 145 | 32, /* 32 bits in a word. */ |
| 146 | 32, /* 32 bits in an address. */ |
| 147 | 8, /* 8 bits in a byte. */ |
| 148 | bfd_arch_h8300, |
| 149 | bfd_mach_h8300sn, |
| 150 | "h8300sn", /* Architecture name. */ |
| 151 | "h8300sn", /* Printable name. */ |
| 152 | 1, |
| 153 | FALSE, /* The default machine. */ |
| 154 | compatible, |
| 155 | h8300_scan, |
| 156 | &h8300sx_info_struct |
| 157 | }; |
| 158 | |
| 159 | |
| 160 | static const bfd_arch_info_type h8300hn_info_struct = |
| 161 | { |
| 162 | 32, /* 32 bits in a word. */ |
| 163 | 32, /* 32 bits in an address. */ |
| 164 | 8, /* 8 bits in a byte. */ |
| 165 | bfd_arch_h8300, |
| 166 | bfd_mach_h8300hn, |
| 167 | "h8300hn", /* Architecture name. */ |
| 168 | "h8300hn", /* Printable name. */ |
| 169 | 1, |
| 170 | FALSE, /* The default machine. */ |
| 171 | compatible, |
| 172 | h8300_scan, |
| 173 | &h8300sn_info_struct |
| 174 | }; |
| 175 | |
| 176 | static const bfd_arch_info_type h8300s_info_struct = |
| 177 | { |
| 178 | 32, /* 32 bits in a word. */ |
| 179 | 32, /* 32 bits in an address. */ |
| 180 | 8, /* 8 bits in a byte. */ |
| 181 | bfd_arch_h8300, |
| 182 | bfd_mach_h8300s, |
| 183 | "h8300s", /* Architecture name. */ |
| 184 | "h8300s", /* Printable name. */ |
| 185 | 1, |
| 186 | FALSE, /* The default machine. */ |
| 187 | compatible, |
| 188 | h8300_scan, |
| 189 | & h8300hn_info_struct |
| 190 | }; |
| 191 | |
| 192 | static const bfd_arch_info_type h8300h_info_struct = |
| 193 | { |
| 194 | 32, /* 32 bits in a word. */ |
| 195 | 32, /* 32 bits in an address. */ |
| 196 | 8, /* 8 bits in a byte. */ |
| 197 | bfd_arch_h8300, |
| 198 | bfd_mach_h8300h, |
| 199 | "h8300h", /* Architecture name. */ |
| 200 | "h8300h", /* Printable name. */ |
| 201 | 1, |
| 202 | FALSE, /* The default machine. */ |
| 203 | compatible, |
| 204 | h8300_scan, |
| 205 | &h8300s_info_struct |
| 206 | }; |
| 207 | |
| 208 | const bfd_arch_info_type bfd_h8300_arch = |
| 209 | { |
| 210 | 16, /* 16 bits in a word. */ |
| 211 | 16, /* 16 bits in an address. */ |
| 212 | 8, /* 8 bits in a byte. */ |
| 213 | bfd_arch_h8300, |
| 214 | bfd_mach_h8300, |
| 215 | "h8300", /* Architecture name. */ |
| 216 | "h8300", /* Printable name. */ |
| 217 | 1, |
| 218 | TRUE, /* The default machine. */ |
| 219 | compatible, |
| 220 | h8300_scan, |
| 221 | &h8300h_info_struct |
| 222 | }; |
| 223 | |
| 224 | /* Pad the given address to 32 bits, converting 16-bit and 24-bit |
| 225 | addresses into the values they would have had on a h8s target. */ |
| 226 | |
| 227 | bfd_vma |
| 228 | bfd_h8300_pad_address (bfd *abfd, bfd_vma address) |
| 229 | { |
| 230 | /* Cope with bfd_vma's larger than 32 bits. */ |
| 231 | address &= 0xffffffffu; |
| 232 | |
| 233 | switch (bfd_get_mach (abfd)) |
| 234 | { |
| 235 | case bfd_mach_h8300: |
| 236 | case bfd_mach_h8300hn: |
| 237 | case bfd_mach_h8300sn: |
| 238 | case bfd_mach_h8300sxn: |
| 239 | /* Sign extend a 16-bit address. */ |
| 240 | if (address >= 0x8000) |
| 241 | return address | 0xffff0000u; |
| 242 | return address; |
| 243 | |
| 244 | case bfd_mach_h8300h: |
| 245 | /* Sign extend a 24-bit address. */ |
| 246 | if (address >= 0x800000) |
| 247 | return address | 0xff000000u; |
| 248 | return address; |
| 249 | |
| 250 | case bfd_mach_h8300s: |
| 251 | case bfd_mach_h8300sx: |
| 252 | return address; |
| 253 | |
| 254 | default: |
| 255 | abort (); |
| 256 | } |
| 257 | } |