vax-dis.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* Print VAX instructions.
  2. Copyright (C) 1995-2022 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 "disassemble.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 (((((((unsigned) 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 bool
  100. parse_disassembler_options (const 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. += 1 + 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 bool
  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 bool
  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. {
  207. unsigned char *q = p0 + 1;
  208. unsigned char nextmode = NEXTBYTE (q);
  209. if (nextmode < 0x60 || nextmode == 0x8f)
  210. /* Literal, index, register, or immediate is invalid. In
  211. particular don't recurse into another index mode which
  212. might overflow the_buffer. */
  213. (*info->fprintf_func) (info->stream, "[invalid base]");
  214. else
  215. p += print_insn_mode (d, size, p0 + 1, addr + 1, info);
  216. (*info->fprintf_func) (info->stream, "[%s]", reg_names[reg]);
  217. }
  218. break;
  219. case 0x50: /* Register: Rn */
  220. (*info->fprintf_func) (info->stream, "%s", reg_names[reg]);
  221. break;
  222. case 0x60: /* Register deferred: (Rn) */
  223. (*info->fprintf_func) (info->stream, "(%s)", reg_names[reg]);
  224. break;
  225. case 0x70: /* Autodecrement: -(Rn) */
  226. (*info->fprintf_func) (info->stream, "-(%s)", reg_names[reg]);
  227. break;
  228. case 0x80: /* Autoincrement: (Rn)+ */
  229. if (reg == 0xF)
  230. { /* Immediate? */
  231. int i;
  232. FETCH_DATA (info, p + size);
  233. (*info->fprintf_func) (info->stream, "$0x");
  234. if (d[1] == 'd' || d[1] == 'f' || d[1] == 'g' || d[1] == 'h')
  235. {
  236. int float_word;
  237. float_word = p[0] | (p[1] << 8);
  238. if ((d[1] == 'd' || d[1] == 'f')
  239. && (float_word & 0xff80) == 0x8000)
  240. {
  241. (*info->fprintf_func) (info->stream, "[invalid %c-float]",
  242. d[1]);
  243. }
  244. else
  245. {
  246. for (i = 0; i < size; i++)
  247. (*info->fprintf_func) (info->stream, "%02x",
  248. p[size - i - 1]);
  249. (*info->fprintf_func) (info->stream, " [%c-float]", d[1]);
  250. }
  251. }
  252. else
  253. {
  254. for (i = 0; i < size; i++)
  255. (*info->fprintf_func) (info->stream, "%02x", p[size - i - 1]);
  256. }
  257. p += size;
  258. }
  259. else
  260. (*info->fprintf_func) (info->stream, "(%s)+", reg_names[reg]);
  261. break;
  262. case 0x90: /* Autoincrement deferred: @(Rn)+ */
  263. if (reg == 0xF)
  264. (*info->fprintf_func) (info->stream, "*0x%x", NEXTLONG (p));
  265. else
  266. (*info->fprintf_func) (info->stream, "@(%s)+", reg_names[reg]);
  267. break;
  268. case 0xB0: /* Displacement byte deferred: *displ(Rn). */
  269. (*info->fprintf_func) (info->stream, "*");
  270. /* Fall through. */
  271. case 0xA0: /* Displacement byte: displ(Rn). */
  272. if (reg == 0xF)
  273. (*info->print_address_func) (addr + 2 + NEXTBYTE (p), info);
  274. else
  275. (*info->fprintf_func) (info->stream, "0x%x(%s)", NEXTBYTE (p),
  276. reg_names[reg]);
  277. break;
  278. case 0xD0: /* Displacement word deferred: *displ(Rn). */
  279. (*info->fprintf_func) (info->stream, "*");
  280. /* Fall through. */
  281. case 0xC0: /* Displacement word: displ(Rn). */
  282. if (reg == 0xF)
  283. (*info->print_address_func) (addr + 3 + NEXTWORD (p), info);
  284. else
  285. (*info->fprintf_func) (info->stream, "0x%x(%s)", NEXTWORD (p),
  286. reg_names[reg]);
  287. break;
  288. case 0xF0: /* Displacement long deferred: *displ(Rn). */
  289. (*info->fprintf_func) (info->stream, "*");
  290. /* Fall through. */
  291. case 0xE0: /* Displacement long: displ(Rn). */
  292. if (reg == 0xF)
  293. (*info->print_address_func) (addr + 5 + NEXTLONG (p), info);
  294. else
  295. (*info->fprintf_func) (info->stream, "0x%x(%s)", NEXTLONG (p),
  296. reg_names[reg]);
  297. break;
  298. }
  299. return p - p0;
  300. }
  301. /* Returns number of bytes "eaten" by the operand, or return -1 if an
  302. invalid operand was found, or -2 if an opcode tabel error was
  303. found. */
  304. static int
  305. print_insn_arg (const char *d,
  306. unsigned char *p0,
  307. bfd_vma addr, /* PC for this arg to be relative to. */
  308. disassemble_info *info)
  309. {
  310. int arg_len;
  311. /* Check validity of addressing length. */
  312. switch (d[1])
  313. {
  314. case 'b' : arg_len = 1; break;
  315. case 'd' : arg_len = 8; break;
  316. case 'f' : arg_len = 4; break;
  317. case 'g' : arg_len = 8; break;
  318. case 'h' : arg_len = 16; break;
  319. case 'l' : arg_len = 4; break;
  320. case 'o' : arg_len = 16; break;
  321. case 'w' : arg_len = 2; break;
  322. case 'q' : arg_len = 8; break;
  323. default : abort ();
  324. }
  325. /* Branches have no mode byte. */
  326. if (d[0] == 'b')
  327. {
  328. unsigned char *p = p0;
  329. if (arg_len == 1)
  330. (*info->print_address_func) (addr + 1 + NEXTBYTE (p), info);
  331. else
  332. (*info->print_address_func) (addr + 2 + NEXTWORD (p), info);
  333. return p - p0;
  334. }
  335. return print_insn_mode (d, arg_len, p0, addr, info);
  336. }
  337. /* Print the vax instruction at address MEMADDR in debugged memory,
  338. on INFO->STREAM. Returns length of the instruction, in bytes. */
  339. int
  340. print_insn_vax (bfd_vma memaddr, disassemble_info *info)
  341. {
  342. static bool parsed_disassembler_options = false;
  343. const struct vot *votp;
  344. const char *argp;
  345. unsigned char *arg;
  346. struct private priv;
  347. bfd_byte *buffer = priv.the_buffer;
  348. info->private_data = & priv;
  349. priv.max_fetched = priv.the_buffer;
  350. priv.insn_start = memaddr;
  351. if (! parsed_disassembler_options
  352. && info->disassembler_options != NULL)
  353. {
  354. parse_disassembler_options (info->disassembler_options);
  355. /* To avoid repeated parsing of these options. */
  356. parsed_disassembler_options = true;
  357. }
  358. if (OPCODES_SIGSETJMP (priv.bailout) != 0)
  359. /* Error return. */
  360. return -1;
  361. argp = NULL;
  362. /* Check if the info buffer has more than one byte left since
  363. the last opcode might be a single byte with no argument data. */
  364. if (info->buffer_length - (memaddr - info->buffer_vma) > 1
  365. && (info->stop_vma == 0 || memaddr < (info->stop_vma - 1)))
  366. {
  367. FETCH_DATA (info, buffer + 2);
  368. }
  369. else
  370. {
  371. FETCH_DATA (info, buffer + 1);
  372. buffer[1] = 0;
  373. }
  374. /* Decode function entry mask. */
  375. if (is_function_entry (info, memaddr))
  376. {
  377. int i = 0;
  378. int register_mask = buffer[1] << 8 | buffer[0];
  379. (*info->fprintf_func) (info->stream, ".word 0x%04x # Entry mask: <",
  380. register_mask);
  381. for (i = 15; i >= 0; i--)
  382. if (register_mask & (1 << i))
  383. (*info->fprintf_func) (info->stream, " %s", entry_mask_bit[i]);
  384. (*info->fprintf_func) (info->stream, " >");
  385. return 2;
  386. }
  387. /* Decode PLT entry offset longword. */
  388. if (is_plt_tail (info, memaddr))
  389. {
  390. int offset;
  391. FETCH_DATA (info, buffer + 4);
  392. offset = ((unsigned) buffer[3] << 24 | buffer[2] << 16
  393. | buffer[1] << 8 | buffer[0]);
  394. (*info->fprintf_func) (info->stream, ".long 0x%08x", offset);
  395. return 4;
  396. }
  397. for (votp = &votstrs[0]; votp->name[0]; votp++)
  398. {
  399. vax_opcodeT opcode = votp->detail.code;
  400. /* 2 byte codes match 2 buffer pos. */
  401. if ((bfd_byte) opcode == buffer[0]
  402. && (opcode >> 8 == 0 || opcode >> 8 == buffer[1]))
  403. {
  404. argp = votp->detail.args;
  405. break;
  406. }
  407. }
  408. if (argp == NULL)
  409. {
  410. /* Handle undefined instructions. */
  411. (*info->fprintf_func) (info->stream, ".word 0x%x",
  412. (buffer[0] << 8) + buffer[1]);
  413. return 2;
  414. }
  415. /* Point at first byte of argument data, and at descriptor for first
  416. argument. */
  417. arg = buffer + ((votp->detail.code >> 8) ? 2 : 1);
  418. /* Make sure we have it in mem */
  419. FETCH_DATA (info, arg);
  420. (*info->fprintf_func) (info->stream, "%s", votp->name);
  421. if (*argp)
  422. (*info->fprintf_func) (info->stream, " ");
  423. while (*argp)
  424. {
  425. arg += print_insn_arg (argp, arg, memaddr + (arg - buffer), info);
  426. argp += 2;
  427. if (*argp)
  428. (*info->fprintf_func) (info->stream, ",");
  429. }
  430. return arg - buffer;
  431. }