From 04abcfd4f3a0fbdc29bfcbb88c4cec7d417fe0c7 Mon Sep 17 00:00:00 2001 From: Tristan Gingold Date: Wed, 24 Apr 2019 08:43:38 +0200 Subject: grt: extract grt-dynload from grt-cvpi --- Makefile.in | 5 ++-- src/grt/Makefile.inc | 8 ++++- src/grt/grt-cvpi.c | 66 +++++------------------------------------ src/grt/grt-dynload.c | 52 ++++++++++++++++++++++++++++++++ src/grt/grt-dynload.h | 11 +++++++ src/vhdl/translate/trans_be.adb | 2 +- 6 files changed, 82 insertions(+), 62 deletions(-) create mode 100644 src/grt/grt-dynload.c create mode 100644 src/grt/grt-dynload.h diff --git a/Makefile.in b/Makefile.in index 9c2624f9e..093ede3ad 100644 --- a/Makefile.in +++ b/Makefile.in @@ -486,9 +486,10 @@ distclean: clean $(RM) -f Makefile config.status ghdl.gpr clean-c: force - $(RM) -f memsegs_c.o chkstk.o jumps.o times.o grt-cbinding.o grt-cvpi.o + $(RM) -f memsegs_c.o chkstk.o jumps.o times.o + $(RM) -f grt-cbinding.o grt-cvpi.o grt-modules.o $(RM) -f fstapi.o fastlz.o lz4.o - $(RM) -f pic/times.o pic/grt-cvpi.o pic/jumps.o + $(RM) -f pic/times.o pic/grt-cvpi.o pic/grt-modules.o pic/jumps.o create-dirs: force for d in $(VHDLLIB_SUBDIRS); do \ 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 -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 -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 +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 + +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__ */ + + diff --git a/src/vhdl/translate/trans_be.adb b/src/vhdl/translate/trans_be.adb index 9fac3a799..699c1e55e 100644 --- a/src/vhdl/translate/trans_be.adb +++ b/src/vhdl/translate/trans_be.adb @@ -37,7 +37,7 @@ package body Trans_Be is when others => Error_Kind ("sem_foreign", Decl); end case; - -- Let is generate error messages. + -- Let it generate error messages. Fi := Translate_Foreign_Id (Decl); end Sem_Foreign; -- cgit v1.2.3