构建arm的裸机rust程序失败记录

简述

今天尝试在构建thumbv7em-none-eabi平台的no_std程序时,编译后的可执行文件的查看大小一直是0。

1
cargo size

输出:

1
2
text    data     bss     dec     hex filename
   0       0       0       0       0 base

最后查了半天资料,都没有找到问题所在。最后,才在cortex-m-rt中的文档发现问题所在。需要手动指定链接-C link-arg=-Tlink.x

搭建一个最小的可构建no_std的arm平台rust程序流程

添加thumbv7em-none-eabi目标:

1
rustup target add thumbv7em-none-eabi

创建项目:

1
cargo new base

修改项目使用的cargo配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# pwd is "base"
mkdir .cargo
cat > .cargo/config.toml <<EOF
[build]
target = "thumbv7em-none-eabi"

# 没有配置这个参数就是导致编译后的文件size为0的原因。
[target.thumbv7em-none-eabi]
rustflags = ["-C", "link-arg=-Tlink.x"] 
EOF

添加 cortex-m-rtpanic-halt依赖:

1
2
# pwd is base
cargo add panic-halt cortex-m-rt

cortex-m-rt创建必要的’memory.x‘文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#pwd is base
cat > memory.x <<EOF
/*
内容可根据设备文档和cortex-m-rt文档进行修改。
*/
MEMORY
{
    RAM     :   ORIGIN = 0x08002000, LENGTH = 0xFD800
    FLASH   :   ORIGIN = 0x10000000, LENGTH = 0x200000 
}
EOF

创建最小的可构建文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# pwd is base
cat > src/main.rs <<EOF
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use panic_halt as _;

#[entry]
fn main() -> ! {
    loop {}
}
EOF

参考资料

  1. https://docs.rs/cortex-m-rt/0.7.3/cortex_m_rt/
  2. https://jzow.github.io/discovery/f3discovery/05-led-roulette/build-it.html
使用 Hugo 构建
主题 StackJimmy 设计