Pragma mangle and D shared objects

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Oct 25 11:38:20 PDT 2014


On Sat, Oct 25, 2014 at 12:15:19PM -0400, Etienne Cimon via Digitalmars-d-learn wrote:
> On 2014-10-25 11:56, Etienne Cimon wrote:
> >That looks like exactly the solution I need, very clever. It'll take
> >some time to wrap my head around it :-P

It's not that complicated, really. It's basically putting together 3
things:

1) .mangleof to extract a mangled symbol from some function declaration
2) regex to replace the function name in the mangled symbol
3) compile-time introspection to build a function declaration out of a
   user-specified signature.


> Just brainstorming here, but I think every dynamic library should hold
> a utility container (hash map?) that searches for and returns the
> mangled names in itself using regex match. This container would always
> be in the same module/function name in every dynamic library.

Actually, the object file (library) itself should already have a list of
exported symbols; you could then use core.demangle to extract the
function signatures from the mangled symbols and construct a hash of all
exported symbols and their types. The only thing is, I don't know of any
cross-platform method of retrieving the list of exported symbols -- the
Posix dlsym() family of functions only allow lookup by explicit symbol
name, no iteration primitives are specified. But the information is
definitely there, since that's how the OS's dynamic linker figures out
how to link dynamic libraries in the first place!


[...]
> Of course, the return type and arguments would have to be decided in
> advance, but otherwise it allows loading and use of (possibly remote
> and unknown) DLLs, in a very simple way.

Well, generally, in order to make use of the functions in the first
place, you'd need some kind of pre-determined return type and parameter
types, otherwise the program couldn't possibly know how to pass
arguments or interpret the return value!

But if you could extract the list of exported symbols from a library,
then you could demangle them to determine their signatures, and thereby
find all symbols matching some given signature.


T

-- 
Curiosity kills the cat. Moral: don't be the cat.


More information about the Digitalmars-d-learn mailing list