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