LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices


Reply
  Search this Thread
Old 02-14-2015, 10:05 PM   #1
linux_walt
Member
 
Registered: Dec 2014
Location: Houston, TX
Distribution: Debian wheezy
Posts: 127

Rep: Reputation: 29
compiling modules ok, but error when installing: Invalid module format


Hello, I am working through the first example in Linux Device Drivers, Third Edition, with updated source code from a repository on github. Below are the steps I followed, which lead to a version mismatch.

Clone book examples repository
https://github.com/duxing2007/ldd3-examples-3.x

$ git tag
v3.0
v3.0.1
v3.10
v3.12
v3.14
v3.2
v3.4

$ git checkout v3.4

My kernel module tree is linux-sunxi:
3.4.103-00033-g9a1cd03

I used this module tree when building an SD card image, however the system is not running on that image at the moment. The running system's Linux version (Debian wheezy) is:

$ uname -r
3.4.79

The module (hello.c) is compiling fine, however when I go to install it I get this error:

$ sudo insmod hello.ko
Error: could not insert module hello.ko: Invalid module format

Now that I have laid everything out, maybe I see the problem. Is it because the running kernel is 3.4.79, and the module was built using kernel sources from 3.4.103?

Here is a list of the tags in my local linux-sunxi git repository:
3.4.103-00033-g9a1cd03$ git tag
latest
lichee3-i-onik.de-a10_source_1.5
lichee3-i-onik.de-a13_source_1.5
lichee3-qware
linaro-packaging
...
...
v3.4.7
v3.5

Since the running system is Linux 3.4.79, anything I try to install in it should be version <= 3.4.79, or should it be 3.4.0 < my stuff < 3.4.79?

Thanks!

Last edited by linux_walt; 02-14-2015 at 10:14 PM.
 
Old 02-14-2015, 10:51 PM   #2
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Rep: Reputation: 85
I have a high suspicion the problem is your module's version magic string, a static version string, is not correct. Please run and give the output of the following.

Code:
modinfo hello.ko
And you will probably see that it doesn't match the output you gave earlier.

Code:
$ uname -r
3.4.79
Please verify, and I will be happy to help you resolve the issue.

Last edited by fakie_flip; 02-14-2015 at 10:51 PM. Reason: grammar, missing a single quote
 
Old 02-14-2015, 11:04 PM   #3
linux_walt
Member
 
Registered: Dec 2014
Location: Houston, TX
Distribution: Debian wheezy
Posts: 127

Original Poster
Rep: Reputation: 29
Hi flip, thanks for the input. Indeed it does not match:
Code:
~/src/modules/ldd3-examples-3.x/misc-modules$ sudo modinfo hello.ko
[sudo] password for walter:
filename:       /home/walter/src/modules/ldd3-examples-3.x/misc-modules/hello.ko
license:        Dual BSD/GPL
srcversion:     31FE72DA6A560C890FF9B3F
depends:
vermagic:       3.4.103-00033-g9a1cd03 SMP preempt mod_unload modversions ARMv7 p2v8
I have a directory in my Debian wheezy system, that I used to make a card image. This directory holds the kernel files for 3.4.103-00033-g9a1cd03. Since it's the only module tree I have, I used it to compile the 'hello.c' module.

Now the hello.ko is not loading in the real, running system, v3.4.79 .

I'm in the process of re-cloning the linux-sunxi repository (I messed mine up). The plan is -not- to use the 3.4.103 from it, but checkout the 3.4.7 tag. Build it, then retry building the 'hello.c' module again. Does that sound correct?

Last edited by linux_walt; 02-14-2015 at 11:45 PM.
 
Old 02-15-2015, 12:05 AM   #4
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Rep: Reputation: 85
Your running kernel version 3.4.79 and your kernel modules version 3.4.103 are definately a mismatch, so we have verified the problem.

Have a read here:

http://www.tldp.org/LDP/lkmpg/2.6/html/x380.html

You do not need that git repository you are using for a hello world kernel module. You do need the kernel sources. If you are running the default generic debian kernel from its repositories, be sure to have the exact same source tree for that kernel.

It's better to obtain the latest vanilla kernel sources from kernel.org, and use it.

Quote:
1.2.1.3. Compiling Issues and Kernel Version

Very often, Linux distros will distribute kernel source that has been patched in various non-standard ways, which may cause trouble.

A more common problem is that some Linux distros distribute incomplete kernel headers. You'll need to compile your code using various header files from the Linux kernel. Murphy's Law states that the headers that are missing are exactly the ones that you'll need for your module work.

To avoid these two problems, I highly recommend that you download, compile and boot into a fresh, stock Linux kernel which can be downloaded from any of the Linux kernel mirror sites. See the Linux Kernel HOWTO for more details.

Ironically, this can also cause a problem. By default, gcc on your system may look for the kernel headers in their default location rather than where you installed the new copy of the kernel (usually in /usr/src/. This can be fixed by using gcc's -I switch.
That quote comes from a section of The Linux Documentation Project's Website on The Linux Kernel Module Programming Guide.

Here is the url to the homepage of that with the full table of contents. I believe it to be a much better guide to compiling your first hello world kernel module.

http://www.tldp.org/LDP/lkmpg/2.6/html/index.html

As a side note, dkms can possibly allow you to load a mismatched kernel module though this may not be the best approach. From my understanding, it is typically used for proprietary kernel modules such as nvidia's and vmware's. Aside from that, a module compiled for one kernel won't load if you boot a different kernel unless you enable CONFIG_MODVERSIONS in the kernel.

Last edited by fakie_flip; 02-15-2015 at 12:23 AM. Reason: Hyperlink for kernel.org
 
1 members found this post helpful.
Old 02-15-2015, 01:03 AM   #5
linux_walt
Member
 
Registered: Dec 2014
Location: Houston, TX
Distribution: Debian wheezy
Posts: 127

Original Poster
Rep: Reputation: 29
Quote:
Your running kernel version 3.4.79 and your kernel modules version 3.4.103 are definately a mismatch, so we have verified the problem.
Yup!
Quote:
Have a read here:
http://linux.die.net/lkmpg/x380.html
Will do it first thing in the morning.
Quote:
You do not need that git repository you are using for a hello world kernel module. You do need the kernel sources.
Do you mean the example repository,
https://github.com/duxing2007/ldd3-examples-3.x
or the kernel repository,
https://github.com/linux-sunxi/linux-sunxi.git?
I'm confused when you say 'You do need the kernel sources'. Aren't the sources in the repository?

Quote:
If you are running the default generic debian kernel from its repositories, be sure to have the exact same source tree for that kernel.
Nope, I have to run the linux-sunxi kernel fom here:
https://github.com/linux-sunxi/linux-sunxi.git

This is because sunxi says they had to tweak the Linux kernel so that it can run their Cubieboard2 specific drivers. Can't quote exactly where I read that at the moment.

Quote:
It's better to obtain the latest vanilla kernel sources from kernel.org, and use it.
1.2.1.3. Compiling Issues and Kernel Version
I did read that, and have to admit I made the decision to ignore it. The reason is that (right or wrong) I didn't want to build the examples in a perfect world. Since the created modules will eventually have to work within the linux-sunxi kernel, I thought it would be best to just tackle any difficulties up front.

Quote:
Here is the url to the homepage of that with the full table of contents. I believe it to be a much better guide to compiling your first hello world kernel module.
That's odd because I remember the quote, but apparently it is not from the book I am following. I must have come across it while googling. Thanks for the link.

Quote:
As a side note, dkms can possibly allow you to load a mismatched kernel module though this may not be the best approach. From my understanding, it is typically used for proprietary kernel modules such as nvidia's and vmware's. Aside from that, a module compiled for one kernel won't load if you boot a different kernel unless you enable CONFIG_MODVERSIONS in the kernel.
That's very interesting and will look into it. Thanks again.

Now must get some sleep.

Although there is one more thing I need to clear up, for sure a misunderstanding of git. I have a fresh copy of the linux-sunxi kernel repository. The first thing that you are supposed to do, before running 'make' on the image and modules, is run this command:
Code:
make ARCH=arm sun7i_defconfig
This creates a file .config .

Next:
Code:
make -j2 ARCH=arm uImage modules
sudo make ARCH=arm  INSTALL_MOD_PATH=output modules_install
This results in the kernel 3.4.103 .

When I try to checkout v3.4.7, and repeat the same steps, I get an error on the very first step:
Code:
~/sysdev/linux-sunxi$ make ARCH=arm sun7i_defconfig
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/conf.o
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTLD  scripts/kconfig/conf
***
*** Can't find default configuration "arch/arm/configs/sun7i_defconfig"!
***
make[1]: *** [sun7i_defconfig] Error 1
make: *** [sun7i_defconfig] Error 2
v3.4.7 Is a git tag. I was hoping that when checking out a tagged version, it would include everything to create the kernel and modules for that version.

Last edited by linux_walt; 02-15-2015 at 01:07 AM.
 
Old 02-15-2015, 04:57 AM   #6
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Rep: Reputation: 85
Quote:
Originally Posted by linux_walt View Post
Code:
~/sysdev/linux-sunxi$ make ARCH=arm sun7i_defconfig
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/conf.o
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTLD  scripts/kconfig/conf
***
*** Can't find default configuration "arch/arm/configs/sun7i_defconfig"!
***
make[1]: *** [sun7i_defconfig] Error 1
make: *** [sun7i_defconfig] Error 2
v3.4.7 Is a git tag. I was hoping that when checking out a tagged version, it would include everything to create the kernel and modules for that version.
I looked into it, and that file is missing from the repository for tag v3.4.7.

https://github.com/linux-sunxi/linux...ch/arm/configs

You could try contacting the developers.
 
1 members found this post helpful.
Old 02-15-2015, 10:01 PM   #7
linux_walt
Member
 
Registered: Dec 2014
Location: Houston, TX
Distribution: Debian wheezy
Posts: 127

Original Poster
Rep: Reputation: 29
Thanks flip, I'm just getting used to navigating around a GitHub repository, thanks to your assistance. I only knew how to clone, didn't realize I could dig in there to find information. Not quite confident enough to ask someone there to please add my file.

However, I did try adding it myself, in my local repository, and got past the first steps successfully:
~/sysdev/linux-sunxi$ make ARCH=arm sun7i_defconfig

Won't go into the gory details of what happened next. Enough to say that, as stated in a reference from previous posts: There is no way in hell that the module will load, unless it's 'vermagic' number matches exactly your system's kernel version.

So to the 'some progress' part. My kernel is 3.4.79. Since my git kernel tree produces v3.4.103, and there is a tag in there for 3.4.7, somewhere in my repository must be a v3.4.79. Some googling and a few trials later:
Code:
~/sysdev/linux-sunxi$ git log --all --grep='v3.4.79'
commit 7369062e14dc82006e544c04928ebb3bf97fd98f
Merge: 9ab10a5 e3b1f41
Author: (name edited out) <email edited out>
Date:   Sat Feb 8 14:41:47 2014 +0100

    Merge tag 'v3.4.79' into reference-3.4

    This is the 3.4.79 stable release
A moment of happiness .

Next I checked it out, and tagged it:
Code:
~/sysdev/linux-sunxi$ git checkout  7369062e14dc82006e544c04928ebb3bf97fd98f
~/sysdev/linux-sunxi$ git tag v3.4.79
~/sysdev/linux-sunxi$ git tag
...
v3.4.7
v3.4.79
v3.5
Next I copied sun7i_defconfig file from the GitHub repository main branch, and put it into my local repository: ~/sysdev/linux-sunxi/arch/arm/configs . Not sure you can just do that, but onwards.

I now had my git kernel tree exactly matching the working system, and the sun7i_defconfig file in the right place. The kernel build went ok, except for a missing .h file:

Code:
CC [M]  drivers/input/touchscreen/synaptics_i2c_rmi.o
drivers/input/touchscreen/synaptics_i2c_rmi.c:18:32: fatal error: linux/earlysuspend.h: No such file or directory
compilation terminated.
  CC [M]  net/bluetooth/bnep/sock.o
make[3]: *** [drivers/input/touchscreen/synaptics_i2c_rmi.o] Error 1
make[2]: *** [drivers/input/touchscreen] Error 2
make[1]: *** [drivers/input] Error 2
make: *** [drivers] Error 2
make: *** Waiting for unfinished jobs....
The rest of the kernel modules seem to have been compiled, at least enough for me to compile the 'hello.c' module. Running modinfo command on the new hello.ko:

Code:
~/src/modules/ldd3-examples-3.x/misc-modules$ sudo modinfo hello.ko
[sudo] password for walter:
filename:       /home/walter/src/modules/ldd3-examples-3.x/misc-modules/hello.ko
license:        Dual BSD/GPL
srcversion:     31FE72DA6A560C890FF9B3F
depends:
vermagic:       3.4.79-02708-g7369062 preempt mod_unload modversions ARMv5 p2v8
As you can see, it's close! The compiler added "-02708-g7369062".

I looked into how/where the compiler adds this extra information. At the very top it has:
Code:
VERSION = 3
PATCHLEVEL = 4
SUBLEVEL = 79
EXTRAVERSION =
NAME = Saber-toothed Squirrel
Then searching for 'EXTRAVERSION':
Code:
~/sysdev/linux-sunxi$ cat Makefile | grep EXTRAVERSION
EXTRAVERSION =
KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
At this point I'm going to take a step back. I don't understand how a hardware vendor could supply a Linux driver, and expect it to work anywhere. Which leads to this bit of info that you provided earlier:
Quote:
As a side note, dkms can possibly allow you to load a mismatched kernel module though this may not be the best approach. From my understanding, it is typically used for proprietary kernel modules such as nvidia's and vmware's. Aside from that, a module compiled for one kernel won't load if you boot a different kernel unless you enable CONFIG_MODVERSIONS in the kernel.
Have not followed up on this, so now is the time.

Thanks!

Last edited by linux_walt; 02-15-2015 at 10:27 PM.
 
Old 02-22-2015, 04:01 PM   #8
linux_walt
Member
 
Registered: Dec 2014
Location: Houston, TX
Distribution: Debian wheezy
Posts: 127

Original Poster
Rep: Reputation: 29
Just an update. I was able to get my 'out of tree' module to install by recompiling the kernel, replacing the original uImage in nanda with the newly created uImage, then using the same compiler to compile the module.

Everything seems to be working as before, even if I have not yet moved the modules from the new build tree into the system.

Here's the kernel I used:
Code:
git clone -b sunxi-3.4 https://github.com/linux-sunxi/linux-sunxi.git
Kernel and new module info:
Code:
$ uname -r
3.4.103-00033-g9a1cd03

$ sudo modinfo hello.ko
filename:       /home/walter/src/modules/ldd3-examples-3.x/misc-modules/hello.ko
license:        Dual BSD/GPL
srcversion:     31FE72DA6A560C890FF9B3F
depends:
vermagic:       3.4.103-00033-g9a1cd03 SMP preempt mod_unload modversions ARMv7 p2v8
Just a couple of gotchas:
After running 'make ARCH=arm sun7i_defconfig' make sure, if installing the uImage on nand, that the nand options are selected:
Code:
CONFIG_SUNXI_NAND=y
CONFIG_SUNXI_NAND_COMPAT_DEV=y
Do this manualy. I have not had any luck trying to tweak the .config file using:
Code:
make ARCH=arm menuconfig
No idea what I'm doing wrong, but so far it has always led to errors (and much wasted time) when finally building the kernel. I even tried using the 'defconfig' program without making any changes. Running 'diff' command on .config and a backup copy of it (not run through 'defconfig') showed a lot of changes.

Also, learned about 'make mrproper'. I think this is the command to run, if you need to recompile the kernel a second (or n'th) time. I was using just 'make clean' and, as far as I can remember, that never worked.

Links:
The Linux Kernel Module Programming Guide
http://www.tldp.org/LDP/lkmpg/2.6/html/index.html

Building External Modules
https://www.kernel.org/doc/Documenta...ld/modules.txt

How to: Compile Linux kernel modules
http://www.cyberciti.biz/tips/compil...el-module.html

Kernel Symbols and CONFIG_MODVERSIONS
http://linux.die.net/lkmpg/x380.html

Difference between make mrproper and make clean?
http://www.linuxquestions.org/questi...-clean-473144/

Dont add “+” to linux kernel version
http://stackoverflow.com/questions/1...kernel-version

Last edited by linux_walt; 02-24-2015 at 07:34 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] "invalid module format" error for Bluetooth modules with -current kernel 2.6.33.1 cgorac Slackware 13 04-02-2010 03:02 PM
rtl8187 driver module load error (Invalid module format) j_75080 Linux - Networking 3 07-06-2009 09:37 PM
error: -1 Invalid module format when using insmod with module cross-compiled for arm AndrewShanks Linux - Embedded & Single-board computer 4 10-15-2007 03:50 AM
insmod: error inserting 'module.o': -1 Invalid module format ksrinivas Linux - Newbie 5 10-11-2006 07:29 AM
invalid module format when starting alsa modules abk4523 Linux - Software 1 06-11-2005 08:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 04:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration