insmod 错误 -1 Invalid module format

21lyw 2009-06-29 11:43:21
查看系统信息为: disagrees about version of symbol struct_module

当前运行内核版本是 Linux version 2.6.18-1.2798.fc6 (brewbuilder@hs20-bc2-3.build.redhat.com) (gcc version 4.1.1 20061011 (Red Hat 4.1.1-30)) #1 SMP Mon Oct 16 14:54:20 EDT 2006
源码下载的是:2.6.18.1

程序为


#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h> /* everything... */

#define DEVICE_NAME "demo"
#define demo_MAJOR 249
#define demo_MINOR 0
#define MAX_BUF_LEN 20

//MODULE_LICENSE("GPL");

#ifdef UTS_RELEASE
#undef UTS_RELEASE
#endif
#define UTS_RELEASE "2.6.18-1"



static char drv_buf[20];


static int demo_open(struct inode *inode, struct file *file)
{
//MOD_INC_USE_COUNT;
//sprintf(drv_buf,"device open sucess!\n");
//printk("device open sucess!\n");
return 0;
}
static int demo_release(struct inode *inode, struct file *filp)
{
//MOD_DEC_USE_COUNT;
//printk("device release\n");
return 0;
}


static ssize_t demo_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
if(count > MAX_BUF_LEN)
count=MAX_BUF_LEN;
//copy_to_user(buffer, drv_buf,count);
printk("user read data from driver\n");
return count;
}
static ssize_t demo_write(struct file *filp,const char *buffer, size_t count)
{
//copy_from_user(drv_buf , buffer, count);
printk("user write data to driver\n");
//your code here
return count;
}
static struct file_operations demo_fops =
{
write: demo_write,
read: demo_read,
open: demo_open,
release: demo_release,
};

static int demo_init(void)
{
int result;
/*SET_MODULE_OWNER(&demo_fops);*/
result = register_chrdev(demo_MAJOR, "demo", &demo_fops);
if (result < 0) return result;
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void demo_exit(void)
{
unregister_chrdev(demo_MAJOR, "demo");
printk(DEVICE_NAME " unloaded\n");
}
module_init(demo_init);
module_exit(demo_exit);
...全文
1101 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
21lyw 2009-06-29
  • 打赏
  • 举报
回复
当前运行内核版本是 2.6.18-1.2798.fc6
源码下载的是: 2.6.18.1

版本要求这么高?
pottichu 2009-06-29
  • 打赏
  • 举报
回复
当前运行的 kernel 版本和 你编译用的kernel source 版本不一致。
yhf365 2009-06-29
  • 打赏
  • 举报
回复
同意9楼的说法,
代码中貌似有问题,
在Linux设备驱动第三版中,这样声明write:
ssize_t scull_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
楼主少了一个表示读写位置的指针loff_t *f_pos
read中也一样。
函数原型不一致,可能就是造成格式无法识别的原因
puheavy123 2009-06-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 linaxing 的回复:]
用modprobe来加载并且用--force选项
[/Quote]

嗯,有时换modprobe就没问题了,lz试试看行不行
pottichu 2009-06-29
  • 打赏
  • 举报
回复
ssize_t read(struct file *filp, char __user *buff, size_t count, loff_t *offp);
ssize_t write(struct file *filp, const char __user *buff, size_t count, loff_t *offp);

难怪会有错误。。。

你看看你的 write 实现。。
21lyw 2009-06-29
  • 打赏
  • 举报
回复
编译显示

[root@localhost demo]# make
make -C /usr/src/linux-2.6.18.1 M=/root/demo modules
make[1]: Entering directory `/usr/src/linux-2.6.18.1'
CC [M] /root/demo/demo.o
/root/demo/demo.c:54: 警号:从不兼容的指针类型初始化
/root/demo/demo.c:19: 警告:drv_buf 定义后未使用
Building modules, stage 2.
MODPOST
CC /root/demo/demo.mod.o
LD [M] /root/demo/demo.ko
make[1]: Leaving directory `/usr/src/linux-2.6.18.1'
[root@localhost demo]#

linaxing 2009-06-29
  • 打赏
  • 举报
回复
用modprobe来加载并且用--force选项
puheavy123 2009-06-29
  • 打赏
  • 举报
回复
编译过程有没有出现什么错误啊。。。。

我编译过几个,都没什么问题哦
21lyw 2009-06-29
  • 打赏
  • 举报
回复
是一样的:

[root@localhost demo]# cat /usr/include/linux/version.h
#define LINUX_VERSION_CODE 132626
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
[root@localhost demo]# cat /usr/src/linux-2.6.18.1/include/linux/version.h
#define LINUX_VERSION_CODE 132626
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
[root@localhost demo]#





pottichu 2009-06-29
  • 打赏
  • 举报
回复
系统kernel 版本号存放在:
/usr/include/linux/version.h 中,

你下载的kernel 中, 版本好存放在:
include/linux/version.h

你可以直接修改你下载的 include/linux/version.h

但,十分不推荐这种做法。
pottichu 2009-06-29
  • 打赏
  • 举报
回复
必须一致,module 在加载的时候,会作版本检查。

4,441

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧