2008. 7. 4. 11:10
Byte Stack Alignment

Applies to: Assembler, Compilers, Linker, ARM Developer Suite (ADS), RealView Developer Suite (RVDS) 2.0, RealView Developer Suite (RVDS) 2.1, RealView Developer Suite (RVDS) 2.2, RealView Development Suite (RVDS) 3.0, RealView Development Suite (RVDS) 3.1


What is 8 Byte Stack Alignment?

8 byte stack alignment is a requirement of the ARM Architecture Procedure Call Standard [AAPCS]. This specifies that functions must maintain an 8 byte aligned stack address (e.g. 0x00, 0x08, 0x10, 0x18, 0x20) on all external interfaces. In practice this requirement is met if:

At each external interface, the current stack pointer is a multiple of 8 bytes.
Your OS maintains 8 byte stack alignment on its external interfaces e.g. on task switches
8 byte stack alignment is of particular benefit to processors supporting LDRD and STRD instructions e.g. ARMv5TE. If the stack is not 8 byte aligned the use of LDRD and STRD may cause an alignment fault, depending on the target and configuration used.

8 Byte Stack Alignment in RVCT

In the RealView Compilation Tools (RVCT) v2.0 and above, all generated code and C library code will maintain 8 byte stack alignment on external interfaces. The generated code may not keep the stack 8 byte aligned internally, for example in leaf functions. However all generated code will keep the stack 8 byte aligned providing that it is already correctly aligned at the entry to the function. The compiler will not generate code to correct a mis-aligned stack.

Please be aware that 8 byte stack alignment is not new to RVDS. ADS does maintain 8 byte stack alignment, only the alignment of "double" and "long long" has changed between ADS and RVDS. In ADS "double" and "long long" data types were 4-byte aligned ("EBA4"), unless -Oldrd or __align were used. In RVCT 2.0 and later, double and long long data types are now 8-byte aligned ("EBA8"), unless --apcs /adsabi is used.

The compiler supports the option --apcs /adsabi to compile code that is compatible with the old ADS ABI. However, this is deprecated and will be removed in a future release.

The RVCT tools check 8 byte stack alignment using two build attributes REQUIRE8 and PRESERVE8, these can either be true or false. These are embedded into the object files and executables generated by the tools. REQUIRE8 is used to indicate that the code requires the stack pointer to be 8 byte aligned. PRESERVE8 is used to indicate that the code preserves 8 bytes stack alignment.

The assembler uses the REQUIRE8 and PRESERVE8 directives to indicate whether REQUIRE8 and PRESERVE8 build attributes should be true or false. If you omit PRESERVE8 the assembler will decide whether to set the PRES8 build attribute or not, by examining instructions that modify the SP. ARM recommends that you specify PRESERVE8 explicitly. If your assembly requires the stack to be 8 byte aligned, e.g. LDRD's using SP, you should specify the REQUIRE8 directive.

In RVCT 2.1 and later the assembler (armasm) can generate a warning if it detects a stack access that would leave the stack not 8 byte aligned. This can be enabled by adding --diag_warning 1546 to your assembler command line.

The REQUIRE8 and PRESERVE8 build attributes set by the compiler and assembler are used by the linker to prevent functions that require 8 byte alignment calling functions that do not preserve 8 byte alignment. If this occurs the linker will generate an error, for example:

Error: L6238E: foo.o(.text) contains invalid call from '~PRES8' function to 'REQ8' function

Please see the "Linker Error: L6238E: foo.o(.text) contains invalid call from '~PRES8' function to 'REQ8' function" FAQ for further details.
[출처] What is 8 Byte Stack Alignment?|작성자 두리번