This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: [BUG] ld behavior varies for C++ static initializer depending on .a or .o input
Zack Weinberg <zack at codesourcery dot com> writes:
> Ian Lance Taylor <ian at airs dot com> writes:
>
> > Hal Black <black at ieee dot org> writes:
> >> Yes. That's the desired behavior. If a developer were to develop a
> >> library with a static initializer with side-effects, it would be
> >> something that should be called whenever that library was used.
> >
> > No Unix linker has ever worked that way.
> >
> > Basically, you misunderstand how ld and ar work. If you feel that you
> > have been misled by existing documentation, please let us know so that
> > we can fix it.
>
> I'd like to point out that while this is true for static (.a)
> libraries, it is not true for shared (.so) libraries -- the linker
> generates DT_NEEDED entries for every shared library on the command
> line, whether or not they satisfy undefined symbols. Personally I
> consider this a bug.
I think that one is debatable. It's certainly true that shared
libraries and archive libraries don't work the same way; as you say,
one obvious difference is in fact the very one we are talking
about--all static initializers in a shared library are run, whether
they are required or not. There are a number of other
differences--only in simple case can you just replace a .a file with a
.so file.
I think the general attitude of linker implementors has been that if
you list a shared library on the command line, you must want it in
there. That is, a .so file is in that sense more like a .o file than
a .a file.
But I agree that it could be argued the other way. I don't know of
any ELF linker which does not generate a DT_NEEDED for a .so listed on
the command line, but perhaps there is one.
Ian