Strip Linux kernel sources according to .config

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Strip Linux kernel sources according to .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?




3 Answers
3



Well got some steps into a solution.



First, one can obtain the used compiler commands by


make KBUILD_VERBOSE=1 | tee build.log
grep '^ gcc' build.log



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



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.


-c


-o ...


-E


-fdirectives-only


-undef


-nostdinc



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 '#include' kernel/kmod.c


#ifdef CONFIG_...



The only thing left is to filter out the preprocessor comments and the remaining #defines from autoconf.h by means of grep -v '^#'.


#define


grep -v '^#'



The whole pipe looks like:


grep -v '#include' kernel/kmod.c | gcc -E -fdirectives-only -undef <ORIGINAL KERNEL BUILD GCC OPTIONS WITHOUT -c AND -o ...> - |grep -v '^#'



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?



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.





This won't work as many #ifdef... statements are evaluated inside the source files thus unused files and almost unused files are compiled too.
– dronus
Sep 9 '11 at 10:47





@dronus: That's the only simple way that I can think of (The next is, basically, writing or modifying a C preprocessor). It should remove things like unused modules and archs. Do you really need it to be more accurate? What are you trying to achieve?
– Piotr Praszmo
Sep 9 '11 at 11:09





Well I just like to get some thrustable overview about how the kernel works, but its nearly impossible to read hundred MBs of code. So I like to build a minimal kernel and read the real used code of it.
– dronus
Sep 13 '11 at 20:36



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 :


cd /kernel/sources
make
wget https://github.com/Hitachi-India-Pvt-Ltd-RD/minimization/raw/master/minimize.py
export PATH=$PATH:`pwd`
make C=2 CHECK=minimize.py CF="-mindir ../path-to-minimized-source-tree/"



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


make localmodconfig





Wow, I will have to look into it.
– dronus
Aug 18 at 11:14






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered