void return_input (void) { char array[30]; gets (array); printf("%s\n", array); }
在gcc中编译后,该函数转换为如下汇编代码:
push %ebp mov %esp,%ebp sub $0x28,%esp mov %gs:0x14,%eax mov %eax,-0x4(%ebp) xor %eax,%eax lea -0x22(%ebp),%eax mov %eax,(%esp) call 0x8048374 lea -0x22(%ebp),%eax mov %eax,(%esp) call 0x80483a4 mov -0x4(%ebp),%eax xor %gs:0x14,%eax je 0x80484ac call 0x8048394 leave ret
我不明白两行:
mov %gs:0x14,%eax xor %gs:0x14,%eax
什么是 %gs,这两行到底做了什么?
这是编译命令:
cc -c -mpreferred-stack-boundary=2 -ggdb file.c
最佳答案
GS 是一个段寄存器,它在 linux 中的使用可以在 here 上阅读。 (它基本上用于每个线程数据)。
mov %gs:0x14,%eax
xor %gs:0x14,%eax
此代码用于验证堆栈没有爆炸或损坏,使用存储在 GS+0x14 的金丝雀值,参见 this .
gcc -fstack-protector=strong
在许多现代发行版中默认启用;您可以使用 gcc -fno-stack-protector
不添加这些检查。 (在 x86 上,线程本地存储很便宜,因此 GCC 将随机金丝雀值保存在那里,使其更难泄漏。)
关于linux - 汇编中的 %gs 是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9249315/