当前位置:网站首页>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 .
边栏推荐
- The method of exchanging two numbers in C language
- [system programming] process, thread problem summary
- [sword finger offer] interview question 51: reverse pairs in the array - merge sort
- SQL multi table query
- synchronized和ReentrantLock的区别
- CAS比较交换的知识、ABA问题、锁升级的流程
- 无线网络分析有关的安全软件(aircrack-ng)
- [sword finger offer] interview question 55 - Ⅰ / Ⅱ: depth of binary tree / balanced binary tree
- Is the array name the address of the first element?
- vant-ui toast和dialog使用
猜你喜欢

CAS比较交换的知识、ABA问题、锁升级的流程

DRF学习笔记(四):DRF视图

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

C language: custom type

Is low code the future of development? On low code platform

DRF学习笔记(一):数据序列化

drf使用:get请求获取数据(小例子)

/Dev/loop1 takes up 100% of the problem

单机高并发模型设计

Network principle (1) - overview of basic principles
随机推荐
[sword finger offer] interview question 56-i: the number of numbers in the array I
语音直播系统——提升云存储安全性的必要手段
DRF学习笔记(准备)
[Yunxiang book club issue 13] packaging format and coding format of audio files
Analysis of spark task scheduling exceptions
Insert sort directly
To meet risc-v challenges? ARM CPU introduces custom instruction function!
Spark 3.0 DPP implementation logic
It is said that the US government will issue sales licenses to Huawei to some US enterprises!
Understand │ what is cross domain? How to solve cross domain problems?
Interview focus - TCP protocol of transport layer
Is low code the future of development? On low code platform
[system programming] process, thread problem summary
drf使用:get请求获取数据(小例子)
SQL multi table query
Keil implements compilation with makefile
Is the array name the address of the first element?
[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN
[sword finger offer] interview question 50: the first character that appears only once - hash table lookup
[sword finger offer] interview question 39: numbers that appear more than half of the time in the array