用gcc编译STM32汇编代码

wljmcqd8  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(164)

我尝试为STM32编译一个简单的汇编程序,以检查GCC是否正常工作:

.syntax  unified
.cpu  cortex-m3
.thumb

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

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

编译器生成以下消息:

/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

我无法识别发生的问题,即使在做了一些研究之后。我了解编译器的基本原理,但我不精通使用GCC。

vnzz0bqm

vnzz0bqm1#

如果您的目标只是验证gcc是否正常工作,那么您可以使用以下命令构建一个可执行文件-您的程序被稍微修改了一下,以便它有一个入口点(Reset_Handler),.space 0xe4现在是.space 0xf8,这样代码就可以在4字节的边界上对齐。我决定程序从0x00080100开始,但根据目标上中断向量的数量,这可能是一个不同的值。

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

编译/链接:

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

转储内存内容/反汇编:

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

理想情况下,您现在需要:

  • 查找适合您特定stm32型号的gcc/ld教程,
  • 熟悉stm32的GNU ld链接器脚本(您可以在Internet上找到许多示例),
  • 熟悉gcc/ld命令行选项。

相关问题