You copied the Doc URL to your clipboard.

Placing a variable at a specific address with scatter-loading

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

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

    #include <stdio.h>
    extern int sqr(int n1);
    // Place at address 0x10000
    const int gValue __attribute__((section(".ARM.__at_0x10000"))) = 3;
    int main()
        int squared;
        printf("Value squared is: %d\n", squared);
  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 0x0
        ER1 0x0
            *(+RO)                      ; rest of code and read-only data
        ER2 +0
            *(.ARM.__at_0x10000)         ; Place gValue at 0x10000
        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 --no_autoat --scatter=scatter.scat --map function.o main.o -o squared.axf

    The --map option displays the memory map of the image.

The memory map shows that the variable is placed in the ER2 execution region at address 0x10000:

    Execution Region ER2 (Base: 0x00002a1c, Size: 0x0000d5e8, Max: 0xffffffff, ABSOLUTE)

    Base Addr    Size         Type   Attr      Idx    E Section Name        Object

    0x00002a00 0x0000001c   Code   RO            3    .text               function.o
    0x00002a1c 0x0000d5e4 PAD
    0x00010000   0x00000004   Data   RO           29 .ARM.__at_0x10000   main.o...

In this example, the size of ER1 is unknown. Therefore, gValue might be placed in ER1 or ER2. To make sure that gValue is placed in ER2, you must include the corresponding selector in ER2 and link with the --no_autoat command-line option. If you omit --no_autoat, gValue is placed in a separate load region LR$$.ARM.__AT_0x10000 that contains the execution region ER$$.ARM.__AT_0x10000.