disas.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* General "disassemble this chunk" code. Used for debugging. */
  2. #include "config.h"
  3. #include "dis-asm.h"
  4. #include "elf.h"
  5. #include <errno.h>
  6. #include "cpu.h"
  7. #include "disas.h"
  8. /* Filled in by elfload.c. Simplistic, but will do for now. */
  9. struct syminfo *syminfos = NULL;
  10. /* Get LENGTH bytes from info's buffer, at target address memaddr.
  11. Transfer them to myaddr. */
  12. int
  13. buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
  14. struct disassemble_info *info)
  15. {
  16. if (memaddr < info->buffer_vma
  17. || memaddr + length > info->buffer_vma + info->buffer_length)
  18. /* Out of bounds. Use EIO because GDB uses it. */
  19. return EIO;
  20. memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
  21. return 0;
  22. }
  23. /* Get LENGTH bytes from info's buffer, at target address memaddr.
  24. Transfer them to myaddr. */
  25. static int
  26. target_read_memory (bfd_vma memaddr,
  27. bfd_byte *myaddr,
  28. int length,
  29. struct disassemble_info *info)
  30. {
  31. cpu_memory_rw_debug(cpu_single_env, memaddr, myaddr, length, 0);
  32. return 0;
  33. }
  34. /* Print an error message. We can assume that this is in response to
  35. an error return from buffer_read_memory. */
  36. void
  37. perror_memory (int status, bfd_vma memaddr, struct disassemble_info *info)
  38. {
  39. if (status != EIO)
  40. /* Can't happen. */
  41. (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
  42. else
  43. /* Actually, address between memaddr and memaddr + len was
  44. out of bounds. */
  45. (*info->fprintf_func) (info->stream,
  46. "Address 0x%" PRIx64 " is out of bounds.\n", memaddr);
  47. }
  48. /* This could be in a separate file, to save minuscule amounts of space
  49. in statically linked executables. */
  50. /* Just print the address is hex. This is included for completeness even
  51. though both GDB and objdump provide their own (to print symbolic
  52. addresses). */
  53. void
  54. generic_print_address (bfd_vma addr, struct disassemble_info *info)
  55. {
  56. (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr);
  57. }
  58. /* Just return the given address. */
  59. int
  60. generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info)
  61. {
  62. return 1;
  63. }
  64. bfd_vma bfd_getl64 (const bfd_byte *addr)
  65. {
  66. unsigned long long v;
  67. v = (unsigned long long) addr[0];
  68. v |= (unsigned long long) addr[1] << 8;
  69. v |= (unsigned long long) addr[2] << 16;
  70. v |= (unsigned long long) addr[3] << 24;
  71. v |= (unsigned long long) addr[4] << 32;
  72. v |= (unsigned long long) addr[5] << 40;
  73. v |= (unsigned long long) addr[6] << 48;
  74. v |= (unsigned long long) addr[7] << 56;
  75. return (bfd_vma) v;
  76. }
  77. bfd_vma bfd_getl32 (const bfd_byte *addr)
  78. {
  79. unsigned long v;
  80. v = (unsigned long) addr[0];
  81. v |= (unsigned long) addr[1] << 8;
  82. v |= (unsigned long) addr[2] << 16;
  83. v |= (unsigned long) addr[3] << 24;
  84. return (bfd_vma) v;
  85. }
  86. bfd_vma bfd_getb32 (const bfd_byte *addr)
  87. {
  88. unsigned long v;
  89. v = (unsigned long) addr[0] << 24;
  90. v |= (unsigned long) addr[1] << 16;
  91. v |= (unsigned long) addr[2] << 8;
  92. v |= (unsigned long) addr[3];
  93. return (bfd_vma) v;
  94. }
  95. bfd_vma bfd_getl16 (const bfd_byte *addr)
  96. {
  97. unsigned long v;
  98. v = (unsigned long) addr[0];
  99. v |= (unsigned long) addr[1] << 8;
  100. return (bfd_vma) v;
  101. }
  102. bfd_vma bfd_getb16 (const bfd_byte *addr)
  103. {
  104. unsigned long v;
  105. v = (unsigned long) addr[0] << 24;
  106. v |= (unsigned long) addr[1] << 16;
  107. return (bfd_vma) v;
  108. }
  109. #ifdef TARGET_ARM
  110. static int
  111. print_insn_thumb1(bfd_vma pc, disassemble_info *info)
  112. {
  113. return print_insn_arm(pc | 1, info);
  114. }
  115. #endif
  116. /* Disassemble this for me please... (debugging). 'flags' has the following
  117. values:
  118. i386 - 1 means 16 bit code, 2 means 64 bit code
  119. arm - bit 0 = thumb, bit 1 = reverse endian
  120. ppc - nonzero means little endian
  121. other targets - unused
  122. */
  123. void target_disas(FILE *out, target_ulong code, target_ulong size, int flags)
  124. {
  125. target_ulong pc;
  126. int count;
  127. struct disassemble_info disasm_info;
  128. int (*print_insn)(bfd_vma pc, disassemble_info *info);
  129. INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
  130. disasm_info.read_memory_func = target_read_memory;
  131. disasm_info.buffer_vma = code;
  132. disasm_info.buffer_length = size;
  133. #ifdef TARGET_WORDS_BIGENDIAN
  134. disasm_info.endian = BFD_ENDIAN_BIG;
  135. #else
  136. disasm_info.endian = BFD_ENDIAN_LITTLE;
  137. #endif
  138. #if defined(TARGET_I386)
  139. if (flags == 2)
  140. disasm_info.mach = bfd_mach_x86_64;
  141. else if (flags == 1)
  142. disasm_info.mach = bfd_mach_i386_i8086;
  143. else
  144. disasm_info.mach = bfd_mach_i386_i386;
  145. print_insn = print_insn_i386;
  146. #elif defined(TARGET_ARM)
  147. if (flags & 1) {
  148. print_insn = print_insn_thumb1;
  149. } else {
  150. print_insn = print_insn_arm;
  151. }
  152. if (flags & 2) {
  153. #ifdef TARGET_WORDS_BIGENDIAN
  154. disasm_info.endian = BFD_ENDIAN_LITTLE;
  155. #else
  156. disasm_info.endian = BFD_ENDIAN_BIG;
  157. #endif
  158. }
  159. #elif defined(TARGET_SPARC)
  160. print_insn = print_insn_sparc;
  161. #ifdef TARGET_SPARC64
  162. disasm_info.mach = bfd_mach_sparc_v9b;
  163. #endif
  164. #elif defined(TARGET_PPC)
  165. if (flags >> 16)
  166. disasm_info.endian = BFD_ENDIAN_LITTLE;
  167. if (flags & 0xFFFF) {
  168. /* If we have a precise definitions of the instructions set, use it */
  169. disasm_info.mach = flags & 0xFFFF;
  170. } else {
  171. #ifdef TARGET_PPC64
  172. disasm_info.mach = bfd_mach_ppc64;
  173. #else
  174. disasm_info.mach = bfd_mach_ppc;
  175. #endif
  176. }
  177. print_insn = print_insn_ppc;
  178. #elif defined(TARGET_M68K)
  179. print_insn = print_insn_m68k;
  180. #elif defined(TARGET_MIPS)
  181. #ifdef TARGET_WORDS_BIGENDIAN
  182. print_insn = print_insn_big_mips;
  183. #else
  184. print_insn = print_insn_little_mips;
  185. #endif
  186. #elif defined(TARGET_SH4)
  187. disasm_info.mach = bfd_mach_sh4;
  188. print_insn = print_insn_sh;
  189. #elif defined(TARGET_ALPHA)
  190. disasm_info.mach = bfd_mach_alpha_ev6;
  191. print_insn = print_insn_alpha;
  192. #elif defined(TARGET_CRIS)
  193. if (flags != 32) {
  194. disasm_info.mach = bfd_mach_cris_v0_v10;
  195. print_insn = print_insn_crisv10;
  196. } else {
  197. disasm_info.mach = bfd_mach_cris_v32;
  198. print_insn = print_insn_crisv32;
  199. }
  200. #elif defined(TARGET_S390X)
  201. disasm_info.mach = bfd_mach_s390_64;
  202. print_insn = print_insn_s390;
  203. #elif defined(TARGET_MICROBLAZE)
  204. disasm_info.mach = bfd_arch_microblaze;
  205. print_insn = print_insn_microblaze;
  206. #elif defined(TARGET_LM32)
  207. disasm_info.mach = bfd_mach_lm32;
  208. print_insn = print_insn_lm32;
  209. #else
  210. fprintf(out, "0x" TARGET_FMT_lx
  211. ": Asm output not supported on this arch\n", code);
  212. return;
  213. #endif
  214. for (pc = code; size > 0; pc += count, size -= count) {
  215. fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
  216. count = print_insn(pc, &disasm_info);
  217. #if 0
  218. {
  219. int i;
  220. uint8_t b;
  221. fprintf(out, " {");
  222. for(i = 0; i < count; i++) {
  223. target_read_memory(pc + i, &b, 1, &disasm_info);
  224. fprintf(out, " %02x", b);
  225. }
  226. fprintf(out, " }");
  227. }
  228. #endif
  229. fprintf(out, "\n");
  230. if (count < 0)
  231. break;
  232. if (size < count) {
  233. fprintf(out,
  234. "Disassembler disagrees with translator over instruction "
  235. "decoding\n"
  236. "Please report this to qemu-devel@nongnu.org\n");
  237. break;
  238. }
  239. }
  240. }
  241. /* Disassemble this for me please... (debugging). */
  242. void disas(FILE *out, void *code, unsigned long size)
  243. {
  244. uintptr_t pc;
  245. int count;
  246. struct disassemble_info disasm_info;
  247. int (*print_insn)(bfd_vma pc, disassemble_info *info);
  248. INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
  249. disasm_info.buffer = code;
  250. disasm_info.buffer_vma = (uintptr_t)code;
  251. disasm_info.buffer_length = size;
  252. #ifdef HOST_WORDS_BIGENDIAN
  253. disasm_info.endian = BFD_ENDIAN_BIG;
  254. #else
  255. disasm_info.endian = BFD_ENDIAN_LITTLE;
  256. #endif
  257. #if defined(CONFIG_TCG_INTERPRETER)
  258. print_insn = print_insn_tci;
  259. #elif defined(__i386__)
  260. disasm_info.mach = bfd_mach_i386_i386;
  261. print_insn = print_insn_i386;
  262. #elif defined(__x86_64__)
  263. disasm_info.mach = bfd_mach_x86_64;
  264. print_insn = print_insn_i386;
  265. #elif defined(_ARCH_PPC)
  266. print_insn = print_insn_ppc;
  267. #elif defined(__alpha__)
  268. print_insn = print_insn_alpha;
  269. #elif defined(__sparc__)
  270. print_insn = print_insn_sparc;
  271. #if defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
  272. disasm_info.mach = bfd_mach_sparc_v9b;
  273. #endif
  274. #elif defined(__arm__)
  275. print_insn = print_insn_arm;
  276. #elif defined(__MIPSEB__)
  277. print_insn = print_insn_big_mips;
  278. #elif defined(__MIPSEL__)
  279. print_insn = print_insn_little_mips;
  280. #elif defined(__m68k__)
  281. print_insn = print_insn_m68k;
  282. #elif defined(__s390__)
  283. print_insn = print_insn_s390;
  284. #elif defined(__hppa__)
  285. print_insn = print_insn_hppa;
  286. #elif defined(__ia64__)
  287. print_insn = print_insn_ia64;
  288. #else
  289. fprintf(out, "0x%lx: Asm output not supported on this arch\n",
  290. (long) code);
  291. return;
  292. #endif
  293. for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
  294. fprintf(out, "0x%08" PRIxPTR ": ", pc);
  295. count = print_insn(pc, &disasm_info);
  296. fprintf(out, "\n");
  297. if (count < 0)
  298. break;
  299. }
  300. }
  301. /* Look up symbol for debugging purpose. Returns "" if unknown. */
  302. const char *lookup_symbol(target_ulong orig_addr)
  303. {
  304. const char *symbol = "";
  305. struct syminfo *s;
  306. for (s = syminfos; s; s = s->next) {
  307. symbol = s->lookup_symbol(s, orig_addr);
  308. if (symbol[0] != '\0') {
  309. break;
  310. }
  311. }
  312. return symbol;
  313. }
  314. #if !defined(CONFIG_USER_ONLY)
  315. #include "monitor.h"
  316. static int monitor_disas_is_physical;
  317. static CPUArchState *monitor_disas_env;
  318. static int
  319. monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
  320. struct disassemble_info *info)
  321. {
  322. if (monitor_disas_is_physical) {
  323. cpu_physical_memory_read(memaddr, myaddr, length);
  324. } else {
  325. cpu_memory_rw_debug(monitor_disas_env, memaddr,myaddr, length, 0);
  326. }
  327. return 0;
  328. }
  329. static int GCC_FMT_ATTR(2, 3)
  330. monitor_fprintf(FILE *stream, const char *fmt, ...)
  331. {
  332. va_list ap;
  333. va_start(ap, fmt);
  334. monitor_vprintf((Monitor *)stream, fmt, ap);
  335. va_end(ap);
  336. return 0;
  337. }
  338. void monitor_disas(Monitor *mon, CPUArchState *env,
  339. target_ulong pc, int nb_insn, int is_physical, int flags)
  340. {
  341. int count, i;
  342. struct disassemble_info disasm_info;
  343. int (*print_insn)(bfd_vma pc, disassemble_info *info);
  344. INIT_DISASSEMBLE_INFO(disasm_info, (FILE *)mon, monitor_fprintf);
  345. monitor_disas_env = env;
  346. monitor_disas_is_physical = is_physical;
  347. disasm_info.read_memory_func = monitor_read_memory;
  348. disasm_info.buffer_vma = pc;
  349. #ifdef TARGET_WORDS_BIGENDIAN
  350. disasm_info.endian = BFD_ENDIAN_BIG;
  351. #else
  352. disasm_info.endian = BFD_ENDIAN_LITTLE;
  353. #endif
  354. #if defined(TARGET_I386)
  355. if (flags == 2)
  356. disasm_info.mach = bfd_mach_x86_64;
  357. else if (flags == 1)
  358. disasm_info.mach = bfd_mach_i386_i8086;
  359. else
  360. disasm_info.mach = bfd_mach_i386_i386;
  361. print_insn = print_insn_i386;
  362. #elif defined(TARGET_ARM)
  363. print_insn = print_insn_arm;
  364. #elif defined(TARGET_ALPHA)
  365. print_insn = print_insn_alpha;
  366. #elif defined(TARGET_SPARC)
  367. print_insn = print_insn_sparc;
  368. #ifdef TARGET_SPARC64
  369. disasm_info.mach = bfd_mach_sparc_v9b;
  370. #endif
  371. #elif defined(TARGET_PPC)
  372. #ifdef TARGET_PPC64
  373. disasm_info.mach = bfd_mach_ppc64;
  374. #else
  375. disasm_info.mach = bfd_mach_ppc;
  376. #endif
  377. print_insn = print_insn_ppc;
  378. #elif defined(TARGET_M68K)
  379. print_insn = print_insn_m68k;
  380. #elif defined(TARGET_MIPS)
  381. #ifdef TARGET_WORDS_BIGENDIAN
  382. print_insn = print_insn_big_mips;
  383. #else
  384. print_insn = print_insn_little_mips;
  385. #endif
  386. #elif defined(TARGET_SH4)
  387. disasm_info.mach = bfd_mach_sh4;
  388. print_insn = print_insn_sh;
  389. #elif defined(TARGET_S390X)
  390. disasm_info.mach = bfd_mach_s390_64;
  391. print_insn = print_insn_s390;
  392. #elif defined(TARGET_LM32)
  393. disasm_info.mach = bfd_mach_lm32;
  394. print_insn = print_insn_lm32;
  395. #else
  396. monitor_printf(mon, "0x" TARGET_FMT_lx
  397. ": Asm output not supported on this arch\n", pc);
  398. return;
  399. #endif
  400. for(i = 0; i < nb_insn; i++) {
  401. monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
  402. count = print_insn(pc, &disasm_info);
  403. monitor_printf(mon, "\n");
  404. if (count < 0)
  405. break;
  406. pc += count;
  407. }
  408. }
  409. #endif