Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 21 Jan 2000 12:24:49 -0800 (PST)
From:      John Polstra <jdp@polstra.com>
To:        fanf@demon.net
Cc:        hackers@freebsd.org
Subject:   Re: LD_PRELOAD
Message-ID:  <200001212024.MAA11973@vashon.polstra.com>
In-Reply-To: <E12BjiC-00031J-00@fanf.noc.demon.net>
References:  <E12BjiC-00031J-00@fanf.noc.demon.net>

next in thread | previous in thread | raw e-mail | index | archive | help
In article <E12BjiC-00031J-00@fanf.noc.demon.net>,
Tony Finch  <fanf@demon.net> 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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200001212024.MAA11973>