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