1 /* udis86 - libudis86/udis86.c
3 * Copyright (c) 2002-2013 Vivek Thampi
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
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.
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.
31 #if !defined(__UD_STANDALONE__)
35 #endif /* !__UD_STANDALONE__ */
37 static void ud_inp_init(struct ud
*u
);
39 /* =============================================================================
41 * Initializes ud_t object.
42 * =============================================================================
47 memset((void*)u
, 0, sizeof(struct ud
));
49 u
->mnemonic
= UD_Iinvalid
;
51 #ifndef __UD_STANDALONE__
52 ud_set_input_file(u
, stdin
);
53 #endif /* __UD_STANDALONE__ */
55 ud_set_asm_buffer(u
, u
->asm_buf_int
, sizeof(u
->asm_buf_int
));
59 /* =============================================================================
61 * Disassembles one instruction and returns the number of
62 * bytes disassembled. A zero means end of disassembly.
63 * =============================================================================
66 ud_disassemble(struct ud
* u
)
72 if ((len
= ud_decode(u
)) > 0) {
73 if (u
->translator
!= NULL
) {
82 /* =============================================================================
83 * ud_set_mode() - Set Disassemly Mode.
84 * =============================================================================
87 ud_set_mode(struct ud
* u
, uint8_t m
)
92 case 64: u
->dis_mode
= m
; return;
93 default: u
->dis_mode
= 16; return;
97 /* =============================================================================
98 * ud_set_vendor() - Set vendor.
99 * =============================================================================
102 ud_set_vendor(struct ud
* u
, unsigned v
)
105 case UD_VENDOR_INTEL
:
112 u
->vendor
= UD_VENDOR_AMD
;
116 /* =============================================================================
117 * ud_set_pc() - Sets code origin.
118 * =============================================================================
121 ud_set_pc(struct ud
* u
, uint64_t o
)
126 /* =============================================================================
127 * ud_set_syntax() - Sets the output syntax.
128 * =============================================================================
131 ud_set_syntax(struct ud
* u
, void (*t
)(struct ud
*))
136 /* =============================================================================
137 * ud_insn() - returns the disassembled instruction
138 * =============================================================================
141 ud_insn_asm(const struct ud
* u
)
146 /* =============================================================================
147 * ud_insn_offset() - Returns the offset.
148 * =============================================================================
151 ud_insn_off(const struct ud
* u
)
153 return u
->insn_offset
;
157 /* =============================================================================
158 * ud_insn_hex() - Returns hex form of disassembled instruction.
159 * =============================================================================
162 ud_insn_hex(struct ud
* u
)
164 u
->insn_hexcode
[0] = 0;
167 const unsigned char *src_ptr
= ud_insn_ptr(u
);
169 src_hex
= (char*) u
->insn_hexcode
;
170 /* for each byte used to decode instruction */
171 for (i
= 0; i
< ud_insn_len(u
) && i
< sizeof(u
->insn_hexcode
) / 2;
173 sprintf(src_hex
, "%02x", *src_ptr
& 0xFF);
177 return u
->insn_hexcode
;
181 /* =============================================================================
183 * Returns a pointer to buffer containing the bytes that were
185 * =============================================================================
187 extern const uint8_t*
188 ud_insn_ptr(const struct ud
* u
)
190 return (u
->inp_buf
== NULL
) ?
191 u
->inp_sess
: u
->inp_buf
+ (u
->inp_buf_index
- u
->inp_ctr
);
195 /* =============================================================================
197 * Returns the count of bytes disassembled.
198 * =============================================================================
201 ud_insn_len(const struct ud
* u
)
207 /* =============================================================================
209 * Return the operand struct representing the nth operand of
210 * the currently disassembled instruction. Returns NULL if
211 * there's no such operand.
212 * =============================================================================
214 const struct ud_operand
*
215 ud_insn_opr(const struct ud
*u
, unsigned int n
)
217 if (n
> 2 || u
->operand
[n
].type
== UD_NONE
) {
220 return &u
->operand
[n
];
225 /* =============================================================================
227 * Returns non-zero if the given operand is of a segment register type.
228 * =============================================================================
231 ud_opr_is_sreg(const struct ud_operand
*opr
)
233 return opr
->type
== UD_OP_REG
&&
234 opr
->base
>= UD_R_ES
&&
235 opr
->base
<= UD_R_GS
;
239 /* =============================================================================
241 * Returns non-zero if the given operand is of a general purpose
243 * =============================================================================
246 ud_opr_is_gpr(const struct ud_operand
*opr
)
248 return opr
->type
== UD_OP_REG
&&
249 opr
->base
>= UD_R_AL
&&
250 opr
->base
<= UD_R_R15
;
254 /* =============================================================================
255 * ud_set_user_opaque_data
256 * ud_get_user_opaque_data
257 * Get/set user opaqute data pointer
258 * =============================================================================
261 ud_set_user_opaque_data(struct ud
* u
, void* opaque
)
263 u
->user_opaque_data
= opaque
;
267 ud_get_user_opaque_data(const struct ud
*u
)
269 return u
->user_opaque_data
;
273 /* =============================================================================
275 * Allow the user to set an assembler output buffer. If `buf` is NULL,
276 * we switch back to the internal buffer.
277 * =============================================================================
280 ud_set_asm_buffer(struct ud
*u
, char *buf
, size_t size
)
283 ud_set_asm_buffer(u
, u
->asm_buf_int
, sizeof(u
->asm_buf_int
));
286 u
->asm_buf_size
= size
;
291 /* =============================================================================
292 * ud_set_sym_resolver
293 * Set symbol resolver for relative targets used in the translation
296 * The resolver is a function that takes a uint64_t address and returns a
297 * symbolic name for the that address. The function also takes a second
298 * argument pointing to an integer that the client can optionally set to a
299 * non-zero value for offsetted targets. (symbol+offset) The function may
300 * also return NULL, in which case the translator only prints the target
303 * The function pointer maybe NULL which resets symbol resolution.
304 * =============================================================================
307 ud_set_sym_resolver(struct ud
*u
, const char* (*resolver
)(struct ud
*,
311 u
->sym_resolver
= resolver
;
315 /* =============================================================================
317 * Return the current instruction mnemonic.
318 * =============================================================================
320 enum ud_mnemonic_code
321 ud_insn_mnemonic(const struct ud
*u
)
327 /* =============================================================================
329 * Looks up mnemonic code in the mnemonic string table.
330 * Returns NULL if the mnemonic code is invalid.
331 * =============================================================================
334 ud_lookup_mnemonic(enum ud_mnemonic_code c
)
336 if (c
< UD_MAX_MNEMONIC_CODE
) {
337 return ud_mnemonics_str
[c
];
346 * Initializes the input system.
349 ud_inp_init(struct ud
*u
)
354 u
->inp_buf_index
= 0;
358 UD_NON_STANDALONE(u
->inp_file
= NULL
);
362 /* =============================================================================
365 * =============================================================================
368 ud_set_input_hook(register struct ud
* u
, int (*hook
)(struct ud
*))
374 /* =============================================================================
376 * Set buffer as input.
377 * =============================================================================
380 ud_set_input_buffer(register struct ud
* u
, const uint8_t* buf
, size_t len
)
384 u
->inp_buf_size
= len
;
385 u
->inp_buf_index
= 0;
389 #ifndef __UD_STANDALONE__
390 /* =============================================================================
393 * =============================================================================
396 inp_file_hook(struct ud
* u
)
398 return fgetc(u
->inp_file
);
402 ud_set_input_file(register struct ud
* u
, FILE* f
)
405 u
->inp_hook
= inp_file_hook
;
408 #endif /* __UD_STANDALONE__ */
411 /* =============================================================================
413 * Skip n input bytes.
414 * ============================================================================
417 ud_input_skip(struct ud
* u
, size_t n
)
422 if (u
->inp_buf
== NULL
) {
424 int c
= u
->inp_hook(u
);
431 if (n
> u
->inp_buf_size
||
432 u
->inp_buf_index
> u
->inp_buf_size
- n
) {
433 u
->inp_buf_index
= u
->inp_buf_size
;
436 u
->inp_buf_index
+= n
;
441 UDERR(u
, "cannot skip, eoi received\b");
446 /* =============================================================================
448 * Returns non-zero on end-of-input.
449 * =============================================================================
452 ud_input_end(const struct ud
*u
)
457 /* vim:set ts=2 sw=2 expandtab */