内核模块未加载(但insmod返回0)

问题描述 投票:2回答:1

我要为现有设备添加一些功能(mips arch) - 我尝试了几个SDK,此时我有一些进展,但是:insmod返回0(成功)并且lsmod显示它们,但是printk和create_proc_entry都没有工作....但我查看了部分.gnu.linkonce.this_module:除了模块名称 - 没有有用的信息 - 部分填充0x0的

我发现在设备大小为.gnu.linkonce.this_module的原生.ko文件中,小到8个字节 - 但根据这个部分用于临时加载信息到struct模块的事实 - 我的问题无关紧要意见......

https://ufile.io/eco1s有几个文件:khelloworld.ko - 我的helloworld模块 - 尝试创建procfs条目khelloworld.ko - 尝试在rootfs(/tmp/test.file)本机模块中创建文件:xt_mark.ko md5.ko cbc.ko

我没有内核配置 - 但我需要编译该模块...我只知道版本

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>  /* Necessary because we use the proc fs */
#include <linux/init.h>     /* Needed for the macros */

#define procfs_name "khelloworld"


MODULE_LICENSE("GPL");
MODULE_INFO(vermagic, "2.6.32.68 mod_unload MIPS32_R2 32BIT ");
MODULE_AUTHOR     ("XAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");


struct proc_dir_entry *Our_Proc_File;

static int
procfile_read(char *buffer,
      char **buffer_location,
      off_t offset, int buffer_length, int *eof, void *data);

    static int __init khelloworld_init( void ) {
    printk(KERN_INFO "try to create /proc \n"); 
    Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);

    if (Our_Proc_File == NULL) {
        remove_proc_entry(procfs_name, NULL);
        printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
           procfs_name);
        return -ENOMEM;
    }

    Our_Proc_File->read_proc = procfile_read;
    Our_Proc_File->owner     = THIS_MODULE;
    Our_Proc_File->mode      = S_IFREG | S_IRUGO;
    Our_Proc_File->uid       = 0;
    Our_Proc_File->gid       = 0;
    Our_Proc_File->size      = 37;

    printk(KERN_INFO "/proc/%s created\n", procfs_name);    
    return 3;   /* everything is ok */
}

static void __exit khelloworld_exit( void ) {
    remove_proc_entry(procfs_name, NULL);
    printk(KERN_INFO "/proc/%s removed\n", procfs_name);
}


module_init(khelloworld_init);
module_exit(khelloworld_exit);

int
procfile_read(char *buffer,
      char **buffer_location,
      off_t offset, int buffer_length, int *eof, void *data)
{
    int ret;

    printk(KERN_INFO "procfile_read (/proc/%s) called\n", procfs_name);

    /* 
     * We give all of our information in one go, so if the
     * user asks us if we have more information the
     * answer should always be no.
     *
     * This is important because the standard read
     * function from the library would continue to issue
     * the read system call until the kernel replies
     * that it has no more information, or until its
     * buffer is filled.
     */
    if (offset > 0) {
        /* we have finished to read, return 0 */
        ret  = 0;
    } else {
        /* fill the buffer, return the buffer size */
        ret = sprintf(buffer, "HelloWorld!\n");
    }

    return ret;
}
linux-kernel mips router mips32
1个回答
3
投票

readelf -a显示init函数的重定位条目与本机模块大小写不同:

xt_mark.ko

Relocation section '.rel.gnu.linkonce.this_module' at offset 0x958 contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
000000bc  00001502 R_MIPS_32         00000000   init_module
00000130  00001402 R_MIPS_32         00000000   cleanup_module


khelloworld.ko

Relocation section '.rel.gnu.linkonce.this_module' at offset 0xafc contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
000000ac  00002502 R_MIPS_32         00000000   init_module
0000010c  00002402 R_MIPS_32         00000000   cleanup_module

请注意,对于本机模块,init_module指针位于module结构的偏移量0xbc中,而在模块中,它位于偏移量0xac中。因此,加载程序找不到您的init函数,也不会调用它。

正如here所解释的,这可能是构建环境和本机构建环境之间内核配置差异的结果。 CONFIG_UNUSED_SYMBOLS很可能是罪魁祸首(参见module定义here)。

或者,您可以(风险自负!)将二进制补丁应用于生成的模块,将0xac更改为0xbc。

© www.soinside.com 2019 - 2024. All rights reserved.