2

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>
4
  • 1
    Maybe you missing license, something like that : MODULE_LICENSE("GPL");
    – Dabo
    Apr 11, 2015 at 14:25
  • 1
    @Dabo I posted a similar answer without reading your comment. If you post yours as an answer, I can delete mine.
    – holgac
    Apr 11, 2015 at 14:29
  • 2
    @holgac, aaahh, that is ok, +1 :)
    – Dabo
    Apr 11, 2015 at 14:32
  • 2
    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. Apr 11, 2015 at 14:37

1 Answer 1

11

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.

2

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.