Home > front end >  Compiling assembly code with gcc for STM32
Compiling assembly code with gcc for STM32

Time:12-24

I tried to compile a simple assembly program for the STM32 to investigate whether GCC is working correctly:

.syntax  unified
.cpu  cortex-m3
.thumb

.word  0x20000400
.word  0x080000ed
.space  0xe4

nop
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb main.s

The compiler produces the following message:

/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/thumb/v7-m/nofp/crt0.o: in function `_mainCRTStartup':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v7-m/nofp/libgloss/arm/semihv2m/../../../../../../../../libgloss/arm/crt0.S:545: undefined reference to `main'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc.a(lib_a-exit.o): in function `exit':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v7-m/nofp/newlib/libc/stdlib/../../../../../../../../newlib/libc/stdlib/exit.c:64: undefined reference to `_exit'
collect2: error: ld returned 1 exit status

I was unable to identify the problem that occurred, even after doing some research about it. I understood the basic principles of a compiler but I am not proficient in working with GCC.

CodePudding user response:

If your goal is just to verify that your gcc is working, you can build an executable using the following command - your program was slightly modified so that it would have an entry point (Reset_Handler), and .space 0xe4 is now .space 0xf8 so that the code would be aligned on a 4 bytes boundary. I decided the program would start at 0x00080100, but depending on the number of interrupt vectors on your target, this could have been a different value.

    .syntax   unified
    .cpu      cortex-m3
    .thumb
    .word     0x20000400
    .word     0x08000100
    .space    0xf8
    .text
    .global   ResetHandler
ResetHandler:
              nop
             .end

Compiling/linking:

    arm-none-eabi-gcc -o main.elf main.s -nostartfiles -nostdlib -e ResetHandler -Wl,-Ttext,0x080000

Dumping memory content/disassembling:

arm-none-eabi-objdump -D -s -j .text main.elf

main.elf:     file format elf32-littlearm

Contents of section .text:
 80000 00040020 00010008 00000000 00000000  ... ............
 80010 00000000 00000000 00000000 00000000  ................
 80020 00000000 00000000 00000000 00000000  ................
 80030 00000000 00000000 00000000 00000000  ................
 80040 00000000 00000000 00000000 00000000  ................
 80050 00000000 00000000 00000000 00000000  ................
 80060 00000000 00000000 00000000 00000000  ................
 80070 00000000 00000000 00000000 00000000  ................
 80080 00000000 00000000 00000000 00000000  ................
 80090 00000000 00000000 00000000 00000000  ................
 800a0 00000000 00000000 00000000 00000000  ................
 800b0 00000000 00000000 00000000 00000000  ................
 800c0 00000000 00000000 00000000 00000000  ................
 800d0 00000000 00000000 00000000 00000000  ................
 800e0 00000000 00000000 00000000 00000000  ................
 800f0 00000000 00000000 00000000 00000000  ................
 80100 00bf                                 ..              

Disassembly of section .text:

00080000 <ResetHandler-0x100>:
   80000:       20000400        andcs   r0, r0, r0, lsl #8
   80004:       08000100        stmdaeq r0, {r8}
        ...

00080100 <ResetHandler>:
   80100:       bf00            nop

You would ideally now need to:

  • Find a tutorial for gcc/ld suitable for your specific stm32 model,
  • Familiarize yourself with GNU ld linker scripts for your stm32 (you will find a lot of examples on the Internet),
  • Familiarize with gcc/ld command-line options.
  • Related