Programmer's self-cultivation chapeter 3 object file






  • Programmer's leveling manual
    https://coolshell.cn/articles/4990.html
  • Insert picture description here

ELF file

  • ELF and PE are both variants of COFF files, so .lib and .dll under windows (library static; dynamicl linking library dynamic) are very similar to .a and .so under Linux (archive static; shared object dynamic)

The common object file format (English: Common Object File Format, abbreviated as COFF), also known as the common object file format, is a file format used for executable files, object codes, and shared libraries (shared library), used on UNIX-like systems .
Insert picture description here

format

The most basic .o file looks like this
Insert picture description here

.bss

.bss段存放未初始化的全局变量和局部静态变量
未初始化的静态变量肯定是在这个段里面的,但是未初始化的全局变量不一定放着里面
当静态变量被赋值为0的时候被看作没有初始化,所以优化掉了可以被放在.bss段里面

.data

.data段存放存放已初始化的全局变量和局部静态变量

.text / .code

.text段存放代码

In addition to this, the object file has many other sections


command

readelf

  • -s symbol
  • -t --section-details
  • -h --file-header-
    > no -f option

objdump -s (source) -t ( symbol table ) -h section header

od command


objdump

  • -f file header
  • -h section header
    Insert picture description here
    -h The framed line is an attribute. Content indicates that the content of this section is stored in this file. You can see that the .bss section is not stored in this file.
  • -d disassemble
  • -s full-contents in hex format
  • -t print symbol table

size

Check the length of the code segment, data segment, BSS segment.
Insert picture description here
The segment name written by yourself cannot start with a dot (.)


objcopy

  • Binary file as a segment in the target file
  • -R parameter can remove certain segments

nm

The abbreviation of name
nm xx.o-> symbol value (heximal) symbol_type symbol name
and some parameters can be used to display only certain segments


Custom segment




3.4 File structure description

Insert picture description here

  • file header
    readelf -h / objdump -f 命令

===== Let's review it from above

3.5

  • Function signature

  • c ++ filt

  • Why does p94 define a weak reference pthread_create? Because only weak references will find strong references otherwise you will not load even if -lpthread linker. Then after it is defined, it will not look like without -lpthread. The value of weak reference defaults to 0 (or a value specified by a program), so if (weak_reference) can realize the code between multiple different function versions Flexible conversion without writing multiple copies

  • There is one difference between C and C ++ compilers: C will not modify variables when C is compiled, but C ++ will

Published 177 original articles · Like 28 · Visits 50,000+

Guess you like

Origin blog.csdn.net/Hesy_H/article/details/100782411