This bug is seen on arm-*-eabi when generating thumb2 instructions with
interrupt functions that store variables on the stack. I've reduced the problem
to the following simple test case:

void function(int * x);

void interrupt_handler() __attribute__((__interrupt__));
void interrupt_handler()
{
    int data;
    function(&data);
}

The undefined function is just the simplest way I could think of to prevent the
compiler from optimizing away the data variable or converting it to a register.

The above code is compiled with:
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Os -c -o bug.o bug.c

Which generates the assembly for interrupt_handler:
mov     r0, sp
bic.w   r1, r0, #7      ; 0x7
mov     sp, r1
push    {r0, lr}
sub     sp, #8
add     r0, sp, #4
bl      0 <function>
ldmia.w sp!, {r0, r2, r3, lr}
mov     sp, r0
bx      lr

The problem is the last ldmia instruction which incorrectly pops off the values
from the stack.

When compiling with any other optimization flags besides -Os (-O0, -O1, -O2 or
-O3) the problem does not occur.

I see this bug in the gcc snapshot version 4.4.0-20090313 with the following
configure options:

../../../src/gcc-4.4-20090313/configure 
  --prefix=/usr/local/arm-none-eabi 
  --target=arm-none-eabi --build=x86_64-linux-gnu 
  --host=x86_64-linux-gnu --enable-interwork --enable-languages=c,c++ 
  --with-newlib --without-headers --disable-shared --with-gnu-as 
  --with-gnu-ld --disable-newlib-supplied-syscalls --disable-libgloss 
  --disable-libssp --disable-libmudflap --disable-libssp 
  --disable-libgomp --disable-libstdcxx-pch --disable-nls 
 
--with-gmp=/home/logang/Projects/toolchain/x86_64-linux-gnu/arm-none-eabi/gcc/../../support
 
 
--with-mpfr=/home/logang/Projects/toolchain/x86_64-linux-gnu/arm-none-eabi/gcc/../../support
 
  'CFLAGS_FOR_TARGET=-ffunction-sections -Os -fdata-sections
-fomit-frame-pointer -D__BUFSIZ__=256 -DPREFER_SIZE_OVER_SPEED
-D__OPTIMIZE_SIZE__'

This bug does not occur in gcc 4.3.3 with everything else the same.

Thanks,
Logan


-- 
           Summary: GCC Generates Incorrect Stack Instructins for Interrupts
                    When using -Os
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: logang at ece dot ualberta dot ca
 GCC build triplet: x86_64-linux-gnu
  GCC host triplet: x86_64-linux-gnu
GCC target triplet: arm-*-eabi


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39585

Reply via email to