Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nm vs "readelf -s"

Tags:

linux

nm

readelf

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

like image 372
camino Avatar asked Apr 01 '12 02:04

camino


People also ask

What is readelf read option in Linux?

This option causes readelf to print each section header resp. each segment one a single line, which is far more readable on terminals wider than 80 columns. Display the command line options understood by readelf . Read command-line options from file. The options read are inserted in place of the original @ file option.

Why NM give no result for striped libtest?

1 Answer 1 ActiveOldestVotes 64 why nm give no result for striped libtest.so There are twosymbol tables in the original libtest.so: a "regular" one (in .symtaband .strtabsections) and a dynamic one (in .dynsymand .dynstrsections).

Should readelf break output lines to fit into 80 columns?

Don’t break output lines to fit into 80 columns. By default readelf breaks section header and segment listing lines for 64-bit ELF files, so that they fit into 80 columns. This option causes readelf to print each section header resp. each segment one a single line, which is far more readable on terminals wider than 80 columns.

How does readelf read a 64-bit ELF file?

By default readelf breaks section header and segment listing lines for 64-bit ELF files, so that they fit into 80 columns. This option causes readelf to print each section header resp. each segment one a single line, which is far more readable on terminals wider than 80 columns. Display the command line options understood by readelf .


1 Answers

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.

like image 172
Employed Russian Avatar answered Sep 22 '22 13:09

Employed Russian