Subject: Re: libpthread and static constructor order
To: None <firstname.lastname@example.org>
From: J.T. Conklin <email@example.com>
Date: 11/03/2007 10:33:48
Joerg Sonnenberger <firstname.lastname@example.org> writes:
> On Fri, Nov 02, 2007 at 06:57:56AM -0700, J.T. Conklin wrote:
>> > On Thu, Nov 01, 2007 at 08:53:53PM -0700, J.T. Conklin wrote:
>> >> I'm running into this problem now with ACE / TAO, which has static
>> >> ctors which end up using pthread's thread specific storage.
>> > In that case the library has to explicitly link against libpthread. ELF
>> > constructors are run in dependency order and if you can build the
>> > library with -z defs, you should be on the save side.
>> Hmm... This is something I tried yesterday without much success.
> Note that what I said above is how it *should* behave. From reading
> ld.elf_so I am not sure whether it is actually doing that. Can you check
> in which order the DT_NEEDED entries are in the binary? I think the part
> about resorting the order list is not done at all...
Since ACE / TAO are huge, I wrote a small unit test program to verify
NetBSD's behavior (see the enclosed shar file).
To simplify things, I used gcc's __constructor__ attribute instead of
C++. There are two shared libraries, libfoo and libbar. libfoo has a
constructor that writes "foo\n" to standard error; while libbar's ctor
writes "bar\n". libfoo has no dependencies, while libbar depends on
I've linked a "null" main() program with these shared libraries. For
"foobar", I've linked with -lfoo -lbar; for "barfoo", I've linked with
and my understanding is the output should be "foo\nbar\n" regardless
of the link order specified when compiling the main program, as libbar
depends on libfoo. But "foobar" (which links with -lfoo -lbar), prints
"bar\nfoo\n"., indicating that libbar is initialized before libfoo.
So while Thor's right that "libfool" (heh) is wrong in suppressing the
libpthread dependency, it appears that NetBSD ld.elf_so also needs
# This is a shell archive. Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file". Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
# This archive contains:
echo x - Makefile
sed 's/^X//' >Makefile << 'END-of-Makefile'
Xall: barfoo foobar
X rm -f barfoo foobar *.o *.so
Xfoobar: libfoo.so libbar.so
Xbarfoo: libfoo.so libbar.so main.o
X $(CC) $(LDFLAGS) -o $@ main.o -lbar -lfoo
Xfoobar: libfoo.so libbar.so main.o
X $(CC) $(LDFLAGS) -o $@ main.o -lfoo -lbar
X $(CC) $(CFLAGS) -o $@ -c main.c
X $(CC) $(CFLAGS) -o $@ -fPIC -c foo.c
X $(CC) -o $@ -fPIC -c bar.c
X $(CC) $(LDFLAGS) -o $@ -shared foo.o
X $(CC) $(LDFLAGS) -o $@ -shared bar.o -lfoo
echo x - bar.c
sed 's/^X//' >bar.c << 'END-of-bar.c'
Xvoid bar_ctor() __attribute__((__constructor__));
X write(2, "bar\n", 4);
echo x - foo.c
sed 's/^X//' >foo.c << 'END-of-foo.c'
Xvoid foo_ctor() __attribute__((__constructor__));
X write(2, "foo\n", 4);
echo x - main.c
sed 's/^X//' >main.c << 'END-of-main.c'
X return 0;