Technical article 100872

Using dlopen() on my own DLL fails with "relocation error: symbol not found".

OpenServer 5.0.0OpenServer 5.0.2OpenServer 5.0.4

Written · Last revised

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_int

The 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.c

When 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

From the Xinuos technical article database, record 100872. Reproduced with permission. The original is at www.sco.com/ta/100872, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.