We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

AR# 16549

6.1 / 3.2 EDK - How do I determine my stack size?


General Description:

My program is not working, and we have determined that we are overwriting our stack. What do I set my stack size to?


There is no definitive answer on what your stack size should be. Each program is different and will have different stack usage from program to program.

Determining the stack size needed will be figured out using a debugger at runtime. You can see in the debugger where your stack overflowed and adjust the size as necessary.

To set the stack size:

By default, the EDK tools build the executable with a default stack size of 0x100 (256) bytes. The stack size can be set at compile time by using:

mb-gcc file1.c file2.c -Wl,defsym -Wl,_STACK_SIZE=0x400

This will set the stack size to 0x400 (1024) bytes.

For MicroBlaze mb-gcc compiler, there is a switch called "-mxl-stack-check". This option lets users check if the stack overflows during the execution of the program.

The compiler inserts code in the prologue of every function, comparing the stack pointer value with the available memory. If the stack pointer exceeds the available free memory, the program jumps to the subroutine "_stack_overflow_exit". This subroutine sets the value of the variable "_stack_overflow_error" to 1.

The standard stack overflow handler can be overridden by providing the function "_stack_overflow_exit" in the source code, which acts as the stack overflow handler.

Setting the "-mxl-stack-check" will allow you to determine where the stack overflowed and to increase the size

as needed.

As a rule of thumb, after you estimate your stack size at runtime, increase the stack by 2.
AR# 16549
Date Created 09/03/2007
Last Updated 09/21/2010
Status Archive
Type General Article