2
votes

This is probably a stupid question, but I looked for hours online and couldn't find an answer...

I'm writing a kernel module that also creates a character device. It compiles with no errors and warnings but when I try sudo insmod my_mod.ko I get:

insmod: error inserting 'my_mod.ko': -1 Unknown symbol in module

and when I try to look at dmesg I see:

my_mod: Unknown symbol __class_create (err 0)
my_mod: Unknown symbol device_create_file (err 0)
my_mod: Unknown symbol device_create (err 0)

I'm guessing that I missed an include but I can't find which...

What are the includes needed?

My includes are currently:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/ip.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/cdev.h>
#include <linux/fs.h>
1
Maybe you missing license, something like that : MODULE_LICENSE("GPL");Dabo
@Dabo I posted a similar answer without reading your comment. If you post yours as an answer, I can delete mine.holgac
@holgac, aaahh, that is ok, +1 :)Dabo
If you were missing some include, you would have compile time error. But what you have is run time error. Basically kernel can't link some of functions you are using with actual functions in kernel code. @holgac explained pretty well why it's happening in his answer.Sam Protsenko

1 Answers

11
votes

The function __class_create is exported only for GPL modules (exported with EXPORT_SYMBOL_GPL). So, you need to use a GPL license with MODULE_LICENSE macro to make use of that function. Same goes for other functions as well.

This should do the trick:

MODULE_LICENSE("GPL");

To learn about what exporting is, take a look at here. Basically, dynamic modules do not have access to variables and functions in kernel, and kernel needs to specify what to export, to enable access. That's the purpose of EXPORT_SYMBOL and EXPORT_SYMBOL_GPL macros, which are used everywhere.

And the difference between EXPORT_SYMBOL and EXPORT_SYMBOL_GPL is that the latter only reveals the function or the variable if the module is GPL licensed.