https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70063

            Bug ID: 70063
           Summary: msp430 stack corruption for naked functions
           Product: gcc
           Version: 5.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: awygle at gmail dot com
  Target Milestone: ---

Created attachment 37853
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=37853&action=edit
A nearly minimal example of code producing the bug

When compiling msp430 code including functions which have both arguments and
the naked attribute using -O0, the stack is corrupted and the return pointer is
lost.

This looks like an msp430-style retread of
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54220 to me.

Code:

# 1 "bug.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "bug.c"

__attribute__((naked))
void buggy(int* arg1, int* arg2) {
  asm volatile("sub #4, r1");
  (*arg1) = 2;
  (*arg2) = 3;
  asm volatile ("nop");
  asm volatile ("nop");
  asm volatile ("nop");
  asm volatile ("nop");
  asm volatile ("nop");
  asm volatile ("ret");
}

int main() {

  int x, y;
  x = 0;
  y = 1;

  buggy(&x, &y);

  while(1) {}

}

Compiling:

[awygle %] msp430-elf-gcc -v -Wall -Wextra -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -mmcu=msp430fr5969 bug.c -o bug.elf
-save-temps

Using built-in specs.
COLLECT_GCC=msp430-elf-gcc
COLLECT_LTO_WRAPPER=/home/awygle/toolchain/install/libexec/gcc/msp430-elf/5.3.0/lto-wrapper
Target: msp430-elf
Configured with: ../gcc-5.3.0/configure
--prefix=/home/awygle/toolchain/install/ --target=msp430-elf --without-headers
--with-newlib --enable-languages=c,c++ --disable-multilib -v
Thread model: single
gcc version 5.3.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-Wall' '-Wextra' '-fno-strict-aliasing' '-fwrapv'
'-fno-aggressive-loop-optimizations' '-mmcu=msp430fr5969' '-o' 'bug.elf'
'-save-temps'
 /home/awygle/toolchain/install/libexec/gcc/msp430-elf/5.3.0/cc1 -E -quiet -v
bug.c -mmcu=msp430fr5969 -Wall -Wextra -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -fpch-preprocess -o bug.i
ignoring nonexistent directory
"/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/../../../../msp430-elf/sys-include"
#include "..." search starts here:
#include <...> search starts here:
 /home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/include
 /home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/include-fixed

/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/../../../../msp430-elf/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-Wall' '-Wextra' '-fno-strict-aliasing' '-fwrapv'
'-fno-aggressive-loop-optimizations' '-mmcu=msp430fr5969' '-o' 'bug.elf'
'-save-temps'
 /home/awygle/toolchain/install/libexec/gcc/msp430-elf/5.3.0/cc1 -fpreprocessed
bug.i -quiet -dumpbase bug.c -mmcu=msp430fr5969 -auxbase bug -Wall -Wextra
-version -fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations -o
bug.s
GNU C11 (GCC) version 5.3.0 (msp430-elf)
        compiled by GNU C version 4.9.2, GMP version 6.0.0, MPFR version
3.1.2-p11, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C11 (GCC) version 5.3.0 (msp430-elf)
        compiled by GNU C version 4.9.2, GMP version 6.0.0, MPFR version
3.1.2-p11, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 4cbf41f67559bdb236a322a0045966e6
COLLECT_GCC_OPTIONS='-v' '-Wall' '-Wextra' '-fno-strict-aliasing' '-fwrapv'
'-fno-aggressive-loop-optimizations' '-mmcu=msp430fr5969' '-o' 'bug.elf'
'-save-temps'

/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/../../../../msp430-elf/bin/as
--traditional-format -mP -mmcu=msp430fr5969 -md -o bug.o bug.s
COMPILER_PATH=/home/awygle/toolchain/install/libexec/gcc/msp430-elf/5.3.0/:/home/awygle/toolchain/install/libexec/gcc/msp430-elf/5.3.0/:/home/awygle/toolchain/install/libexec/gcc/msp430-elf/:/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/:/home/awygle/toolchain/install/lib/gcc/msp430-elf/:/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/../../../../msp430-elf/bin/
LIBRARY_PATH=/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/:/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/../../../../msp430-elf/lib/
COLLECT_GCC_OPTIONS='-v' '-Wall' '-Wextra' '-fno-strict-aliasing' '-fwrapv'
'-fno-aggressive-loop-optimizations' '-mmcu=msp430fr5969' '-o' 'bug.elf'
'-save-temps'
 /home/awygle/toolchain/install/libexec/gcc/msp430-elf/5.3.0/collect2 -plugin
/home/awygle/toolchain/install/libexec/gcc/msp430-elf/5.3.0/liblto_plugin.so
-plugin-opt=/home/awygle/toolchain/install/libexec/gcc/msp430-elf/5.3.0/lto-wrapper
-plugin-opt=-fresolution=bug.res -plugin-opt=-pass-through=-lgcc
-plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc
-plugin-opt=-pass-through=-lcrt -plugin-opt=-pass-through=-lgcc -o bug.elf
/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/../../../../msp430-elf/lib/crt0.o
/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/crtbegin.o
-L/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0
-L/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/../../../../msp430-elf/lib
bug.o -lgcc --start-group -lc -lgcc -lcrt --end-group --script=msp430fr5969.ld
-lgcc /home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/crtend.o
/home/awygle/toolchain/install/lib/gcc/msp430-elf/5.3.0/../../../../msp430-elf/lib/crtn.o
-lgcc

ASM output that's incorrect:

buggy:
  MOV.W R12, 2(R1)
  MOV.W R13, @R1 ; return address is destroyed
  sub #4, r1

Reply via email to