Check-in [adeb3bb79b]
Overview
Comment:Updated to search loaded libraries for symbols on Win32 as we do on UNIX
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: adeb3bb79bccce379eee6d885a05e17d22cf8136
User & Date: rkeene on 2014-05-04 07:03:23
Other Links: manifest | tags
Context
2014-05-04
07:04
tcc4tcl version 0.3 check-in: df44ccaed5 user: rkeene tags: trunk, 0.3
07:03
Updated to search loaded libraries for symbols on Win32 as we do on UNIX check-in: adeb3bb79b user: rkeene tags: trunk
07:02
Removed spurious information from patch check-in: b3f189ad77 user: rkeene tags: trunk
Changes

Added build/tcc-patches/0.9.26/tcc-0.9.26-win32useopenlibs.diff version [269482c81f].



























































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
diff -uNr tcc-0.9.26.orig/tccpe.c tcc-0.9.26-1win32useopenlibs/tccpe.c
--- tcc-0.9.26.orig/tccpe.c	2013-02-15 08:24:00.000000000 -0600
+++ tcc-0.9.26-1win32useopenlibs/tccpe.c	2014-05-04 02:00:45.195490003 -0500
@@ -38,6 +38,9 @@
 # define ADDR3264 DWORD
 #endif
 
+#ifdef TCC_IS_NATIVE
+#include <psapi.h>
+#endif
 #ifdef _WIN32
 void dbg_printf (const char *fmt, ...)
 {
@@ -831,7 +834,26 @@
                     if (dllref) {
                         if ( !dllref->handle )
                             dllref->handle = LoadLibrary(dllref->name);
-                        v = (ADDR3264)GetProcAddress(dllref->handle, name);
+                        if (dllref->handle) {
+                            v = (ADDR3264)GetProcAddress(dllref->handle, name);
+                        }
+                    }
+                    if (!v) {
+                        HANDLE cur_proc = GetCurrentProcess();
+                        HMODULE *modules;
+                        DWORD needed, i;
+
+                        needed = 0;
+                        EnumProcessModules(cur_proc, NULL, 0, &needed);
+                        modules = tcc_malloc(needed);
+                        if (EnumProcessModules(cur_proc, modules, needed, &needed)) {
+                            for (i = 0; i < needed / sizeof(HMODULE); i++) {
+                                v = (ADDR3264)GetProcAddress(modules[i], name);
+                                if (v) {
+                                    break;
+                                }
+                            }
+                        }
                     }
                     if (!v)
                         tcc_error_noabort("undefined symbol '%s'", name);
@@ -1209,9 +1231,34 @@
 
             const char *name = symtab_section->link->data + sym->st_name;
             unsigned type = ELFW(ST_TYPE)(sym->st_info);
-            int imp_sym = pe_find_import(pe->s1, sym);
+            int imp_sym;
             struct import_symbol *is;
 
+            imp_sym = pe_find_import(pe->s1, sym);
+            if (0 == imp_sym) {
+                HANDLE cur_proc = GetCurrentProcess();
+                HMODULE *modules;
+                DWORD needed, i;
+                const char *symname;
+                void *addr;
+
+                symname = pe_export_name(pe->s1, sym);
+
+                needed = 0;
+                EnumProcessModules(cur_proc, NULL, 0, &needed);
+                modules = tcc_malloc(needed);
+                if (EnumProcessModules(cur_proc, modules, needed, &needed)) {
+                    for (i = 0; i < needed / sizeof(HMODULE); i++) {
+                        addr = GetProcAddress(modules[i], symname);
+                        if (addr) {
+                            put_elf_sym( pe->s1->dynsymtab_section, (ADDR3264) addr, sizeof(addr), ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), 0, SHN_UNDEF, symname);
+                            imp_sym = pe_find_import(pe->s1, sym);
+                            break;
+                        }
+                    }
+                }
+            }
+
             if (0 == imp_sym)
                 goto not_found;
 

Modified configure.ac from [77b6918fa2] to [ed00a008d9].

1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
dnl Define ourselves
AC_INIT(tcc4tcl, @@VERS@@)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_GNU_SOURCE


dnl Determine system information
DC_CHK_OS_INFO

dnl Determine if a shared or static build is requested
AC_ARG_ENABLE([static], AS_HELP_STRING([--enable-static], [build static library instead of shared library]), [
	if test "$enableval" = "no"; then








>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dnl Define ourselves
AC_INIT(tcc4tcl, @@VERS@@)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_GNU_SOURCE
AC_LANG(C)

dnl Determine system information
DC_CHK_OS_INFO

dnl Determine if a shared or static build is requested
AC_ARG_ENABLE([static], AS_HELP_STRING([--enable-static], [build static library instead of shared library]), [
	if test "$enableval" = "no"; then
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
fi

if test "${TCC4TCL_TARGET}" = "shared"; then
	dnl Determine how to make shared objects
	DC_GET_SHOBJFLAGS

	dnl Only export symbols we wish to expose
	DC_SETVERSIONSCRIPT([tcc4tcl.syms], [tcc4tcl.vers])
	DC_FIND_STRIP_AND_REMOVESYMS([tcc4tcl.syms])







	TARGET="tcc4tcl.${SHOBJEXT}"


















else
	TARGET="tcc4tcl-static.a"
fi
AC_SUBST(TARGET)
AC_SUBST(TCC4TCL_TARGET)

dnl Find out if we have the functions needed to open shared objects
AC_SEARCH_LIBS(dlopen, dl,, [
        AC_SEARCH_LIBS(shl_load, dld dl)
])
AC_CHECK_FUNCS(dlopen shl_load)

dnl Look for appropriate headers
AC_CHECK_HEADERS(unistd.h stdlib.h string.h strings.h dlfcn.h dl.h)

dnl Perform Tcl Extension required stuff
TCLEXT_INIT







dnl Produce output
AC_OUTPUT(Makefile pkgIndex.tcl tcc4tcl.syms)







<
|

>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

















>
>
>
>
>
>



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
fi

if test "${TCC4TCL_TARGET}" = "shared"; then
	dnl Determine how to make shared objects
	DC_GET_SHOBJFLAGS

	dnl Only export symbols we wish to expose

	TARGET="tcc4tcl.${SHOBJEXT}"

	AC_CHECK_HEADERS(windows.h)
	AC_CHECK_HEADERS(psapi.h,,, [
#ifdef HAVE_WINDOWS_H
#  include <windows.h>
#endif
])

	AC_MSG_CHECKING([for EnumProcessModules in -lpsapi])
	SAVE_LIBS="$LIBS"
	LIBS="-lpsapi $LIBS"
	AC_LINK_IFELSE([AC_LANG_PROGRAM([
#ifdef HAVE_WINDOWS_H
#  include <windows.h>
#endif
#ifdef HAVE_PSAPI_H
#  include <psapi.h>
#endif
], [
	EnumProcessModules(NULL, NULL, 0, NULL);
])], [
	AC_MSG_RESULT([found])
], [
	AC_MSG_RESULT([not found])
	LIBS="$SAVE_LIBS"
])
else
	TARGET="tcc4tcl-static.a"
fi
AC_SUBST(TARGET)
AC_SUBST(TCC4TCL_TARGET)

dnl Find out if we have the functions needed to open shared objects
AC_SEARCH_LIBS(dlopen, dl,, [
        AC_SEARCH_LIBS(shl_load, dld dl)
])
AC_CHECK_FUNCS(dlopen shl_load)

dnl Look for appropriate headers
AC_CHECK_HEADERS(unistd.h stdlib.h string.h strings.h dlfcn.h dl.h)

dnl Perform Tcl Extension required stuff
TCLEXT_INIT

dnl This must be done last since it breaks the compilation
if test "${TCC4TCL_TARGET}" = "shared"; then
	DC_SETVERSIONSCRIPT([tcc4tcl.syms], [tcc4tcl.vers])
	DC_FIND_STRIP_AND_REMOVESYMS([tcc4tcl.syms])
fi

dnl Produce output
AC_OUTPUT(Makefile pkgIndex.tcl tcc4tcl.syms)

Modified tcc4tcl.tcl from [3b40b22f76] to [271b665fc7].

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
           $handle command $tclcommand $cname
        }
       return
   }
}
proc tcc4tcl::to_dll {code dll {libs {}}} {
    tcc4tcl $::tcc4tcl::dir dll tcc_1
    tcc_1 add_library tcl8.5 
    tcc_1 add_library_path .
    foreach lib $libs {tcc_1 add_library $lib}
    if {$::tcl_platform(platform) eq "windows"} {
        tcc_1 define DLL_EXPORT {__declspec(dllexport)} 
        set f [open $::tcc4tcl::dir/c/dllcrt1.c]
        tcc_1 compile [read $f]
        close $f
        set f [open $::tcc4tcl::dir/c/dllmain.c]







<
<







51
52
53
54
55
56
57


58
59
60
61
62
63
64
           $handle command $tclcommand $cname
        }
       return
   }
}
proc tcc4tcl::to_dll {code dll {libs {}}} {
    tcc4tcl $::tcc4tcl::dir dll tcc_1


    foreach lib $libs {tcc_1 add_library $lib}
    if {$::tcl_platform(platform) eq "windows"} {
        tcc_1 define DLL_EXPORT {__declspec(dllexport)} 
        set f [open $::tcc4tcl::dir/c/dllcrt1.c]
        tcc_1 compile [read $f]
        close $f
        set f [open $::tcc4tcl::dir/c/dllmain.c]
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  append tcc(code) $code \n
}
proc ::tcc4tcl::cc {code} {
  variable tcc
  if {![info exists tcc(cc)]} {
      set tcc(cc) tcc1
      tcc4tcl $::tcc4tcl::dir $tcc(cc)
      $tcc(cc) add_library tcl8.5
      $tcc(cc) add_include_path [file join $::tcc4tcl::dir include]
  }
  Log code:$code
  $tcc(cc) compile $code
}
#----------------------------------------------------------- New DLL API
proc ::tcc4tcl::dll {{name ""}} {
    variable count







<
<







90
91
92
93
94
95
96


97
98
99
100
101
102
103
  append tcc(code) $code \n
}
proc ::tcc4tcl::cc {code} {
  variable tcc
  if {![info exists tcc(cc)]} {
      set tcc(cc) tcc1
      tcc4tcl $::tcc4tcl::dir $tcc(cc)


  }
  Log code:$code
  $tcc(cc) compile $code
}
#----------------------------------------------------------- New DLL API
proc ::tcc4tcl::dll {{name ""}} {
    variable count