http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54220
Bug #: 54220 Summary: [avr] Potential stack corruption in naked functions at -O0 Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target AssignedTo: unassig...@gcc.gnu.org ReportedBy: senthil_kumar.selva...@atmel.com Target: AVR Naked functions do not have a prologue and an epilogue by design, but code emitted with -O0 for function parameters assume their presence and attempt to copy values onto the stack. This can corrupt the caller's stack frame. Note the stores to Y+2 and Y+1 in the example below. [scratch]$ cat test.c void __attribute__((naked)) func(int x) { __asm volatile ("ret"); } [scratch]$ avr-gcc -O0 -S test.c [scratch]$ cat test.s .file "test.c" __SREG__ = 0x3f __SP_H__ = 0x3e __SP_L__ = 0x3d __CCP__ = 0x34 __tmp_reg__ = 0 __zero_reg__ = 1 .global __do_copy_data .global __do_clear_bss .text .global func .type func, @function func: /* prologue: naked */ /* frame size = 2 */ /* stack size = 0 */ .L__stack_usage = 0 std Y+2,r25 std Y+1,r24 /* #APP */ ; 3 "test.c" 1 ret ; 0 "" 2 /* epilogue start */ /* #NOAPP */ .size func, .-func