host_dasm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <bfd.h>
  6. #include <dis-asm.h>
  7. #include "host_dasm.h"
  8. extern char **g_argv;
  9. static struct disassemble_info di;
  10. #ifdef ARM
  11. #define print_insn_func print_insn_little_arm
  12. #define BFD_ARCH bfd_arch_arm
  13. #define BFD_MACH bfd_mach_arm_4T
  14. #else
  15. #define print_insn_func print_insn_i386_intel
  16. #define BFD_ARCH bfd_arch_i386
  17. #define BFD_MACH bfd_mach_i386_i386_intel_syntax
  18. #endif
  19. /* symbols */
  20. static asymbol **symbols;
  21. static long symcount, symstorage;
  22. static int init_done;
  23. /* Filter out (in place) symbols that are useless for disassembly.
  24. COUNT is the number of elements in SYMBOLS.
  25. Return the number of useful symbols. */
  26. static long
  27. remove_useless_symbols (asymbol **symbols, long count)
  28. {
  29. asymbol **in_ptr = symbols, **out_ptr = symbols;
  30. while (--count >= 0)
  31. {
  32. asymbol *sym = *in_ptr++;
  33. if (sym->name == NULL || sym->name[0] == '\0' || sym->name[0] == '$')
  34. continue;
  35. if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
  36. continue;
  37. if (bfd_is_und_section (sym->section)
  38. || bfd_is_com_section (sym->section))
  39. continue;
  40. if (sym->value + sym->section->vma == 0)
  41. continue;
  42. /*
  43. printf("sym: %08lx %04x %08x v %08x \"%s\"\n",
  44. (unsigned int)sym->value, (unsigned int)sym->flags, (unsigned int)sym->udata.i,
  45. (unsigned int)sym->section->vma, sym->name);
  46. */
  47. *out_ptr++ = sym;
  48. }
  49. return out_ptr - symbols;
  50. }
  51. static void slurp_symtab(const char *filename)
  52. {
  53. bfd *abfd;
  54. symcount = 0;
  55. abfd = bfd_openr(filename, NULL);
  56. if (abfd == NULL) {
  57. fprintf(stderr, "failed to open: %s\n", filename);
  58. goto no_symbols;
  59. }
  60. if (!bfd_check_format(abfd, bfd_object))
  61. goto no_symbols;
  62. if (!(bfd_get_file_flags(abfd) & HAS_SYMS))
  63. goto no_symbols;
  64. symstorage = bfd_get_symtab_upper_bound(abfd);
  65. if (symstorage <= 0)
  66. goto no_symbols;
  67. symbols = malloc(symstorage);
  68. if (symbols == NULL)
  69. goto no_symbols;
  70. symcount = bfd_canonicalize_symtab(abfd, symbols);
  71. if (symcount < 0)
  72. goto no_symbols;
  73. symcount = remove_useless_symbols(symbols, symcount);
  74. // bfd_close(abfd);
  75. return;
  76. no_symbols:
  77. fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd));
  78. if (symbols != NULL)
  79. free(symbols);
  80. symbols = NULL;
  81. if (abfd != NULL)
  82. bfd_close(abfd);
  83. }
  84. static const char *lookup_name(bfd_vma addr)
  85. {
  86. asymbol **sptr = symbols;
  87. int i;
  88. for (i = 0; i < symcount; i++) {
  89. asymbol *sym = *sptr++;
  90. if (addr == sym->value + sym->section->vma)
  91. return sym->name;
  92. }
  93. return NULL;
  94. }
  95. /* Like target_read_memory, but slightly different parameters. */
  96. static int
  97. dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len,
  98. struct disassemble_info *info)
  99. {
  100. memcpy(myaddr, (void *)(int)memaddr, len);
  101. return 0;
  102. }
  103. static void
  104. dis_asm_memory_error(int status, bfd_vma memaddr,
  105. struct disassemble_info *info)
  106. {
  107. fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr);
  108. }
  109. static void
  110. dis_asm_print_address(bfd_vma addr, struct disassemble_info *info)
  111. {
  112. const char *name;
  113. printf("%08x", (int)addr);
  114. name = lookup_name(addr);
  115. if (name != NULL)
  116. printf(" <%s>", name);
  117. }
  118. static int insn_printf(void *f, const char *format, ...)
  119. {
  120. va_list args;
  121. size_t n;
  122. va_start(args, format);
  123. n = vprintf(format, args);
  124. va_end(args);
  125. return n;
  126. }
  127. static void host_dasm_init(void)
  128. {
  129. bfd_init();
  130. slurp_symtab(g_argv[0]);
  131. init_disassemble_info(&di, NULL, insn_printf);
  132. di.flavour = bfd_target_unknown_flavour;
  133. di.memory_error_func = dis_asm_memory_error;
  134. di.print_address_func = dis_asm_print_address;
  135. // di.symbol_at_address_func = dis_asm_symbol_at_address;
  136. di.read_memory_func = dis_asm_read_memory;
  137. di.arch = BFD_ARCH;
  138. di.mach = BFD_MACH;
  139. di.endian = BFD_ENDIAN_LITTLE;
  140. disassemble_init_for_target(&di);
  141. init_done = 1;
  142. }
  143. void host_dasm(void *addr, int len)
  144. {
  145. bfd_vma vma_end, vma = (bfd_vma)(long)addr;
  146. const char *name;
  147. if (!init_done)
  148. host_dasm_init();
  149. vma_end = vma + len;
  150. while (vma < vma_end) {
  151. name = lookup_name(vma);
  152. if (name != NULL)
  153. printf("%s:\n", name);
  154. printf(" %08lx ", (long)vma);
  155. vma += print_insn_func(vma, &di);
  156. printf("\n");
  157. }
  158. }
  159. void host_dasm_new_symbol_(void *addr, const char *name)
  160. {
  161. bfd_vma vma = (bfd_vma)(long)addr;
  162. asymbol *sym, **tmp;
  163. if (!init_done)
  164. host_dasm_init();
  165. if (symbols == NULL)
  166. return;
  167. if (symstorage <= symcount * sizeof(symbols[0])) {
  168. tmp = realloc(symbols, symstorage * 2);
  169. if (tmp == NULL)
  170. return;
  171. symstorage *= 2;
  172. symbols = tmp;
  173. }
  174. symbols[symcount] = calloc(sizeof(*symbols[0]), 1);
  175. if (symbols[symcount] == NULL)
  176. return;
  177. // a HACK (should use correct section), but ohwell
  178. sym = symbols[symcount];
  179. sym->section = symbols[0]->section;
  180. sym->value = vma - sym->section->vma;
  181. sym->name = name;
  182. symcount++;
  183. }