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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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-onlyto prevent macro expansion and-undefto remove the GNU defined macro definitions.-nostdincto 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
#defines 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?