当前位置:网站首页>How C programs run 01 - the composition of ordinary executable files
How C programs run 01 - the composition of ordinary executable files
2022-07-31 15:57:00 【Mculover666】
学习目的
- Where did the program burn?
- Where the program is loaded into memory?
- 程序如何执行?
一、编译环境搭建
ubuntu 20.04使用arm-linux-gnueabihf-gcc 7.5.0.
二、程序源码
main.c:
#include <stdio.h>
#include "calc.h"
int main(int argc, char *argv[])
{
int a, b;
static int local_val = 2;
static int uninit_local_val;
a = add(2, 3);
b = sub(5, 4);
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}
calc.h:
#ifndef _CALC_H_
#define _CALC_H_
int add(int a, int b);
int sub(int a, int b);
#endif
calc.c:
#include "calc.h"
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
编译:
arm-linux-gnueabihf-gcc main.c calc.c
交叉编译生成 a.out 可执行文件,文件类型是32位ARM平台可执行文件.
三、readelf工具
readelfTools are provided by the compiler,Used to list information about the contents of an executable file.
使用格式如下:
Usage: readelf <option(s)> elf-file(s)
(1)Look at the header of the executable 信息
-h
:用于列出ELF文件的头部信息,Include the platform on which the executable runs、软件版本、程序入口地址,以及program headers、section header等信息;
(2)查看section header
-S
:Used in listing programssection的头部信息
四、The structure of the executable file
An executable file consists of a seriessection构成,section称为段,包括:代码段text、只读数据段rodata、数据段data、bss段等.
每个section用一个section header描述,包括段名、段的类型、段的起始地址、段的偏移、段的大小等.
will be executable for allsection headerput together issection header table,使用readelf 的 -S
The parameter view is the table.
在程序编译的时候,对CFunctions defined in the language code、变量、Uninitialized global variables are compiled into categories,placed in different segments:
- Ordinary code is translated into binary and put into the code segment(text)中
- Constants are placed in the read-only data segment(rodata)中
- Initialized global variables and static local variables are placed in the data segment(data)中
BSS段比较特殊,Uninitialized global variables and static variables are placed into bss段中,But because the values of these variables are all0,There is no need to open up space for storage,所以在可执行文件中bssSections do not take up space.
但是BSS段的大小、起始地址、The address information of each variable will be saved separatelysection header table和符号表symtab中,当程序运行的时候,Based on this information, the loader places the space in memory immediately after the data segment,为BSSsegment to open up a piece of storage space,Allocate storage space for each variable.
总而言之:BSSSections do not take up space in an executable,The corresponding space is allocated only when the program is running.
If the debug option was enabled at compile time,The executable file will also have .debug section,It is used to save the source code location information corresponding to each binary instruction in the executable file,根据这些信息,GDBThe debugger can then support source-level single-step debugging.
在最后环节,The compiler also adds a few others to the executablesection,比如 .init section,这些代码来自CSome assembly code for the language runtime,用来初始化C程序所依赖的环境.
参考资料
边栏推荐
- C程序是如何跑起来的01 —— 普通可执行文件的构成
- tensorflow2.0 cnn(layerwise)
- Linux查看redis版本(查看mongodb版本)
- Implement anti-shake and throttling functions
- JVM参数解析 Xmx、Xms、Xmn、NewRatio、SurvivorRatio、PermSize、PrintGC「建议收藏」
- 字符串反转的实现方法总结「建议收藏」
- Foreign media right, apple on May be true in inventory
- Unity中实现点选RenderTexture中的3D模型
- [TypeScript] In-depth study of TypeScript type operations
- Implementing DDD based on ABP
猜你喜欢
Tencent Cloud Deployment----DevOps
gerrit中如何切换远程服务器
radiobutton的使用
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
The 2nd China PWA Developer Day
WPF project - basic usage of controls entry, you must know XAML
01 邂逅typescript,环境搭建
Qt实战案例(54)——利用QPixmap设计图片透明度
长得很怪的箱图
2022年整理LeetCode最新刷题攻略分享(附中文详细题解)
随机推荐
01 邂逅typescript,环境搭建
The new BMW 3 Series is on the market, with safety and comfort
字符串反转的实现方法总结「建议收藏」
Oracle dynamically registers non-1521 ports
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
Use of radiobutton
深度学习机器学习理论及应用实战-必备知识点整理分享
Insert into data table to insert data
MySQL数据库操作
form 表单提交后,使页面不跳转[通俗易懂]
Graham‘s Scan法求解凸包问题
Replication Latency Case (3) - Monotonic Read
11 pinia使用
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
t-sne 数据可视化网络中的部分参数+
[TypeScript] In-depth study of TypeScript type operations
OPPO在FaaS领域的探索与思考
i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)
Delete table data or clear table
tensorflow2.0 cnn(layerwise)