当前位置:网站首页>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")));
边栏推荐
- Three methods of finding LCA of the nearest common ancestor
- [cloud native database] what to do when encountering slow SQL (Part 1)?
- De4000h storage installation configuration
- Daily question: 1175 Prime permutation
- JS逆向之巨量创意signature签名
- [unity] using GB2312, the solution to abnormal program after packaging
- Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
- I did it with two lines of code. As a result, my sister had a more ingenious way
- SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
- OpenApi-Generator:简化RESTful API开发流程
猜你喜欢
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
I did it with two lines of code. As a result, my sister had a more ingenious way
Student course selection information management system based on ssm+jsp framework [source code + database]
2022 Heilongjiang provincial examination on the writing skills of Application Essays
运维必备——ELK日志分析系统
Unity SKFramework框架(十五)、Singleton 单例
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
MAC (MacOS Monterey 12.2 M1) personal use PHP development
随机推荐
Partner cloud form strong upgrade! Pro version, more extraordinary!
[youcans' image processing learning course] general contents
机器学习基础(二)——训练集和测试集的划分
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
Three talking about exception -- error handling
Record idea shortcut keys
Unity skframework framework (XII), score scoring module
记忆函数的性能优化
Lucky numbers in the [leetcode daily question] matrix
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
Why is the default of switch followed by break?
Unity skframework Framework (XVI), package manager Development Kit Manager
Web Foundation
OpenApi-Generator:简化RESTful API开发流程
研究表明“气味相投”更易成为朋友
Web基础
Principle analysis of security rememberme
Error function ERF