GCC - 如何重新对齐堆栈? [英] GCC - How to realign stack?
问题描述
我尝试构建一个使用 pthreads 和 __m128 SSE 类型的应用程序.根据 GCC 手册,默认堆栈对齐是 16 个字节.为了使用__m128,要求是16字节对齐.
I try to build an application which uses pthreads and __m128 SSE type. According to GCC manual, default stack alignment is 16 bytes. In order to use __m128, the requirement is the 16-byte alignment.
我的目标 CPU 支持 SSE.我使用不支持运行时堆栈重新对齐的 GCC 编译器(例如 -mstackrealign).我不能使用任何其他 GCC 编译器版本.
My target CPU supports SSE. I use a GCC compiler which doesn't support runtime stack realignment (e.g. -mstackrealign). I cannot use any other GCC compiler version.
我的测试应用程序如下所示:
My test application looks like:
#include <xmmintrin.h>
#include <pthread.h>
void *f(void *x){
__m128 y;
...
}
int main(void){
pthread_t p;
pthread_create(&p, NULL, f, NULL);
}
应用程序产生异常并退出.经过简单的调试(printf "%p", &y),发现变量y不是16字节对齐的.
The application generates an exception and exits. After a simple debugging (printf "%p", &y), I found that the variable y is not 16-byte aligned.
我的问题是:如何在不使用任何 GCC 标志和属性(它们没有帮助)的情况下正确地重新对齐堆栈(16 字节)?我应该在这个线程函数 f() 中使用 GCC 内联汇编器吗?
My question is: how can I realign the stack properly (16-byte) without using any GCC flags and attributes (they don't help)? Should I use GCC inline Assembler within this thread function f()?
推荐答案
我已经解决了这个问题.这是我的解决方案:
I have solved this problem. Here is my solution:
void another_function(){
__m128 y;
...
}
void *f(void *x){
asm("pushl %esp");
asm("subl $16,%esp");
asm("andl $-0x10,%esp");
another_function();
asm("popl %esp");
}
首先,我们将堆栈增加 16 个字节.其次,我们使最不重要的半字节等于 0x0.我们使用 push/pop 操作数保存堆栈指针.我们调用另一个函数,它有自己的所有局部变量 16 字节对齐.所有嵌套函数的局部变量也将 16 字节对齐.
First, we increase the stack by 16 bytes. Second, we make least-significant nibble equal 0x0. We preserve the stack pointer using push/pop operands. We call another function, which has all its own local variables 16-byte aligned. All nested functions will also have their local variables 16-byte aligned.
而且它有效!
这篇关于GCC - 如何重新对齐堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!