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;
main程序的符号表内容:
1 2 3 4 5 6 7 8 9 10
[root@localhost test]# readelf -s test.o
Symbol table '.symtab' contains 6 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.c 2: 0000000000000000 0 SECTION LOCAL DEFAULT 1 .text 3: 0000000000000000 0 SECTION LOCAL DEFAULT 5 .rodata 4: 0000000000000000 21 FUNC GLOBAL DEFAULT 1 main 5: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND puts
typedefstruct { Elf64_Half si_boundto; /* Direct bindings, symbol bound to */ Elf64_Half si_flags; /* Per symbol flags */ } Elf64_Syminfo;
/* Legal values for ST_BIND subfield of st_info (symbol binding). */ #define STB_LOCAL 0 /* Local symbol */ #define STB_GLOBAL 1 /* Global symbol */ #define STB_WEAK 2 /* Weak symbol */ ...
/* Legal values for ST_TYPE subfield of st_info (symbol type). */ #define STT_NOTYPE 0 /* Symbol type is unspecified */ #define STT_OBJECT 1 /* Symbol is a data object */ #define STT_FUNC 2 /* Symbol is a code object */ ...
int a; int b = 1; extern c = 0; extern d = 1; __attribute((weak)) e; __attribute((weak)) f = 1;
voidg() { a = 1; b = 1; c = 1; d = 1; e = 1; f = 1; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
[root@localhost test]# readelf -s sym.o
Symbol table '.symtab' contains 10 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS sym.c 2: 0000000000000000 0 SECTION LOCAL DEFAULT 1 .text 3: 0000000000000000 4 OBJECT GLOBAL DEFAULT 4 a 4: 0000000000000000 4 OBJECT GLOBAL DEFAULT 3 b 5: 0000000000000004 4 OBJECT GLOBAL DEFAULT 4 c 6: 0000000000000004 4 OBJECT GLOBAL DEFAULT 3 d 7: 0000000000000008 4 OBJECT WEAK DEFAULT 4 e 8: 0000000000000008 4 OBJECT WEAK DEFAULT 3 f 9: 0000000000000000 67 FUNC GLOBAL DEFAULT 1 g
可重定位.rela编码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
typedefstruct { Elf64_Addr r_offset; /* Address */ Elf64_Xword r_info; /* Relocation type and symbol index */ Elf64_Sxword r_addend; /* Addend */ } Elf64_Rela;
/* How to extract and insert information held in the r_info field. */ #define ELF64_R_SYM(i) ((i) >> 32) #define ELF64_R_TYPE(i) ((i) & 0xffffffff) #define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type))
/* AMD x86-64 relocations. */ #define R_X86_64_64 1 /* Direct 64 bit */ #define R_X86_64_PC32 2 /* PC relative 32 bit signed */ #define R_X86_64_GOT32 3 /* 32 bit GOT entry */ #define R_X86_64_PLT32 4 /* 32 bit PLT address */
Relocation section '.rela.text' at offset 0x198 contains 3 entries: Offset Info Type Sym. Value Sym. Name + Addend 00000000000a 000400000004 R_X86_64_PLT32 0000000000000000 undef_func - 4 000000000010 000500000002 R_X86_64_PC32 0000000000000000 undef_array - 8 00000000001a 000500000002 R_X86_64_PC32 0000000000000000 undef_array - 4
Relocation section '.rela.eh_frame' at offset 0x1e0 contains 1 entry: Offset Info Type Sym. Value Sym. Name + Addend 000000000020 000200000002 R_X86_64_PC32 0000000000000000 .text + 0
结合被引用的c文件:
1 2
voidundef_func() {} int undef_array[2] = {-1, -2};