• We did not send an email asking for donations - please read this post.

Finding all the kernel source files used when compiling against a certain config file

knave

New Member
Joined
Sep 13, 2022
Messages
5
Reaction score
0
Credits
76
I am having some issues with finding all the kernel source files used when compiling against a certain config file? I do not want the full kernel source tree, just the one that went into building the kernel against a certain config setting, like make defconfig or make allnoconfig, etc...

Am I missing something obvious here? I feel like I am breaking my back trying to find an answer to this question.

All I want to do is to be able to view/read all of the source files (.h.c.S) that went into compiling the kernel. I don't want all the source files in the kernel directory, just the ones that went into building the kernel against my config file.

I assume that different config files use different source files when compiling... Due to the different options selected.

Thanks guys!
 


dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
1,561
Reaction score
1,182
Credits
9,934
It kind of depends on the distro. But for the most part, all you really need to compile the kernel
is the kernel source file. Of course you need the development tools and a dozens of dev libraries.
Assuming you have everything else already in place (i.e glibc, gcc, clang, etc... ) the kernel will just compile.
The only external header file I usually ever need is zlib-devel.

Almost all kernel modules are dynamic these days, they are typically external modules that get loaded
"on the fly" when they are needed. But for the most part, just one config file, just one source file.
That's really it. Of course the kernel is huge. It can take several hours to compile it depending on your
hardware.
 

osprey

Active Member
Joined
Apr 15, 2022
Messages
249
Reaction score
148
Credits
2,286
knave wrote:
All I want to do is to be able to view/read all of the source files (.h.c.S) that went into compiling the kernel. I don't want all the source files in the kernel directory, just the ones that went into building the kernel against my config file.

I assume that different config files use different source files when compiling... Due to the different options selected.

The task you have set yourself to just read just what "went into compiling the kernel" does not seem like a simple matter to me.

The first thing worth noting is that if you want the kernel source files, you will most easily see the kernel source from an unpacked kernel source tarball. It's possible to chase up the source from various git repositories, but as I hope to show in the following, it may not be so practical.

Looking just at the statistics of the task, it's not small. If you take a config file for the kernel, which is usually available on a working system in /boot, the number of configured elements is considerable. For example on this machine I'm writing on:
Code:
[[email protected] /boot]$ wc -l config-5.19.0-1-amd64
10516 config-5.19.0-1-amd64
With one configuration per line, there are 10,516 configuration elements that the kernel has to negotiate to be compiled. Not all the elements are set, so one can take out all those config variables that are not set by removing the configs that are commented out in that config file, config-5.19.0-1-amd64, which is shown by lines that begin with #, for example:
Code:
[[email protected] /boot]$ sed '/#/d' config-5.19.0-1-amd64 > /home/flop/configsSet
wc -l /home/flop/configsSet
6854 /home/flop/configsSet
This kernel has compiled for 6854 config elements that were set in the initial configuration specification.

Next comes the task of finding the header and other code files for those 6854 configured elements. The most straight forward way would be to look at the source of the kernel, in this example, kernel 5.19.0-1-amd64. The source is available in the relevant kernel tarball which can be downloaded from kernel.org.

The relevant code for each config needs to be found. Take for example the configuration for the ethernet device which is usually compiled into standard kernels: CONFIG_ETHERNET=y. With the kernel source tarball unpacked, one might navigate to the include files and search for etheret related code:
Code:
[[email protected] ~/src/linux-5.19.0/include/linux]$ ls | grep -i eth
etherdevice.h
ethtool.h
ethtool_netlink.h
if_ether.h
mv643xx_eth.h
pxa168_eth.h
rethook.h
sh_eth.h
It looks like there's a few relevant files with "etherdevice.h" looking most promising. Reading the file itself, one comes across a reference to another configuration which it refers to: CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS. A look back at the original config file: config-5.19.0-1-amd64, shows this configuration to also be compiled into the kernel, so that will likely have to be followed up to be read to get a fuller picture of what the software is doing.

But wait, there's more.
Code:
[[email protected] ~/src/linux-5.19.0/include/net]$ ls | grep -i eth
ethoc.h
It's another file of code relevant to ethernet in another directory of the kernel source.

After discovering and reading all the ethernet code, there's still over 6000 other configurations to follow up. There will some overlap to mollify the task, but not having done it all myself I can't say by how much.

The kernel source unpacked takes up about 1.4 gig. The documentation is about 62 meg. There's heaps of driver code which is compiled into kernels when only a few drivers are generally needed for any particular machine. As for finding all code and documentation relevant to ethernet, including the driver code for the particular relevant NICs, I shall leave that as an exercise for the reader.

The point is, it's complicated. Doubtless someone very familiar with any particular configuration element could direct one efficiently to the relevant source code, but for the ordinary interested punter on the hunt, it may well be quite a different matter.

knave asked:
Am I missing something obvious here?
Maybe.

That all said, it should be noted that when configuring a kernel and setting a config such as CONFIG_ETHERNET=y, that there will be a file that references all the files that are necessary to enable the ethernet capabilities, and that file would potentially be the one to start with in a search for what is involved. As for finding it, again, I'll leave that to the interested reader.
 
Last edited:

gvisoc

Well-Known Member
Joined
May 29, 2020
Messages
446
Reaction score
535
Credits
4,570
The easiest option that comes to mind without actually searching for a proper scripted existing solution is the following:

1. Compile the kernel.
2. After compiling the kernel, so some ls and grep to get the .o and .ko generated files in the kernel tree
3. from there, using the same names, look for .c and .h

I am sure that there are some sorceries you can do with ls, grep, sed and awk to get it in a one-liner, but my wizardry is nil.

Now, if you want to actually search for a proper solution, begin by examining the perl and python scripts that come with the kernel, and also the configure helper scripts. I am inclined to think that this can be extracted from a regular usage of those.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online

No members online now.

Top