vax-dis.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /* Print VAX instructions.
  2. Copyright (C) 1995-2015 Free Software Foundation, Inc.
  3. Contributed by Pauline Middelink <middelin@polyware.iaf.nl>
  4. This file is part of the GNU opcodes library.
  5. This library is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. It is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include <setjmp.h>
  19. #include <string.h>
  20. #include "opcode/vax.h"
  21. #include "dis-asm.h"
  22. static char *reg_names[] =
  23. {
  24. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  25. "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc"
  26. };
  27. /* Definitions for the function entry mask bits. */
  28. static char *entry_mask_bit[] =
  29. {
  30. /* Registers 0 and 1 shall not be saved, since they're used to pass back
  31. a function's result to its caller... */
  32. "~r0~", "~r1~",
  33. /* Registers 2 .. 11 are normal registers. */
  34. "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11",
  35. /* Registers 12 and 13 are argument and frame pointer and must not
  36. be saved by using the entry mask. */
  37. "~ap~", "~fp~",
  38. /* Bits 14 and 15 control integer and decimal overflow. */
  39. "IntOvfl", "DecOvfl",
  40. };
  41. /* Sign-extend an (unsigned char). */
  42. #define COERCE_SIGNED_CHAR(ch) ((signed char)(ch))
  43. /* Get a 1 byte signed integer. */
  44. #define NEXTBYTE(p) \
  45. (p += 1, FETCH_DATA (info, p), \
  46. COERCE_SIGNED_CHAR(p[-1]))
  47. /* Get a 2 byte signed integer. */
  48. #define COERCE16(x) ((int) (((x) ^ 0x8000) - 0x8000))
  49. #define NEXTWORD(p) \
  50. (p += 2, FETCH_DATA (info, p), \
  51. COERCE16 ((p[-1] << 8) + p[-2]))
  52. /* Get a 4 byte signed integer. */
  53. #define COERCE32(x) ((int) (((x) ^ 0x80000000) - 0x80000000))
  54. #define NEXTLONG(p) \
  55. (p += 4, FETCH_DATA (info, p), \
  56. (COERCE32 ((((((p[-1] << 8) + p[-2]) << 8) + p[-3]) << 8) + p[-4])))
  57. /* Maximum length of an instruction. */
  58. #define MAXLEN 25
  59. struct private
  60. {
  61. /* Points to first byte not fetched. */
  62. bfd_byte * max_fetched;
  63. bfd_byte the_buffer[MAXLEN];
  64. bfd_vma insn_start;
  65. OPCODES_SIGJMP_BUF bailout;
  66. };
  67. /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
  68. to ADDR (exclusive) are valid. Returns 1 for success, longjmps
  69. on error. */
  70. #define FETCH_DATA(info, addr) \
  71. ((addr) <= ((struct private *)(info->private_data))->max_fetched \
  72. ? 1 : fetch_data ((info), (addr)))
  73. static int
  74. fetch_data (struct disassemble_info *info, bfd_byte *addr)
  75. {
  76. int status;
  77. struct private *priv = (struct private *) info->private_data;
  78. bfd_vma start = priv->insn_start + (priv->max_fetched - priv->the_buffer);
  79. status = (*info->read_memory_func) (start,
  80. priv->max_fetched,
  81. addr - priv->max_fetched,
  82. info);
  83. if (status != 0)
  84. {
  85. (*info->memory_error_func) (status, start, info);
  86. OPCODES_SIGLONGJMP (priv->bailout, 1);
  87. }
  88. else
  89. priv->max_fetched = addr;
  90. return 1;
  91. }
  92. /* Entry mask handling. */
  93. static unsigned int entry_addr_occupied_slots = 0;
  94. static unsigned int entry_addr_total_slots = 0;
  95. static bfd_vma * entry_addr = NULL;
  96. /* Parse the VAX specific disassembler options. These contain function
  97. entry addresses, which can be useful to disassemble ROM images, since
  98. there's no symbol table. Returns TRUE upon success, FALSE otherwise. */
  99. static bfd_boolean
  100. parse_disassembler_options (char * options)
  101. {
  102. const char * entry_switch = "entry:";
  103. while ((options = strstr (options, entry_switch)))
  104. {
  105. options += strlen (entry_switch);
  106. /* The greater-than part of the test below is paranoia. */
  107. if (entry_addr_occupied_slots >= entry_addr_total_slots)
  108. {
  109. /* A guesstimate of the number of entries we will have to create. */
  110. entry_addr_total_slots +=
  111. strlen (options) / (strlen (entry_switch) + 5);
  112. entry_addr = realloc (entry_addr, sizeof (bfd_vma)
  113. * entry_addr_total_slots);
  114. }
  115. if (entry_addr == NULL)
  116. return FALSE;
  117. entry_addr[entry_addr_occupied_slots] = bfd_scan_vma (options, NULL, 0);
  118. entry_addr_occupied_slots ++;
  119. }
  120. return TRUE;
  121. }
  122. #if 0 /* FIXME: Ideally the disassembler should have target specific
  123. initialisation and termination function pointers. Then
  124. parse_disassembler_options could be the init function and
  125. free_entry_array (below) could be the termination routine.
  126. Until then there is no way for the disassembler to tell us
  127. that it has finished and that we no longer need the entry
  128. array, so this routine is suppressed for now. It does mean
  129. that we leak memory, but only to the extent that we do not
  130. free it just before the disassembler is about to terminate
  131. anyway. */
  132. /* Free memory allocated to our entry array. */
  133. static void
  134. free_entry_array (void)
  135. {
  136. if (entry_addr)
  137. {
  138. free (entry_addr);
  139. entry_addr = NULL;
  140. entry_addr_occupied_slots = entry_addr_total_slots = 0;
  141. }
  142. }
  143. #endif
  144. /* Check if the given address is a known function entry point. This is
  145. the case if there is a symbol of the function type at this address.
  146. We also check for synthetic symbols as these are used for PLT entries
  147. (weak undefined symbols may not have the function type set). Finally
  148. the address may have been forced to be treated as an entry point. The
  149. latter helps in disassembling ROM images, because there's no symbol
  150. table at all. Forced entry points can be given by supplying several
  151. -M options to objdump: -M entry:0xffbb7730. */
  152. static bfd_boolean
  153. is_function_entry (struct disassemble_info *info, bfd_vma addr)
  154. {
  155. unsigned int i;
  156. /* Check if there's a function or PLT symbol at our address. */
  157. if (info->symbols
  158. && info->symbols[0]
  159. && (info->symbols[0]->flags & (BSF_FUNCTION | BSF_SYNTHETIC))
  160. && addr == bfd_asymbol_value (info->symbols[0]))
  161. return TRUE;
  162. /* Check for forced function entry address. */
  163. for (i = entry_addr_occupied_slots; i--;)
  164. if (entry_addr[i] == addr)
  165. return TRUE;
  166. return FALSE;
  167. }
  168. /* Check if the given address is the last longword of a PLT entry.
  169. This longword is data and depending on the value it may interfere
  170. with disassembly of further PLT entries. We make use of the fact
  171. PLT symbols are marked BSF_SYNTHETIC. */
  172. static bfd_boolean
  173. is_plt_tail (struct disassemble_info *info, bfd_vma addr)
  174. {
  175. if (info->symbols
  176. && info->symbols[0]
  177. && (info->symbols[0]->flags & BSF_SYNTHETIC)
  178. && addr == bfd_asymbol_value (info->symbols[0]) + 8)
  179. return TRUE;
  180. return FALSE;
  181. }
  182. static int
  183. print_insn_mode (const char *d,
  184. int size,
  185. unsigned char *p0,
  186. bfd_vma addr, /* PC for this arg to be relative to. */
  187. disassemble_info *info)
  188. {
  189. unsigned char *p = p0;
  190. unsigned char mode, reg;
  191. /* Fetch and interpret mode byte. */
  192. mode = (unsigned char) NEXTBYTE (p);
  193. reg = mode & 0xF;
  194. switch (mode & 0xF0)
  195. {
  196. case 0x00:
  197. case 0x10:
  198. case 0x20:
  199. case 0x30: /* Literal mode $number. */
  200. if (d[1] == 'd' || d[1] == 'f' || d[1] == 'g' || d[1] == 'h')
  201. (*info->fprintf_func) (info->stream, "$0x%x [%c-float]", mode, d[1]);
  202. else
  203. (*info->fprintf_func) (info->stream, "$0x%x", mode);
  204. break;
  205. case 0x40: /* Index: base-addr[Rn] */
  206. p += print_insn_mode (d, size, p0 + 1, addr + 1, info);
  207. (*info->fprintf_func) (info->stream, "[%s]", reg_names[reg]);
  208. break;
  209. case 0x50: /* Register: Rn */
  210. (*info->fprintf_func) (info->stream, "%s", reg_names[reg]);
  211. break;
  212. case 0x60: /* Register deferred: (Rn) */
  213. (*info->fprintf_func) (info->stream, "(%s)", reg_names[reg]);
  214. break;
  215. case 0x70: /* Autodecrement: -(Rn) */
  216. (*info->fprintf_func) (info->stream, "-(%s)", reg_names[reg]);
  217. break;
  218. case 0x80: /* Autoincrement: (Rn)+ */
  219. if (reg == 0xF)
  220. { /* Immediate? */
  221. int i;
  222. FETCH_DATA (info, p + size);
  223. (*info->fprintf_func) (info->stream, "$0x");
  224. if (d[1] == 'd' || d[1] == 'f' || d[1] == 'g' || d[1] == 'h')
  225. {
  226. int float_word;
  227. float_word = p[0] | (p[1] << 8);
  228. if ((d[1] == 'd' || d[1] == 'f')
  229. && (float_word & 0xff80) == 0x8000)
  230. {
  231. (*info->fprintf_func) (info->stream, "[invalid %c-float]",
  232. d[1]);
  233. }
  234. else
  235. {
  236. for (i = 0; i < size; i++)
  237. (*info->fprintf_func) (info->stream, "%02x",
  238. p[size - i - 1]);
  239. (*info->fprintf_func) (info->stream, " [%c-float]", d[1]);
  240. }
  241. }
  242. else
  243. {
  244. for (i = 0; i < size; i++)
  245. (*info->fprintf_func) (info->stream, "%02x", p[size - i - 1]);
  246. }
  247. p += size;
  248. }
  249. else
  250. (*info->fprintf_func) (info->stream, "(%s)+", reg_names[reg]);
  251. break;
  252. case 0x90: /* Autoincrement deferred: @(Rn)+ */
  253. if (reg == 0xF)
  254. (*info->fprintf_func) (info->stream, "*0x%x", NEXTLONG (p));
  255. else
  256. (*info->fprintf_func) (info->stream, "@(%s)+", reg_names[reg]);
  257. break;
  258. case 0xB0: /* Displacement byte deferred: *displ(Rn). */
  259. (*info->fprintf_func) (info->stream, "*");
  260. case 0xA0: /* Displacement byte: displ(Rn). */
  261. if (reg == 0xF)
  262. (*info->print_address_func) (addr + 2 + NEXTBYTE (p), info);
  263. else
  264. (*info->fprintf_func) (info->stream, "0x%x(%s)", NEXTBYTE (p),
  265. reg_names[reg]);
  266. break;
  267. case 0xD0: /* Displacement word deferred: *displ(Rn). */
  268. (*info->fprintf_func) (info->stream, "*");
  269. case 0xC0: /* Displacement word: displ(Rn). */
  270. if (reg == 0xF)
  271. (*info->print_address_func) (addr + 3 + NEXTWORD (p), info);
  272. else
  273. (*info->fprintf_func) (info->stream, "0x%x(%s)", NEXTWORD (p),
  274. reg_names[reg]);
  275. break;
  276. case 0xF0: /* Displacement long deferred: *displ(Rn). */
  277. (*info->fprintf_func) (info->stream, "*");
  278. case 0xE0: /* Displacement long: displ(Rn). */
  279. if (reg == 0xF)
  280. (*info->print_address_func) (addr + 5 + NEXTLONG (p), info);
  281. else
  282. (*info->fprintf_func) (info->stream, "0x%x(%s)", NEXTLONG (p),
  283. reg_names[reg]);
  284. break;
  285. }
  286. return p - p0;
  287. }
  288. /* Returns number of bytes "eaten" by the operand, or return -1 if an
  289. invalid operand was found, or -2 if an opcode tabel error was
  290. found. */
  291. static int
  292. print_insn_arg (const char *d,
  293. unsigned char *p0,
  294. bfd_vma addr, /* PC for this arg to be relative to. */
  295. disassemble_info *info)
  296. {
  297. int arg_len;
  298. /* Check validity of addressing length. */
  299. switch (d[1])
  300. {
  301. case 'b' : arg_len = 1; break;
  302. case 'd' : arg_len = 8; break;
  303. case 'f' : arg_len = 4; break;
  304. case 'g' : arg_len = 8; break;
  305. case 'h' : arg_len = 16; break;
  306. case 'l' : arg_len = 4; break;
  307. case 'o' : arg_len = 16; break;
  308. case 'w' : arg_len = 2; break;
  309. case 'q' : arg_len = 8; break;
  310. default : abort ();
  311. }
  312. /* Branches have no mode byte. */
  313. if (d[0] == 'b')
  314. {
  315. unsigned char *p = p0;
  316. if (arg_len == 1)
  317. (*info->print_address_func) (addr + 1 + NEXTBYTE (p), info);
  318. else
  319. (*info->print_address_func) (addr + 2 + NEXTWORD (p), info);
  320. return p - p0;
  321. }
  322. return print_insn_mode (d, arg_len, p0, addr, info);
  323. }
  324. /* Print the vax instruction at address MEMADDR in debugged memory,
  325. on INFO->STREAM. Returns length of the instruction, in bytes. */
  326. int
  327. print_insn_vax (bfd_vma memaddr, disassemble_info *info)
  328. {
  329. static bfd_boolean parsed_disassembler_options = FALSE;
  330. const struct vot *votp;
  331. const char *argp;
  332. unsigned char *arg;
  333. struct private priv;
  334. bfd_byte *buffer = priv.the_buffer;
  335. info->private_data = & priv;
  336. priv.max_fetched = priv.the_buffer;
  337. priv.insn_start = memaddr;
  338. if (! parsed_disassembler_options
  339. && info->disassembler_options != NULL)
  340. {
  341. parse_disassembler_options (info->disassembler_options);
  342. /* To avoid repeated parsing of these options. */
  343. parsed_disassembler_options = TRUE;
  344. }
  345. if (OPCODES_SIGSETJMP (priv.bailout) != 0)
  346. /* Error return. */
  347. return -1;
  348. argp = NULL;
  349. /* Check if the info buffer has more than one byte left since
  350. the last opcode might be a single byte with no argument data. */
  351. if (info->buffer_length - (memaddr - info->buffer_vma) > 1
  352. && (info->stop_vma == 0 || memaddr < (info->stop_vma - 1)))
  353. {
  354. FETCH_DATA (info, buffer + 2);
  355. }
  356. else
  357. {
  358. FETCH_DATA (info, buffer + 1);
  359. buffer[1] = 0;
  360. }
  361. /* Decode function entry mask. */
  362. if (is_function_entry (info, memaddr))
  363. {
  364. int i = 0;
  365. int register_mask = buffer[1] << 8 | buffer[0];
  366. (*info->fprintf_func) (info->stream, ".word 0x%04x # Entry mask: <",
  367. register_mask);
  368. for (i = 15; i >= 0; i--)
  369. if (register_mask & (1 << i))
  370. (*info->fprintf_func) (info->stream, " %s", entry_mask_bit[i]);
  371. (*info->fprintf_func) (info->stream, " >");
  372. return 2;
  373. }
  374. /* Decode PLT entry offset longword. */
  375. if (is_plt_tail (info, memaddr))
  376. {
  377. int offset;
  378. FETCH_DATA (info, buffer + 4);
  379. offset = buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0];
  380. (*info->fprintf_func) (info->stream, ".long 0x%08x", offset);
  381. return 4;
  382. }
  383. for (votp = &votstrs[0]; votp->name[0]; votp++)
  384. {
  385. vax_opcodeT opcode = votp->detail.code;
  386. /* 2 byte codes match 2 buffer pos. */
  387. if ((bfd_byte) opcode == buffer[0]
  388. && (opcode >> 8 == 0 || opcode >> 8 == buffer[1]))
  389. {
  390. argp = votp->detail.args;
  391. break;
  392. }
  393. }
  394. if (argp == NULL)
  395. {
  396. /* Handle undefined instructions. */
  397. (*info->fprintf_func) (info->stream, ".word 0x%x",
  398. (buffer[0] << 8) + buffer[1]);
  399. return 2;
  400. }
  401. /* Point at first byte of argument data, and at descriptor for first
  402. argument. */
  403. arg = buffer + ((votp->detail.code >> 8) ? 2 : 1);
  404. /* Make sure we have it in mem */
  405. FETCH_DATA (info, arg);
  406. (*info->fprintf_func) (info->stream, "%s", votp->name);
  407. if (*argp)
  408. (*info->fprintf_func) (info->stream, " ");
  409. while (*argp)
  410. {
  411. arg += print_insn_arg (argp, arg, memaddr + arg - buffer, info);
  412. argp += 2;
  413. if (*argp)
  414. (*info->fprintf_func) (info->stream, ",");
  415. }
  416. return arg - buffer;
  417. }