X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Futils%2Fmono-dl.c;h=aa57e04a50ec92185fc5d54a045e518f4be98458;hb=00af1bc3675ef335672a3413a2a8fa3c3ca45bb0;hp=4d4b95048452f941aeb85ad585e3bc56d782da0e;hpb=6df4b5ac5e19997ddf1d3183ec777977186c5560;p=mono.git diff --git a/mono/utils/mono-dl.c b/mono/utils/mono-dl.c index 4d4b9504845..aa57e04a50e 100644 --- a/mono/utils/mono-dl.c +++ b/mono/utils/mono-dl.c @@ -10,6 +10,7 @@ #include "config.h" #include "mono/utils/mono-dl.h" #include "mono/utils/mono-embed.h" +#include "mono/utils/mono-path.h" #include #include @@ -17,194 +18,14 @@ #include #include -#ifdef TARGET_WIN32 -#define SOPREFIX "" -static const char suffixes [][5] = { - ".dll" -}; -#elif defined(__APPLE__) -#define SOPREFIX "lib" -static const char suffixes [][8] = { - ".dylib", - ".so", - ".bundle" -}; -#else -#define SOPREFIX "lib" -static const char suffixes [][4] = { - ".so" -}; -#endif - -#ifdef TARGET_WIN32 - -#include -#include - -#define SO_HANDLE_TYPE HMODULE -#define LL_SO_OPEN(file,flags) w32_load_module ((file), (flags)) -#define LL_SO_CLOSE(module) do { if (!(module)->main_module) FreeLibrary ((module)->handle); } while (0) -#define LL_SO_SYMBOL(module, name) w32_find_symbol ((module), (name)) -#define LL_SO_TRFLAGS(flags) 0 -#define LL_SO_ERROR() w32_dlerror () - -#elif defined (HAVE_DL_LOADER) - -#include - -#ifndef RTLD_LAZY -#define RTLD_LAZY 1 -#endif /* RTLD_LAZY */ - -#define SO_HANDLE_TYPE void* -#define LL_SO_OPEN(file,flags) dlopen ((file), (flags)) -#define LL_SO_CLOSE(module) dlclose ((module)->handle) -#define LL_SO_SYMBOL(module, name) dlsym ((module)->handle, (name)) -#define LL_SO_TRFLAGS(flags) convert_flags ((flags)) -#define LL_SO_ERROR() g_strdup (dlerror ()) - -static int -convert_flags (int flags) -{ - int lflags = flags & MONO_DL_LOCAL? 0: RTLD_GLOBAL; - - if (flags & MONO_DL_LAZY) - lflags |= RTLD_LAZY; - else - lflags |= RTLD_NOW; - return lflags; -} - -#else -/* no dynamic loader supported */ -#define SO_HANDLE_TYPE void* -#define LL_SO_OPEN(file,flags) NULL -#define LL_SO_CLOSE(module) -#define LL_SO_SYMBOL(module, name) NULL -#define LL_SO_TRFLAGS(flags) (flags) -#define LL_SO_ERROR() g_strdup ("No support for dynamic loader") - -#endif - -static GSList *fallback_handlers; - struct MonoDlFallbackHandler { MonoDlFallbackLoad load_func; MonoDlFallbackSymbol symbol_func; MonoDlFallbackClose close_func; void *user_data; }; - -struct _MonoDl { - SO_HANDLE_TYPE handle; - int main_module; - - /* If not NULL, use the methods in MonoDlFallbackHandler instead of the LL_* methods */ - MonoDlFallbackHandler *dl_fallback; -}; - -#ifdef TARGET_WIN32 - -static char* -w32_dlerror (void) -{ - char* ret = NULL; - wchar_t* buf = NULL; - DWORD code = GetLastError (); - - if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, - code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0, NULL)) - { - ret = g_utf16_to_utf8 (buf, wcslen(buf), NULL, NULL, NULL); - LocalFree (buf); - } else { - g_assert_not_reached (); - } - return ret; -} - -static gpointer -w32_find_symbol (MonoDl *module, const gchar *symbol_name) -{ - HMODULE *modules; - DWORD buffer_size = sizeof (HMODULE) * 1024; - DWORD needed, i; - gpointer proc = NULL; - - /* get the symbol directly from the specified module */ - if (!module->main_module) - return GetProcAddress (module->handle, symbol_name); - - /* get the symbol from the main module */ - proc = GetProcAddress (module->handle, symbol_name); - if (proc != NULL) - return proc; - - /* get the symbol from the loaded DLLs */ - modules = (HMODULE *) g_malloc (buffer_size); - if (modules == NULL) - return NULL; - - if (!EnumProcessModules (GetCurrentProcess (), modules, - buffer_size, &needed)) { - g_free (modules); - return NULL; - } - - /* check whether the supplied buffer was too small, realloc, retry */ - if (needed > buffer_size) { - g_free (modules); - - buffer_size = needed; - modules = (HMODULE *) g_malloc (buffer_size); - - if (modules == NULL) - return NULL; - - if (!EnumProcessModules (GetCurrentProcess (), modules, - buffer_size, &needed)) { - g_free (modules); - return NULL; - } - } - - for (i = 0; i < needed / sizeof (HANDLE); i++) { - proc = GetProcAddress (modules [i], symbol_name); - if (proc != NULL) { - g_free (modules); - return proc; - } - } - - g_free (modules); - return NULL; -} - - -static gpointer -w32_load_module (const char* file, int flags) -{ - gpointer hModule = NULL; - if (file) { - gunichar2* file_utf16 = g_utf8_to_utf16 (file, strlen (file), NULL, NULL, NULL); - guint last_sem = SetErrorMode (SEM_FAILCRITICALERRORS); - guint32 last_error = 0; - - hModule = LoadLibrary (file_utf16); - if (!hModule) - last_error = GetLastError (); - - SetErrorMode (last_sem); - g_free (file_utf16); - if (!hModule) - SetLastError (last_error); - } else { - hModule = GetModuleHandle (NULL); - } - return hModule; -} -#endif +static GSList *fallback_handlers; /* * read a value string from line with any of the following formats: @@ -234,7 +55,7 @@ read_string (char *p, FILE *file) if (!endp) return NULL; *endp = 0; - return g_memdup (startp, (endp - startp) + 1); + return (char *) g_memdup (startp, (endp - startp) + 1); } if (*p == 0) return NULL; @@ -242,7 +63,7 @@ read_string (char *p, FILE *file) while (*p && !isspace (*p)) ++p; *p = 0; - return g_memdup (startp, (p - startp) + 1); + return (char *) g_memdup (startp, (p - startp) + 1); } /* @@ -313,27 +134,30 @@ mono_dl_open (const char *name, int flags, char **error_msg) MonoDl *module; void *lib; MonoDlFallbackHandler *dl_fallback = NULL; - int lflags = LL_SO_TRFLAGS (flags); + int lflags = mono_dl_convert_flags (flags); if (error_msg) *error_msg = NULL; - module = malloc (sizeof (MonoDl)); + module = (MonoDl *) malloc (sizeof (MonoDl)); if (!module) { if (error_msg) *error_msg = g_strdup ("Out of memory"); return NULL; } module->main_module = name == NULL? TRUE: FALSE; - lib = LL_SO_OPEN (name, lflags); + + lib = mono_dl_open_file (name, lflags); + if (!lib) { GSList *node; for (node = fallback_handlers; node != NULL; node = node->next){ MonoDlFallbackHandler *handler = (MonoDlFallbackHandler *) node->data; - *error_msg = NULL; + if (error_msg) + *error_msg = NULL; lib = handler->load_func (name, lflags, error_msg, handler->user_data); - if (*error_msg != NULL) + if (error_msg && *error_msg != NULL) g_free (*error_msg); if (lib != NULL){ @@ -361,12 +185,12 @@ mono_dl_open (const char *name, int flags, char **error_msg) llname = get_dl_name_from_libtool (lname); g_free (lname); if (llname) { - lib = LL_SO_OPEN (llname, lflags); + lib = mono_dl_open_file (llname, lflags); g_free (llname); } if (!lib) { if (error_msg) { - *error_msg = LL_SO_ERROR (); + *error_msg = mono_dl_current_error_string (); } free (module); return NULL; @@ -402,11 +226,11 @@ mono_dl_symbol (MonoDl *module, const char *name, void **symbol) char *usname = malloc (strlen (name) + 2); *usname = '_'; strcpy (usname + 1, name); - sym = LL_SO_SYMBOL (module, usname); + sym = mono_dl_lookup_symbol (module, usname); free (usname); } #else - sym = LL_SO_SYMBOL (module, name); + sym = mono_dl_lookup_symbol (module, name); #endif } @@ -417,7 +241,7 @@ mono_dl_symbol (MonoDl *module, const char *name, void **symbol) } if (symbol) *symbol = NULL; - return (module->dl_fallback != NULL) ? err : LL_SO_ERROR (); + return (module->dl_fallback != NULL) ? err : mono_dl_current_error_string (); } /** @@ -437,7 +261,7 @@ mono_dl_close (MonoDl *module) if (dl_fallback->close_func != NULL) dl_fallback->close_func (module->handle, dl_fallback->user_data); } else - LL_SO_CLOSE (module); + mono_dl_close_handle (module); free (module); } @@ -487,16 +311,16 @@ mono_dl_build_path (const char *directory, const char *name, void **iter) suffixlen = 0; } else { idx--; - if (idx >= G_N_ELEMENTS (suffixes)) + if (mono_dl_get_so_suffixes () [idx][0] == '\0') return NULL; first_call = FALSE; - suffix = suffixes [idx]; + suffix = mono_dl_get_so_suffixes () [idx]; suffixlen = strlen (suffix); } - prlen = strlen (SOPREFIX); - if (prlen && strncmp (name, SOPREFIX, prlen) != 0) - prefix = SOPREFIX; + prlen = strlen (mono_dl_get_so_prefix ()); + if (prlen && strncmp (name, mono_dl_get_so_prefix (), prlen) != 0) + prefix = mono_dl_get_so_prefix (); else prefix = ""; @@ -545,3 +369,63 @@ mono_dl_fallback_unregister (MonoDlFallbackHandler *handler) g_slist_remove (fallback_handlers, handler); g_free (handler); } + +static MonoDl* +try_load (const char *lib_name, char *dir, int flags, char **err) +{ + gpointer iter; + MonoDl *runtime_lib; + char *path; + iter = NULL; + *err = NULL; + while ((path = mono_dl_build_path (dir, lib_name, &iter))) { + g_free (*err); + runtime_lib = mono_dl_open (path, flags, err); + g_free (path); + if (runtime_lib) + return runtime_lib; + } + return NULL; +} + +MonoDl* +mono_dl_open_runtime_lib (const char* lib_name, int flags, char **error_msg) +{ + MonoDl *runtime_lib = NULL; + char buf [4096]; + int binl; + *error_msg = NULL; + + binl = mono_dl_get_executable_path (buf, sizeof (buf)); + + if (binl != -1) { + char *base; + char *resolvedname, *name; + buf [binl] = 0; + resolvedname = mono_path_resolve_symlinks (buf); + base = g_path_get_dirname (resolvedname); + name = g_strdup_printf ("%s/.libs", base); + runtime_lib = try_load (lib_name, name, flags, error_msg); + g_free (name); + if (!runtime_lib) { + char *newbase = g_path_get_dirname (base); + name = g_strdup_printf ("%s/lib", newbase); + runtime_lib = try_load (lib_name, name, flags, error_msg); + g_free (name); + } +#ifdef __MACH__ + if (!runtime_lib) { + char *newbase = g_path_get_dirname (base); + name = g_strdup_printf ("%s/Libraries", newbase); + runtime_lib = try_load (lib_name, name, flags, error_msg); + g_free (name); + } +#endif + g_free (base); + g_free (resolvedname); + } + if (!runtime_lib) + runtime_lib = try_load (lib_name, NULL, flags, error_msg); + + return runtime_lib; +}