Making the Kernel free of “missing-prototypes”

As I mentioned in my last blog-post, I began working on my project “Tree-wide warning fixes and static analysis enhancements”. I started with the task of elimination of all instances of the warning:

warning: no previous prototype for 'name_of_symbol' [-Wmissing-prototypes]

The kernel build log has many such occurrences. This warning is a non-default GCC warning. So, the question arises:

question How do you generate these warnings?

The answer is simple. You just need to make a little change to your Makefile i.e. add -Wmissing-prototypes to KBUILD_CFLAGS in the top-level Makefile and do a build with defconfig or allyesconfig (If -Wmissing-prototypes is specified, it tells the compiler to detect global functions that are defined without a previous prototype declaration).
Voila!, you will notice a pile of new warnings about symbols with no prototype declarations. Alternatively, you can build the kernel with W=1.

One more thing which is important while building a kernel, is to save the log (This will help you to deal with the log conveniently). To do this, use the following command:

make 2>&1 | tee /path/to/build.log

question Why this warning?

These warnings are particularly useful to remove unused code from the kernel and thus making the kernel much smaller for everyone. These also help to detect any ambiguity related to prototype type mismatch which is a real bug and can even break the kernel build.

question How to eliminate it?

There are various ways of removing this warning. Umm, before that we need to learn one more thing, which is to find the occurrences of a particular symbol. This is easily done by the powerful tool GIT, the command being:

git grep -w <symbol-name>

Alternatively, you can find a header file containing that symbol using:

git grep -w <symbol-name> -- '*.h'

Then for each symbol, check if the symbol is used elsewhere in the kernel, and apply the appropriate fix:

– If it isn’t used anywhere else, mark it “static”.
– If it is defined and not used anywhere else, remove the function definition.
– If it is used elsewhere and has a prototype in a header file, include that header file in the source file.
– If it is used elsewhere and doesn’t have a prototype in a header file, typically because the callers elsewhere use an explicit prototype in a .c file, then you should either find an appropriate header file to put the prototype in, create one, or as a last resort add a prototype right above the definition to silence the warning.

After making these changes, you submit the edited portion of the file(s) in the form of a patch to appropriate recipients as indicated by script. This script informs you about whom the patches have to be send to and why. To learn more about sending patches to the kernel’s mailing list, you can take the help of this easy-to-follow tutorial Linux Kernel: Submit a patch.

For now, I plan to eliminate *all* these instances by the next time I write a blog-post 😀 and move on to test these warnings on different architectures.
I will keep you posted about more such interesting things which I will keep learning during my internship. Till then, good-bye 🙂



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s