[tools]GNU 開發工具:nm

[tools]GNU 開發工具:nm

nm

nm這東西是來列出object files(binary file)的符號(symbol),包括列出地址(address)、類型、名稱。

以下我打了一個範例。

#include<stdio.h>

void foo(){
    printf("bar");
}

int Global_var;
int Global_var_init = 0;

int main()
{
    int local_var;
    int local_var_init = 0;
    int a = local_var_init + Global_var_init;
    foo();
}

接下來是對其編譯出來的a.out下nm指令的結果。

0000000000601030 B __bss_start
0000000000601030 b completed.6916
0000000000601020 D __data_start
0000000000601020 W data_start
0000000000400430 t deregister_tm_clones
00000000004004b0 t __do_global_dtors_aux
0000000000600e10 t __do_global_dtors_aux_fini_array_entry
0000000000601028 D __dso_handle
0000000000600e20 d _DYNAMIC
0000000000601030 D _edata
0000000000601040 B _end
00000000004005b4 T _fini
00000000004004f6 T foo
00000000004004d0 t frame_dummy
0000000000600e08 t __frame_dummy_init_array_entry
0000000000400718 r __FRAME_END__
0000000000601000 d _GLOBAL_OFFSET_TABLE_
0000000000601038 B Global_var
0000000000601034 B Global_var_init
                 w __gmon_start__
00000000004005c8 r __GNU_EH_FRAME_HDR
00000000004003c8 T _init
0000000000600e10 t __init_array_end
0000000000600e08 t __init_array_start
00000000004005c0 R _IO_stdin_used
0000000000600e18 d __JCR_END__
0000000000600e18 d __JCR_LIST__
00000000004005b0 T __libc_csu_fini
0000000000400540 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
000000000040050c T main
                 U printf@@GLIBC_2.2.5
0000000000400470 t register_tm_clones
0000000000400400 T _start
0000000000601030 D __TMC_END__ 

第一列為地址,第二列為類型,第三列為名稱。類型部份可以man一下nm。以下是擷取自man nm。

DESCRIPTION
       GNU nm lists the symbols from object files objfile....  If no object files are listed as arguments, nm assumes the file a.out.

       For each symbol, nm shows:

       ·   The symbol value, in the radix selected by options (see below), or hexadecimal by default.

       ·   The symbol type.  At least the following types are used; others are, as well, depending on the object file format.  If lowercase,
           the symbol is usually local; if uppercase, the symbol is global (external).  There are however a few lowercase symbols that are
           shown for special global symbols ("u", "v" and "w").

           "A" The symbol's value is absolute, and will not be changed by further linking.

           "B"
           "b" The symbol is in the uninitialized data section (known as BSS).

           "C" The symbol is common.  Common symbols are uninitialized data.  When linking, multiple common symbols may appear with the same
               name.  If the symbol is defined anywhere, the common symbols are treated as undefined references.

           "D"
           "d" The symbol is in the initialized data section.

           "G"
           "g" The symbol is in an initialized data section for small objects.  Some object file formats permit more efficient access to small
               data objects, such as a global int variable as opposed to a large global array.

           "i" For PE format files this indicates that the symbol is in a section specific to the implementation of DLLs.  For ELF format
               files this indicates that the symbol is an indirect function.  This is a GNU extension to the standard set of ELF symbol types.
               It indicates a symbol which if referenced by a relocation does not evaluate to its address, but instead must be invoked at
               runtime.  The runtime execution will then return the value to be used in the relocation.

           "I" The symbol is an indirect reference to another symbol.

           "N" The symbol is a debugging symbol.

           "p" The symbols is in a stack unwind section.
           
                      "R"
           "r" The symbol is in a read only data section.

           "S"
           "s" The symbol is in an uninitialized data section for small objects.

           "T"
           "t" The symbol is in the text (code) section.

           "U" The symbol is undefined.

           "u" The symbol is a unique global symbol.  This is a GNU extension to the standard set of ELF symbol bindings.  For such a symbol
               the dynamic linker will make sure that in the entire process there is just one symbol with this name and type in use.

           "V"
           "v" The symbol is a weak object.  When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is
               used with no error.  When a weak undefined symbol is linked and the symbol is not defined, the value of the weak symbol becomes
               zero with no error.  On some systems, uppercase indicates that a default value has been specified.

           "W"
           "w" The symbol is a weak symbol that has not been specifically tagged as a weak object symbol.  When a weak defined symbol is
               linked with a normal defined symbol, the normal defined symbol is used with no error.  When a weak undefined symbol is linked
               and the symbol is not defined, the value of the symbol is determined in a system-specific manner without error.  On some
               systems, uppercase indicates that a default value has been specified.

           "-" The symbol is a stabs symbol in an a.out object file.  In this case, the next values printed are the stabs other field, the
               stabs desc field, and the stab type.  Stabs symbols are used to hold debugging information.

           "?" The symbol type is unknown, or object file format specific.

       ·   The symbol name.

這個工具可以幫助我們了解object file的資料layout,函式定義等,對於無source code只有object files的開發者來說十分方便(有source code可用compiler的參數來分析)。

nm可以幫助我們分析undefined reference狀況與data的分區(bss,text,data)。

相關連結:1.http://www.wuzesheng.com/?p=1595
                 2.http://sp1.wikidot.com/elfobjfile
                 3.http://enginechang.logdown.com/posts/248172-linker-loader-library
            4.https://github.com/torvalds/linux/blob/9256d5a308c95a50c6e85d682492ae1f86a70f9b/arch/powerpc/boot/elf.h

comments powered by Disqus