--as-needed and --no-add-needed linker options

Ian Lance Taylor iant@google.com
Thu Dec 30 01:48:00 GMT 2010


ali hagigat <hagigatali@gmail.com> writes:

> I do not understand the necessity of introducing the options of
> --as-needed and --no-add-needed!
> Why adding or not adding DT_NEEDED tag is so important that two
> separate options are considered for ld?

When the DT_NEEDED tag is used, it means that the dynamic linker must
find the shared library at runtime.  If no symbols are required from the
shared library, then the effort expended to find it is wasted.  The
--no-add-needed option directs the linker to only add a DT_NEEDED tag if
some symbol from the shared library is used by the program.

This is normally used with -lgcc, the supporting library used by gcc.
On many targets, for many programs, no symbols are required from -lgcc.
On some targets, for some programs, symbols are required.  Therefore,
gcc uses --no-as-needed -lgcc --as-needed.  This tells the linker to
only add a DT_NEEDED tag for -lgcc if some symbol is actually required.

The --no-as-needed option is not actually necessary, and the program
will normally work just fine if you don't use it.  It's purely an
optimization.

> I wonder if anybody can explain this tag and the situation when a
> dynamic library references a symbol in another dynamic library.
> If i create a dynamic library with some external references and then
> link it with my program, what happens after executing my program?

I assume that the above information suffices to answer these questions.

Ian



More information about the Binutils mailing list