Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 14 Feb 2004 17:48:25 -0800 (PST)
From:      =?ISO-8859-1?Q?Mikko_Ty=F6l=E4j=E4rvi?= <mbsd@pacbell.net>
To:        Kimura Fuyuki <fuyuki@nigredo.org>
Cc:        freebsd-current@freebsd.org
Subject:   Re: relocator oddity?
Message-ID:  <20040214172047.K96655@sotec.home>
In-Reply-To: <86n07oa7sa.wl%fuyuki@nigredo.org>
References:  <86n07oa7sa.wl%fuyuki@nigredo.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 12 Feb 2004, Kimura Fuyuki wrote:

> Someone please run the test below and tell me why dltest[12] show the
> different results on my 5.2.1-RC2 box. At least NetBSD 1.5.2 and some
> sort of Linux pass the test.
>
> #!/bin/sh
>
> cat <<[EOF] >shared.c
> double sin(double d) {return 9;}
> double faked_sin(double d) {return sin(d);}
> [EOF]
>
> cat <<[EOF] >dltest.c
> #include <stdio.h>
> #include <dlfcn.h>
>
> int main(void)
> {
> 	void *h;
> 	double (*faked_sin)(double);
>
> 	h = dlopen("./shared.so", RTLD_LAZY);
>         faked_sin = dlsym(h, "faked_sin");
> 	printf("%f\n", faked_sin(0));
> }
> [EOF]
>
> cc -shared -o shared.so shared.c

# Try this:
cc -shared -o shared.so shared.c -Wl,-Bsymbolic

> cc dltest.c -o dltest1
> ./dltest1 >result1
>
> cc dltest.c -o dltest2 -lm
> ./dltest2 >result2
>
> if diff result1 result2 >/dev/null; then
>         echo "You are very talented."
> else
> 	echo "Please tell me what's happening!"
> fi

Use the line I changed above and try again.

If you want to ensure that global symbol refereces inside a shared
object are bound to symbols inside the object itself, you'll have to
tell the linker to do so, otherwise it will resolve the symbols at run
time, possibly from somewhere else.

Another way is to make your internal "sin()" implementation static.

This is how one can do lots of funny things with LD_PRELOAD :)

I see the same result on Solaris 9 (w/ gcc 3.3) as on FreeBSD.

I don't know if there is a complete specification for the dynamic
linker search order somewhere, or if it is "implentation defined".  I
do know that there are subtle differences between systems, though.

    $.02,
    /Mikko



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