Linux gcc -mpreferred-stack-boundary 选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10251203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:55:00  来源:igfitidea点击:

gcc -mpreferred-stack-boundary option

linuxgcccompilationstack

提问by Naruto Uzumaki

I want to know what's the use of -mpreferred-stack-boundaryoption during compilation in GNU compiler. I've checked the documentation but the explanation is lost on me. Could someone please explain it.

我想知道-mpreferred-stack-boundary在 GNU 编译器中编译期间选项的用途是什么。我已经检查了文档,但解释对我来说丢失了。有人可以解释一下吗。

采纳答案by Employed Russian

I want to know what's the use of -mpreferred-stack-boundary option during compilation in GNU debugger.

我想知道在 GNU 调试器中编译期间 -mpreferred-stack-boundary 选项的用途是什么。

The option has absolutely nothingto do with the debugger.

该选项与调试器完全无关

It affects generated code in your binary. By default, GCC will arrange things so that every function, immediately upon entry, has its stack pointer aligned on 16-byte boundary (this may be important if you have local variables, and enable sse2instructions).

它会影响二进制文件中生成的代码。默认情况下,GCC 会安排一些事情,以便每个函数在进入时立即将其堆栈指针对齐在 16 字节边界上(如果您有局部变量和启用sse2指令,这可能很重要)。

If you change the default to e.g. -mpreferred-stack-boundary=2, then GCC will align stack pointer on 4-byte boundary. This will reduce stack requirements of your routines, but will crash if your code (or code you call) doesuse sse2, so is generally not safe.

如果您将默认值更改为 eg -mpreferred-stack-boundary=2,则 GCC 将在 4 字节边界上对齐堆栈指针。这将减少例程的堆栈要求,但如果您的代码(或您调用的代码)确实使用sse2,则会崩溃,因此通常不安全。

回答by flerb

It has to do with the byte boundaries that your program uses when it is layed out in memory.

它与您的程序在内存中布局时使用的字节边界有关。

What a stack boundary=2 does is ensure that the stack is set up into dword-size increments, this prevents your machine from optimizing the stack.

堆栈边界 = 2 的作用是确保堆栈设置为 dword 大小的增量,这可以防止您的机器优化堆栈。

If you check out:

如果您退房:

`info gcc and search by entering "/mpreferred-stack-boundary"` it says:
>-mpreferred-stack-boundary=num
> 

Attempt to keep the stack boundary aligned to a 2 raised to the num byte boundary. If -mpreferred-stack-boundary is not specified, the default is 4 (16 bytes or 128 bits).

尝试使堆栈边界与 2 对齐到 num 字节边界。如果未指定 -mpreferred-stack-boundary,则默认值为 4(16 字节或 128 位)。

A default stack boundary of 4 is the same for both Intel 386 and and AMD x86-64 machines.

对于 Intel 386 和 AMD x86-64 机器,默认堆栈边界 4 是相同的。

When I try to use the "m-preferred-stack-boundary=2" option on my 64-bit linux machine compilation fails with the error

当我尝试在 64 位 linux 机器上使用“m-preferred-stack-boundary=2”选项时,编译失败并显示错误

"-mpreffered-stack-boundary=2 is not between 4 and 12".

“-mpreffered-stack-boundary=2 不在 4 和 12 之间”。

This is because the width of the address field was increased from 4 bytes to 8 bytes in 64 bit machines. So it cannot write individual chunks at a stack-boundary=2 because 2^2=4 bytes. However, interestingly, a stack boundary of 3 still returns an error on both 32 and 64 bit machines, which would be an 8 byte boundary.

这是因为地址字段的宽度在 64 位机器中从 4 字节增加到 8 字节。所以它不能在 stack-boundary=2 写入单个块,因为 2^2=4 字节。然而,有趣的是,堆栈边界为 3 仍然会在 32 位和 64 位机器上返回错误,这将是 8 字节边界。

I can't include the link because I don't have 10 reputation...but a search turns stuff up pretty easily As far as I can tell it's a security feature because 8 bytes is prone to misaligning the stack...no doubt someone else knows better, or has more detail.

我不能包含链接,因为我没有 10 个声望……但是搜索很容易找到东西据我所知,这是一个安全功能,因为 8 个字节容易使堆栈错位……毫无疑问其他人知道得更好,或者有更多细节。

How the stack got misalignedSays:

堆栈如何未对齐说:

To ensure proper alignment of this values on the stack, the stack boundary must be as aligned as that required by any value stored on the stack. Further, every function must be generated such that it keeps the stack aligned. Thus calling a function compiled with a higher preferred stack boundary from a function compiled with a lower preferred stack boundary will most likely misalign the stack. It is recommended that libraries that use callbacks always use the default setting.

This extra alignment does consume extra stack space, and generally increases code size. Code that is sensitive to stack space usage, such as embedded systems and operating system kernels, may want to reduce the preferred alignment to -mpreferred-stack-boundary=2.

为了确保堆栈上这些值的正确对齐,堆栈边界必须与堆栈中存储的任何值所需的对齐。此外,必须生成每个函数以保持堆栈对齐。因此,从使用较低首选堆栈边界编译的函数调用使用较高首选堆栈边界编译的函数很可能会使堆栈错位。建议使用回调的库始终使用默认设置。

这种额外的对齐确实会消耗额外的堆栈空间,并且通常会增加代码大小。对堆栈空间使用敏感的代码,例如嵌入式系统和操作系统内核,可能希望将首选对齐减少到 -mpreferred-stack-boundary=2。

回答by Samir Ouldsaadi

right, it is useful to use -mpreferred-stack-boundary with 2 to easily disassemble what's going on, otherwise it will be optimized and hard to track what's going on in the stack. for 64 bit right you need 4 at least

对,使用 -mpreferred-stack-boundary 和 2 来轻松反汇编正在发生的事情是很有用的,否则它将被优化并且难以跟踪堆栈中发生的事情。对于 64 位权利,您至少需要 4 个