Linux ELF - Why does normal linking run faster than 'ldd -r'?

79 views Asked by At

I have an exe in which none of the code changed, but I am afraid that it links to symbols that no longer exist on its shared objects. I found two ways to test that:

  • Run ldd -r
  • Relink the exe

In some cases it seems like relinking is faster than running ldd -r what is the reason for this?

1

There are 1 answers

1
Employed Russian On

In some cases it seems like relinking is faster than running ldd -r what is the reason for this?

Consider a simple case: main.o calls foo() from libfoo.so, and is linked like this:

gcc main.o -L. -lfoo

The amount of work ld has to do: discover that foo is being called, find that it is defined in libfoo.so, done. Not very much work.

Now suppose that libfoo.so itself has been linked against libbar.so, and calls 10000000 different symbols from it.

What does ldd -r have to do? It will first look in a.out for any unresolved symbols (there is only one: foo), and find a definition for it in libfoo.so (easy). Next it has to consider every undefined symbol in libfoo.so, and find a definition for all of them as well (in libbar.so). That is about 1000000 times harder. Repeat for libbar.so, and every other library linked into it.

It should not then be very surprising then that under above conditions ld will take significantly less time than ldd -r.