gcc / -ffunction-sections、-fdata-sections、-Wl,--gc-sections

通过GCC的-ffunction-sections和-fdata-sections编译选项,以及-Wl,--gc-sections链接选项,可以将每个函数和数据放入独立的section,并在链接时删除未使用的section,从而减小可执行程序的体积。在示例中,使用这些选项的main_sections比常规编译的main_normal文件更小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、背景

有时我们的程序会定义一些暂时使用不上的功能和函数,虽然我们不使用这些功能和函数,但它们往往会浪费我们的 ROM 和 RAM 的空间。这在使用静态库时,体现的更为严重。有时,我们只使用了静态库仅有的几个功能,但是系统默认会自动把整个静态库全部链接到可执行程序中,造成可执行程序的大小大大增加。

二、参数详解

为了解决前面分析的问题,我们引入了标题中的几个参数。GCC 链接操作是以 section 作为最小的处理单元,只要一个 section 中的某个符号被引用,该 section 就会被加入到可执行程序中去。

因此,GCC 在编译时可以使用 -ffunction-sections-fdata-sections 将每个函数或符号创建为一个 sections,其中每个 sections 名与 function 或 data 名保持一致。而在链接阶段, -Wl,–gc-sections 指示链接器去掉不用的section(其中-wl, 表示后面的参数 -gc-sections 传递给链接器),这样就能减少最终的可执行程序的大小了。

我们常常使用下面的配置启用这个功能:

CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections

三、栗子

#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 文件如下: 

main_sections:
        gcc -ffunction-sections -fdata-sections -c main.c
        gcc -Wl,--gc-sections -o $@ main.o 
 
main_normal:
        gcc -c main.c
        gcc -o $@ main.o
 
clean:
        rm -rf *.o main_sections main_normal

执行

make  main_sections

make  main_normal

 可以发现文件大小 main_sections 比 main_normal 小。

 四、分析 sections 

 1、查看 setions 信息

(1)执行 main_sections 对应 main.o

readelf -t main.o

( -t 选项是打印出 sections 信息 ) 

结果:

从 object 文件中可以发现,fun_0 ~ fun_3 每个函数都是一个独立的 section 。

(2)执行 main_normal 对应 main.o

可以发现 所有 fun_* 共享一个默认的 sections(.text)。

2、分析 elf 文件。

readelf -a main_normal | grep fun_ 

结果:

 

readelf -a main_sections | grep fun_ 

结果: 

 

可以看见,在最终的目标文件中,未使用的函数并未被链接进最终的目标文件。

转载:gcc之 -ffunction-sections_springcrazy的博客-优快云博客

 

(SAW:Game Over!) 

 

 

 

 

elro -ffunction-sections -fdata-sections" /home/ubuntu/tina-v821-release/prebuilt/hostbuilt/make4.1/bin/make -C /home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/hostapd AR="riscv32-linux-musl-ar" AS="riscv32-linux-musl-gcc -c -Os -pipe -fno-caller-saves -fno-plt -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Wformat -Werror=format-security -DPIC -fpic -D_FORTIFY_SOURCE=2 -Wl,-z,now -Wl,-z,relro -ffunction-sections -fdata-sections" LD=riscv32-linux-musl-ld NM="riscv32-linux-musl-nm" CC="riscv32-linux-musl-gcc" GCC="riscv32-linux-musl-gcc" CXX="riscv32-linux-musl-g++" RANLIB="riscv32-linux-musl-ranlib" STRIP=riscv32-linux-musl-strip OBJCOPY=riscv32-linux-musl-objcopy OBJDUMP=riscv32-linux-musl-objdump SIZE=riscv32-linux-musl-size CONFIG_ACS=y CONFIG_DRIVER_NL80211=y CONFIG_IEEE80211N= CONFIG_IEEE80211AC= CONFIG_IEEE80211AX= CONFIG_DRIVER_WEXT= CONFIG_WEP=y LIBS="-L/home/ubuntu/tina-v821-release/prebuilt/rootfsbuilt/riscv/nds32le-linux-musl-v5d//sysroot/usr/lib -DPIC -fpic -specs=/home/ubuntu/tina-v821-release/openwrt/openwrt/include/hardened-ld-pie.specs -znow -zrelro -Wl,--gc-sections -fuse-linker-plugin -lubox -lubus -lm -lnl-tiny" LIBS_c="" AR="riscv32-linux-musl-ar" BCHECK= hostapd hostapd_cli make[4]: Entering directory '/home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/hostapd' In file included from main.c:26: /home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/src/ap/hostapd.h:84:21: error: field 'ubus' has incomplete type 84 | struct ubus_object ubus; | ^~~~ Makefile:1302: recipe for target 'main.o' failed make[4]: *** [main.o] Error 1 make[4]: Leaving directory '/home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/hostapd' Makefile:708: recipe for target '/home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/.built' failed make[3]: *** [/home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/.built] Error 2 make[3]: Leaving directory '/home/ubuntu/tina-v821-release/openwrt/openwrt/package/network/services/hostapd' time: package/network/services/hostapd/full-internal/compile#0.71#0.22#0.79#1751822626.32#1751822627.10 ERROR: package/network/services/hostapd failed to build (build variant: full-internal). More error defails please refer to: /home/ubuntu/tina-v821-release/openwrt/openwrt/build_log/package/network/services/hostapd/full-internal/compile.txt package/Makefile:114: recipe for target 'package/network/services/hostapd/compile' failed make[2]: *** [package/network/services/hostapd/compile] Error 1 make[2]: Leaving directory '/home/ubuntu/tina-v821-release/openwrt/openwrt' package/Makefile:110: recipe for target '/home/ubuntu/tina-v821-release/openwrt/openwrt/staging_dir/target/stamp/.package_compile' failed make[1]: *** [/home/ubuntu/tina-v821-release/openwrt/openwrt/staging_dir/target/stamp/.package_compile] Error 2 make[1]: Leaving directory '/home/ubuntu/tina-v821-release/openwrt/openwrt' /home/ubuntu/tina-v821-release/openwrt/openwrt/include/toplevel.mk:238: recipe for target 'world' failed make: *** [world] Error 2 make: Leaving directory '/home/ubuntu/tina-v821-release/openwrt/openwrt' INFO: build_openwrt_rootfs failed 编译tianlinux的时候报错如上,帮我看看怎么办?
最新发布
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值