host_dasm.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * DRC host disassembler interface for MIPS/ARM32 for use without binutils
  3. * (C) kub, 2018,2019
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #if defined __mips__
  10. #include "dismips.c"
  11. #define disasm dismips
  12. #elif defined __arm__
  13. #include "disarm.c"
  14. #define disasm disarm
  15. #endif
  16. /* symbols */
  17. typedef struct { const char *name; void *value; } asymbol;
  18. static asymbol **symbols;
  19. static long symcount, symstorage = 8;
  20. static const char *lookup_name(void *addr)
  21. {
  22. asymbol **sptr = symbols;
  23. int i;
  24. for (i = 0; i < symcount; i++) {
  25. asymbol *sym = *sptr++;
  26. if (addr == sym->value)
  27. return sym->name;
  28. }
  29. return NULL;
  30. }
  31. #ifdef disasm
  32. void host_dasm(void *addr, int len)
  33. {
  34. void *end = (char *)addr + len;
  35. const char *name;
  36. char buf[64];
  37. unsigned long insn, symaddr;
  38. while (addr < end) {
  39. name = lookup_name(addr);
  40. if (name != NULL)
  41. printf("%s:\n", name);
  42. insn = *(unsigned long *)addr;
  43. printf(" %08lx %08lx ", (long)addr, insn);
  44. if(disasm((uintptr_t)addr, insn, buf, sizeof(buf), &symaddr))
  45. {
  46. if (symaddr)
  47. name = lookup_name((void *)symaddr);
  48. if (symaddr && name)
  49. printf("%s <%s>\n", buf, name);
  50. else if (symaddr && !name)
  51. printf("%s <unknown>\n", buf);
  52. else
  53. printf("%s\n", buf);
  54. } else
  55. printf("unknown (0x%08lx)\n", insn);
  56. addr = (char *)addr + sizeof(long);
  57. }
  58. }
  59. #else
  60. void host_dasm(void *addr, int len)
  61. {
  62. uint8_t *end = (uint8_t *)addr + len;
  63. char buf[64];
  64. uint8_t *p = addr;
  65. int i = 0, o = 0;
  66. o = snprintf(buf, sizeof(buf), "%p: ", p);
  67. while (p < end) {
  68. o += snprintf(buf+o, sizeof(buf)-o, "%02x ", *p++);
  69. if (++i >= 16) {
  70. buf[o] = '\0';
  71. printf("%s\n", buf);
  72. o = snprintf(buf, sizeof(buf), "%p: ", p);
  73. i = 0;
  74. }
  75. }
  76. if (i) {
  77. buf[o] = '\0';
  78. printf("%s\n", buf);
  79. }
  80. }
  81. #endif
  82. void host_dasm_new_symbol_(void *addr, const char *name)
  83. {
  84. asymbol *sym, **tmp;
  85. if (symbols == NULL)
  86. symbols = malloc(symstorage);
  87. if (symstorage <= symcount * sizeof(symbols[0])) {
  88. tmp = realloc(symbols, symstorage * 2);
  89. if (tmp == NULL)
  90. return;
  91. symstorage *= 2;
  92. symbols = tmp;
  93. }
  94. symbols[symcount] = calloc(sizeof(*symbols[0]), 1);
  95. if (symbols[symcount] == NULL)
  96. return;
  97. // a HACK (should use correct section), but ohwell
  98. sym = symbols[symcount];
  99. sym->value = addr;
  100. sym->name = name;
  101. symcount++;
  102. }