From owner-freebsd-hackers Fri Jan 21 12:24:55 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from wall.polstra.com (rtrwan160.accessone.com [206.213.115.74]) by hub.freebsd.org (Postfix) with ESMTP id AAA4B154C8 for ; Fri, 21 Jan 2000 12:24:52 -0800 (PST) (envelope-from jdp@polstra.com) Received: from vashon.polstra.com (vashon.polstra.com [206.213.73.13]) by wall.polstra.com (8.9.3/8.9.3) with ESMTP id MAA23996; Fri, 21 Jan 2000 12:24:50 -0800 (PST) (envelope-from jdp@polstra.com) From: John Polstra Received: (from jdp@localhost) by vashon.polstra.com (8.9.3/8.9.1) id MAA11973; Fri, 21 Jan 2000 12:24:49 -0800 (PST) (envelope-from jdp@polstra.com) Date: Fri, 21 Jan 2000 12:24:49 -0800 (PST) Message-Id: <200001212024.MAA11973@vashon.polstra.com> To: fanf@demon.net Subject: Re: LD_PRELOAD In-Reply-To: References: Organization: Polstra & Co., Seattle, WA Cc: hackers@freebsd.org Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG In article , Tony Finch wrote: > I'm experimenting with using LD_PRELOAD to implement "shim" wrappers > around functions in libc. It's really easy to do on Solaris but I'm > having some difficulty on FreeBSD. > > The first problem I had was compiling my shim library so that the rtld > would accept it. The right way to do it on FreeBSD is like this: gcc -fpic -c *.c gcc -shared -o libshim.so *.o > This brings me on to the second problem. I want to do some > initialization of my library before the real action starts. On Solaris > the linker calls a function in the object called _init() when it is > loaded; it's easy to make this work. I can see similar functionality > in FreeBSD's rtld but it looks like I have to jump through obscure > hoops to make it work (an ELF DT_INIT section if I understand it > correctly). The names "_init" and "_fini" are "reserved for the implementation" in ANSI/ISO-speak. You shouldn't use them. You can get what you want like this with gcc: void myInitFunction(void) __attribute__ ((constructor)); void myInitFunction(void) { /* Your initialization code here. */ } Or, write it in C++ and use a global constructor. > code like > if (!initialized) > init_me_baby(); > at the start of entry-point functions. This seems grotty and > inefficient to me and I'd like to avoid it if possible. Fine, but then you're venturing into areas not covered by the relevant standards. That will make your code less portable. > I also note a user-interface incompatibility between FreeBSD's > implementation of LD_PRELOAD and Solaris's: on Solaris the filenames > listed in LD_PRELOAD are space-separated, but on FreeBSD they are > colon or semicolon separated. That could be a bug. You're probably the first person on earth to have more than one library in LD_PRELOAD. :-) What does Linux do? John -- John Polstra jdp@polstra.com John D. Polstra & Co., Inc. Seattle, Washington USA "Disappointment is a good sign of basic intelligence." -- Chögyam Trungpa To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message