Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocate an array in C a specific location using linker commands

I am a beginner... I would like to write to a specific memory location in my embedded flash...How do I mention it in my C header file? And then link it with the specific memory location using linker scripts. Right now I have declared the array as extern and it compiles properly. While liking, I need to tell the linker that I need it at this particular location. Should it be given in .ld file? What is a .dld file? This is not for GCC, for diab compiler. I have seen a sample code bubble.dld for bubble sort. But in some projects .dld files are created while making the project. In what step is it actually created?

like image 293
Christy Wald Avatar asked Nov 30 '16 09:11

Christy Wald


1 Answers

First solution

in ".c":

// Talk to linker to place this in ".mysection"
__attribute__((section(".mysection"))) char MyArrray[52];

in ".ld":

MEMORY {
   m_interrupts (RX)    : ORIGIN = 0x00040000, LENGTH = 0x000001E8
   m_text      (RX)     : ORIGIN = 0x00050000, LENGTH = 0x000BFE18
   /* memory which will contain secion ".mysection" */
   m_my_memory (RX)       : ORIGIN = 0x00045000, LENGTH = 0x00000100
}

SECTIONS
{
  /***** Other sections *****/

  /* place "mysection" inside "m_my_memory" */
  .mysection :
  {
    . = ALIGN(4);
    KEEP(*(.mysection));
    . = ALIGN(4);
  } > m_my_memory

  /***** Other sections *****/
}

Second solution

in ".c"

extern char myArray[52];

in ".ld"

MEMORY {
   m_interrupts (RX)    : ORIGIN = 0x00040000, LENGTH = 0x000001E8
   m_text      (RX)     : ORIGIN = 0x00050000, LENGTH = 0x000BFE18
   /* memory which will contain secion "myArray" */
   m_my_memory (RX)       : ORIGIN = 0x00045000, LENGTH = 0x00000100
}

SECTIONS
{
  /***** Other sections *****/

  /* place "myArray" inside "m_my_memory" */
  .mysection :
  {
    . = ALIGN(4);
    myArray = .; /* Place myArray at current address, ie first address of m_my_memory */
    . = ALIGN(4);
  } > m_my_memory

  /***** Other sections *****/
}

See this good manual to learn more how to place elements where you want

like image 154
Garf365 Avatar answered Nov 15 '22 11:11

Garf365