| 1 | /* BFD library support routines for the Hitachi H8/300 architecture. |
| 2 | Copyright (C) 1990-1991 Free Software Foundation, Inc. |
| 3 | Hacked by 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 20 | |
| 21 | #include "bfd.h" |
| 22 | #include "sysdep.h" |
| 23 | #include "libbfd.h" |
| 24 | |
| 25 | #define DEFINE_TABLE |
| 26 | #include "opcode/h8300.h" |
| 27 | |
| 28 | #define MAXSAME 14 |
| 29 | |
| 30 | static struct h8_opcode * h8_opcodes_sorted[256][MAXSAME]; |
| 31 | |
| 32 | /* Run through the opcodes and sort them into order to make them easy |
| 33 | to disassemble |
| 34 | */ |
| 35 | static void |
| 36 | DEFUN_VOID(bfd_h8_disassemble_init) |
| 37 | { |
| 38 | unsigned int i; |
| 39 | struct h8_opcode *p; |
| 40 | |
| 41 | for (p = h8_opcodes; p->name; p++) { |
| 42 | int where = 0; |
| 43 | int n1 = 0; |
| 44 | int n2 = 0; |
| 45 | int n3 = 0; |
| 46 | int n4= 0; |
| 47 | if ((int)p->data.nib[0] < 16) { |
| 48 | n1 =(int) p->data.nib[0] ; |
| 49 | } else n1 = 0; |
| 50 | if ((int)p->data.nib[1] < 16) { |
| 51 | n2 = (int) p->data.nib[1]; |
| 52 | }else n2 = 0; |
| 53 | |
| 54 | for (i = 0; i < MAXSAME; i++) { |
| 55 | int j = n1 * 16 + n2; |
| 56 | if (h8_opcodes_sorted[j][i] == (struct h8_opcode *)NULL) { |
| 57 | h8_opcodes_sorted[j][i] = p; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (i==MAXSAME)abort(); |
| 63 | |
| 64 | /* Just make sure there are an even number of nibbles in it, and |
| 65 | that the count is the same s the length */ |
| 66 | for (i = 0; p->data.nib[i] != E; i++) ; |
| 67 | if (i & 1) abort(); |
| 68 | if (i/2 != p->length) abort(); |
| 69 | } |
| 70 | for (i = 0; i < 256; i++) |
| 71 | { |
| 72 | if (h8_opcodes_sorted[i][0]) |
| 73 | p = h8_opcodes_sorted[i][0]; |
| 74 | else h8_opcodes_sorted[i][0] = p; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | unsigned int |
| 80 | DEFUN(bfd_h8_disassemble,(addr, data, stream), |
| 81 | bfd_vma addr AND |
| 82 | CONST bfd_byte *data AND |
| 83 | FILE *stream) |
| 84 | { |
| 85 | /* Find the first entry in the table for this opcode */ |
| 86 | CONST static char *regnames[] = { |
| 87 | "r0h","r1h","r2h","r3h","r4h","r5h","r6h","r7h", |
| 88 | "r0l","r1l","r2l","r3l","r4l","r5l","r6l","r7l" }; |
| 89 | |
| 90 | int rs = 0; |
| 91 | int rd = 0; |
| 92 | int rdisp = 0; |
| 93 | int abs = 0; |
| 94 | struct h8_opcode **p = h8_opcodes_sorted[(unsigned)(data[0])]; |
| 95 | struct h8_opcode *q; |
| 96 | |
| 97 | /* Find the exact opcode/arg combo */ |
| 98 | while (*p) { |
| 99 | op_type *nib; |
| 100 | unsigned int len = 0; |
| 101 | q = *p++; |
| 102 | nib =q->data.nib; |
| 103 | while (*nib != E) { |
| 104 | op_type looking_for = *nib; |
| 105 | int thisnib = data[len>>1] ; |
| 106 | thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf); |
| 107 | if ((int)looking_for & (int)B31) { |
| 108 | if (((int)thisnib & 0x8) ==0) goto fail; |
| 109 | looking_for = (op_type)((int)looking_for & ~(int)B31); |
| 110 | } |
| 111 | if ((int)looking_for & (int)B30) { |
| 112 | if (((int)thisnib & 0x8) !=0) goto fail; |
| 113 | looking_for = (op_type)((int)looking_for & ~(int)B30); |
| 114 | } |
| 115 | switch (looking_for) { |
| 116 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
| 117 | case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
| 118 | if ((int)looking_for != thisnib) goto fail; |
| 119 | break; |
| 120 | case ABS16SRC: |
| 121 | case ABS16DST: |
| 122 | case DISPSRC: |
| 123 | case DISPDST: |
| 124 | case IMM16: |
| 125 | abs = (data[len>>1]) * 256 + data[(len+2)>>1]; |
| 126 | len+=3; |
| 127 | nib+=3; |
| 128 | break; |
| 129 | case DISPREG: |
| 130 | rdisp = thisnib; |
| 131 | break; |
| 132 | case KBIT: |
| 133 | abs = thisnib == 0x8 ? 2:1; |
| 134 | break; |
| 135 | case IMM8: |
| 136 | case ABS8SRC: |
| 137 | case ABS8DST: |
| 138 | case MEMIND: |
| 139 | case DISP8: |
| 140 | abs= data[len>>1]; |
| 141 | len++; |
| 142 | nib++; |
| 143 | break; |
| 144 | case IMM3: |
| 145 | abs = thisnib ; |
| 146 | break; |
| 147 | case RS8: |
| 148 | case RS16: |
| 149 | case RSINC: |
| 150 | case RSIND: |
| 151 | rs = thisnib; |
| 152 | break; |
| 153 | case RD16: |
| 154 | case RDDEC: |
| 155 | case RD8: |
| 156 | case RDIND: |
| 157 | rd = thisnib; |
| 158 | break; |
| 159 | default: |
| 160 | fprintf(stream, "Dont understand \n"); |
| 161 | goto found; |
| 162 | } |
| 163 | len++; |
| 164 | nib++; |
| 165 | } |
| 166 | goto found; |
| 167 | fail: |
| 168 | ; |
| 169 | |
| 170 | } |
| 171 | fprintf(stream, "%02x %02x .word\tH'%x,H'%x", |
| 172 | data[0], data[1], |
| 173 | data[0], data[1]); |
| 174 | return 2; |
| 175 | found:; |
| 176 | { int i; |
| 177 | |
| 178 | for (i = 0; i < q->length; i++) { |
| 179 | fprintf(stream, "%02x ", data[i]); |
| 180 | } |
| 181 | for (; i < 6; i++) { |
| 182 | fprintf(stream, " "); |
| 183 | } |
| 184 | } |
| 185 | fprintf(stream, "%s\t",q->name); |
| 186 | /* Now print out the args */ |
| 187 | { |
| 188 | op_type *args = q->args.nib; |
| 189 | int hadone = 0; |
| 190 | while (*args != E) { |
| 191 | if (hadone) |
| 192 | fprintf(stream, ","); |
| 193 | switch ((int)(*args) & ~((int)B30|(int)B31)) { |
| 194 | case IMM16: |
| 195 | case IMM8: |
| 196 | case IMM3: |
| 197 | fprintf(stream, "#0x%x", (unsigned)abs); |
| 198 | break; |
| 199 | case RD8: |
| 200 | fprintf(stream, "%s", regnames[rd]); |
| 201 | break; |
| 202 | case RS8: |
| 203 | fprintf(stream, "%s",regnames[rs]); |
| 204 | break; |
| 205 | case RD16: |
| 206 | fprintf(stream, "r%d", rd& 0x7); |
| 207 | break; |
| 208 | case RS16: |
| 209 | fprintf(stream, "r%d", rs & 0x7); |
| 210 | break; |
| 211 | case RSINC: |
| 212 | fprintf(stream, "@r%d+", rs & 0x7); |
| 213 | break; |
| 214 | case RDDEC: |
| 215 | fprintf(stream, "@-r%d", rd & 0x7); |
| 216 | break; |
| 217 | case RDIND: |
| 218 | fprintf(stream, "@r%d", rd & 0x7); |
| 219 | break; |
| 220 | case RSIND: |
| 221 | fprintf(stream, "@r%d",rs & 0x7); |
| 222 | break; |
| 223 | case ABS8SRC: |
| 224 | case ABS16SRC: |
| 225 | case ABS16DST: |
| 226 | case ABS8DST: |
| 227 | fprintf(stream, "@0x%x", (unsigned)abs); |
| 228 | break; |
| 229 | case MEMIND: |
| 230 | fprintf(stream, "@@%d (%x)",abs, abs); |
| 231 | break; |
| 232 | case DISP8: |
| 233 | fprintf(stream, ".%s%d (%x)",(char)abs>0 ? "+" :"", (char)abs, |
| 234 | addr + (char)abs); |
| 235 | break; |
| 236 | case DISPSRC: |
| 237 | case DISPDST: |
| 238 | fprintf(stream, "@(0x%x,r%d)", abs, rdisp & 0x7); |
| 239 | break; |
| 240 | case CCR: |
| 241 | fprintf(stream, "ccr"); |
| 242 | break; |
| 243 | case KBIT: |
| 244 | fprintf(stream, "#%d",abs); |
| 245 | break; |
| 246 | default: |
| 247 | abort(); |
| 248 | } |
| 249 | hadone = 1; |
| 250 | args++; |
| 251 | } |
| 252 | } |
| 253 | return q->length; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | |
| 258 | unsigned int DEFUN(print_insn_h8300,(addr, data, file), |
| 259 | bfd_vma addr AND |
| 260 | CONST char *data AND |
| 261 | PTR file) |
| 262 | { |
| 263 | static boolean init; |
| 264 | if (!init) { |
| 265 | bfd_h8_disassemble_init(); |
| 266 | init= 1; |
| 267 | } |
| 268 | |
| 269 | return bfd_h8_disassemble(addr, (bfd_byte *)data, file); |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | Relocations for the H8 |
| 274 | |
| 275 | */ |
| 276 | static bfd_reloc_status_type |
| 277 | DEFUN(howto16_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section), |
| 278 | bfd *abfd AND |
| 279 | arelent *reloc_entry AND |
| 280 | asymbol *symbol_in AND |
| 281 | unsigned char *data AND |
| 282 | asection *ignore_input_section) |
| 283 | { |
| 284 | long relocation = 0; |
| 285 | bfd_vma addr = reloc_entry->address; |
| 286 | long x = bfd_get_16(abfd, (bfd_byte *)data + addr); |
| 287 | |
| 288 | HOWTO_PREPARE(relocation, symbol_in); |
| 289 | |
| 290 | x = (x + relocation + reloc_entry->addend); |
| 291 | |
| 292 | bfd_put_16(abfd, x, (bfd_byte *)data + addr); |
| 293 | return bfd_reloc_ok; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | static bfd_reloc_status_type |
| 298 | DEFUN(howto8_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section), |
| 299 | bfd *abfd AND |
| 300 | arelent *reloc_entry AND |
| 301 | asymbol *symbol_in AND |
| 302 | unsigned char *data AND |
| 303 | asection *ignore_input_section) |
| 304 | { |
| 305 | long relocation = 0; |
| 306 | bfd_vma addr = reloc_entry->address; |
| 307 | long x = bfd_get_8(abfd, (bfd_byte *)data + addr); |
| 308 | |
| 309 | HOWTO_PREPARE(relocation, symbol_in); |
| 310 | |
| 311 | x = (x + relocation + reloc_entry->addend); |
| 312 | |
| 313 | bfd_put_8(abfd, x, (bfd_byte *)data + addr); |
| 314 | return bfd_reloc_ok; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | static bfd_reloc_status_type |
| 319 | DEFUN(howto8_FFnn_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section), |
| 320 | bfd *abfd AND |
| 321 | arelent *reloc_entry AND |
| 322 | asymbol *symbol_in AND |
| 323 | unsigned char *data AND |
| 324 | asection *ignore_input_section) |
| 325 | { |
| 326 | long relocation = 0; |
| 327 | bfd_vma addr = reloc_entry->address; |
| 328 | |
| 329 | long x = bfd_get_8(abfd, (bfd_byte *)data + addr); |
| 330 | abort(); |
| 331 | HOWTO_PREPARE(relocation, symbol_in); |
| 332 | |
| 333 | x = (x + relocation + reloc_entry->addend); |
| 334 | |
| 335 | bfd_put_8(abfd, x, (bfd_byte *)data + addr); |
| 336 | return bfd_reloc_ok; |
| 337 | } |
| 338 | |
| 339 | static bfd_reloc_status_type |
| 340 | DEFUN(howto8_pcrel_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section), |
| 341 | bfd *abfd AND |
| 342 | arelent *reloc_entry AND |
| 343 | asymbol *symbol_in AND |
| 344 | unsigned char *data AND |
| 345 | asection *ignore_input_section) |
| 346 | { |
| 347 | long relocation = 0; |
| 348 | bfd_vma addr = reloc_entry->address; |
| 349 | long x = bfd_get_8(abfd, (bfd_byte *)data + addr); |
| 350 | abort(); |
| 351 | HOWTO_PREPARE(relocation, symbol_in); |
| 352 | |
| 353 | x = (x + relocation + reloc_entry->addend); |
| 354 | |
| 355 | bfd_put_8(abfd, x, (bfd_byte *)data + addr); |
| 356 | return bfd_reloc_ok; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | static reloc_howto_type howto_16 |
| 361 | = NEWHOWTO(howto16_callback,"abs16",1,0,0); |
| 362 | static reloc_howto_type howto_8 |
| 363 | = NEWHOWTO(howto8_callback,"abs8",0,0,0); |
| 364 | |
| 365 | static reloc_howto_type howto_8_FFnn |
| 366 | = NEWHOWTO(howto8_FFnn_callback,"ff00+abs8",0,0,0); |
| 367 | |
| 368 | static reloc_howto_type howto_8_pcrel |
| 369 | = NEWHOWTO(howto8_pcrel_callback,"pcrel8",0,1,1); |
| 370 | |
| 371 | |
| 372 | |
| 373 | static CONST struct reloc_howto_struct * |
| 374 | DEFUN(local_bfd_reloc_type_lookup,(arch, code), |
| 375 | CONST struct bfd_arch_info *arch AND |
| 376 | bfd_reloc_code_type code) |
| 377 | { |
| 378 | switch (code) { |
| 379 | case BFD_RELOC_16: |
| 380 | return &howto_16; |
| 381 | case BFD_RELOC_8_FFnn: |
| 382 | return &howto_8_FFnn; |
| 383 | case BFD_RELOC_8: |
| 384 | return &howto_8; |
| 385 | case BFD_RELOC_8_PCREL: |
| 386 | return &howto_8_pcrel; |
| 387 | } |
| 388 | return (reloc_howto_type *)NULL; |
| 389 | } |
| 390 | |
| 391 | int bfd_default_scan_num_mach(); |
| 392 | |
| 393 | static boolean |
| 394 | DEFUN(h8300_scan,(info, string), |
| 395 | CONST struct bfd_arch_info *info AND |
| 396 | CONST char *string) |
| 397 | { |
| 398 | if (strcmp(string,"h8300") == 0) return true; |
| 399 | if (strcmp(string,"H8300") == 0) return true; |
| 400 | if (strcmp(string,"h8/300") == 0) return true; |
| 401 | if (strcmp(string,"H8/300") == 0) return true; |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | static bfd_arch_info_type arch_info_struct = |
| 406 | { |
| 407 | 16, /* 16 bits in a word */ |
| 408 | 16, /* 16 bits in an address */ |
| 409 | 8, /* 8 bits in a byte */ |
| 410 | bfd_arch_h8300, |
| 411 | 0, /* only 1 machine */ |
| 412 | "H8/300", /* arch_name */ |
| 413 | "H8/300", /* printable name */ |
| 414 | true, /* the default machine */ |
| 415 | bfd_default_compatible, |
| 416 | h8300_scan, |
| 417 | print_insn_h8300, |
| 418 | local_bfd_reloc_type_lookup, |
| 419 | 0, |
| 420 | }; |
| 421 | |
| 422 | void |
| 423 | DEFUN_VOID(bfd_h8300_arch) |
| 424 | { |
| 425 | bfd_arch_linkin(&arch_info_struct); |
| 426 | } |