Date: Sat, 22 Jan 2000 02:44:08 +0000 From: Tony Finch <dot@dotat.at> To: jdp@polstra.com, freebsd-hackers@freebsd.org Cc: Tony Finch <dot@dotat.at> Subject: Re: LD_PRELOAD Message-ID: <E12BqX2-000Oju-00@fanf.eng.demon.net> In-Reply-To: <200001212024.MAA11973@vashon.polstra.com> References: <E12BjiC-00031J-00@fanf.noc.demon.net> <E12BjiC-00031J-00@fanf.noc.demon.net>
next in thread | previous in thread | raw e-mail | index | archive | help
John Polstra <jdp@polstra.com> wrote:
>Tony Finch <fanf@demon.net> 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?E12BqX2-000Oju-00>
