about summary refs log tree commit diff stats
path: root/miasm/tools/emul_lib/libcodenat_tcc.c
blob: 371db4a835abf7f0e360786f5799086182679cda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
** Copyright (C) 2011 EADS France, Fabrice Desclaux <fabrice.desclaux@eads.net>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along
** with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <Python.h>

#include <inttypes.h>
#include <libtcc.h>



/* tcc global state */
TCCState *tcc_state = NULL;


char *emul_lib_dir = NULL;
char *emul_lib_path = NULL;
char *emul_libpython_dir = NULL;

PyObject* tcc_set_emul_lib_path(PyObject* self, PyObject* args)
{
	char* libdir;
	char* libpath;
	char* libpython_dir;
	if (!PyArg_ParseTuple(args, "sss", &libdir, &libpath, &libpython_dir))
		return NULL;
	emul_lib_dir = (char*)malloc(strlen(libdir)+1);
	emul_lib_path = (char*)malloc(strlen(libpath)+1);
	emul_libpython_dir = (char*)malloc(strlen(libpython_dir)+1);
	strcpy(emul_lib_dir, libdir);
	strcpy(emul_lib_path, libpath);
	strcpy(emul_libpython_dir, libpython_dir);
	return Py_None;
}

void tcc_init_state(void)
{
	tcc_state = tcc_new();
	if (!tcc_state) {
		fprintf(stderr, "Impossible de creer un contexte TCC\n");
		exit(1);
	}
	tcc_set_output_type(tcc_state, TCC_OUTPUT_MEMORY);

	tcc_add_include_path(tcc_state, emul_libpython_dir);
	tcc_add_file(tcc_state, emul_lib_path);
}




PyObject*  tcc_exec_bloc(PyObject* self, PyObject* args)
{
	uint64_t (*func)(void);

	unsigned long ret;
	if (!PyArg_ParseTuple(args, "i", &func))
		return NULL;
	ret = func();
	return PyLong_FromUnsignedLong(ret);
}

PyObject* tcc_compil(PyObject* self, PyObject* args)
{
	char* func_name;
	char* func_code;
	int (*entry)(void);

	if (!PyArg_ParseTuple(args, "ss", &func_name, &func_code))
		return NULL;

	tcc_init_state();
	if (tcc_compile_string(tcc_state, func_code) != 0) {
		printf("Erreur de compilation !\n");
		printf("%s\n", func_code);
		exit(0);
	}
	/* XXX use tinycc devel with -fPIC patch in makefile */
	if (tcc_relocate(tcc_state) < 0)
		exit(0);
	entry = tcc_get_symbol(tcc_state, func_name);
	if (!entry){
		printf("Erreur de symbole !\n");
		printf("%s\n", func_name);
		exit(0);
	}

	return PyInt_FromLong((long)entry);

}


static PyObject *TccError;


static PyMethodDef TccMethods[] = {
    {"tcc_set_emul_lib_path",  tcc_set_emul_lib_path, METH_VARARGS,
     "init tcc path"},
    {"tcc_exec_bloc",  tcc_exec_bloc, METH_VARARGS,
     "tcc exec bloc"},
    {"tcc_compil",  tcc_compil, METH_VARARGS,
     "tcc compil"},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC
initlibcodenat_tcc(void)
{
    PyObject *m;

    m = Py_InitModule("libcodenat_tcc", TccMethods);
    if (m == NULL)
	    return;

    TccError = PyErr_NewException("tcc.error", NULL, NULL);
    Py_INCREF(TccError);
    PyModule_AddObject(m, "error", TccError);
}