libdis.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. #ifndef LIBDISASM_H
  2. #define LIBDISASM_H
  3. #ifdef WIN32
  4. #include <windows.h>
  5. #endif
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <stdint.h>
  9. /* 'NEW" types
  10. * __________________________________________________________________________*/
  11. #ifndef LIBDISASM_QWORD_H /* do not interfere with qword.h */
  12. #define LIBDISASM_QWORD_H
  13. #ifdef _MSC_VER
  14. typedef __int64 qword_t;
  15. #else
  16. typedef int64_t qword_t;
  17. #endif
  18. #endif
  19. #include <sys/types.h>
  20. #ifdef __cplusplus
  21. //extern "C" {
  22. #endif
  23. /* 'NEW" x86 API
  24. * __________________________________________________________________________*/
  25. /* ========================================= Error Reporting */
  26. /* REPORT CODES
  27. * These are passed to a reporter function passed at initialization.
  28. * Each code determines the type of the argument passed to the reporter;
  29. * this allows the report to recover from errors, or just log them.
  30. */
  31. enum x86_report_codes {
  32. report_disasm_bounds, /* RVA OUT OF BOUNDS : The disassembler could
  33. not disassemble the supplied RVA as it is
  34. out of the range of the buffer. The
  35. application should store the address and
  36. attempt to determine what section of the
  37. binary it is in, then disassemble the
  38. address from the bytes in that section.
  39. data: uint32_t rva */
  40. report_insn_bounds, /* INSTRUCTION OUT OF BOUNDS: The disassembler
  41. could not disassemble the instruction as
  42. the instruction would require bytes beyond
  43. the end of the current buffer. This usually
  44. indicated garbage bytes at the end of a
  45. buffer, or an incorrectly-sized buffer.
  46. data: uint32_t rva */
  47. report_invalid_insn, /* INVALID INSTRUCTION: The disassembler could
  48. not disassemble the instruction as it has an
  49. invalid combination of opcodes and operands.
  50. This will stop automated disassembly; the
  51. application can restart the disassembly
  52. after the invalid instruction.
  53. data: uint32_t rva */
  54. report_unknown
  55. };
  56. /* Disassembly formats:
  57. * AT&T is standard AS/GAS-style: "mnemonic\tsrc, dest, imm"
  58. * Intel is standard MASM/NASM/TASM: "mnemonic\tdest,src, imm"
  59. * Native is tab-delimited: "RVA\tbytes\tmnemonic\tdest\tsrc\timm"
  60. * XML is your typical <insn> ... </insn>
  61. * Raw is addr|offset|size|bytes|prefix... see libdisasm_formats.7
  62. */
  63. enum x86_asm_format {
  64. unknown_syntax = 0, /* never use! */
  65. native_syntax, /* header: 35 bytes */
  66. intel_syntax, /* header: 23 bytes */
  67. att_syntax, /* header: 23 bytes */
  68. xml_syntax, /* header: 679 bytes */
  69. raw_syntax /* header: 172 bytes */
  70. };
  71. /* 'arg' is optional arbitrary data provided by the code passing the
  72. * callback -- for example, it could be 'this' or 'self' in OOP code.
  73. * 'code' is provided by libdisasm, it is one of the above
  74. * 'data' is provided by libdisasm and is context-specific, per the enums */
  75. typedef void (*DISASM_REPORTER)( enum x86_report_codes code,
  76. void *data, void *arg );
  77. /* ========================================= Libdisasm Management Routines */
  78. enum x86_options { /* these can be ORed together */
  79. opt_none= 0,
  80. opt_ignore_nulls=1, /* ignore sequences of > 4 NULL bytes */
  81. opt_16_bit=2, /* 16-bit/DOS disassembly */
  82. opt_att_mnemonics=4 /* use AT&T syntax names for alternate opcode mnemonics */
  83. };
  84. /* ========================================= Instruction Representation */
  85. /* these defines are only intended for use in the array decl's */
  86. #define MAX_REGNAME 8
  87. #define MAX_PREFIX_STR 32
  88. #define MAX_MNEM_STR 16
  89. #define MAX_INSN_SIZE 20 /* same as in i386.h */
  90. #define MAX_OP_STRING 32 /* max possible operand size in string form */
  91. #define MAX_OP_RAW_STRING 64 /* max possible operand size in raw form */
  92. #define MAX_OP_XML_STRING 256 /* max possible operand size in xml form */
  93. #define MAX_NUM_OPERANDS 8 /* max # implicit and explicit operands */
  94. /* in these, the '2 *' is arbitrary: the max # of operands should require
  95. * more space than the rest of the insn */
  96. #define MAX_INSN_STRING 512 /* 2 * 8 * MAX_OP_STRING */
  97. #define MAX_INSN_RAW_STRING 1024 /* 2 * 8 * MAX_OP_RAW_STRING */
  98. #define MAX_INSN_XML_STRING 4096 /* 2 * 8 * MAX_OP_XML_STRING */
  99. enum x86_reg_type { /* NOTE: these may be ORed together */
  100. reg_undef = 0x00000, // used only in ia32_reg_table initializater
  101. reg_gen = 0x00001, /* general purpose */
  102. reg_in = 0x00002, /* incoming args, ala RISC */
  103. reg_out = 0x00004, /* args to calls, ala RISC */
  104. reg_local = 0x00008, /* local vars, ala RISC */
  105. reg_fpu = 0x00010, /* FPU data register */
  106. reg_seg = 0x00020, /* segment register */
  107. reg_simd = 0x00040, /* SIMD/MMX reg */
  108. reg_sys = 0x00080, /* restricted/system register */
  109. reg_sp = 0x00100, /* stack pointer */
  110. reg_fp = 0x00200, /* frame pointer */
  111. reg_pc = 0x00400, /* program counter */
  112. reg_retaddr = 0x00800, /* return addr for func */
  113. reg_cond = 0x01000, /* condition code / flags */
  114. reg_zero = 0x02000, /* zero register, ala RISC */
  115. reg_ret = 0x04000, /* return value */
  116. reg_src = 0x10000, /* array/rep source */
  117. reg_dest = 0x20000, /* array/rep destination */
  118. reg_count = 0x40000 /* array/rep/loop counter */
  119. };
  120. /* x86_reg_t : an X86 CPU register */
  121. struct x86_reg_t {
  122. char name[MAX_REGNAME];
  123. enum x86_reg_type type; /* what register is used for */
  124. unsigned int size; /* size of register in bytes */
  125. unsigned int id; /* register ID #, for quick compares */
  126. unsigned int alias; /* ID of reg this is an alias for */
  127. unsigned int shift; /* amount to shift aliased reg by */
  128. x86_reg_t * aliased_reg( ) {
  129. x86_reg_t * reg = (x86_reg_t * )calloc( sizeof(x86_reg_t), 1 );
  130. reg->x86_reg_from_id( id );
  131. return reg;
  132. }
  133. void x86_reg_from_id( unsigned int _id);
  134. };
  135. /* x86_ea_t : an X86 effective address (address expression) */
  136. typedef struct {
  137. unsigned int scale; /* scale factor */
  138. x86_reg_t index, base; /* index, base registers */
  139. int32_t disp; /* displacement */
  140. char disp_sign; /* is negative? 1/0 */
  141. char disp_size; /* 0, 1, 2, 4 */
  142. } x86_ea_t;
  143. /* x86_absolute_t : an X86 segment:offset address (descriptor) */
  144. typedef struct {
  145. unsigned short segment; /* loaded directly into CS */
  146. union {
  147. unsigned short off16; /* loaded directly into IP */
  148. uint32_t off32; /* loaded directly into EIP */
  149. } offset;
  150. } x86_absolute_t;
  151. enum x86_op_type { /* mutually exclusive */
  152. op_unused = 0, /* empty/unused operand: should never occur */
  153. op_register = 1, /* CPU register */
  154. op_immediate = 2, /* Immediate Value */
  155. op_relative_near = 3, /* Relative offset from IP */
  156. op_relative_far = 4, /* Relative offset from IP */
  157. op_absolute = 5, /* Absolute address (ptr16:32) */
  158. op_expression = 6, /* Address expression (scale/index/base/disp) */
  159. op_offset = 7, /* Offset from start of segment (m32) */
  160. op_unknown
  161. };
  162. #define x86_optype_is_address( optype ) \
  163. ( optype == op_absolute || optype == op_offset )
  164. #define x86_optype_is_relative( optype ) \
  165. ( optype == op_relative_near || optype == op_relative_far )
  166. #define x86_optype_is_memory( optype ) \
  167. ( optype > op_immediate && optype < op_unknown )
  168. enum x86_op_datatype { /* these use Intel's lame terminology */
  169. op_byte = 1, /* 1 byte integer */
  170. op_word = 2, /* 2 byte integer */
  171. op_dword = 3, /* 4 byte integer */
  172. op_qword = 4, /* 8 byte integer */
  173. op_dqword = 5, /* 16 byte integer */
  174. op_sreal = 6, /* 4 byte real (single real) */
  175. op_dreal = 7, /* 8 byte real (double real) */
  176. op_extreal = 8, /* 10 byte real (extended real) */
  177. op_bcd = 9, /* 10 byte binary-coded decimal */
  178. op_ssimd = 10, /* 16 byte : 4 packed single FP (SIMD, MMX) */
  179. op_dsimd = 11, /* 16 byte : 2 packed double FP (SIMD, MMX) */
  180. op_sssimd = 12, /* 4 byte : scalar single FP (SIMD, MMX) */
  181. op_sdsimd = 13, /* 8 byte : scalar double FP (SIMD, MMX) */
  182. op_descr32 = 14, /* 6 byte Intel descriptor 2:4 */
  183. op_descr16 = 15, /* 4 byte Intel descriptor 2:2 */
  184. op_pdescr32 = 16, /* 6 byte Intel pseudo-descriptor 32:16 */
  185. op_pdescr16 = 17, /* 6 byte Intel pseudo-descriptor 8:24:16 */
  186. op_bounds16 = 18, /* signed 16:16 lower:upper bounds */
  187. op_bounds32 = 19, /* signed 32:32 lower:upper bounds */
  188. op_fpuenv16 = 20, /* 14 byte FPU control/environment data */
  189. op_fpuenv32 = 21, /* 28 byte FPU control/environment data */
  190. op_fpustate16 = 22, /* 94 byte FPU state (env & reg stack) */
  191. op_fpustate32 = 23, /* 108 byte FPU state (env & reg stack) */
  192. op_fpregset = 24, /* 512 bytes: register set */
  193. op_fpreg = 25, /* FPU register */
  194. op_none = 0xFF /* operand without a datatype (INVLPG) */
  195. };
  196. enum x86_op_access { /* ORed together */
  197. op_read = 1,
  198. op_write = 2,
  199. op_execute = 4
  200. };
  201. struct x86_op_flags { /* ORed together, but segs are mutually exclusive */
  202. union {
  203. unsigned int op_signed:1, /* signed integer */
  204. op_string:1,// = 2, /* possible string or array */
  205. op_constant:1,// = 4, /* symbolic constant */
  206. op_pointer:1,// = 8, /* operand points to a memory address */
  207. op_sysref:1,// = 0x010, /* operand is a syscall number */
  208. op_implied:1,// = 0x020, /* operand is implicit in the insn */
  209. op_hardcode:1,// = 0x40, /* operand is hardcoded in insn definition */
  210. /* NOTE: an 'implied' operand is one which can be considered a side
  211. * effect of the insn, e.g. %esp being modified by PUSH or POP. A
  212. * 'hard-coded' operand is one which is specified in the instruction
  213. * definition, e.g. %es:%edi in MOVSB or 1 in ROL Eb, 1. The difference
  214. * is that hard-coded operands are printed by disassemblers and are
  215. * required to re-assemble, while implicit operands are invisible. */
  216. op_seg : 3;
  217. unsigned int whole;
  218. };
  219. enum {
  220. op_es_seg = 0x100, /* ES segment override */
  221. op_cs_seg = 0x200, /* CS segment override */
  222. op_ss_seg = 0x300, /* SS segment override */
  223. op_ds_seg = 0x400, /* DS segment override */
  224. op_fs_seg = 0x500, /* FS segment override */
  225. op_gs_seg = 0x600 /* GS segment override */
  226. };
  227. };
  228. /* x86_op_t : an X86 instruction operand */
  229. struct x86_op_t{
  230. friend struct x86_insn_t;
  231. enum x86_op_type type; /* operand type */
  232. enum x86_op_datatype datatype; /* operand size */
  233. enum x86_op_access access; /* operand access [RWX] */
  234. x86_op_flags flags; /* misc flags */
  235. union {
  236. /* sizeof will have to work on these union members! */
  237. /* immediate values */
  238. char sbyte;
  239. short sword;
  240. int32_t sdword;
  241. qword_t sqword;
  242. unsigned char byte;
  243. unsigned short word;
  244. uint32_t dword;
  245. qword_t qword;
  246. float sreal;
  247. double dreal;
  248. /* misc large/non-native types */
  249. unsigned char extreal[10];
  250. unsigned char bcd[10];
  251. qword_t dqword[2];
  252. unsigned char simd[16];
  253. unsigned char fpuenv[28];
  254. /* offset from segment */
  255. uint32_t offset;
  256. /* ID of CPU register */
  257. x86_reg_t reg;
  258. /* offsets from current insn */
  259. char relative_near;
  260. int32_t relative_far;
  261. /* segment:offset */
  262. x86_absolute_t absolute;
  263. /* effective address [expression] */
  264. x86_ea_t expression;
  265. } data;
  266. /* this is needed to make formatting operands more sane */
  267. void * insn; /* pointer to x86_insn_t owning operand */
  268. size_t size()
  269. {
  270. return operand_size();
  271. }
  272. /* get size of operand data in bytes */
  273. size_t operand_size();
  274. /* format (sprintf) an operand into 'buf' using specified syntax */
  275. int x86_format_operand(char *buf, int len, enum x86_asm_format format );
  276. bool is_address( ) {
  277. return ( type == op_absolute || type == op_offset );
  278. }
  279. bool is_relative( ) {
  280. return ( type == op_relative_near || type == op_relative_far );
  281. }
  282. char * format( enum x86_asm_format format );
  283. x86_op_t * copy()
  284. {
  285. x86_op_t *op = (x86_op_t *) calloc( sizeof(x86_op_t), 1 );
  286. if ( op ) {
  287. memcpy( op, this, sizeof(x86_op_t) );
  288. }
  289. return op;
  290. }
  291. int32_t long_from_operand();
  292. private:
  293. };
  294. /* Linked list of x86_op_t; provided for manual traversal of the operand
  295. * list in an insn. Users wishing to add operands to this list, e.g. to add
  296. * implicit operands, should use x86_operand_new in x86_operand_list.h */
  297. struct x86_oplist_t {
  298. x86_op_t op;
  299. struct x86_oplist_t *next;
  300. };
  301. enum x86_insn_type {
  302. insn_invalid = 0, /* invalid instruction */
  303. /* insn_controlflow */
  304. insn_jmp = 0x1001,
  305. insn_jcc = 0x1002,
  306. insn_call = 0x1003,
  307. insn_callcc = 0x1004,
  308. insn_return = 0x1005,
  309. /* insn_arithmetic */
  310. insn_add = 0x2001,
  311. insn_sub = 0x2002,
  312. insn_mul = 0x2003,
  313. insn_div = 0x2004,
  314. insn_inc = 0x2005,
  315. insn_dec = 0x2006,
  316. insn_shl = 0x2007,
  317. insn_shr = 0x2008,
  318. insn_rol = 0x2009,
  319. insn_ror = 0x200A,
  320. /* insn_logic */
  321. insn_and = 0x3001,
  322. insn_or = 0x3002,
  323. insn_xor = 0x3003,
  324. insn_not = 0x3004,
  325. insn_neg = 0x3005,
  326. /* insn_stack */
  327. insn_push = 0x4001,
  328. insn_pop = 0x4002,
  329. insn_pushregs = 0x4003,
  330. insn_popregs = 0x4004,
  331. insn_pushflags = 0x4005,
  332. insn_popflags = 0x4006,
  333. insn_enter = 0x4007,
  334. insn_leave = 0x4008,
  335. /* insn_comparison */
  336. insn_test = 0x5001,
  337. insn_cmp = 0x5002,
  338. /* insn_move */
  339. insn_mov = 0x6001, /* move */
  340. insn_movcc = 0x6002, /* conditional move */
  341. insn_xchg = 0x6003, /* exchange */
  342. insn_xchgcc = 0x6004, /* conditional exchange */
  343. /* insn_string */
  344. insn_strcmp = 0x7001,
  345. insn_strload = 0x7002,
  346. insn_strmov = 0x7003,
  347. insn_strstore = 0x7004,
  348. insn_translate = 0x7005, /* xlat */
  349. /* insn_bit_manip */
  350. insn_bittest = 0x8001,
  351. insn_bitset = 0x8002,
  352. insn_bitclear = 0x8003,
  353. /* insn_flag_manip */
  354. insn_clear_carry = 0x9001,
  355. insn_clear_zero = 0x9002,
  356. insn_clear_oflow = 0x9003,
  357. insn_clear_dir = 0x9004,
  358. insn_clear_sign = 0x9005,
  359. insn_clear_parity = 0x9006,
  360. insn_set_carry = 0x9007,
  361. insn_set_zero = 0x9008,
  362. insn_set_oflow = 0x9009,
  363. insn_set_dir = 0x900A,
  364. insn_set_sign = 0x900B,
  365. insn_set_parity = 0x900C,
  366. insn_tog_carry = 0x9010,
  367. insn_tog_zero = 0x9020,
  368. insn_tog_oflow = 0x9030,
  369. insn_tog_dir = 0x9040,
  370. insn_tog_sign = 0x9050,
  371. insn_tog_parity = 0x9060,
  372. /* insn_fpu */
  373. insn_fmov = 0xA001,
  374. insn_fmovcc = 0xA002,
  375. insn_fneg = 0xA003,
  376. insn_fabs = 0xA004,
  377. insn_fadd = 0xA005,
  378. insn_fsub = 0xA006,
  379. insn_fmul = 0xA007,
  380. insn_fdiv = 0xA008,
  381. insn_fsqrt = 0xA009,
  382. insn_fcmp = 0xA00A,
  383. insn_fcos = 0xA00C,
  384. insn_fldpi = 0xA00D,
  385. insn_fldz = 0xA00E,
  386. insn_ftan = 0xA00F,
  387. insn_fsine = 0xA010,
  388. insn_fsys = 0xA020,
  389. /* insn_interrupt */
  390. insn_int = 0xD001,
  391. insn_intcc = 0xD002, /* not present in x86 ISA */
  392. insn_iret = 0xD003,
  393. insn_bound = 0xD004,
  394. insn_debug = 0xD005,
  395. insn_trace = 0xD006,
  396. insn_invalid_op = 0xD007,
  397. insn_oflow = 0xD008,
  398. /* insn_system */
  399. insn_halt = 0xE001,
  400. insn_in = 0xE002, /* input from port/bus */
  401. insn_out = 0xE003, /* output to port/bus */
  402. insn_cpuid = 0xE004,
  403. insn_lmsw = 0xE005,
  404. insn_smsw = 0xE006,
  405. insn_clts = 0xE007,
  406. /* insn_other */
  407. insn_nop = 0xF001,
  408. insn_bcdconv = 0xF002, /* convert to or from BCD */
  409. insn_szconv = 0xF003 /* change size of operand */
  410. };
  411. /* These flags specify special characteristics of the instruction, such as
  412. * whether the inatruction is privileged or whether it serializes the
  413. * pipeline.
  414. * NOTE : These may not be accurate for all instructions; updates to the
  415. * opcode tables have not been completed. */
  416. enum x86_insn_note {
  417. insn_note_ring0 = 1, /* Only available in ring 0 */
  418. insn_note_smm = 2, /* "" in System Management Mode */
  419. insn_note_serial = 4, /* Serializing instruction */
  420. insn_note_nonswap = 8, /* Does not swap arguments in att-style formatting */
  421. insn_note_nosuffix = 16 /* Does not have size suffix in att-style formatting */
  422. };
  423. /* This specifies what effects the instruction has on the %eflags register */
  424. enum x86_eflags
  425. {
  426. insn_eflag_carry,
  427. insn_eflag_zero,
  428. insn_eflag_overflow,
  429. insn_eflag_direction,
  430. insn_eflag_sign,
  431. insn_eflag_parity
  432. };
  433. enum x86_flag_status {
  434. insn_carry_set = 0x1, /* CF */
  435. insn_zero_set = 0x2, /* ZF */
  436. insn_oflow_set = 0x4, /* OF */
  437. insn_dir_set = 0x8, /* DF */
  438. insn_sign_set = 0x10, /* SF */
  439. insn_parity_set = 0x20, /* PF */
  440. insn_carry_or_zero_set = 0x40,
  441. insn_zero_set_or_sign_ne_oflow = 0x80,
  442. insn_carry_clear = 0x100,
  443. insn_zero_clear = 0x200,
  444. insn_oflow_clear = 0x400,
  445. insn_dir_clear = 0x800,
  446. insn_sign_clear = 0x1000,
  447. insn_parity_clear = 0x2000,
  448. insn_sign_eq_oflow = 0x4000,
  449. insn_sign_ne_oflow = 0x8000
  450. };
  451. /* The CPU model in which the insturction first appeared; this can be used
  452. * to mask out instructions appearing in earlier or later models or to
  453. * check the portability of a binary.
  454. * NOTE : These may not be accurate for all instructions; updates to the
  455. * opcode tables have not been completed. */
  456. enum x86_insn_cpu {
  457. cpu_8086 = 1, /* Intel */
  458. cpu_80286 = 2,
  459. cpu_80386 = 3,
  460. cpu_80387 = 4,
  461. cpu_80486 = 5,
  462. cpu_pentium = 6,
  463. cpu_pentiumpro = 7,
  464. cpu_pentium2 = 8,
  465. cpu_pentium3 = 9,
  466. cpu_pentium4 = 10,
  467. cpu_k6 = 16, /* AMD */
  468. cpu_k7 = 32,
  469. cpu_athlon = 48
  470. };
  471. /* CPU ISA subsets: These are derived from the Instruction Groups in
  472. * Intel Vol 1 Chapter 5; they represent subsets of the IA32 ISA but
  473. * do not reflect the 'type' of the instruction in the same way that
  474. * x86_insn_group does. In short, these are AMD/Intel's somewhat useless
  475. * designations.
  476. * NOTE : These may not be accurate for all instructions; updates to the
  477. * opcode tables have not been completed. */
  478. enum x86_insn_isa {
  479. isa_gp = 1, /* general purpose */
  480. isa_fp = 2, /* floating point */
  481. isa_fpumgt = 3, /* FPU/SIMD management */
  482. isa_mmx = 4, /* Intel MMX */
  483. isa_sse1 = 5, /* Intel SSE SIMD */
  484. isa_sse2 = 6, /* Intel SSE2 SIMD */
  485. isa_sse3 = 7, /* Intel SSE3 SIMD */
  486. isa_3dnow = 8, /* AMD 3DNow! SIMD */
  487. isa_sys = 9 /* system instructions */
  488. };
  489. enum x86_insn_prefix {
  490. insn_no_prefix = 0,
  491. insn_rep_zero = 1, /* REPZ and REPE */
  492. insn_rep_notzero = 2, /* REPNZ and REPNZ */
  493. insn_lock = 4 /* LOCK: */
  494. };
  495. /* TODO: maybe provide insn_new/free(), and have disasm return new insn_t */
  496. /* FOREACH types: these are used to limit the foreach results to
  497. * operands which match a certain "type" (implicit or explicit)
  498. * or which are accessed in certain ways (e.g. read or write). Note
  499. * that this operates on the operand list of single instruction, so
  500. * specifying the 'real' operand type (register, memory, etc) is not
  501. * useful. Note also that by definition Execute Access implies Read
  502. * Access and implies Not Write Access.
  503. * The "type" (implicit or explicit) and the access method can
  504. * be ORed together, e.g. op_wo | op_explicit */
  505. enum x86_op_foreach_type {
  506. op_any = 0, /* ALL operands (explicit, implicit, rwx) */
  507. op_dest = 1, /* operands with Write access */
  508. op_src = 2, /* operands with Read access */
  509. op_ro = 3, /* operands with Read but not Write access */
  510. op_wo = 4, /* operands with Write but not Read access */
  511. op_xo = 5, /* operands with Execute access */
  512. op_rw = 6, /* operands with Read AND Write access */
  513. op_implicit = 0x10, /* operands that are implied by the opcode */
  514. op_explicit = 0x20 /* operands that are not side-effects */
  515. };
  516. /* Operand FOREACH callback: 'arg' is an abritrary parameter passed to the
  517. * foreach routine, 'insn' is the x86_insn_t whose operands are being
  518. * iterated over, and 'op' is the current x86_op_t */
  519. struct x86_insn_t;
  520. struct ia32_insn_t;
  521. typedef void (*x86_operand_fn)(x86_op_t *op, x86_insn_t *insn, void *arg);
  522. /* x86_insn_t : an X86 instruction */
  523. struct x86_insn_t {
  524. enum x86_insn_group {
  525. insn_none = 0, /* invalid instruction */
  526. insn_controlflow = 1,
  527. insn_arithmetic = 2,
  528. insn_logic = 3,
  529. insn_stack = 4,
  530. insn_comparison = 5,
  531. insn_move = 6,
  532. insn_string = 7,
  533. insn_bit_manip = 8,
  534. insn_flag_manip = 9,
  535. insn_fpu = 10,
  536. insn_interrupt = 13,
  537. insn_system = 14,
  538. insn_other = 15
  539. };
  540. private:
  541. void x86_oplist_append(x86_oplist_t *op);
  542. public:
  543. /* information about the instruction */
  544. uint32_t addr; /* load address */
  545. uint32_t offset; /* offset into file/buffer */
  546. x86_insn_group group; /* meta-type, e.g. INS_EXEC */
  547. x86_insn_type type; /* type, e.g. INS_BRANCH */
  548. x86_insn_note note; /* note, e.g. RING0 */
  549. unsigned char bytes[MAX_INSN_SIZE];
  550. unsigned char size; /* size of insn in bytes */
  551. /* 16/32-bit mode settings */
  552. unsigned char addr_size; /* default address size : 2 or 4 */
  553. unsigned char op_size; /* default operand size : 2 or 4 */
  554. /* CPU/instruction set */
  555. enum x86_insn_cpu cpu;
  556. enum x86_insn_isa isa;
  557. /* flags */
  558. enum x86_flag_status flags_set; /* flags set or tested by insn */
  559. enum x86_flag_status flags_tested;
  560. bool containsFlag(x86_eflags flg,x86_flag_status in);
  561. /* stack */
  562. unsigned char stack_mod; /* 0 or 1 : is the stack modified? */
  563. int32_t stack_mod_val; /* val stack is modified by if known */
  564. /* the instruction proper */
  565. enum x86_insn_prefix prefix; /* prefixes ORed together */
  566. char prefix_string[MAX_PREFIX_STR]; /* prefixes [might be truncated] */
  567. char mnemonic[MAX_MNEM_STR];
  568. x86_oplist_t *operands; /* list of explicit/implicit operands */
  569. size_t operand_count; /* total number of operands */
  570. size_t explicit_count; /* number of explicit operands */
  571. /* convenience fields for user */
  572. void *block; /* code block containing this insn */
  573. void *function; /* function containing this insn */
  574. int tag; /* tag the insn as seen/processed */
  575. x86_op_t *x86_operand_new();
  576. /* convenience routine: returns count of operands matching 'type' */
  577. size_t x86_operand_count( enum x86_op_foreach_type type );
  578. /* accessor functions for the operands */
  579. x86_op_t * x86_operand_1st( );
  580. x86_op_t * x86_operand_2nd( );
  581. x86_op_t * x86_operand_3rd( );
  582. x86_op_t * get_dest();
  583. int32_t x86_get_rel_offset( );
  584. x86_op_t * x86_get_branch_target( );
  585. x86_op_t * x86_get_imm( );
  586. /* More accessor fuctions, this time for user-defined info... */
  587. uint8_t * x86_get_raw_imm( );
  588. /* set the address (usually RVA) of the insn */
  589. void x86_set_insn_addr( uint32_t addr );
  590. /* format (sprintf) an instruction mnemonic into 'buf' using specified syntax */
  591. int x86_format_mnemonic( char *buf, int len, enum x86_asm_format format);
  592. int x86_format_insn( char *buf, int len, enum x86_asm_format);
  593. void x86_oplist_free( );
  594. /* returns 0 if an instruction is invalid, 1 if valid */
  595. bool is_valid( );
  596. uint32_t x86_get_address( );
  597. void make_invalid(unsigned char *buf);
  598. /* instruction tagging: these routines allow the programmer to mark
  599. * instructions as "seen" in a DFS, for example. libdisasm does not use
  600. * the tag field.*/
  601. /* set insn->tag to 1 */
  602. void x86_tag_insn( );
  603. /* return insn->tag */
  604. int x86_insn_is_tagged();
  605. /* set insn->tag to 0 */
  606. void x86_untag_insn();
  607. /* Operand foreach: invokes 'func' with 'insn' and 'arg' as arguments. The
  608. * 'type' parameter is used to select only operands matching specific
  609. * criteria. */
  610. int x86_operand_foreach( x86_operand_fn func, void *arg, enum x86_op_foreach_type type );
  611. /* set the offset (usually offset into file) of the insn */
  612. void x86_set_insn_offset( unsigned int offset );
  613. /* set a pointer to the function owning the instruction. The
  614. * type of 'func' is user-defined; libdisasm does not use the func field. */
  615. void x86_set_insn_function( void * func );
  616. /* set a pointer to the block of code owning the instruction. The
  617. * type of 'block' is user-defined; libdisasm does not use the block field. */
  618. void x86_set_insn_block( void * block );
  619. private:
  620. };
  621. class Ia32_Decoder
  622. {
  623. x86_insn_t *m_decoded;
  624. void handle_insn_metadata( ia32_insn_t *raw_insn );
  625. void ia32_stack_mod();
  626. void ia32_handle_cpu( unsigned int cpu );
  627. void ia32_handle_mnemtype(unsigned int mnemtype);
  628. void ia32_handle_notes(unsigned int notes);
  629. void ia32_handle_eflags( unsigned int eflags);
  630. void ia32_handle_prefix( unsigned int prefixes );
  631. size_t ia32_decode_insn( unsigned char *buf, size_t buf_len, ia32_insn_t *raw_insn, unsigned int prefixes );
  632. size_t ia32_decode_operand( unsigned char *buf, size_t buf_len, unsigned int raw_op, unsigned int raw_flags, unsigned int prefixes, unsigned char modrm );
  633. size_t decode_operand_size( unsigned int op_type, x86_op_t *op );
  634. size_t decode_operand_value( unsigned char *buf, size_t buf_len, x86_op_t *op, unsigned int addr_meth, size_t op_size, unsigned int op_value, unsigned char modrm, size_t gen_regs );
  635. size_t ia32_modrm_decode( unsigned char *buf, unsigned int buf_len, x86_op_t *op, size_t gen_regs );
  636. unsigned int ia32_insn_implicit_ops( unsigned int impl_idx );
  637. size_t handle_insn_suffix( unsigned char *buf, size_t buf_len, ia32_insn_t *raw_insn );
  638. public:
  639. size_t ia32_disasm_addr( unsigned char * buf, size_t buf_len);
  640. void decode_into(x86_insn_t *ins) {m_decoded=ins;}
  641. };
  642. /* DISASSEMBLY ROUTINES
  643. * Canonical order of arguments is
  644. * (buf, buf_len, buf_rva, offset, len, insn, func, arg, resolve_func)
  645. * ...but of course all of these are not used at the same time.
  646. */
  647. class X86_Disasm
  648. {
  649. public:
  650. /* Function prototype for caller-supplied callback routine
  651. * These callbacks are intended to process 'insn' further, e.g. by
  652. * adding it to a linked list, database, etc */
  653. typedef void (*DISASM_CALLBACK)( x86_insn_t *insn, void * arg );
  654. /* Function prototype for caller-supplied address resolver.
  655. * This routine is used to determine the rva to disassemble next, given
  656. * the 'dest' operand of a jump/call. This allows the caller to resolve
  657. * jump/call targets stored in a register or on the stack, and also allows
  658. * the caller to prevent endless loops by checking if an address has
  659. * already been disassembled. If an address cannot be resolved from the
  660. * operand, or if the address has already been disassembled, this routine
  661. * should return -1; in all other cases the RVA to be disassembled next
  662. * should be returned. */
  663. typedef int32_t (*DISASM_RESOLVER)( x86_op_t *op, x86_insn_t * current_insn,
  664. void *arg );
  665. protected:
  666. DISASM_REPORTER __x86_reporter_func;
  667. void * __x86_reporter_arg;
  668. Ia32_Decoder m_decoder;
  669. public:
  670. X86_Disasm( x86_options options=opt_none,DISASM_REPORTER reporter=0, void *arg=0 ) :
  671. __x86_reporter_func(reporter),
  672. __x86_reporter_arg(arg) {
  673. x86_init( options,reporter,arg);
  674. }
  675. /* management routines */
  676. /* 'arg' is caller-specific data which is passed as the first argument
  677. * to the reporter callback routine */
  678. int x86_init( x86_options options, DISASM_REPORTER reporter, void *arg);
  679. void x86_set_reporter( DISASM_REPORTER reporter, void *arg);
  680. void x86_set_options( x86_options options );
  681. x86_options x86_get_options( void );
  682. int x86_cleanup(void);
  683. /* x86_disasm: Disassemble a single instruction from a buffer of bytes.
  684. * Returns size of instruction in bytes.
  685. * Caller is responsible for calling x86_oplist_free() on
  686. * a reused "insn" to avoid leaking memory when calling this
  687. * function repeatedly.
  688. * buf : Buffer of bytes to disassemble
  689. * buf_len : Length of the buffer
  690. * buf_rva : Load address of the start of the buffer
  691. * offset : Offset in buffer to disassemble
  692. * insn : Structure to fill with disassembled instruction
  693. */
  694. unsigned int x86_disasm( unsigned char *buf, unsigned int buf_len,
  695. uint32_t buf_rva, unsigned int offset,
  696. x86_insn_t * insn );
  697. /* x86_disasm_range: Sequential disassembly of a range of bytes in a buffer,
  698. * invoking a callback function each time an instruction
  699. * is successfully disassembled. The 'range' refers to the
  700. * bytes between 'offset' and 'offset + len' in the buffer;
  701. * 'len' is assumed to be less than the length of the buffer.
  702. * Returns number of instructions processed.
  703. * buf : Buffer of bytes to disassemble (e.g. .text section)
  704. * buf_rva : Load address of buffer (e.g. ELF Virtual Address)
  705. * offset : Offset in buffer to start disassembly at
  706. * len : Number of bytes to disassemble
  707. * func : Callback function to invoke (may be NULL)
  708. * arg : Arbitrary data to pass to callback (may be NULL)
  709. */
  710. unsigned int x86_disasm_range( unsigned char *buf, uint32_t buf_rva,
  711. unsigned int offset, unsigned int len,
  712. DISASM_CALLBACK func, void *arg );
  713. /* x86_disasm_forward: Flow-of-execution disassembly of the bytes in a buffer,
  714. * invoking a callback function each time an instruction
  715. * is successfully disassembled.
  716. * buf : Buffer to disassemble (e.g. .text section)
  717. * buf_len : Number of bytes in buffer
  718. * buf_rva : Load address of buffer (e.g. ELF Virtual Address)
  719. * offset : Offset in buffer to start disassembly at (e.g. entry point)
  720. * func : Callback function to invoke (may be NULL)
  721. * arg : Arbitrary data to pass to callback (may be NULL)
  722. * resolver: Caller-supplied address resolver. If no resolver is
  723. * supplied, a default internal one is used -- however the
  724. * internal resolver does NOT catch loops and could end up
  725. * disassembling forever..
  726. * r_arg : Arbitrary data to pass to resolver (may be NULL)
  727. */
  728. unsigned int x86_disasm_forward( unsigned char *buf, unsigned int buf_len,
  729. uint32_t buf_rva, unsigned int offset,
  730. DISASM_CALLBACK func, void *arg,
  731. DISASM_RESOLVER resolver, void *r_arg );
  732. /* Call the register reporter to report an error */
  733. void x86_report_error( enum x86_report_codes code, void *data );
  734. /* fill 'buf' with a description of the format's syntax */
  735. int x86_format_header( char *buf, int len, enum x86_asm_format format);
  736. /* Endianness of an x86 CPU : 0 is big, 1 is little; always returns 1 */
  737. unsigned int x86_endian(void);
  738. /* Default address and operand size in bytes */
  739. unsigned int x86_addr_size(void);
  740. unsigned int x86_op_size(void);
  741. /* Size of a machine word in bytes */
  742. unsigned int x86_word_size(void);
  743. /* maximum size of a code instruction */
  744. unsigned int x86_max_insn_size(void);
  745. /* register IDs of Stack, Frame, Instruction pointer and Flags register */
  746. unsigned int x86_sp_reg(void);
  747. unsigned int x86_fp_reg(void);
  748. unsigned int x86_ip_reg(void);
  749. unsigned int x86_flag_reg(void);
  750. };
  751. /* Instruction operands: these are stored as a list of explicit and
  752. * implicit operands. It is recommended that the 'foreach' routines
  753. * be used to when examining operands for purposes of data flow analysis */
  754. /* Operand Convenience Routines: the following three routines are common
  755. * operations on operands, intended to ease the burden of the programmer. */
  756. /* format (sprintf) an instruction mnemonic into 'buf' using specified syntax */
  757. //int x86_format_mnemonic(x86_insn_t *insn, char *buf, int len,
  758. // enum x86_asm_format format);
  759. /* fill 'reg' struct with details of register 'id' */
  760. //void x86_reg_from_id( unsigned int id, x86_reg_t * reg );
  761. /* convenience macro demonstrating how to get an aliased register; proto is
  762. * void x86_get_aliased_reg( x86_reg_t *alias_reg, x86_reg_t *output_reg )
  763. * where 'alias_reg' is a reg operand and 'output_reg' is filled with the
  764. * register that the operand is an alias for */
  765. /*
  766. #define x86_get_aliased_reg( alias_reg, output_reg ) \
  767. x86_reg_from_id( alias_reg->alias, output_reg )
  768. */
  769. /* ================================== Invariant Instruction Representation */
  770. /* Invariant instructions are used for generating binary signatures;
  771. * the instruction is modified so that all variant bytes in an instruction
  772. * are replaced with a wildcard byte.
  773. *
  774. * A 'variant byte' is one that is expected to be modified by either the
  775. * static or the dynamic linker: for example, an address encoded in an
  776. * instruction.
  777. *
  778. * By comparing the invariant representation of one instruction [or of a
  779. * sequence of instructions] with the invariant representation of another,
  780. * one determine whether the two invariant representations are from the same
  781. * relocatable object [.o] file. Thus one can use binary signatures [which
  782. * are just sequences of invariant instruction representations] to look for
  783. * library routines which have been statically-linked into a binary.
  784. *
  785. * The invariant routines are faster and smaller than the disassembly
  786. * routines; they can be used to determine the size of an instruction
  787. * without all of the overhead of a full instruction disassembly.
  788. */
  789. /* This byte is used to replace variant bytes */
  790. #define X86_WILDCARD_BYTE 0xF4
  791. struct x86_invariant_op_t{
  792. enum x86_op_type type; /* operand type */
  793. enum x86_op_datatype datatype; /* operand size */
  794. enum x86_op_access access; /* operand access [RWX] */
  795. x86_op_flags flags; /* misc flags */
  796. };
  797. struct x86_invariant_t {
  798. unsigned char bytes[64]; /* invariant representation */
  799. unsigned int size; /* number of bytes in insn */
  800. x86_insn_t::x86_insn_group group; /* meta-type, e.g. INS_EXEC */
  801. enum x86_insn_type type; /* type, e.g. INS_BRANCH */
  802. x86_invariant_op_t operands[3]; /* operands: dest, src, imm */
  803. } ;
  804. /* return a version of the instruction with the variant bytes masked out */
  805. size_t x86_invariant_disasm( unsigned char *buf, int buf_len,
  806. x86_invariant_t *inv );
  807. /* return the size in bytes of the intruction pointed to by 'buf';
  808. * this used x86_invariant_disasm since it faster than x86_disasm */
  809. size_t x86_size_disasm( unsigned char *buf, unsigned int buf_len );
  810. #ifdef __cplusplus
  811. //}
  812. #endif
  813. #endif