在阅读linux内核代码的过程中经常会看到驱动代码中在最后调用arch_initcall subsys_initcall module_init等函数,虽然知道这些函数是来确定驱动在内核启动过程中的加载顺序的,但是没有仔细看过实现原理,借清明节空闲来看一下具体实现,以mips处理器2.6.21内核为例。
在linux/init.h中可以看到以下代码:
/* initcalls are now grouped by functionality into separate
* subsections. Ordering inside the subsections is determined
* by link order.
* For backwards compatibility, initcall() puts the call in
* the device init subsection.
*
* The `id' arg to __define_initcall() is needed so that multiple initcalls
* can point at the same handler without causing duplicate-symbol build errors.
*/
#define __define_initcall(level,fn,id) \
static initcall_t __initcall_##fn##id __attribute_used__ \
__attribute__((__section__(".initcall" level ".init"))) = fn
/*
* A "pure" initcall has no dependencies on anything else, and purely
* initializes variables that couldn't be statically initialized.
*
* This only exists for built-in code, not for modules.
*/
#define pure_initcall(fn) __define_initcall("0",fn,1)
#define core_initcall(fn) _