Dynamic linking again

Duncan Coutts duncan@coutts.uklinux.net
Sun, 1 Sep 2002 18:46:45 +0100


So here's another idea. ghc's linker provides (via the ffi) a command
addDLL to load dynamic libraries. It uses dlopen on unix and LoadLibrary
on win32. 

Now, with dlopen if you pass it NULL rather than the filename of a
library it will give you back a handle to itself - the program that
called it. If you've linked your program with the right incantations
this gives you access to all the symbols in the main program.

So if this method works it would be the easiest way to get plugins
working. No need for me to modify ghc (I think)! There do seem to be some problems
with using -rdynamic with ghc however:
http://www.haskell.org/pipermail/gtkhs/2002-March/000253.html

As it is noted, it produces large executibles since all symbols get
exported. I've not yet been able to find a way to limit the symbols that
are exported.

I believe that a similar thing is avaliable on windows since glib's
GModule library claims to provide a portable interface for plugins on
Linux/Sun HP-UX and Windows.
http://developer.gnome.org/doc/API/glib/glib-dynamic-loading-of-modules.html

The rightlinker incantations in this case are -rdynamic or perhaps
-export-dynamic. So says the man page for ld, though there are some
better explanations elsewhere:

http://www.tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN93

Specifically this bit:

In some cases, the call to gcc to create the object file
will also need to include the option ``-Wl,-export-dynamic''. Normally,
the dynamic symbol table contains only symbols which are used by a
dynamic object. This option (when creating an ELF file) adds all symbols
to the dynamic symbol table (see ld(1) for more information). You need
to use this option when there are 'reverse dependencies', i.e., a DL
library has unresolved symbols that by convention must be defined in the
programs that intend to load these libraries. For ``reverse
dependencies'' to work, the master program must make its symbols
dynamically available. Note that you could say ``-rdynamic'' instead of
``-Wl,export-dynamic'' if you only work with Linux systems, but
according to the ELF documentation the ``-rdynamic'' flag doesn't always
work for gcc on non-Linux systems.

So can anyone poke holes in my grand plan or should I give it a go?

Duncan