首页 文章

为Linux内核编写内置对象?

提问于
浏览
3

无论我在哪里搜索Linux内核开发,我都会得到创建Linux内核模块的答案 . 例

/*
* 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来实现 .

但我不想要这个 . 我想添加一个内置程序,而不是一个驱动程序,基本上是一个服务,以促进从内核级别上传一些数据的 Cloud . 我不想在编译内核时想要程序的模块选项 .

我理解的只是程序我应该使用obj-y而不是obj-m . 但是没有手册来编写这类程序 . 为什么?我错过了什么吗?这些程序是否也具有init_module和cleanup_module函数,即使它们不是模块?

2 回答

  • 3

    例如,考虑您的源在linux内核源代码树中的 driver/new 下 . 您需要在 driversnew 下修改 Makefile's 以将模块静态构建到Linux内核中 .

    drivers/Makefile 下,在末尾添加以下行 .

    obj-y   += new/
    

    drivers/new/Makefile 下,在末尾添加以下行 .

    obj-y   += hello.o
    

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

    注意:在将模块静态构建到linux中时,请进行更改

    int init_module(void)
    

    int __init init_module(void)
    

    并改变

    void cleanup_module(void)
    

    void __exit cleanup_module(void)
    
  • 0

    看看kernel doc Makefiles

    参考:

    “--- 3.2内置对象目标 - 对象

    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
    

相关问题