about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorserpilliere <devnull@localhost>2011-09-15 16:32:16 +0200
committerserpilliere <devnull@localhost>2011-09-15 16:32:16 +0200
commit1563f31dd1ba4670410f14774c151491f6662fdb (patch)
tree128255da6dc7fcebaed983e4b2e6c44293d558c3
parent89ac1031a83b96f9f7d6976e2bda3c7a311c1993 (diff)
downloadmiasm-1563f31dd1ba4670410f14774c151491f6662fdb.tar.gz
miasm-1563f31dd1ba4670410f14774c151491f6662fdb.zip
fix tcc include lib
Debian and ubuntu, ... have moved includes by arch and there are missing in tcc default path
-rw-r--r--miasm/tools/codenat.py23
-rw-r--r--miasm/tools/emul_lib/libcodenat_tcc.c50
-rw-r--r--miasm/tools/to_c_helper.py4
-rw-r--r--miasm/tools/win_api.py7
4 files changed, 60 insertions, 24 deletions
diff --git a/miasm/tools/codenat.py b/miasm/tools/codenat.py
index a601ffae..ad60fd9d 100644
--- a/miasm/tools/codenat.py
+++ b/miasm/tools/codenat.py
@@ -126,11 +126,24 @@ def codenat_tcc_load():
     import emul_lib.libcodenat_tcc as libcntcc
     lib_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "emul_lib")
     lib_path = os.path.join(lib_dir, 'libcodenat_tcc.so')
-    libpath = libcodenat_interface.__file__
-    libdir = os.path.dirname(libpath)
-    print libpath
-    print libdir
-    libcntcc.tcc_set_emul_lib_path(libdir, libpath, get_python_inc())
+    libcodenat_path = libcodenat_interface.__file__
+    libdir = os.path.dirname(libcodenat_path)
+
+
+    # XXX HACK
+    # As debian, ubuntu, ... have moved some include files using arch directory,
+    # TCC doesn't know them, so we get the info from GCC
+    # For example /usr/include/x86_64-linux-gnu which contains limits.h
+    from subprocess import Popen, PIPE
+    p = Popen(["cc", "-Wp,-v", "-E", "-"], stdout = PIPE, stderr = PIPE, stdin = PIPE)
+    p.stdin.close()
+    include_path = p.stderr.read().split('\n')
+    include_path = [x[1:] for x in include_path if x.startswith(' /usr/include')]
+    include_path += [libdir, get_python_inc()]
+
+
+    include_path = ";".join(include_path)
+    libcntcc.tcc_set_emul_lib_path(include_path, libcodenat_path)
 
 def codenat_tcc_init():
     global libcntcc
diff --git a/miasm/tools/emul_lib/libcodenat_tcc.c b/miasm/tools/emul_lib/libcodenat_tcc.c
index 3eda63c6..31167f33 100644
--- a/miasm/tools/emul_lib/libcodenat_tcc.c
+++ b/miasm/tools/emul_lib/libcodenat_tcc.c
@@ -26,28 +26,46 @@
 TCCState *tcc_state = NULL;
 
 
-char *emul_lib_dir = NULL;
-char *emul_lib_path = NULL;
-char *emul_libpython_dir = NULL;
+int include_path_array_count = 0;
+char **include_path_array = NULL;
+
+char *libcodenat_path = 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))
+	char* include_path_arg;
+	char* libcodenat_path_arg;
+
+	char* str1, * str2;
+
+	if (!PyArg_ParseTuple(args, "ss",
+			      &include_path_arg,
+			      &libcodenat_path_arg))
 		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);
+
+	if (include_path_array)
+		free(include_path_array);
+
+	str2 = strdup(include_path_arg);
+	while (str2){
+		str1 = strsep(&str2, ";");
+		if (str1){
+			include_path_array_count ++;
+			include_path_array = realloc(include_path_array,
+						     include_path_array_count * sizeof(char*));
+			include_path_array[include_path_array_count-1] = strdup(str1);
+			printf("adding include file: %s\n", str1);
+		}
+	}
+
+	libcodenat_path = (char*)malloc(strlen(libcodenat_path_arg)+1);
+	strcpy(libcodenat_path, libcodenat_path_arg);
 	return Py_None;
 }
 
 void tcc_init_state(void)
 {
+	int i;
 	tcc_state = tcc_new();
 	if (!tcc_state) {
 		fprintf(stderr, "Impossible de creer un contexte TCC\n");
@@ -55,8 +73,10 @@ void tcc_init_state(void)
 	}
 	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);
+	tcc_add_file(tcc_state, libcodenat_path);
+	for (i=0;i<include_path_array_count; i++){
+		tcc_add_include_path(tcc_state, include_path_array[i]);
+	}
 }
 
 
diff --git a/miasm/tools/to_c_helper.py b/miasm/tools/to_c_helper.py
index de665aad..f29ccbd4 100644
--- a/miasm/tools/to_c_helper.py
+++ b/miasm/tools/to_c_helper.py
@@ -289,7 +289,7 @@ def bloc2C(all_bloc, addr2label = None, gen_exception_code = False, dbg_instr =
             if (not filtered_ad) or b.label.offset in filtered_ad:
                 if tick_dbg!=None:
                     out.append('if (my_tick > %d)'%tick_dbg)
-                out.append(r'fprintf(stderr, "%s\n");'%str(b.label.name))
+                out.append(r'fprintf(stdout, "%s\n");'%str(b.label.name))
         
         
         for l in b.lines:
@@ -346,7 +346,7 @@ def bloc2C(all_bloc, addr2label = None, gen_exception_code = False, dbg_instr =
             if dbg_instr and ((not filtered_ad) or l.offset in filtered_ad):
                 if tick_dbg!=None:
                     out.append('if (vmcpu.my_tick > %d)'%tick_dbg)
-                out.append(r'fprintf(stderr, "%s\n");'%str(l))
+                out.append(r'fprintf(stdout, "%s\n");'%str(l))
             else:
                 out.append(r'//%s'%str(l))
 
diff --git a/miasm/tools/win_api.py b/miasm/tools/win_api.py
index 9b8e12b9..f483dd19 100644
--- a/miasm/tools/win_api.py
+++ b/miasm/tools/win_api.py
@@ -858,9 +858,12 @@ def kernel32_GetModuleHandleA():
     print whoami(), hex(ret_ad), hex(dllname)
 
     if dllname:
-        libname = vm_get_str(dllname, 0x100)
-        libname = libname[:libname.find('\x00')]
+        libname = get_str_ansi(dllname)
         print libname
+        if not libname.lower().endswith('.dll'):
+            print 'warning adding .dll to modulename'
+            libname += '.dll'
+            print libname
         eax = runtime_dll.lib_get_add_base(libname)
     else:
         eax = current_pe.NThdr.ImageBase