当前位置:网站首页>Memory management 01 - link script
Memory management 01 - link script
2022-07-02 13:32:00 【Sui Bianbian】
The first blog post of the new year , Wish me success in my work first , All the best ! I won't go into details if I wish you all .
Before starting the memory management of the operating system , First, let's focus on the link script , Because the dynamic memory, that is, the address of the heap, is allocated in the link script , Only knowing the starting address and length of the heap can we allocate and manage memory .
1. What is the function of link script ?
The function of link is to compile multiple object files (.o) combined , Generate the final executable (.elf). As shown in the middle of the above figure .o Target file , The rightmost one is generated by links .elf file . besides , Link scripts also focus on one issue , That is, where the generated segments are loaded in memory .
For example, it is easy to understand , Here's a RISC-V Code link script :
OUTPUT_ARCH( "riscv" ) /* The code uses RISC-V framework */
ENTRY( _start ) /* The code entry symbol is _start, Is the symbol of the assembly startup function */
MEMORY
{
/* It defines a segment whose starting address is 0x80000000, The length is 128MB The memory area of , named ram*/
ram (wxa!ri) : ORIGIN = 0x80000000, LENGTH = 128M
}
SECTIONS
{
/* In all input files .text paragraph 、.text.* The paragraphs are all together , Make up the output elf In the document .text paragraph ;
* Besides , Two symbols are defined _text_start and _text_end , Pay attention to the symbols '.' Represents the current address ;
* Generated .text The paragraph is put in ram In this memory area .
*/
.text : {
PROVIDE(_text_start = .);
*(.text .text.*)
PROVIDE(_text_end = .);
} >ram
.rodata : {
PROVIDE(_rodata_start = .);
*(.rodata .rodata.*)
PROVIDE(_rodata_end = .);
} >ram
.data : {
. = ALIGN(4096);
PROVIDE(_data_start = .);
*(.sdata .sdata.*)
*(.data .data.*)
PROVIDE(_data_end = .);
} >ram
.bss :{
PROVIDE(_bss_start = .);
*(.sbss .sbss.*)
*(.bss .bss.*)
*(COMMON)
PROVIDE(_bss_end = .);
} >ram
PROVIDE(_memory_start = ORIGIN(ram));
PROVIDE(_memory_end = ORIGIN(ram) + LENGTH(ram));
PROVIDE(_heap_start = _bss_end);
PROVIDE(_heap_size = _memory_end - _heap_start);
}
You can see from the simple link script above ,MEMORY Grammar and SECTIONS Syntax is the most critical two in linking scripts . The link script will in turn .text、.rodata、.data Wait for the paragraphs to be placed in order ram In the memory area . And by definition _heap_start and _heap_size All the remaining space after allocation is allocated to the heap ( In this example, no space is allocated to the stack , Because stack can also start.S In the code space , It's up there .text Space contains ).
On the surface , Our problem is solved , The starting address and length of the heap are known , But the problem is , The symbols defined in the link script can be in C Is it used directly in the code ? Look at the second section .
2. stay C The symbols defined in the link script are used in the language
Symbols are names , Variable names and function names are symbols . There is a special " The symbol table " Paragraph is used to put symbols . For example, we define a global variable :int g_foo = 100;
Clearly in data The segment will have four bytes of space to store 100 This value , The assumption is 0x00001000~0x00001003 These four bytes . and g_foo This symbol or variable name , Then follow 0x00001000 This address is stored together in the symbol table .
typedef struct
{
Elf32_Word st_name; /* Symbol name (string tbl index) */ Byte offset in string table , Pointing to the symbol with null The ending string name .
Elf32_Addr st_value; /* Symbol value */ The address of the symbol , Is the offset from the start of the section that defines the target , For executable object files , This value is an absolute runtime address .
Elf32_Word st_size; /* Symbol size */ Length of object , For example, the length of a pointer or struct Number of bytes contained in , If the length is unknown , Its value can be set to 0.
unsigned char st_info; /* Symbol type and binding */ The exact purpose of a symbol is determined by st_info Definition , It's divided into two parts .
unsigned char st_other; /* Symbol visibility */ not used
Elf32_Section st_shndx; /* Section index */ Save the index of a section ( In the section header table ), The symbol is bound to the section , This symbol is usually defined in the code in this section .
} Elf32_Sym;
The structure of the symbol is defined as shown above . For the above g_foo For this variable , In its symbol structure st_name Indirectly means "g_foo" This string ,st_value The value is 0x00001000,st_size It stands for 4. stay C In language , When we use &g_foo
You can get g_foo The address of the variable , namely st_value.
however , The symbols defined in the link script are different from variables , The symbol in the link script has no corresponding memory address to store the value . for instance , A symbol _heap_start = 0x00002000
, In its symbol structure ,st_value The value is 0x00002000. When we're in C I want to use _heap_start, It can be realized through the following program :
extern int _heap_start ;
int val = &_heap_start ;
val The value is 0x00002000.
Follow your train of thought , We can define a real variable in assembly code , Its value is equal to the symbolic value , In this way C When used in a language, you don't need to use an address . This is based on the premise that : Symbolic values can be used directly in assembly .
Write the following code in the assembly :
.global HEAP_START
HEAP_START: .word _heap_start
then , You can go to C Do in language extern Use directly after declaration HEAP_START, Its value is 0x00002000.
3. Use the link script to specify the physical location of the variables
MEMORY
{undefined
ps7_ddr_0_S_AXI_BASEADDR : ORIGIN = 0x00100000, LENGTH = 0x3FF00000
ps7_ram_0_S_AXI_BASEADDR : ORIGIN = 0x00000000, LENGTH = 0x00030000
ps7_ram_1_S_AXI_BASEADDR : ORIGIN = 0xFFFF0000, LENGTH = 0x0000FE00
ps7_ddr_0_A_AXI_MATRIX : ORIGIN = 0x20000000, LENGTH = 0x100000
}
.matirx : {undefined
__matrix_start = .;
*(.matrix)
*(.matrix.*)
__matrix_end = .;
} > ps7_ddr_0_A_AXI_MATRIX
_end = .;
}
First, define a separate memory area and a separate section.
And then in C Language programming , Use the following statement to specify the storage location of global variables . stay Xilinx SDK Pro testing is available in programming .
int matrix[16][16384] __attribute__((section(".matrix")));
边栏推荐
- Unity SKFramework框架(十三)、Question 问题模块
- [技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
- PR usage skills, how to use PR to watermark?
- Unity SKFramework框架(十二)、Score 计分模块
- [indomitable medal activity] life goes on and writing goes on
- [youcans' image processing learning course] general contents
- Engineers who can't read device manuals are not good cooks
- SSL证书的分类有哪些?如何选择合适的SSL证书?
- Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
- Fundamentals of face recognition (facenet)
猜你喜欢
TVOC, VOC, VOCs gas detection + Solution
Finally, someone explained the supervised learning clearly
2022 Heilongjiang provincial examination on the writing skills of Application Essays
上海交大教授:何援军——包围盒(包容体/包围盒子)
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
题解:《压缩技术》(原版、续集版)
Unity SKFramework框架(十二)、Score 计分模块
[error record] cannot open "XXX" because Apple cannot check whether it contains malware
Jerry's watch time synchronization [chapter]
Unity skframework framework (XVI), package manager development kit Manager
随机推荐
Word efficiency guide - word's own template
【笔耕不辍勋章活动】生命不止,写作不息
Unity skframework framework (XX), VFX lab special effects library
Engineers who can't read device manuals are not good cooks
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
Solution: Compression Technology (original version and sequel version)
Unity SKFramework框架(十二)、Score 计分模块
口袋奇兵点评
Chinese name extraction (toy code - accurate head is too small, right to play)
Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
Node. JS accessing PostgreSQL database through ODBC
Unity SKFramework框架(十三)、Question 问题模块
JS逆向之行行查data解密
验证失败,请检查您的回电网址。您可以按照指导进行操作
能自动更新的万能周报模板,有手就会用!
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
Performance optimization of memory function
Security RememberMe原理分析
Unity SKFramework框架(十四)、Extension 扩展函数