您将如何解释此拆卸清单? [英] How would you explain this disassembly listing?
问题描述
我有一个简单的C语言函数,位于单独的文件字符串中.c:
I have a simple function in C language, in separate file string.c:
void var_init(){
char *hello = "Hello";
}
编译为:
gcc -ffreestanding -c string.c -o string.o
然后我使用命令
objdump -d string.o
查看反汇编清单.我得到的是:
to see disassemble listing. What I got is:
string.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <var_init>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 48 8d 05 00 00 00 00 lea 0x0(%rip),%rax # b <var_init+0xb>
b: 48 89 45 f8 mov %rax,-0x8(%rbp)
f: 90 nop
10: 5d pop %rbp
11: c3 retq
我迷失了对这个清单的了解.《从头开始写操作系统》一书讲述了一些关于旧的反汇编的内容,并稍微揭开了神秘的面纱,但是它们的清单完全不同,而且我什至没有看到数据像作者所说的那样被解释为我的代码.
I lost in understanding this listing. The book "Writing OS from scratch" says something about old disassembly and slightly uncover the mistery, but their listing is completely different and I even not see that data interpreted as code in mine as author says.
推荐答案
此命令
lea 0x0(%rip),%rax
将字符串文字的地址存储在寄存器 rax
中.
stores the address of the string literal in the register rax
.
这条命令
mov %rax,-0x8(%rbp)
将地址从寄存器 rax
复制到分配的堆栈存储器中.从堆栈 -0x8
中的偏移量可以看到,该地址占用8个字节.
copies the address from the register rax
into the allocated stack memory. The address occupies 8 bytes as it is seen from the offset in the stack -0x8
.
此存储仅发生是因为您在调试模式下进行了编译;通常可以将其优化掉.接下来发生的事情是本地变量(在在堆栈指针下方的红色区域)将被有效地丢弃,因为该函数会破坏其堆栈框架并返回.
This store only happens at all because you compiled in debug mode; it would normally be optimized away. The next thing that happens is that the local vars (in the red-zone below the stack pointer) are effectively discarded as the function tears down its stack frame and returns.
您正在查看的材料可能包括 sub $ 16,%rsp
或类似的东西,用于为低于RBP的本地人分配空间,然后稍后再分配该空间;x86-64 System V ABI在叶函数(不需要调用任何其他函数)中不需要它;他们可以只使用读取区域.(另请参见 x86-64上的红色区域到底在哪里?).或使用 gcc -mno-red-zone
进行编译,无论如何您可能都想要独立代码:
The material you're looking at probably included a sub $16, %rsp
or similar to allocate space for locals below RBP, then deallocating that space later; the x86-64 System V ABI doesn't need that in leaf functions (that don't call any other functions); they can just use the read zone. (See also Where exactly is the red zone on x86-64?). Or compile with gcc -mno-red-zone
, which you probably want anyway for freestanding code: Why can't kernel code use a Red Zone
然后,它恢复调用者RBP的保存值(之前已将其设置为帧指针;请注意,相对于RBP寻址了本地人的空间).
Then it restores the saved value of the caller's RBP (which was earlier set up as a frame pointer; notice that space for locals was addressed relative to RBP).
pop %rbp
并退出,有效地将返回地址弹出到RIP
and exits, effectively popping the return address into RIP
retq
这篇关于您将如何解释此拆卸清单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!