選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * Intel 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. "rip"
  72. };
  73. uint64_t
  74. ud_syn_rel_target(struct ud *u, struct ud_operand *opr)
  75. {
  76. const uint64_t trunc_mask = 0xffffffffffffffffull >> (64 - u->opr_mode);
  77. switch (opr->size) {
  78. case 8 : return (u->pc + opr->lval.sbyte) & trunc_mask;
  79. case 16: return (u->pc + opr->lval.sword) & trunc_mask;
  80. case 32: return (u->pc + opr->lval.sdword) & trunc_mask;
  81. default: UD_ASSERT(!"invalid relative offset size.");
  82. return 0ull;
  83. }
  84. }
  85. /*
  86. * asmprintf
  87. * Printf style function for printing translated assembly
  88. * output. Returns the number of characters written and
  89. * moves the buffer pointer forward. On an overflow,
  90. * returns a negative number and truncates the output.
  91. */
  92. int
  93. ud_asmprintf(struct ud *u, const char *fmt, ...)
  94. {
  95. int ret;
  96. int avail;
  97. va_list ap;
  98. va_start(ap, fmt);
  99. avail = u->asm_buf_size - u->asm_buf_fill - 1 /* nullchar */;
  100. ret = vsnprintf((char*) u->asm_buf + u->asm_buf_fill, avail, fmt, ap);
  101. if (ret < 0 || ret > avail) {
  102. u->asm_buf_fill = u->asm_buf_size - 1;
  103. } else {
  104. u->asm_buf_fill += ret;
  105. }
  106. va_end(ap);
  107. return ret;
  108. }
  109. void
  110. ud_syn_print_addr(struct ud *u, uint64_t addr)
  111. {
  112. const char *name = NULL;
  113. if (u->sym_resolver) {
  114. int64_t offset = 0;
  115. name = u->sym_resolver(u, addr, &offset);
  116. if (name) {
  117. if (offset) {
  118. ud_asmprintf(u, "%s%+" FMT64 "d", name, offset);
  119. } else {
  120. ud_asmprintf(u, "%s", name);
  121. }
  122. return;
  123. }
  124. }
  125. ud_asmprintf(u, "0x%" FMT64 "x", addr);
  126. }
  127. void
  128. ud_syn_print_imm(struct ud* u, const struct ud_operand *op)
  129. {
  130. uint64_t v;
  131. if (op->_oprcode == OP_sI && op->size != u->opr_mode) {
  132. if (op->size == 8) {
  133. v = (int64_t)op->lval.sbyte;
  134. } else {
  135. UD_ASSERT(op->size == 32);
  136. v = (int64_t)op->lval.sdword;
  137. }
  138. if (u->opr_mode < 64) {
  139. v = v & ((1ull << u->opr_mode) - 1ull);
  140. }
  141. } else {
  142. switch (op->size) {
  143. case 8 : v = op->lval.ubyte; break;
  144. case 16: v = op->lval.uword; break;
  145. case 32: v = op->lval.udword; break;
  146. case 64: v = op->lval.uqword; break;
  147. default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */
  148. }
  149. }
  150. ud_asmprintf(u, "0x%" FMT64 "x", v);
  151. }
  152. void
  153. ud_syn_print_mem_disp(struct ud* u, const struct ud_operand *op, int sign)
  154. {
  155. UD_ASSERT(op->offset != 0);
  156. if (op->base == UD_NONE && op->index == UD_NONE) {
  157. uint64_t v;
  158. UD_ASSERT(op->scale == UD_NONE && op->offset != 8);
  159. /* unsigned mem-offset */
  160. switch (op->offset) {
  161. case 16: v = op->lval.uword; break;
  162. case 32: v = op->lval.udword; break;
  163. case 64: v = op->lval.uqword; break;
  164. default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */
  165. }
  166. ud_asmprintf(u, "0x%" FMT64 "x", v);
  167. } else {
  168. int64_t v;
  169. UD_ASSERT(op->offset != 64);
  170. switch (op->offset) {
  171. case 8 : v = op->lval.sbyte; break;
  172. case 16: v = op->lval.sword; break;
  173. case 32: v = op->lval.sdword; break;
  174. default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */
  175. }
  176. if (v < 0) {
  177. ud_asmprintf(u, "-0x%" FMT64 "x", -v);
  178. } else if (v > 0) {
  179. ud_asmprintf(u, "%s0x%" FMT64 "x", sign? "+" : "", v);
  180. }
  181. }
  182. }
  183. /*
  184. vim: set ts=2 sw=2 expandtab
  185. */