5.6.9 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.

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(void)
    {
        int squared;
        squared=sqr(gValue);
        printf("Value squared is: %d\n", squared);
        return 0;
    }
  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
        {
            function.o
            *(.ARM.__at_0x10000)        ; Place gValue at 0x10000
        }
        ; RW and ZI data to be placed at 0x200000
        RAM 0x200000 (0x1FF00-0x2000)
        {
            *(+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=arm-arm-none-eabi -march=armv8-a -c function.c
    armclang --target=arm-arm-none-eabi -march=armv8-a -c main.c
    armlink --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: 0x00002a54, Size: 0x0000d5b0, Max: 0xffffffff, ABSOLUTE)

    Base Addr    Size         Type   Attr      Idx    E Section Name        Object

    0x00002a54   0x0000001c   Code   RO            4    .text.sqr           function.o
    0x00002a70   0x0000d590   PAD
    0x00010000   0x00000004   Data   RO            9    .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 to placed in a separate load region LR$$.ARM.__at_0x10000 that contains the execution region ER$$.ARM.__at_0x10000.

Non-ConfidentialPDF file icon PDF versionARM 100748_0607_00_en
Copyright © 2016, 2017 ARM Limited or its affiliates. All rights reserved.