RVOS练习3-1

编写一个简单的打印”hello world!”的程序文件:hello.c

1
2
3
4
5
6
#include <stdio.h>
void main()
{
printf("hello world!\n");
return;
}

对源文件进行本地编译,生成针对支持 x86_64 指令集架构处理器的目标文件 hello.o

1
gcc -g -o hello.c

查看hello.o的文件的文件头信息

hello.o的文件头信息可使用如下指令查看:

1
readelf -h hello.o

输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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: 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: 2080 (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: 23
Section header string table index: 22

对 hello.o 反汇编,并查看 hello.c 的 C 程序源码和机器指令的对应关系

使用以下指令对hello.o进行反汇编:

1
objdump -S hello.o

输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

hello.o: file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
#include <stdio.h>
void main()
{
0: f3 0f 1e fa endbr64
4: 55 push %rbp
5: 48 89 e5 mov %rsp,%rbp
printf("hello world!\n");
8: 48 8d 05 00 00 00 00 lea 0x0(%rip),%rax # f <main+0xf>
f: 48 89 c7 mov %rax,%rdi
12: e8 00 00 00 00 call 17 <main+0x17>
return;
17: 90 nop
}
18: 5d pop %rbp
19: c3 ret