disas.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 miniscule 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 - nonzero means thumb code
  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)
  148. print_insn = print_insn_thumb1;
  149. else
  150. print_insn = print_insn_arm;
  151. #elif defined(TARGET_SPARC)
  152. print_insn = print_insn_sparc;
  153. #ifdef TARGET_SPARC64
  154. disasm_info.mach = bfd_mach_sparc_v9b;
  155. #endif
  156. #elif defined(TARGET_PPC)
  157. if (flags >> 16)
  158. disasm_info.endian = BFD_ENDIAN_LITTLE;
  159. if (flags & 0xFFFF) {
  160. /* If we have a precise definitions of the instructions set, use it */
  161. disasm_info.mach = flags & 0xFFFF;
  162. } else {
  163. #ifdef TARGET_PPC64
  164. disasm_info.mach = bfd_mach_ppc64;
  165. #else
  166. disasm_info.mach = bfd_mach_ppc;
  167. #endif
  168. }
  169. print_insn = print_insn_ppc;
  170. #elif defined(TARGET_M68K)
  171. print_insn = print_insn_m68k;
  172. #elif defined(TARGET_MIPS)
  173. #ifdef TARGET_WORDS_BIGENDIAN
  174. print_insn = print_insn_big_mips;
  175. #else
  176. print_insn = print_insn_little_mips;
  177. #endif
  178. #elif defined(TARGET_SH4)
  179. disasm_info.mach = bfd_mach_sh4;
  180. print_insn = print_insn_sh;
  181. #elif defined(TARGET_ALPHA)
  182. disasm_info.mach = bfd_mach_alpha_ev6;
  183. print_insn = print_insn_alpha;
  184. #elif defined(TARGET_CRIS)
  185. if (flags != 32) {
  186. disasm_info.mach = bfd_mach_cris_v0_v10;
  187. print_insn = print_insn_crisv10;
  188. } else {
  189. disasm_info.mach = bfd_mach_cris_v32;
  190. print_insn = print_insn_crisv32;
  191. }
  192. #elif defined(TARGET_S390X)
  193. disasm_info.mach = bfd_mach_s390_64;
  194. print_insn = print_insn_s390;
  195. #elif defined(TARGET_MICROBLAZE)
  196. disasm_info.mach = bfd_arch_microblaze;
  197. print_insn = print_insn_microblaze;
  198. #else
  199. fprintf(out, "0x" TARGET_FMT_lx
  200. ": Asm output not supported on this arch\n", code);
  201. return;
  202. #endif
  203. for (pc = code; size > 0; pc += count, size -= count) {
  204. fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
  205. count = print_insn(pc, &disasm_info);
  206. #if 0
  207. {
  208. int i;
  209. uint8_t b;
  210. fprintf(out, " {");
  211. for(i = 0; i < count; i++) {
  212. target_read_memory(pc + i, &b, 1, &disasm_info);
  213. fprintf(out, " %02x", b);
  214. }
  215. fprintf(out, " }");
  216. }
  217. #endif
  218. fprintf(out, "\n");
  219. if (count < 0)
  220. break;
  221. if (size < count) {
  222. fprintf(out,
  223. "Disassembler disagrees with translator over instruction "
  224. "decoding\n"
  225. "Please report this to qemu-devel@nongnu.org\n");
  226. break;
  227. }
  228. }
  229. }
  230. /* Disassemble this for me please... (debugging). */
  231. void disas(FILE *out, void *code, unsigned long size)
  232. {
  233. unsigned long pc;
  234. int count;
  235. struct disassemble_info disasm_info;
  236. int (*print_insn)(bfd_vma pc, disassemble_info *info);
  237. INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
  238. disasm_info.buffer = code;
  239. disasm_info.buffer_vma = (unsigned long)code;
  240. disasm_info.buffer_length = size;
  241. #ifdef HOST_WORDS_BIGENDIAN
  242. disasm_info.endian = BFD_ENDIAN_BIG;
  243. #else
  244. disasm_info.endian = BFD_ENDIAN_LITTLE;
  245. #endif
  246. #if defined(CONFIG_TCG_INTERPRETER)
  247. print_insn = print_insn_tci;
  248. #elif defined(__i386__)
  249. disasm_info.mach = bfd_mach_i386_i386;
  250. print_insn = print_insn_i386;
  251. #elif defined(__x86_64__)
  252. disasm_info.mach = bfd_mach_x86_64;
  253. print_insn = print_insn_i386;
  254. #elif defined(_ARCH_PPC)
  255. print_insn = print_insn_ppc;
  256. #elif defined(__alpha__)
  257. print_insn = print_insn_alpha;
  258. #elif defined(__sparc__)
  259. print_insn = print_insn_sparc;
  260. #if defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
  261. disasm_info.mach = bfd_mach_sparc_v9b;
  262. #endif
  263. #elif defined(__arm__)
  264. print_insn = print_insn_arm;
  265. #elif defined(__MIPSEB__)
  266. print_insn = print_insn_big_mips;
  267. #elif defined(__MIPSEL__)
  268. print_insn = print_insn_little_mips;
  269. #elif defined(__m68k__)
  270. print_insn = print_insn_m68k;
  271. #elif defined(__s390__)
  272. print_insn = print_insn_s390;
  273. #elif defined(__hppa__)
  274. print_insn = print_insn_hppa;
  275. #elif defined(__ia64__)
  276. print_insn = print_insn_ia64;
  277. #else
  278. fprintf(out, "0x%lx: Asm output not supported on this arch\n",
  279. (long) code);
  280. return;
  281. #endif
  282. for (pc = (unsigned long)code; size > 0; pc += count, size -= count) {
  283. fprintf(out, "0x%08lx: ", pc);
  284. count = print_insn(pc, &disasm_info);
  285. fprintf(out, "\n");
  286. if (count < 0)
  287. break;
  288. }
  289. }
  290. /* Look up symbol for debugging purpose. Returns "" if unknown. */
  291. const char *lookup_symbol(target_ulong orig_addr)
  292. {
  293. const char *symbol = "";
  294. struct syminfo *s;
  295. for (s = syminfos; s; s = s->next) {
  296. symbol = s->lookup_symbol(s, orig_addr);
  297. if (symbol[0] != '\0') {
  298. break;
  299. }
  300. }
  301. return symbol;
  302. }
  303. #if !defined(CONFIG_USER_ONLY)
  304. #include "monitor.h"
  305. static int monitor_disas_is_physical;
  306. static CPUState *monitor_disas_env;
  307. static int
  308. monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
  309. struct disassemble_info *info)
  310. {
  311. if (monitor_disas_is_physical) {
  312. cpu_physical_memory_read(memaddr, myaddr, length);
  313. } else {
  314. cpu_memory_rw_debug(monitor_disas_env, memaddr,myaddr, length, 0);
  315. }
  316. return 0;
  317. }
  318. static int GCC_FMT_ATTR(2, 3)
  319. monitor_fprintf(FILE *stream, const char *fmt, ...)
  320. {
  321. va_list ap;
  322. va_start(ap, fmt);
  323. monitor_vprintf((Monitor *)stream, fmt, ap);
  324. va_end(ap);
  325. return 0;
  326. }
  327. void monitor_disas(Monitor *mon, CPUState *env,
  328. target_ulong pc, int nb_insn, int is_physical, int flags)
  329. {
  330. int count, i;
  331. struct disassemble_info disasm_info;
  332. int (*print_insn)(bfd_vma pc, disassemble_info *info);
  333. INIT_DISASSEMBLE_INFO(disasm_info, (FILE *)mon, monitor_fprintf);
  334. monitor_disas_env = env;
  335. monitor_disas_is_physical = is_physical;
  336. disasm_info.read_memory_func = monitor_read_memory;
  337. disasm_info.buffer_vma = pc;
  338. #ifdef TARGET_WORDS_BIGENDIAN
  339. disasm_info.endian = BFD_ENDIAN_BIG;
  340. #else
  341. disasm_info.endian = BFD_ENDIAN_LITTLE;
  342. #endif
  343. #if defined(TARGET_I386)
  344. if (flags == 2)
  345. disasm_info.mach = bfd_mach_x86_64;
  346. else if (flags == 1)
  347. disasm_info.mach = bfd_mach_i386_i8086;
  348. else
  349. disasm_info.mach = bfd_mach_i386_i386;
  350. print_insn = print_insn_i386;
  351. #elif defined(TARGET_ARM)
  352. print_insn = print_insn_arm;
  353. #elif defined(TARGET_ALPHA)
  354. print_insn = print_insn_alpha;
  355. #elif defined(TARGET_SPARC)
  356. print_insn = print_insn_sparc;
  357. #ifdef TARGET_SPARC64
  358. disasm_info.mach = bfd_mach_sparc_v9b;
  359. #endif
  360. #elif defined(TARGET_PPC)
  361. #ifdef TARGET_PPC64
  362. disasm_info.mach = bfd_mach_ppc64;
  363. #else
  364. disasm_info.mach = bfd_mach_ppc;
  365. #endif
  366. print_insn = print_insn_ppc;
  367. #elif defined(TARGET_M68K)
  368. print_insn = print_insn_m68k;
  369. #elif defined(TARGET_MIPS)
  370. #ifdef TARGET_WORDS_BIGENDIAN
  371. print_insn = print_insn_big_mips;
  372. #else
  373. print_insn = print_insn_little_mips;
  374. #endif
  375. #elif defined(TARGET_SH4)
  376. disasm_info.mach = bfd_mach_sh4;
  377. print_insn = print_insn_sh;
  378. #elif defined(TARGET_S390X)
  379. disasm_info.mach = bfd_mach_s390_64;
  380. print_insn = print_insn_s390;
  381. #else
  382. monitor_printf(mon, "0x" TARGET_FMT_lx
  383. ": Asm output not supported on this arch\n", pc);
  384. return;
  385. #endif
  386. for(i = 0; i < nb_insn; i++) {
  387. monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
  388. count = print_insn(pc, &disasm_info);
  389. monitor_printf(mon, "\n");
  390. if (count < 0)
  391. break;
  392. pc += count;
  393. }
  394. }
  395. #endif