Problem
I am developing a dynamic link library (DLL) that contains unresolved references to global variables and functions in my main object module.
I use dlopen(S) to load this DLL, but it fails with a return value of NULL. dlerror(S) returns this diagnostic error:
dynamic linker: relocation error: symbol not found: main_intThe symbol 'main_int' refers to a global variable declared in my main object module.
Here is an example source code for the main object main.c and my DLL function mylib.c:
/********** main.c ****************************************************/#include <stdio.h>
#include <dlfcn.h>typedef void (*PFUNC)();#define SYM_NAME "lib_func"
#define DLL_NAME "mylib.so"int main_int = -1;void main_func()
{
fprintf(stdout, "Entered main_func()...\n"); main_int = 1;
}void main(int argc, char *argv[]) { void *lib_handle; PFUNC fptr;
printf("%s: Opening DLL (%s)...\n", argv[0], DLL_NAME);if ((lib_handle = dlopen(DLL_NAME, RTLD_LAZY)) == NULL) { fprintf(stderr, "Error in opening DLL %s\n", DLL_NAME); fprintf(stderr, "%s\n", dlerror()); exit(-1); }
printf("%s: Looking up func (%s)...\n", argv[0], SYM_NAME);
if ((fptr = (PFUNC)dlsym(lib_handle, SYM_NAME)) == NULL) { fprintf(stderr, "Error in finding symbol %s\n", SYM_NAME); fprintf(stderr, "%s\n", dlerror()); exit(-1); }
printf("%s: Calling func (%s), main_int=%d\n", argv[0], SYM_NAME, main_int);
(*fptr)();printf("%s: Returned from func (%s), main_int=%d\n", argv[0], SYM_NAME, main_int);
dlclose(lib_handle);
}/********** mylib.c ***************************************************/#include <stdio.h>extern int main_int;void lib_func(void)
{fprintf(stdout, "Entered lib_func()...\n");main_int = 0; main_func();
}/********** End of mylib.c ********************************************/I have compiled the source files as follows:
$ cc -KPIC -belf -c mylib.c
$ cc -G -o mylib.so mylib.o
$ cc -belf main.cWhen I run the generated binary, it reports this error:
$ LD_LIBRARY_PATH=.
$ export LD_LIBRARY_PATH
$ a.out
a.out: Opening DLL (mylib.so)...
Error in opening DLL mylib.so
dynamic linker: relocation error: symbol not found: main_int Cause
By default, when building an executable, only the names referenced by shared objects are exported. Hence, the symbols main_int and main_func are not visible from within the mylib.so shared object.
This is fully documented in the manual page for ld(CP).
Solution
It is necessary to explicitly export the definitions for the global variable main_int and function main_func. This can be done using the '-Bexport' flag for ld(CP). '-Bexport' with no additional arguments will export all global definitions, whereas '-Bexport' with a set of symbol names will hide all global definitions except those listed.
For example:(Compile the first two passes as above:)
$ cc -KPIC -belf -c mylib.c
$ cc -G -o mylib.so mylib.o$ cc -Wl,-Bexport -belf main.c
$ a.out
a.out: Opening DLL (mylib.so)...
a.out: Looking up func (lib_func)...
a.out: Calling func (lib_func), main_int=-1
Entered lib_func()...
Entered main_func()...
a.out: Returned from func (lib_func), main_int=1