riscv-operating-system homework

3-1

使? gcc 编译代码并使? binutils ?具对?成的?标文件和可执?文件(ELF 格式)进?分析。具体要求如下:
  • 编写?个简单的打印 “hello world!” 的程序源文件:hello.c
  • 对源文件进?本地编译,?成针对?持 x86_64 指令集架构处理器的?标文件 hello.o。
  • 查看 hello.o 的文件的文件头信息。
  • 查看 hello.o 的 Section header table。
  • 对 hello.o 反汇编,并查看 hello.c 的 C 程序源码和机器指令的对应关系。
【riscv-operating-system homework】简单地打印 "hello world !"
#include int main(){ printf("hello world !\n"); return 0; }

编译为目标文件
gcc hello.cc -o hello.o

查看文件头信息
$ readelf -h hello.o ELF Header: Magic:7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class:ELF64 Data:2's complement, little endian Version:1 (current) OS/ABI:UNIX - System V ABI Version:0 Type:DYN (Shared object file) Machine:Advanced Micro Devices X86-64 Version:0x1 Entry point address:0x1060 Start of program headers:64 (bytes into file) Start of section headers:14712 (bytes into file) Flags:0x0 Size of this header:64 (bytes) Size of program headers:56 (bytes) Number of program headers:13 Size of section headers:64 (bytes) Number of section headers:31 Section header string table index: 30

查看 Section header table
$ readelf -l hello.oElf file type is DYN (Shared object file) Entry point 0x1060 There are 13 program headers, starting at offset 64Program Headers: TypeOffsetVirtAddrPhysAddr FileSizMemSizFlagsAlign PHDR0x0000000000000040 0x0000000000000040 0x0000000000000040 0x00000000000002d8 0x00000000000002d8R0x8 INTERP0x0000000000000318 0x0000000000000318 0x0000000000000318 0x000000000000001c 0x000000000000001cR0x1 [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2] LOAD0x0000000000000000 0x0000000000000000 0x0000000000000000 0x00000000000005f8 0x00000000000005f8R0x1000 LOAD0x0000000000001000 0x0000000000001000 0x0000000000001000 0x00000000000001f5 0x00000000000001f5R E0x1000 LOAD0x0000000000002000 0x0000000000002000 0x0000000000002000 0x0000000000000160 0x0000000000000160R0x1000 LOAD0x0000000000002db8 0x0000000000003db8 0x0000000000003db8 0x0000000000000258 0x0000000000000260RW0x1000 DYNAMIC0x0000000000002dc8 0x0000000000003dc8 0x0000000000003dc8 0x00000000000001f0 0x00000000000001f0RW0x8 NOTE0x0000000000000338 0x0000000000000338 0x0000000000000338 0x0000000000000020 0x0000000000000020R0x8 NOTE0x0000000000000358 0x0000000000000358 0x0000000000000358 0x0000000000000044 0x0000000000000044R0x4 GNU_PROPERTY0x0000000000000338 0x0000000000000338 0x0000000000000338 0x0000000000000020 0x0000000000000020R0x8 GNU_EH_FRAME0x0000000000002014 0x0000000000002014 0x0000000000002014 0x0000000000000044 0x0000000000000044R0x4 GNU_STACK0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000RW0x10 GNU_RELRO0x0000000000002db8 0x0000000000003db8 0x0000000000003db8 0x0000000000000248 0x0000000000000248R0x1 Section to Segment mapping: Segment Sections... 00 01.interp 02.interp .note.gnu.property .note.gnu.build-id .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt 03.init .plt .plt.got .plt.sec .text .fini 04.rodata .eh_frame_hdr .eh_frame 05.init_array .fini_array .dynamic .got .data .bss 06.dynamic 07.note.gnu.property 08.note.gnu.build-id .note.ABI-tag 09.note.gnu.property 10.eh_frame_hdr 11 12.init_array .fini_array .dynamic .got

反汇编
$ objdump -d hello.o > 3-1.asm 3-1.o:file format elf64-x86-64Disassembly of section .init:0000000000001000 <_init>: 1000:f3 0f 1e faendbr64 1004:48 83 ec 08sub$0x8,%rsp 1008:48 8b 05 d9 2f 00 00mov0x2fd9(%rip),%rax# 3fe8 <__gmon_start__> 100f:48 85 c0test%rax,%rax 1012:74 02je1016 <_init+0x16> 1014:ff d0callq*%rax 1016:48 83 c4 08add$0x8,%rsp 101a:c3retqDisassembly of section .plt:0000000000001020 <.plt>: 1020:ff 35 9a 2f 00 00pushq0x2f9a(%rip)# 3fc0 <_GLOBAL_OFFSET_TABLE_+0x8> 1026:f2 ff 25 9b 2f 00 00bnd jmpq *0x2f9b(%rip)# 3fc8 <_GLOBAL_OFFSET_TABLE_+0x10> 102d:0f 1f 00nopl(%rax) 1030:f3 0f 1e faendbr64 1034:68 00 00 00 00pushq$0x0 1039:f2 e9 e1 ff ff ffbnd jmpq 1020 <.plt> 103f:90nopDisassembly of section .plt.got:0000000000001040 <__cxa_finalize@plt>: 1040:f3 0f 1e faendbr64 1044:f2 ff 25 ad 2f 00 00bnd jmpq *0x2fad(%rip)# 3ff8 <__cxa_finalize@GLIBC_2.2.5> 104b:0f 1f 44 00 00nopl0x0(%rax,%rax,1)Disassembly of section .plt.sec:0000000000001050 : 1050:f3 0f 1e faendbr64 1054:f2 ff 25 75 2f 00 00bnd jmpq *0x2f75(%rip)# 3fd0 105b:0f 1f 44 00 00nopl0x0(%rax,%rax,1)Disassembly of section .text:0000000000001060 <_start>: 1060:f3 0f 1e faendbr64 1064:31 edxor%ebp,%ebp 1066:49 89 d1mov%rdx,%r9 1069:5epop%rsi 106a:48 89 e2mov%rsp,%rdx 106d:48 83 e4 f0and$0xfffffffffffffff0,%rsp 1071:50push%rax 1072:54push%rsp 1073:4c 8d 05 66 01 00 00lea0x166(%rip),%r8# 11e0 <__libc_csu_fini> 107a:48 8d 0d ef 00 00 00lea0xef(%rip),%rcx# 1170 <__libc_csu_init> 1081:48 8d 3d c1 00 00 00lea0xc1(%rip),%rdi# 1149
1088:ff 15 52 2f 00 00callq*0x2f52(%rip)# 3fe0 <__libc_start_main@GLIBC_2.2.5> 108e:f4hlt 108f:90nop0000000000001090 : 1090:48 8d 3d 79 2f 00 00lea0x2f79(%rip),%rdi# 4010 <__TMC_END__> 1097:48 8d 05 72 2f 00 00lea0x2f72(%rip),%rax# 4010 <__TMC_END__> 109e:48 39 f8cmp%rdi,%rax 10a1:74 15je10b8 10a3:48 8b 05 2e 2f 00 00mov0x2f2e(%rip),%rax# 3fd8 <_ITM_deregisterTMCloneTable> 10aa:48 85 c0test%rax,%rax 10ad:74 09je10b8 10af:ff e0jmpq*%rax 10b1:0f 1f 80 00 00 00 00nopl0x0(%rax) 10b8:c3retq 10b9:0f 1f 80 00 00 00 00nopl0x0(%rax)00000000000010c0 : 10c0:48 8d 3d 49 2f 00 00lea0x2f49(%rip),%rdi# 4010 <__TMC_END__> 10c7:48 8d 35 42 2f 00 00lea0x2f42(%rip),%rsi# 4010 <__TMC_END__> 10ce:48 29 fesub%rdi,%rsi 10d1:48 89 f0mov%rsi,%rax 10d4:48 c1 ee 3fshr$0x3f,%rsi 10d8:48 c1 f8 03sar$0x3,%rax 10dc:48 01 c6add%rax,%rsi 10df:48 d1 fesar%rsi 10e2:74 14je10f8 10e4:48 8b 05 05 2f 00 00mov0x2f05(%rip),%rax# 3ff0 <_ITM_registerTMCloneTable> 10eb:48 85 c0test%rax,%rax 10ee:74 08je10f8 10f0:ff e0jmpq*%rax 10f2:66 0f 1f 44 00 00nopw0x0(%rax,%rax,1) 10f8:c3retq 10f9:0f 1f 80 00 00 00 00nopl0x0(%rax)0000000000001100 <__do_global_dtors_aux>: 1100:f3 0f 1e faendbr64 1104:80 3d 05 2f 00 00 00cmpb$0x0,0x2f05(%rip)# 4010 <__TMC_END__> 110b:75 2bjne1138 <__do_global_dtors_aux+0x38> 110d:55push%rbp 110e:48 83 3d e2 2e 00 00cmpq$0x0,0x2ee2(%rip)# 3ff8 <__cxa_finalize@GLIBC_2.2.5> 1115:00 1116:48 89 e5mov%rsp,%rbp 1119:74 0cje1127 <__do_global_dtors_aux+0x27> 111b:48 8b 3d e6 2e 00 00mov0x2ee6(%rip),%rdi# 4008 <__dso_handle> 1122:e8 19 ff ff ffcallq1040 <__cxa_finalize@plt> 1127:e8 64 ff ff ffcallq1090 112c:c6 05 dd 2e 00 00 01movb$0x1,0x2edd(%rip)# 4010 <__TMC_END__> 1133:5dpop%rbp 1134:c3retq 1135:0f 1f 00nopl(%rax) 1138:c3retq 1139:0f 1f 80 00 00 00 00nopl0x0(%rax)0000000000001140 : 1140:f3 0f 1e faendbr64 1144:e9 77 ff ff ffjmpq10c0 0000000000001149
: 1149:f3 0f 1e faendbr64 114d:55push%rbp 114e:48 89 e5mov%rsp,%rbp 1151:48 8d 3d ac 0e 00 00lea0xeac(%rip),%rdi# 2004 <_IO_stdin_used+0x4> 1158:e8 f3 fe ff ffcallq1050 115d:b8 00 00 00 00mov$0x0,%eax 1162:5dpop%rbp 1163:c3retq 1164:66 2e 0f 1f 84 00 00nopw%cs:0x0(%rax,%rax,1) 116b:00 00 00 116e:66 90xchg%ax,%ax0000000000001170 <__libc_csu_init>: 1170:f3 0f 1e faendbr64 1174:41 57push%r15 1176:4c 8d 3d 3b 2c 00 00lea0x2c3b(%rip),%r15# 3db8 <__frame_dummy_init_array_entry> 117d:41 56push%r14 117f:49 89 d6mov%rdx,%r14 1182:41 55push%r13 1184:49 89 f5mov%rsi,%r13 1187:41 54push%r12 1189:41 89 fcmov%edi,%r12d 118c:55push%rbp 118d:48 8d 2d 2c 2c 00 00lea0x2c2c(%rip),%rbp# 3dc0 <__do_global_dtors_aux_fini_array_entry> 1194:53push%rbx 1195:4c 29 fdsub%r15,%rbp 1198:48 83 ec 08sub$0x8,%rsp 119c:e8 5f fe ff ffcallq1000 <_init> 11a1:48 c1 fd 03sar$0x3,%rbp 11a5:74 1fje11c6 <__libc_csu_init+0x56> 11a7:31 dbxor%ebx,%ebx 11a9:0f 1f 80 00 00 00 00nopl0x0(%rax) 11b0:4c 89 f2mov%r14,%rdx 11b3:4c 89 eemov%r13,%rsi 11b6:44 89 e7mov%r12d,%edi 11b9:41 ff 14 dfcallq*(%r15,%rbx,8) 11bd:48 83 c3 01add$0x1,%rbx 11c1:48 39 ddcmp%rbx,%rbp 11c4:75 eajne11b0 <__libc_csu_init+0x40> 11c6:48 83 c4 08add$0x8,%rsp 11ca:5bpop%rbx 11cb:5dpop%rbp 11cc:41 5cpop%r12 11ce:41 5dpop%r13 11d0:41 5epop%r14 11d2:41 5fpop%r15 11d4:c3retq 11d5:66 66 2e 0f 1f 84 00data16 nopw %cs:0x0(%rax,%rax,1) 11dc:00 00 00 00 00000000000011e0 <__libc_csu_fini>: 11e0:f3 0f 1e faendbr64 11e4:c3retqDisassembly of section .fini:00000000000011e8 <_fini>: 11e8:f3 0f 1e faendbr64 11ec:48 83 ec 08sub$0x8,%rsp 11f0:48 83 c4 08add$0x8,%rsp 11f4:c3retq

3-2
如下例? C 语?代码:
#include int global_init = 0x11111111; const int global_const = 0x22222222; void main() { static int static_var = 0x33333333; static int static_var_uninit; int auto_var = 0x44444444; printf("hello world!\n"); return; }

请问编译为 .o 文件后,global_init, global_const, static_var, static_var_uninit, auto_var 这些变 量分别存放在那些 section ?,"hello world!\n" 这个字符串?在哪??并尝试??具查看并验证你的猜测。
  • global_initstatic_var.data
Disassembly of section .data:0000000000004000 <__data_start>: ...0000000000004008 <__dso_handle>: 4008:08 40 00or%al,0x0(%rax) 400b:00 00add%al,(%rax) 400d:00 00add%al,(%rax) ...0000000000004010 : 4010:11 11adc%edx,(%rcx) 4012:11 11adc%edx,(%rcx)0000000000004014 : 4014:33 33xor(%rbx),%esi 4016:33 33xor(%rbx),%esi

  • global_constrodata
Disassembly of section .rodata:0000000000002000 <_IO_stdin_used>: 2000:01 00add%eax,(%rax) 2002:02 00add(%rax),%al0000000000002004 : 2004:22 22and(%rdx),%ah 2006:22 22and(%rdx),%ah 2008:68 65 6c 6c 6fpushq$0x6f6c6c65 200d:20 77 6fand%dh,0x6f(%rdi) 2010:72 6cjb207e <__GNU_EH_FRAME_HDR+0x66> 2012:64 21 00and%eax,%fs:(%rax)

  • static_var_uninitbss
Disassembly of section .bss:0000000000004018 : 4018:00 00add%al,(%rax) ...000000000000401c : 401c:00 00add%al,(%rax) ...

  • auto_var 为局部变量,不在任何 section 中,在代码段中直接使用
4-1
针对 rv32ima 指令集架构,重复 3-1 的操作
指令用法与 3-1 基本相同
$riscv64-unknown-elf-gcc 3-1.c -march=rv32ima -mabi=ilp32 -g -Wall -o 4-1.o $readelf -h 4-1.o ELF Header: Magic:7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class:ELF32 Data:2's complement, little endian Version:1 (current) OS/ABI:UNIX - System V ABI Version:0 Type:EXEC (Executable file) Machine:RISC-V Version:0x1 Entry point address:0x10090 Start of program headers:52 (bytes into file) Start of section headers:26136 (bytes into file) Flags:0x0 Size of this header:52 (bytes) Size of program headers:32 (bytes) Number of program headers:2 Size of section headers:40 (bytes) Number of section headers:21 Section header string table index: 20 $readelf -l 4-1.o Elf file type is EXEC (Executable file) Entry point 0x10090 There are 2 program headers, starting at offset 52Program Headers: TypeOffsetVirtAddrPhysAddrFileSiz MemSizFlg Align LOAD0x000000 0x00010000 0x00010000 0x0362c 0x0362c R E 0x1000 LOAD0x00362c 0x0001462c 0x0001462c 0x00858 0x008b0 RW0x1000 Section to Segment mapping: Segment Sections... 00.text .rodata 01.eh_frame .init_array .fini_array .data .sdata .sbss .bss $riscv64-unknown-elf-objdump -d 4-1.o > 4-1.asm

  • 反汇编节选
4-1.o:file format elf32-littleriscvDisassembly of section .text:00010074 : 10074:ffff0797auipca5,0xffff0 10078:f8c78793addia5,a5,-116 # 0 1007c:00078863beqza5,1008c 10080:00000517auipca0,0x0 10084:14050513addia0,a0,320 # 101c0 <__libc_fini_array> 10088:0f00006fj10178 1008c:00008067ret00010090 <_start>: 10090:00005197auipcgp,0x5 10094:db018193addigp,gp,-592 # 14e40 <__global_pointer$> 10098:04418513addia0,gp,68 # 14e84 <_edata> 1009c:09c18613addia2,gp,156 # 14edc <__BSS_END__> 100a0:40a60633suba2,a2,a0 100a4:00000593lia1,0 100a8:20c000efjalra,102b4 100ac:00000517auipca0,0x0 100b0:11450513addia0,a0,276 # 101c0 <__libc_fini_array> 100b4:0c4000efjalra,10178 100b8:168000efjalra,10220 <__libc_init_array> 100bc:00012503lwa0,0(sp) 100c0:00410593addia1,sp,4 100c4:00000613lia2,0 100c8:07c000efjalra,10144
100cc:0c00006fj1018c ... ... 00010144
: 10144:ff010113addisp,sp,-16 10148:00112623swra,12(sp) 1014c:00812423sws0,8(sp) 10150:01010413addis0,sp,16 10154:000137b7luia5,0x13 10158:61878513addia0,a5,1560 # 13618 <__modsi3+0x30> 1015c:304000efjalra,10460 10160:00000793lia5,0 10164:00078513mva0,a5 10168:00c12083lwra,12(sp) 1016c:00812403lws0,8(sp) 10170:01010113addisp,sp,16 10174:00008067ret ... ...

qemu-riscv32 模拟运行
$ qemu-riscv32 4-1.o hello world !

    推荐阅读