10.5 Stack Allocation

The MPLAB XC32 C compiler dedicates general-purpose register 29 (PIC32M devices) or 13 (PIC32C/SAM devices) as the software Stack Pointer. All processor stack operations, including function calls, interrupts, and exceptions use the software stack. The stack grows downward from high addresses to low addresses.

By default, 32-bit linker dynamically allocates the largest stack possible from unused data memory. Previous releases used output sections specified in the linker script to allocate the stack.

The location and size of the stack is reported in the link map output file and the Memory-Usage Report, under the heading Dynamic Memory Usage. Applications can ensure that at least a minimum sized stack is available by specifying the size on the linker command line using the --defsym=_min_stack_size=size linker command line option. An example of allocating a stack of 2048 bytes using the command line is:

xc32-gcc foo.c -Wl,--defsym=_min_stack_size=2048.
The linker script a default _min_stack_size of 1024.
Note: See the C compiler user's guide relevant for your target device for more information on the compiler's usage of the stack.

The linker's reported size of the .stack section is the minimum size required to avoid a link error. The effective stack size is usually larger than the reported .stack section size.