diff options
author | Tristan Gingold <tgingold@free.fr> | 2019-04-24 08:43:38 +0200 |
---|---|---|
committer | Tristan Gingold <tgingold@free.fr> | 2019-04-24 08:43:38 +0200 |
commit | 04abcfd4f3a0fbdc29bfcbb88c4cec7d417fe0c7 (patch) | |
tree | 6800cdb4583800e309df24f07a59207dc009ea5f /src/grt | |
parent | c73726c65ed88037d26731352cd779bc66b09c68 (diff) | |
download | ghdl-04abcfd4f3a0fbdc29bfcbb88c4cec7d417fe0c7.tar.gz ghdl-04abcfd4f3a0fbdc29bfcbb88c4cec7d417fe0c7.tar.bz2 ghdl-04abcfd4f3a0fbdc29bfcbb88c4cec7d417fe0c7.zip |
grt: extract grt-dynload from grt-cvpi
Diffstat (limited to 'src/grt')
-rw-r--r-- | src/grt/Makefile.inc | 8 | ||||
-rw-r--r-- | src/grt/grt-cvpi.c | 66 | ||||
-rw-r--r-- | src/grt/grt-dynload.c | 52 | ||||
-rw-r--r-- | src/grt/grt-dynload.h | 11 |
4 files changed, 78 insertions, 59 deletions
diff --git a/src/grt/Makefile.inc b/src/grt/Makefile.inc index 70e7c8ca6..89f0675ad 100644 --- a/src/grt/Makefile.inc +++ b/src/grt/Makefile.inc @@ -71,7 +71,7 @@ GRT_FST_OBJS := fstapi.o lz4.o fastlz.o # Additionnal object files (C or asm files). GRT_BASE_C_OBJS:=$(GRT_TARGET_OBJS) grt-cbinding.o -GRT_ADD_OBJS:=$(GRT_BASE_C_OBJS) grt-cvpi.o $(GRT_FST_OBJS) +GRT_ADD_OBJS:=$(GRT_BASE_C_OBJS) grt-cvpi.o grt-dynload.o $(GRT_FST_OBJS) GRT_ADD_SHOBJS:=$(patsubst %, pic/%, $(GRT_BASE_C_OBJS)) # Source files create by grt. @@ -164,6 +164,12 @@ grt-cvpi.o: $(GRTSRCDIR)/grt-cvpi.c pic/grt-cvpi.o: $(GRTSRCDIR)/grt-cvpi.c $(GRT_C_COMPILE_PIC) +grt-dynload.o: $(GRTSRCDIR)/grt-dynload.c + $(GRT_C_COMPILE) + +pic/grt-dynload.o: $(GRTSRCDIR)/grt-dynload.c + $(GRT_C_COMPILE_PIC) + grt-cthreads.o: $(GRTSRCDIR)/grt-cthreads.c $(GRT_C_COMPILE) diff --git a/src/grt/grt-cvpi.c b/src/grt/grt-cvpi.c index 2bf23364b..17f0e75af 100644 --- a/src/grt/grt-cvpi.c +++ b/src/grt/grt-cvpi.c @@ -32,6 +32,7 @@ #include "vpi_user.h" #include "vpi_thunk.h" +#include "grt-dynload.h" /* Extension of a shared library. */ #if defined (WINNT) @@ -76,59 +77,6 @@ static vpi_thunk __ghdl_vpi_thunk_v1 = //----------------------------------------------------------------------------- // VPI module load & startup -static void * module_open (const char *path); -static void * module_symbol (void *handle, const char *symbol); -static const char *module_error (void); - -#if defined(__WIN32__) -#include <windows.h> -static void * -module_open (const char *path) -{ - return (void *)LoadLibrary (path); -} - -static void * -module_symbol (void *handle, const char *symbol) -{ - return (void *)GetProcAddress ((HMODULE)handle, symbol); -} - -static const char * -module_error (void) -{ - static char msg[256]; - - FormatMessage - (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError (), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) &msg, - sizeof (msg) - 1, - NULL); - return msg; -} -#else -#include <dlfcn.h> -static void * -module_open (const char *path) -{ - return dlopen (path, RTLD_LAZY); -} - -static void * -module_symbol (void *handle, const char *symbol) -{ - return dlsym (handle, symbol); -} - -static const char * -module_error (void) -{ - return dlerror (); -} -#endif #if defined (__APPLE__) /* On Darwin: look in rpath. */ @@ -153,11 +101,11 @@ loadVpiModule (const char* modulename) - exec path\lib => see windows_default_path */ - vpimod = module_open (modulename); + vpimod = grt_dynload_open (modulename); if (vpimod == NULL) { - const char *msg = module_error (); + const char *msg = grt_dynload_error (); fprintf (stderr, "%s\n", msg == NULL ? "unknown dlopen error" : msg); return -1; @@ -167,14 +115,15 @@ loadVpiModule (const char* modulename) No need to load the library several times. */ if (libghdlvpi_mod == NULL) { - libghdlvpi_mod = module_open (libghdlvpi_name); + libghdlvpi_mod = grt_dynload_open (libghdlvpi_name); if (libghdlvpi_mod != NULL) { vpi_thunk **vpi_thunk_ptr; for (i = 0; i < 2; i++) { - vpi_thunk_ptr = module_symbol (libghdlvpi_mod, &"_VPI_THUNK"[i]); + vpi_thunk_ptr = + grt_dynload_symbol (libghdlvpi_mod, &"_VPI_THUNK"[i]); if (vpi_thunk_ptr != NULL) { @@ -190,7 +139,8 @@ loadVpiModule (const char* modulename) for (i = 0; i < 2; i++) // try with and w/o leading underscores { - void *vpitable = module_symbol (vpimod, &"_vlog_startup_routines"[i]); + void *vpitable = + grt_dynload_symbol (vpimod, &"_vlog_startup_routines"[i]); if (vpitable) { diff --git a/src/grt/grt-dynload.c b/src/grt/grt-dynload.c new file mode 100644 index 000000000..af595f454 --- /dev/null +++ b/src/grt/grt-dynload.c @@ -0,0 +1,52 @@ +#if defined(__WIN32__) +#include <windows.h> +void * +grt_dynload_open (const char *path) +{ + return (void *)LoadLibrary (path); +} + +void * +grt_dynload_symbol (void *handle, const char *symbol) +{ + return (void *)GetProcAddress ((HMODULE)handle, symbol); +} + +const char * +grt_dynload_error (void) +{ + static char msg[256]; + + FormatMessage + (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError (), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &msg, + sizeof (msg) - 1, + NULL); + return msg; +} + +#else + +#include <dlfcn.h> + +void * +grt_dynload_open (const char *path) +{ + return dlopen (path, RTLD_LAZY); +} + +void * +grt_dynload_symbol (void *handle, const char *symbol) +{ + return dlsym (handle, symbol); +} + +const char * +grt_dynload_error (void) +{ + return dlerror (); +} +#endif diff --git a/src/grt/grt-dynload.h b/src/grt/grt-dynload.h new file mode 100644 index 000000000..ff2551a2f --- /dev/null +++ b/src/grt/grt-dynload.h @@ -0,0 +1,11 @@ +/* Very simple wrappers for loading dll/so files. */ +#ifndef __GRT_DYNLOAD__ +#define __GRT_DYNLOAD__ + +void * grt_dynload_open (const char *path); +void * grt_dynload_symbol (void *handle, const char *symbol); +const char *grt_dynload_error (void); + +#endif /* __GRT_DYNLOAD__ */ + + |