aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcomex2015-01-10 22:44:55 -0500
committercomex2015-01-10 22:44:55 -0500
commit102a7371f0a19ee5569d1cd2e8761d7ab3fec75b (patch)
tree6e0f718543fde994a8f727b08a6cabfcb7122fae
parentadd licensing (diff)
downloadsubstitute-102a7371f0a19ee5569d1cd2e8761d7ab3fec75b.tar.gz
...
-rw-r--r--Makefile5
-rw-r--r--lib/dis-arm.inc36
-rw-r--r--lib/dis.h47
-rw-r--r--test/test-dis.c19
4 files changed, 106 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 5ae270f..515258d 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,8 @@ all: \
out/libsubstitute.dylib \
out/test-find-syms \
out/test-find-syms-cpp \
- out/test-substrate
+ out/test-substrate \
+ out/test-dis
out:
mkdir out
@@ -24,6 +25,8 @@ LIB_OBJS := out/find-syms.o out/substrate-compat.o
out/libsubstitute.dylib: $(LIB_OBJS) lib/*.h out
$(CC) -dynamiclib -fvisibility=hidden -o $@ $(LIB_OBJS)
+out/test-dis: test/test-dis.c Makefile
+ $(CC) -std=c11 -o $@ $< -Ilib
out/test-%: test/test-%.c Makefile out/libsubstitute.dylib
$(CC) -std=c89 -o $@ $< -Ilib -Lout -lsubstitute
out/test-%-cpp: test/test-%.c Makefile out/libsubstitute.dylib
diff --git a/lib/dis-arm.inc b/lib/dis-arm.inc
new file mode 100644
index 0000000..37581a3
--- /dev/null
+++ b/lib/dis-arm.inc
@@ -0,0 +1,36 @@
+static inline tdis_ret P(addr_offset_none_addr_33_STC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) {}
+static inline tdis_ret P(GPR_Rn_GPR_Rm_1_ADDrr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rn) {
+
+ return P(regs)(ctx, Rm, 0, 4, Rn, 0, 4);
+}
+static inline tdis_ret P(GPR_Rn_3_ADDri)(tdis_ctx ctx, struct bitslice Rn) {
+ return P(regs)(ctx, Rn, 0, 4, Rn, 0, 0);
+}
+static inline tdis_ret P(addrmode3_addr_2_STRD)(tdis_ctx ctx, struct bitslice addr) {
+ return P(regs)(ctx, addr, 9, 4, addr, 0, 4);
+}
+static inline tdis_ret P(addrmode3_pre_addr_2_STRD_PRE)(tdis_ctx ctx, struct bitslice addr) {
+ return P(regs)(ctx, addr, 9, 4, addr, 0, 4);
+}
+static inline tdis_ret P(addrmode_imm12_addr_2_STRBi12)(tdis_ctx ctx, struct bitslice addr) {
+ return P(regs)(ctx, addr, 13, 4, addr, 0, 0);
+}
+static inline tdis_ret P(addrmode_imm12_pre_addr_2_STRB_PRE_IMM)(tdis_ctx ctx, struct bitslice addr) {
+ return P(regs)(ctx, addr, 13, 4, addr, 0, 0);
+}
+static inline tdis_ret P(addrmode5_addr_8_STC2L_OFFSET)(tdis_ctx ctx, struct bitslice addr) {
+ return P(regs)(ctx, addr, 9, 4, addr, 0, 0);
+}
+static inline tdis_ret P(addrmode5_pre_addr_4_STC2L_PRE)(tdis_ctx ctx, struct bitslice addr) {
+ return P(regs)(ctx, addr, 9, 4, addr, 0, 0);
+}
+static inline tdis_ret P(adrlabel_label_1_ADR)(tdis_ctx ctx, struct bitslice label) {
+ return P(adr)(ctx, ctx->pc + 8 + bs_get(label, ctx->op));
+}
+static inline tdis_ret P(br_target_target_1_Bcc)(tdis_ctx ctx, struct bitslice target) {
+ return P(branch)(ctx, ctx->pc + 8 + sext(bs_get(target, ctx->op), 24));
+}
+static inline tdis_ret P(ldst_so_reg_addr_2_STRB_PRE_REG)(tdis_ctx ctx, struct bitslice addr) {
+ return P(regs)(ctx, addr, 13, 4, addr, 0, 4);
+}
+
diff --git a/lib/dis.h b/lib/dis.h
new file mode 100644
index 0000000..8063235
--- /dev/null
+++ b/lib/dis.h
@@ -0,0 +1,47 @@
+#pragma once
+
+static inline int sext(unsigned val, int bits) {
+ return val & (1 << (bits - 1)) ? ((int)val - (1 << bits)) : (int)val;
+}
+
+struct bitslice_run {
+ int inpos, outpos, len;
+};
+
+struct bitslice {
+ int nruns;
+ const struct bitslice_run *runs;
+};
+
+__attribute__((always_inline))
+static inline unsigned bs_get(struct bitslice bs, unsigned op) {
+ unsigned ret = 0;
+ for(int i = 0; i < bs.nruns; i++) {
+ const struct bitslice_run *run = &bs.runs[i];
+ unsigned masked = op & ((1 << run->len) - 1);
+ if (run->outpos < run->inpos)
+ masked >>= run->inpos - run->outpos;
+ else if (run->outpos > run->inpos)
+ masked <<= run->outpos - run->inpos;
+ ret |= masked;
+ }
+ return ret;
+}
+
+__attribute__((always_inline))
+static inline unsigned bs_set(struct bitslice bs, unsigned val, unsigned op) {
+ for(int i = 0; i < bs.nruns; i++) {
+ const struct bitslice_run *run = &bs.runs[i];
+ unsigned mask = (1 << run->len) - 1;
+ unsigned masked = val & mask;
+ if (run->outpos < run->inpos) {
+ masked <<= run->inpos - run->outpos;
+ mask <<= run->inpos - run->outpos;
+ } else if (run->outpos > run->inpos) {
+ masked >>= run->outpos - run->inpos;
+ mask >>= run->outpos - run->inpos;
+ }
+ op = (op & ~mask) | masked;
+ }
+ return op;
+}
diff --git a/test/test-dis.c b/test/test-dis.c
new file mode 100644
index 0000000..127b0b4
--- /dev/null
+++ b/test/test-dis.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include "dis.h"
+unsigned f(unsigned x) {
+ struct bitslice addr = {.nruns = 4, .runs = (struct bitslice_run[]) {{0,0,4}, {5,5,7}, {16,13,4}, {23,12,1}}};
+ return bs_get(addr, x);
+
+}
+unsigned fs(unsigned val, unsigned op) {
+ struct bitslice addr = {.nruns = 4, .runs = (struct bitslice_run[]) {{0,0,4}, {5,5,7}, {16,13,4}, {23,12,1}}};
+ return bs_set(addr, val, op);
+
+}
+int main() {
+ printf("%x\n", f(0xdeadbeef));
+ printf("%x\n", f(0xdeadbeee));
+ printf("%x\n", f(0xfeedface));
+ printf("%x\n", fs(0xdead, 0xdeadbeef));
+
+}