LDD2代码阅读手记: hello2.c
时间:2006-05-21 来源:lanttor.guo
笔记原创: 兰特
联系邮件: [email protected]
参考资料:
1. Linux Device Drivers, Second Edition, Alessandro
2. The Linux Kernel Module Programming Guide (v2.4.0), Peter Jay Salzman Ori Pomerantz
/*
* $Id: hello.c,v 1.7 2000/08/17 11:11:48 rubini Exp $
*/
#define MODULE
#include <linux/module.h>
/*
* These lines, although not shown in the book,
* are needed to make hello.c run properly even when
* your kernel has version support enabled
*/
int init_module(void) { printk("<1>Hello, world\n"); return 0; }
void cleanup_module(void) { printk("<1>Goodbye cruel world\n"); }
int helloi;
MODULE_LICENSE("GPL");
这个程序在Hello World程序上仅仅多加了一条全局变量的定义: int helloi
这里涉及到了内核符号表的概念.公共内核符号表中包含了所有的全局内核符号(全局的变量和函数)的地址,
实现驱动模块时,在很多情况下需要使用这些全局符号.
当模块被装入内核后,它所导出的任何符号都变成了公共符号表的一部分. 默认情况下,大部分版本的modutils导出所有的非静态符号. 上述程序模块hello2.o被载入内核后,我们通过命令 cat /proc/ksyms看到新增加的符号:
cat /proc/ksyms | grep hello2
输出:
e07be3cc helloi_R__ver_helloi [hello2]
e07be000 __insmod_hello2_O/home/guotaotao/ldd2/ldd2-samples-1.0.1/misc-modules/hello2.o_M446FB4D3_V132116 [hello2]
e07be060 __insmod_hello2_S.text_L53 [hello2]
e07be095 __insmod_hello2_S.rodata_L41 [hello2]
如果我们准备导出模块的一个子集, 则首先需要定义一个预处理宏EXPORT_SYMTAB, 这个宏必须在包含module.h头文件之前定义.
然后可以使用EXPORT_SYMBOL(name)宏来导出符号.
在我们这个程序里, 我们再多加一个全局变量的定义: int helloj, 然后EXPORT_SYMBOL(helloi). 如下:
/*
* $Id: hello.c,v 1.7 2000/08/17 11:11:48 rubini Exp $
*/
#define MODULE
#include <linux/module.h>
/*
* These lines, although not shown in the book,
* are needed to make hello.c run properly even when
* your kernel has version support enabled
*/
int init_module(void) { printk("<1>Hello, world\n"); return 0; }
void cleanup_module(void) { printk("<1>Goodbye cruel world\n"); }
int helloi;
int helloj;
EXPORT_SYMBOL(helloi);
MODULE_LICENSE("GPL");
编译: gcc -c -I/usr/src/linux -D__KERNEL__ -DEXPORT_SYMTAB hello2.c (我们在命令行中定义预处理宏EXPORT_SYMTAB)
用insmod加载这个模块后,用命令cat /proc/ksyms | grep hello2 看导出的符号:
e07be3cc helloi_R__ver_helloi [hello2]
e07be000 __insmod_hello2_O/home/guotaotao/ldd2/ldd2-samples-1.0.1/misc-modules/hello2.o_M446FB4D3_V132116 [hello2]
e07be060 __insmod_hello2_S.text_L53 [hello2]
e07be095 __insmod_hello2_S.rodata_L41 [hello2]
我们发现helloi被导出了, 而helloj并没有导出.
所以, 我们就可以用这样的方式导出我们想导出的全局符号, 而不用导出所有的全局符号.
联系邮件: [email protected]
参考资料:
1. Linux Device Drivers, Second Edition, Alessandro
2. The Linux Kernel Module Programming Guide (v2.4.0), Peter Jay Salzman Ori Pomerantz
/*
* $Id: hello.c,v 1.7 2000/08/17 11:11:48 rubini Exp $
*/
#define MODULE
#include <linux/module.h>
/*
* These lines, although not shown in the book,
* are needed to make hello.c run properly even when
* your kernel has version support enabled
*/
int init_module(void) { printk("<1>Hello, world\n"); return 0; }
void cleanup_module(void) { printk("<1>Goodbye cruel world\n"); }
int helloi;
MODULE_LICENSE("GPL");
这个程序在Hello World程序上仅仅多加了一条全局变量的定义: int helloi
这里涉及到了内核符号表的概念.公共内核符号表中包含了所有的全局内核符号(全局的变量和函数)的地址,
实现驱动模块时,在很多情况下需要使用这些全局符号.
当模块被装入内核后,它所导出的任何符号都变成了公共符号表的一部分. 默认情况下,大部分版本的modutils导出所有的非静态符号. 上述程序模块hello2.o被载入内核后,我们通过命令 cat /proc/ksyms看到新增加的符号:
cat /proc/ksyms | grep hello2
输出:
e07be3cc helloi_R__ver_helloi [hello2]
e07be000 __insmod_hello2_O/home/guotaotao/ldd2/ldd2-samples-1.0.1/misc-modules/hello2.o_M446FB4D3_V132116 [hello2]
e07be060 __insmod_hello2_S.text_L53 [hello2]
e07be095 __insmod_hello2_S.rodata_L41 [hello2]
如果我们准备导出模块的一个子集, 则首先需要定义一个预处理宏EXPORT_SYMTAB, 这个宏必须在包含module.h头文件之前定义.
然后可以使用EXPORT_SYMBOL(name)宏来导出符号.
在我们这个程序里, 我们再多加一个全局变量的定义: int helloj, 然后EXPORT_SYMBOL(helloi). 如下:
/*
* $Id: hello.c,v 1.7 2000/08/17 11:11:48 rubini Exp $
*/
#define MODULE
#include <linux/module.h>
/*
* These lines, although not shown in the book,
* are needed to make hello.c run properly even when
* your kernel has version support enabled
*/
int init_module(void) { printk("<1>Hello, world\n"); return 0; }
void cleanup_module(void) { printk("<1>Goodbye cruel world\n"); }
int helloi;
int helloj;
EXPORT_SYMBOL(helloi);
MODULE_LICENSE("GPL");
编译: gcc -c -I/usr/src/linux -D__KERNEL__ -DEXPORT_SYMTAB hello2.c (我们在命令行中定义预处理宏EXPORT_SYMTAB)
用insmod加载这个模块后,用命令cat /proc/ksyms | grep hello2 看导出的符号:
e07be3cc helloi_R__ver_helloi [hello2]
e07be000 __insmod_hello2_O/home/guotaotao/ldd2/ldd2-samples-1.0.1/misc-modules/hello2.o_M446FB4D3_V132116 [hello2]
e07be060 __insmod_hello2_S.text_L53 [hello2]
e07be095 __insmod_hello2_S.rodata_L41 [hello2]
我们发现helloi被导出了, 而helloj并没有导出.
所以, 我们就可以用这样的方式导出我们想导出的全局符号, 而不用导出所有的全局符号.
相关阅读 更多 +