Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2242149
  • 博文数量: 395
  • 博客积分: 10994
  • 博客等级: 上将
  • 技术积分: 5586
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-17 19:49
文章存档

2014年(1)

2013年(10)

2012年(74)

2011年(303)

2010年(7)

分类: LINUX

2011-06-16 11:43:11

直接列代码,如下:

第一。这一步内容没有导出内核符号,看看实验现象

[root@bogon 4-1-4]# ls

hello.c  integer_suanfa.c  Makefile

[root@bogon 4-1-4]# vi hello.c

#include

#include

 

MODULE_LICENSE("GPL");

 

extern int add_integer(int a, int b);

extern int sub_integer(int a, int b);

 

 

static int __init hello_init(void){

        int res=add_integer(1,3);

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

        return 0;

}

 

 

static void __exit hello_exit(void){

        int res=sub_integer(3,1);

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

}

 

module_init(hello_init);

module_exit(hello_exit);

~                            

[root@bogon 4-1-4]# vi integer_suanfa.c

#include

#include

 

MODULE_LICENSE("GPL");

 

int add_integer(int a,int b){

        return a+b;

}

 

int sub_integer(int a, int b){

        return a-b;

}

 

static int __init integer_init(void){

        return 0;

}

 

static void __exit integer_exit(void){

 

}

 

module_init(integer_init);

module_exit(integer_exit);

 

//EXPORT_SYMBOL(add_integer);

//EXPORT_SYMBOL(sub_integer); //这里是内核符号导出,先屏蔽了,看看结果

[root@bogon 4-1-4]# vi Makefile

ifneq ($(KERNELRELEASE),)

 

obj-m := hello.o integer_suanfa.o

 

else

 

KDIR := /lib/modules/2.6.29/build

all:

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

clean:

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

 

endif

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

make -C /lib/modules/2.6.29/build M=/home/guoqian/4-1-4 modules

make[1]: Entering directory `/home/guoqian/4-1-1/linux-2.6.29'

  CC [M]  /home/guoqian/4-1-4/hello.o

  CC [M]  /home/guoqian/4-1-4/integer_suanfa.o

  Building modules, stage 2.

  MODPOST 2 modules

WARNING: "add_integer" [/home/guoqian/4-1-4/hello.ko] undefined!

WARNING: "sub_integer" [/home/guoqian/4-1-4/hello.ko] undefined!

(注,上面出现了警告,却是有问题,不管,往下看)

  CC      /home/guoqian/4-1-4/hello.mod.o

  LD [M]  /home/guoqian/4-1-4/hello.ko

  CC      /home/guoqian/4-1-4/integer_suanfa.mod.o

  LD [M]  /home/guoqian/4-1-4/integer_suanfa.ko

make[1]: Leaving directory `/home/guoqian/4-1-1/linux-2.6.29'

[root@bogon 4-1-4]# ls

hello.c      hello.o               integer_suanfa.mod.o  modules.order

hello.ko     integer_suanfa.c      integer_suanfa.o      Module.symvers

hello.mod.c  integer_suanfa.ko     Makefile

hello.mod.o  integer_suanfa.mod.c  Module.markers

[root@bogon 4-1-4]# insmod integer_suanfa.ko

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

insmod: error inserting 'hello.ko': -1 Unknown symbol in module

//这里出现了问题,提示错误,模块中不能识别的符号,下面查看内核运行中的记录的内核符号,如下

[root@bogon 4-1-4]# cat /proc/kallsyms >log1

[root@bogon 4-1-4]# vi log1

c04010e8 T _stext

c04010e8 T do_one_initcall

c04010e8 T stext

c04011f0 t run_init_process

c0401204 t init_post

c04012d4 T name_to_dev_t

c0401484 T hard_smp_processor_id

c040149c t get_apic_id

c04014c1 t target_cpus

c04014c7 t init_apic_ldr

c0401509 t cpu_mask_to_apicid

c040150c t cpu_mask_to_apicid_and

c0401519 t phys_pkg_id

c040151e t vector_allocation_domain

........

//发现重定向内容太多,那么我们用grep来查看

[root@bogon 4-1-4]# cat /proc/kallsyms |grep add_integer

eba76000 t add_integer  [integer_suanfa]

[root@bogon 4-1-4]# cat /proc/kallsyms |grep sub_integer

eba76004 t sub_integer  [integer_suanfa]

看到了,有信息,但是这不是内核符号的准确信息

[root@bogon 4-1-4]# lsmod |grep integer_suanfa

integer_suanfa          1060  0

[root@bogon 4-1-4]# rmmod integer_suanfa

[root@bogon 4-1-4]# lsmod |grep integer_suanfa

 

第二。这一步内容导出内核符号,再看看实验现象,是否成功

[root@bogon 4-1-4]# vi integer_suanfa.c

#include

#include

 

MODULE_LICENSE("GPL");

 

int add_integer(int a,int b){

        return a+b;

}

 

int sub_integer(int a, int b){

        return a-b;

}

 

static int __init integer_init(void){

        return 0;

}

 

static void __exit integer_exit(void){

 

}

 

module_init(integer_init);

module_exit(integer_exit);

 

EXPORT_SYMBOL(add_integer);

EXPORT_SYMBOL(sub_integer);

//导出了内核符号                              

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

make -C /lib/modules/2.6.29/build M=/home/guoqian/4-1-4 modules

make[1]: Entering directory `/home/guoqian/4-1-1/linux-2.6.29'

  CC [M]  /home/guoqian/4-1-4/integer_suanfa.o

  Building modules, stage 2.

  MODPOST 2 modules

  CC      /home/guoqian/4-1-4/hello.mod.o

  LD [M]  /home/guoqian/4-1-4/hello.ko

  CC      /home/guoqian/4-1-4/integer_suanfa.mod.o

  LD [M]  /home/guoqian/4-1-4/integer_suanfa.ko

make[1]: Leaving directory `/home/guoqian/4-1-1/linux-2.6.29'

[root@bogon 4-1-4]# insmod integer_suanfa.ko

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

[root@bogon 4-1-4]#

Message from syslogd@ at Thu Jun 16 02:29:34 2011 ...

bogon kernel: hello init ,res=4

成功了,结构正常,呵呵

[root@bogon 4-1-4]# lsmod |grep integer_suanfa

integer_suanfa          1444  1 hello

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

hello                   1124  0

integer_suanfa          1444  1 hello

[root@bogon 4-1-4]# cat /proc/kallsyms |grep add_integer

eba98000 u add_integer  [hello]

eba98014 r __ksymtab_add_integer        [integer_suanfa]

eba98030 r __kstrtab_add_integer        [integer_suanfa]

eba98020 r __kcrctab_add_integer        [integer_suanfa]

eba98000 T add_integer  [integer_suanfa]

926a4a21 a __crc_add_integer    [integer_suanfa]

[root@bogon 4-1-4]# cat /proc/kallsyms |grep sub_integer

eba98004 u sub_integer  [hello]

eba9800c r __ksymtab_sub_integer        [integer_suanfa]

eba98024 r __kstrtab_sub_integer        [integer_suanfa]

eba9801c r __kcrctab_sub_integer        [integer_suanfa]

eba98004 T sub_integer  [integer_suanfa]

60d3bf06 a __crc_sub_integer    [integer_suanfa]

此时的内核导出符号信息才是正确的(有_ksymtab….信息。。。)

[root@bogon 4-1-4]# rmmod hello

[root@bogon 4-1-4]#

Message from syslogd@ at Thu Jun 16 02:31:34 2011 ...

bogon kernel: hello exit ,res=2

 

[root@bogon 4-1-4]# rmmod integer_suanfa

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

[root@bogon 4-1-4]# lsmod |grep integer_suanfa

[root@bogon 4-1-4]#

 

总结:我感觉内核符号导出基本上和windows下的c语言中的多文件编译(多个.c一起编译)效果差不多,呵呵。。。。

 

 

 

 

 

 

 

 

 

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

上一篇:linux内核模块参数

下一篇:2440-uboot移植

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