Search code examples
unixdlldlopen

dlopen()/dlsym() on the main executable: how portable is it?


I'm building a compiler and a virtual machine for executing my byte code. The language allows to bind external C functions, which may be defined in some external shared object, as well as the main compiler/VM binary (some essential language built-ins).

I know I can dynamically bind symbols within the main executable with dlopen(NULL, ...), however NOT after I run strip on the binary. I have the following questions then:

  1. Is there a way to do this on a strip'ed binary?
  2. How portable is this feature across UNIX systems in general?
  3. Is it possible to do the same trick on Windows somehow?
  4. Any alternatives ways of binding dynamically within the main executable?

Solution

    1. Use strip -d instead to only strip debug symbols.

    2. The dlopen(3) man page says:

      CONFORMING TO
             POSIX.1-2001 describes dlclose(), dlerror(), dlopen(), and dlsym().
      

      So, very portable across *nix.

    3. Windows uses LoadLibrary() and GetProcAddress() instead.

    4. No.