2017-12-21 33 views
2

私はいくつかのSDKを試しましたが、今のところ進歩はありますが、 insmodが返します0(成功)とlsmodはそれらを示していますが、printkもcreate_proc_entryも動作しません....しかし、私はセクション.gnu.linkonce.this_moduleを見ました:モジュール名を除く - 有用な情報はありません - セクションは0x0カーネルモジュールがロードされていない(しかし、insmodは0を返します)

セクション.gnu.linkonce.this_moduleのデバイスサイズがネイティブの.koファイルであることが判明しました。これは8バイトになりますが、セクションが構造モジュールへの一時的な読み込み情報として使用されているという事実によると私の意見に関係なく...

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; 
} 
+1

ようこそスタックオーバーフロー!ここでは、問題のコードやその他の情報(ビルドログなど)を**リンクされていない**質問の投稿自体に含めます。あなたの質問を編集して、その問題を修正してください。 – Tsyvarev

答えて

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 

ネイティブモジュールの場合は、構造体のオフセット0xbcにinit_moduleポインタがありますが、モジュールではオフセット0xacにあることに注意してください。その結果、ローダーはあなたのinit関数を見つけず、それを呼び出さない。

説明したように、これはおそらく、ビルド環境とネイティブのビルド環境のカーネル構成の違いの結果である可能性があります。 CONFIG_UNUSED_SYMBOLSが原因である可能性が非常に高いです(module定義hereを参照)。

また、結果のモジュールにバイナリパッチを適用して、0xacを0xbcに変更することもできます(リスクはあります)。

関連する問題