Linker script :: Dynamic Memory section

1

I would like to have a dynamic Memory map, like example to have flash spliced in 5 sections and according to a define in some file .h to set a proper memory map. But have some problems to do it :)

So this region would be dynamic allocated by defines in some .h

MEMORY
{
  if SOME_DEFINE == PART0
    rom (rx)  : ORIGIN = 0x00400000, LENGTH = 0x00040000 /* flash, 256K */
    ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00006000 /* sram, 24K */
  else 
    rom (rx)  : ORIGIN = 0x00400000, LENGTH = 0x00040000 /* flash, 256K */
    ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00006000 /* sram, 24K */
  endif
}
embedded
linker-scripts
asked on Stack Overflow Sep 27, 2016 by Danijel • edited Sep 27, 2016 by Danijel

1 Answer

2

I've addressed a similar need before using variables:

Define a master linker script, looking something like this:

$ head common_layout.ld
/* You can do something like this for optional sections */
CCFG_ORIGIN = DEFINED(CCFG_ORIGIN) ? CCFG_ORIGIN : 0;
CCFG_LENGTH = DEFINED(CCFG_LENGTH) ? CCFG_LENGTH : 0;

MEMORY
{
  rom (rx)  : ORIGIN =  ROM_ORIGIN, LENGTH =  ROM_LENGTH
  ccfg (rx) : ORIGIN = CCFG_ORIGIN, LENGTH = CCFG_LENGTH
  ram (rwx) : ORIGIN =  RAM_ORIGIN, LENGTH =  RAM_LENGTH
}

Then, for each chip you're dealing with, you can create a file with specifics for that chip (or have your build system create a temp file on the fly if it's really that dynamic):

$ cat chip_layout.ld
/* Memory Spaces Definitions */
ROM_ORIGIN  = 0x00010000; /* Bootloader is at 0x0000 */
ROM_LENGTH  = 0x00020000;
RAM_ORIGIN  = 0x20000000;
RAM_LENGTH = 0x00020000;

Then point your build tool to something that stitches them together, i.e. gcc -Tlayout.ld ...

$ cat layout.ld
INCLUDE ./chip_layout.ld
INCLUDE ../kernel_layout.ld
answered on Stack Overflow Mar 28, 2018 by Pat

User contributions licensed under CC BY-SA 3.0