Showing results for 
Search instead for 
Did you mean: 

Question with editing linker file (variable stored in flash)

Senior II

Hello! I was trying to set aside a section in flash for a variable (I do have a reason to be doing so but, for the time being, I'm in the learning process to make sure I understand how it works before I do anything (too) stupid).

Anyway, my chip was the STM32L412RB. I am currently setting it up so I can set aside the last 16 bytes of flash for a variable location that I'm calling FLASH_ARRAY.

I managed to get it to recognize the last 16 bytes of flash for the variable, so that part looks good. Here is my code

  RAM    (xrw)     : ORIGIN = 0x20000000,   LENGTH = 40K
  FLASH    (rx)    : ORIGIN = 0x8000000,   LENGTH = 128k-16
  FLASH_VAR (rx)   : ORIGIN = 0x801FFF0,   LENGTH = 16

So far, all looks good and it's assigning the section 16 bytes in this section in the Memory Details section in STM32CubeIDE.

The problem comes when I'm trying to assign a specific location within this section (say I want to assign a specific variable inside this location, for instance). Below is effectively how I want to assign a variable, for instance (below is only one variable but, I could potentially add multiple variables in this section).

uint8_t flash_array[16]
__attribute__((section(".flash_var .FLASH_ARRAY")));

If I understand what I'm seeing on another example (as I'm seeing at this location: ), this can potentially be done with the following added to the "SECTIONS" section:

.flash_var :
     KEEP(*(.flash_var .FLASH_ARRAY))
     *(.flash_var *);

When I try and run this though, I get the following errors:

section `.flash_var' will not fit in region `FLASH_VAR'
`FLASH_VAR' overflowed by 947837 bytes

Clearly, I set that last section up wrong, since the errors are introduced when I add that in. Since I don't seem to assign the variable a specific location (or size), I assume it's defaulting to a significantly larger size? I originally thought it might've been related to the min heap (0x200) or stack size (0x400) but that doesn't make sense in this context, from what I can see.

Could even be that I can't declare a section in flash smaller than a flash page size (which I need to look up, actually, that could be it). I found something regarding there being 64 pages of 2 kb programming. Does this mean I need to have sections that are all at least 2 kB? 64 pages of 2 kb would be 128 kb, so that does make sense . . .

EDIT: Ok, I tried the following and I'm getting the same errors, so I guess it's not that . . .

  RAM    (xrw)     : ORIGIN = 0x20000000,   LENGTH = 40K
  FLASH    (rx)    : ORIGIN = 0x8000000,   LENGTH = 126k
  FLASH_VAR (rx)   : ORIGIN = 0x801E000,   LENGTH = 2k

Any idea what might be going on here? I believe I'm close but I'm not quite there yet. Any info you can give me would be appreciated. Thanks!

Principal III

Try simpler names, not containing spaces " ". Like:




Actually, the names have no semantics that would matter. Also the KEEP only matters if you are *not* using your variable and still want it to be in the flash (like that version number info).

Otherwise, your code looks good.



Associate II

Hello! Did you solve the issue? I got your same error

Show salient portion of code and whole error message rather than #metoo an older thread.

If you've overflowed a section, put less in it or figure why it is so large.

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