Where are global variables located in the elf file

前端 未结 2 1749
面向向阳花
面向向阳花 2021-01-02 15:52

I want to learn about elf files, but when I think of global variables, global static variables and scope static variables, I have some confusion. For example:



        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 16:33

    You can use objdump -t to view the symbol table:

    $ objdump -t foo | grep -P '      \b(a|b|c|d|e|f)\b'
    0000000000601034 l     O .data  0000000000000004              c
    0000000000601040 l     O .bss   0000000000000004              d
    0000000000601044 l     O .bss   0000000000000004              f.1710
    0000000000601038 l     O .data  0000000000000004              e.1709
    0000000000601048 g     O .bss   0000000000000004              b
    0000000000601030 g     O .data  0000000000000004              a
    

    You are right that b, d, and f are .bss while a, c, and e are .data. Whether the symbol is static or not is recorded in a separate flag of the symbol table—that’s the l or g flag in the second column.

    The elf(5) man page says that these are recorded using the STB_LOCAL and STB_GLOBAL values for the st_info member of the symbol table. /usr/include/elf.h says that STB_GLOBAL is 1, while STB_LOCAL is 0. There is a macro ST_BIND to retrieve the binding bits of the st_info field.


    There are tons of other flags for objdump—see the man page. objdump works with all architectures, but there is also an elfdump tool that does a bit better job of showing elf-specific stuff. objdump and the underlying BFD library can do a bad job of showing some file-format-specific data.

提交回复
热议问题