Use dlinfo to print all symbols in a library

风格不统一 提交于 2019-12-06 00:19:46

There are (at least) two bugs in your program:

  • you are subtracting load_addr, but that only works on systems where libm.so.6 is prelinked. On systems where it is not prelinked, your program crashes trying to print SONAME.
  • your symbol printing loop: while (syment->st_shndx != STN_UNDEF) { is incorrect, because shared libraries start with just such a symbol, e.g.

    readelf -Ws /lib/x86_64-linux-gnu/libm.so.6 | head
    
    Symbol table '.dynsym' contains 415 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND   <<<-- your loop stops here
         1: 00000000000053c8     0 SECTION LOCAL  DEFAULT   11
         2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __errno_location@GLIBC_2.2.5 (5)
         3: 0000000000000000     0 TLS     GLOBAL DEFAULT  UND errno@GLIBC_PRIVATE (6)
         4: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND strtod@GLIBC_2.2.5 (5)
         5: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND strlen@GLIBC_2.2.5 (5)
         6: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __get_cpu_features@GLIBC_PRIVATE (6)
    

Instead, you should loop until you've looped over all the symbols (I believe the only way you can calculate the number of symbols is by decoding DT_HASH or DT_GNU_HASH).

When I adjust your source for the two problems above, I get:

Needed:
 - libc.so.6
-------------------------------------------------------------------------------
Symbols:
|Name            |Type      |Visibility|Section |Addr            |Size    |
|                |         0|         0|       0|(nil)           |       0|
|                |         3|         0|      11|0x53c8          |       0|
|__errno_location|        18|         0|       0|(nil)           |       0|
|errno           |        22|         0|       0|(nil)           |       0|
|strtod          |        18|         0|       0|(nil)           |       0|
|strlen          |        18|         0|       0|(nil)           |       0|
|__get_cpu_features|        18|         0|       0|(nil)           |       0|
|__assert_fail   |        18|         0|       0|(nil)           |       0|
|fputs           |        18|         0|       0|(nil)           |       0|
|strtof          |        18|         0|       0|(nil)           |       0|

... which matches readelf output.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!