gpt4 book ai didi

rust - 如何在 Rust 的特定内存区域中声明静态变量?

转载 作者:行者123 更新时间:2023-11-29 08:05:51 26 4
gpt4 key购买 nike

我有一个静态常量,我想将其放在我的 MCU 的特定内存区域中,该程序是用 Rust 为 ARM stm32m4 MCU 编写的。

在我的测试用例中,我这样定义了变量:

#[link_section = ".device_info"]
static DEVINFO: &'static str = "This is in the correct place no?";

在我的 memory.x 文件中,我指定了:

MEMORY
{
FLASH : org = 0x08000000, len = 15k
DEVICE_INFO : org = 0x08003C00, len = 1k
EEPROM : org = 0x08004000, len = 16k
..Others
}

SECTIONS {
.device_info : {
*(.device_info);
. = ALIGN(4);
} > DEVICE_INFO
} INSERT AFTER .text;

这构建了,但是当我检查我的输出文件时,我想找到位于 0x8003c00 的文本 "This is in the correct place no?",但是当我用这个搜索时:

arm-none-eabi-objdump  target/thumbv7em-none-eabihf/debug/binaryfile -s | rg -C4 This

它给出了这个作为输出:

 8001b80 401b0008 2b000000 6b1b0008 15000000  @...+...k.......
8001b90 59010000 15000000 00000000 00000000 Y...............
8001ba0 696e6465 78206f75 74206f66 20626f75 index out of bou
8001bb0 6e64733a 20746865 206c656e 20697320 nds: the len is
8001bc0 54686973 20697320 696e2074 68652063 This is in the c
8001bd0 6f727265 63742070 6c616365 206e6f3f orrect place no?
8001be0 4e6f2076 616c6964 20666972 6d776172 No valid firmwar
8001bf0 65737372 632f6c69 622e7273 e01b0008 essrc/lib.rs....
8001c00 12000000 f21b0008 0a000000 ac000000 ................

编译时如何获取存放在8003c00的字符串?或者有任何值(value)吗?

基本上,最后,我想在那个特定位置存储一个更大的结构,因为这是我的引导加载程序,我想稍后从我的应用程序代码中读取该结构的值。

最佳答案

&'static str 的值仍然只是一个指针,所以您只在 .device_info 部分存储一个地址,而不是它指向的数据.要在那里存储实际值,您可以使用:

#[link_section = ".device_info"]
static DEVINFO: [u8; 32] = *b"This is in the correct place no?";

关于rust - 如何在 Rust 的特定内存区域中声明静态变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55493611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com