gpt4 book ai didi

assembly - GCC 访问栈顶上方的内存

转载 作者:行者123 更新时间:2023-12-04 14:29:36 30 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





rsp doesn't move when entering new function [duplicate]

(1 个回答)


6 个月前关闭。




我有执行一些 SSE 计算的 C 函数。当我用 GCC 编译它时,我得到下一个代码

/* Start of function */
mov (%rdi),%rax
movslq %ecx,%rcx
...
mov 0x8(%rdi),%rax
pxor %xmm12,%xmm3
movaps %xmm0,-0x28(%rsp)
movaps %xmm6,%xmm1
...
movaps 0x50(%rax,%rcx,1),%xmm2
movaps 0x60(%rax,%rcx,1),%xmm15
pxor %xmm2,%xmm0
pxor %xmm2,%xmm6
movaps -0x28(%rsp),%xmm2
pxor %xmm15,%xmm5
pxor %xmm15,%xmm2
movaps 0x70(%rax,%rcx,1),%xmm15
movaps (%rax,%rcx,1),%xmm11
mov 0x10(%rdi),%rax
movaps %xmm15,-0x18(%rsp)
pxor %xmm11,%xmm4
pxor %xmm12,%xmm11
pxor %xmm15,%xmm12

movaps指令 - 它通过堆栈顶部访问内存:
movaps %xmm15,-0x18(%rsp)

不是访问未定义的内存吗?为什么 GCC 生成了如此错误的代码?

最佳答案

在程序集级别没有“未定义的内存”这样的东西。只要行为符合预期,gcc 可以自由地发出以它认为合适的任何方式访问堆栈的代码。

我对为什么会发生这种情况的猜测是,这是一个叶子函数,调整堆栈指针是徒劳的。您可以尝试通过检查任何 call 的程序集来验证这一点。指示。 (您也可以检查 C 源代码,但内联可能会使它不太可靠。)

某些平台(包括 x86-64)的 ABI 明确允许这种技巧。来自 the AMD64 ABI documentation :

The 128-byte area beyond the location pointed to by %rsp is considered to be reserved and shall not be modified by signal or interrupt handlers. Therefore, functions may use this area for temporary data that is not needed across function calls. In particular, leaf functions may use this area for their entire stack frame, rather than adjusting the stack pointer in the prologue and epilogue. This area is known as the red zone.



This blog post可能会使有关该主题的阅读变得有趣。

关于assembly - GCC 访问栈顶上方的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20661190/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com