From owner-freebsd-hackers Fri Jan 21 18:44:19 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from internal.mail.demon.net (internal.mail.demon.net [193.195.224.3]) by hub.freebsd.org (Postfix) with ESMTP id B3F9714EC4 for ; Fri, 21 Jan 2000 18:44:11 -0800 (PST) (envelope-from fanf@demon.net) Received: from fanf.eng.demon.net (fanf.eng.demon.net [195.11.55.89]) by internal.mail.demon.net with ESMTP id CAA05527; Sat, 22 Jan 2000 02:44:09 GMT Received: from fanf by fanf.eng.demon.net with local (Exim 3.12 #3) id 12BqX2-000Oju-00; Sat, 22 Jan 2000 02:44:08 +0000 To: jdp@polstra.com, freebsd-hackers@freebsd.org From: Tony Finch Cc: Tony Finch Subject: Re: LD_PRELOAD In-Reply-To: <200001212024.MAA11973@vashon.polstra.com> References: Message-Id: Date: Sat, 22 Jan 2000 02:44:08 +0000 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG John Polstra wrote: >Tony Finch wrote: >> >> I'm experimenting with using LD_PRELOAD to implement "shim" >> wrappers around functions in libc. 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 That works fine, thanks! Any idea why my clumsy success worked and why my clumsy failures didn't? >> 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. I don't have any choice on Solaris, unfortunately (and my code has to be portable between FreeBSD and Solaris). I suppose in that case Solaris is the implementation and they have reserved it for my use. I gather from the linker errors that FreeBSD has reserved the names for a different use? >You can get what you want like this with gcc: > > void myInitFunction(void) __attribute__ ((constructor)); Is that attribute equivalent to ((section (".init")))? >Or, write it in C++ and use a global constructor. No. Never. No way. NO. :-) >> 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? According to the documentation I looked at it uses arbitrary whitespace as the separator. I haven't looked at the code to check. Thanks for your help. Tony. -- dot it thus To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message