https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77882
--- Comment #9 from Elad Lahav <e2lahav at gmail dot com> ---
While trying to write a simple test to demonstrate that the suggested patch
works I ran into a couple of issues. First, here is the test code:
#include <stdio.h>
#include <stdlib.h>
#if defined(__aarch64__)
static long __attribute__((naked))
add_one(long a)
{
__asm__ __volatile__("add x0, x0, #1; ret");
}
#elif defined(__arm__)
static long __attribute__((naked))
add_one(long a)
{
__asm__ __volatile__("add r0, r0, #1; bx lr");
}
#endif
int
main(int argc, char **argv)
{
long a = 0;
if (argc > 1) {
a = strtol(argv[1], NULL, 0);
}
a = add_one(a);
printf("a=%ld\n", a);
return 0;
}
1. GCC emits a warning:
/home/elahav/src/projects/aarch64_naked/aarch64_naked.c:15:1: warning: no
return statement in function returning non-void [-Wreturn-type]
}
This is true for ARMv7 as well. I would not expect such a warning in a naked
function.
2. With -O0 GCC puts an instruction before 'add':
0000000000000000 <add_one>:
0: f90007e0 str x0, [sp, #8]
4: 91000400 add x0, x0, #0x1
8: d65f03c0 ret
c: d503201f nop
The ARMv7 version is better, though still has extra baggage:
00000000 <add_one>:
0: f100 0001 add.w r0, r0, #1
4: 4770 bx lr
6: bf00 nop
8: 4618 mov r0, r3
With -O2 I get the expected results:
0000000000000000 <add_one>:
0: 91000400 add x0, x0, #0x1
4: d65f03c0 ret