[LinkerScript.7] 简单的链接器脚本命令: 区域别名 - Simple Linker Script Commands : Region Alias


Assign alias names to memory regions

存储区域的别名分配


Alias names can be added to existing memory regions created with the MEMORY command. Each name corresponds to at most one memory region

别名可以被加到用MEMORY命令创建的存储区域.每个名称对应着最多一个存储区域


REGION_ALIAS(alias,region)

The REGION_ALIAS function creates an alias name alias for the memory region region. This allows a flexible mapping of output sections to memory regions. An example follows.

REGION_ALIAS函数创建了存储区域region的一个别名.这使得输出section可以灵活地映射到存储区域。后面有一个例子。


Suppose we have an application for embedded systems which come with various memory storage devices. All have a general purpose, volatile memory RAM that allows code execution or data storage. Some may have a read-only, non-volatile memory ROM that allows code execution and read-only data access. The last variant is a read-only, non-volatile memory ROM2 with read-only data access and no code execution capability. We have four output sections:

  • .text program code;
  • .rodata read-only data;
  • .data read-write initialized data;
  • .bss read-write zero initialized data.
假设我们有一个具有各种存储设备的嵌入式系统的应用.所有的都是基于一个目的,易失性存储器RAM允许执行代码或存储数据。有些可能是只读,非易失性存储器ROM允许执行代码和数据只读访问。最后的是一个只读,非易失性存储器ROM2,具有只读数据访问和不可运行代码的特性。我们有四个输出sections:

 .text 程序代码

 .rodata 只读数据

 .data 可读写初始化数据

 .bss 可读写初始化数据,但数据必须被初始化为0.


The goal is to provide a linker command file that contains a system independent part defining the output sections and a system dependent part mapping the output sections to the memory regions available on the system. Our embedded systems come with three different memory setups AB and C:

SectionVariant AVariant BVariant C 
.textRAMROMROM 
.rodataRAMROMROM2 
.dataRAMRAM/ROMRAM/ROM2 
.bssRAMRAMRAM 

目标是提供一个链接器命令文件,该文件包含定义输出sections的系统独立部分和映射输出sections到系统上有效的存储区域的系统非独立部分.我们的嵌入式系统带有三个不同的存储配置A,B和C:

SectionVariant AVariant BVariant C 
.textRAMROMROM 
.rodataRAMROMROM2 
.dataRAMRAM/ROMRAM/ROM2 
.bssRAMRAMRAM 


The notation RAM/ROM or RAM/ROM2 means that this section is loaded into region ROM or ROM2 respectively. Please note that the load address of the .data section starts in all three variants at the end of the .rodata section.

The base linker script that deals with the output sections follows. It includes the system dependent linkcmds.memory file that describes the memory layout:

符号RAM/ROM 或 RAM/ROM2 表示这个section会被加载到相应的区域ROM 或 ROM2.请注意data section的加载地址是基于三个变量中.rodata section的末尾开始的.

这个基本链接器脚本处理了如下输出section.它包含了系统非独立的linkcmds.memory文件,该文件用于描述存储分布:


 INCLUDE linkcmds.memory
     
     SECTIONS
       {
         .text :
           {
             *(.text)
           } > REGION_TEXT
         .rodata :
           {
             *(.rodata)
             rodata_end = .;
           } > REGION_RODATA
         .data : AT (rodata_end)
           {
             data_start = .;
             *(.data)
           } > REGION_DATA
         data_size = SIZEOF(.data);
         data_load_start = LOADADDR(.data);
         .bss :
           {
             *(.bss)
           } > REGION_BSS
       }



Now we need three different linkcmds.memory files to define memory regions and alias names. The content of linkcmds.memory for the three variants AB and C:

现在我们需要三个不同的 linkcmds.memory 文件来定义存储区域和别名。 针对A,B和C的linkcmds.memory的内容如下:


A
Here everything goes into the RAM.
这里所有的都进入到RAM
          MEMORY
            {
              RAM : ORIGIN = 0, LENGTH = 4M
            }
          
          REGION_ALIAS("REGION_TEXT", RAM);
          REGION_ALIAS("REGION_RODATA", RAM);
          REGION_ALIAS("REGION_DATA", RAM);
          REGION_ALIAS("REGION_BSS", RAM);

B
Program code and read-only data go into the ROM. Read-write data goes into the RAM. An image of the initialized data is loaded into the ROM and will be copied during system start into the RAM.
程序代码和只读数据进入的是ROM。可读写的数据进入的是RAM。初始化数据的一个镜像被加载到ROM并且当系统启动时会被拷贝到RAM。
          MEMORY
            {
              ROM : ORIGIN = 0, LENGTH = 3M
              RAM : ORIGIN = 0x10000000, LENGTH = 1M
            }
          
          REGION_ALIAS("REGION_TEXT", ROM);
          REGION_ALIAS("REGION_RODATA", ROM);
          REGION_ALIAS("REGION_DATA", RAM);
          REGION_ALIAS("REGION_BSS", RAM);

C
Program code goes into the ROM. Read-only data goes into the ROM2. Read-write data goes into the RAM. An image of the initialized data is loaded into the ROM2 and will be copied during system start into the RAM.
程序代码进入的是ROM。只读数据进入的是ROM2。可读写的数据进入的是RAM。初始化数据的一个镜像被加载到ROM2并且当系统启动时被拷贝到RAM。
          MEMORY
            {
              ROM : ORIGIN = 0, LENGTH = 2M
              ROM2 : ORIGIN = 0x10000000, LENGTH = 1M
              RAM : ORIGIN = 0x20000000, LENGTH = 1M
            }
          
          REGION_ALIAS("REGION_TEXT", ROM);
          REGION_ALIAS("REGION_RODATA", ROM2);
          REGION_ALIAS("REGION_DATA", RAM);
          REGION_ALIAS("REGION_BSS", RAM);

It is possible to write a common system initialization routine to copy the .data section from ROM or ROM2 into the RAM if necessary:

如果需要,请尽可能地写一个通用的系统初始化例程,实现把.data section从ROM/ROM2拷贝到RAM。

     #include <string.h>
     
     extern char data_start [];
     extern char data_size [];
     extern char data_load_start [];
     
     void copy_data(void)
     {
       if (data_start != data_load_start)
         {
           memcpy(data_start, data_load_start, (size_t) data_size);
         }
     }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值