diff --git a/disasm.c b/disasm.c index d2d9d914..665e3d28 100644 --- a/disasm.c +++ b/disasm.c @@ -25,19 +25,56 @@ extern struct itemplate **itable[]; * Flags that go into the `segment' field of `insn' structures * during disassembly. */ -#define SEG_RELATIVE 1 -#define SEG_32BIT 2 -#define SEG_RMREG 4 -#define SEG_DISP8 8 -#define SEG_DISP16 16 -#define SEG_DISP32 32 -#define SEG_NODISP 64 -#define SEG_SIGNED 128 +#define SEG_RELATIVE 1 +#define SEG_32BIT 2 +#define SEG_RMREG 4 +#define SEG_DISP8 8 +#define SEG_DISP16 16 +#define SEG_DISP32 32 +#define SEG_NODISP 64 +#define SEG_SIGNED 128 +#define SEG_64BIT 256 + +/* + * REX flags + */ +#define REX_P 0x40 /* REX prefix present */ +#define REX_W 0x08 /* 64-bit operand size */ +#define REX_R 0x04 /* ModRM reg extension */ +#define REX_X 0x02 /* SIB index extension */ +#define REX_B 0x01 /* ModRM r/m extension */ -static int whichreg(int32_t regflags, int regval) -{ #include "regdis.c" +#define getu8(x) (*(uint8_t *)(x)) +#if defined(__i386__) || defined(__x86_64__) +/* Littleendian CPU which can handle unaligned references */ +#define getu16(x) (*(uint16_t *)(x)) +#define getu32(x) (*(uint32_t *)(x)) +#define getu64(x) (*(uint64_t *)(x)) +#else +static uint16_t getu16(uint8_t *data) +{ + return (uint16_t)data[0] + ((uint16_t)data[1] << 8); +} +static uint32_t getu32(uint8_t *data) +{ + return (uint32_t)getu16(data) + ((uint32_t)getu16(data+2) << 16); +} +static uint64_t getu64(uint8_t *data) +{ + return (uint64_t)getu32(data) + ((uint64_t)getu32(data+4) << 32); +} +#endif + +#define gets8(x) ((int8_t)getu8(x)) +#define gets16(x) ((int16_t)getu16(x)) +#define gets32(x) ((int32_t)getu32(x)) +#define gets64(x) ((int64_t)getu64(x)) + +/* Important: regval must already have been adjusted for rex extensions */ +static int whichreg(int32_t regflags, int regval, int rex) +{ if (!(REG_AL & ~regflags)) return R_AL; if (!(REG_AX & ~regflags)) @@ -62,36 +99,46 @@ static int whichreg(int32_t regflags, int regval) return (regval == 1) ? R_CS : 0; if (!(REG_DESS & ~regflags)) return (regval == 0 || regval == 2 - || regval == 3 ? sreg[regval] : 0); + || regval == 3 ? rd_sreg[regval] : 0); if (!(REG_FSGS & ~regflags)) - return (regval == 4 || regval == 5 ? sreg[regval] : 0); + return (regval == 4 || regval == 5 ? rd_sreg[regval] : 0); if (!(REG_SEG67 & ~regflags)) - return (regval == 6 || regval == 7 ? sreg[regval] : 0); + return (regval == 6 || regval == 7 ? rd_sreg[regval] : 0); - /* All the entries below look up regval in an 8-entry array */ - if (regval < 0 || regval > 7) + /* All the entries below look up regval in an 16-entry array */ + if (regval < 0 || regval > 15) return 0; - if (!((REGMEM | BITS8) & ~regflags)) - return reg8[regval]; + if (!(rex & REX_P) && regval > 7) + return 0; /* Internal error! */ + + if (!((REGMEM | BITS8) & ~regflags)) { + if (rex & REX_P) + return rd_reg8_rex[regval]; + else + return rd_reg8[regval]; + } if (!((REGMEM | BITS16) & ~regflags)) - return reg16[regval]; + return rd_reg16[regval]; if (!((REGMEM | BITS32) & ~regflags)) - return reg32[regval]; + return rd_reg32[regval]; if (!(REG_SREG & ~regflags)) - return sreg[regval]; + return rd_sreg[regval & 7]; /* Ignore REX */ if (!(REG_CREG & ~regflags)) - return creg[regval]; + return rd_creg[regval]; if (!(REG_DREG & ~regflags)) - return dreg[regval]; - if (!(REG_TREG & ~regflags)) - return treg[regval]; + return rd_dreg[regval]; + if (!(REG_TREG & ~regflags)) { + if (rex & REX_P) + return 0; /* TR registers are ill-defined with rex */ + return rd_treg[regval]; + } if (!(FPUREG & ~regflags)) - return fpureg[regval]; + return rd_fpureg[regval & 7]; /* Ignore REX */ if (!(MMXREG & ~regflags)) - return mmxreg[regval]; + return rd_mmxreg[regval & 7]; /* Ignore REX */ if (!(XMMREG & ~regflags)) - return xmmreg[regval]; + return rd_xmmreg[regval]; return 0; } @@ -109,7 +156,7 @@ static const char *whichcond(int condval) * Process an effective address (ModRM) specification. */ static uint8_t *do_ea(uint8_t *data, int modrm, int asize, - int segsize, operand * op) + int segsize, operand * op, int rex) { int mod, rm, scale, index, base; @@ -175,7 +222,7 @@ static uint8_t *do_ea(uint8_t *data, int modrm, int asize, break; case 1: op->segment |= SEG_DISP8; - op->offset = (char)*data++; + op->offset = (int8_t)*data++; break; case 2: op->segment |= SEG_DISP16; @@ -189,40 +236,35 @@ static uint8_t *do_ea(uint8_t *data, int modrm, int asize, * Once again, specifies displacement size (this time * none, byte or *dword*), while specifies the base * register. Again, [EBP] is missing, replaced by a pure - * disp32 (this time that's mod=0,rm=*5*). However, rm=4 + * disp32 (this time that's mod=0,rm=*5*) in 32-bit mode, + * and RIP-relative addressing in 64-bit mode. + * + * However, rm=4 * indicates not a single base register, but instead the * presence of a SIB byte... */ + int a64 = asize == 64; + op->indexreg = -1; - switch (rm) { - case 0: - op->basereg = R_EAX; - break; - case 1: - op->basereg = R_ECX; - break; - case 2: - op->basereg = R_EDX; - break; - case 3: - op->basereg = R_EBX; - break; - case 5: - op->basereg = R_EBP; - break; - case 6: - op->basereg = R_ESI; - break; - case 7: - op->basereg = R_EDI; - break; - } + + if (a64) + op->basereg = rd_reg64[rm | ((rex & REX_B) ? 8 : 0)]; + else + op->basereg = rd_reg32[rm | ((rex & REX_B) ? 8 : 0)]; + if (rm == 5 && mod == 0) { - op->basereg = -1; - if (segsize != 32) - op->addr_size = 32; - mod = 2; /* fake disp32 */ + if (segsize == 64) { + op->basereg = R_RIP; + op->segment |= SEG_RELATIVE; + mod = 2; /* fake disp32 */ + } else { + op->basereg = -1; + if (segsize != 32) + op->addr_size = 32; + mod = 2; /* fake disp32 */ + } } + if (rm == 4) { /* process SIB */ scale = (*data >> 6) & 03; index = (*data >> 3) & 07; @@ -230,78 +272,36 @@ static uint8_t *do_ea(uint8_t *data, int modrm, int asize, data++; op->scale = 1 << scale; - switch (index) { - case 0: - op->indexreg = R_EAX; - break; - case 1: - op->indexreg = R_ECX; - break; - case 2: - op->indexreg = R_EDX; - break; - case 3: - op->indexreg = R_EBX; - break; - case 4: - op->indexreg = -1; - break; - case 5: - op->indexreg = R_EBP; - break; - case 6: - op->indexreg = R_ESI; - break; - case 7: - op->indexreg = R_EDI; - break; - } - switch (base) { - case 0: - op->basereg = R_EAX; - break; - case 1: - op->basereg = R_ECX; - break; - case 2: - op->basereg = R_EDX; - break; - case 3: - op->basereg = R_EBX; - break; - case 4: - op->basereg = R_ESP; - break; - case 6: - op->basereg = R_ESI; - break; - case 7: - op->basereg = R_EDI; - break; - case 5: - if (mod == 0) { - mod = 2; - op->basereg = -1; - } else - op->basereg = R_EBP; - break; - } + if (index == 4) + op->indexreg = -1; /* ESP/RSP/R12 cannot be an index */ + else if (a64) + op->indexreg = rd_reg64[index | ((rex & REX_X) ? 8 : 0)]; + else + op->indexreg = rd_reg64[index | ((rex & REX_X) ? 8 : 0)]; + + if (base == 5 && mod == 0) { + op->basereg = -1; + mod = 2; /* Fake disp32 */ + } else if (a64) + op->basereg = rd_reg64[base | ((rex & REX_B) ? 8 : 0)]; + else + op->basereg = rd_reg32[base | ((rex & REX_B) ? 8 : 0)]; } + switch (mod) { case 0: op->segment |= SEG_NODISP; break; case 1: op->segment |= SEG_DISP8; - op->offset = (char)*data++; + op->offset = gets8(data); + data++; break; case 2: op->segment |= SEG_DISP32; - op->offset = *data++; - op->offset |= ((unsigned)*data++) << 8; - op->offset |= ((int32_t)*data++) << 16; - op->offset |= ((int32_t)*data++) << 24; + op->offset = getu32(data); + data += 4; break; } return data; @@ -313,12 +313,23 @@ static uint8_t *do_ea(uint8_t *data, int modrm, int asize, * stream in data. Return the number of bytes matched if so. */ static int matches(struct itemplate *t, uint8_t *data, int asize, - int osize, int segsize, int rep, insn * ins) + int osize, int segsize, int rep, insn * ins, + int rex, int *rexout) { uint8_t *r = (uint8_t *)(t->code); uint8_t *origdata = data; int a_used = FALSE, o_used = FALSE; int drep = 0; + + *rexout = rex; + + if (segsize == 64) { + if (t->flags & IF_NOLONG) + return FALSE; + } else { + if (t->flags & IF_X64) + return FALSE; + } if (rep == 0xF2) drep = P_REPNE; @@ -402,7 +413,7 @@ static int matches(struct itemplate *t, uint8_t *data, int asize, if (*data++) return FALSE; if (c >= 014 && c <= 016) { - ins->oprs[c - 014].offset = (char)*data++; + ins->oprs[c - 014].offset = (int8_t)*data++; ins->oprs[c - 014].segment |= SEG_SIGNED; } if (c >= 020 && c <= 022) @@ -410,55 +421,60 @@ static int matches(struct itemplate *t, uint8_t *data, int asize, if (c >= 024 && c <= 026) ins->oprs[c - 024].offset = *data++; if (c >= 030 && c <= 032) { - ins->oprs[c - 030].offset = *data++; - ins->oprs[c - 030].offset |= (((unsigned)*data++) << 8); + ins->oprs[c - 030].offset = getu16(data); + data += 2; } if (c >= 034 && c <= 036) { - ins->oprs[c - 034].offset = *data++; - ins->oprs[c - 034].offset |= (((unsigned)*data++) << 8); - if (osize == 32) { - ins->oprs[c - 034].offset |= (((int32_t)*data++) << 16); - ins->oprs[c - 034].offset |= (((int32_t)*data++) << 24); - } + if (osize == 32) { + ins->oprs[c - 034].offset = getu32(data); + data += 4; + } else { + ins->oprs[c - 034].offset = getu16(data); + data += 2; + } if (segsize != asize) ins->oprs[c - 034].addr_size = asize; } if (c >= 040 && c <= 042) { - ins->oprs[c - 040].offset = *data++; - ins->oprs[c - 040].offset |= (((unsigned)*data++) << 8); - ins->oprs[c - 040].offset |= (((int32_t)*data++) << 16); - ins->oprs[c - 040].offset |= (((int32_t)*data++) << 24); + ins->oprs[c - 040].offset = getu32(data); + data += 4; } if (c >= 044 && c <= 046) { - ins->oprs[c - 044].offset = *data++; - ins->oprs[c - 044].offset |= (((unsigned)*data++) << 8); - if (asize == 32) { - ins->oprs[c - 044].offset |= (((int32_t)*data++) << 16); - ins->oprs[c - 044].offset |= (((int32_t)*data++) << 24); - } + /* hpa: should this be gets32/gets16? */ + if (asize == 32) { + ins->oprs[c - 044].offset = getu32(data); + data += 4; + } else { + ins->oprs[c - 044].offset = getu16(data); + data += 2; + } if (segsize != asize) ins->oprs[c - 044].addr_size = asize; } if (c >= 050 && c <= 052) { - ins->oprs[c - 050].offset = (char)*data++; + ins->oprs[c - 050].offset = gets8(data++); ins->oprs[c - 050].segment |= SEG_RELATIVE; } + if (c >= 054 && c <= 056) { + ins->oprs[c - 054].offset = getu64(data); + data += 8; + } if (c >= 060 && c <= 062) { - ins->oprs[c - 060].offset = *data++; - ins->oprs[c - 060].offset |= (((unsigned)*data++) << 8); + ins->oprs[c - 060].offset = gets16(data); + data += 2; ins->oprs[c - 060].segment |= SEG_RELATIVE; ins->oprs[c - 060].segment &= ~SEG_32BIT; } if (c >= 064 && c <= 066) { - ins->oprs[c - 064].offset = *data++; - ins->oprs[c - 064].offset |= (((unsigned)*data++) << 8); - if (osize == 32) { - ins->oprs[c - 064].offset |= (((int32_t)*data++) << 16); - ins->oprs[c - 064].offset |= (((int32_t)*data++) << 24); + if (osize == 32) { + ins->oprs[c - 064].offset = getu32(data); + data += 4; ins->oprs[c - 064].segment |= SEG_32BIT; - } else + } else { + ins->oprs[c - 064].offset = getu16(data); + data += 2; ins->oprs[c - 064].segment &= ~SEG_32BIT; - ins->oprs[c - 064].segment |= SEG_RELATIVE; + } if (segsize != osize) { ins->oprs[c - 064].type = (ins->oprs[c - 064].type & NON_SIZE) @@ -466,10 +482,8 @@ static int matches(struct itemplate *t, uint8_t *data, int asize, } } if (c >= 070 && c <= 072) { - ins->oprs[c - 070].offset = *data++; - ins->oprs[c - 070].offset |= (((unsigned)*data++) << 8); - ins->oprs[c - 070].offset |= (((int32_t)*data++) << 16); - ins->oprs[c - 070].offset |= (((int32_t)*data++) << 24); + ins->oprs[c - 070].offset = getu32(data); + data += 4; ins->oprs[c - 070].segment |= SEG_32BIT | SEG_RELATIVE; } if (c >= 0100 && c < 0130) { @@ -477,24 +491,22 @@ static int matches(struct itemplate *t, uint8_t *data, int asize, ins->oprs[c & 07].basereg = (modrm >> 3) & 07; ins->oprs[c & 07].segment |= SEG_RMREG; data = do_ea(data, modrm, asize, segsize, - &ins->oprs[(c >> 3) & 07]); + &ins->oprs[(c >> 3) & 07], rex); } if (c >= 0130 && c <= 0132) { - ins->oprs[c - 0130].offset = *data++; - ins->oprs[c - 0130].offset |= (((unsigned)*data++) << 8); + ins->oprs[c - 0130].offset = getu16(data); + data += 2; } if (c >= 0140 && c <= 0142) { - ins->oprs[c - 0140].offset = *data++; - ins->oprs[c - 0140].offset |= (((unsigned)*data++) << 8); - ins->oprs[c - 0140].offset |= (((int32_t)*data++) << 16); - ins->oprs[c - 0140].offset |= (((int32_t)*data++) << 24); + ins->oprs[c - 0140].offset = getu32(data); + data += 4; } if (c >= 0200 && c <= 0277) { int modrm = *data++; if (((modrm >> 3) & 07) != (c & 07)) return FALSE; /* spare field doesn't match up */ data = do_ea(data, modrm, asize, segsize, - &ins->oprs[(c >> 3) & 07]); + &ins->oprs[(c >> 3) & 07], rex); } if (c >= 0300 && c <= 0302) { if (asize) @@ -504,7 +516,7 @@ static int matches(struct itemplate *t, uint8_t *data, int asize, a_used = TRUE; } if (c == 0310) { - if (asize == 32) + if (asize != 16) return FALSE; else a_used = TRUE; @@ -522,7 +534,7 @@ static int matches(struct itemplate *t, uint8_t *data, int asize, a_used = TRUE; } if (c == 0320) { - if (osize == 32) + if (osize != 16) return FALSE; else o_used = TRUE; @@ -534,11 +546,19 @@ static int matches(struct itemplate *t, uint8_t *data, int asize, o_used = TRUE; } if (c == 0322) { - if (osize != segsize) + if (osize != (segsize == 16) ? 16 : 32) return FALSE; else o_used = TRUE; } + if (c == 0323) { + rex |= REX_W; /* 64-bit only instruction */ + osize = 64; + } + if (c == 0324) { + if (!(rex & REX_P)) + return FALSE; + } if (c == 0330) { int t = *r++, d = *data++; if (d < t || d > t + 15) @@ -568,10 +588,15 @@ static int matches(struct itemplate *t, uint8_t *data, int asize, if (drep) ins->prefixes[ins->nprefix++] = drep; if (!a_used && asize != segsize) - ins->prefixes[ins->nprefix++] = (asize == 16 ? P_A16 : P_A32); - if (!o_used && osize != segsize) - ins->prefixes[ins->nprefix++] = (osize == 16 ? P_O16 : P_O32); + ins->prefixes[ins->nprefix++] = asize == 16 ? P_A16 : P_A32; + if (!o_used && osize == ((segsize == 16) ? 32 : 16)) { + fprintf(stderr, "osize = %d, segsize = %d\n", osize, segsize); + ins->prefixes[ins->nprefix++] = osize == 16 ? P_O16 : P_O32; + } + /* Fix: check for redundant REX prefixes */ + + *rexout = rex; return data - origdata; } @@ -581,7 +606,7 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, struct itemplate **p, **best_p; int length, best_length = 0; char *segover; - int rep, lock, asize, osize, i, slen, colon; + int rep, lock, asize, osize, i, slen, colon, rex, rexout, best_rex; uint8_t *origdata; int works; insn tmp_ins, ins; @@ -590,7 +615,9 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, /* * Scan for prefixes. */ - asize = osize = segsize; + asize = segsize; + osize = (segsize == 64) ? 32 : segsize; + rex = 0; segover = NULL; rep = lock = 0; origdata = data; @@ -599,46 +626,47 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, rep = *data++; else if (*data == 0xF0) lock = *data++; - else if (*data == 0x2E || *data == 0x36 || *data == 0x3E || - *data == 0x26 || *data == 0x64 || *data == 0x65) { - switch (*data++) { - case 0x2E: - segover = "cs"; - break; - case 0x36: - segover = "ss"; - break; - case 0x3E: - segover = "ds"; - break; - case 0x26: - segover = "es"; - break; - case 0x64: - segover = "fs"; - break; - case 0x65: - segover = "gs"; - break; - } - } else if (*data == 0x66) - osize = 48 - segsize, data++; - else if (*data == 0x67) - asize = 48 - segsize, data++; - else + else if (*data == 0x2E) + segover = "cs", data++; + else if (*data == 0x36) + segover = "ss", data++; + else if (*data == 0x3E) + segover = "ds", data++; + else if (*data == 0x26) + segover = "es", data++; + else if (*data == 0x64) + segover = "fs", data++; + else if (*data == 0x65) + segover = "gs", data++; + else if (*data == 0x66) { + if (segsize != 64) /* 66 prefix is ignored in 64-bit mode */ + osize = 48 - segsize; + data++; + } else if (*data == 0x67) { + asize = (segsize == 32) ? 16 : 32; + data++; + } else if (segsize == 64 && (*data & 0xf0) == REX_P) { + rex = *data++; + if (rex & REX_W) + osize = 64; + break; /* REX is always the last prefix */ + } else { break; + } } tmp_ins.oprs[0].segment = tmp_ins.oprs[1].segment = tmp_ins.oprs[2].segment = tmp_ins.oprs[0].addr_size = tmp_ins.oprs[1].addr_size = - tmp_ins.oprs[2].addr_size = (segsize == 16 ? 0 : SEG_32BIT); + tmp_ins.oprs[2].addr_size = (segsize == 64 ? SEG_64BIT : + segsize == 32 ? SEG_32BIT : 0); tmp_ins.condition = -1; - best = ~0UL; /* Worst possible */ + best = -1; /* Worst possible */ best_p = NULL; + best_rex = 0; for (p = itable[*data]; *p; p++) { if ((length = matches(*p, data, asize, osize, - segsize, rep, &tmp_ins))) { + segsize, rep, &tmp_ins, rex, &rexout))) { works = TRUE; /* * Final check to make sure the types of r/m match up. @@ -656,7 +684,7 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, ((((*p)->opd[i] & (REGISTER | FPUREG)) || (tmp_ins.oprs[i].segment & SEG_RMREG)) && !whichreg((*p)->opd[i], - tmp_ins.oprs[i].basereg))) { + tmp_ins.oprs[i].basereg, rexout))) { works = FALSE; break; } @@ -670,6 +698,7 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, best_p = p; best_length = length; ins = tmp_ins; + best_rex = rexout; } } } @@ -681,6 +710,9 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, /* Pick the best match */ p = best_p; length = best_length; + rex = best_rex; + if (best_rex & REX_W) + osize = 64; slen = 0; @@ -739,8 +771,8 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, /* * sort out wraparound */ - if (!(ins.oprs[i].segment & SEG_32BIT)) - ins.oprs[i].offset &= 0xFFFF; + if (!(ins.oprs[i].segment & (SEG_32BIT|SEG_64BIT))) + ins.oprs[i].offset &= 0xffff; /* * add sync marker, if autosync is on */ @@ -756,7 +788,7 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, if (((*p)->opd[i] & (REGISTER | FPUREG)) || (ins.oprs[i].segment & SEG_RMREG)) { ins.oprs[i].basereg = whichreg((*p)->opd[i], - ins.oprs[i].basereg); + ins.oprs[i].basereg, rex); if ((*p)->opd[i] & TO) slen += snprintf(output + slen, outbufsize - slen, "to "); slen += snprintf(output + slen, outbufsize - slen, "%s", @@ -781,6 +813,9 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, } else if ((*p)->opd[i] & BITS32) { slen += snprintf(output + slen, outbufsize - slen, "dword "); + } else if ((*p)->opd[i] & BITS64) { + slen += + snprintf(output + slen, outbufsize - slen, "qword "); } else if ((*p)->opd[i] & NEAR) { slen += snprintf(output + slen, outbufsize - slen, "near "); @@ -825,9 +860,10 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, output[slen++] = '['; if (ins.oprs[i].addr_size) slen += snprintf(output + slen, outbufsize - slen, "%s", - (ins.oprs[i].addr_size == 32 ? "dword " : - ins.oprs[i].addr_size == - 16 ? "word " : "")); + (ins.oprs[i].addr_size == 64 ? "qword " : + ins.oprs[i].addr_size == 32 ? "dword " : + ins.oprs[i].addr_size == 16 ? "word " : + "")); if (segover) { slen += snprintf(output + slen, outbufsize - slen, "%s:", @@ -853,26 +889,39 @@ int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize, started = TRUE; } if (ins.oprs[i].segment & SEG_DISP8) { - int sign = '+'; - if (ins.oprs[i].offset & 0x80) { - ins.oprs[i].offset = -(char)ins.oprs[i].offset; - sign = '-'; - } + int minus = 0; + int8_t offset = ins.oprs[i].offset; + if (offset < 0) { + minus = 1; + offset = -offset; + } slen += - snprintf(output + slen, outbufsize - slen, "%c0x%"PRIx64"", - sign, ins.oprs[i].offset); + snprintf(output + slen, outbufsize - slen, "%s0x%"PRIx8"", + minus ? "-" : "+", offset); } else if (ins.oprs[i].segment & SEG_DISP16) { - if (started) - output[slen++] = '+'; + int minus = 0; + int16_t offset = ins.oprs[i].offset; + if (offset < 0) { + minus = 1; + offset = -offset; + } slen += - snprintf(output + slen, outbufsize - slen, "0x%"PRIx64"", - ins.oprs[i].offset); + snprintf(output + slen, outbufsize - slen, "%s0x%"PRIx16"", + minus ? "-" : started ? "+" : "", offset); } else if (ins.oprs[i].segment & SEG_DISP32) { - if (started) - output[slen++] = '+'; - slen += - snprintf(output + slen, outbufsize - slen, "0x%"PRIx64"", - ins.oprs[i].offset); + char *prefix = ""; + int32_t offset = ins.oprs[i].offset; + if (ins.oprs[i].basereg == R_RIP) { + prefix = ":"; + } else if (offset < 0) { + offset = -offset; + prefix = "-"; + } else { + prefix = started ? "+" : ""; + } + slen += + snprintf(output + slen, outbufsize - slen, + "%s0x%"PRIx32"", prefix, offset); } output[slen++] = ']'; } else { diff --git a/nasm.h b/nasm.h index e860eadc..92f678ec 100644 --- a/nasm.h +++ b/nasm.h @@ -223,7 +223,7 @@ enum { /* token types, other than chars */ typedef struct { int32_t segment; - int32_t offset; + int64_t offset; int known; } loc_t; diff --git a/regs.pl b/regs.pl index e96029a2..c412c660 100755 --- a/regs.pl +++ b/regs.pl @@ -118,7 +118,7 @@ if ( $fmt eq 'h' ) { # Output regdis.c print "/* automatically generated from $file - do not edit */\n"; foreach $class ( sort(keys(%disclass)) ) { - printf "static const int %-8s[] = {", $class; + printf "static const int rd_%-8s[] = {", $class; @foo = @{$disclass{$class}}; @bar = (); for ( $i = 0 ; $i < scalar(@foo) ; $i++ ) {