User guide

2.5 Stack limit checking
Select the software stack limit checking (/swst) option unless the maximum amount of stack memory required by
your complete program can be accurately calculated at the design stage.
Select the no software stack limit checking (/noswst) option only if you can accurately calculate, at the design
stage, the maximum amount of stack memory that your complete program requires. This is the default.
It is possible to write assembly code in such a way that stack limit checking is irrelevant. The code in a file might not
require stack limit checking, but be compatible with other code assembled either /swst or /noswst. Use the
software stack limit checking not applicable (/swstna) option in this case.
2.5.1 Rules for stack limit checked code
In the stack limit checked variants of the ATPCS:
sl must point at least 256 bytes above the lowest usable address in the stack.
Note
If an interrupt handler can use the User mode stack, you must allow sufficient space for it, between sl and the
lowest usable address in the stack, in addition to the 256 bytes.
sl must not be altered by code compiled or assembled with stack limit checking selected. (sl is altered by
run-time support code).
The value held in sp must always be greater than or equal to the value in sl.
2.5.2 Register usage with stack limit checking
You must not alter r10, or restore it, in routines assembled or compiled with the stack checking option selected.
Register r10 is the stack limit pointer, sl.
In all other respects the usage of registers is the same with or without stack limit checking (see Register roles and
names).
2.5.3 Stack checking in C and C++
If you select the software stack limit checking (/swst) option, the compilers generate object code that performs
stack checking.
2.5.4 Stack checking in assembly language
If you select the software stack checking (/swst) option for your assembly code, it is your responsibility to write code
that performs stack checking.
A leaf routine is a routine that does not call any other subroutine.
There are three cases to consider:
Leaf routine using less than 256 bytes of stack
Nonleaf routine using less than 256 bytes of stack
Routine using more than 256 bytes of stack.
For this purpose, leaf routines include routines in which every call is a tail call.
Leaf routine using less than 256 bytes of stack
A leaf routine that uses less than 256 bytes of stack does not need to check the stack limit. This is a consequence of
the rules above (see Rules for stack limit checked code).
For this purpose, a leaf routine can be a combination of routines with a total stack usage less than 256 bytes.
Nonleaf routine using less than 256 bytes of stack
A nonleaf routine that uses less than 256 bytes of stack can use a limit-checking sequence such as the following:
SUB sp, sp, #size ; ARM code version
CMP sp, sl
BLLO __ARM_stack_overflow
or in Thumb code:
ADD sp, #-size ; Thumb code version
CMP sp, sl
BLLO __Thumb_stack_overflow
Using the Procedure Call Standard
Copyright ?1999 2001 ARM Limited 2-7