LLVM代码空间优化(二)去除不使用函数

news/2024/7/3 14:33:14 标签: 代码空间, LLVM, 编译器

目录

接上文
LLVM代码空间优化(一) 编译器自带的优化选项

英文原版的-Wl,–gc-sections解释说明如下:

6.3.3.2 Compilation options
The operation of eliminating the unused code and data from the final executable is directly performed by the linker.
In order to do this, it has to work with objects compiled with the following options: -ffunction-sections -fdata-sections.
These options are usable with C and Ada files. They will place respectively each function or data in a separate section in the resulting object file.
Once the objects and static libraries are created with these options, the linker can perform the dead code elimination. You can do this by setting the -Wl,–gc-sections option to gcc command or in the -largs section of gnatmake. This will perform a garbage collection of code and data never referenced.
If the linker performs a partial link (-r linker option), then you will need to provide the entry point using the -e / --entry linker option.
Note that objects compiled without the -ffunction-sections and -fdata-sections options can still be linked with the executable. However, no dead code elimination will be performed on those objects (they will be linked as is).

The GNAT static library is now compiled with -ffunction-sections and -fdata-sections on some platforms. This allows you to eliminate the unused code and data of the GNAT library from your executable.

大致说明如下:

  • 在编译C、Ada源文件(C++也可以),在gcc/g++编译选项中增加-ffunction-sections、-fdata-sections,在编译生成的.o目标文件中,会将每个函数或数据段,放在各种单独独立的section中;
  • 在链接生成最终可执行文件时,如果带有-Wl,–gc-sections参数,并且之前编译目标文件时带有-ffunction-sections、-fdata-sections参数,则链接器ld不会链接未使用的函数,从而减小可执行文件大小;
  • 如果使用了-r的链接参数,来产生重定位的输出,需要显示的调用-e参数来指定程序入口。否则-Wl,–gc-sections不会生效。

使用方式:需要在编译的时候,加入CFLAG += -ffunction-sections, -fdata-sections 选项,在链接的时候,加入LDFLAG += --gc-sections选项

工作原理:编译的时候,把每个函数作为一个section,每个数据(应该是指全局变量之类的吧)也作为一个section,这样链接的时候,–gc-sections会把没用到的section丢弃掉,最终的可执行文件就只包含用到了的函数和数据。
也就是说,链接的单位,是函数级别,这样就能丢弃没使用的函数。如果不加-ffunction-sections选项,则默认似乎是每个源文件为一个section进行链接,这样子只要这个文件中用到了一个函数,那么所有的函数都会被链接进来。


http://www.niftyadmin.cn/n/316054.html

相关文章

教你如何正确使用ChatGPT

目录 前言 一、ChatGPT Sidebar 二、免费镜像 三、共享账号 总结 前言 ChatGPT 是一种基于深度学习技术的自然语言处理工具,能够用于文本生成、语言翻译等任务。然而,其使用需要一定的技术基础和相关知识,不少用户可能会遇到一些问题。…

【Java AWT 图形界面编程】IntelliJ IDEA 乱码问题最佳配置方案 ( 配置文件编码 | 配置编译器编码参数 | 配置运行时编码参数 )

文章目录 一、IntelliJ IDEA 乱码问题二、IntelliJ IDEA 乱码问题最佳配置方案1、文件编码设置成 UTF-82、编译器编码参数设置成 UTF-83、 配置运行时编码参数为 GBK 一、IntelliJ IDEA 乱码问题 在 IntelliJ IDEA 中开发 AWT / Swing 图形界面程序 , 经常遇到乱码问题 ; 文件…

【Linux0.11代码分析】06 之 kernel 1号进程 init() 代码分析

【Linux0.11代码分析】06 之 kernel 1号进程 init 代码分析 系列文章如下: 系列文章汇总:《【Linux0.11代码分析】之 系列文章链接汇总(全)》 . 1.《【Linux0.11代码分析】01 之 代码目录分析》 2.《【Linux0.11代码分析】02 之 b…

常见自动化测试工具及框架有哪些?怎么选?

目录 一、自动化测试简介 1、什么是自动化测试 2、它可以做自动化测试么 二、工具篇 1、UFT(QTP) 2、Selenium 3、Appium 4、工具对比 三、框架篇 1、TestNG 2、unittest 3、pytest 4、Robot Framework 5、框架对比 四、如何选择 总结 一…

改进YOLOv8 | 特征融合篇 | YOLOv8 应用 BiFPN 结构 | 《 EfficientDet: 可扩展和高效的目标检测》

模型效率在计算机视觉中变得越来越重要。在本文中,我们系统地研究了目标检测中的神经网络架构设计选择,并提出了几种关键的优化方法来提高效率。首先,我们提出了一种加权双向特征金字塔网络(BiFPN),它可以实现简单快速的多尺度特征融合;其次,我们提出了一种复合缩放方法…

11-CSS-概述、与HTML的结合方式

一、概述 CSS(层叠样式表)是一种用于控制网页外观和布局的样式语言。它可以独立于 HTML 或 XHTML 文档,以及任何标记语言使用,因此可以用于设计不同类型的文档,如 XML、SVG、XUL 等。CSS 提供了广泛的样式选择器&…

自动化测试框架搭建步骤教程

说起自动化测试,我想大家都会有个疑问,要不要做自动化测试? 自动化测试给我们带来的收益是否会超出在建设时所投入的成本,这个嘛别说是我,即便是高手也很难回答,自动化测试的初衷是美好的,而测试…

django常用命令/vue-cli图形化界面创建方式/vueo-cli命令行创建方式/若依框架/elementui

django常用命令 django-admin.exe startproject mysite python .\manage.py startapp app01 python manage.py makemigrations python manage.py migrate python manage.py runserver python manage.py runserver 0.0.0.0:8000 from django.shortcuts import render,Http…