$ size main.o # section sizes text data bss dec hex filename 179 8 4 191 bf main.o
Extract one section
1 2 3 4 5
$ dd if=main.o of=code.o bs=1 count=87 skip=64 $ objdump -D -b binary -m i386 code.o > codedump $ objdump -d main.o > maindump $ diff maindump codedump # We can see that we have the same bytes
$ nm main.o U _GLOBAL_OFFSET_TABLE_ 0000000000000000 T func1 0000000000000000 D global_init_var 0000000000000004 C global_uninit_var 0000000000000024 T main U printf 0000000000000004 d static_var.1802 0000000000000000 b static_var2.1803
$ readelf -s main.o
Symbol table '.symtab' contains 17 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS main.c 2: 0000000000000000 0 SECTION LOCAL DEFAULT 1 3: 0000000000000000 0 SECTION LOCAL DEFAULT 3 4: 0000000000000000 0 SECTION LOCAL DEFAULT 4 5: 0000000000000000 0 SECTION LOCAL DEFAULT 5 6: 0000000000000004 4 OBJECT LOCAL DEFAULT 3 static_var.1802 7: 0000000000000000 4 OBJECT LOCAL DEFAULT 4 static_var2.1803 8: 0000000000000000 0 SECTION LOCAL DEFAULT 7 9: 0000000000000000 0 SECTION LOCAL DEFAULT 8 10: 0000000000000000 0 SECTION LOCAL DEFAULT 6 11: 0000000000000000 4 OBJECT GLOBAL DEFAULT 3 global_init_var 12: 0000000000000004 4 OBJECT GLOBAL DEFAULT COM global_uninit_var 13: 0000000000000000 36 FUNC GLOBAL DEFAULT 1 func1 14: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND _GLOBAL_OFFSET_TABLE_ 15: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND printf 16: 0000000000000024 51 FUNC GLOBAL DEFAULT 1 main
$ objdump --syms main.o
main.o: file format elf64-x86-64
SYMBOL TABLE: 0000000000000000 l df *ABS* 0000000000000000 main.c 0000000000000000 l d .text 0000000000000000 .text 0000000000000000 l d .data 0000000000000000 .data 0000000000000000 l d .bss 0000000000000000 .bss 0000000000000000 l d .rodata 0000000000000000 .rodata 0000000000000004 l O .data 0000000000000004 static_var.1802 0000000000000000 l O .bss 0000000000000004 static_var2.1803 0000000000000000 l d .note.GNU-stack 0000000000000000 .note.GNU-stack 0000000000000000 l d .eh_frame 0000000000000000 .eh_frame 0000000000000000 l d .comment 0000000000000000 .comment 0000000000000000 g O .data 0000000000000004 global_init_var 0000000000000004 O *COM* 0000000000000004 global_uninit_var 0000000000000000 g F .text 0000000000000024 func1 0000000000000000 *UND* 0000000000000000 _GLOBAL_OFFSET_TABLE_ 0000000000000000 *UND* 0000000000000000 printf 0000000000000024 g F .text 0000000000000033 main
Symbol decorate
How c++ support function overload and multi-namespace?
The compiler will “hash” a symbol and assign a signature to it
G++
Visual C++
Since different compiler use different decoration strategy we have difficult in linking object files compiled by different compiler
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 test.cpp 2: 0000000000000000 0 SECTION LOCAL DEFAULT 1 3: 0000000000000000 0 SECTION LOCAL DEFAULT 2 4: 0000000000000000 0 SECTION LOCAL DEFAULT 3 5: 0000000000000000 0 SECTION LOCAL DEFAULT 5 6: 0000000000000000 0 SECTION LOCAL DEFAULT 6 7: 0000000000000000 0 SECTION LOCAL DEFAULT 4 8: 0000000000000000 10 FUNC GLOBAL DEFAULT 1 _Z4funci 9: 000000000000000a 12 FUNC GLOBAL DEFAULT 1 _Z4funcd
C used to decorate symbols with a prefixed _, but it doesn’t do this nowadays, so we can prevent c++ symbol decoration by declaring that these codes are C codes