Fixing --as-needed problems [Re: SOURCES: rpm-macros.patch - --as-needed by default]

sparky at ep09.pld-linux.org sparky at ep09.pld-linux.org
Tue Aug 1 16:08:11 CEST 2006


On Tue, Aug 01, 2006 at 07:05:38AM +0200, Fryderyk Dziarmagowski wrote:
> --- sparky at ep09.pld-linux.org wrote:
> 
> > i already know how to fix most of them, and if someone asks i can
> > describe the process
> 
> please, do it.

First of all, explanation how it works. With --as-needed option only
libraries which contain symbols required by _object_files_ are linked.
All the ld options are positional, and position is very important.
About positions:

Correct ones are:
$ gcc $(LDFLAGS) -o $@ $(OBJECTS) $(LIBS)
and this generally works well.

If there is something like that:
$ gcc -Wl,--as-needed -l<library> <objects.o>
the library will _never_ be linked, because it's not required at the
time it's checked.

And with something like that:
$ gcc -l<library> -Wl,--as-needed <objects.o>
library will allways be linked, even if it's not required.


Most of problems are with readline/ncurses and tinfo, and it's nice
example:
tinfo is required by both readline and ncurses, and both are linked with
this library (now, when I've fixed readline).

But some packages link with readline or ncurses while they use _only_
symbols from tinfo. Without --as-needed those executables work because
they are linked with tinfo from r./n. libraries. With --as-needed it
will not work, because readline/ncurses contains no symbols required
so they are not linked, it's dependencies naturally are neither linked.
So there is a need to pass -ltinfo. If it requires only symbols from
tinfo it's ok to s/ncurses/tinfo/. But if it realy requires r./n. but
there is some executable (or ./configure) which requires only tinfo both
-l<read/ncur> and -ltinfo should be passed.


Other common problem is when package produces some shared libraries
without linking them with all required libraries, and everything is
linked at the end to one binary. So we have:
$ gcc -Wl,--as-needed -o executable <objects.o> -l2 -l1
and normally objects require only library 2, and -l1 is required by -l2.
As I said it checks only for symbols from objects, so -l1 is not linked.
Normally it is easy to fix it, simply make sure while linking -l2 it is
linked to -l1.


That's all, for now.


And for some more information chceck:
http://www.gentoo.org/proj/en/qa/asneeded.xml

-- 
 ____  Sparky{PI] -- Przemyslaw _  ___  _  _  ........... LANG...Pl..Ca..Es..En
/____) ___  ___  _ _ || Iskra  |  | _ \| |  | : WWW........ppcrcd.pld-linux.org
\____\| -_)'___| ||^'||//\\// <   |  _/| |  | : JID......sparky<at>jabberes.org
(____/||   (_-_|_||  ||\\ ||   |_ |_|  |_| _| : Mail....sparky<at>pld-linux.org


More information about the pld-devel-en mailing list