summaryrefslogtreecommitdiff
path: root/dln.c
diff options
context:
space:
mode:
Diffstat (limited to 'dln.c')
-rw-r--r--dln.c50
1 files changed, 39 insertions, 11 deletions
diff --git a/dln.c b/dln.c
index db6ea5aa0f..d3b03e3e87 100644
--- a/dln.c
+++ b/dln.c
@@ -25,6 +25,7 @@ static void dln_loaderror(const char *format, ...);
#endif
#include "dln.h"
#include "internal.h"
+#include "internal/box.h"
#include "internal/compilers.h"
#ifdef HAVE_STDLIB_H
@@ -76,6 +77,10 @@ void *xrealloc();
# include <unistd.h>
#endif
+#ifndef UNREACHABLE_RETURN
+# define UNREACHABLE_RETURN(x) return (x)
+#endif
+
#ifndef dln_loaderror
static void
dln_loaderror(const char *format, ...)
@@ -284,7 +289,7 @@ dln_incompatible_func(void *handle, const char *funcname, void *const fp, const
void *ex = dlsym(handle, funcname);
if (!ex) return false;
if (ex == fp) return false;
-# if defined(HAVE_DLADDR)
+# if defined(HAVE_DLADDR) && !defined(__CYGWIN__)
Dl_info dli;
if (dladdr(ex, &dli)) {
*libname = dli.dli_fname;
@@ -347,6 +352,7 @@ dln_open(const char *file)
void *handle;
#if defined(_WIN32)
+# define DLN_DEFINED
char message[1024];
/* Convert the file path to wide char */
@@ -373,6 +379,7 @@ dln_open(const char *file)
# endif
#elif defined(USE_DLN_DLOPEN)
+# define DLN_DEFINED
# ifndef RTLD_LAZY
# define RTLD_LAZY 1
@@ -383,9 +390,13 @@ dln_open(const char *file)
# ifndef RTLD_GLOBAL
# define RTLD_GLOBAL 0
# endif
+# ifndef RTLD_LOCAL
+# define RTLD_LOCAL 0 /* TODO: 0??? some systems (including libc) use 0x00100 for RTLD_GLOBAL, 0x00000 for RTLD_LOCAL */
+# endif
/* Load file */
- handle = dlopen(file, RTLD_LAZY|RTLD_GLOBAL);
+ int mode = rb_box_available() ? RTLD_LAZY|RTLD_LOCAL : RTLD_LAZY|RTLD_GLOBAL;
+ handle = dlopen(file, mode);
if (handle == NULL) {
error = dln_strerror();
goto failed;
@@ -437,7 +448,7 @@ dln_sym(void *handle, const char *symbol)
#endif
}
-static void *
+static uintptr_t
dln_sym_func(void *handle, const char *symbol)
{
void *func = dln_sym(handle, symbol);
@@ -453,7 +464,7 @@ dln_sym_func(void *handle, const char *symbol)
#endif
dln_loaderror("%s - %s", error, symbol);
}
- return func;
+ return (uintptr_t)func;
}
#define dln_sym_callable(rettype, argtype, handle, symbol) \
@@ -497,10 +508,10 @@ abi_check_enabled_p(void)
}
#endif
-void *
-dln_load(const char *file)
+static void *
+dln_load_and_init(const char *file, const char *init_fct_name)
{
-#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
+#if defined(DLN_DEFINED)
void *handle = dln_open(file);
#ifdef RUBY_DLN_CHECK_ABI
@@ -512,18 +523,17 @@ dln_load(const char *file)
}
#endif
- char *init_fct_name;
- init_funcname(&init_fct_name, file);
-
/* Call the init code */
dln_sym_callable(void, (void), handle, init_fct_name)();
return handle;
#elif defined(_AIX)
+# define DLN_DEFINED
{
void (*init_fct)(void);
+ /* TODO: check - AIX's load system call will return the first/last symbol/function? */
init_fct = (void(*)(void))load((char*)file, 1, 0);
if (init_fct == NULL) {
aix_loaderror(file);
@@ -536,7 +546,25 @@ dln_load(const char *file)
}
#else
dln_notimplement();
+ UNREACHABLE_RETURN(0);
#endif
+}
- return 0; /* dummy return */
+void *
+dln_load(const char *file)
+{
+ return dln_load_feature(file, file);
+}
+
+void *
+dln_load_feature(const char *file, const char *fname)
+{
+#if defined(DLN_DEFINED)
+ char *init_fct_name;
+ init_funcname(&init_fct_name, fname);
+ return dln_load_and_init(file, init_fct_name);
+#else
+ dln_notimplement();
+ UNREACHABLE_RETURN(0);
+#endif
}