[Dlang-internal] How do I find and call a function in a module I only know during runtime?

Andrei Alexandrescu via Dlang-internal dlang-internal at puremagic.com
Mon Dec 26 14:32:55 PST 2016


On 12/26/2016 04:39 PM, Vladimir Panteleev wrote:
> On Monday, 26 December 2016 at 20:19:52 UTC, Andrei Alexandrescu wrote:
>> Yah, I'm trying this:
>
> Your program works for me verbatim on GNU/Linux on x86_64.

Thanks for the info. Interesting. I'm on Linux Mint building with dmd 
flag free. I just updated all of dmd, druntime, and phobos to the latest 
and greatest.

> A more portable approach would be to instrument the modules at
> compile-time (e.g. by iterating the modules' members and constructing a
> static array), and using it at run-time.

You still need to fetch the static array address. Essentially what I'm 
looking for is, starting from ModuleInfo, get to the address of 
something tangible that is planted intentionally (so I know its name). 
What is a good way to achieve that?

>> void main()
>> {
>>     void *hndl = dlopen(null, 1);
>>     hndl !is null || assert(0);
>>     auto n = "runAllTests".ptr;//m.name ~ "" '\0';
>
> Perhaps try a leading underscore for the mangled function name. Not sure
> how things are different on OS X.

Yah, it's all Mint and according to objdump the name is runAllTests.

>>     auto p = cast(void function()) dlsym(hndl, n);
>
> This will use the wrong calling convention. You need to cast to an
> extern(C) function pointer.

Thanks for saving me some head scratching later :o).


Andrei



More information about the Dlang-internal mailing list