#include <stdio.h>
#include <string.h>
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#include <mach-o/reloc.h>
#include "stuff/bytesex.h"
#include "otool.h"
#include "ofile_print.h"
#define MAX_MNEMONIC 9
#define MAX_RESULT 14
#define WBIT(x) (x & 0x1)
#define REGNO(x) (x & 0x7)
#define VBIT(x) ((x)>>1 & 0x1)
#define OPSIZE(data16,wbit) ((wbit) ? ((data16) ? 2:4) : 1 )
#define REG_ONLY 3
#define BYTEOPERAND 0
#define LONGOPERAND 1
#define EBP 5
#define ESP 4
struct instable {
char name[MAX_MNEMONIC];
const struct instable *indirect;
unsigned adr_mode;
int suffix;
};
#define TERM 0
#define INVALID {"",TERM,UNKNOWN,0}
static void get_operand(
const char **symadd,
const char **symsub,
unsigned long *value,
unsigned long *value_size,
char *result,
const unsigned long mode,
const unsigned long r_m,
const unsigned long wbit,
const enum bool data16,
const enum bool addr16,
const enum bool sse2,
const enum bool mmx,
const char *sect,
unsigned long sect_addr,
unsigned long *length,
unsigned long *left,
const unsigned long addr,
const struct relocation_info *sorted_relocs,
const unsigned long nsorted_relocs,
const struct nlist *symbols,
const unsigned long nsymbols,
const char *strings,
const unsigned long strings_size,
const struct nlist *sorted_symbols,
const unsigned long nsorted_symbols,
const enum bool verbose);
static void immediate(
const char **symadd,
const char **symsub,
unsigned long *value,
unsigned long value_size,
const char *sect,
unsigned long sect_addr,
unsigned long *length,
unsigned long *left,
const unsigned long addr,
const struct relocation_info *sorted_relocs,
const unsigned long nsorted_relocs,
const struct nlist *symbols,
const unsigned long nsymbols,
const char *strings,
const unsigned long strings_size,
const struct nlist *sorted_symbols,
const unsigned long nsorted_symbols,
const enum bool verbose);
static void displacement(
const char **symadd,
const char **symsub,
unsigned long *value,
const unsigned long value_size,
const char *sect,
unsigned long sect_addr,
unsigned long *length,
unsigned long *left,
const unsigned long addr,
const struct relocation_info *sorted_relocs,
const unsigned long nsorted_relocs,
const struct nlist *symbols,
const unsigned long nsymbols,
const char *strings,
const unsigned long strings_size,
const struct nlist *sorted_symbols,
const unsigned long nsorted_symbols,
const enum bool verbose);
static void get_symbol(
const char **symadd,
const char **symsub,
unsigned long *offset,
const unsigned long sect_offset,
const unsigned long value,
const struct relocation_info *relocs,
const unsigned long nrelocs,
const struct nlist *symbols,
const unsigned long nsymbols,
const char *strings,
const unsigned long strings_size,
const struct nlist *sorted_symbols,
const unsigned long nsorted_symbols,
const enum bool verbose);
static void print_operand(
const char *seg,
const char *symadd,
const char *symsub,
const unsigned int value,
const unsigned int value_size,
const char *result,
const char *tail);
static unsigned long get_value(
const unsigned long size,
const char *sect,
unsigned long *length,
unsigned long *left);
static void modrm_byte(
unsigned long *mode,
unsigned long *reg,
unsigned long *r_m,
unsigned char byte);
#define GET_OPERAND(symadd, symsub, value, value_size, result) \
get_operand((symadd), (symsub), (value), (value_size), (result), \
mode, r_m, wbit, data16, addr16, sse2, mmx, sect, \
sect_addr, &length, &left, addr, sorted_relocs, \
nsorted_relocs, symbols, nsymbols, strings, strings_size, \
sorted_symbols, nsorted_symbols, verbose)
#define DISPLACEMENT(symadd, symsub, value, value_size) \
displacement((symadd), (symsub), (value), (value_size), sect, \
sect_addr, &length, &left, addr, sorted_relocs, \
nsorted_relocs, symbols, nsymbols, strings, strings_size, \
sorted_symbols, nsorted_symbols, verbose)
#define IMMEDIATE(symadd, symsub, value, value_size) \
immediate((symadd), (symsub), (value), (value_size), sect, sect_addr, \
&length, &left, addr, sorted_relocs, nsorted_relocs, \
symbols, nsymbols, strings, strings_size, sorted_symbols, \
nsorted_symbols, verbose)
#define GET_SYMBOL(symadd, symsub, offset, sect_offset, value) \
get_symbol((symadd), (symsub), (offset), (sect_offset), (value), \
sorted_relocs, nsorted_relocs, symbols, nsymbols, strings,\
strings_size, sorted_symbols, nsorted_symbols, verbose)
#define GUESS_SYMBOL(value) \
guess_symbol((value), sorted_symbols, nsorted_symbols, verbose)
#define UNKNOWN 0
#define MRw 2
#define IMlw 3
#define IMw 4
#define IR 5
#define OA 6
#define AO 7
#define MS 8
#define SM 9
#define Mv 10
#define Mw 11
#define M 12
#define R 13
#define RA 14
#define SEG 15
#define MR 16
#define IA 17
#define MA 18
#define SD 19
#define AD 20
#define SA 21
#define D 22
#define INM 23
#define SO 24
#define BD 25
#define I 26
#define P 27
#define V 28
#define DSHIFT 29
#define U 30
#define OVERRIDE 31
#define GO_ON 32
#define O 33
#define JTAB 34
#define IMUL 35
#define CBW 36
#define MvI 37
#define ENTER 38
#define RMw 39
#define Ib 40
#define F 41
#define FF 42
#define DM 43
#define AM 44
#define LSEG 45
#define MIb 46
#define SREG 47
#define PREFIX 48
#define INT3 49
#define DSHIFTcl 50
#define CWD 51
#define RET 52
#define MOVZ 53
#define XINST 54
#define BSWAP 55
#define Pi 56
#define Po 57
#define Vi 58
#define Vo 59
#define Mb 60
#define INMl 61
#define SSE2 62
#define SSE2i 63
#define SSE2i1 64
#define SSE2tm 65
#define SSE2tfm 66
#define PFCH 67
#define SFEN 68
#define Mnol 69
static const char * const REG16[8][2] = {
{"%al", "%ax"},
{"%cl", "%cx"},
{"%dl", "%dx"},
{"%bl", "%bx"},
{"%ah", "%sp"},
{"%ch", "%bp"},
{"%dh", "%si"},
{"%bh", "%di"}
};
static const char * const REG32[8][2] = {
{"%al", "%eax"},
{"%cl", "%ecx"},
{"%dl", "%edx"},
{"%bl", "%ebx"},
{"%ah", "%esp"},
{"%ch", "%ebp"},
{"%dh", "%esi"},
{"%bh", "%edi"}
};
static const char dispsize16 [8][4] = {
{0, 1, 2, 0},
{0, 1, 2, 0},
{0, 1, 2, 0},
{0, 1, 2, 0},
{0, 1, 2, 0},
{0, 1, 2, 0},
{2, 1, 2, 0},
{0, 1, 2, 0}
};
static const char dispsize32 [8][4] = {
{0, 1, 4, 0},
{0, 1, 4, 0},
{0, 1, 4, 0},
{0, 1, 4, 0},
{0, 1, 4, 0},
{4, 1, 4, 0},
{0, 1, 4, 0},
{0, 1, 4, 0}
};
static const char * const regname16[4][8] = {
{"%bx,%si", "%bx,%di", "%bp,%si", "%bp,%di", "%si", "%di", "", "%bx"},
{"%bx,%si", "%bx,%di", "%bp,%si", "%bp,%di", "%si", "%di", "%bp", "%bx"},
{"%bx,%si", "%bx,%di", "%bp,%si", "%bp,%di", "%si", "%di", "%bp", "%bx"},
{"%ax", "%cx", "%dx", "%bx", "%sp", "%bp", "%si", "%di"}
};
static const char * const regname32[4][8] = {
{"%eax", "%ecx", "%edx", "%ebx", "%esp", "", "%esi", "%edi"},
{"%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi"},
{"%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi"},
{"%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi"}
};
static const char * const scale_factor[4] = {
"1",
"2",
"4",
"8"
};
static const char * const indexname[8] = {
",%eax",
",%ecx",
",%edx",
",%ebx",
"",
",%ebp",
",%esi",
",%edi"
};
static const char * const SEGREG[8] = {
"%es",
"%cs",
"%ss",
"%ds",
"%fs",
"%gs",
"%?6",
"%?7",
};
static const char * const DEBUGREG[8] = {
"%db0", "%db1", "%db2", "%db3", "%db4", "%db5", "%db6", "%db7"
};
static const char * const CONTROLREG[8] = {
"%cr0", "%cr1", "%cr2", "%cr3", "%cr4?", "%cr5?", "%cr6?", "%cr7?"
};
static const char * const TESTREG[8] = {
"%tr0?", "%tr1?", "%tr2?", "%tr3", "%tr4", "%tr5", "%tr6", "%tr7"
};
static const struct instable op0F00[8] = {
{"sldt",TERM,M,1}, {"str",TERM,M,1},
{"lldt",TERM,M,1}, {"ltr",TERM,M,1},
{"verr",TERM,M,1}, {"verw",TERM,M,1},
INVALID, INVALID,
};
static const struct instable op0F01[8] = {
{"sgdt",TERM,M,1}, {"sidt",TERM,M,1},
{"lgdt",TERM,M,1}, {"lidt",TERM,M,1},
{"smsw",TERM,M,1}, INVALID,
{"lmsw",TERM,M,1}, {"invlpg",TERM,M,1},
};
static const struct instable op0FBA[8] = {
INVALID, INVALID,
INVALID, INVALID,
{"bt",TERM,MIb,1}, {"bts",TERM,MIb,1},
{"btr",TERM,MIb,1}, {"btc",TERM,MIb,1},
};
static const struct instable op0FAE[8] = {
{"fxsave",TERM,M,1}, {"fxrstor",TERM,M,1},
{"ldmxcsr",TERM,M,1}, {"stmxcsr",TERM,M,1},
INVALID, {"lfence",TERM,GO_ON,0},
{"mfence",TERM,GO_ON,0},{"clflush",TERM,SFEN,1},
};
static const struct instable op0F[16][16] = {
{ {"",op0F00,TERM,0}, {"",op0F01,TERM,0},
{"lar",TERM,MR,0}, {"lsl",TERM,MR,0},
INVALID, INVALID,
{"clts",TERM,GO_ON,0}, INVALID,
{"invd",TERM,GO_ON,0}, {"wbinvd",TERM,GO_ON,0},
INVALID, INVALID,
INVALID, INVALID,
INVALID, INVALID },
{ {"mov",TERM,SSE2,0}, {"mov",TERM,SSE2tm,0},
{"mov",TERM,SSE2,0}, {"movl",TERM,SSE2tm,0},
{"unpckl",TERM,SSE2,0}, {"unpckh",TERM,SSE2,0},
{"mov",TERM,SSE2,0}, {"movh",TERM,SSE2tm,0},
{"prefetch",TERM,PFCH,1},INVALID,
INVALID, INVALID,
INVALID, INVALID,
INVALID, INVALID },
{ {"mov",TERM,SREG,1}, {"mov",TERM,SREG,1},
{"mov",TERM,SREG,1}, {"mov",TERM,SREG,1},
{"mov",TERM,SREG,1}, INVALID,
{"mov",TERM,SREG,1}, INVALID,
{"mova",TERM,SSE2,0}, {"mova",TERM,SSE2tm,0},
{"cvt",TERM,SSE2,0}, {"movnt",TERM,SSE2tm,0},
{"cvt",TERM,SSE2,0}, {"cvt",TERM,SSE2,0} ,
{"ucomi",TERM,SSE2,0}, {"comi",TERM,SSE2,0} },
{ {"wrmsr",TERM,GO_ON,0}, {"rdtsc",TERM,GO_ON,0},
{"rdmsr",TERM,GO_ON,0}, {"rdpmc",TERM,GO_ON,0},
{"sysenter",TERM,GO_ON,0},{"sysexit",TERM,GO_ON,0},
INVALID, INVALID,
INVALID, INVALID,
INVALID, INVALID,
INVALID, INVALID,
INVALID, INVALID },
{ {"cmovo",TERM,MRw,1}, {"cmovno",TERM,MRw,1},
{"cmovb",TERM,MRw,1}, {"cmovae",TERM,MRw,1},
{"cmove",TERM,MRw,1}, {"cmovne",TERM,MRw,1},
{"cmovbe",TERM,MRw,1}, {"cmova",TERM,MRw,1},
{"cmovs",TERM,MRw,1}, {"cmovns",TERM,MRw,1},
{"cmovp",TERM,MRw,1}, {"cmovnp",TERM,MRw,1},
{"cmovl",TERM,MRw,1}, {"cmovge",TERM,MRw,1},
{"cmovle",TERM,MRw,1}, {"cmovg",TERM,MRw,1} },
{ {"movmsk",TERM,SSE2,0}, {"sqrt",TERM,SSE2,0},
{"rsqrt",TERM,SSE2,0}, {"rcp",TERM,SSE2,0},
{"and",TERM,SSE2,0}, {"andn",TERM,SSE2,0},
{"or",TERM,SSE2,0}, {"xor",TERM,SSE2,0},
{"add",TERM,SSE2,0}, {"mul",TERM,SSE2,0},
{"cvt",TERM,SSE2,0}, {"cvt",TERM,SSE2,0},
{"sub",TERM,SSE2,0}, {"min",TERM,SSE2,0},
{"div",TERM,SSE2,0}, {"max",TERM,SSE2,0} },
{ {"punpcklbw",TERM,SSE2,0},{"punpcklwd",TERM,SSE2,0},
{"punpckldq",TERM,SSE2,0},{"packsswb",TERM,SSE2,0},
{"pcmpgtb",TERM,SSE2,0},{"pcmpgtw",TERM,SSE2,0},
{"pcmpgtd",TERM,SSE2,0},{"packuswb",TERM,SSE2,0},
{"punpckhbw",TERM,SSE2,0},{"punpckhwd",TERM,SSE2,0},
{"punpckhdq",TERM,SSE2,0},{"packssdw",TERM,SSE2,0},
{"punpckl",TERM,SSE2,0},{"punpckh",TERM,SSE2,0},
{"movd",TERM,SSE2,0}, {"mov",TERM,SSE2,0} },
{ {"pshu",TERM,SSE2i,0}, {"ps",TERM,SSE2i1,0},
{"ps",TERM,SSE2i1,0}, {"ps",TERM,SSE2i1,0},
{"pcmpeqb",TERM,SSE2,0},{"pcmpeqw",TERM,SSE2,0},
{"pcmpeqd",TERM,SSE2,0},{"emms",TERM,GO_ON,0},
INVALID, INVALID,
INVALID, INVALID,
INVALID, INVALID,
{"mov",TERM,SSE2tfm,0}, {"mov",TERM,SSE2tm,0} },
{ {"jo",TERM,D,1}, {"jno",TERM,D,1},
{"jb",TERM,D,1}, {"jae",TERM,D,1},
{"je",TERM,D,1}, {"jne",TERM,D,1},
{"jbe",TERM,D,1}, {"ja",TERM,D,1},
{"js",TERM,D,1}, {"jns",TERM,D,1},
{"jp",TERM,D,1}, {"jnp",TERM,D,1},
{"jl",TERM,D,1}, {"jge",TERM,D,1},
{"jle",TERM,D,1}, {"jg",TERM,D,1} },
{ {"seto",TERM,Mb,1}, {"setno",TERM,Mb,1},
{"setb",TERM,Mb,1}, {"setae",TERM,Mb,1},
{"sete",TERM,Mb,1}, {"setne",TERM,Mb,1},
{"setbe",TERM,Mb,1}, {"seta",TERM,Mb,1},
{"sets",TERM,Mb,1}, {"setns",TERM,Mb,1},
{"setp",TERM,Mb,1}, {"setnp",TERM,Mb,1},
{"setl",TERM,Mb,1}, {"setge",TERM,Mb,1},
{"setle",TERM,Mb,1}, {"setg",TERM,Mb,1} },
{ {"push",TERM,LSEG,1}, {"pop",TERM,LSEG,1},
{"cpuid",TERM,GO_ON,0}, {"bt",TERM,RMw,1},
{"shld",TERM,DSHIFT,1}, {"shld",TERM,DSHIFTcl,1},
INVALID, INVALID,
{"push",TERM,LSEG,1}, {"pop",TERM,LSEG,1},
{"rsm",TERM,GO_ON,0}, {"bts",TERM,RMw,1},
{"shrd",TERM,DSHIFT,1}, {"shrd",TERM,DSHIFTcl,1},
{"",op0FAE,TERM,0}, {"imul",TERM,MRw,1} },
{ {"cmpxchgb",TERM,XINST,0},{"cmpxchg",TERM,XINST,1},
{"lss",TERM,MR,0}, {"btr",TERM,RMw,1},
{"lfs",TERM,MR,0}, {"lgs",TERM,MR,0},
{"movzb",TERM,MOVZ,1}, {"movzwl",TERM,MOVZ,0},
INVALID, INVALID,
{"",op0FBA,TERM,0}, {"btc",TERM,RMw,1},
{"bsf",TERM,MRw,1}, {"bsr",TERM,MRw,1},
{"movsb",TERM,MOVZ,1}, {"movswl",TERM,MOVZ,0} },
{ {"xaddb",TERM,XINST,0}, {"xadd",TERM,XINST,1},
{"cmp",TERM,SSE2i,0}, {"movnti",TERM,RMw,0},
{"pinsrw",TERM,SSE2i,0},{"pextrw",TERM,SSE2i,0},
{"shuf",TERM,SSE2i,0}, {"cmpxchg8b",TERM,M,1},
{"bswap",TERM,BSWAP,0}, {"bswap",TERM,BSWAP,0},
{"bswap",TERM,BSWAP,0}, {"bswap",TERM,BSWAP,0},
{"bswap",TERM,BSWAP,0}, {"bswap",TERM,BSWAP,0},
{"bswap",TERM,BSWAP,0}, {"bswap",TERM,BSWAP,0} },
{ INVALID, {"psrlw",TERM,SSE2,0},
{"psrld",TERM,SSE2,0}, {"psrlq",TERM,SSE2,0},
{"paddq",TERM,SSE2,0}, {"pmullw",TERM,SSE2,0},
{"mov",TERM,SSE2tm,0}, {"pmovmskb",TERM,SSE2,0},
{"psubusb",TERM,SSE2,0},{"psubusw",TERM,SSE2,0},
{"pminub",TERM,SSE2,0}, {"pand",TERM,SSE2,0},
{"paddusb",TERM,SSE2,0},{"paddusw",TERM,SSE2,0},
{"pmaxub",TERM,SSE2,0}, {"pandn",TERM,SSE2,0} },
{ {"pavgb",TERM,SSE2,0}, {"psraw",TERM,SSE2,0},
{"psrad",TERM,SSE2,0}, {"pavgw",TERM,SSE2,0},
{"pmulhuw",TERM,SSE2,0},{"pmulhw",TERM,SSE2,0},
{"cvt",TERM,SSE2,0}, {"movn",TERM,SSE2tm,0},
{"psubsb",TERM,SSE2,0}, {"psubsw",TERM,SSE2,0},
{"pminsw",TERM,SSE2,0}, {"por",TERM,SSE2,0},
{"paddsb",TERM,SSE2,0}, {"paddsw",TERM,SSE2,0},
{"pmaxsw",TERM,SSE2,0}, {"pxor",TERM,SSE2,0} },
{ INVALID, {"psllw",TERM,SSE2,0},
{"pslld",TERM,SSE2,0}, {"psllq",TERM,SSE2,0},
{"pmuludq",TERM,SSE2,0},{"pmaddwd",TERM,SSE2,0},
{"psadbw",TERM,SSE2,0}, {"maskmov",TERM,SSE2,0},
{"psubb",TERM,SSE2,0}, {"psubw",TERM,SSE2,0},
{"psubd",TERM,SSE2,0}, {"psubq",TERM,SSE2,0},
{"paddb",TERM,SSE2,0}, {"paddw",TERM,SSE2,0},
{"paddd",TERM,SSE2,0}, {"ud2",TERM,GO_ON,0} },
};
static const struct instable op80[8] = {
{"addb",TERM,IMlw,0}, {"orb",TERM,IMw,0},
{"adcb",TERM,IMlw,0}, {"sbbb",TERM,IMlw,0},
{"andb",TERM,IMw,0}, {"subb",TERM,IMlw,0},
{"xorb",TERM,IMw,0}, {"cmpb",TERM,IMlw,0},
};
static const struct instable op81[8] = {
{"add",TERM,IMlw,1}, {"or",TERM,IMw,1},
{"adc",TERM,IMlw,1}, {"sbb",TERM,IMlw,1},
{"and",TERM,IMw,1}, {"sub",TERM,IMlw,1},
{"xor",TERM,IMw,1}, {"cmp",TERM,IMlw,1},
};
static const struct instable op82[8] = {
{"addb",TERM,IMlw,0}, INVALID,
{"adcb",TERM,IMlw,0}, {"sbbb",TERM,IMlw,0},
INVALID, {"subb",TERM,IMlw,0},
INVALID, {"cmpb",TERM,IMlw,0},
};
static const struct instable op83[8] = {
{"add",TERM,IMlw,1}, {"or",TERM,IMlw,1},
{"adc",TERM,IMlw,1}, {"sbb",TERM,IMlw,1},
{"and",TERM,IMlw,1}, {"sub",TERM,IMlw,1},
{"xor",TERM,IMlw,1}, {"cmp",TERM,IMlw,1},
};
static const struct instable opC0[8] = {
{"rolb",TERM,MvI,0}, {"rorb",TERM,MvI,0},
{"rclb",TERM,MvI,0}, {"rcrb",TERM,MvI,0},
{"shlb",TERM,MvI,0}, {"shrb",TERM,MvI,0},
INVALID, {"sarb",TERM,MvI,0},
};
static const struct instable opD0[8] = {
{"rolb",TERM,Mv,0}, {"rorb",TERM,Mv,0},
{"rclb",TERM,Mv,0}, {"rcrb",TERM,Mv,0},
{"shlb",TERM,Mv,0}, {"shrb",TERM,Mv,0},
INVALID, {"sarb",TERM,Mv,0},
};
static const struct instable opC1[8] = {
{"rol",TERM,MvI,1}, {"ror",TERM,MvI,1},
{"rcl",TERM,MvI,1}, {"rcr",TERM,MvI,1},
{"shl",TERM,MvI,1}, {"shr",TERM,MvI,1},
INVALID, {"sar",TERM,MvI,1},
};
static const struct instable opD1[8] = {
{"rol",TERM,Mv,1}, {"ror",TERM,Mv,1},
{"rcl",TERM,Mv,1}, {"rcr",TERM,Mv,1},
{"shl",TERM,Mv,1}, {"shr",TERM,Mv,1},
INVALID, {"sar",TERM,Mv,1},
};
static const struct instable opD2[8] = {
{"rolb",TERM,Mv,0}, {"rorb",TERM,Mv,0},
{"rclb",TERM,Mv,0}, {"rcrb",TERM,Mv,0},
{"shlb",TERM,Mv,0}, {"shrb",TERM,Mv,0},
INVALID, {"sarb",TERM,Mv,0},
};
static const struct instable opD3[8] = {
{"rol",TERM,Mv,1}, {"ror",TERM,Mv,1},
{"rcl",TERM,Mv,1}, {"rcr",TERM,Mv,1},
{"shl",TERM,Mv,1}, {"shr",TERM,Mv,1},
INVALID, {"sar",TERM,Mv,1},
};
static const struct instable opF6[8] = {
{"testb",TERM,IMw,0}, INVALID,
{"notb",TERM,Mw,0}, {"negb",TERM,Mw,0},
{"mulb",TERM,MA,0}, {"imulb",TERM,MA,0},
{"divb",TERM,MA,0}, {"idivb",TERM,MA,0},
};
static const struct instable opF7[8] = {
{"test",TERM,IMw,1}, INVALID,
{"not",TERM,Mw,1}, {"neg",TERM,Mw,1},
{"mul",TERM,MA,1}, {"imul",TERM,MA,1},
{"div",TERM,MA,1}, {"idiv",TERM,MA,1},
};
static const struct instable opFE[8] = {
{"incb",TERM,Mw,0}, {"decb",TERM,Mw,0},
INVALID, INVALID,
INVALID, INVALID,
INVALID, INVALID,
};
static const struct instable opFF[8] = {
{"inc",TERM,Mw,1}, {"dec",TERM,Mw,1},
{"call",TERM,INM,1}, {"lcall",TERM,INMl,1},
{"jmp",TERM,INM,1}, {"ljmp",TERM,INMl,1},
{"push",TERM,M,1}, INVALID,
};
static const struct instable opFP1n2[8][8] = {
{ {"fadds",TERM,M,1}, {"fmuls",TERM,M,1},
{"fcoms",TERM,M,1}, {"fcomps",TERM,M,1},
{"fsubs",TERM,M,1}, {"fsubrs",TERM,M,1},
{"fdivs",TERM,M,1}, {"fdivrs",TERM,M,1} },
{ {"flds",TERM,M,1}, INVALID,
{"fsts",TERM,M,1}, {"fstps",TERM,M,1},
{"fldenv",TERM,M,1}, {"fldcw",TERM,M,1},
{"fnstenv",TERM,M,1}, {"fnstcw",TERM,M,1} },
{ {"fiaddl",TERM,M,1}, {"fimull",TERM,M,1},
{"ficoml",TERM,M,1}, {"ficompl",TERM,M,1},
{"fisubl",TERM,M,1}, {"fisubrl",TERM,M,1},
{"fidivl",TERM,M,1}, {"fidivrl",TERM,M,1} },
{ {"fildl",TERM,Mnol,1}, INVALID,
{"fistl",TERM,M,1}, {"fistpl",TERM,Mnol,1},
INVALID, {"fldt",TERM,M,1},
INVALID, {"fstpt",TERM,M,1} },
{ {"faddl",TERM,M,1}, {"fmull",TERM,M,1},
{"fcoml",TERM,M,1}, {"fcompl",TERM,M,1},
{"fsubl",TERM,M,1}, {"fsubrl",TERM,M,1},
{"fdivl",TERM,M,1}, {"fdivrl",TERM,M,1} },
{ {"fldl",TERM,M,1}, INVALID,
{"fstl",TERM,M,1}, {"fstpl",TERM,M,1},
{"frstor",TERM,M,1}, INVALID,
{"fnsave",TERM,M,1}, {"fnstsw",TERM,M,1} },
{ {"fiadds",TERM,M,1}, {"fimuls",TERM,M,1},
{"ficoms",TERM,M,1}, {"ficomps",TERM,M,1},
{"fisubs",TERM,M,1}, {"fisubrs",TERM,M,1},
{"fidivs",TERM,M,1}, {"fidivrs",TERM,M,1} },
{ {"filds",TERM,M,1}, INVALID,
{"fists",TERM,M,1}, {"fistps",TERM,M,1},
{"fbld",TERM,M,1}, {"fildq",TERM,M,1},
{"fbstp",TERM,M,1}, {"fistpq",TERM,M,1} },
};
static const struct instable opFP3[8][8] = {
{ {"fadd",TERM,FF,0}, {"fmul",TERM,FF,0},
{"fcom",TERM,F,0}, {"fcomp",TERM,F,0},
{"fsub",TERM,FF,0}, {"fsubr",TERM,FF,0},
{"fdiv",TERM,FF,0}, {"fdivr",TERM,FF,0} },
{ {"fld",TERM,F,0}, {"fxch",TERM,F,0},
{"fnop",TERM,GO_ON,0}, {"fstp",TERM,F,0},
INVALID, INVALID,
INVALID, INVALID },
{ {"fcmovb",TERM,FF,0}, {"fcmove",TERM,FF,0},
{"fcmovbe",TERM,FF,0}, {"fcmovu",TERM,FF,0},
INVALID, {"fucompp",TERM,GO_ON,0},
INVALID, INVALID },
{ {"fcmovnb",TERM,FF,0}, {"fcmovne",TERM,FF,0},
{"fcmovnbe",TERM,FF,0}, {"fcmovnu",TERM,FF,0},
INVALID, {"fucomi",TERM,FF,0},
{"fcomi",TERM,FF,0}, INVALID },
{ {"fadd",TERM,FF,0}, {"fmul",TERM,FF,0},
{"fcom",TERM,F,0}, {"fcomp",TERM,F,0},
{"fsub",TERM,FF,0}, {"fsubr",TERM,FF,0},
{"fdiv",TERM,FF,0}, {"fdivr",TERM,FF,0} },
{ {"ffree",TERM,F,0}, {"fxch",TERM,F,0},
{"fst",TERM,F,0}, {"fstp",TERM,F,0},
{"fucom",TERM,F,0}, {"fucomp",TERM,F,0},
INVALID, INVALID },
{ {"faddp",TERM,FF,0}, {"fmulp",TERM,FF,0},
{"fcomp",TERM,F,0}, {"fcompp",TERM,GO_ON,0},
{"fsubp",TERM,FF,0}, {"fsubrp",TERM,FF,0},
{"fdivp",TERM,FF,0}, {"fdivrp",TERM,FF,0} },
{ {"ffree",TERM,F,0}, {"fxch",TERM,F,0},
{"fstp",TERM,F,0}, {"fstp",TERM,F,0},
{"fnstsw",TERM,M,1}, {"fucomip",TERM,FF,0},
{"fcomip",TERM,FF,0}, INVALID },
};
static const struct instable opFP4[4][8] = {
{ {"fchs",TERM,GO_ON,0}, {"fabs",TERM,GO_ON,0},
INVALID, INVALID,
{"ftst",TERM,GO_ON,0}, {"fxam",TERM,GO_ON,0},
INVALID, INVALID },
{ {"fld1",TERM,GO_ON,0}, {"fldl2t",TERM,GO_ON,0},
{"fldl2e",TERM,GO_ON,0},{"fldpi",TERM,GO_ON,0},
{"fldlg2",TERM,GO_ON,0},{"fldln2",TERM,GO_ON,0},
{"fldz",TERM,GO_ON,0}, INVALID },
{ {"f2xm1",TERM,GO_ON,0}, {"fyl2x",TERM,GO_ON,0},
{"fptan",TERM,GO_ON,0}, {"fpatan",TERM,GO_ON,0},
{"fxtract",TERM,GO_ON,0},{"fprem1",TERM,GO_ON,0},
{"fdecstp",TERM,GO_ON,0},{"fincstp",TERM,GO_ON,0} },
{ {"fprem",TERM,GO_ON,0}, {"fyl2xp1",TERM,GO_ON,0},
{"fsqrt",TERM,GO_ON,0}, {"fsincos",TERM,GO_ON,0},
{"frndint",TERM,GO_ON,0},{"fscale",TERM,GO_ON,0},
{"fsin",TERM,GO_ON,0}, {"fcos",TERM,GO_ON,0} },
};
static const struct instable opFP5[8] = {
INVALID, INVALID,
{"fnclex",TERM,GO_ON,0},{"fninit",TERM,GO_ON,0},
{"fsetpm",TERM,GO_ON,0},INVALID,
INVALID, INVALID,
};
static const struct instable distable[16][16] = {
{ {"addb",TERM,RMw,0}, {"add",TERM,RMw,1},
{"addb",TERM,MRw,0}, {"add",TERM,MRw,1},
{"addb",TERM,IA,0}, {"add",TERM,IA,1},
{"push",TERM,SEG,1}, {"pop",TERM,SEG,1},
{"orb",TERM,RMw,0}, {"or",TERM,RMw,1},
{"orb",TERM,MRw,0}, {"or",TERM,MRw,1},
{"orb",TERM,IA,0}, {"or",TERM,IA,1},
{"push",TERM,SEG,1},
{"",(const struct instable *)op0F,TERM,0} },
{ {"adcb",TERM,RMw,0}, {"adc",TERM,RMw,1},
{"adcb",TERM,MRw,0}, {"adc",TERM,MRw,1},
{"adcb",TERM,IA,0}, {"adc",TERM,IA,1},
{"push",TERM,SEG,1}, {"pop",TERM,SEG,1},
{"sbbb",TERM,RMw,0}, {"sbb",TERM,RMw,1},
{"sbbb",TERM,MRw,0}, {"sbb",TERM,MRw,1},
{"sbbb",TERM,IA,0}, {"sbb",TERM,IA,1},
{"push",TERM,SEG,1}, {"pop",TERM,SEG,1} },
{ {"andb",TERM,RMw,0}, {"and",TERM,RMw,1},
{"andb",TERM,MRw,0}, {"and",TERM,MRw,1},
{"andb",TERM,IA,0}, {"and",TERM,IA,1},
{"%es:",TERM,OVERRIDE,0}, {"daa",TERM,GO_ON,0},
{"subb",TERM,RMw,0}, {"sub",TERM,RMw,1},
{"subb",TERM,MRw,0}, {"sub",TERM,MRw,1},
{"subb",TERM,IA,0}, {"sub",TERM,IA,1},
{"%cs:",TERM,OVERRIDE,0}, {"das",TERM,GO_ON,0} },
{ {"xorb",TERM,RMw,0}, {"xor",TERM,RMw,1},
{"xorb",TERM,MRw,0}, {"xor",TERM,MRw,1},
{"xorb",TERM,IA,0}, {"xor",TERM,IA,1},
{"%ss:",TERM,OVERRIDE,0}, {"aaa",TERM,GO_ON,0},
{"cmpb",TERM,RMw,0}, {"cmp",TERM,RMw,1},
{"cmpb",TERM,MRw,0}, {"cmp",TERM,MRw,1},
{"cmpb",TERM,IA,0}, {"cmp",TERM,IA,1},
{"%ds:",TERM,OVERRIDE,0}, {"aas",TERM,GO_ON,0} },
{ {"inc",TERM,R,1}, {"inc",TERM,R,1},
{"inc",TERM,R,1}, {"inc",TERM,R,1},
{"inc",TERM,R,1}, {"inc",TERM,R,1},
{"inc",TERM,R,1}, {"inc",TERM,R,1},
{"dec",TERM,R,1}, {"dec",TERM,R,1},
{"dec",TERM,R,1}, {"dec",TERM,R,1},
{"dec",TERM,R,1}, {"dec",TERM,R,1},
{"dec",TERM,R,1}, {"dec",TERM,R,1} },
{ {"push",TERM,R,1}, {"push",TERM,R,1},
{"push",TERM,R,1}, {"push",TERM,R,1},
{"push",TERM,R,1}, {"push",TERM,R,1},
{"push",TERM,R,1}, {"push",TERM,R,1},
{"pop",TERM,R,1}, {"pop",TERM,R,1},
{"pop",TERM,R,1}, {"pop",TERM,R,1},
{"pop",TERM,R,1}, {"pop",TERM,R,1},
{"pop",TERM,R,1}, {"pop",TERM,R,1} },
{ {"pusha",TERM,GO_ON,1}, {"popa",TERM,GO_ON,1},
{"bound",TERM,MR,1}, {"arpl",TERM,RMw,0},
{"%fs:",TERM,OVERRIDE,0}, {"%gs:",TERM,OVERRIDE,0},
{"data16",TERM,DM,0}, {"addr16",TERM,AM,0},
{"push",TERM,I,1}, {"imul",TERM,IMUL,1},
{"push",TERM,Ib,1}, {"imul",TERM,IMUL,1},
{"insb",TERM,GO_ON,0}, {"ins",TERM,GO_ON,1},
{"outsb",TERM,GO_ON,0}, {"outs",TERM,GO_ON,1} },
{ {"jo",TERM,BD,0}, {"jno",TERM,BD,0},
{"jb",TERM,BD,0}, {"jae",TERM,BD,0},
{"je",TERM,BD,0}, {"jne",TERM,BD,0},
{"jbe",TERM,BD,0}, {"ja",TERM,BD,0},
{"js",TERM,BD,0}, {"jns",TERM,BD,0},
{"jp",TERM,BD,0}, {"jnp",TERM,BD,0},
{"jl",TERM,BD,0}, {"jge",TERM,BD,0},
{"jle",TERM,BD,0}, {"jg",TERM,BD,0} },
{ {"",op80,TERM,0}, {"",op81,TERM,0},
{"",op82,TERM,0}, {"",op83,TERM,0},
{"testb",TERM,MRw,0}, {"test",TERM,MRw,1},
{"xchgb",TERM,MRw,0}, {"xchg",TERM,MRw,1},
{"movb",TERM,RMw,0}, {"mov",TERM,RMw,1},
{"movb",TERM,MRw,0}, {"mov",TERM,MRw,1},
{"mov",TERM,SM,1}, {"lea",TERM,MR,1},
{"mov",TERM,MS,1}, {"pop",TERM,M,1} },
{ {"nop",TERM,GO_ON,0}, {"xchg",TERM,RA,1},
{"xchg",TERM,RA,1}, {"xchg",TERM,RA,1},
{"xchg",TERM,RA,1}, {"xchg",TERM,RA,1},
{"xchg",TERM,RA,1}, {"xchg",TERM,RA,1},
{"",TERM,CBW,0}, {"",TERM,CWD,0},
{"lcall",TERM,SO,0}, {"wait/",TERM,PREFIX,0},
{"pushf",TERM,GO_ON,1}, {"popf",TERM,GO_ON,1},
{"sahf",TERM,GO_ON,0}, {"lahf",TERM,GO_ON,0} },
{ {"movb",TERM,OA,0}, {"mov",TERM,OA,1},
{"movb",TERM,AO,0}, {"mov",TERM,AO,1},
{"movsb",TERM,SD,0}, {"movs",TERM,SD,1},
{"cmpsb",TERM,SD,0}, {"cmps",TERM,SD,1},
{"testb",TERM,IA,0}, {"test",TERM,IA,1},
{"stosb",TERM,AD,0}, {"stos",TERM,AD,1},
{"lodsb",TERM,SA,0}, {"lods",TERM,SA,1},
{"scasb",TERM,AD,0}, {"scas",TERM,AD,1} },
{ {"movb",TERM,IR,0}, {"movb",TERM,IR,0},
{"movb",TERM,IR,0}, {"movb",TERM,IR,0},
{"movb",TERM,IR,0}, {"movb",TERM,IR,0},
{"movb",TERM,IR,0}, {"movb",TERM,IR,0},
{"mov",TERM,IR,1}, {"mov",TERM,IR,1},
{"mov",TERM,IR,1}, {"mov",TERM,IR,1},
{"mov",TERM,IR,1}, {"mov",TERM,IR,1},
{"mov",TERM,IR,1}, {"mov",TERM,IR,1} },
{ {"",opC0,TERM,0}, {"",opC1,TERM,0},
{"ret",TERM,RET,0}, {"ret",TERM,GO_ON,0},
{"les",TERM,MR,0}, {"lds",TERM,MR,0},
{"movb",TERM,IMw,0}, {"mov",TERM,IMw,1},
{"enter",TERM,ENTER,0}, {"leave",TERM,GO_ON,0},
{"lret",TERM,RET,0}, {"lret",TERM,GO_ON,0},
{"int",TERM,INT3,0}, {"int",TERM,Ib,0},
{"into",TERM,GO_ON,0}, {"iret",TERM,GO_ON,0} },
{ {"",opD0,TERM,0}, {"",opD1,TERM,0},
{"",opD2,TERM,0}, {"",opD3,TERM,0},
{"aam",TERM,U,0}, {"aad",TERM,U,0},
{"falc",TERM,GO_ON,0}, {"xlat",TERM,GO_ON,0},
{"",(const struct instable *)opFP1n2,TERM,0},
{"",(const struct instable *)opFP1n2,TERM,0},
{"",(const struct instable *)opFP1n2,TERM,0},
{"",(const struct instable *)opFP1n2,TERM,0},
{"",(const struct instable *)opFP1n2,TERM,0},
{"",(const struct instable *)opFP1n2,TERM,0},
{"",(const struct instable *)opFP1n2,TERM,0},
{"",(const struct instable *)opFP1n2,TERM,0} },
{ {"loopnz",TERM,BD,0}, {"loopz",TERM,BD,0},
{"loop",TERM,BD,0}, {"jcxz",TERM,BD,0},
{"inb",TERM,Pi,0}, {"in",TERM,Pi,1},
{"outb",TERM,Po,0}, {"out",TERM,Po,1},
{"call",TERM,D,1}, {"jmp",TERM,D,1},
{"ljmp",TERM,SO,0}, {"jmp",TERM,BD,0},
{"inb",TERM,Vi,0}, {"in",TERM,Vi,1},
{"outb",TERM,Vo,0}, {"out",TERM,Vo,1} },
{ {"lock/",TERM,PREFIX,0}, INVALID,
{"repnz/",TERM,PREFIX,0}, {"repz/",TERM,PREFIX,0},
{"hlt",TERM,GO_ON,0}, {"cmc",TERM,GO_ON,0},
{"",opF6,TERM,0}, {"",opF7,TERM,0},
{"clc",TERM,GO_ON,0}, {"stc",TERM,GO_ON,0},
{"cli",TERM,GO_ON,0}, {"sti",TERM,GO_ON,0},
{"cld",TERM,GO_ON,0}, {"std",TERM,GO_ON,0},
{"",opFE,TERM,0}, {"",opFF,TERM,0} },
};
unsigned long
i386_disassemble(
char *sect,
unsigned long left,
unsigned long addr,
unsigned long sect_addr,
enum byte_sex object_byte_sex,
struct relocation_info *sorted_relocs,
unsigned long nsorted_relocs,
struct nlist *symbols,
unsigned long nsymbols,
struct nlist *sorted_symbols,
unsigned long nsorted_symbols,
char *strings,
unsigned long strings_size,
unsigned long *indirect_symbols,
unsigned long nindirect_symbols,
struct mach_header *mh,
struct load_command *load_commands,
enum bool verbose)
{
char mnemonic[MAX_MNEMONIC+2];
const char *seg;
const char *symbol0, *symbol1;
const char *symadd0, *symsub0, *symadd1, *symsub1;
unsigned long value0, value1;
unsigned long value0_size, value1_size;
char result0[MAX_RESULT], result1[MAX_RESULT];
const char *indirect_symbol_name;
unsigned long length;
unsigned char byte;
unsigned opcode1, opcode2, opcode3, opcode4, opcode5, prefix_byte;
const struct instable *dp, *prefix_dp;
unsigned long wbit, vbit;
enum bool got_modrm_byte;
unsigned long mode, reg, r_m;
const char *reg_name;
enum bool data16;
enum bool addr16;
enum bool sse2;
enum bool mmx;
if(left == 0){
printf("(end of section)\n");
return(0);
}
memset(mnemonic, '\0', sizeof(mnemonic));
seg = "";
symbol0 = NULL;
symbol1 = NULL;
value0 = 0;
value1 = 0;
value0_size = 0;
value1_size = 0;
memset(result0, '\0', sizeof(result0));
memset(result1, '\0', sizeof(result1));
data16 = FALSE;
addr16 = FALSE;
sse2 = FALSE;
mmx = FALSE;
reg_name = NULL;
wbit = 0;
length = 0;
byte = 0;
opcode4 = 0;
opcode5 = 0;
prefix_dp = NULL;
prefix_byte = 0;
for(;;){
byte = get_value(sizeof(char), sect, &length, &left);
opcode1 = byte >> 4 & 0xf;
opcode2 = byte & 0xf;
dp = &distable[opcode1][opcode2];
if(dp->adr_mode == PREFIX){
if(prefix_dp != NULL)
printf(dp->name);
prefix_dp = dp;
prefix_byte = byte;
}
else if(dp->adr_mode == AM){
addr16 = !addr16;
prefix_byte = byte;
}
else if(dp->adr_mode == DM){
data16 = !data16;
prefix_byte = byte;
}
else if(dp->adr_mode == OVERRIDE){
seg = dp->name;
prefix_byte = byte;
}
else
break;
}
if(dp->indirect == (const struct instable *)op0F){
byte = get_value(sizeof(char), sect, &length, &left);
opcode4 = byte >> 4 & 0xf;
opcode5 = byte & 0xf;
dp = &op0F[opcode4][opcode5];
if(dp->adr_mode == SSE2 ||
dp->adr_mode == SSE2i ||
dp->adr_mode == SSE2i1 ||
dp->adr_mode == SSE2tm ||
dp->adr_mode == SSE2tfm){
prefix_dp = NULL;
}
else{
if(prefix_dp != NULL)
printf(prefix_dp->name);
}
}
else{
if(prefix_dp != NULL && prefix_byte == 0xf3 &&
opcode1 == 0x9 && opcode2 == 0x0){
printf("pause\n");
return(length);
}
if(prefix_dp != NULL)
printf(prefix_dp->name);
}
got_modrm_byte = FALSE;
if(dp->indirect != TERM){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, (unsigned long *)&opcode3, &r_m, byte);
if(opcode1 == 0xD && opcode2 >= 0x8){
if(opcode2 == 0xB && mode == 0x3 && opcode3 == 4)
dp = &opFP5[r_m];
else if(opcode2 == 0xB && mode == 0x3 && opcode3 > 6){
printf(".byte 0x%01x%01x, 0x%01x%01x 0x%02x #bad opcode\n",
(unsigned int)opcode1, (unsigned int)opcode2,
(unsigned int)opcode4, (unsigned int)opcode5,
(unsigned int)byte);
return(length);
}
else if(opcode2 == 0x9 && mode == 0x3 && opcode3 >= 4)
dp = &opFP4[opcode3-4][r_m];
else if(mode == 0x3)
dp = &opFP3[opcode2-8][opcode3];
else
dp = &opFP1n2[opcode2-8][opcode3];
}
else
dp = dp->indirect + opcode3;
}
if(dp->indirect != TERM){
printf(".byte 0x%02x #bad opcode\n", (unsigned int)byte);
return(length);
}
if(dp->adr_mode != CBW && dp->adr_mode != CWD){
if(dp->suffix){
if(data16 == TRUE)
sprintf(mnemonic, "%sw", dp->name);
else{
if(dp->adr_mode == Mnol)
sprintf(mnemonic, "%s", dp->name);
else
sprintf(mnemonic, "%sl", dp->name);
}
}
else{
sprintf(mnemonic, "%s", dp->name);
}
}
switch(dp -> adr_mode){
case BSWAP:
reg_name = REG32[(opcode5 & 0x7)][1];
printf("%s\t%s\n", mnemonic, reg_name);
return(length);
case XINST:
wbit = WBIT(opcode5);
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
if(data16 == TRUE)
reg_name = REG16[reg][wbit];
else
reg_name = REG32[reg][wbit];
printf("%s\t%s,", mnemonic, reg_name);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case MOVZ:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
if(data16 == TRUE)
reg_name = REG16[reg][LONGOPERAND];
else
reg_name = REG32[reg][LONGOPERAND];
wbit = WBIT(opcode5);
data16 = 1;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
",");
printf("%s\n", reg_name);
return(length);
case IMUL:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
wbit = LONGOPERAND;
GET_OPERAND(&symadd1, &symsub1, &value1, &value1_size, result1);
value0_size = OPSIZE(data16, opcode2 == 0x9);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
if(data16 == TRUE)
reg_name = REG16[reg][wbit];
else
reg_name = REG32[reg][wbit];
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", ",");
print_operand(seg, symadd1, symsub1, value1, value1_size, result1,
",");
printf("%s\n", reg_name);
return(length);
case MRw:
wbit = WBIT(opcode2);
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
if(data16 == TRUE)
reg_name = REG16[reg][wbit];
else
reg_name = REG32[reg][wbit];
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
",");
printf("%s\n", reg_name);
return(length);
case RMw:
wbit = WBIT(opcode2);
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
if(data16 == TRUE)
reg_name = REG16[reg][wbit];
else
reg_name = REG32[reg][wbit];
printf("%s\t%s,", mnemonic, reg_name);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case SSE2tfm:
data16 = FALSE;
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
switch(opcode4 << 4 | opcode5){
case 0x7e:
if(prefix_byte == 0x66){
printf("%sd\t%%xmm%lu,", mnemonic, reg);
wbit = LONGOPERAND;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size,
result0);
print_operand(seg, symadd0, symsub0, value0, value0_size,
result0, "\n");
}
else if(prefix_byte == 0xf0){
printf("%sd\t%%mm%lu,", mnemonic, reg);
mmx = TRUE;
GET_OPERAND(&symadd1, &symsub1, &value1, &value1_size,
result1);
print_operand(seg, symadd1, symsub1, value1, value1_size,
result1, "\n");
}
else if(prefix_byte == 0xf3){
printf("%sq\t", mnemonic);
sse2 = TRUE;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size,
result0);
print_operand(seg, symadd0, symsub0, value0, value0_size,
result0, ",");
printf("%%xmm%lu\n", reg);
}
else{
printf("%sd\t%%mm%lu,", mnemonic, reg);
wbit = LONGOPERAND;
GET_OPERAND(&symadd1, &symsub1, &value1, &value1_size,
result1);
print_operand(seg, symadd1, symsub1, value1, value1_size,
result1, "\n");
}
}
return(length);
case SSE2tm:
data16 = FALSE;
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
sprintf(result0, "%%xmm%lu", reg);
switch(opcode4 << 4 | opcode5){
case 0x11:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%supd\t", mnemonic);
else if(prefix_byte == 0xf2)
printf("%ssd\t", mnemonic);
else if(prefix_byte == 0xf3)
printf("%sss\t", mnemonic);
else
printf("%sups\t", mnemonic);
break;
case 0x13:
case 0x17:
case 0x29:
case 0x2b:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%spd\t", mnemonic);
else if(prefix_byte == 0xf2)
printf("%ssd\t", mnemonic);
else if(prefix_byte == 0xf3)
printf("%sss\t", mnemonic);
else
printf("%sps\t", mnemonic);
break;
case 0xd6:
if(prefix_byte == 0x66){
sse2 = TRUE;
printf("%sq\t", mnemonic);
}
break;
case 0x7f:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%sdqa\t", mnemonic);
else if(prefix_byte == 0xf3)
printf("%sdqu\t", mnemonic);
else{
sprintf(result0, "%%mm%lu", reg);
printf("%sq\t", mnemonic);
mmx = TRUE;
}
break;
case 0xe7:
if(prefix_byte == 0x66){
printf("%stdq\t", mnemonic);
}
else{
sprintf(result0, "%%mm%lu", reg);
printf("%stq\t", mnemonic);
mmx = TRUE;
}
break;
}
printf("%s,", result0);
GET_OPERAND(&symadd1, &symsub1, &value1, &value1_size, result1);
print_operand(seg, symadd1, symsub1, value1, value1_size,
result1, "\n");
return(length);
case SSE2:
data16 = FALSE;
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
sprintf(result1, "%%xmm%lu", reg);
switch(opcode4 << 4 | opcode5){
case 0x14:
case 0x15:
case 0x28:
case 0x51:
case 0x52:
case 0x53:
case 0x54:
case 0x55:
case 0x56:
case 0x57:
case 0x58:
case 0x59:
case 0x5c:
case 0x5d:
case 0x5e:
case 0x5f:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%spd\t", mnemonic);
else if(prefix_byte == 0xf2)
printf("%ssd\t", mnemonic);
else if(prefix_byte == 0xf3)
printf("%sss\t", mnemonic);
else
printf("%sps\t", mnemonic);
break;
case 0x12:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%slpd\t", mnemonic);
else if(prefix_byte == 0xf2)
printf("%slsd\t", mnemonic);
else if(prefix_byte == 0xf3)
printf("%slss\t", mnemonic);
else{
if(mode == REG_ONLY)
printf("%shlps\t", mnemonic);
else
printf("%slps\t", mnemonic);
}
break;
case 0x16:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%shpd\t", mnemonic);
else if(prefix_byte == 0xf2)
printf("%shsd\t", mnemonic);
else if(prefix_byte == 0xf3)
printf("%shss\t", mnemonic);
else{
if(mode == REG_ONLY)
printf("%slhps\t", mnemonic);
else
printf("%shps\t", mnemonic);
}
break;
case 0x50:
sse2 = TRUE;
reg_name = REG32[reg][1];
strcpy(result1, reg_name);
if(prefix_byte == 0x66)
printf("%spd\t", mnemonic);
else
printf("%sps\t", mnemonic);
break;
case 0x10:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%supd\t", mnemonic);
else if(prefix_byte == 0xf2)
printf("%ssd\t", mnemonic);
else if(prefix_byte == 0xf3)
printf("%sss\t", mnemonic);
else
printf("%sups\t", mnemonic);
break;
case 0x2a:
if(prefix_byte == 0x66){
mmx = TRUE;
printf("%spi2pd\t", mnemonic);
}
else if(prefix_byte == 0xf2){
wbit = LONGOPERAND;
printf("%ssi2sd\t", mnemonic);
}
else if(prefix_byte == 0xf3){
wbit = LONGOPERAND;
printf("%ssi2ss\t", mnemonic);
}
else{
mmx = TRUE;
printf("%spi2ps\t", mnemonic);
}
break;
case 0x2c:
if(prefix_byte == 0x66){
sse2 = TRUE;
printf("%stpd2pi\t", mnemonic);
sprintf(result1, "%%mm%lu", reg);
}
else if(prefix_byte == 0xf2){
sse2 = TRUE;
printf("%stsd2si\t", mnemonic);
reg_name = REG32[reg][1];
strcpy(result1, reg_name);
}
else if(prefix_byte == 0xf3){
sse2 = TRUE;
printf("%stss2si\t", mnemonic);
reg_name = REG32[reg][1];
strcpy(result1, reg_name);
}
else{
sse2 = TRUE;
printf("%stps2pi\t", mnemonic);
sprintf(result1, "%%mm%lu", reg);
}
break;
case 0x2d:
if(prefix_byte == 0x66){
sse2 = TRUE;
printf("%spd2pi\t", mnemonic);
sprintf(result1, "%%mm%lu", reg);
}
else if(prefix_byte == 0xf2){
sse2 = TRUE;
printf("%ssd2si\t", mnemonic);
reg_name = REG32[reg][1];
strcpy(result1, reg_name);
}
else if(prefix_byte == 0xf3){
sse2 = TRUE;
printf("%sss2si\t", mnemonic);
reg_name = REG32[reg][1];
strcpy(result1, reg_name);
}
else{
sse2 = TRUE;
printf("%sps2pi\t", mnemonic);
sprintf(result1, "%%mm%lu", reg);
}
break;
case 0x2e:
case 0x2f:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%ssd\t", mnemonic);
else
printf("%sss\t", mnemonic);
break;
case 0xe0:
case 0xe3:
if(prefix_byte == 0x66){
sse2 = TRUE;
printf("%s\t", mnemonic);
}
else{
sprintf(result1, "%%mm%lu", reg);
printf("%s\t", mnemonic);
mmx = TRUE;
}
break;
case 0xe6:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%stpd2dq\t", mnemonic);
if(prefix_byte == 0xf3)
printf("%sdq2pd\t", mnemonic);
else if(prefix_byte == 0xf2)
printf("%spd2dq\t", mnemonic);
break;
case 0x5a:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%spd2ps\t", mnemonic);
else if(prefix_byte == 0xf2)
printf("%ssd2ss\t", mnemonic);
else if(prefix_byte == 0xf3)
printf("%sss2sd\t", mnemonic);
else
printf("%sps2pd\t", mnemonic);
break;
case 0x5b:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%sps2dq\t", mnemonic);
else if(prefix_byte == 0xf3)
printf("%stps2dq\t", mnemonic);
else
printf("%sdq2ps\t", mnemonic);
break;
case 0x60:
case 0x61:
case 0x62:
case 0x63:
case 0x64:
case 0x65:
case 0x66:
case 0x67:
case 0x68:
case 0x69:
case 0x6a:
case 0x6b:
case 0x74:
case 0x75:
case 0x76:
case 0xd1:
case 0xd2:
case 0xd3:
case 0xd4:
case 0xd5:
case 0xd8:
case 0xd9:
case 0xdb:
case 0xdc:
case 0xdd:
case 0xdf:
case 0xe1:
case 0xe2:
case 0xe5:
case 0xe8:
case 0xe9:
case 0xeb:
case 0xec:
case 0xed:
case 0xef:
case 0xf1:
case 0xf2:
case 0xf3:
case 0xf5:
case 0xf8:
case 0xf9:
case 0xfa:
case 0xfb:
case 0xfc:
case 0xfd:
case 0xfe:
if(prefix_byte == 0x66){
printf("%s\t", mnemonic);
sse2 = TRUE;
}
else{
sprintf(result1, "%%mm%lu", reg);
printf("%s\t", mnemonic);
mmx = TRUE;
}
break;
case 0x6c:
case 0x6d:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%sqdq\t", mnemonic);
break;
case 0x6f:
if(prefix_byte == 0x66){
sse2 = TRUE;
printf("%sdqa\t", mnemonic);
}
else if(prefix_byte == 0xf3){
sse2 = TRUE;
printf("%sdqu\t", mnemonic);
}
else{
sprintf(result1, "%%mm%lu", reg);
printf("%sq\t", mnemonic);
mmx = TRUE;
}
break;
case 0xd6:
if(prefix_byte == 0xf2){
sprintf(result1, "%%mm%lu", reg);
printf("%sdq2q\t", mnemonic);
sse2 = TRUE;
}
else if(prefix_byte == 0xf3){
printf("%sq2dq\t", mnemonic);
mmx = TRUE;
}
break;
case 0x6e:
if(prefix_byte == 0x66){
printf("%s\t", mnemonic);
wbit = LONGOPERAND;
}
else{
sprintf(result1, "%%mm%lu", reg);
printf("%s\t", mnemonic);
wbit = LONGOPERAND;
}
break;
case 0xd7:
if(prefix_byte == 0x66){
reg_name = REG32[reg][1];
printf("%s\t%%xmm%lu,%s\n", mnemonic, r_m, reg_name);
return(length);
}
else{
reg_name = REG32[reg][1];
printf("%s\t%%mm%lu,%s\n", mnemonic, r_m, reg_name);
return(length);
}
break;
case 0xda:
case 0xde:
case 0xe4:
case 0xea:
case 0xee:
case 0xf4:
case 0xf6:
if(prefix_byte == 0x66){
sse2 = TRUE;
printf("%s\t", mnemonic);
}
else{
sprintf(result1, "%%mm%lu", reg);
printf("%s\t", mnemonic);
mmx = TRUE;
}
break;
case 0xf7:
sse2 = TRUE;
if(prefix_byte == 0x66)
printf("%sdqu\t", mnemonic);
else{
printf("%sq\t%%mm%lu,%%mm%lu\n", mnemonic, r_m, reg);
return(length);
}
break;
}
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
print_operand(seg, symadd0, symsub0, value0, value0_size,
result0, ",");
printf("%s\n", result1);
return(length);
case SSE2i:
data16 = FALSE;
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
if((opcode4 << 4 | opcode5) == 0x70 && prefix_byte == 0)
mmx = TRUE;
else if((opcode4 << 4 | opcode5) == 0xc4)
wbit = LONGOPERAND;
else
sse2 = TRUE;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
byte = get_value(sizeof(char), sect, &length, &left);
switch(opcode4 << 4 | opcode5){
case 0x70:
if(prefix_byte == 0x66)
printf("%sfd\t$0x%x,", mnemonic, byte);
else if(prefix_byte == 0xf2)
printf("%sflw\t$0x%x,", mnemonic, byte);
else if(prefix_byte == 0xf3)
printf("%sfhw\t$0x%x,", mnemonic, byte);
else{
printf("%sfw\t$0x%x,", mnemonic, byte);
print_operand(seg, symadd0, symsub0, value0, value0_size,
result0, ",");
printf("%%mm%lu\n", reg);
return(length);
}
break;
case 0xc4:
if(prefix_byte == 0x66){
printf("%s\t$0x%x,", mnemonic, byte);
}
else{
printf("%s\t$0x%x,", mnemonic, byte);
print_operand(seg, symadd0, symsub0, value0, value0_size,
result0, ",");
printf("%%mm%lu\n", reg);
return(length);
}
break;
case 0xc5:
if(prefix_byte == 0x66){
reg_name = REG32[reg][1];
printf("%s\t$0x%x,%%xmm%lu,%s\n", mnemonic, byte, r_m,
reg_name);
return(length);
}
else{
reg_name = REG32[reg][1];
printf("%s\t$0x%x,%%mm%lu,%s\n", mnemonic, byte, r_m,
reg_name);
return(length);
}
break;
default:
if(prefix_byte == 0x66)
printf("%spd\t$0x%x,", mnemonic, byte);
else if(prefix_byte == 0xf2)
printf("%ssd\t$0x%x,", mnemonic, byte);
else if(prefix_byte == 0xf3)
printf("%sss\t$0x%x,", mnemonic, byte);
else
printf("%sps\t$0x%x,", mnemonic, byte);
break;
}
print_operand(seg, symadd0, symsub0, value0, value0_size,
result0, ",");
printf("%%xmm%lu\n", reg);
return(length);
case SSE2i1:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
byte = get_value(sizeof(char), sect, &length, &left);
switch(opcode4 << 4 | opcode5){
case 0x71:
if(prefix_byte == 0x66){
if(reg == 0x2)
printf("%srlw\t$0x%x,", mnemonic, byte);
else if(reg == 0x4)
printf("%sraw\t$0x%x,", mnemonic, byte);
else if(reg == 0x6)
printf("%sllw\t$0x%x,", mnemonic, byte);
}
else{
if(reg == 0x2)
printf("%srlw\t$0x%x,", mnemonic, byte);
else if(reg == 0x4)
printf("%sraw\t$0x%x,", mnemonic, byte);
else if(reg == 0x6)
printf("%sllw\t$0x%x,", mnemonic, byte);
printf("%%mm%lu\n", r_m);
return(length);
}
break;
case 0x72:
if(prefix_byte == 0x66){
if(reg == 0x2)
printf("%srld\t$0x%x,", mnemonic, byte);
else if(reg == 0x4)
printf("%srad\t$0x%x,", mnemonic, byte);
else if(reg == 0x6)
printf("%slld\t$0x%x,", mnemonic, byte);
}
else{
if(reg == 0x2)
printf("%srld\t$0x%x,", mnemonic, byte);
else if(reg == 0x4)
printf("%srad\t$0x%x,", mnemonic, byte);
else if(reg == 0x6)
printf("%slld\t$0x%x,", mnemonic, byte);
printf("%%mm%lu\n", r_m);
return(length);
}
break;
case 0x73:
if(prefix_byte == 0x66){
if(reg == 0x7)
printf("%slldq\t$0x%x,", mnemonic, byte);
else if(reg == 0x3)
printf("%srldq\t$0x%x,", mnemonic, byte);
else if(reg == 0x2)
printf("%srlq\t$0x%x,", mnemonic, byte);
else if(reg == 0x6)
printf("%sllq\t$0x%x,", mnemonic, byte);
}
else{
if(reg == 0x2)
printf("%srlq\t$0x%x,", mnemonic, byte);
else if(reg == 0x6)
printf("%sllq\t$0x%x,", mnemonic, byte);
printf("%%mm%lu\n", r_m);
return(length);
}
break;
}
printf("%%xmm%lu\n", r_m);
return(length);
case PFCH:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
switch(reg){
case 0:
printf("%snta", dp->name);
break;
case 1:
printf("%st0", dp->name);
break;
case 2:
printf("%st1", dp->name);
break;
case 3:
printf("%st2", dp->name);
break;
}
if(data16 == TRUE)
printf("w\t");
else
printf("l\t");
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
print_operand(seg, symadd0, symsub0, value0, value0_size,
result0, "\n");
return(length);
case SFEN:
if(mode == REG_ONLY && r_m == 0){
printf("sfence\n");
return(length);
}
printf("%s\t", mnemonic);
reg = opcode3;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
print_operand(seg, symadd0, symsub0, value0, value0_size,
result0, "\n");
return(length);
case DSHIFT:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
wbit = LONGOPERAND;
GET_OPERAND(&symadd1, &symsub1, &value1, &value1_size, result1);
value0_size = sizeof(char);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
if(data16 == TRUE)
reg_name = REG16[reg][wbit];
else
reg_name = REG32[reg][wbit];
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", ",");
printf("%s,", reg_name);
print_operand(seg, symadd1, symsub1, value1, value1_size, result1,
"\n");
return(length);
case DSHIFTcl:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
wbit = LONGOPERAND;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
if(data16 == TRUE)
reg_name = REG16[reg][wbit];
else
reg_name = REG32[reg][wbit];
printf("%s\t%%cl,%s,", mnemonic, reg_name);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case IMlw:
wbit = WBIT(opcode2);
GET_OPERAND(&symadd1, &symsub1, &value1, &value1_size, result1);
value0_size = OPSIZE(data16, opcode2 == 1);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", ",");
print_operand(seg, symadd1, symsub1, value1, value1_size, result1,
"\n");
return(length);
case IMw:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
wbit = WBIT(opcode2);
GET_OPERAND(&symadd1, &symsub1, &value1, &value1_size, result1);
value0_size = OPSIZE(data16, wbit);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", ",");
print_operand(seg, symadd1, symsub1, value1, value1_size, result1,
"\n");
return(length);
case IR:
wbit = (opcode2 >> 3) & 0x1;
reg = REGNO(opcode2);
value0_size = OPSIZE(data16, wbit);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
if(data16 == TRUE)
reg_name = REG16[reg][wbit];
else
reg_name = REG32[reg][wbit];
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", ",");
printf("%s\n", reg_name);
return(length);
case OA:
value0_size = OPSIZE(data16, LONGOPERAND);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, "", ",");
wbit = WBIT(opcode2);
reg_name = (data16 ? REG16 : REG32)[0][wbit];
printf("%s\n", reg_name);
return(length);
case AO:
value0_size = OPSIZE(data16, LONGOPERAND);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
wbit = WBIT(opcode2);
reg_name = (data16 ? REG16 : REG32)[0][wbit];
printf("%s\t%s,", mnemonic, reg_name);
print_operand(seg, symadd0, symsub0, value0, value0_size, "", "\n");
return(length);
case MS:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
wbit = LONGOPERAND;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
",");
printf("%s\n", SEGREG[reg]);
return(length);
case SM:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
wbit = LONGOPERAND;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
printf("%s\t%s,", mnemonic, SEGREG[reg]);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case Mv:
vbit = VBIT(opcode2);
wbit = WBIT(opcode2);
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
reg_name = vbit ? "%cl," : "" ;
printf("%s\t%s", mnemonic, reg_name);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case MvI:
vbit = VBIT(opcode2);
wbit = WBIT(opcode2);
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
value1_size = sizeof(char);
IMMEDIATE(&symadd1, &symsub1, &value1, value1_size);
reg_name = vbit ? "%cl," : "" ;
printf("%s\t$", mnemonic);
print_operand("", symadd1, symsub1, value1, value1_size, "", ",");
printf("%s", reg_name);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case MIb:
wbit = LONGOPERAND;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
value1_size = sizeof(char);
IMMEDIATE(&symadd1, &symsub1, &value1, value1_size);
printf("%s\t$", mnemonic);
print_operand("", symadd1, symsub1, value1, value1_size, "", ",");
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case Mw:
wbit = WBIT(opcode2);
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case Mnol:
case M:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
wbit = LONGOPERAND;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case Mb:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
wbit = BYTEOPERAND;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case SREG:
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
vbit = 0;
switch(opcode5){
case 2:
vbit = 1;
case 0:
reg_name = CONTROLREG[reg];
break;
case 3:
vbit = 1;
case 1:
reg_name = DEBUGREG[reg];
break;
case 6:
vbit = 1;
case 4:
reg_name = TESTREG[reg];
break;
}
if(vbit)
printf("%s\t%s,%s\n", mnemonic, REG32[r_m][1], reg_name);
else
printf("%s\t%s,%s\n", mnemonic, reg_name, REG32[r_m][1]);
return(length);
case R:
reg = REGNO(opcode2);
if(data16 == TRUE)
reg_name = REG16[reg][LONGOPERAND];
else
reg_name = REG32[reg][LONGOPERAND];
printf("%s\t%s\n", mnemonic, reg_name);
return(length);
case RA:
reg = REGNO(opcode2);
if(data16 == TRUE){
reg_name = REG16[reg][LONGOPERAND];
printf("%s\t%s,%%ax\n", mnemonic, reg_name);
}
else{
reg_name = REG32[reg][LONGOPERAND];
printf("%s\t%s,%%eax\n", mnemonic, reg_name);
}
return(length);
case SEG:
reg = byte >> 3 & 0x3;
printf("%s\t%s\n", mnemonic, SEGREG[reg]);
return(length);
case LSEG:
reg = byte >> 3 & 0x7;
printf("%s\t%s\n", mnemonic, SEGREG[reg]);
return(length);
case MR:
if(got_modrm_byte == FALSE){
got_modrm_byte = TRUE;
byte = get_value(sizeof(char), sect, &length, &left);
modrm_byte(&mode, ®, &r_m, byte);
}
wbit = LONGOPERAND;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
if(data16 == TRUE)
reg_name = REG16[reg][wbit];
else
reg_name = REG32[reg][wbit];
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
",");
printf("%s\n", reg_name);
return(length);
case IA:
value0_size = OPSIZE(data16, WBIT(opcode2));
switch(value0_size) {
case 1: reg_name = "%al"; break;
case 2: reg_name = "%ax"; break;
case 4: reg_name = "%eax"; break;
}
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", ",");
printf("%s\n", reg_name);
return(length);
case MA:
wbit = WBIT(opcode2);
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case SD:
if(addr16 == TRUE)
printf("%s\t%s(%%si),(%%di)\n", mnemonic, seg);
else
printf("%s\t%s(%%esi),(%%edi)\n", mnemonic, seg);
return(length);
case AD:
wbit = WBIT(opcode2);
reg_name = (data16 ? REG16 : REG32)[0][wbit];
if(addr16 == TRUE)
printf("%s\t%s,%s(%%di)\n", mnemonic, reg_name, seg);
else
printf("%s\t%s,%s(%%edi)\n", mnemonic, reg_name, seg);
return(length);
case SA:
wbit = WBIT(opcode2);
reg_name = (data16 ? REG16 : REG32)[0][wbit];
if(addr16 == TRUE)
printf("%s\t%s(%%si),%s\n", mnemonic, seg, reg_name);
else
printf("%s\t%s(%%esi),%s\n", mnemonic, seg, reg_name);
return(length);
case D:
value0_size = OPSIZE(data16, LONGOPERAND);
DISPLACEMENT(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, "", "");
if(verbose){
indirect_symbol_name = guess_indirect_symbol(value0,
mh, load_commands, object_byte_sex, indirect_symbols,
nindirect_symbols, symbols, nsymbols, strings,strings_size);
if(indirect_symbol_name != NULL)
printf("\t; symbol stub for: %s", indirect_symbol_name);
}
printf("\n");
return(length);
case INM:
wbit = LONGOPERAND;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
if((mode == 0 && (r_m == 5 || r_m == 4)) || mode == 1 || mode == 2)
printf("%s\t*", mnemonic);
else
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case INMl:
wbit = LONGOPERAND;
GET_OPERAND(&symadd0, &symsub0, &value0, &value0_size, result0);
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, result0,
"\n");
return(length);
case SO:
value1_size = OPSIZE(data16, LONGOPERAND);
IMMEDIATE(&symadd1, &symsub1, &value1, value1_size);
value0_size = sizeof(short);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", ",$");
print_operand(seg, symadd1, symsub1, value1, value1_size, "", "\n");
return(length);
case BD:
value0_size = sizeof(char);
DISPLACEMENT(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t", mnemonic);
print_operand(seg, symadd0, symsub0, value0, sizeof(long), "",
"\n");
return(length);
case I:
value0_size = OPSIZE(data16, LONGOPERAND);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", "\n");
return(length);
case Ib:
value0_size = sizeof(char);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", "\n");
return(length);
case ENTER:
value0_size = sizeof(short);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
value1_size = sizeof(char);
IMMEDIATE(&symadd1, &symsub1, &value1, value1_size);
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", ",$");
print_operand("", symadd1, symsub1, value1, value1_size, "", "\n");
return(length);
case RET:
value0_size = sizeof(short);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t$", mnemonic);
print_operand("", symadd0, symsub0, value0, value0_size, "", "\n");
return(length);
case P:
value0_size = sizeof(char);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t$", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, "", "\n");
return(length);
case Pi:
value0_size = sizeof(char);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t$", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, "",
",%eax\n");
return(length);
case Po:
value0_size = sizeof(char);
IMMEDIATE(&symadd0, &symsub0, &value0, value0_size);
printf("%s\t%%eax,$", mnemonic);
print_operand(seg, symadd0, symsub0, value0, value0_size, "", "\n");
return(length);
case V:
printf("%s\t%s(%%dx)\n", mnemonic, seg);
return(length);
case Vi:
printf("%s\t%s%%dx,%%eax\n", mnemonic, seg);
return(length);
case Vo:
printf("%s\t%s%%eax,%%dx\n", mnemonic, seg);
return(length);
case INT3:
printf("%s\t$0x3\n", mnemonic);
return(length);
case U:
byte = get_value(sizeof(char), sect, &length, &left);
printf("%s\n", mnemonic);
return(length);
case CBW:
if(data16 == TRUE)
printf("cbtw\n");
else
printf("cwtl\n");
return(length);
case CWD:
if(data16 == TRUE)
printf("cwtd\n");
else
printf("cltd\n");
return(length);
case GO_ON:
printf("%s\n", mnemonic);
return(length);
case F:
printf("%s\t%%st(%1.1lu)\n", mnemonic, r_m);
return(length);
case FF:
if(((opcode2 >> 2) & 0x1) == 0x1 && opcode2 != 0xf)
printf("%s\t%%st,%%st(%1.1lu)\n", mnemonic, r_m);
else
printf("%s\t%%st(%1.1lu),%%st\n", mnemonic, r_m);
return(length);
case AM:
case DM:
case OVERRIDE:
case PREFIX:
case UNKNOWN:
default:
printf(".byte 0x%02x #bad opcode\n", (unsigned int)byte);
return(length);
}
}
static
void
get_operand(
const char **symadd,
const char **symsub,
unsigned long *value,
unsigned long *value_size,
char *result,
const unsigned long mode,
const unsigned long r_m,
const unsigned long wbit,
const enum bool data16,
const enum bool addr16,
const enum bool sse2,
const enum bool mmx,
const char *sect,
unsigned long sect_addr,
unsigned long *length,
unsigned long *left,
const unsigned long addr,
const struct relocation_info *sorted_relocs,
const unsigned long nsorted_relocs,
const struct nlist *symbols,
const unsigned long nsymbols,
const char *strings,
const unsigned long strings_size,
const struct nlist *sorted_symbols,
const unsigned long nsorted_symbols,
const enum bool verbose)
{
enum bool s_i_b;
unsigned char byte;
unsigned long ss;
unsigned long index;
unsigned long base;
unsigned long sect_offset;
unsigned long offset;
*symadd = NULL;
*symsub = NULL;
*value = 0;
*result = '\0';
if(r_m == ESP && mode != REG_ONLY && addr16 == FALSE){
s_i_b = TRUE;
byte = get_value(sizeof(char), sect, length, left);
modrm_byte(&ss, &index, &base, byte);
}
else
s_i_b = FALSE;
if(addr16)
*value_size = dispsize16[r_m][mode];
else
*value_size = dispsize32[r_m][mode];
if(s_i_b == TRUE && mode == 0 && base == EBP)
*value_size = sizeof(long);
if(*value_size != 0){
sect_offset = addr + *length - sect_addr;
*value = get_value(*value_size, sect, length, left);
GET_SYMBOL(symadd, symsub, &offset, sect_offset, *value);
if(*symadd != NULL){
*value = offset;
}
else{
*symadd = GUESS_SYMBOL(*value);
if(*symadd != NULL)
*value = 0;
}
}
if(s_i_b == TRUE){
sprintf(result, "(%s%s,%s)", regname32[mode][base],
indexname[index], scale_factor[ss]);
}
else{
if(mode == REG_ONLY){
if(sse2 == TRUE)
sprintf(result, "%%xmm%lu", r_m);
else if(mmx == TRUE)
sprintf(result, "%%mm%lu", r_m);
else if(data16 == TRUE)
strcpy(result, REG16[r_m][wbit]);
else
strcpy(result, REG32[r_m][wbit]);
}
else{
if(r_m == EBP && mode == 0){
*result = '\0';
}
else {
if(addr16 == TRUE)
sprintf(result, "(%s)", regname16[mode][r_m]);
else
sprintf(result, "(%s)", regname32[mode][r_m]);
}
}
}
}
static
void
immediate(
const char **symadd,
const char **symsub,
unsigned long *value,
unsigned long value_size,
const char *sect,
unsigned long sect_addr,
unsigned long *length,
unsigned long *left,
const unsigned long addr,
const struct relocation_info *sorted_relocs,
const unsigned long nsorted_relocs,
const struct nlist *symbols,
const unsigned long nsymbols,
const char *strings,
const unsigned long strings_size,
const struct nlist *sorted_symbols,
const unsigned long nsorted_symbols,
const enum bool verbose)
{
unsigned long sect_offset, offset;
sect_offset = addr + *length - sect_addr;
*value = get_value(value_size, sect, length, left);
GET_SYMBOL(symadd, symsub, &offset, sect_offset, *value);
if(*symadd == NULL){
*symadd = GUESS_SYMBOL(*value);
if(*symadd != NULL)
*value = 0;
}
else if(*symsub != NULL){
*value = offset;
}
}
static
void
displacement(
const char **symadd,
const char **symsub,
unsigned long *value,
const unsigned long value_size,
const char *sect,
unsigned long sect_addr,
unsigned long *length,
unsigned long *left,
const unsigned long addr,
const struct relocation_info *sorted_relocs,
const unsigned long nsorted_relocs,
const struct nlist *symbols,
const unsigned long nsymbols,
const char *strings,
const unsigned long strings_size,
const struct nlist *sorted_symbols,
const unsigned long nsorted_symbols,
const enum bool verbose)
{
unsigned long sect_offset, offset;
sect_offset = addr + *length - sect_addr;
*value = get_value(value_size, sect, length, left);
switch(value_size){
case 1:
if((*value) & 0x80)
*value = *value | 0xffffff00;
break;
case 2:
if((*value) & 0x8000)
*value = *value | 0xffff0000;
break;
}
*value += addr + *length;
GET_SYMBOL(symadd, symsub, &offset, sect_offset, *value);
if(*symadd == NULL){
*symadd = GUESS_SYMBOL(*value);
if(*symadd != NULL)
*value = 0;
}
else if(*symsub != NULL){
*value = offset;
}
}
static
void
get_symbol(
const char **symadd,
const char **symsub,
unsigned long *offset,
const unsigned long sect_offset,
const unsigned long value,
const struct relocation_info *relocs,
const unsigned long nrelocs,
const struct nlist *symbols,
const unsigned long nsymbols,
const char *strings,
const unsigned long strings_size,
const struct nlist *sorted_symbols,
const unsigned long nsorted_symbols,
const enum bool verbose)
{
unsigned long i;
struct scattered_relocation_info *sreloc, *pair;
unsigned int r_symbolnum;
unsigned long n_strx;
const char *name, *add, *sub;
static char add_buffer[11];
static char sub_buffer[11];
*symadd = NULL;
*symsub = NULL;
*offset = value;
if(verbose == FALSE)
return;
for(i = 0; i < nrelocs; i++){
if(((relocs[i].r_address) & R_SCATTERED) != 0){
sreloc = (struct scattered_relocation_info *)(relocs + i);
if(sreloc->r_type == GENERIC_RELOC_PAIR){
fprintf(stderr, "Stray GENERIC_RELOC_PAIR relocation entry "
"%lu\n", i);
continue;
}
if(sreloc->r_type == GENERIC_RELOC_VANILLA){
if(sreloc->r_address == sect_offset){
name = guess_symbol(sreloc->r_value,
sorted_symbols,
nsorted_symbols,
verbose);
if(name != NULL){
*symadd = name;
*offset = value - sreloc->r_value;
return;
}
}
continue;
}
if(sreloc->r_type != GENERIC_RELOC_SECTDIFF){
fprintf(stderr, "Unknown relocation r_type for entry "
"%lu\n", i);
continue;
}
if(i + 1 < nrelocs){
pair = (struct scattered_relocation_info *)(relocs + i + 1);
if(pair->r_scattered == 0 ||
pair->r_type != GENERIC_RELOC_PAIR){
fprintf(stderr, "No GENERIC_RELOC_PAIR relocation "
"entry after entry %lu\n", i);
continue;
}
}
else{
fprintf(stderr, "No GENERIC_RELOC_PAIR relocation entry "
"after entry %lu\n", i);
continue;
}
i++;
if(sreloc->r_address == sect_offset){
add = guess_symbol(sreloc->r_value, sorted_symbols,
nsorted_symbols, verbose);
sub = guess_symbol(pair->r_value, sorted_symbols,
nsorted_symbols, verbose);
if(add == NULL){
sprintf(add_buffer, "0x%x",
(unsigned int)sreloc->r_value);
add = add_buffer;
}
if(sub == NULL){
sprintf(sub_buffer, "0x%x",
(unsigned int)pair->r_value);
sub = sub_buffer;
}
*symadd = add;
*symsub = sub;
*offset = value - (sreloc->r_value - pair->r_value);
return;
}
}
else{
if((unsigned long)relocs[i].r_address == sect_offset){
r_symbolnum = relocs[i].r_symbolnum;
if(relocs[i].r_extern){
if(r_symbolnum >= nsymbols)
return;
n_strx = symbols[r_symbolnum].n_un.n_strx;
if(n_strx <= 0 || n_strx >= strings_size)
return;
*symadd = strings + n_strx;
return;
}
break;
}
}
}
}
static
void
print_operand(
const char *seg,
const char *symadd,
const char *symsub,
const unsigned int value,
const unsigned int value_size,
const char *result,
const char *tail)
{
if(symadd != NULL){
if(symsub != NULL){
if(value_size != 0){
if(value != 0)
printf("%s%s-%s+0x%0*x%s%s", seg, symadd, symsub,
(int)value_size * 2, value, result, tail);
else
printf("%s%s-%s%s%s",seg, symadd, symsub, result, tail);
}
else{
printf("%s%s%s%s", seg, symadd, result, tail);
}
}
else{
if(value_size != 0){
if(value != 0)
printf("%s%s+0x%0*x%s%s", seg, symadd,
(int)value_size * 2, value, result, tail);
else
printf("%s%s%s%s", seg, symadd, result, tail);
}
else{
printf("%s%s%s%s", seg, symadd, result, tail);
}
}
}
else{
if(value_size != 0){
printf("%s0x%0*x%s%s", seg, (int)value_size *2, value, result,
tail);
}
else{
printf("%s%s%s", seg, result, tail);
}
}
}
static
unsigned long
get_value(
const unsigned long size,
const char *sect,
unsigned long *length,
unsigned long *left)
{
unsigned long i, value;
unsigned char byte;
if(left == 0)
return(0);
value = 0;
for(i = 0; i < size; i++) {
byte = 0;
if(*left > 0){
byte = sect[*length];
(*length)++;
(*left)--;
}
value |= (unsigned long)byte << (8*i);
}
return(value);
}
static
void
modrm_byte(
unsigned long *mode,
unsigned long *reg,
unsigned long *r_m,
unsigned char byte)
{
*r_m = byte & 0x7;
*reg = byte >> 3 & 0x7;
*mode = byte >> 6 & 0x3;
}