5.6.6 Placing constants at fixed locations

There are some situations when you want to place constants at fixed memory locations. For example, you might want to write a value to FLASH to read-protect a SoC device.

Procedure

  1. Create a C file abs_address.c to define an integer and a string constant.
    unsigned int const number = 0x12345678;
    char* const string = "Hello World";
  2. Create a scatter file, scatter.scat, to place the constants in separate sections ER_RONUMBERS and ER_ROSTRINGS.
    LR_1 0x040000          ; load region starts at 0x40000   
    {                      ; start of execution region descriptions      
        ER_RO 0x040000     ; load address = execution address
        {
            *(+RO +RW)     ; all RO sections (must include section with 
                           ; initial entry point)
        }
        ER_RONUMBERS +0
        {
            *(.rodata.number, +RO-DATA)
        }
        ER_ROSTRINGS +0
        {
            *(.rodata.string, .rodata.str1.1, +RO-DATA)
        }
                           ; rest of scatter-loading description
    
        ARM_LIB_STACK 0x80000 EMPTY -0x20000  ; Stack region growing down
        { }
        ARM_LIB_HEAP 0x28000000 EMPTY 0x80000 ; Heap region growing up
        { }
    }

    armclang puts string literals in a section called .rodata.str1.1

  3. Compile and link the file.
    armclang --target=arm-arm-eabi-none -mcpu=cortex-a9 abs_address.c -g -c -o abs_address.o
    armlink --cpu=cortex-a9 --scatter=scatter.scat abs_address.o --output=abs_address.axf
  4. Run fromelf on the image to view the contents of the output sections.
    fromelf -c -d abs_address.axf
    The output contains the following sections:
    ...
    ** Section #2 'ER_RONUMBERS' (SHT_PROGBITS) [SHF_ALLOC]
        Size   : 4 bytes (alignment 4)
        Address: 0x00040000
    
        0x040000:   78 56 34 12                                        xV4.
    
    
    ** Section #3 'ER_ROSTRINGS' (SHT_PROGBITS) [SHF_ALLOC]
        Size   : 16 bytes (alignment 4)
        Address: 0x00040004
    
        0x040004:   48 65 6c 6c 6f 20 57 6f 72 6c 64 00 04 00 04 00    Hello World.....
    ...
  5. Replace the ER_RONUMBERS and ER_ROSTRINGS sections in the scatter file with the following ER_RODATA section:
        ER_RODATA +0
        {
            abs_address.o(.rodata.number, .rodata.string, .rodata.str1.1, +RO-DATA)
        }
  6. Repeat steps 3 and 4.
    The integer and string constants are both placed in the ER_RODATA section, for example:
    ** Section #2 'ER_RODATA' (SHT_PROGBITS) [SHF_ALLOC]
        Size   : 20 bytes (alignment 4)
        Address: 0x00040000
    
        0x040000:   78 56 34 12 48 65 6c 6c 6f 20 57 6f 72 6c 64 00    xV4.Hello World.
        0x040010:   04 00 04 00                                        ....
Non-ConfidentialPDF file icon PDF versionARM 100748_0607_00_en
Copyright © 2016, 2017 ARM Limited or its affiliates. All rights reserved.