首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为Linux内核编写内置对象?

为Linux内核编写内置对象?
EN

Stack Overflow用户
提问于 2015-01-13 06:27:39
回答 2查看 1.8K关注 0票数 5

无论我在哪里搜索,我都可以找到创建Linux内核模块的答案。示例

代码语言:javascript
复制
 /*
* hello−1.c − The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}

这里有init_module和cleanup_module函数,我理解它们包含初始化和清理内核时要执行的内容。通过在makefile中添加obj-m += hello-1.c来实现。

但我不想这样。我想添加一个内置程序,而不是驱动程序,基本上是一个服务,以方便云端上传一些来自内核级别的数据。在编译内核时,我不想要程序的模块选项。

我理解只是程序,我应该使用obj-y而不是obj-m。但是没有编写这种程序的手册。为什么?我是不是遗漏了什么?这些程序是否也具有init_module和cleanup_module功能,即使它们不是模块?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-13 07:14:10

例如,假设您的源代码在linux内核源代码树中的driver/new下。您需要在driversnew下修改new来静态地将模块构建到Linux内核中。

drivers/Makefile下,将下面一行添加到末尾。

代码语言:javascript
复制
obj-y   += new/

drivers/new/Makefile下,将下面一行添加到末尾。

代码语言:javascript
复制
obj-y   += hello.o

在构建linux内核之后。并加载以查看您的模块已经使用printk命令打印了dmesg消息。

注意:当静态地将模块构建到linux中时,请更改

代码语言:javascript
复制
int init_module(void)

代码语言:javascript
复制
int __init init_module(void)

和改变

代码语言:javascript
复制
void cleanup_module(void)

代码语言:javascript
复制
void __exit cleanup_module(void)
票数 5
EN

Stack Overflow用户

发布于 2015-01-13 08:44:10

查看内核文档Makefiles

请参阅:

代码语言:javascript
复制
The kbuild Makefile specifies object files for vmlinux
in the $(obj-y) lists.  These lists depend on the kernel
configuration.

Kbuild compiles all the $(obj-y) files.  It then calls
"$(LD) -r" to merge these files into one built-in.o file.
built-in.o is later linked into vmlinux by the parent Makefile.

The order of files in $(obj-y) is significant.  Duplicates in
the lists are allowed: the first instance will be linked into
built-in.o and succeeding instances will be ignored.

Link order is significant, because certain functions
(module_init() / __initcall) will be called during boot in the
order they appear. So keep in mind that changing the link
order may e.g. change the order in which your SCSI
controllers are detected, and thus your disks are renumbered.

Example:
    #drivers/isdn/i4l/Makefile
    # Makefile for the kernel ISDN subsystem and device drivers.
    # Each configuration option enables a list of files.
    obj-$(CONFIG_ISDN_I4L)         += isdn.o
    obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27916116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档