www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Using dlopen/dlsym

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Consider this code:

===========
import core.sys.posix.dlfcn;
extern(C) void fun() {}
void main()
{
     fun();
     void *hndl = dlopen(null, RTLD_LAZY);
     if (!hndl) assert(0);
     auto p = dlsym(hndl, "fun".ptr);
     if (!p)
     {
         import core.stdc.stdio;
         printf("%s\n", dlerror());
     }
}
===========

It works (i.e. outputs nothing) on http://dpaste.dzfl.pl and on 
Vladimir's machine.

On my Mint and Ubuntu machines it outputs:

./test: undefined symbol: fun

I'm building with no flags using dmd. What could be the problem here?


Thanks,

Adnrei
Dec 26 2016
next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 27 December 2016 at 00:05:39 UTC, Andrei Alexandrescu 
wrote:
 Consider this code:

 ===========
 import core.sys.posix.dlfcn;
 extern(C) void fun() {}
 void main()
 {
     fun();
     void *hndl = dlopen(null, RTLD_LAZY);
     if (!hndl) assert(0);
     auto p = dlsym(hndl, "fun".ptr);
Does auto p = dlsym(hndl, fun.mangleof.ptr); work any better?
Dec 26 2016
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 12/26/2016 07:35 PM, Nicholas Wilson wrote:
 On Tuesday, 27 December 2016 at 00:05:39 UTC, Andrei Alexandrescu wrote:
 Consider this code:

 ===========
 import core.sys.posix.dlfcn;
 extern(C) void fun() {}
 void main()
 {
     fun();
     void *hndl = dlopen(null, RTLD_LAZY);
     if (!hndl) assert(0);
     auto p = dlsym(hndl, "fun".ptr);
Does auto p = dlsym(hndl, fun.mangleof.ptr); work any better?
No, same result. -- Andrei
Dec 26 2016
prev sibling next sibling parent Benjiro <benjiro benjiro.com> writes:
On Tuesday, 27 December 2016 at 00:05:39 UTC, Andrei Alexandrescu 
wrote:
 Consider this code:

 ===========
 import core.sys.posix.dlfcn;
 extern(C) void fun() {}
 void main()
 {
     fun();
     void *hndl = dlopen(null, RTLD_LAZY);
     if (!hndl) assert(0);
     auto p = dlsym(hndl, "fun".ptr);
     if (!p)
     {
         import core.stdc.stdio;
         printf("%s\n", dlerror());
     }
 }
 ===========

 It works (i.e. outputs nothing) on http://dpaste.dzfl.pl and on 
 Vladimir's machine.

 On my Mint and Ubuntu machines it outputs:

 ./test: undefined symbol: fun

 I'm building with no flags using dmd. What could be the problem 
 here?


 Thanks,

 Adnrei
Odd ... Works perfect on Debian ( DMD64 D Compiler v2.071.2 ).
Dec 26 2016
prev sibling next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 27 December 2016 at 00:05:39 UTC, Andrei Alexandrescu 
wrote:
 I'm building with no flags using dmd.
Do dmd -v for verbose output and see what linker flags it is doing. Perhaps you have a configuration difference that is causing it not to export the symbol (`fun` isn't marked `export`... I don't think that matters on linux but it might on some versions or with some configurations).
Dec 26 2016
parent reply Mike Wey <mike-wey example.com> writes:
On 12/27/2016 06:02 AM, Adam D. Ruppe wrote:
 On Tuesday, 27 December 2016 at 00:05:39 UTC, Andrei Alexandrescu wrote:
 I'm building with no flags using dmd.
Do dmd -v for verbose output and see what linker flags it is doing. Perhaps you have a configuration difference that is causing it not to export the symbol (`fun` isn't marked `export`... I don't think that matters on linux but it might on some versions or with some configurations).
dmd will need to pass "--export-dynamic" to the linker, so that the symbol is actually exported. -- Mike Wey
Dec 27 2016
parent Martin Nowak <code dawg.eu> writes:
On Tuesday, 27 December 2016 at 18:12:42 UTC, Mike Wey wrote:
 dmd will need to pass "--export-dynamic" to the linker, so that 
 the symbol is actually exported.
Thanks, at least one useful answer out of nine!
Dec 31 2016
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2016-12-27 01:05, Andrei Alexandrescu wrote:
 Consider this code:

 ===========
 import core.sys.posix.dlfcn;
 extern(C) void fun() {}
 void main()
 {
     fun();
     void *hndl = dlopen(null, RTLD_LAZY);
     if (!hndl) assert(0);
     auto p = dlsym(hndl, "fun".ptr);
     if (!p)
     {
         import core.stdc.stdio;
         printf("%s\n", dlerror());
     }
 }
 ===========

 It works (i.e. outputs nothing) on http://dpaste.dzfl.pl and on
 Vladimir's machine.

 On my Mint and Ubuntu machines it outputs:

 ./test: undefined symbol: fun

 I'm building with no flags using dmd. What could be the problem here?
I tried on Mint 18 and Ubuntu 16.10 with DMD 2.072.2-b1, it worked for me. Which versions of Mint, Ubuntu, dmd, gcc, ldd etc. are you using? -- /Jacob Carlborg
Dec 27 2016
prev sibling next sibling parent Chris Wright <dhasenan gmail.com> writes:
On Mon, 26 Dec 2016 19:05:39 -0500, Andrei Alexandrescu wrote:
 I'm building with no flags using dmd. What could be the problem here?
What DMD version are you using?
Dec 27 2016
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2016-12-27 01:05, Andrei Alexandrescu wrote:
 Consider this code:

 ===========
 import core.sys.posix.dlfcn;
 extern(C) void fun() {}
 void main()
 {
     fun();
     void *hndl = dlopen(null, RTLD_LAZY);
     if (!hndl) assert(0);
     auto p = dlsym(hndl, "fun".ptr);
     if (!p)
     {
         import core.stdc.stdio;
         printf("%s\n", dlerror());
     }
 }
 ===========

 It works (i.e. outputs nothing) on http://dpaste.dzfl.pl and on
 Vladimir's machine.

 On my Mint and Ubuntu machines it outputs:

 ./test: undefined symbol: fun

 I'm building with no flags using dmd. What could be the problem here?
Although I don't think this is the problem, the correct way to check if dlsym was successful is to call dlerror, not check if the result from dlsym was null. From the man page: "Since the value of the symbol could actually be NULL (so that a NULL return from dlsym() need not indicate an error), the correct way to test for an error is to call dlerror(3) to clear any old error conditions, then call dlsym(), and then call dlerror(3) again, saving its return value into a variable, and check whether this saved value is not NULL" -- /Jacob Carlborg
Dec 27 2016
prev sibling next sibling parent unDEFER <undefer gmail.com> writes:
It works on my Ubuntu 16.04 and dmd v2.071.1
But it wants to call dlopen() as core.sys.posix.dlfcn.dlopen().
Dec 27 2016
prev sibling parent Martin Nowak <code dawg.eu> writes:
On Tuesday, 27 December 2016 at 00:05:39 UTC, Andrei Alexandrescu 
wrote:
 ./test: undefined symbol: fun

 I'm building with no flags using dmd. What could be the problem 
 here?
Importing symbols from your executable requires to tell the linker to create a dynamic symbol table, i.e. using --export-dynamic for ld. We also add that flag by default on many platforms because our exception backtraces still rely on the dynamic symbol tables, instead of exclusively using dedicated DWARF information. Combining both effects might lead to confusing behavior. Also see: [Issue 11870 – replace dynamic symbol table (--export-dynamic) for backtraces](https://issues.dlang.org/show_bug.cgi?id=11870) https://dlang.org/changelog/2.069.0.html#curl-dynamic-loading
Dec 31 2016