gcc -mpreferred-stack-boundary option gcc -mpreferred-stack-boundary option linux linux

gcc -mpreferred-stack-boundary option


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

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 use sse2, so is generally not safe.


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:

`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).

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

"-mpreffered-stack-boundary=2 is not between 4 and 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.

I can't include the link because I don't have 10 reputation...but a search turns stuff up pretty easilyAs 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 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.


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