xtensa-dis.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /* xtensa-dis.c. Disassembly functions for Xtensa.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. Contributed by Bob Wilson at Tensilica, Inc. (bwilson@tensilica.com)
  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 file; see the file COPYING. If not, write to the
  15. Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <string.h>
  22. #include "xtensa-isa.h"
  23. #include "ansidecl.h"
  24. #include "libiberty.h"
  25. #include "bfd.h"
  26. #include "elf/xtensa.h"
  27. #include "disassemble.h"
  28. #include <setjmp.h>
  29. extern xtensa_isa xtensa_default_isa;
  30. #ifndef MAX
  31. #define MAX(a,b) (a > b ? a : b)
  32. #endif
  33. int show_raw_fields;
  34. struct dis_private
  35. {
  36. bfd_byte *byte_buf;
  37. OPCODES_SIGJMP_BUF bailout;
  38. /* Persistent fields, valid for last_section only. */
  39. asection *last_section;
  40. property_table_entry *insn_table_entries;
  41. int insn_table_entry_count;
  42. /* Cached property table search position. */
  43. bfd_vma insn_table_cur_addr;
  44. int insn_table_cur_idx;
  45. };
  46. static void
  47. xtensa_coalesce_insn_tables (struct dis_private *priv)
  48. {
  49. const int mask = ~(XTENSA_PROP_DATA | XTENSA_PROP_NO_TRANSFORM);
  50. int count = priv->insn_table_entry_count;
  51. int i, j;
  52. /* Loop over all entries, combining adjacent ones that differ only in
  53. the flag bits XTENSA_PROP_DATA and XTENSA_PROP_NO_TRANSFORM. */
  54. for (i = j = 0; j < count; ++i)
  55. {
  56. property_table_entry *entry = priv->insn_table_entries + i;
  57. *entry = priv->insn_table_entries[j];
  58. for (++j; j < count; ++j)
  59. {
  60. property_table_entry *next = priv->insn_table_entries + j;
  61. int fill = xtensa_compute_fill_extra_space (entry);
  62. int size = entry->size + fill;
  63. if (entry->address + size == next->address)
  64. {
  65. int entry_flags = entry->flags & mask;
  66. int next_flags = next->flags & mask;
  67. if (next_flags == entry_flags)
  68. entry->size = next->address - entry->address + next->size;
  69. else
  70. break;
  71. }
  72. else
  73. {
  74. break;
  75. }
  76. }
  77. }
  78. priv->insn_table_entry_count = i;
  79. }
  80. static property_table_entry *
  81. xtensa_find_table_entry (bfd_vma memaddr, struct disassemble_info *info)
  82. {
  83. struct dis_private *priv = (struct dis_private *) info->private_data;
  84. int i;
  85. if (priv->insn_table_entries == NULL
  86. || priv->insn_table_entry_count < 0)
  87. return NULL;
  88. if (memaddr < priv->insn_table_cur_addr)
  89. priv->insn_table_cur_idx = 0;
  90. for (i = priv->insn_table_cur_idx; i < priv->insn_table_entry_count; ++i)
  91. {
  92. property_table_entry *block = priv->insn_table_entries + i;
  93. if (block->size != 0)
  94. {
  95. if ((memaddr >= block->address
  96. && memaddr < block->address + block->size)
  97. || memaddr < block->address)
  98. {
  99. priv->insn_table_cur_addr = memaddr;
  100. priv->insn_table_cur_idx = i;
  101. return block;
  102. }
  103. }
  104. }
  105. return NULL;
  106. }
  107. /* Check whether an instruction crosses an instruction block boundary
  108. (according to property tables).
  109. If it does, return 0 (doesn't fit), else return 1. */
  110. static int
  111. xtensa_instruction_fits (bfd_vma memaddr, int size,
  112. property_table_entry *insn_block)
  113. {
  114. unsigned max_size;
  115. /* If no property table info, assume it fits. */
  116. if (insn_block == NULL || size <= 0)
  117. return 1;
  118. /* If too high, limit nextstop by the next insn address. */
  119. if (insn_block->address > memaddr)
  120. {
  121. /* memaddr is not in an instruction block, but is followed by one. */
  122. max_size = insn_block->address - memaddr;
  123. }
  124. else
  125. {
  126. /* memaddr is in an instruction block, go no further than the end. */
  127. max_size = insn_block->address + insn_block->size - memaddr;
  128. }
  129. /* Crossing a boundary, doesn't "fit". */
  130. if ((unsigned)size > max_size)
  131. return 0;
  132. return 1;
  133. }
  134. static int
  135. fetch_data (struct disassemble_info *info, bfd_vma memaddr)
  136. {
  137. int length, status = 0;
  138. struct dis_private *priv = (struct dis_private *) info->private_data;
  139. int insn_size = xtensa_isa_maxlength (xtensa_default_isa);
  140. insn_size = MAX (insn_size, 4);
  141. /* Read the maximum instruction size, padding with zeros if we go past
  142. the end of the text section. This code will automatically adjust
  143. length when we hit the end of the buffer. */
  144. memset (priv->byte_buf, 0, insn_size);
  145. for (length = insn_size; length > 0; length--)
  146. {
  147. status = (*info->read_memory_func) (memaddr, priv->byte_buf, length,
  148. info);
  149. if (status == 0)
  150. return length;
  151. }
  152. (*info->memory_error_func) (status, memaddr, info);
  153. OPCODES_SIGLONGJMP (priv->bailout, 1);
  154. /*NOTREACHED*/
  155. }
  156. static void
  157. print_xtensa_operand (bfd_vma memaddr,
  158. struct disassemble_info *info,
  159. xtensa_opcode opc,
  160. int opnd,
  161. unsigned operand_val)
  162. {
  163. xtensa_isa isa = xtensa_default_isa;
  164. int signed_operand_val, status;
  165. bfd_byte litbuf[4];
  166. if (show_raw_fields)
  167. {
  168. if (operand_val < 0xa)
  169. (*info->fprintf_func) (info->stream, "%u", operand_val);
  170. else
  171. (*info->fprintf_func) (info->stream, "0x%x", operand_val);
  172. return;
  173. }
  174. (void) xtensa_operand_decode (isa, opc, opnd, &operand_val);
  175. signed_operand_val = (int) operand_val;
  176. if (xtensa_operand_is_register (isa, opc, opnd) == 0)
  177. {
  178. if (xtensa_operand_is_PCrelative (isa, opc, opnd) == 1)
  179. {
  180. (void) xtensa_operand_undo_reloc (isa, opc, opnd,
  181. &operand_val, memaddr);
  182. info->target = operand_val;
  183. (*info->print_address_func) (info->target, info);
  184. /* Also display value loaded by L32R (but not if reloc exists,
  185. those tend to be wrong): */
  186. if ((info->flags & INSN_HAS_RELOC) == 0
  187. && !strcmp ("l32r", xtensa_opcode_name (isa, opc)))
  188. status = (*info->read_memory_func) (operand_val, litbuf, 4, info);
  189. else
  190. status = -1;
  191. if (status == 0)
  192. {
  193. unsigned literal = bfd_get_bits (litbuf, 32,
  194. info->endian == BFD_ENDIAN_BIG);
  195. (*info->fprintf_func) (info->stream, " (");
  196. (*info->print_address_func) (literal, info);
  197. (*info->fprintf_func) (info->stream, ")");
  198. }
  199. }
  200. else
  201. {
  202. if ((signed_operand_val > -256) && (signed_operand_val < 256))
  203. (*info->fprintf_func) (info->stream, "%d", signed_operand_val);
  204. else
  205. (*info->fprintf_func) (info->stream, "0x%x", signed_operand_val);
  206. }
  207. }
  208. else
  209. {
  210. int i = 1;
  211. xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd);
  212. (*info->fprintf_func) (info->stream, "%s%u",
  213. xtensa_regfile_shortname (isa, opnd_rf),
  214. operand_val);
  215. while (i < xtensa_operand_num_regs (isa, opc, opnd))
  216. {
  217. operand_val++;
  218. (*info->fprintf_func) (info->stream, ":%s%u",
  219. xtensa_regfile_shortname (isa, opnd_rf),
  220. operand_val);
  221. i++;
  222. }
  223. }
  224. }
  225. /* Print the Xtensa instruction at address MEMADDR on info->stream.
  226. Returns length of the instruction in bytes. */
  227. int
  228. print_insn_xtensa (bfd_vma memaddr, struct disassemble_info *info)
  229. {
  230. unsigned operand_val;
  231. int bytes_fetched, size, maxsize, i, n, noperands, nslots;
  232. xtensa_isa isa;
  233. xtensa_opcode opc;
  234. xtensa_format fmt;
  235. static struct dis_private priv;
  236. static bfd_byte *byte_buf = NULL;
  237. static xtensa_insnbuf insn_buffer = NULL;
  238. static xtensa_insnbuf slot_buffer = NULL;
  239. int first, first_slot, valid_insn;
  240. property_table_entry *insn_block;
  241. if (!xtensa_default_isa)
  242. xtensa_default_isa = xtensa_isa_init (0, 0);
  243. info->target = 0;
  244. maxsize = xtensa_isa_maxlength (xtensa_default_isa);
  245. /* Set bytes_per_line to control the amount of whitespace between the hex
  246. values and the opcode. For Xtensa, we always print one "chunk" and we
  247. vary bytes_per_chunk to determine how many bytes to print. (objdump
  248. would apparently prefer that we set bytes_per_chunk to 1 and vary
  249. bytes_per_line but that makes it hard to fit 64-bit instructions on
  250. an 80-column screen.) The value of bytes_per_line here is not exactly
  251. right, because objdump adds an extra space for each chunk so that the
  252. amount of whitespace depends on the chunk size. Oh well, it's good
  253. enough.... Note that we set the minimum size to 4 to accomodate
  254. literal pools. */
  255. info->bytes_per_line = MAX (maxsize, 4);
  256. /* Allocate buffers the first time through. */
  257. if (!insn_buffer)
  258. {
  259. insn_buffer = xtensa_insnbuf_alloc (xtensa_default_isa);
  260. slot_buffer = xtensa_insnbuf_alloc (xtensa_default_isa);
  261. byte_buf = (bfd_byte *) xmalloc (MAX (maxsize, 4));
  262. }
  263. priv.byte_buf = byte_buf;
  264. info->private_data = (void *) &priv;
  265. /* Prepare instruction tables. */
  266. if (info->section != NULL)
  267. {
  268. asection *section = info->section;
  269. if (priv.last_section != section)
  270. {
  271. bfd *abfd = section->owner;
  272. if (priv.last_section != NULL)
  273. {
  274. /* Reset insn_table_entries. */
  275. priv.insn_table_entry_count = 0;
  276. free (priv.insn_table_entries);
  277. priv.insn_table_entries = NULL;
  278. }
  279. priv.last_section = section;
  280. /* Read insn_table_entries. */
  281. priv.insn_table_entry_count =
  282. xtensa_read_table_entries (abfd, section,
  283. &priv.insn_table_entries,
  284. XTENSA_PROP_SEC_NAME, false);
  285. if (priv.insn_table_entry_count == 0)
  286. {
  287. free (priv.insn_table_entries);
  288. priv.insn_table_entries = NULL;
  289. /* Backwards compatibility support. */
  290. priv.insn_table_entry_count =
  291. xtensa_read_table_entries (abfd, section,
  292. &priv.insn_table_entries,
  293. XTENSA_INSN_SEC_NAME, false);
  294. }
  295. priv.insn_table_cur_idx = 0;
  296. xtensa_coalesce_insn_tables (&priv);
  297. }
  298. /* Else nothing to do, same section as last time. */
  299. }
  300. if (OPCODES_SIGSETJMP (priv.bailout) != 0)
  301. /* Error return. */
  302. return -1;
  303. /* Fetch the maximum size instruction. */
  304. bytes_fetched = fetch_data (info, memaddr);
  305. insn_block = xtensa_find_table_entry (memaddr, info);
  306. /* Don't set "isa" before the setjmp to keep the compiler from griping. */
  307. isa = xtensa_default_isa;
  308. size = 0;
  309. nslots = 0;
  310. valid_insn = 0;
  311. fmt = 0;
  312. if (!insn_block || (insn_block->flags & XTENSA_PROP_INSN))
  313. {
  314. /* Copy the bytes into the decode buffer. */
  315. memset (insn_buffer, 0, (xtensa_insnbuf_size (isa) *
  316. sizeof (xtensa_insnbuf_word)));
  317. xtensa_insnbuf_from_chars (isa, insn_buffer, priv.byte_buf,
  318. bytes_fetched);
  319. fmt = xtensa_format_decode (isa, insn_buffer);
  320. if (fmt != XTENSA_UNDEFINED
  321. && ((size = xtensa_format_length (isa, fmt)) <= bytes_fetched)
  322. && xtensa_instruction_fits (memaddr, size, insn_block))
  323. {
  324. /* Make sure all the opcodes are valid. */
  325. valid_insn = 1;
  326. nslots = xtensa_format_num_slots (isa, fmt);
  327. for (n = 0; n < nslots; n++)
  328. {
  329. xtensa_format_get_slot (isa, fmt, n, insn_buffer, slot_buffer);
  330. if (xtensa_opcode_decode (isa, fmt, n, slot_buffer)
  331. == XTENSA_UNDEFINED)
  332. {
  333. valid_insn = 0;
  334. break;
  335. }
  336. }
  337. }
  338. }
  339. if (!valid_insn)
  340. {
  341. if (insn_block && (insn_block->flags & XTENSA_PROP_LITERAL)
  342. && (memaddr & 3) == 0 && bytes_fetched >= 4)
  343. {
  344. info->bytes_per_chunk = 4;
  345. return 4;
  346. }
  347. else
  348. {
  349. (*info->fprintf_func) (info->stream, ".byte %#02x", priv.byte_buf[0]);
  350. return 1;
  351. }
  352. }
  353. if (nslots > 1)
  354. (*info->fprintf_func) (info->stream, "{ ");
  355. info->insn_type = dis_nonbranch;
  356. info->insn_info_valid = 1;
  357. first_slot = 1;
  358. for (n = 0; n < nslots; n++)
  359. {
  360. if (first_slot)
  361. first_slot = 0;
  362. else
  363. (*info->fprintf_func) (info->stream, "; ");
  364. xtensa_format_get_slot (isa, fmt, n, insn_buffer, slot_buffer);
  365. opc = xtensa_opcode_decode (isa, fmt, n, slot_buffer);
  366. (*info->fprintf_func) (info->stream, "%s",
  367. xtensa_opcode_name (isa, opc));
  368. if (xtensa_opcode_is_branch (isa, opc))
  369. info->insn_type = dis_condbranch;
  370. else if (xtensa_opcode_is_jump (isa, opc))
  371. info->insn_type = dis_branch;
  372. else if (xtensa_opcode_is_call (isa, opc))
  373. info->insn_type = dis_jsr;
  374. /* Print the operands (if any). */
  375. noperands = xtensa_opcode_num_operands (isa, opc);
  376. first = 1;
  377. for (i = 0; i < noperands; i++)
  378. {
  379. if (xtensa_operand_is_visible (isa, opc, i) == 0)
  380. continue;
  381. if (first)
  382. {
  383. (*info->fprintf_func) (info->stream, "\t");
  384. first = 0;
  385. }
  386. else
  387. (*info->fprintf_func) (info->stream, ", ");
  388. (void) xtensa_operand_get_field (isa, opc, i, fmt, n,
  389. slot_buffer, &operand_val);
  390. print_xtensa_operand (memaddr, info, opc, i, operand_val);
  391. }
  392. }
  393. if (nslots > 1)
  394. (*info->fprintf_func) (info->stream, " }");
  395. info->bytes_per_chunk = size;
  396. info->display_endian = info->endian;
  397. return size;
  398. }