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/*
32 * Register Table - Order Matters (types.h)!
33 *
34 */
35const char* ud_reg_tab[] =
36{
37  "al",   "cl",   "dl",   "bl",
38  "ah",   "ch",   "dh",   "bh",
39  "spl",  "bpl",  "sil",  "dil",
40  "r8b",  "r9b",  "r10b", "r11b",
41  "r12b", "r13b", "r14b", "r15b",
42
43  "ax",   "cx",   "dx",   "bx",
44  "sp",   "bp",   "si",   "di",
45  "r8w",  "r9w",  "r10w", "r11w",
46  "r12w", "r13w", "r14w", "r15w",
47
48  "eax",  "ecx",  "edx",  "ebx",
49  "esp",  "ebp",  "esi",  "edi",
50  "r8d",  "r9d",  "r10d", "r11d",
51  "r12d", "r13d", "r14d", "r15d",
52
53  "rax",  "rcx",  "rdx",  "rbx",
54  "rsp",  "rbp",  "rsi",  "rdi",
55  "r8",   "r9",   "r10",  "r11",
56  "r12",  "r13",  "r14",  "r15",
57
58  "es",   "cs",   "ss",   "ds",
59  "fs",   "gs",
60
61  "cr0",  "cr1",  "cr2",  "cr3",
62  "cr4",  "cr5",  "cr6",  "cr7",
63  "cr8",  "cr9",  "cr10", "cr11",
64  "cr12", "cr13", "cr14", "cr15",
65
66  "dr0",  "dr1",  "dr2",  "dr3",
67  "dr4",  "dr5",  "dr6",  "dr7",
68  "dr8",  "dr9",  "dr10", "dr11",
69  "dr12", "dr13", "dr14", "dr15",
70
71  "mm0",  "mm1",  "mm2",  "mm3",
72  "mm4",  "mm5",  "mm6",  "mm7",
73
74  "st0",  "st1",  "st2",  "st3",
75  "st4",  "st5",  "st6",  "st7",
76
77  "xmm0", "xmm1", "xmm2", "xmm3",
78  "xmm4", "xmm5", "xmm6", "xmm7",
79  "xmm8", "xmm9", "xmm10", "xmm11",
80  "xmm12", "xmm13", "xmm14", "xmm15",
81
82  "ymm0", "ymm1", "ymm2",   "ymm3",
83  "ymm4", "ymm5", "ymm6",   "ymm7",
84  "ymm8", "ymm9", "ymm10",  "ymm11",
85  "ymm12", "ymm13", "ymm14", "ymm15",
86
87  "rip"
88};
89
90
91uint64_t
92ud_syn_rel_target(struct ud *u, struct ud_operand *opr)
93{
94  const uint64_t trunc_mask = 0xffffffffffffffffull >> (64 - u->opr_mode);
95  switch (opr->size) {
96  case 8 : return (u->pc + opr->lval.sbyte)  & trunc_mask;
97  case 16: return (u->pc + opr->lval.sword)  & trunc_mask;
98  case 32: return (u->pc + opr->lval.sdword) & trunc_mask;
99  default: UD_ASSERT(!"invalid relative offset size.");
100    return 0ull;
101  }
102}
103
104
105/*
106 * asmprintf
107 *    Printf style function for printing translated assembly
108 *    output. Returns the number of characters written and
109 *    moves the buffer pointer forward. On an overflow,
110 *    returns a negative number and truncates the output.
111 */
112int
113ud_asmprintf(struct ud *u, const char *fmt, ...)
114{
115  int ret;
116  int avail;
117  va_list ap;
118  va_start(ap, fmt);
119  avail = u->asm_buf_size - u->asm_buf_fill - 1 /* nullchar */;
120  ret = vsnprintf((char*) u->asm_buf + u->asm_buf_fill, avail, fmt, ap);
121  if (ret < 0 || ret > avail) {
122      u->asm_buf_fill = u->asm_buf_size - 1;
123  } else {
124      u->asm_buf_fill += ret;
125  }
126  va_end(ap);
127  return ret;
128}
129
130
131void
132ud_syn_print_addr(struct ud *u, uint64_t addr)
133{
134  const char *name = NULL;
135  if (u->sym_resolver) {
136    int64_t offset = 0;
137    name = u->sym_resolver(u, addr, &offset);
138    if (name) {
139      if (offset) {
140        ud_asmprintf(u, "%s%+" FMT64 "d", name, offset);
141      } else {
142        ud_asmprintf(u, "%s", name);
143      }
144      return;
145    }
146  }
147  ud_asmprintf(u, "0x%" FMT64 "x", addr);
148}
149
150
151void
152ud_syn_print_imm(struct ud* u, const struct ud_operand *op)
153{
154  uint64_t v;
155  if (op->_oprcode == OP_sI && op->size != u->opr_mode) {
156    if (op->size == 8) {
157      v = (int64_t)op->lval.sbyte;
158    } else {
159      UD_ASSERT(op->size == 32);
160      v = (int64_t)op->lval.sdword;
161    }
162    if (u->opr_mode < 64) {
163      v = v & ((1ull << u->opr_mode) - 1ull);
164    }
165  } else {
166    switch (op->size) {
167    case 8 : v = op->lval.ubyte;  break;
168    case 16: v = op->lval.uword;  break;
169    case 32: v = op->lval.udword; break;
170    case 64: v = op->lval.uqword; break;
171    default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */
172    }
173  }
174  ud_asmprintf(u, "0x%" FMT64 "x", v);
175}
176
177
178void
179ud_syn_print_mem_disp(struct ud* u, const struct ud_operand *op, int sign)
180{
181  UD_ASSERT(op->offset != 0);
182 if (op->base == UD_NONE && op->index == UD_NONE) {
183    uint64_t v;
184    UD_ASSERT(op->scale == UD_NONE && op->offset != 8);
185    /* unsigned mem-offset */
186    switch (op->offset) {
187    case 16: v = op->lval.uword;  break;
188    case 32: v = op->lval.udword; break;
189    case 64: v = op->lval.uqword; break;
190    default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */
191    }
192    ud_asmprintf(u, "0x%" FMT64 "x", v);
193  } else {
194    int64_t v;
195    UD_ASSERT(op->offset != 64);
196    switch (op->offset) {
197    case 8 : v = op->lval.sbyte;  break;
198    case 16: v = op->lval.sword;  break;
199    case 32: v = op->lval.sdword; break;
200    default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */
201    }
202    if (v < 0) {
203      ud_asmprintf(u, "-0x%" FMT64 "x", -v);
204    } else if (v > 0) {
205      ud_asmprintf(u, "%s0x%" FMT64 "x", sign? "+" : "", v);
206    }
207  }
208}
209
210/*
211vim: set ts=2 sw=2 expandtab
212*/
213