当前位置:网站首页>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")));
边栏推荐
- JS generates 4-digit verification code
- What are eNB, EPC and PGW?
- 免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
- Finally, someone explained the supervised learning clearly
- What are eNB, EPC and PGW?
- Error function ERF
- ADB basic commands
- Three methods of finding LCA of the nearest common ancestor
- Achievements in science and Technology (27)
- Jerry's watch time synchronization [chapter]
猜你喜欢

SAP MM 因物料有负库存导致MMPV开账期失败问题之对策

Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm

【云原生数据库】遇到慢SQL该怎么办(上)?
![Jerry's watch delete alarm clock [chapter]](/img/7f/d51b37872b4ce905a0a723a514b2dc.jpg)
Jerry's watch delete alarm clock [chapter]
![Student course selection information management system based on ssm+jsp framework [source code + database]](/img/71/900d83dba41974589b15d23e632119.png)
Student course selection information management system based on ssm+jsp framework [source code + database]

Unity skframework framework (XII), score scoring module

Solution: Compression Technology (original version and sequel version)

Unity SKFramework框架(十四)、Extension 扩展函数

OpenApi-Generator:简化RESTful API开发流程

Web Foundation
随机推荐
Redis database persistence
Should I have a separate interface assembly- Should I have a separate assembly for interfaces?
[indomitable medal activity] life goes on and writing goes on
Unity skframework framework (XV), singleton singleton
nohup命令
Jerry's weather code table [chapter]
leetcode621. 任务调度器
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
Unity skframework framework (XXI), texture filter map resource filtering tool
[Unity]使用GB2312,打包后程序不正常解决方案
Post order traversal sequence of 24 binary search tree of sword finger offer
国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
量子三体问题: Landau Fall
Lucky numbers in the [leetcode daily question] matrix
中文姓名提取(玩具代码——准头太小,权当玩闹)
[cloud native database] what to do when encountering slow SQL (Part 1)?
[youcans' image processing learning course] general contents
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
Solution: Compression Technology (original version and sequel version)
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials