DamV
DamV

Reputation: 35

RAM section is part of the binary firmware

I am trying to use a custom RAM section to be able to pass information across reboot. This section will not be erased at boot and so the variables placed in this section will be kept across reboots (if there is no alimentation loss of course).

I use GNU toolchain and a Cortex-M0 (STM32) MCU

So I added in the linker script a new memory area before RAM :

RAM_PERSIST (xrw) : ORIGIN = 0x20000000, LENGTH = 0x0040
RAM (xrw)         : ORIGIN = 0x20000040, LENGTH = 0x0FD0

Then a section to go in there :

.pds :
{
  KEEP(*(.pds))
} >RAM_PERSIST

Finally in the C code, I declare some data in this section :

data_t __attribute((section(".pds")) data;

I does compile but I could not upload the generated binary on my target. Using objdump I discovered that my firmware got a new section ".sec2" beginning at 0x20000000 :

> (...)/arm-none-eabi-objdump -s ./obj/firmware.hex | tail
 8006d20 f8bc08bc 9e467047 f8b5c046 f8bc08bc  .....FpG...F....
 8006d30 9e467047 e9000008 c1000008 00127a00  .FpG..........z.
 8006d40 19000000 e0930400 409c0000 400d0300  ........@...@...
 8006d50 c0c62d00 30750000 ffffffff 01000000  ..-.0u..........
 8006d60 04000000                             ....
Contents of section .sec2:
 20000000 00000000 00000000 00000000 00000000  ................
 20000010 00000000 00000000 00000000 00000000  ................
 20000020 00000000 00000000 00000000 00000000  ................
 20000030 00000000 00000000 00000000 00000000  ................

So I think I have to tell the linker this section is not in the flash so must not be part of the firmware.

Am I right ? If so, how to do that ?

Thanks by advance.

Upvotes: 0

Views: 424

Answers (2)

DamV
DamV

Reputation: 35

I did achieve what I wanted by adding NOLOAD attibute to my custom section :

.pds (NOLOAD): { KEEP(*(.pds)) } >RAM

Here is the NOLOAD description (gcc documentation) :

(NOLOAD) The (NOLOAD) directive will mark a section to not be loaded at run time. The linker will process the section normally, but will mark it so that a program loader will not load it into memory. For example, in the script sample below, the ROM section is addressed at memory location 0 and does not need to be loaded when the program is run. The contents of the ROM section will appear in the linker output file as usual.

SECTIONS {
  ROM  0  (NOLOAD)  : { ... }
  ...
}

I found a similar post which helped me, I add a link here for reference : GCC (NOLOAD) directive loads memory into section anyway

Upvotes: 1

old_timer
old_timer

Reputation: 71536

MEMORY
{
    rom : ORIGIN = 0x00000000, LENGTH = 0x40000
    ram : ORIGIN = 0x20000000, LENGTH = 0x4000
}

SECTIONS
{
    .text : { *(.text*) } > rom
    .rodata : { *(.rodata*) } > rom
    .bss : { *(.bss*) } > ram
}

I had more control/success when I stopped using xrw, etc in the memory definition and instead went with control over .text, .bss, .data, etc. and if you then further want a specific object somewhere you add that. etc...

Upvotes: 1

Related Questions