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:
On some ELF systems (notably Linux), you can dlopen() PIE executables. When using GCC, just compile the executable with -fpie or -fPIE , and link it with -pie , and export the appropriate symbols using --dynamic-list or -rdynamic (explained in more detail in this other SO answer.
dlopen() The function dlopen() loads the dynamic shared object (shared library) file named by the null-terminated string filename and returns an opaque "handle" for the loaded object. This handle is employed with other functions in the dlopen API, such as dlsym(3), dladdr(3), dlinfo(3), and dlclose().
If the same library is loaded again with dlopen(), the same file handle is returned. The dl library maintains reference counts for library handles, so a dynamic library is not deallocated until dlclose() has been called on it as many times as dlopen() has succeeded on it.
The dlopen() function shall make an executable object file specified by file available to the calling program.
Use strip -d
instead to only strip debug symbols.
The dlopen(3)
man page says:
CONFORMING TO POSIX.1-2001 describes dlclose(), dlerror(), dlopen(), and dlsym().
So, very portable across *nix.
Windows uses LoadLibrary()
and GetProcAddress()
instead.
No.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With