Home > Back-end >  Can't get multi files kernel module to work
Can't get multi files kernel module to work

Time:11-24

I'm trying to build a loadable kernel module using multiple source files. According to section 3.3 of https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt I've got to use obj-m for main object file and modulename-y for the rest of it. Here's my mwe:

helpers.h

#ifndef __HELPERS_H__
#define __HELPERS_H__

void helper_print_init(void);
void helper_print_exit(void); 

#endif // __HELPERS_H__

helpers.c

#include "helpers.h"
#include <linux/kernel.h>

void helper_print_init(void) {
    printk("multi_file_ko_init_helper\n");
}

void helper_print_exit(void) {
    printk("multi_file_ko_exit_helper\n");
}

multiFileKo.c

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>

//#include "helpers.h"

static int __init multi_file_ko_init(void) {
    printk("multi_file_ko_init\n");
//    helper_print_init();
    return 0;
}

static void __exit multi_file_ko_exit(void) {
    printk("multi_file_ko_exit\n");
//    helper_print_exit();
}

module_init(multi_file_ko_init);
module_exit(multi_file_ko_exit);

MODULE_LICENSE("MIT");
MODULE_AUTHOR("AUTHOR");
MODULE_DESCRIPTION("gpio");
MODULE_VERSION("0.0");

NOTE THAT multiFileKo.c does not even actually uses helpers for now. I tried to actually call those functions but for simplicity just commented things out from mwe.

Now if I compile it with kbuild like follows, using only main file, I get dmesg output as expected:

obj-m := multiFileKo.o

But when I try to compile it linked with helpers, even without actually using them as follows, dmesg remains silent even though insmod/rmmod seem to be working:

obj-m := multiFileKo.o
multiFileKo-y := helpers.o

Obviously if I uncomment everything in multiFileKo.c it does not work either. So the fact of linking additional object file seems to be breaking my module regardless of what that additional object file does.

Approach with multiFileKo-objs does not work for me either. I saw this earlier, but sure where this takes it's origin, since makefiles manual uses it only in context of host programs.

CodePudding user response:

Info which lead to solution was provided by @Tsyvarev. Original info may be found in comments to first post.

obj-m := multiFileKo.o defines the name of module. It also uses multiFileKo.c as a source file by default. But this principle works for single-source-file modules only.

In case multiple source files are used to create a module, obj-m := multiFileKo.o shall define module name, but not the sources. ALL object files (referencing actual sources) then shall be listed in multiFileKo-objs := list. Source files are not allowed to have the same name which module have.

From my experiments and make utility manual I can also say that it seems that listing sources in multiFileKo-y := also works. Maybe -obj is still working due to compatibility, since make documents now advise to use -y list.

To sum up, correct approach would be:

obj-m := multiFileKo.o
multiFileKo-y := multiFileKo_main.o helpers.o

Source files:

multiFileKo_main.c // contains init and exit functions
helpers.c

Output would be stored in multiFileKo.ko

  • Related