如题,写完驱动后编译内核的时候碰到了这个错误,头一次看到这个编译错误,略懵逼,先给出完整的错误:
WARNING: modpost: Found 2 section mismatch(es).
To see full details build your kernel with:
'make CONFIG_DEBUG_SECTION_MISMATCH=y'
FATAL: modpost: Section mismatches detected.
Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.
根据提示,加上编译选项“CONFIG_DEBUG_SECTION_MISMATCH=y”后可以看到详细的错误地方,然后就能看到下面的错误:
WARNING: drivers/misc/mediatek/built-in.o(.data+0x56ec): Section mismatch in reference from the variable aht10_init_info to the function .init.text:aht10_local_init()
The variable aht10_init_info references
the function __init aht10_local_init()
If the reference is valid then annotate the
variable with __init* or __refdata (see linux/init.h) or name the variable:
*_template, *_timer, *_sht, *_ops, *_probe, *_probe_one, *_console
如果看到这个提示还不懂什么意思的话,可以看看这篇文章:What is kernel section mismatch?。结合文章,意思就是变量aht10_init_info引用了__init aht10_local_init函数,因为函数用__init修饰后,会在执行完就释放函数所占用的内存,那么aht10_init_info再引用__init aht10_local_init函数就是非法的了,我们看一下两者的关系:
static struct hmdy_init_info aht10_init_info = {
.name = "aht10",
.init = aht10_local_init,
.uninit = aht10_local_uninit,
};
static __init int aht10_local_init(void)
{
...
return 0;
}
所以想解决这个问题,我们将函数的__init修饰符去掉就好:
static int aht10_local_init(void)
{
...
return 0;
}