最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Linux內(nèi)核模塊如何編譯?(有這篇文就足夠~)

2022-06-10 17:01 作者:補給站Linux內(nèi)核  | 我要投稿

本文主要介紹如何在內(nèi)核外編譯內(nèi)核模塊,

即: how to build an out-of-tree kernel module.

1. 代碼hello.c

2. Makefile

3. 編譯測試

make sudo insmod hello.ko dmesg sudo rmmod hello

  • 內(nèi)核信息如下:

[156596.317933] hello world. [156604.933930] hello exit!


【文章福利】小編推薦自己的Linux內(nèi)核技術交流群:【891587639】整理了一些個人覺得比較好的學習書籍、視頻資料共享在群文件里面,有需要的可以自行添加哦?。。。ê曨l教程、電子書、實戰(zhàn)項目及代碼)? ? ?


4. Makefile分析

4.1 多個源文件

  • 如果你有一個模塊名為 module.ko, 是來自 多個源文件( 姑且稱之為, file1.c 和 file2.c ), 正確的書寫應當是:

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



4.2 clean

rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.order

  • 可以換成:

make -C $(KDIR) M=$(PWD) clean


4.3 KERNELRELEASE

  • 這個 makefile 在一次典型的建立中要被讀 2 次。


  • KERNELRELEASE是在內(nèi)核源碼的頂層Makefile中定義的一個變量,在第一次(剛開始)讀取執(zhí)行此Makefile時,KERNELRELEASE沒有被定義(為空),所以make將讀取執(zhí)行else之后的內(nèi)容。如果make的目標是clean,直接執(zhí)行clean操作,然后結束。當make的目標為all時,-C(KDIR)指明跳轉(zhuǎn)到內(nèi)核源碼目錄下讀取那里的Makefile,M=(PWD)表明然后返回到當前目錄繼續(xù)讀入、執(zhí)行當前的Makefile。當從內(nèi)核源碼目錄返回時,KERNELRELEASE已被被定義,kbuild也被啟動去解析kbuild語法的語句,make將繼續(xù)讀取else之前的內(nèi)容(指 obj-m :=hello.o)。else之前的內(nèi)容為kbuild語法的語句,指明模塊源碼中各文件的依賴關系,以及要生成的目標模塊名。

4.4 obj-m

obj-m :=hello.o表示編譯連接后將生成hello.o模塊。 module-objs := file1.o file2.o file3.o表示module.o 由file1.o,file2.o與file3.o 鏈接生成。

  • 在modules.txt中提到:

obj-m := <module_name>.o


The kbuild system will build <module_name>.o from <module_name>.c, and, after linking, will result in the kernel module <module_name>.ko. The above line can be put in either a "Kbuild" file or a "Makefile." When the module is built from multiple sources, an additional line is needed listing the files: <module_name>-y := <src1>.o <src2>.o ...

4.5 $(KDIR)

/lib/modules/$(shell uname -r)/build 是內(nèi)核top makefile所在路徑。 $(shell uname -r):是調(diào)用shell命令顯示內(nèi)核版本,在我系統(tǒng)上是4.4.0-109-generic

4.6 kbuild makefile

  • Kbuild系統(tǒng)使用Kbuild Makefile來編譯內(nèi)核或模塊。當Kernel Makefile被解析完成后,Kbuild會讀取相關的Kbuild Makefile進行內(nèi)核或模塊的編譯。Kbuild Makefile有特定的語法指定哪些編譯進內(nèi)核中、哪些編譯為模塊、及對應的源文件是什么等。內(nèi)核及驅(qū)動開發(fā)人員需要編寫這個Kbuild Makefile文件。

  • 目標定義是Kbuild Makefile的主要部分,也是核心部分。主要是定義了要編譯的文件,所有的選項,以及到哪些子目錄去執(zhí)行遞歸操作。 最簡單的Kbuild makefile 只包含一行:

例子: obj-y += foo.o

  • 該例子告訴Kbuild在這目錄里,有一個名為foo.o的目標文件。foo.o將從foo.c 或foo.S文件編譯得到。 如果foo.o要編譯成一模塊,那就要用obj-m了。所采用的形式如下:

例子: obj-$(CONFIG_FOO) += foo.o

  • $(CONFIG_FOO)可以為y(編譯進內(nèi)核) 或m(編譯成模塊)。如果CONFIG_FOO不是y 和m,那么該文件就不會被編譯聯(lián)接了。

4.7 -C $KDIR and M=$PWD

  • 以下在內(nèi)核文檔/kbuild/modules.txt中有介紹。

-C $KDIR The directory where the kernel source is located. "make" will actually change to the specified directory when executing and will change back when finished. M=$PWD Informs kbuild that an external module is being built. The value given to "M" is the absolute path of the directory where the external module (kbuild file) is located.

4.8 modules

When building an external module, only a subset of the "make" targets are available. make -C $KDIR M=$PWD [target] The default will build the module(s) located in the current directory, so a target does not need to be specified. All output files will also be generated in this directory. No attempts are made to update the kernel source, and it is a precondition that a successful "make" has been executed for the kernel. modules The default target for external modules. It has the same functionality as if no target was specified. See description above. modules_install Install the external module(s). The default location is /lib/modules/<kernel_release>/extra/, but a prefix may be added with INSTALL_MOD_PATH (discussed in section 5). clean Remove all generated files in the module directory only. help List the available targets for external modules.




Linux內(nèi)核模塊如何編譯?(有這篇文就足夠~)的評論 (共 條)

分享到微博請遵守國家法律
天等县| 石河子市| 宕昌县| 盐池县| 育儿| 汝州市| 色达县| 西丰县| 镇江市| 舞阳县| 祁连县| 宜州市| 阳西县| 百色市| 大连市| 玉门市| 灵石县| 海盐县| 台山市| 晴隆县| 丽江市| 平顺县| 疏附县| 长宁区| 长沙市| 巫溪县| 岳池县| 老河口市| 扎赉特旗| 卫辉市| 榆林市| 永清县| 邯郸县| 定西市| 龙门县| 元江| 攀枝花市| 成安县| 曲周县| 六枝特区| 宁强县|