当前位置:网站首页>Examples illustrate the division basis of code segment (.Text), data segment (.Data), BSS segment, read-only data segment (.Rodata) and stack
Examples illustrate the division basis of code segment (.Text), data segment (.Data), BSS segment, read-only data segment (.Rodata) and stack
2022-07-21 08:20:00 【Kobayashi meow】
Catalog
Program examples introduce the contents of each paragraph 、 Analysis section (segment) Distribution
Let's take a small example to illustrate , Let everyone have a superficial impression .
The following program defines some different types of variables , It mainly includes initialized global variables (globle_var1 ) And static global variables (globle_static_var1 )、 Uninitialized global variables (globle_var2) And static global variables (globle_static_var2)、 Constant (globle_const_var 、const_var ) And local variables (var1 、var2). And through the function func Print out the sum result , A very simple program .
//hello.c
#include<stdio.h>
int globle_var1 = 10;
int globle_var2;
static int globle_static_var1 = 20;
static int globle_static_var2;
const int globle_const_var = 30;
void func(int i)
{
printf("%d\n",i);
}
int main()
{
static int static_var1 = 70;
static int static_var2;
const int const_var = 40;
int var1 = 30;
int var2;
var2 = globle_var1+globle_static_var1+globle_const_var+static_var1+const_var+var1;
func(var2);
return 0;
}
stay Target file The contents of at least include the compiled machine instruction code 、 data , It also includes some information needed for linking , For example, symbol table 、 Debugging information 、 String, etc. . The object file classifies this information by different attributes “ paragraph ”(segment) Form storage of . Here are some common paragraphs .
The structure of the compiled object file is also explained according to a simple program . It mainly analyzes the common segment information in the table , In fact, there are other segments, such as annotation information segment ( .comment ), But that's not our focus .
The following figure is the structure analysis of an object file , The main analysis code and data are stored in those segments , There is no stack segment information in the figure , Because the stack will not be allocated until the program is loaded and ready to run , Therefore, the memory of the stack segment has not been allocated at this stage of the target file . And I believe you are very familiar with stack , Stack is to store function local variables ,malloc() Memory allocation is allocated from the heap .
Analyze code snippets (.text)、 Data segment (.data)、bss paragraph 、 Read only data segment (.rodata) Basis of division
When I used to study, I only memorized the contents of each paragraph , I don't know why it is necessary to divide different sections . Let's analyze the basis of division , And what are the benefits of this division .
First , You can distinguish Code segment and Data segment . After the program source code is compiled, the machine instructions will be placed in the code segment ; Then the data segment here includes " .data “、” .bss “、” .rodata ", The global variables and local variables defined in the program are first called data segments .
Why put the program “ Code segment ” and “ Data segment ” Separate storage ?
- When the program is loaded , Data and instructions are mapped to two virtual memory areas respectively . Data segments are readable and writable for processes , The code segment is read-only to the process , Therefore, the permissions of these two virtual memory areas can be set to read-write and read-only respectively , Prevent program instructions from being intentionally and unintentionally rewritten .
- modern CPU The cache of is generally designed to separate data cache and instruction cache , The instructions and data of the program are stored separately CPU It is beneficial to improve the cache hit rate of .
- When multiple copies of the program are running in the system , For example, multiple threads run the same program at the same time , Their code segment instructions are the same , Therefore, only one code segment of the program needs to be saved in memory , Then divide the data segment of each copy process , This saves a lot of space .
Why does the data segment need to be divided " .data “、” .bss “、” .rodata " So much trouble ? What's the difference? ?
It is mainly distinguished according to two dimensions , Whether it takes up memory space 、 read-write permission .
Initialized global variables and local static variables are stored in " .data “ paragraph . Uninitialized global variables and local static variables are generally placed in ” .bss " paragraph , Because the default value of uninitialized variables is 0, They could have been put in .data paragraph , But because they are all 0, So for them in .data Segments allocate space and store data 0 There is no need to .
" .data" Paragraph and " .bss “ Segments are readable and writable data segments , and ” .rodata “ It's read-only data , Mainly a few const Variables and string constants . Set up separately ” .radata “ The advantage of paragraph is , When the program is loaded ” .rodata “ The attributes of the segment are mapped to read-only , In this way, any modification of this segment will be treated as an illegal operation . In addition, on some platforms ” .rodata " Segments are stored in read only memory , for example ROM, Ensure read-only through hardware .
So if by command objdump Check the size of each segment and find ,.data Segment size is 12 byte , It happens to be a variable globle_var1、globle_static_var1、static_var1 Size . however .bss The size of the segment is 8 Bytes , It's not a variable globle_var2、globle_static_var2、static_var2 The size of the variable , It can be seen that .bss There is no memory allocated for variables .
[email protected]-virtual-machine:/mnt/hgfs/share$ ls
a.out hello.c hello.o
[email protected]-virtual-machine:/mnt/hgfs/share$ objdump -x -s -d hello.o
hello.o: File format elf64-x86-64
hello.o
Architecture :i386:x86-64, sign 0x00000011:
HAS_RELOC, HAS_SYMS
Initial address 0x0000000000000000
section :
Idx Name Size VMA LMA File off Algn
0 .text 00000073 0000000000000000 0000000000000000 00000040 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 0000000c 0000000000000000 0000000000000000 000000b4 2**2
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000008 0000000000000000 0000000000000000 000000c0 2**2
ALLOC
3 .rodata 00000008 0000000000000000 0000000000000000 000000c0 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .comment 00000036 0000000000000000 0000000000000000 000000c8 2**0
CONTENTS, READONLY
5 .note.GNU-stack 00000000 0000000000000000 0000000000000000 000000fe 2**0
CONTENTS, READONLY
6 .eh_frame 00000058 0000000000000000 0000000000000000 00000100 2**3
......
reference
[1] Yu Jiazi , Shifan , Pan Aimin . Self cultivation of programmers —— link 、 Loading and storage [M]. Beijing : Electronic industry press ,2009.4
边栏推荐
猜你喜欢
重组单克隆抗体丨ProSci CD154 抗体实例分析
Chapter3 : Fighting COVID-19 with Artificial Intelligence
Data competition mode in go language
内存对齐的前因后果
Open up the chain and build a credible value ecosystem of the whole chain - Chang'an chain Oracle
常用的10款数据可视化工具软件
从放弃到精通,这13篇推荐系统论文一定要读!【附资料】
ProSci 14-3-3(磷酸 Ser58)抗体说明书
解决函数名冲突问题(dlopen,dlsym,dlclose)
redis排行榜
随机推荐
Super practical transformation strategy! The 2022 central state-owned enterprise cloud native landing practical guide was heavily released (with download link)
Jay Chou's new album is stolen immediately after its release. How to protect intellectual property rights Those things on the cloud
After Huawei, no one can stop apple. It is expected that iphone14 will be popular with hundreds of millions of domestic users
A strange problem in the running process of LoadRunner
grafana可视化配置图表Time series
【线性DP】数字三角形
鸿蒙3.0发布,多屏融合稳步推进,谷歌却再受重挫
Go语言中的数据竞争模式
Detailed explanation of C language array
Overview | comprehensive comparative research on image denoising
简述php文件上传原理
干货丨重中之重:数据分析中常用指标及术语!
#yyds干货盘点# 解决名企真题:小招喵跑步
执行程序的三种模式,内存与缓存
从0到1 拿下C语言—程序结构及使用示例
点云格式读取和保存
综述 | 图像去噪综合比较研究
限制input框中的输入类型及长度
The method of using postman to test the interface and send the request with cookie
华为之后再无谁可阻挡苹果,预期iPhone14获国内用户热捧至亿部