int a = 1; int b; func1(static_var + static_var2 + a + b);
return a; }
elf header
初次学习主要关注 Start of section headers即可,初次旨在弄清楚文件结构
/* 其中指出了节表的开始地址,也指出了节表中元素数量 */ root@L:/home/l/c++# readelf -h ./elfdemo.o ELF Header: Magic: 7f454c 46020101000000000000000000 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: REL (Relocatable file) Machine: Advanced Micro Devices X86-64 Version: 0x1 Entry point address: 0x0 Start of program headers: 0 (bytes into file) Start of section headers: 1040 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 0 (bytes) Number of program headers: 0 Size of section headers: 64 (bytes) Number of section headers: 14 Section header string table index: 13/*字符串表的节索引*/
typedefstruct { Elf64_Word sh_name; /* Section name (string tbl index) */ Elf64_Word sh_type; /* Section type *//*段的类型(用处)*/ Elf64_Xword sh_flags; /* Section flags *//*标志位*/ Elf64_Addr sh_addr; /* Section virtual addr at execution */ Elf64_Off sh_offset; /* Section file offset *//*文件偏移地址*/ Elf64_Xword sh_size; /* Section size in bytes *//*节长*/ Elf64_Word sh_link; /* Link to another section */ Elf64_Word sh_info; /* Additional section information */ Elf64_Xword sh_addralign; /* Section alignment *//*对齐,若为8,则起始地址除8=0*/ Elf64_Xword sh_entsize; /* Entry size if section holds table *//*项长度,符号表24*/ } Elf64_Shdr;
root@L:/home/l/c++# readelf -S ./elfdemo.o There are 14 section headers, starting at offset 0x410:
Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL000000000000000000000000 00000000000000000000000000000000000 [ 1] .text PROGBITS 000000000000000000000040 00000000000000620000000000000000 AX 001 [ 2] .rela.text RELA 0000000000000000000002f0 00000000000000780000000000000018 I 1118 [ 3] .data PROGBITS 0000000000000000000000a4 00000000000000080000000000000000 WA 004 [ 4] .bss NOBITS 0000000000000000000000ac 00000000000000080000000000000000 WA 004 [ 5] .rodata PROGBITS 0000000000000000000000ac 00000000000000040000000000000000 A 001 [ 6] .comment PROGBITS 0000000000000000000000b0 000000000000002c 0000000000000001 MS 001 [ 7] .note.GNU-stack PROGBITS 0000000000000000000000dc 00000000000000000000000000000000001 [ 8] .note.gnu.pr[...] NOTE 0000000000000000000000e0 00000000000000200000000000000000 A 008 [ 9] .eh_frame PROGBITS 000000000000000000000100 00000000000000580000000000000000 A 008 [10] .rela.eh_frame RELA 000000000000000000000368 00000000000000300000000000000018 I 1198 [11] .symtab SYMTAB 000000000000000000000158 000000000000013800000000000000181288 [12] .strtab STRTAB 000000000000000000000290 000000000000005a 0000000000000000001 [13] .shstrtab STRTAB 000000000000000000000398 00000000000000740000000000000000001 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings), I (info), L (link order), O (extra OS processing required), G (group), T (TLS), C (compressed), x (unknown), o (OS specific), E (exclude), D (mbind), l (large), p (processor specific)
Symbol table(重要节之一)
其中元素结构为:
/* 1.Symbol name,为符号名所在位置的index,而符号名在字符串表中,会在下面介绍 2.st_info:符号类型和绑定, 符号绑定(binding):表示符号的作用域和链接属性,例如是局部符号还是全局符号,还有弱符号。 符号类型(type):表示符号的类型,例如它是一个函数、变量还是某种特殊的符号。 3.st_other,符号可见性,可由符号绑定决定,也可以自定义,决定了是否能被外部引用。 */ typedefuint32_t Elf64_Word; typedefuint16_t Elf64_Section; typedefuint64_t Elf64_Addr; typedefuint64_t Elf64_Xword; typedefstruct { Elf64_Word st_name; /* Symbol name (string tbl index) */ unsignedchar st_info; /* Symbol type and binding */ unsignedchar st_other; /* Symbol visibility */ Elf64_Section st_shndx; /* Section index */ Elf64_Addr st_value; /* Symbol value */ Elf64_Xword st_size; /* Symbol size */ } Elf64_Sym; 一个结构体占:4+2+1+1+8+8=24bytes