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.
.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 A
, B
and C
:
Section | Variant A | Variant B | Variant C |
.text | RAM | ROM | ROM |
.rodata | RAM | ROM | ROM2 |
.data | RAM | RAM/ROM | RAM/ROM2 |
.bss | RAM | RAM | RAM |
目标是提供一个链接器命令文件,该文件包含定义输出sections的系统独立部分和映射输出sections到系统上有效的存储区域的系统非独立部分.我们的嵌入式系统带有三个不同的存储配置A,B和C:
Section | Variant A | Variant B | Variant C |
.text | RAM | ROM | ROM |
.rodata | RAM | ROM | ROM2 |
.data | RAM | RAM/ROM | RAM/ROM2 |
.bss | RAM | RAM | RAM |
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 A
, B
and C
:
现在我们需要三个不同的 linkcmds.memory 文件来定义存储区域和别名。 针对A,B和C的linkcmds.memory的内容如下:
-
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);
-
Program code and read-only data go into the
ROM
. Read-write data goes into theRAM
. An image of the initialized data is loaded into theROM
and will be copied during system start into theRAM
. -
程序代码和只读数据进入的是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);
-
Program code goes into the
ROM
. Read-only data goes into theROM2
. Read-write data goes into theRAM
. An image of the initialized data is loaded into theROM2
and will be copied during system start into theRAM
. -
程序代码进入的是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);
A
B
C
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); } }