linux - 汇编中的 %gs 是什么

标签 linux assembly x86 thread-local-storage memory-segmentation

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/

相关文章:

linux - flock 和 NFS——意外关机时会发生什么?

linux - 建立到服务器的远程 ssh 连接后,vscode-server 似乎没有完成安装

c - 将数组作为参数从 C 传递给 x86 函数

assembly - 如何在 x86 汇编中划分 float ?

python - 调用 bash 脚本作为 Python 子进程 - Bash 陷入无限循环,输入错误

c - read() 系统调用实际读取的数据量

c++ - 如何在 Microsoft Visual Studio C/C++ 编译器中关闭位置无关代码编译?

assembly - 为什么对非核心响应/非核心请求的需求过多?

assembly - x86组件集 'push'和 'pusha'的区别

assembly - 奥利数据库 : sign extending with Movsx