根据.config剥离Linux内核源代码
是否有任何有效的方法(也许通过滥用 gcc 预处理器?)来获取一组剥离的内核源代码,其中根据 .config 不需要的所有代码都被省略?
Is there any efficient way (maybe by abusing the gcc preprocessor?) to get a set of stripped kernel sources where all code not needed according to .config is left out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我们采取了一些解决方案。
首先,可以通过以下方式获取所使用的编译器命令
。目前,我仅选择一个 gcc 命令行来进行后续步骤。例如 kernel/kmod.c 的构建,它看起来像:
我现在删除选项
-c
,-o ...
并添加-E< /code>,从而禁用编译并将预处理器输出写入屏幕。此外,我添加
-fdirectives-only
以防止宏扩展,并添加-undef
以删除 GNU 定义的宏定义。内核 makefile 已添加用于删除标准 C 头文件的-nostdinc
。现在,包含仍然包含在内,因此在预处理器输出上进行了扩展。因此,我通过 grep 管道输入文件来删除它们:
grep -v '#include' kernel/kmod.c
。现在只剩下一个包含:autoconf.h 由 Makefile 的命令行包含。这很棒,因为它实际上定义了#ifdef CONFIG_...
使用的宏来选择活动内核代码。剩下的唯一事情就是通过
grep -v '^#'
从 autoconf.h 中过滤掉预处理器注释和剩余的#define
。整个管道看起来像:
结果是 kernel/kmod.c 的过滤版本,其中包含实际构建到 kmod.o 中的代码。
问题仍然存在:如何对整个源树执行此操作?是否存在实际构建但从未使用并在链接时被剥离的文件?
Well got some steps into a solution.
First, one can obtain the used compiler commands by
For now, I select only one gcc command line for further steps. For example the build of kernel/kmod.c, it looks like:
I now remove the option
-c
,-o ...
and add-E
, thus disabling compilation and writing preprocessor output to the screen. Further I add-fdirectives-only
to prevent macro expansion and-undef
to remove the GNU defined macro definitions.-nostdinc
to remove the standard c headers is already added by the kernel makefile.Now includes are still included and thus expanded on the preprocessor output. Thus I pipe the input file through grep removing them:
grep -v '#include' kernel/kmod.c
. Now only one include is left: autoconf.h is included by the Makefile's command line. This is great as it actually defines the macros used by#ifdef CONFIG_...
to select the active kernel code.The only thing left is to filter out the preprocessor comments and the remaining
#define
s from autoconf.h by means ofgrep -v '^#'
.The whole pipe looks like:
and the result is a filtered version of kernel/kmod.c containing the code that is actually build into kmod.o.
Questions remain: How to do that for the whole source tree? Are there files that are actually build but never used and stripped at linking?
内核最小化脚本:
受此问题启发的项目并提供了一个简单的答案......
它包含一个 Python 脚本,可在构建期间生成最小化的源代码。新的最小化源树将仅包含使用过的源。 (项目页面)
:
信息 脚本已在内核 v4.14.x 上进行了测试,但是从生成的最小化源中再次构建内核需要复制 make 文件和 Kconfig 文件等...至少我们可以轻松隔离仅使用的源以进行调查和开发
用法:
注释&提醒:
如果我们在目标机器内部或针对目标机器进行构建,我们还可以使用
make localmodconfig
命令来缩小当前配置文件,仅包含当前使用的模块(如果在“最小化”之前使用)它将产生更多的剥离源Kernel Minimization Script :
A project inspired by this question and providing an easy answer...
It contains a Python script that generate a minimized sources code during build time. The new minimized source tree will only contain used sources. (project page)
Info :
The script is tested working with the kernel v4.14.x, however building the kernel one more time from those generated minimized sources require to copy make files and Kconfig files etc... at least we could easily isolate only used source for investigations and development
Usage :
Note & Reminder :
If we are building within and against the targeted machine, we also have the
make localmodconfig
command that shrink the current config file with only the currently used modules, if used before "Minimization" it will generate further more stripped sources编译所有内容并使用 atime 找出哪些文件未使用。它可能不是很准确,但可能值得一试。
Compile everything and use atime to find out which files were not used. It might not be very accurate but it's probably worth a try.