1. 程式人生 > >解決insmod: error inserting 'hello.ko': -1 Invalid module format

解決insmod: error inserting 'hello.ko': -1 Invalid module format

實現 功能:在PC的LINUX實現驅動測試,不用在2440上測試

解決insmod: error inserting ‘hello.ko’: -1 Invalid module format

第一次寫Linux驅動,環境搭建了好久,第一次可能是由於GCC的版本問題,編譯出來的驅動只能insmod,而無法rmmod

然後是make時使用的核心版本和本系統的版本不一致,

導致出現insmod: error inserting ‘hello.o’ :-l invalid module format錯誤

我一直在玩開發板,下載的是linux-2.6.30核心,以前為 開發板編譯驅動時,比較順利。

而我的上位機linux系統是fedora10 實驗需要,需要為我的上位機寫一個驅動。可是寫好了以後,

當我 insmod hello.ko時,卻一直有一個錯誤

     insmod: error inserting 'hello.ko': -1 Invalid module format。

網上一查,原因很明確:編譯時用的hello.ko的kenerl 不是 我的pc的kenerl版本。

但解決起來,就比較麻煩了,百度一下,說什麼要重新編譯pc上的核心,還要修改grub。

PC機上的驅動,其實與開發板上的驅動 一點關係都沒有。

我只要以pc機的核心為基準,在makefile 那改變核心路徑就可以了,可以順利地編譯了!

PC機下輸入 uname -r 就可檢視pc機當前執行的核心版本

注意,我是在fedora10 下

cd /usr/src

ls

發現核心標頭檔案,如下

/usr/src/kernels/2.6.27.5-117.fc10.i686

解決方法:為生成hello.ko

其Makefile改為如下內容:

############### 1 KERNELDIR=/usr/src/kernels/2.6.27.5-117.fc10.i686 //在此新增核心路徑 2 3 PWD:=(shellpwd)4INSTALLDIR=(shell pwd) 4 INSTALLDIR=(PWD) 5 #CC=arm-linux-gcc 6 CC=gcc

8 obj-m := hello.o 9 modules: 10 $(MAKE) -C (

KERNELDIR)M=(KERNELDIR) M=(PWD) modules 11 12 clean: 13 rm -rf *.o *.ko *.mod.c *.markers *.order *.symvers 14 .PHONY:modules clean

////////////

make insmod hello.ko 看輸出資訊 cat /var/log/messages | tail -6 ubleshootd_t:s0 key=(null) Oct 2 11:08:07 tdp setroubleshoot: SELinux is preventing updatedb (locate_t) “sys_resource” locate_t. For complete SELinux messages. run sealert -l 42f53b70-53bb-42db-b6cb-39c247a562ea Oct 2 11:44:19 tdp kernel: Hello, world!! Oct 2 11:44:19 tdp kernel: this is my hello world!!

哈哈 ,成功啦