Welcome to the Linux Foundation Forum!

Error building linux kernel module (Invalid module format)

Hello, currently I began learning linux kernel programming and I faced the following problem. After successfully creating and compiling some modules I made a new directory that contained two files, my_module.c (containing just init and exit functions with pr_info) and Makefile. Accidentally on this particular Makefile I didn't put M= before ${PWM} like in the example below.

...
all:
make -C /lib/modules/$(uname -r)/build ${PWD} modules
...

Before even realizing, I had tried to build this module as root. Getting the following
result.

make[1]: Entering directory '/usr/src/linux-headers-5.11.0-41-generic'
SYNC include/config/auto.conf.cmd
HOSTCC scripts/basics/fixdep
HOSTCC scripts/kconfig/conf.o
HOSTCC scripts/kconfig/confdata.o
HOSTCC scripts/kconfig/expr.o
LEX scripts/kconfig/lexer.lex.c
YACC scripts/kconfig/parser.tab.[ch]
HOSTCC scripts/kconfig/lexer.lex.o
HOSTCC scripts/kconfig/parser.tab.o
HOSTCC scripts/kconfig/preprocess.o
HOSTCC scripts/kconfig/symbol.o
HOSTCC scripts/kconfig/util.o
HOSTLD scripts/kconfig/cong
make[2]: *** No rule to make target 'arch/x86/tools/relocs_32.c', need by 'arch/x86/tools/relocs_32.o'. Stop.
make[1]: *** [arch/x86/Makefile:211: archscripts] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.11.0-41-generic'
make: *** [Makefile:4: all] Error 2

But the actual problem isn't this. After putting M= on the Makefile and trying to build my module again, I cannot insmode it, since if I try to insmod that module I get this error

insmod: ERROR: could not insert module my_module.ko: Invalid module format

Moreover, on dmesg I see the following output.

module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1, loc 000000001528b80b, val ffffffffc0797000

Now, none of the previously running modules (that I made) can be build and run. Does anyone know how I could fix this error? Thank you in advance!

Categories

Upcoming Training