nm vs "readelf -s"
Asked Answered
W

1

36

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

Witling answered 1/4, 2012 at 2:36 Comment(0)
I
66

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.

Impart answered 1/4, 2012 at 2:55 Comment(3)
Your username is awesome. So, basically, if I link a static library against a binary at compile time, the library's symbols will show up with nm, but if I do dynamic linking, the symbols will show up in readelf -S and nm -D, correct?Mudfish
@AlejandroCárdenas I don't believe your understanding is quite correct. Also, there is a difference between readelf -s and readelf -S. It's probably better to ask a separate question.Impart
Yes, sorry, I meant readelf -s. Well, the core question remains, I do not understand the difference between nm and readelf -s since you tell me what I understood is incorrect. Would you care to elaborate a bit more, please?Mudfish

© 2022 - 2024 — McMap. All rights reserved.