Is there a way to set the flags of dlopen when Nim loads dynamic .so libraries on Linux?
I want to add the RTLD_GLOBAL flag to it.
Thanks
@Araq Excellent! I made a modification to lib/system/dyncalls.nim and it works like a charm, thanks.
Just changed the nimLoadLibrary procedure line from:
result = dlopen(path, RTLD_NOW)
I will try to find the PR you mentioned. to:
result = dlopen(path, RTLD_NOW or RTLD_GLOBAL)
and added the RTLD_GLOBAL constant definition:
const RTLD_GLOBAL = cint(0x0100)
Will try to find the PR you mentioned. Strange that no-one ran into this issue before.Here's some context:
2015: https://github.com/nim-lang/Nim/issues/2408 2014: https://github.com/nim-lang/Nim/pull/897
@shashlick Thanks! Oh I see, the one PR added a global_symbols flag to the dynlib.loadLib call. That is one way of making it work, great! But that means changing all the dynamic library procedure definitions from the nice pragma syntax:
proc myExtFunc*() {.importc: "myExtFunc", dynlib: myLibrary, cdecl.}
to a load-to-variable way of:
import dynlib
var myExtlib = dynlib.loadLib("myExtlib.so", global_symbols=true)
var myExtFunc*: proc() {.cdecl.}
myExtFunc = cast[type(myExtFunc)](myExtlib.symAddr("myExtFunc"))
which works but does not look as satisfyingly simple.
The issue from 2015 that would add the global symbol functionality to the dynlib pragma is still unresolved, so I am guessing that no one made a PR for @Araq 's suggestion of loading with RTLD_GLOBAL by default and adding a -d:noRtldGlobal switch. I would basically do almost the same thing that @micklat did for the dynlib.loadLib, but with the -d:noRtldGlobal switch. Is it still an option for me adding a PR like that?
Is anyone working on this?
One workaround is to override dlopen and force RTLD_GLOBAL flag.
{. passL:"-rdynamic -Wl,-wrap,dlopen".}
{.emit: """
#include <dlfcn.h>
#include <stdio.h>
void *__real_dlopen(const char *filename, int flags);
void *__wrap_dlopen(const char *filename, int flags)
{
printf("shadow dlopen with RTLD_GLOBAL: %s\n", filename);
fflush(stdout);
return __real_dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
}
""".}
IUP fontDlg works now.