From owner-freebsd-questions@FreeBSD.ORG Thu Nov 13 04:29:16 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3E2161065670 for ; Thu, 13 Nov 2008 04:29:16 +0000 (UTC) (envelope-from jdc@koitsu.dyndns.org) Received: from QMTA02.westchester.pa.mail.comcast.net (qmta02.westchester.pa.mail.comcast.net [76.96.62.24]) by mx1.freebsd.org (Postfix) with ESMTP id D62E28FC13 for ; Thu, 13 Nov 2008 04:29:15 +0000 (UTC) (envelope-from jdc@koitsu.dyndns.org) Received: from OMTA03.westchester.pa.mail.comcast.net ([76.96.62.27]) by QMTA02.westchester.pa.mail.comcast.net with comcast id eRPw1a0060bG4ec52UUxy0; Thu, 13 Nov 2008 04:28:57 +0000 Received: from koitsu.dyndns.org ([69.181.141.110]) by OMTA03.westchester.pa.mail.comcast.net with comcast id eUVD1a0072P6wsM3PUVD9X; Thu, 13 Nov 2008 04:29:14 +0000 X-Authority-Analysis: v=1.0 c=1 a=_jwf7xEnl2UA:10 a=uO20VAJa9lgA:10 a=FP58Ms26AAAA:8 a=QycZ5dHgAAAA:8 a=fFdmG6hn0bpZdDcaxsAA:9 a=mMYMt8EIpYevY6BBfegA:7 a=tqEM6qBmW8F8Pw_rraTChOUayTwA:4 a=JNvzRHLLhzsA:10 a=yZ3J24pBv6YA:10 a=EoioJ0NPDVgA:10 a=LY0hPdMaydYA:10 Received: by icarus.home.lan (Postfix, from userid 1000) id 25F2F5C19; Wed, 12 Nov 2008 20:29:12 -0800 (PST) Date: Wed, 12 Nov 2008 20:29:12 -0800 From: Jeremy Chadwick To: Markus Hoenicka Message-ID: <20081113042912.GA10018@icarus.home.lan> References: <18715.28127.306511.577084@yeti.mininet> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <18715.28127.306511.577084@yeti.mininet> User-Agent: Mutt/1.5.18 (2008-05-17) Cc: freebsd-questions@freebsd.org Subject: Re: dlsym can't use handle returned by dlopen? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Nov 2008 04:29:16 -0000 On Thu, Nov 13, 2008 at 12:59:27AM +0100, Markus Hoenicka wrote: > FreeBSD yeti.mininet 6.1-RELEASE FreeBSD 6.1-RELEASE #1: Mon Aug 28 22:24:48 CEST 2006 markus@yeti.mininet:/usr/src/sys/i386/compile/YETI i386 > > I'm trying to do the following: libdbi (http://libdbi.sourceforge.net) > is a database abstraction layer which is linked as a shared object > into applications. The libdbi library uses dlopen() to load database > drivers which in turn are linked against database client > libraries. Now I want to access functions defined in the database > client library from within the libdbi library. In brief, I thought > this is going to work like this: > > dlhandle = dlopen("path", RTLD_NOW); > ... > function_pointer = dlsym(dlhandle, "function_name"); > > dlhandle is not NULL and does not crash the app when passed to > dlclose(), so I assume the handle is valid. Accessing the functions > does work on most systems (Linux, OSX, Cygwin, to name a few), but I > get "Undefined symbol" errors on FreeBSD. Interestingly, the following > does work: I've personally used dlopen() and dlsym(), and they do work as documented (my original goal with bsdhwmon was to keep each chip in a separate .so. It worked, but added complexities of the program nature itself kept me from using it at the time). I tested this on both i386 and amd64. I know that the .so's you're loading with dlopen() need to be built a specific way/with certain arguments, otherwise they won't work (I believe what I saw was dlsym() returning NULL). My symbol names were getting stomped on, and there was a compiler flag that addressed that. I can go back and write code that does all of this if you'd like, but my point is that they do work. > function_pointer = dlsym(RTLD_DEFAULT, "function_name"); > > Why is that? Or rather: what am I doing wrong? This code right here is *completely* wrong. RTLD_DEFAULT is a mode bit for dlopen(). I'm willing to bet a strict set of warnings would catch this. Try building your application with: -g3 -ggdb -Werror -Wall -Wunused -Waggregate-return -Wbad-function-cast -Wcast-align -Wdeclaration-after-statement -Wdisabled-optimization -Wfloat-equal -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wold-style-definition -Wpacked -Wpointer-arith -Wredundant-decls -Wsign-compare -Wstrict-prototypes -Wunreachable-code -Wwrite-strings And see what appears. -- | Jeremy Chadwick jdc at parodius.com | | Parodius Networking http://www.parodius.com/ | | UNIX Systems Administrator Mountain View, CA, USA | | Making life hard for others since 1977. PGP: 4BD6C0CB |