当前位置:网站首页>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程序所依赖的环境.

参考资料
边栏推荐
- npm安装时卡在sill idealTree buildDeps,npm安装速度慢,npm安装卡在一个地方不动
- 双边滤波加速「建议收藏」
- The arm button controls the flashing of the led light (embedded button experiment report)
- 百度网盘网页版加速播放(有可用的网站吗)
- Foreign media right, apple on May be true in inventory
- tensorflow2.0 cnn(layerwise)
- 对话庄表伟:开源第一课
- After Grafana is installed, the web opens and reports an error
- 基于ABP实现DDD
- 多主复制的适用场景(1)-多IDC
猜你喜欢
随机推荐
Matlab matrix basic operations (definition, operation)
Website vulnerability repair service provider's analysis of unauthorized vulnerability
MySQL多表联合查询
软件实现AT命令操作过程
7、常见面试口语提问问题汇总
Replication Latency Case (3) - Monotonic Read
Bilateral filtering acceleration "recommended collection"
[MySQL] Mysql paradigm and the role of foreign keys
SringMVC中个常见的几个问题
What is the difference between BI software in the domestic market?
Kubernetes principle analysis and practical application manual, too complete
做事软件开发-法的重要性所在以及合理结论的认识
网站漏洞修复服务商关于越权漏洞分析
Codeforces Round #796 (Div. 2) (A-D)
Getting Started with TextBlock Control Basic Tools Usage, Get Started
2.索引及调优篇【mysql高级】
"Autumn Recruitment Series" MySQL Interview Core 25 Questions (with answers)
11 pinia use
Character pointer assignment [easy to understand]
npm安装时卡在sill idealTree buildDeps,npm安装速度慢,npm安装卡在一个地方不动









