天天看點

Linux核心子產品:子產品的編譯

子產品源碼:

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
  printk(KERN_ALERT " Hello World enter\n");
  return 0;
}
static void hello_exit(void)
{
  printk(KERN_ALERT " Hello World exit\n ");
}
module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("Song Baohua");
MODULE_DESCRIPTION("A simple Hello World Module");
MODULE_ALIAS("a simplest module");
           

Makefile:

obj-m := hello.o
           

使用如下指令編譯helloworld子產品:

make -C /usr/src/linux-2.6.15.5/ M=/driver_study/ modules
           

如果目前處于子產品所在的目錄,則以下指令與上述指令同等:

make –C /usr/src/linux-2.6.15.5 M=$(pwd) modules
           

其中 -C後指定的是Linux核心源碼目錄,而M=後指定的是hello.c和Makefile所在的目錄,編譯結果如下所示:

[[email protected] driver_study]# make -C /usr/src/linux-2.6.15.5/
M=/driver_study/ modules
make: Entering directory '/usr/src/linux-2.6.15.5'
CC [M] /driver_study/hello.o
/driver_study/hello.c:18:35: warning: no newline at end of file
Building modules, stage 2.
MODPOST
CC /driver_study/hello.mod.o
LD [M] /driver_study/hello.ko
make: Leaving directory '/usr/src/linux-2.6.15.5'
           

從中可以看出,編譯過程經曆了這樣的步驟:先進入Linux核心所在的目錄,并編譯出hello.o檔案,運作MODPOST會生成臨時hello.mod.c檔案,而後根據此檔案編譯出hello.mod.o,之後連接配接hello.o和hello.mod.o檔案得到子產品目标檔案hello.ko,最後離開Linux核心所在的目錄。

中間生成的hello.mod.c檔案的源代碼如下所示:

#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
 
MODULE_INFO(vermagic, VERMAGIC_STRING);

struct module _ _this_module
_ _attribute_ _((section(".gnu.linkonce.this_module"))) = {
 .name = KBUILD_MODNAME,
 .init = init_module,
 #ifdef CONFIG_MODULE_UNLOAD
  .exit = cleanup_module,
 #endif
};

static const char _ _module_depends[]
_ _attribute_used_ _
_ _attribute_ _((section(".modinfo"))) =
"depends=";
           

hello.mod.o産生了ELF的兩個節,即modinfo和.gun.linkonce.this_module。

如果一個子產品包括多個.c檔案,則應該如下寫Makefile:

obj-m := modulename.o
module-objs := file1.o file2.o
           

參考資料:《linux裝置驅動開發詳解》 宋寶華