You copied the Doc URL to your clipboard.

Placing a variable in a named section with scatter-loading

This example shows how to modify your source code to place code and data in a specific section using a scatter file:

  1. Create the source file main.c containing the following code:

    #include <stdio.h>
    
    extern int sqr(int n1);
    int gSquared __attribute__((section("foo")));  // Place in section foo
    
    int main()
    {
        gSquared=sqr(3);
        printf("Value squared is: %d\n", gSquared);
    }
    
  2. Create the source file function.c containing the following code:

    int sqr(int n1)
    {
        return n1*n1;
    }
    
    
  3. Create the scatter file scatter.scat containing the following load region:

    LR1 0x0000 0x20000
    {
        ER1 0x0 0x2000
        {
            *(+RO)                      ; rest of code and read-only data
        }
        ER2 0x8000 0x2000
        {
            main.o
        }
        ER3 0x10000 0x2000
        {
            function.o
            *(foo)                      ; Place gSquared in ER3
        }
        RAM 0x200000 (0x1FF00-0x2000)   ; RW & ZI data to be placed at 0x200000
        {
            *(+RW, +ZI)
        }
        ARM_LIB_STACK 0x800000 EMPTY -0x10000
        {
        }
        ARM_LIB_HEAP  +0 EMPTY 0x10000
        {
        }
    }
    

    The ARM_LIB_STACK and ARM_LIB_HEAP regions are required because the program is being linked with the semihosting libraries.

  4. Compile and link the sources:

    armclang -target armv8a-arm-none-eabi -c -g function.c
    armclang -target armv8a-arm-none-eabi -c -g main.c
    armlink --force-scanlib --cpu=8-A.32 --map --scatter=scatter.scat function.o main.o -o squared.axf
    

    The --map option displays the memory map of the image. Also, --autoat is the default.

In this example, __attribute__((section("foo"))) specifies that the global variable gSquared is to be placed in a section called foo. The scatter file specifies that the section foo is to be placed in the ER3 execution region.

The memory map shows:

  Load Region LR1 (Base: 0x00000000, Size: 0x00001778, Max: 0x00020000, ABSOLUTE)
...
    Execution Region ER3 (Base: 0x00010000, Size: 0x00000004, Max: 0x00002000, ABSOLUTE)

    Base Addr    Size         Type   Attr      Idx    E Section Name        Object

    0x00010000   0x0000000c   Code   RO            3    .text               function.o    0x00010000   0x00000004   Data   RW           15    foo                 main.o
...

Note

If you omit *(foo) from the scatter file, the section is placed in the region of the same type. That is RAM in this example.

Was this page helpful? Yes No