kernel module in not loaded (but insmod returns 0)

2019-07-08 08:41发布

I've to add some functionality to an existing device (mips arch) - I've tryed several SDK's and at this moment I have some progress, but: insmod returns 0(success) and lsmod shows them, but aither printk nor create_proc_entry doesn't work .... BUT I've viewed section .gnu.linkonce.this_module: except module name - there is no usefull info - section is filled with 0x0's

I've found that in native .ko files in device size of section .gnu.linkonce.this_module is smaller into 8 bytes - but according to the fact that section is used for temporary loading info to struct module - there is not matter in my oppinion ...

https://ufile.io/eco1s there are several files: khelloworld.ko - my helloworld module - try to create procfs entry khelloworld.ko - try to create file in rootfs (/tmp/test.file) native modules: xt_mark.ko md5.ko cbc.ko

I don't have kernel configuration - but I need to compile that module... I know only the version

#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;
}

1条回答
该账号已被封号
2楼-- · 2019-07-08 09:03

readelf -a shows that the relocation entry for your init function is different than in the native module case:

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

Note how for the native modules, the init_module pointer is in offset 0xbc of the module struct, whereas in your module it is in offset 0xac. As a result, the loader does not find your init function and does not call it.

As explained here, this is likely the result of a kernel configuration difference between your build environment and the native build environment. It's very likely that CONFIG_UNUSED_SYMBOLS is the culprit (see the module definition here).

Alternatively, you can (at your own risk!) apply a binary patch to your resulting module to change the 0xac to 0xbc.

查看更多
登录 后发表回答