当前位置:网站首页>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程序所依赖的环境.
参考资料
边栏推荐
猜你喜欢
随机推荐
Single-cell sequencing workflow (single-cell RNA sequencing)
国内市场上的BI软件,到底有啥区别
【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
MySQL数据库操作
Linux check redis version (check mongodb version)
Deployment应用生命周期与Pod健康检查
Graham‘s Scan法求解凸包问题
Browser's built-in color picker
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
hough变换检测直线原理(opencv霍夫直线检测)
ML.NET相关资源整理
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
[TypeScript] In-depth study of TypeScript type operations
org.apache.jasperException(could not initialize class org)
Precautions and solutions when SIGABRT error is reported
做事软件开发-法的重要性所在以及合理结论的认识
多主复制的适用场景(2)-需离线操作的客户端和协作编辑
外媒所言非虚,苹果降价或许是真的在清库存
对话庄表伟:开源第一课
leetcode303 Weekly Match Replay