From blaze@infosec.ru  Tue Dec 19 06:29:52 2000
Return-Path: <blaze@infosec.ru>
Received: from pr.infosec.ru (pr.infosec.ru [194.135.141.98])
	by hub.freebsd.org (Postfix) with ESMTP id B928637B6A0
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 19 Dec 2000 06:29:31 -0800 (PST)
Received: from xen.infosec.ru.infosec.ru (WS_BLAZE [200.0.0.51]) by pr.infosec.ru with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2650.21)
	id ZBKH0L0A; Tue, 19 Dec 2000 17:31:26 +0300
Message-Id: <868zpcqyaq.fsf@xen.infosec.ru>
Date: 19 Dec 2000 17:29:49 +0300
From: Andrey Sverdlichenko <blaze@infosec.ru>
Sender: blaze@xen.infosec.ru
To: FreeBSD-gnats-submit@freebsd.org
Subject: dlopen() can't link to symbols in main program module

>Number:         23643
>Category:       i386
>Synopsis:       dlopen() can't link to symbols in main program module
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Dec 19 06:30:03 PST 2000
>Closed-Date:    Fri Jun 15 11:14:17 PDT 2001
>Last-Modified:  Fri Jun 15 11:15:11 PDT 2001
>Originator:     Andrey Sverdlichenko
>Release:        FreeBSD 4.1.1-STABLE i386
>Organization:
NIP "Informzaschita"
>Environment:
FreeBSD xen.infosec.ru 4.1.1-STABLE FreeBSD 4.1.1-STABLE #4: Mon Oct 30 15:39:57 MSK 2000     blaze@xen.infosec.ru:/var/tmp/src/sys/compile/XEN  i386

>Description:

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"


>Fix:



>Release-Note:
>Audit-Trail:

From: Carlos A M dos Santos <casantos@cpmet.ufpel.tche.br>
To: Andrey Sverdlichenko <blaze@infosec.ru>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG,
	GNATS Management <gnats-admin@FreeBSD.ORG>, freebsd-bugs@FreeBSD.ORG
Subject: Re: i386/23643: dlopen() can't link to symbols in main program module
Date: Tue, 19 Dec 2000 15:53:33 +0000 (GMT)

 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
 
 

From: avn@any.ru
To: freebsd-gnats-submit@FreeBSD.org, blaze@infosec.ru
Cc:  
Subject: Re: i386/23643: dlopen() can't link to symbols in%
Date: Fri, 15 Jun 2001 01:09:47 +0400 (MSD)

 This PR can be closed.
 (wrong usage of gcc flags by originator).
 
 
State-Changed-From-To: open->closed 
State-Changed-By: roam 
State-Changed-When: Fri Jun 15 11:14:17 PDT 2001 
State-Changed-Why:  
Asked and answered - read the dlopen(3) man page, pay particular attention 
to the NOTES section. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=23643 
>Unformatted:
