Dynamically loading and unloading (C) object files

Edsko de Vries edskodevries at gmail.com
Thu Oct 31 18:06:10 UTC 2013

Hi guys,

We started experimenting with dynamically loading and unloading C
object files (using the GHC API). I have a simple example with an
object file exporting a single symbol "f"; I have two versions of the
object file; in the first one, f is defined

int f() {
  return 1234;

The second one uses the value "4321" instead. I can dynamically load
and unload these object files (using "linkObj" and "unlinkObj" from
"ObjLink"). I can load the first, dynamically compile some Haskell
code that imports this symbol, then upload this object file and load
the second, and as long as I make sure that the Haskell code gets
recompiled (perhaps a relink step suffices, haven't experimented an
awful lot yet) it can then interact with the second object file.

All this is good news -- in fact, it is more than we had hoped for,
because ghci does not allow dynamically unloading or loading any
object files at all. Object files specified on the command line are
handled in initDynLinker and the ghci top-level function doesn't even
get to see them, and we cannot load object files from inside a ghci

So that leads me to wonder: are there limitations that we should be
aware of? Have I simply been lucky so far?

Any points or suggestions for things to try would be appreciated,


More information about the ghc-devs mailing list