3

I have static constant that I want to place in a specific memory region of my MCU, and the program is written in Rust for an ARM stm32m4 MCU.

In my test case I have defined the variable as such:

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

In my memory.x file I have specified:

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;

This builds, but when I check my output file, I want to find the text "This is in the correct place no?" located at 0x8003c00, but instead when I search with this:

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

It gives this as output:

 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  ................

How do I get the string to be stored at 8003c00 when compiling? Or any value for that matter?

Basically, in the end, I want to store a larger structure at that specific position as this is my bootloader, and I want to read that structure's value from my application code later.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Kristoffer
  • 456
  • 5
  • 17
  • 1
    What _do_ you have at address 0x8003c00? The value `8001bc0` per chance? – Lundin Apr 03 '19 at 11:10
  • Well sir, I do have c01b0008, but that is due to my endianess... – Kristoffer Apr 03 '19 at 11:20
  • Right. So your code only stores a pointer in flash, not the actual string. I don't know anything about Rust, but in C you would do `const char str[] = "This is in the correct place no?"` rather than `const char* str = ...`. – Lundin Apr 03 '19 at 11:25
  • Thank you, at least i know there are no other errors, rust is not very forgiving for creating arbitrary lengt arrays though, but that is another problem – Kristoffer Apr 03 '19 at 11:38
  • As lundin said, the pointer to the string is stored at that position. Is that a problem? You else have to store a byte array `[u8]` at the position. A problem could be the length of the string (not `\0` terminated) as well as the encoding (Rust uses UTF-8 per default) – hellow Apr 03 '19 at 12:06
  • See also [How do I declare a static variable as a reference to a hard-coded memory address?](https://stackoverflow.com/q/48701896/155423) – Shepmaster Apr 03 '19 at 12:34

1 Answers1

3

The value of &'static str is still just a pointer, so you are storing only an address in the .device_info section, not the data it is pointing to. To store the actual value there, you could use:

#[link_section = ".device_info"]
static DEVINFO: [u8; 32] = *b"This is in the correct place no?";
Peter Hall
  • 53,120
  • 14
  • 139
  • 204