Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 25 Dec 1998 12:23:26 +0500 (KGT)
From:      CyberPsychotic <fygrave@tigerteam.net>
To:        freebsd-hackers@FreeBSD.ORG
Subject:   dynamic libraries loading
Message-ID:  <Pine.LNX.4.05.9812251142440.319-100000@gizmo.kyrnet.kg>

next in thread | raw e-mail | index | archive | help
Hello people,
 sorry for pulling another question in a row, but here I got stuck abit
with another thing while bringing my library (loaded with LD_PRELOAD) from
linux to BSD world:

 I used my own _init(..) function on Linux, which is called, when the
library is loaded, and compiling it library with -nostdlib switch was
enough to get rid off names collision. However I noticed that neither
FreeBSD nor BSDi calls _init when loads library.

 On bsdi-users mailing list I was suggested to use
__attribute__((__constructor__)) to achieve this . The similar thing seem
to work on FreeBSD as well, but I am wondering if any other technique
could be used?

thanks beforehands

Fyodor


---------- Forwarded message ----------
Date: Thu, 24 Dec 1998 10:39:54 -0700
From: Donn Seeley <donn@bsdi.com>
To: fygrave@tigerteam.net
Cc: seebs@plethora.net, bsdi-users@bsdi.com, unix-lizards@mailinglists.org
Subject: Re: LD_PRELOAD equvalency 

	Date: Thu, 24 Dec 1998 10:22:12 +0500 (KGT)
	From: CyberPsychotic <mlists@gizmo.kyrnet.kg>

	[...]
	~ Also, you should identify the version you're using.

	BSDi 3.1 server.

As others have pointed out, BSD/OS 3.1 didn't support dynamically
linked executables, and LD_PRELOAD is a feature of dynamically
linked executables.  3.1 did support dynamically linked objects
(dlopen(3)) but that feature alone doesn't provide support for
LD_PRELOAD.  If you want to use LD_PRELOAD, I recommend that you
upgrade to 4.0.

	well, yep.. , but here's what I am trying to do:  [...]

	bash-2.02# cat lib.c
	#include <stdio.h>

	int _init(void) {

	printf("I am here\n");
	return 0;
	}
	bash-2.02# gcc -shared lib.c -o lib.so
	bash-2.02# LD_PRELOAD="./lib.so"; export LD_PRELOAD
	bash-2.02# /usr/bin/troff aaa
	/usr/bin/troff: fatal error: can't open `aaa': No such file or directory
	bash-2.02#ktrace -f ktr.out /usr/bin/troff aaa
	bash-2.02#ktdump -f ktr.out | grep lib.so

	gives nothing. So it doesn't seem to even attempt to load
	it. Ideas?

This won't work in either 3.1 or 4.0.  /usr/bin/troff is statically
linked in both versions of BSD/OS, and LD_PRELOAD won't affect a
statically linked program.  However, groff (GNU troff) sources are
provided on the contributed software CD-ROM that comes with every
distribution, so you can rebuild troff as a dynamically linked
executable if you want to.  (Use 'LDCC=cc; export LDCC' or 'setenv
LDCC cc', as appropriate for your shell, to override the default
shlicc(1) link command.)

Even if you use a dynamically linked executable in 4.0, you should
do this slightly differently.  Here's an example using cat(1) from
the BSD/OS source distribution:

% cat here.c
#include <stdio.h>

static void my_init(void) __attribute__((__constructor__));

static void
my_init()
{

        printf("I am here\n");
}
% cc -shared -o here.so here.c -lc
% cc -o cat -O2 /usr/src/bin/cat/cat.c
% (setenv LD_PRELOAD $cwd/here.so; ./cat /etc/fstab)
I am here
#
# Filesystem mount table information.  See the fstab(5) man page
# and the /etc/fstab.sample file for more information and examples.
[...]
% 

Note that building cat(1) with cc(1) instead of shlicc(1) produces
a dynamically linked executable in 4.0:

% file cat /bin/cat
cat:      ELF 32-bit LSB executable, Intel 80386, version 1, dynamically linked
/bin/cat: ELF 32-bit LSB executable, Intel 80386, version 1, statically linked, stripped
% shlist cat /bin/cat
cat: /shlib/ld-bsdi.so libgcc.so libc.so
/bin/cat: /shlib/libc_s.4.0.0
% size cat /bin/cat
text    data    bss     dec     hex
4169    388     364     4921    1339    cat
2241    160     36      2437    985     /bin/cat
% 

You should also note that it isn't safe to attempt to replace
symbols like _init in dynamically linked programs.  The GCC
__attribute__((__constructor__)) feature is a handy way of getting
the right effect (although it's not portable).

Donn Seeley / BSDI Engineering


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?Pine.LNX.4.05.9812251142440.319-100000>