From owner-freebsd-hackers Fri Jan 21 11:27:41 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from fanf.noc.demon.net (fanf.noc.demon.net [195.11.55.83]) by hub.freebsd.org (Postfix) with ESMTP id 59C1E158CE; Fri, 21 Jan 2000 11:27:16 -0800 (PST) (envelope-from fanf@demon.net) Received: from fanf by fanf.noc.demon.net with local (Exim 3.02 #13) id 12BjiC-00031J-00; Fri, 21 Jan 2000 19:27:12 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit From: Tony Finch To: freebsd-hackers@freebsd.org, jdp@freebsd.org Cc: Tony Finch Subject: LD_PRELOAD X-Mailer: VM 6.34 under Emacs 19.34.1 Message-Id: Date: Fri, 21 Jan 2000 19:27:12 +0000 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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