Not sure why there are two threads about this. I wrote something about "good code" and workaround in the other thread. I think I did not explain clearly.
The proper way to write the code is apparently:
Code: Select all
__attribute__((used))
void *retaddr;
__attribute__((noinline))
void xxxxtest(void) {
retaddr = __builtin_return_address(0);
/* Used for enforcing registers stacking.*/
asm volatile("" : : : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
"r8", "r9", "r10", "r11", "r12");
}
The correctly compiled code would be something like:
Code: Select all
xxxxtest:
push {r4, r5, r6, r7, lr}
mov r4, fp
mov r7, r10
mov r6, r9
mov r5, r8
mov r2, lr
ldr r3, .L3
push {r4, r5, r6, r7}
str r2, [r3]
pop {r2, r3, r4, r5}
mov r8, r2
mov r9, r3
mov r10, r4
mov fp, r5
pop {r4, r5, r6, r7, pc}
(I probably removed too much of the non-code to make this work as-is).
I'm using r4 where the bad code is using "lr". r4 has already been pushed, but I'm not sure if I'm changing the order of stuff-on-the-stack this way so you will certainly have to double check my code.
Then as a workaround I propose:
Code: Select all
#ifdef COMPILER_BUG_PRESENT
#warning "Compiler bug present. Activating ugly workaround!"
asm ("xxxxtest:
push {r4, r5, r6, r7, lr}
mov r4, fp
mov r7, r10
mov r6, r9
mov r5, r8
mov r2, lr
ldr r3, .L3
push {r4, r5, r6, r7}
str r2, [r3]
pop {r2, r3, r4, r5}
mov r8, r2
mov r9, r3
mov r10, r4
mov fp, r5
pop {r4, r5, r6, r7, pc}");
#else
// On gcc versions 6 and up some optimization mis-compiles the following code. Above is the
// hand-corrected version.
__attribute__((used))
void *retaddr;
__attribute__((noinline))
void xxxxtest(void) {
retaddr = __builtin_return_address(0);
/* Used for enforcing registers stacking.*/
asm volatile("" : : : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
"r8", "r9", "r10", "r11", "r12");
#endif
The reason I find this workaround important is that unless such a fix goes in, many people (like me) are restricted to sticking with a really old version of the compiler.