diff options
| author | serpilliere <fabrice.desclaux@cea.fr> | 2016-04-24 23:40:40 +0200 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2016-04-26 11:05:41 +0200 |
| commit | 40b60e86b51d9bb47c1511344bf95a1072357b27 (patch) | |
| tree | 8b70b817d6debccc1e451512143a4d54d875e035 /miasm2/jitter/Jitgcc.c | |
| parent | b127dbdfe5832a12f7f328dc560344a9900e8918 (diff) | |
| download | miasm-40b60e86b51d9bb47c1511344bf95a1072357b27.tar.gz miasm-40b60e86b51d9bb47c1511344bf95a1072357b27.zip | |
Jitter: add gcc backend
Diffstat (limited to 'miasm2/jitter/Jitgcc.c')
| -rw-r--r-- | miasm2/jitter/Jitgcc.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/miasm2/jitter/Jitgcc.c b/miasm2/jitter/Jitgcc.c new file mode 100644 index 00000000..3e7225cb --- /dev/null +++ b/miasm2/jitter/Jitgcc.c @@ -0,0 +1,87 @@ +#include <Python.h> +#include <inttypes.h> +#include <stdint.h> + +typedef struct { + uint8_t is_local; + uint64_t address; +} block_id; + +typedef int (*jitted_func)(block_id*, PyObject*); + + +PyObject* gcc_exec_bloc(PyObject* self, PyObject* args) +{ + jitted_func func; + PyObject* jitcpu; + PyObject* func_py; + PyObject* lbl2ptr; + PyObject* breakpoints; + PyObject* retaddr = NULL; + int status; + block_id BlockDst; + + if (!PyArg_ParseTuple(args, "OOOO", &retaddr, &jitcpu, &lbl2ptr, &breakpoints)) + return NULL; + + /* The loop will decref retaddr always once */ + Py_INCREF(retaddr); + + for (;;) { + // Init + BlockDst.is_local = 0; + BlockDst.address = 0; + + // Get the expected jitted function address + func_py = PyDict_GetItem(lbl2ptr, retaddr); + if (func_py) + func = (jitted_func) PyInt_AsLong((PyObject*) func_py); + else { + if (BlockDst.is_local == 1) { + fprintf(stderr, "return on local label!\n"); + exit(1); + } + // retaddr is not jitted yet + return retaddr; + } + + // Execute it + status = func(&BlockDst, jitcpu); + Py_DECREF(retaddr); + retaddr = PyLong_FromUnsignedLongLong(BlockDst.address); + + // Check exception + if (status) + return retaddr; + + // Check breakpoint + if (PyDict_Contains(breakpoints, retaddr)) + return retaddr; + } +} + + + +static PyObject *GccError; + + +static PyMethodDef GccMethods[] = { + {"gcc_exec_bloc", gcc_exec_bloc, METH_VARARGS, + "gcc exec bloc"}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +PyMODINIT_FUNC +initJitgcc(void) +{ + PyObject *m; + + m = Py_InitModule("Jitgcc", GccMethods); + if (m == NULL) + return; + + GccError = PyErr_NewException("gcc.error", NULL, NULL); + Py_INCREF(GccError); + PyModule_AddObject(m, "error", GccError); +} + |