Home > database >  Linux Kernel Module ignores main module file when an additional source file is added [duplicate]
Linux Kernel Module ignores main module file when an additional source file is added [duplicate]

Time:09-30

I'm trying to build a loadable kernel module that includes another source file. I have the following in a Makefile or Kbuild file:

obj-m  = mymodule.o
mymodule-y  = other_src_file.o

In this scenario, other_src_file.c will be compiled. Strangely, the main source file mymodule.c will not be compiled. Intentional syntax errors are not caught. An object file mymodule.o is still produced, as is the .KO file. Loading this module on the target platform has no effect.

If I instead remove the second line in the Makefile/Kbuild that includes the other source file, my intentional syntax errors are caught. In a minimal example, init_module() will run and dmesg shows what I put into printk. It would not print anything prior to removing the line with other_src_file.o, despite being unchanged.

So what I find is that by including an additional source file (whether it is being used or not), the main module/C file is effectively ignored. An LKM is produced, but it has no effect from what I can see. Using --debug confirms in the latter case that mymodule.c is used (pipe into grep returns literally anything) whereas the former shows that there is not a single reference to mymodule.c (but many to other_src_file.c)

I've also tried setting up the makefile as the following, but there's no behavioral difference.

obj-m  = mymodule.o
mymodule-y  = other_src_file.o

all:
    make -C ../../../ M=($PWD) modules # -C points to the root of my kernel
clean: 
    clean -c ../../../ M=$(PWD) clean

The output of make looks like the following:

LD some/path/mymodule/built-in.o
CC[M] /some/path/mymodule/other_src_file.o <-- notice it's the only CC; nothing for mymodule.o
LD[M] /some/path/mymodule/mymodule.o
Building modules, stage 2
MODPOST 1 modules
CC /some/path/mymodule/mymodule.mod.o
LD[M] /some/path/mymodule/mymodule.ko

When that other src file is left out, there is a line that shows mymodule.o being compiled.

I'm running in an Ubuntu 20.04 (VM) on x86_64. The kernel is 3.1.10, make is 4.2.1.

I feel like I'm missing something simple (unfamiliar with linux building, fairly familiar with C and compiling otherwise). Would greatly appreciate a pointer here.

CodePudding user response:

The line

obj-m  = mymodule.o

tells KBuild system just to build a module named mymodule.

The sources compiled into that module depend from variable mymodule-y:

  • If the variable is set (like in your code), then source list it taken only from this variable. There is no "automatic" addition of mymodule.c source.
  • If the variable is not set, then, by default, the module is compiled from the source which has the same name.

Note, that one cannot build a module mymodule from several sources, one of which is mymodule.c, that is has the same name as the module itself. Either module or the source file should be renamed. That situation is described in that question.

  • Related