Search code examples
clinkerembeddedld

How to allocate constants in the function to the desired address?


I'm working on a project for an embedded device, that uses the custom ld script. I'm using GNU ARM Embedded Toolchain version 5-2016.

In the linker script, I define a custom section located on the specific address. Here is an abbreviated example:

MEMORY
{
    FLASH (RX)   : ORIGIN = 0x8000000, LENGTH = 1M
}

SECTIONS
{
  .mysection :
  {
    __mysection_start__ = .;
    *(.mysection*)
    *(.mysection)
    . = ALIGN(4);
    __mysection_end__ = .;
  } > FLASH
}

In the code I use __attribute__(section("mysection")) to locate the specific functions in the custom section. Consider at the following example:

void foo1()
{
    const char str[] = "foo1_string";
}

void foo2() __attribute__((section("mysection")));
void foo2()
{
    const char str[] = "foo2_string";
}

In this example I expect that the string literal str defined in the function foo2 will be located in the mysection section, however, it falls in the .rodata section.

What the correct way to allocate the constants from the foo2 function to the mysection section? Is it possible to do this without adding an attribute section to each variable inside the function?

Thanks in advance.


Solution

  • You can place the .rodata section data, from the particular object file(s) into another section

    SECTIONS
    {
      .mysection :
      {
        __mysection_start__ = .;
        *(.mysection*)
        *(.mysection)
        *myfile.o(.rodata)
        *myfile.o(.rodata*)
        . = ALIGN(4);
        __mysection_end__ = .;
      } > FLASH
    }
    

    In this example all data from the myfile.o (usually it will be result of the myfile.c compilation) which is in section .rodata (including string literals) will be placed in the .mysection. You can also use wildcards in the file names.

    I think it will sort out your problem.

    __attribute__((section("mysection"))); is incorrect . It has to be __attribute__((section(".mysection"))); because the name of your section is .mysection