EXPORT_SYMBOL linux kernel 符号输出函数

节选《Linux Kernel Development》third edition charpter 17

文章大意:

内核编译的模块,其内部的函数如果需要在用户空间可被调用(或用户可见),要

通过声明EXPORT_SYMBOL("相应的函名"),来达到可调用(可视)的效果。

EXPORT后函数可以被其他模块使用。

Exported Symbols

When modules are loaded, they are dynamically linked into the kernel.As with user-
space, dynamically linked binaries can call only into external functions explicitly exported
for use. In the kernel, this is handled via special directives called EXPORT_SYMBOL() and
EXPORT_SYMBOL_GPL().
Exported functions are available for use by modules. Functions not exported cannot be
invoked from modules.The linking and invoking rules are much more stringent for mod-
ules than code in the core kernel image. Core code can call any nonstatic interface in the
kernel because all core source files are linked into a single base image. Exported symbols,
of course, must be nonstatic, too.The set of exported kernel symbols are known as the
exported kernel interfaces.
Exporting a symbol is easy.After the function is declared, it is usually followed by an
EXPORT_SYMBOL(). For example
/*
* get_pirate_beard_color - return the color of the current pirate’s beard.
* @pirate is a pointer to a pirate structure
* the color is defined in <linux/beard_colors.h>.
*/
int get_pirate_beard_color(struct pirate *p)
{
return p->beard.color;
}
EXPORT_SYMBOL(get_pirate_beard_color);
Presuming that get_pirate_beard_color() is also declared in an accessible header
file, any module can now access it.
Some developers want their interfaces accessible to only GPL-compliant modules.The
kernel linker enforces this restriction through use of the MODULE_LICENSE() directive. If
you want the previous function accessible to only modules that labeled themselves as
GPL-licensed, use instead
EXPORT_SYMBOL_GPL(get_pirate_beard_color);
If your code is configurable as a module, you must ensure that when compiled as a
module all interfaces that it uses are exported. Otherwise linking errors (and a broken
module) result.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值