When to use --dynamic option in nm

后端 未结 2 1818
谎友^
谎友^ 2021-01-31 10:28

Sometimes when I do nm on a .so file (for example, libstdc++.so.6), it says no symbols, and I need to use nm --dynamic. But for some other .so files, I can see the symbols with

相关标签:
2条回答
  • 2021-01-31 11:23

    You need to use --dynamic or -D option on a shared library if it is stripped and thus only contains a dynamic symbol table.

    You may want to use this option for other shared libraries to explicitly display the dynamic symbol table as this is the table that is consulted by the dynamic linker.

    The file utility indicates whether a shared library is stripped or not. Example:

    $ file /usr/lib64/libcrypt-nss-2.26.so
    [..] ELF 64-bit LSB shared object, x86-64 [..], not stripped
    $ file /usr/lib64/libxml2.so.2.9.7
    [..] ELF 64-bit LSB shared object, x86-64 [..], stripped
    

    Example for how the different symbol tables may contain different symbols:

    $ nm -D /usr/lib64/libcrypt-nss-2.26.so | wc -l
    39
    $ nm /usr/lib64/libcrypt-nss-2.26.so | wc -l 
    112
    
    0 讨论(0)
  • 2021-01-31 11:34

    It could very well possible that if your symbol is not exported from your shared library, it would end up in the normal symbol table instead of the dynamic symbol table.

    There are many types of symbols in ELF files.

    • symbols part of the Normal Symbol table. This is the output from mere nm libabc.so or objdump --syms libabc.so. These symbols are only used during static linking.

    • symbols part of the Dynamic Symbol table. This is the output from nm --dynamic libabc.so or objdump --dynamic-syms libabc.so. Dynamic symbol table is the one used by the run-time linker/loader which binds the symbols between the ELF file which refers them and the ELF file which defines them. It is also used by the static linker, while linking a shared library with an application which requires it. This is the component which helps in showing all the undefined symbol errors during linking.

    • Hidden symbols - these are the symbols which have been marked using _attribute_ ((visibility("hidden"))). These symbols are not exported outside and can only be used within the library. The visibility is checked during the linking step, and hence is enforced only for shared libraries. The default visibility is public, i.e. the symbols are exported unless otherwise specified. The behavior can be modified using the -fvisibility=default|internal|hidden|protected.

    Set the default ELF image symbol visibility to the specified option—all symbols will be marked with this unless overridden within the code. Using this feature can very substantially improve linking and load times of shared object libraries, produce more optimized code, provide near-perfect API export and prevent symbol clashes. It is strongly recommended that you use this in any shared objects you distribute. Despite the nomenclature, default always means public ie; available to be linked against from outside the shared object. protected and internal are pretty useless in real-world usage so the only other commonly used option will be hidden. The default if -fvisibility isn't specified is default, i.e., make every symbol public—this causes the same behavior as previous versions of GCC.

    An overview of these techniques, their benefits and how to use them is at http://gcc.gnu.org/wiki/Visibility.

    To answer your question when would you use the --dynamic option of nm, it would be when you want to list all the symbols exported by your shared library, and the only ones that are available to the ELF images which reference them.

    0 讨论(0)
提交回复
热议问题