grxer@Ubuntu16 /m/h/S/l/chapter3> gcc --version gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
中间会穿插一点11.3.0版本 just a little bit
grxer@Ubuntu22-wsl ~/s/nemu [SIGINT]> gcc --version gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The following types are used for N-bit architectures (N=32,64, ElfN
stands for Elf32 or Elf64, uintN_t stands for uint32_t or uint64_t):
ElfN_Addr Unsigned program address, uintN_t
ElfN_Off Unsigned file offset, uintN_t
ElfN_Section Unsigned section index, uint16_t
ElfN_Versym Unsigned version symbol information, uint16_t
Elf_Byte unsigned char
ElfN_Half uint16_t
ElfN_Sword int32_t
ElfN_Word uint32_t
ElfN_Sxword int64_t
ElfN_Xword uint64_t
simple_section.c
intprintf(constchar* format, ...);
int global_init_var = 84; int global_uninit_var; staticint global_static_var;
SHF_WRITE This section contains data that should be writable during process execution.
SHF_ALLOC This section occupies memory during process execution. Some control sections do notreside in the memory image of an object file. This attribute is off for those sections.表示该节在进程空间里要分配内存,有些节是一些控制信息不会被加载到进程空间,一般代码段数据段,和.bss段都会有这个表示,另外一些节完成任务之后就扔了尼,嘿嘿
SHF_EXECINSTR This section contains executable machineinstructions.
SHF_MASKPROC All bits included in this mask are reserved for processor-specific semantics.
grxer@Ubuntu16 /m/h/S/l/chapter4> readelf -S a.o There are 12 section headers, starting at offset 0x220:
Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL0000000000000000000000000 [ 1] .text PROGBITS 0000000000003400003900 AX 001 [ 2] .rel.text REL 000000000001b0 00001008 I 1014 [ 3] .data PROGBITS 0000000000006d 00000000 WA 001 [ 4] .bss NOBITS 0000000000006d 00000000 WA 001 [ 5] .comment PROGBITS 0000000000006d 00003601 MS 001 [ 6] .note.GNU-stack PROGBITS 000000000000a3 00000000001 [ 7] .eh_frame PROGBITS 000000000000a4 00004400 A 004 [ 8] .rel.eh_frame REL 000000000001c0 00000808 I 1074 [ 9] .shstrtab STRTAB 000000000001c8 00005700001 [10] .symtab SYMTAB 000000000000e80000b0 101184 [11] .strtab STRTAB 0000000000019800001600001 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) grxer@Ubuntu16 /m/h/S/l/chapter4> readelf -S b.o There are 11 section headers, starting at offset 0x1f4:
Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL0000000000000000000000000 [ 1] .text PROGBITS 0000000000003400003900 AX 001 [ 2] .data PROGBITS 0000000000007000000400 WA 004 [ 3] .bss NOBITS 0000000000007400000000 WA 001 [ 4] .comment PROGBITS 0000000000007400003601 MS 001 [ 5] .note.GNU-stack PROGBITS 000000000000aa 00000000001 [ 6] .eh_frame PROGBITS 000000000000ac 00003800 A 004 [ 7] .rel.eh_frame REL 0000000000019800000808 I 964 [ 8] .shstrtab STRTAB 000000000001a0 00005300001 [ 9] .symtab SYMTAB 000000000000e40000a0 101084 [10] .strtab STRTAB 0000000000018400001100001 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) grxer@Ubuntu16 /m/h/S/l/chapter4> readelf -S ab There are 8 section headers, starting at offset 0x2e4:
Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL0000000000000000000000000 [ 1] .text PROGBITS 0804809400009400007200 AX 001 [ 2] .eh_frame PROGBITS 0804810800010800006400 A 004 [ 3] .data PROGBITS 0804916c 00016c 00000400 WA 004 [ 4] .comment PROGBITS 0000000000017000003501 MS 001 [ 5] .shstrtab STRTAB 000000000002aa 00003a 00001 [ 6] .symtab SYMTAB 000000000001a8 0000d0 10774 [ 7] .strtab STRTAB 0000000000027800003200001 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific)
typedefstruct { Elf32_Word st_name; /* Symbol name (string tbl index) */ Elf32_Addr st_value; /* Symbol value */ Elf32_Word st_size; /* Symbol size */ unsignedchar st_info; /* Symbol type and binding */ unsignedchar st_other; /* Symbol visibility */ Elf32_Section st_shndx; /* Section index */ } Elf32_Sym;
grxer@Ubuntu16 /m/h/S/l/chapter4> readelf -s a.o b.o ab
File: a.o
Symbol table '.symtab' contains 11 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 00000000 0 FILE LOCAL DEFAULT ABS a.c 2: 00000000 0 SECTION LOCAL DEFAULT 1 3: 00000000 0 SECTION LOCAL DEFAULT 3 4: 00000000 0 SECTION LOCAL DEFAULT 4 5: 00000000 0 SECTION LOCAL DEFAULT 6 6: 00000000 0 SECTION LOCAL DEFAULT 7 7: 00000000 0 SECTION LOCAL DEFAULT 5 8: 00000000 57 FUNC GLOBAL DEFAULT 1 main 9: 00000000 0 NOTYPE GLOBAL DEFAULT UND shared 10: 00000000 0 NOTYPE GLOBAL DEFAULT UND swap
File: b.o
Symbol table '.symtab' contains 10 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 00000000 0 FILE LOCAL DEFAULT ABS b.c 2: 00000000 0 SECTION LOCAL DEFAULT 1 3: 00000000 0 SECTION LOCAL DEFAULT 2 4: 00000000 0 SECTION LOCAL DEFAULT 3 5: 00000000 0 SECTION LOCAL DEFAULT 5 6: 00000000 0 SECTION LOCAL DEFAULT 6 7: 00000000 0 SECTION LOCAL DEFAULT 4 8: 00000000 4 OBJECT GLOBAL DEFAULT 2 shared 9: 00000000 57 FUNC GLOBAL DEFAULT 1 swap
File: ab
Symbol table '.symtab' contains 13 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 08048094 0 SECTION LOCAL DEFAULT 1 2: 08048108 0 SECTION LOCAL DEFAULT 2 3: 0804916c 0 SECTION LOCAL DEFAULT 3 4: 00000000 0 SECTION LOCAL DEFAULT 4 5: 00000000 0 FILE LOCAL DEFAULT ABS a.c 6: 00000000 0 FILE LOCAL DEFAULT ABS b.c 7: 080480cd 57 FUNC GLOBAL DEFAULT 1 swap 8: 0804916c 4 OBJECT GLOBAL DEFAULT 3 shared 9: 08049170 0 NOTYPE GLOBAL DEFAULT 3 __bss_start 10: 08048094 57 FUNC GLOBAL DEFAULT 1 main 11: 08049170 0 NOTYPE GLOBAL DEFAULT 3 _edata 12: 08049170 0 NOTYPE GLOBAL DEFAULT 3 _end
为什么有些名字显示?
可以看的没显示的类型都为STT_SECTION他们的名字其实就是st_shndx代表的段的段名字
readelf没有显示,objdump给显示出来了
grxer@Ubuntu16 /m/h/S/l/chapter4> objdump -t a.o
a.o: file format elf32-i386
SYMBOL TABLE: 00000000 l df *ABS* 00000000 a.c 00000000 l d .text 00000000 .text 00000000 l d .data 00000000 .data 00000000 l d .bss 00000000 .bss 00000000 l d .note.GNU-stack00000000 .note.GNU-stack 00000000 l d .eh_frame 00000000 .eh_frame 00000000 l d .comment 00000000 .comment 00000000 g F .text 00000039 main 00000000 *UND* 00000000 shared 00000000 *UND* 00000000 swap
test.c #include<stdio.h> voidp1(void); int d = 100; int p= 200; intmain() { p1(); printf("%p\n",&d); printf("%x,%x",d,p); } p1.c #include<stdio.h> double d; voidp1(void) { printf("%p\n",&d); d =1.0; }
grxer@Ubuntu16 /m/h/S/NEMU> gcc -m32 test.c p1.c /usr/bin/ld: Warning: alignment 4 of symbol `d' in /tmp/ccEf1boS.o is smaller than 8 in /tmp/cc2ZKeXO.o grxer@Ubuntu16 /m/h/S/NEMU> ./a.out 0x804a01c 0x804a01c 0,3ff00000