如題,寫完驅動後編譯內核的時候碰到了這個錯誤,頭一次看到這個編譯錯誤,略懵逼,先給出完整的錯誤:
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;
}