当前位置:网站首页>Reduce program ROM ram, GCC -ffunction sections -fdata sections -wl, – detailed explanation of GC sections parameters
Reduce program ROM ram, GCC -ffunction sections -fdata sections -wl, – detailed explanation of GC sections parameters
2022-07-27 16:02:00 【xhoufei2010】
In this paper, from p_fly The blog of :https://blog.csdn.net/pengfei240/article/details/55228228
author : Pit digging specialist
background
Sometimes our program will define some functions and functions that are temporarily unavailable , Although we don't use these functions and functions , But they tend to waste our ROM and RAM Space . This is when using static libraries , More serious . Sometimes , We only use a few functions of the static library , However, the system will automatically link the whole static library to the executable program by default , The size of the executable program is greatly increased .
Parameters,
In order to solve the problems analyzed above , We introduced several parameters in the title .GCC The link operation is based on section As the smallest processing unit , As long as a section A symbol in is referenced , The section Will be added to the executable program . therefore ,GCC You can use -ffunction-sections and -fdata-sections Create each function or symbol as a sections, Each of them sections Name and function or data Keep the names the same . And in the link phase , -Wl,–gc-sections Instructs the linker to remove unused section( among -wl, Represents the following parameter -gc-sections Pass it to the linker ), This reduces the size of the final executable .
We often use the following configuration to enable this function :
CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sectionsExample
main.c The documents are as follows :
#include <stdio.h>
int fun_0(void)
{
printf("%s: %d\n", __FUNCTION__, __LINE__);
return 0;
}
int fun_1(void)
{
printf("%s: %d\n", __FUNCTION__, __LINE__);
return 0;
}
int fun_2(void)
{
printf("%s: %d\n", __FUNCTION__, __LINE__);
return 0;
}
int fun_3(void)
{
printf("%s: %d\n", __FUNCTION__, __LINE__);
return 0;
}
void main(void)
{
fun_0();
fun_3();
}Makefile The documents are as follows :
main_sections:
gcc -ffunction-sections -fdata-sections -c main.c
gcc -Wl,--gc-sections -o [email protected] main.o
main_normal:
gcc -c main.c
gcc -o [email protected] main.o
clean:
rm -rf *.o main_sections main_normalverification
function
$ make main_sections
gcc -ffunction-sections -fdata-sections -c main.c
gcc -Wl,--gc-sections -o main_sections main.o
$ make main_normal
gcc -c main.c
gcc -o main_normal main.oCompare the size
$ ll main_*
-rwxrwxr-x 1 8896 2 month 16 00:42 main_normal*
-rwxrwxr-x 1 8504 2 month 16 00:42 main_sections*To see that the binary file using this function is smaller than the binary file not using this function
analysis sections
$ make clean
rm -rf *.o main_sections main_normal
$ make main_sections
gcc -ffunction-sections -fdata-sections -c main.c
gcc -Wl,--gc-sections -o main_sections main.o
$ readelf -t main.o
...
[ 5] .text.fun_0
PROGBITS PROGBITS 0000000000000000 0000000000000048 0
0000000000000024 0000000000000000 0 1
[0000000000000006]: ALLOC, EXEC
[ 6] .rela.text.fun_0
RELA RELA 0000000000000000 0000000000000508 24
0000000000000048 0000000000000018 5 8
[0000000000000040]: INFO LINK
[ 7] .text.fun_1
PROGBITS PROGBITS 0000000000000000 000000000000006c 0
0000000000000024 0000000000000000 0 1
[0000000000000006]: ALLOC, EXEC
[ 8] .rela.text.fun_1
RELA RELA 0000000000000000 0000000000000550 24
0000000000000048 0000000000000018 7 8
[0000000000000040]: INFO LINK
[ 9] .text.fun_2
PROGBITS PROGBITS 0000000000000000 0000000000000090 0
0000000000000024 0000000000000000 0 1
[0000000000000006]: ALLOC, EXEC
[10] .rela.text.fun_2
RELA RELA 0000000000000000 0000000000000598 24
0000000000000048 0000000000000018 9 8
[0000000000000040]: INFO LINK
[11] .text.fun_3
PROGBITS PROGBITS 0000000000000000 00000000000000b4 0
0000000000000024 0000000000000000 0 1
[0000000000000006]: ALLOC, EXEC
[12] .rela.text.fun_3
RELA RELA 0000000000000000 00000000000005e0 24
0000000000000048 0000000000000018 11 8
[0000000000000040]: INFO LINK from object You can find ,fun_0 ~ fun_3 Each function is an independent section.
And if you use make main_normal Generated object file , Then share a default sections(.text).
analysis elf file
$ readelf -a main_normal | grep fun_
52: 0000000000400526 36 FUNC GLOBAL DEFAULT 14 fun_0
55: 000000000040056e 36 FUNC GLOBAL DEFAULT 14 fun_2
65: 0000000000400592 36 FUNC GLOBAL DEFAULT 14 fun_3
66: 000000000040054a 36 FUNC GLOBAL DEFAULT 14 fun_1
$ readelf -a main_sections | grep fun_
49: 0000000000400526 36 FUNC GLOBAL DEFAULT 14 fun_0
57: 000000000040054a 36 FUNC GLOBAL DEFAULT 14 fun_3Can see , In the final target document , Unused functions are not linked to the final object file .
边栏推荐
- 网络原理(2)——网络开发
- [tensorboard] oserror: [errno 22] invalid argument processing
- Modify spark to support remote access to OSS files
- 快速高效删除node_modules
- Single machine high concurrency model design
- Spark Bucket Table Join
- DRF学习笔记(一):数据序列化
- DRF学习笔记(准备)
- [sword finger offer] interview question 41: median in data flow - large and small heap implementation
- 数据表的约束以及设计、联合查询——8千字攻略+题目练习解答
猜你喜欢

低代码是开发的未来吗?浅谈低代码平台

Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer

The difference between synchronized and reentrantlock

Push down of spark filter operator on parquet file

C language: string function and memory function

SQL multi table query

: 0xC0000005: 写入位置 0x01458000 时发生访问冲突----待解

IP protocol of network layer
![[tensorboard] oserror: [errno 22] invalid argument processing](/img/bf/c995f487607e3b307a268779ec1e94.png)
[tensorboard] oserror: [errno 22] invalid argument processing

Sword finger offer 51. reverse pairs in the array
随机推荐
Single machine high concurrency model design
C language: minesweeping games
Go language slow start -- go operator
Binder初始化过程
语音直播系统——提升云存储安全性的必要手段
CAS比较交换的知识、ABA问题、锁升级的流程
Paper_Book
[system programming] process, thread problem summary
The method of exchanging two numbers in C language
Is the array name the address of the first element?
busybox login: can't execute '/bin/bash': No such file or directory 解决方法
移动端h5常见问题
Openwrt增加对 sd card 支持
Implementation of spark lazy list files
无线网络分析有关的安全软件(aircrack-ng)
profileapi.h header
网络原理(2)——网络开发
Openwrt adds RTC (mcp7940 I2C bus) drive details
[Yunxiang book club issue 13] coding format of video files
The risk of multithreading -- thread safety