cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing a memory section defined in the linker script

JohnnyB
Associate II

[Apologies in advance if this question has been asked a thousand times. I just didn't know how to search to get the answer that I'm looking for.]

I defined a section in the linker script to store version information. However, I'm unable to figure out how to read this memory from the c/c++ code side. 

In the linker script, I defined it in the 2nd section after the .isr_vector, ie:

SECTIONS

{

/* The startup code goes first into FLASH */

.isr_vector :

{

. = ALIGN(4);

KEEP(*(.isr_vector)) /* Startup code */

. = ALIGN(4);

} >FLASH

/* Version Data Into Flash */

.version :

{

. = ALIGN(4);

KEEP(*(.version))

KEEP(*(.version*))

. = ALIGN(4);

} >FLASH

.

.

}

In the main.c, I was able to set the version by:

static const char version[] __attribute__((used)) __attribute__((section (".version"))) = "v0.1.0";

 

Elsewhere in the code, I want to get the version string. I tried the following but the debugger shows the memptr pointer is 0x0:

const char* __attribute__(( section(".version") )) memptr;

 

I know the version was written correctly as I confirmed by looking at the memory map using STMCubeProgerammer and the memory address of the ".version" is 0x8000298.

I don't want to hard code since that address can change. Any assistance is appreciated. Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Kier
Associate II

In the linker script:

_linker_label_referable_in_source = LOADADDR(.version);

 In your source:

    extern uint32_t _linker_label_referable_in_source;
    char* memptr = (char*)&_linker_label_referable_in_source;

 

View solution in original post

5 REPLIES 5

printf("%p\n", &memptr);

printf("%p\n", version);

printf("%p\n", &version[0]);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

&version = 0x8000298

&memptr = 0x80002a0

 

Kier
Associate II

In the linker script:

_linker_label_referable_in_source = LOADADDR(.version);

 In your source:

    extern uint32_t _linker_label_referable_in_source;
    char* memptr = (char*)&_linker_label_referable_in_source;

 

Perhaps explain more clearly what it is you want to achieve by this

You can make different MEMORY locations the Linker can see and park things. You can shrink them, provide them with a fixed address basis. You can have the SECTIONS place content in specific memories. Currently it collects text, data and bss into FLASH so the startup.s code can later copy/initialize the statics in RAM. The Linker processes the script in a linear fashion, so ordering and placement are important.

You can use pointers within unused vector table entries to point to things that can move about, so you can find them within the binary image, say if you load on a PC via fopen(), fread(), etc.

The linker will fixup reference you have within your own C/C++ application.

I've indicated a way to relate the address reported in the .MAP to a pointer you can access from application space.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thanks for your help and suggestions. I'm trying to understand why "&version" and "&memptr" point to different addresses. I have a workaround to my problem but trying to learn how to dereference ".version" defined in the linker script.