disas.c 10 KB

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