nm vs "readelf -s"

camino picture camino · Apr 1, 2012 · Viewed 13.5k times · Source

Suppose we have a shared library named libtest.so, there is one function "foo" in it

use the strip to discards all symbols from libtest.so

$strip libtest.so

so ,now if we use:

$nm libtest.so

it will print out:

nm: libtest.so: no symbols

but if we use :

$readelf -s libtest.so 

foo function still can be seen from its result:

...

10: 000005dc 5 FUNC GLOBAL DEFAULT 12 _Z3foov

...

we also can use command strings to check it:

$strings libtest.so

...

_Z3foov

...

here is my question ,why nm give no result for striped libtest.so?

Thanks

Answer

Employed Russian picture Employed Russian · Apr 1, 2012

why nm give no result for striped libtest.so

There are two symbol tables in the original libtest.so: a "regular" one (in .symtab and .strtab sections) and a dynamic one (in .dynsym and .dynstr sections).

If strip removed both symbol tables, you library would be completely useless: the dynamic loader couldn't resolve any symbols in it. So strip does the only thing that makes sense: removes the "regular" symbol table, leaving the dynamic one intact.

You can see symbols in the dynamic symbol table with nm -D or readelf -s.

The "regular" symbol table is useful only for debugging (for example, it contains entries for static functions, which are not exported by the library, and do not show up in the dynamic symbol table).

But the dynamic loader never looks at the "regular" symbol table (which is not in a format suitable for fast symbol lookups); only at the dynamic one. So the "regular" symbol table is not needed for correct program operation, but the dynamic one is.