Skip to content

add option to dlopen with RTLD_GLOBAL (fixes issue #2312) #2317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ const isimmutable = x->(isa(x,Tuple) || isa(x,Symbol) ||
dlsym(hnd, s::String) = ccall(:jl_dlsym, Ptr{Void}, (Ptr{Void}, Ptr{Uint8}), hnd, s)
dlsym(hnd, s::Symbol) = ccall(:jl_dlsym, Ptr{Void}, (Ptr{Void}, Ptr{Uint8}), hnd, s)
dlsym_e(hnd, s::Union(Symbol,String)) = ccall(:jl_dlsym_e, Ptr{Void}, (Ptr{Void}, Ptr{Uint8}), hnd, s)
dlopen(s::String) = ccall(:jl_load_dynamic_library, Ptr{Void}, (Ptr{Uint8},), s)
dlopen(s::String) = ccall(:jl_load_dynamic_library, Ptr{Void}, (Ptr{Uint8},Int32), s, 0)
dlopen_global(s::String) = ccall(:jl_load_dynamic_library, Ptr{Void}, (Ptr{Uint8},Int32), s, 1)
dlclose(p::Ptr) = ccall(:uv_dlclose,Void,(Ptr{Void},),p)

cfunction(f::Function, r, a) =
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,7 @@ export
# C interface
c_free,
dlopen,
dlopen_global,
dlclose,
dlsym,
dlsym_e,
Expand Down
9 changes: 8 additions & 1 deletion doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3007,9 +3007,16 @@ C Interface

Load a shared library, returning an opaque handle.

There is also a variant :func:`dlopen_global` of this function,
which has the same argument and return value but attempts (on Unix)
to open the library in a way that makes the library's symbols available
to other shared libraries. (This corresponds to the ``RTLD_GLOBAL`` flag
in the POSIX :func:`dlopen` function.)

.. function:: dlsym(handle, sym)

Look up a symbol from a shared library handle, return callable function pointer on success.
Look up a symbol from a shared library handle, return callable
function pointer (via :func:`ccall`) on success.

.. function:: dlsym_e(handle, sym)

Expand Down
2 changes: 1 addition & 1 deletion src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static void *add_library_sym(char *name, char *lib)
else {
hnd = libMap[lib];
if (hnd == NULL) {
hnd = jl_load_dynamic_library(lib);
hnd = jl_load_dynamic_library(lib, 0);
if (hnd != NULL)
libMap[lib] = hnd;
else
Expand Down
21 changes: 14 additions & 7 deletions src/dlload.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ extern char *julia_home;
char *jl_lookup_soname(char *pfx, size_t n);
#endif

int jl_uv_dlopen(const char* filename, uv_lib_t* lib)
int jl_uv_dlopen(const char* filename, uv_lib_t* lib, int global)
{
#ifdef RTLD_DEEPBIND
#if defined(RTLD_GLOBAL) && defined(RTLD_LOCAL)
dlerror(); /* Reset error status. */
lib->handle = dlopen(filename, RTLD_LAZY|RTLD_DEEPBIND);
lib->handle = dlopen(filename, RTLD_LAZY
# ifdef RTLD_DEEPBIND
| RTLD_DEEPBIND
# endif
| (global ? RTLD_GLOBAL : RTLD_LOCAL));
if (lib->handle) {
lib->errmsg = NULL;
return 0;
Expand All @@ -51,7 +55,10 @@ int jl_uv_dlopen(const char* filename, uv_lib_t* lib)
#endif
}

uv_lib_t *jl_load_dynamic_library(char *modname)
/* load the dynamic library modname. If global is true, try to
load into the global namespace, so that the libraries symbols
are available for resolution by subsequent library loads */
uv_lib_t *jl_load_dynamic_library(char *modname, int global)
{
int error;
char *ext;
Expand All @@ -76,7 +83,7 @@ uv_lib_t *jl_load_dynamic_library(char *modname)
#else
else if (modname[0] == '/') {
#endif
error = jl_uv_dlopen(modname,handle);
error = jl_uv_dlopen(modname,handle,global);
if (!error) goto done;
}

Expand All @@ -86,12 +93,12 @@ uv_lib_t *jl_load_dynamic_library(char *modname)
handle->handle = NULL;
/* try loading from standard library path */
snprintf(path, PATHBUF, "%s%s", modname, ext);
error = jl_uv_dlopen(path, handle);
error = jl_uv_dlopen(path, handle, global);
if (!error) goto done;
}
#if defined(__linux__)
char *soname = jl_lookup_soname(modname, strlen(modname));
error = (soname==NULL) || jl_uv_dlopen(soname, handle);
error = (soname==NULL) || jl_uv_dlopen(soname, handle, global);
if (!error) goto done;
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ void julia_init(char *imageFile)
{
jl_page_size = getPageSize();
jl_find_stack_bottom();
jl_dl_handle = jl_load_dynamic_library(NULL);
jl_dl_handle = jl_load_dynamic_library(NULL, 0);
#ifdef __WIN32__
uv_dlopen("ntdll.dll",jl_ntdll_handle); //bypass julia's pathchecking for system dlls
uv_dlopen("Kernel32.dll",jl_kernel32_handle);
Expand Down
2 changes: 1 addition & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ DLLEXPORT void jl_module_export(jl_module_t *from, jl_sym_t *s);
void jl_add_standard_imports(jl_module_t *m);

// external libraries
DLLEXPORT uv_lib_t *jl_load_dynamic_library(char *fname);
DLLEXPORT uv_lib_t *jl_load_dynamic_library(char *fname, int global);
DLLEXPORT void *jl_dlsym_e(uv_lib_t *handle, char *symbol);
DLLEXPORT void *jl_dlsym(uv_lib_t *handle, char *symbol);
DLLEXPORT uv_lib_t *jl_wrap_raw_dl_handle(void *handle);
Expand Down