You copied the Doc URL to your clipboard.

Using __attribute__((section("name"))) to place code and data

You can place code and data by separating them into their own objects without having to use toolchain-specific pragmas or attributes. However, you can also use __attribute__((section("name"))) to place an item in a separate ELF section. You can then use a scatter file to place the named sections at specific locations.

To use __attribute__((section("name"))) to place a variable in a separate section:

  1. Use __attribute__((section("name"))) to specify the named section where the variable is to be placed, for example:

    int variable __attribute__((section("foo"))) = 10;

  2. Use a scatter file to place the named section, for example:

    FLASH 0x24000000 0x4000000
        ...                                ; rest of code
        ADDER 0x08000000
            file.o (foo)                  ; select section foo from file.o

The following example shows the memory map for the FLASH load region:

  Load Region FLASH (Base: 0x24000000, Size: 0x00000004, Max: 0x04000000, ABSOLUTE)

    Execution Region ADDER (Base: 0x08000000, Size: 0x00000004, Max: 0xffffffff, ABSOLUTE)

    Base Addr    Size         Type   Attr      Idx    E Section Name        Object

    0x08000000   0x00000004   Data   RW           16    foo                 file.o

Be aware of the following:

  • linking with --autoat or --no_autoat does not affect the placement

  • if scatter-loading is not used, the section is placed in the default ER_RW execution region of the LR_1 load region

  • if you have a scatter file that does not include the foo selector, then the section is placed in the defined RW execution region.

You can also place a function at a specific address using .ARM.__at_address as the section name. For example, to place the function sqr at 0x20000, specify:

int sqr(int n1) __attribute__((section(".ARM.__at_0x20000")));

int sqr(int n1)
    return n1*n1;