keil 将函数与数据放在固定地址的方法

本章内容截取于keil帮助文档,希望对你有帮助

Methods of placing functions and data at specific addresses

Non-Confidential  PDF version ARM 100070_0607_00_en
ARM® Compiler armlink User Guide Version 6.7
Home > Scatter-loading Features > Root region and the initial entry point > Methods of placing functions and data at specific addresses

7.2.3 Methods of placing functions and data at specific addresses

There are various methods available to place functions and data at specific addresses.

Placing functions and data at specific addresses

To place a single function or data item at a fixed address, you must enable the linker to process the function or data separately from the rest of the input files.

Where they are required, the compiler normally produces RO, RW, and ZI sections from a single source file. These sections contain all the code and data from the source file.

Note:

For images targeted at ARMv7-M or ARMv8-M, the compiler might generateexecute-only (XO) sections.

Typically, you create a scatter file that defines an execution region at the required address with a section description that selects only one section.

To place a function or variable at a specific address, it must be placed in its own section. There are several ways to do this:

  • Place the function or data item in its own source file.
  • Use __attribute__((section("name"))) to place functions and variables in a specially named section, .ARM.__at_address, where address is the address to place the function or variable. For example, __attribute__((section(".ARM.__at_0x4000"))).

    To place ZI data at a specific address, use the variable attribute __attribute__((section("name"))) with the special name .bss.ARM.__at_address

    These specially named sections are called __at sections.

  • Use the .section directive from assembly language. In assembly code, the smallest locatable unit is a .section.

  • Use the -ffunction-sections compiler option to generate one ELF section for each function in the source file.

    This option results in a small increase in code size for some functions because it reduces the potential for sharing addresses, data, and string literals between functions. However, this can help to reduce the final image size overall by enabling the linker to remove unused functions when you specify armlink --remove.

Related concepts

7.3 Example of how to explicitly place a named section with scatter-loading

7.2.6 Restrictions on placing __at sections

Related tasks

7.2.5 Placing __at sections at a specific address

Related reference

11.5 --autoat, --no_autoat

11.84 --map, --no_map

11.119 --scatter=filename

11.92 -o filename, --output=filename

Related information

AREA directive

Placing a variable at a specific address without scatter-loading

This example shows how to modify your source code to place code and data at specific addresses, and does not require a scatter file.

To place code and data at specific addresses without a scatter file:

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

    #include 
    
    extern int sqr(int n1);
    const int gValue __attribute__((section(".ARM.__at_0x5000"))) = 3; // Place at 0x5000
    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. 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 --map 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(".ARM.__AT_0x5000"))) specifies that the global variable gValue is to be placed at the absolute address 0x5000. gValue is placed in the execution region ER$$.ARM.__AT_0x5000 and load region LR$$.ARM.__AT_0x5000.

The memory map shows:

…
  Load Region LR$$.ARM.__AT_0x5000 (Base: 0x00005000, Size: 0x00000004, Max: 0x00000004, ABSOLUTE)

    Execution Region ER$$.ARM.__AT_0x5000 (Base: 0x00005000, Size: 0x00000004, Max: 0x00000004, ABSOLUTE, UNINIT)

    Base Addr    Size         Type   Attr      Idx    E Section Name        Object

    0x00005000   0x00000004   Data   RO           18    .ARM.__AT_0x5000  main.o

Related reference

11.5 --autoat, --no_autoat

11.84 --map, --no_map

11.92 -o filename, --output=filename

Example of how to place 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.

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 
    extern int sqr(int n1);
    int gSquared __attribute__((section("foo")));  // Place in section foo
    int main(void)
    {
        gSquared=sqr(3);
        printf("Value squared is: %d\n", gSquared);
        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 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
        }
        ; 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 --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: 0x00001570, Max: 0x00020000, ABSOLUTE)
…
    Execution Region ER3 (Base: 0x00010000, Size: 0x00000010, Max: 0x00002000, ABSOLUTE)

    Base Addr    Size         Type   Attr      Idx    E Section Name        Object

    0x00010000   0x0000000c   Code   RO            3    .text               function.o
    0x0001000c   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.

Related reference

11.5 --autoat, --no_autoat

11.84 --map, --no_map

11.92 -o filename, --output=filename

11.119 --scatter=filename

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

Related reference

11.5 --autoat, --no_autoat

11.84 --map, --no_map

11.92 -o filename, --output=filename

11.119 --scatter=filename

Non-Confidential  PDF version ARM 100070_0607_00_en
Copyright © 2014–2017 ARM Limited or its affiliates. All rights reserved.    
Home > Scatter-loading Features > Root region and the initial entry point > Methods of placing functions and data at specific addresses

Copyright © Keil, An ARM Company. All rights reserved.

你可能感兴趣的:(嵌入式微控制器编程)