Chinaunix首页 | 论坛 | 博客
  • 博客访问: 202336
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 824
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-12 21:40
个人简介

只有今天的埋头,才有明天的出头。

文章分类

全部博文(80)

文章存档

2014年(80)

我的朋友

分类: LINUX

2014-12-12 10:40:12

内核符号导出

第一步:建立新目录,在目录下面编写两个内核模块,一个模块输出一些符号,给另一个符号,并编写对应的Makefile

#include 

#include 

 

MODULE_LICENSE("GPL");

 

extern int add_integar(int a,int b);

extern int sub_integar(int a,int b);

 

static int __init hello_init(void)

{

        int res=add_integar(1,2);

        printk(KERN_EMERG"hello init , res=%d\n",res);

        return 0;

}

static void __exit hello_exit()

{

        int res=sub_integar(2,1);

        printk(KERN_EMERG"hello exit,res=%d\n",res);

}

 

module_init(hello_init);

module_exit(hello_exit);

#include 

#include 

 

MODULE_LICENSE("GPL");

 

int add_integar(int a,int b)

{

        return a+b;

}

int sub_integar(int a,int b)

{

        return a-b;

}

static int __init sym_init()

{

        return 0;

}

static void __exit sym_exit()

{

}

module_init(sym_init);

module_exit(sym_exit);

ifneq ($(KERNELRELEASE),)

 

obj-m := hello.o calculate.o

 

else

 

KDIR := /lib/modules/2.6.32-279.el6.i686/build

all:

        make -C $(KDIR) M=$(PWD) modules

clean:

        rm -f *.ko *.o *.mod.o *.mod.c *.symvers

 

endif

第二步:编译内核模块

#make生成hello.ko ,calculate.ko

第三步:加载内核模块

[root@localhost 4-1-4]# insmod calculate.ko

[root@localhost 4-1-4]# insmod hello.ko

insmod: error inserting 'hello.ko': -1 Unknown symbol in module //我们的内核没有导出一些符号

第四步:查看当前环境下内核导出了哪些符号

[root@localhost 4-1-4]# cat /proc/kallsyms |grep add_integar

f7e8b000 t add_integar [calculate]

[root@localhost 4-1-4]# cat /proc/kallsyms |grep sub_integar

f7e8b010 t sub_integar [calculate]

当前内核并没有导出我们需要的内核符号

第五步:卸载内核模块

[root@localhost 4-1-4]# rmmod calculate.ko

[root@localhost 4-1-4]# lsmod |grep calculate.ko

第六步:看内核符号导出情况,修改calculate.c

#include 

#include 

 

MODULE_LICENSE("GPL");

 

int add_integar(int a,int b)

{

        return a+b;

}

int sub_integar(int a,int b)

{

        return a-b;

}

static int __init sym_init()

{

        return 0;

}

static void __exit sym_exit()

{

}

module_init(sym_init);

module_exit(sym_exit);//添加下面两行导出

EXPORT_SYMBOL(add_integar);

EXPORT_SYMBOL(sub_integar);

第七步:编译内核模块

[root@localhost 4-1-4]# make clean

rm -f *.ko *.o *.mod.o *.mod.c *.symvers

[root@localhost 4-1-4]# make

make -C /lib/modules/2.6.32-279.el6.i686/build M=/workspace/kernel/prama_1/4-1-4 modules

make[1]: Entering directory `/usr/src/kernels/2.6.32-279.el6.i686'

  CC [M]  /workspace/kernel/prama_1/4-1-4/hello.o

/workspace/kernel/prama_1/4-1-4/hello.c:16: 警告:函数声明不是一个原型

  CC [M]  /workspace/kernel/prama_1/4-1-4/calculate.o

/workspace/kernel/prama_1/4-1-4/calculate.c:15: 警告:函数声明不是一个原型

/workspace/kernel/prama_1/4-1-4/calculate.c:19: 警告:函数声明不是一个原型

  Building modules, stage 2.

  MODPOST 2 modules

  CC      /workspace/kernel/prama_1/4-1-4/calculate.mod.o

  LD [M]  /workspace/kernel/prama_1/4-1-4/calculate.ko.unsigned

  NO SIGN [M] /workspace/kernel/prama_1/4-1-4/calculate.ko

  CC      /workspace/kernel/prama_1/4-1-4/hello.mod.o

  LD [M]  /workspace/kernel/prama_1/4-1-4/hello.ko.unsigned

  NO SIGN [M] /workspace/kernel/prama_1/4-1-4/hello.ko

make[1]: Leaving directory `/usr/src/kernels/2.6.32-279.el6.i686'

第八步:加载内核模块

[root@localhost 4-1-4]# insmod calculate.ko

[root@localhost 4-1-4]# insmod hello.ko

[root@localhost 4-1-4]# 

Message from syslogd@localhost at Dec 12 10:27:38 ...

 kernel:hello init , res=3

[root@localhost 4-1-4]# lsmod |grep calculate

calculate                873  1 hello

[root@localhost 4-1-4]# lsmod |grep hello

hello                    591  0 

calculate                873  1 hello

第九步:查看当前环境下是否导出了需要的符号

 

[root@localhost 4-1-4]# cat /proc/kallsyms |grep sub_integar

f7e9c03c r __ksymtab_sub_integar [calculate]

f7e9c054 r __kstrtab_sub_integar [calculate]

f7e9c04c r __kcrctab_sub_integar [calculate]

f7e9c010 T sub_integar [calculate]

 

[root@localhost 4-1-4]# cat /proc/kallsyms |grep add_integar

f7e9c044 r __ksymtab_add_integar [calculate]

f7e9c060 r __kstrtab_add_integar [calculate]

f7e9c050 r __kcrctab_add_integar [calculate]

f7e9c000 T add_integar [calculate]

 

表明已经导出符号

 

第十步:卸载内核模块

[root@localhost 4-1-4]# rmmod hello.ko

[root@localhost 4-1-4]# 

Message from syslogd@localhost at Dec 12 10:33:03 ...

 kernel:hello exit,res=1

^C

[root@localhost 4-1-4]# rmmod calculate.ko

[root@localhost 4-1-4]# lsmod |grep calculate.ko

[root@localhost 4-1-4]# lsmod |grep hello.ko

 

 

 

 

 

 

 

阅读(1369) | 评论(0) | 转发(0) |
0

上一篇:内核模块参数

下一篇:创建交叉编译环境

给主人留下些什么吧!~~