[En-Nut-Discussion] ARM Alignment for Threads

Bob Wirka bobwirka at yahoo.com
Wed Oct 9 16:40:54 CEST 2013


It seems that setting the alignment to 8 in the load script is not the solution to getting 8 byte stack alignment. Thread stacks can end up on a 4 byte boundary, thus screwing up va_arg().

When a thread is started, it's stack is set via NutHeapAlloc(). There are two problems: 1) NUTMEM_ALIGNMENT defaults to 4, and 2) the pointer returned is set to the address of HEAPNODE->hn_next, which is at a 4 byte offset from the address of the heap node allocated.

So, even if you set NUTMEM_ALIGNMENT to 8, you still can get a stack pointer on a 4 byte boundary.


My ugly hack was to set NUTMEM_ALIGNMENT to 8, and then to add 4 bytes of padding in HEAPNODE between 'size' and 'next'. The result is that threads get a stack that's aligned to 8 bytes, and va_arg() seems to work.

A more elegant solution should be found, but I'm under the gun right now...

Best,

Bob Wirka
Realtime Control Works


More information about the En-Nut-Discussion mailing list