Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 21 Jan 2000 19:27:12 +0000
From:      Tony Finch <fanf@demon.net>
To:        freebsd-hackers@freebsd.org, jdp@freebsd.org
Cc:        Tony Finch <fanf@demon.net>
Subject:   LD_PRELOAD
Message-ID:  <E12BjiC-00031J-00@fanf.noc.demon.net>

next in thread | raw e-mail | index | archive | help
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. On Solaris I can just use `gcc -c libshim.c` then
setting LD_PRELOAD to ./libshim.o does the expected thing. If I try
this on FreeBSD then the rtld says:
	/usr/libexec/ld-elf.so.1: ./libshim.o: unsupported file type
I managed to get it to work with this command line:
	gcc -Xlinker -E -Xlinker -noinhibit-exec -shared -o libshim.so libshim.c
which gives me a bunch of errors as follows (related to my next problem):
	/var/tmp/ccP231581.o: In function `_init':
	/var/tmp/ccP231581.o(.text+0x34): multiple definition of `_init'
	/usr/lib/crti.o(.init+0x0): first defined here
	/var/tmp/ccP231581.o: In function `_fini':
	/var/tmp/ccP231581.o(.text+0xdc): multiple definition of `_fini'
	/usr/lib/crti.o(.fini+0x0): first defined here
How do I get it to link cleanly? Do I really have to link it at all?

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). I also note that a lot of the library code in FreeBSD has
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.

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.

Any pearls of wisdom would be gratefully received.

Tony.
-- 
the man who puts the dot at


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?E12BjiC-00031J-00>