I want to know what's the use of -mpreferred-stack-boundary
option during compilation in GNU compiler. I've checked the documentation but the explanation is lost on me. Could someone please explain it.
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Angular: ngc or tsc?
- Error building gcc 4.8.3 from source: libstdc++.so
- Where is the implementation of included C++/C head
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
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.
If you check out:
A default stack boundary of 4 is the same for both Intel 386 and and AMD x86-64 machines.
When I try to use the "m-preferred-stack-boundary=2" option on my 64-bit linux machine compilation fails with the error
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.
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.
How the stack got misaligned Says:
The option has absolutely nothing to 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
sse2
instructions).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) does usesse2
, so is generally not safe.