Bug 23643

Summary: dlopen() can't link to symbols in main program module
Product: Base System Reporter: Andrey Sverdlichenko <blaze>
Component: i386Assignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.1.1-STABLE   
Hardware: Any   
OS: Any   

Description Andrey Sverdlichenko 2000-12-19 14:30:03 UTC
Call dlopen(..., RTLD_NOW) returns "Undefined symbol" when shared object 
uses a symbol from main program.

How-To-Repeat: 
Makefile
----------- begin Makefile -----------

prog: lib.so main.c
        gcc -g -fpic -DPIC -Wall -o prog main.c

lib.so: lib.c
        gcc -fpic -DPIC -O -pipe -Wall -c lib.c
        cc -shared -o lib.so lib.o

clean:
        rm -f prog *.o *.so
----------- end Makefile ---------------

main.c

----------- begin main.c ---------------
#include <stdio.h>
#include <sys/types.h>
#include <dlfcn.h>

char *str = "ok";

int main() {
        static void * lib_so;
        static void (*lib_so_func)();

        lib_so = dlopen("./lib.so", RTLD_NOW | RTLD_GLOBAL);
        if (!lib_so) {
                fprintf(stderr, "dlopen: %s\n", dlerror());
                return 1;
        }
        lib_so_func = dlsym(lib_so, "lib_so_func");
        if (!lib_so_func) {
                fprintf(stderr, "dlsym: %s\n", dlerror());
                return 1;
        }
        lib_so_func();
        
        return 0;
}

----------- end main.c ------------

lib.c

----------- begin lib.c -----------
#include <stdio.h>

extern char *str;

int lib_so_func() {
        printf("%s\n", str);
        return 1;
}
----------- end lib.c -----------

0:xen:test_so$ make
gcc -fpic -DPIC -O -pipe -Wall -c lib.c
cc -shared -o lib.so lib.o
gcc -g -fpic -DPIC -Wall -o prog main.c

0:xen:test_so$ ./prog 
dlopen: ./lib.so: Undefined symbol "str"
Comment 1 Carlos A M dos Santos 2000-12-19 15:53:33 UTC
On 19 Dec 2000, the fast fingers of Andrey Sverdlichenko wrote:

AS> 0:xen:test_so$ make
AS> gcc -fpic -DPIC -O -pipe -Wall -c lib.c
AS> cc -shared -o lib.so lib.o
AS> gcc -g -fpic -DPIC -Wall -o prog main.c
AS> 
AS> 0:xen:test_so$ ./prog 
AS> dlopen: ./lib.so: Undefined symbol "str"

gcc -g -fpic -DPIC -Wall -export-dynamic -o prog main.c
                         ^^^^^^^^^^^^^^^

man ld


  
--
Carlos A. M. dos Santos

Federal University of Pelotas         Meteorological Research Center
Av. Ildefonso Simoes Lopes 2791       Pelotas, RS, Brasil, CEP 96060-290
WWW: http://www.cpmet.ufpel.tche.br   RENPAC (X.25): 153231641
Phone: +55 53 277-6767                FAX: +55 53 277-6722
Comment 2 avn 2001-06-14 22:09:47 UTC
This PR can be closed.
(wrong usage of gcc flags by originator).
À
Comment 3 Peter Pentchev freebsd_committer freebsd_triage 2001-06-15 19:14:17 UTC
State Changed
From-To: open->closed

Asked and answered - read the dlopen(3) man page, pay particular attention 
to the NOTES section.