2

Linux is large complex piece of software. A running kernel in the system would not be using all the code.

I'm looking for a tool which can give me the source code needed for the .config I used to build the kernel. I want it to delete all the lines skipped by ifdefs and source files not used for the build, even the files inside the drivers folder.

Is there such a tool?

The reason I'm looking for this is I'm not sure which code is actually built inside the kernel. I agree I can do it manually but I thought not having the unwanted ifdefs will give me a much clearer understanding of the code.

3

The configuration of the Linux kernel eventually defines what the preprocessor directives for the C compiler are. The output from the preprocessed sources should be close to what you are looking for, although there also might be dead code that is optimised out by the compiler.

The gcc compiler can be told to generate the preprocessed output to stdout using -E (and then stop):

  -E  Stop after the preprocessing stage; do not run the compiler
      proper.  The output is in the form of preprocessed source code,
      which is sent to the standard output.

For the whole kernel it might be more easy to incorporate the --save-temps compiler flag:

   -save-temps
   -save-temps=cwd
       Store the usual "temporary" intermediate files permanently; place
       them in the current directory and name them based on the source
       file.  Thus, compiling foo.c with -c -save-temps would produce
       files foo.i and foo.s, as well as foo.o.  This creates a
       preprocessed foo.i output file even though the compiler now
       normally uses an integrated preprocessor.

       When used in combination with the -x command line option,
       -save-temps is sensible enough to avoid over writing an input
       source file with the same extension as an intermediate file.  The
       corresponding intermediate file may be obtained by renaming the
       source file before using -save-temps.
  • for compiling the kernel we use 'make'. How do I use the flag with it? Do I have to place the flag in Makefile? – Vignesh Oct 25 '14 at 16:28
  • @Vignesh no you should add the -save-temps flag to some (environment) variable that is used for all the invocations, like the one for optimizations (which should only be specified once). I currently have no kernel source available to check that, but it should not be too difficult to find which variable to change by looking at one of the Makefiles and the invocation of gcc therein. – Anthon Oct 25 '14 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.