根据.config剥离Linux内核源代码 [英] Strip Linux kernel sources according to .config
问题描述
是否有任何有效的方法(也许是通过滥用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?
推荐答案
很好地采取了一些措施.
Well got some steps into a solution.
首先,可以通过
make KBUILD_VERBOSE=1 | tee build.log
grep '^ gcc' build.log
现在,我仅选择一个gcc命令行以进行进一步的操作.例如,kernel/kmod.c的构建看起来像:
For now, I select only one gcc command line for further steps. For example the build of kernel/kmod.c, it looks like:
gcc <LIST OF MANY OPTIONS> -c -o kernel/kmod.o kernel/kmod.c
我现在删除选项-c
,-o ...
并添加-E
,从而禁用编译并将预处理器输出写入屏幕.此外,我添加了-fdirectives-only
来防止宏扩展,并添加了-undef
来删除GNU定义的宏定义.内核makefile已添加-nostdinc
删除标准c标头.
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.
现在包括仍然包括在内,因此在预处理器输出上进行了扩展.因此,我通过grep通过管道将输入文件删除:grep -v '#include' kernel/kmod.c
.现在只剩下一个include:Makefile的命令行中包含了autoconf.h.这很棒,因为它实际上定义了#ifdef CONFIG_...
用来选择活动内核代码的宏.
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.
剩下的唯一事情就是通过grep -v '^#'
从autoconf.h中过滤出预处理程序注释和其余的#define
.
The only thing left is to filter out the preprocessor comments and the remaining #define
s from autoconf.h by means of grep -v '^#'
.
整个管道看起来像:
grep -v '#include' kernel/kmod.c | gcc -E -fdirectives-only -undef <ORIGINAL KERNEL BUILD GCC OPTIONS WITHOUT -c AND -o ...> - |grep -v '^#'
,结果是kernel/kmod.c的过滤版本,其中包含实际上内置在kmod.o中的代码.
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?
这篇关于根据.config剥离Linux内核源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!