2020-08-25 11:23:25 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-08-25 11:23:25 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-08-25 11:23:25 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/fmt/bing.internal.h"
|
2020-12-09 23:04:54 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/tpenc.h"
|
2022-12-11 22:30:50 +00:00
|
|
|
#include "libc/limits.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/log/check.h"
|
2022-08-19 00:41:32 +00:00
|
|
|
#include "libc/mem/arraylist2.internal.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/mem/mem.h"
|
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "third_party/xed/x86.h"
|
2020-09-28 08:13:56 +00:00
|
|
|
#include "tool/build/lib/demangle.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "tool/build/lib/dis.h"
|
2020-10-01 08:20:13 +00:00
|
|
|
#include "tool/build/lib/endian.h"
|
2020-09-28 08:13:56 +00:00
|
|
|
#include "tool/build/lib/high.h"
|
2020-08-27 06:08:08 +00:00
|
|
|
#include "tool/build/lib/memory.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "tool/build/lib/modrm.h"
|
|
|
|
|
|
|
|
#define ADDRLEN 8
|
|
|
|
#define BYTELEN 11
|
|
|
|
#define PFIXLEN 4
|
|
|
|
#define NAMELEN 8
|
|
|
|
#define CODELEN 40
|
|
|
|
#define CODELIM 15
|
|
|
|
#define DATALIM 8
|
|
|
|
#define PIVOTOP pos_opcode
|
|
|
|
|
|
|
|
static char *DisColumn(char *p2, char *p1, long need) {
|
|
|
|
char *p;
|
|
|
|
unsigned long have;
|
|
|
|
DCHECK_GE(p2, p1);
|
|
|
|
have = p2 - p1;
|
|
|
|
p = p2;
|
|
|
|
do {
|
|
|
|
*p++ = ' ';
|
|
|
|
} while (++have < need);
|
|
|
|
*p = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *DisOctets(char *p, const uint8_t *d, size_t n) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (i) *p++ = ',';
|
|
|
|
*p++ = '0';
|
|
|
|
*p++ = 'x';
|
|
|
|
*p++ = "0123456789abcdef"[(d[i] & 0xf0) >> 4];
|
|
|
|
*p++ = "0123456789abcdef"[(d[i] & 0x0f) >> 0];
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *DisByte(char *p, const uint8_t *d, size_t n) {
|
2020-09-28 08:13:56 +00:00
|
|
|
p = HighStart(p, g_high.keyword);
|
2020-08-25 11:23:25 +00:00
|
|
|
p = DisColumn(stpcpy(p, ".byte"), p, NAMELEN);
|
2020-09-28 08:13:56 +00:00
|
|
|
p = HighEnd(p);
|
2020-08-25 11:23:25 +00:00
|
|
|
p = DisOctets(p, d, n);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:13:56 +00:00
|
|
|
static char *DisError(struct Dis *d, char *p) {
|
|
|
|
p = DisColumn(DisByte(p, d->xedd->bytes, MIN(15, d->xedd->length)), p,
|
|
|
|
CODELEN);
|
|
|
|
p = HighStart(p, g_high.comment);
|
2020-08-25 11:23:25 +00:00
|
|
|
*p++ = '#';
|
|
|
|
*p++ = ' ';
|
2020-10-27 10:39:46 +00:00
|
|
|
p = stpcpy(p, IndexDoubleNulString(kXedErrorNames, d->xedd->op.error));
|
2020-09-28 08:13:56 +00:00
|
|
|
p = HighEnd(p);
|
2020-08-25 11:23:25 +00:00
|
|
|
*p = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:13:56 +00:00
|
|
|
static char *DisAddr(struct Dis *d, char *p) {
|
2021-02-08 17:19:00 +00:00
|
|
|
int64_t x = d->addr;
|
2021-10-02 15:17:04 +00:00
|
|
|
if (0 <= x && x < 0x10fff0) {
|
|
|
|
return p + uint64toarray_fixed16(x, p, 24);
|
2022-12-11 22:30:50 +00:00
|
|
|
} else if (INT_MIN <= x && x <= INT_MAX) {
|
2021-02-08 17:19:00 +00:00
|
|
|
return p + uint64toarray_fixed16(x, p, 32);
|
2020-08-25 11:23:25 +00:00
|
|
|
} else {
|
2021-02-08 17:19:00 +00:00
|
|
|
return p + uint64toarray_fixed16(x, p, 48);
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:13:56 +00:00
|
|
|
static char *DisRaw(struct Dis *d, char *p) {
|
2020-08-25 11:23:25 +00:00
|
|
|
long i;
|
2021-10-02 15:17:04 +00:00
|
|
|
int plen;
|
|
|
|
if (0 <= d->addr && d->addr < 0x10fff0) {
|
|
|
|
plen = 2;
|
|
|
|
} else {
|
|
|
|
plen = PFIXLEN;
|
|
|
|
}
|
|
|
|
for (i = 0; i < plen - MIN(plen, d->xedd->op.PIVOTOP); ++i) {
|
2020-08-25 11:23:25 +00:00
|
|
|
*p++ = ' ';
|
|
|
|
*p++ = ' ';
|
|
|
|
}
|
2020-09-28 08:13:56 +00:00
|
|
|
for (i = 0; i < MIN(15, d->xedd->length); ++i) {
|
|
|
|
if (i == d->xedd->op.PIVOTOP) *p++ = ' ';
|
|
|
|
*p++ = "0123456789abcdef"[(d->xedd->bytes[i] & 0xf0) >> 4];
|
2021-05-01 12:11:35 +00:00
|
|
|
*p++ = "0123456789abcdef"[(d->xedd->bytes[i] & 0x0f) >> 0];
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:13:56 +00:00
|
|
|
static char *DisCode(struct Dis *d, char *p) {
|
2020-08-25 11:23:25 +00:00
|
|
|
char optspecbuf[128];
|
2020-09-28 08:13:56 +00:00
|
|
|
if (!d->xedd->op.error) {
|
|
|
|
return DisInst(d, p, DisSpec(d->xedd, optspecbuf));
|
2020-08-25 11:23:25 +00:00
|
|
|
} else {
|
2020-09-28 08:13:56 +00:00
|
|
|
return DisError(d, p);
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:13:56 +00:00
|
|
|
static char *DisLineCode(struct Dis *d, char *p) {
|
2021-10-02 15:17:04 +00:00
|
|
|
int blen, plen;
|
|
|
|
if (0 <= d->addr && d->addr < 0x10fff0) {
|
|
|
|
plen = 2;
|
|
|
|
blen = 6;
|
|
|
|
} else {
|
|
|
|
blen = BYTELEN;
|
|
|
|
plen = PFIXLEN;
|
|
|
|
}
|
2020-09-28 08:13:56 +00:00
|
|
|
p = DisColumn(DisAddr(d, p), p, ADDRLEN);
|
2021-10-02 15:17:04 +00:00
|
|
|
p = DisColumn(DisRaw(d, p), p, plen * 2 + 1 + blen * 2);
|
2020-09-28 08:13:56 +00:00
|
|
|
p = DisCode(d, p);
|
2020-08-25 11:23:25 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:13:56 +00:00
|
|
|
static char *DisLineData(struct Dis *d, char *p, const uint8_t *b, size_t n) {
|
2020-08-25 11:23:25 +00:00
|
|
|
size_t i;
|
2021-02-04 11:30:47 +00:00
|
|
|
uint64_t w;
|
2020-09-28 08:13:56 +00:00
|
|
|
p = DisColumn(DisAddr(d, p), p, ADDRLEN);
|
|
|
|
p = DisColumn(DisByte(p, b, n), p, 64);
|
|
|
|
p = HighStart(p, g_high.comment);
|
2020-08-25 11:23:25 +00:00
|
|
|
*p++ = '#';
|
|
|
|
*p++ = ' ';
|
2021-02-04 11:30:47 +00:00
|
|
|
for (i = 0; i < n; ++i) {
|
2022-09-13 06:10:38 +00:00
|
|
|
w = _tpenc(bing(b[i], 0));
|
2021-02-04 11:30:47 +00:00
|
|
|
do {
|
|
|
|
*p++ = w;
|
|
|
|
} while ((w >>= 8));
|
|
|
|
}
|
2020-09-28 08:13:56 +00:00
|
|
|
p = HighEnd(p);
|
2020-08-25 11:23:25 +00:00
|
|
|
*p = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:13:56 +00:00
|
|
|
static char *DisLabel(struct Dis *d, char *p, const char *name) {
|
|
|
|
p = DisColumn(DisAddr(d, p), p, ADDRLEN);
|
|
|
|
p = HighStart(p, g_high.label);
|
2020-10-06 06:11:49 +00:00
|
|
|
p = Demangle(p, name, DIS_MAX_SYMBOL_LENGTH);
|
2020-09-28 08:13:56 +00:00
|
|
|
p = HighEnd(p);
|
2020-08-25 11:23:25 +00:00
|
|
|
*p++ = ':';
|
|
|
|
*p = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
long DisFind(struct Dis *d, int64_t addr) {
|
2020-09-28 08:13:56 +00:00
|
|
|
int l, r, m, i;
|
|
|
|
l = 0;
|
|
|
|
r = d->ops.i - 1;
|
|
|
|
while (l <= r) {
|
|
|
|
m = (l + r) >> 1;
|
|
|
|
if (d->ops.p[m].addr < addr) {
|
|
|
|
l = m + 1;
|
|
|
|
} else if (d->ops.p[m].addr > addr) {
|
|
|
|
r = m - 1;
|
|
|
|
} else {
|
|
|
|
return m;
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-01 08:20:13 +00:00
|
|
|
static long DisAppendOpLines(struct Dis *d, struct Machine *m, int64_t addr) {
|
2020-09-07 04:39:00 +00:00
|
|
|
void *r;
|
2020-10-01 08:20:13 +00:00
|
|
|
int64_t ip;
|
2020-09-07 04:39:00 +00:00
|
|
|
unsigned k;
|
2020-08-25 11:23:25 +00:00
|
|
|
struct DisOp op;
|
2020-09-07 04:39:00 +00:00
|
|
|
long i, n, symbol;
|
2020-10-11 04:18:53 +00:00
|
|
|
uint8_t *p, b[15];
|
2020-09-07 04:39:00 +00:00
|
|
|
n = 15;
|
2020-10-01 08:20:13 +00:00
|
|
|
ip = addr - Read64(m->cs);
|
|
|
|
if ((symbol = DisFindSym(d, ip)) != -1) {
|
|
|
|
if (d->syms.p[symbol].addr <= ip &&
|
|
|
|
ip < d->syms.p[symbol].addr + d->syms.p[symbol].size) {
|
|
|
|
n = d->syms.p[symbol].size - (ip - d->syms.p[symbol].addr);
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
2020-10-01 08:20:13 +00:00
|
|
|
if (ip == d->syms.p[symbol].addr && d->syms.p[symbol].name) {
|
2020-09-07 04:39:00 +00:00
|
|
|
op.addr = addr;
|
|
|
|
op.size = 0;
|
|
|
|
op.active = true;
|
2020-09-28 08:13:56 +00:00
|
|
|
d->addr = addr;
|
|
|
|
DisLabel(d, d->buf, d->syms.stab + d->syms.p[symbol].name);
|
2020-09-07 04:39:00 +00:00
|
|
|
if (!(op.s = strdup(d->buf))) return -1;
|
|
|
|
APPEND(&d->ops.p, &d->ops.i, &d->ops.n, &op);
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
2020-09-07 04:39:00 +00:00
|
|
|
}
|
|
|
|
n = MAX(1, MIN(15, n));
|
|
|
|
if (!(r = FindReal(m, addr))) return -1;
|
|
|
|
k = 0x1000 - (addr & 0xfff);
|
|
|
|
if (n <= k) {
|
2020-10-11 04:18:53 +00:00
|
|
|
p = r;
|
2020-09-07 04:39:00 +00:00
|
|
|
} else {
|
2020-10-11 04:18:53 +00:00
|
|
|
p = b;
|
2020-09-07 04:39:00 +00:00
|
|
|
memcpy(b, r, k);
|
|
|
|
if ((r = FindReal(m, addr + k))) {
|
|
|
|
memcpy(b + k, r, n - k);
|
2020-08-25 11:23:25 +00:00
|
|
|
} else {
|
2020-09-07 04:39:00 +00:00
|
|
|
n = k;
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-07 04:39:00 +00:00
|
|
|
xed_decoded_inst_zero_set_mode(d->xedd, m->mode);
|
2020-10-11 04:18:53 +00:00
|
|
|
xed_instruction_length_decode(d->xedd, p, n);
|
2021-02-27 18:33:32 +00:00
|
|
|
d->xedd->op.rde = EncodeRde(d->xedd);
|
2020-09-07 04:39:00 +00:00
|
|
|
n = d->xedd->op.error ? 1 : d->xedd->length;
|
|
|
|
op.addr = addr;
|
|
|
|
op.size = n;
|
|
|
|
op.active = true;
|
|
|
|
op.s = NULL;
|
|
|
|
APPEND(&d->ops.p, &d->ops.i, &d->ops.n, &op);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2020-10-01 08:20:13 +00:00
|
|
|
long Dis(struct Dis *d, struct Machine *m, uint64_t addr, uint64_t ip,
|
|
|
|
int lines) {
|
2020-09-07 04:39:00 +00:00
|
|
|
int64_t i, j, symbol;
|
|
|
|
DisFreeOps(&d->ops);
|
|
|
|
if ((symbol = DisFindSym(d, addr)) != -1 &&
|
|
|
|
(d->syms.p[symbol].addr < addr &&
|
|
|
|
addr < d->syms.p[symbol].addr + d->syms.p[symbol].size)) {
|
|
|
|
for (i = d->syms.p[symbol].addr; i < addr; i += j) {
|
2020-10-01 08:20:13 +00:00
|
|
|
if ((j = DisAppendOpLines(d, m, i)) == -1) return -1;
|
2020-09-07 04:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < lines; ++i, addr += j) {
|
2020-10-01 08:20:13 +00:00
|
|
|
if ((j = DisAppendOpLines(d, m, addr)) == -1) return -1;
|
2020-09-07 04:39:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *DisGetLine(struct Dis *d, struct Machine *m, size_t i) {
|
|
|
|
void *r[2];
|
|
|
|
uint8_t b[15];
|
|
|
|
if (i >= d->ops.i) return "";
|
|
|
|
if (d->ops.p[i].s) return d->ops.p[i].s;
|
|
|
|
DCHECK_LE(d->ops.p[i].size, 15);
|
|
|
|
xed_decoded_inst_zero_set_mode(d->xedd, m->mode);
|
|
|
|
xed_instruction_length_decode(
|
|
|
|
d->xedd, AccessRam(m, d->ops.p[i].addr, d->ops.p[i].size, r, b, true),
|
|
|
|
d->ops.p[i].size);
|
2021-02-27 18:33:32 +00:00
|
|
|
d->xedd->op.rde = EncodeRde(d->xedd);
|
2020-10-01 08:20:13 +00:00
|
|
|
d->m = m;
|
2020-10-11 04:18:53 +00:00
|
|
|
d->addr = d->ops.p[i].addr;
|
|
|
|
CHECK_LT(DisLineCode(d, d->buf) - d->buf, sizeof(d->buf));
|
2020-09-07 04:39:00 +00:00
|
|
|
return d->buf;
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DisFreeOp(struct DisOp *o) {
|
|
|
|
free(o->s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisFreeOps(struct DisOps *ops) {
|
|
|
|
long i;
|
|
|
|
for (i = 0; i < ops->i; ++i) {
|
|
|
|
DisFreeOp(&ops->p[i]);
|
|
|
|
}
|
|
|
|
free(ops->p);
|
2021-09-28 05:58:51 +00:00
|
|
|
bzero(ops, sizeof(*ops));
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DisFree(struct Dis *d) {
|
|
|
|
long i;
|
|
|
|
DisFreeOps(&d->ops);
|
|
|
|
free(d->edges.p);
|
|
|
|
free(d->loads.p);
|
|
|
|
free(d->syms.p);
|
2021-09-28 05:58:51 +00:00
|
|
|
bzero(d, sizeof(*d));
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|