syn.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* udis86 - libudis86/syn.c
  2. *
  3. * Copyright (c) 2002-2013 Vivek Thampi
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "types.h"
  27. #include "decode.h"
  28. #include "syn.h"
  29. #include "udint.h"
  30. /*
  31. * Register Table - Order Matters (types.h)!
  32. *
  33. */
  34. const char* ud_reg_tab[] =
  35. {
  36. "al", "cl", "dl", "bl",
  37. "ah", "ch", "dh", "bh",
  38. "spl", "bpl", "sil", "dil",
  39. "r8b", "r9b", "r10b", "r11b",
  40. "r12b", "r13b", "r14b", "r15b",
  41. "ax", "cx", "dx", "bx",
  42. "sp", "bp", "si", "di",
  43. "r8w", "r9w", "r10w", "r11w",
  44. "r12w", "r13w", "r14w", "r15w",
  45. "eax", "ecx", "edx", "ebx",
  46. "esp", "ebp", "esi", "edi",
  47. "r8d", "r9d", "r10d", "r11d",
  48. "r12d", "r13d", "r14d", "r15d",
  49. "rax", "rcx", "rdx", "rbx",
  50. "rsp", "rbp", "rsi", "rdi",
  51. "r8", "r9", "r10", "r11",
  52. "r12", "r13", "r14", "r15",
  53. "es", "cs", "ss", "ds",
  54. "fs", "gs",
  55. "cr0", "cr1", "cr2", "cr3",
  56. "cr4", "cr5", "cr6", "cr7",
  57. "cr8", "cr9", "cr10", "cr11",
  58. "cr12", "cr13", "cr14", "cr15",
  59. "dr0", "dr1", "dr2", "dr3",
  60. "dr4", "dr5", "dr6", "dr7",
  61. "dr8", "dr9", "dr10", "dr11",
  62. "dr12", "dr13", "dr14", "dr15",
  63. "mm0", "mm1", "mm2", "mm3",
  64. "mm4", "mm5", "mm6", "mm7",
  65. "st0", "st1", "st2", "st3",
  66. "st4", "st5", "st6", "st7",
  67. "xmm0", "xmm1", "xmm2", "xmm3",
  68. "xmm4", "xmm5", "xmm6", "xmm7",
  69. "xmm8", "xmm9", "xmm10", "xmm11",
  70. "xmm12", "xmm13", "xmm14", "xmm15",
  71. "ymm0", "ymm1", "ymm2", "ymm3",
  72. "ymm4", "ymm5", "ymm6", "ymm7",
  73. "ymm8", "ymm9", "ymm10", "ymm11",
  74. "ymm12", "ymm13", "ymm14", "ymm15",
  75. "rip"
  76. };
  77. uint64_t
  78. ud_syn_rel_target(struct ud *u, struct ud_operand *opr)
  79. {
  80. #if 1
  81. const uint64_t trunc_mask = 0xffffffffffffffffull >> (64 - u->adr_mode);
  82. #else
  83. const uint64_t trunc_mask = 0xffffffffffffffffull >> (64 - u->opr_mode);
  84. #endif
  85. switch (opr->size) {
  86. case 8 : return (u->pc + opr->lval.sbyte) & trunc_mask;
  87. case 16: return (u->pc + opr->lval.sword) & trunc_mask;
  88. case 32: return (u->pc + opr->lval.sdword) & trunc_mask;
  89. default: UD_ASSERT(!"invalid relative offset size.");
  90. return 0ull;
  91. }
  92. }
  93. /*
  94. * asmprintf
  95. * Printf style function for printing translated assembly
  96. * output. Returns the number of characters written and
  97. * moves the buffer pointer forward. On an overflow,
  98. * returns a negative number and truncates the output.
  99. */
  100. int
  101. ud_asmprintf(struct ud *u, const char *fmt, ...)
  102. {
  103. int ret;
  104. int avail;
  105. va_list ap;
  106. va_start(ap, fmt);
  107. avail = u->asm_buf_size - u->asm_buf_fill - 1 /* nullchar */;
  108. ret = vsnprintf((char*) u->asm_buf + u->asm_buf_fill, avail, fmt, ap);
  109. if (ret < 0 || ret > avail) {
  110. u->asm_buf_fill = u->asm_buf_size - 1;
  111. } else {
  112. u->asm_buf_fill += ret;
  113. }
  114. va_end(ap);
  115. return ret;
  116. }
  117. void
  118. ud_syn_print_addr(struct ud *u, uint64_t addr)
  119. {
  120. const char *name = NULL;
  121. if (u->sym_resolver) {
  122. int64_t offset = 0;
  123. name = u->sym_resolver(u, addr, &offset);
  124. if (name) {
  125. if (offset) {
  126. ud_asmprintf(u, "%s%+" FMT64 "d", name, offset);
  127. } else {
  128. ud_asmprintf(u, "%s", name);
  129. }
  130. return;
  131. }
  132. }
  133. ud_asmprintf(u, "0x%" FMT64 "x", addr);
  134. }
  135. void
  136. ud_syn_print_imm(struct ud* u, const struct ud_operand *op)
  137. {
  138. uint64_t v;
  139. if (op->_oprcode == OP_sI && op->size != u->opr_mode) {
  140. if (op->size == 8) {
  141. v = (int64_t)op->lval.sbyte;
  142. } else {
  143. UD_ASSERT(op->size == 32);
  144. v = (int64_t)op->lval.sdword;
  145. }
  146. if (u->opr_mode < 64) {
  147. v = v & ((1ull << u->opr_mode) - 1ull);
  148. }
  149. } else {
  150. switch (op->size) {
  151. case 8 : v = op->lval.ubyte; break;
  152. case 16: v = op->lval.uword; break;
  153. case 32: v = op->lval.udword; break;
  154. case 64: v = op->lval.uqword; break;
  155. default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */
  156. }
  157. }
  158. #if 1
  159. if (u->sym_resolver) {
  160. int64_t offset = 0;
  161. const char *name = u->sym_resolver(u, v, &offset);
  162. if (name) {
  163. if (offset) {
  164. ud_asmprintf(u, "%s%+" FMT64 "d", name, offset);
  165. } else {
  166. ud_asmprintf(u, "%s", name);
  167. }
  168. return;
  169. }
  170. }
  171. #endif
  172. ud_asmprintf(u, "0x%" FMT64 "x", v);
  173. }
  174. void
  175. ud_syn_print_mem_disp(struct ud* u, const struct ud_operand *op, int sign)
  176. {
  177. UD_ASSERT(op->offset != 0);
  178. if (op->base == UD_NONE && op->index == UD_NONE) {
  179. uint64_t v;
  180. UD_ASSERT(op->scale == UD_NONE && op->offset != 8);
  181. /* unsigned mem-offset */
  182. switch (op->offset) {
  183. case 16: v = op->lval.uword; break;
  184. case 32: v = op->lval.udword; break;
  185. case 64: v = op->lval.uqword; break;
  186. default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */
  187. }
  188. #if 1
  189. if (u->sym_resolver) {
  190. int64_t offset = 0;
  191. const char *name = u->sym_resolver(u, v, &offset);
  192. if (name) {
  193. if (offset) {
  194. ud_asmprintf(u, "%s%+" FMT64 "d", name, offset);
  195. } else {
  196. ud_asmprintf(u, "%s", name);
  197. }
  198. return;
  199. }
  200. }
  201. #endif
  202. ud_asmprintf(u, "0x%" FMT64 "x", v);
  203. } else {
  204. int64_t v;
  205. UD_ASSERT(op->offset != 64);
  206. switch (op->offset) {
  207. case 8 : v = op->lval.sbyte; break;
  208. case 16: v = op->lval.sword; break;
  209. case 32: v = op->lval.sdword; break;
  210. default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */
  211. }
  212. #if 1
  213. if (u->sym_resolver) {
  214. int64_t offset = 0;
  215. const char *name = u->sym_resolver(u, v, &offset);
  216. if (name) {
  217. if (offset) {
  218. ud_asmprintf(u, "%s%+" FMT64 "d", name, offset);
  219. } else {
  220. ud_asmprintf(u, "%s", name);
  221. }
  222. return;
  223. }
  224. }
  225. #endif
  226. if (v < 0) {
  227. ud_asmprintf(u, "-0x%" FMT64 "x", -v);
  228. } else if (v > 0) {
  229. ud_asmprintf(u, "%s0x%" FMT64 "x", sign? "+" : "", v);
  230. }
  231. }
  232. }
  233. /*
  234. vim: set ts=2 sw=2 expandtab
  235. */