Skip to main content
DGo.1
Associate II
September 15, 2021
Question

Placing string in a fixed memory address on flash

  • September 15, 2021
  • 5 replies
  • 7548 views

I want to place a variable at a fixed address on my flash memory:

#define MYDEF "abcdefg"
const u8 MY_CHAR[] __attribute__((section(".ARM.__at_0x0blabla"))) = MYDEF;

this works with KEIL IDE, but I switched to STM32CUBE IDE and it is not working. The mcu is an STM32F10

How do I have to change the attribute? Do I have also to change the linker script file ? This scares my...

Thanks

This topic has been closed for replies.

5 replies

alister
Senior III
September 15, 2021

STM32CubeIDE uses a different toolchain to KEIL.

Its compiler is described at https://gcc.gnu.org/.

Its linker is described at https://sourceware.org/binutils/docs/ld/index.html.

DGo.1
DGo.1Author
Associate II
September 15, 2021

So, I changed this to:

const u8 MY_CHAR[] __attribute__((section(".mySection"))) = MYDEF;

and add this to .ld file:

/* Memories definition */
MEMORY
{
 RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K
 FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 128K
}
 
/* Sections */
SECTIONS
{ 
 /* other placements follow here... */ 
 
 .mySegment 0x8002000 : {KEEP(*(.mySection))}
 
 /* The startup code into "FLASH" Rom type memory */
...and so on

But got compilation error if I put an address related to flash ( i.e. 0x8002000)

If I put 0x2000000 I got no error, what am I doing wrong ?

section .mySegment LMA [0000000008002000,000000000800200e] overlaps section .text LMA [00000000080001e8,000000000800c0cf]

TDK
Super User
September 15, 2021

The GCC linker isn't really set up to do this. You need to create a new section in memory and place the variable there, but it can't be in the middle of your flash.

It's easier to use a pointer and initialize manually.

"If you feel a post has answered your question, please click ""Accept as Solution""."
DGo.1
DGo.1Author
Associate II
September 15, 2021

Noobie question:

In this way also my .bin file will have this string at the address i want?

I want to put a string in a particular address i.e. : 0x8002000 ( so if I open the file with HxD it will show me this string at 0x8002000) is it possible to do with a pointer at compiling time ?

TDK
Super User
September 15, 2021

> I want to put a string in a particular address i.e. : 0x8002000 ( so if I open the file with HxD it will show me this string at 0x8002000) is it possible to do with a pointer at compiling time ?

No, it's not. You'd need to modify the linker to do that. But there's no easy way to place it at an absolute location within an existing memory section.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Guillaume K
ST Employee
September 15, 2021

Does the executable code take more than 0x2000 bytes ? the linker error seems to indicate that the code goes beyond 0x08002000 and you try to force a constant to be at this address.

could you try to place the constant after the end of the code ? (way after 0x08002000).