简体   繁体   English

在汇编器中编写 x86_64 linux 内核模块

[英]Writing x86_64 linux kernel module in assembler

I try write simple kernel module (v3.6) in nasm, but insmod say me:我尝试在 nasm 中编写简单的内核模块 (v3.6),但 insmod 告诉我:

$ sudo insmod  ./hello.ko
insmod: ERROR: could not insert module ./hello.ko: Invalid module format
$ echo $?
1

I compile my code with:我编译我的代码:

$ nasm -f elf64 -o hello.m hello.asm
$ ld -m elf_x86_64 -r -o hello.ko hello.m

and my module code:和我的模块代码:

section .modinfo
    __mod_kernel_version db "kernel_version=3.6.8", 0
    __mod_license        db "license=GPL", 0
    __mod_author         db "author=actics", 0
    __mod_description    db "description=hello world module in nasm", 0


section .data
    init_mess    db "init_module", 10, 0
    cleanup_mess db "cleanup_module", 10, 0


section .text
    global init_module
    global cleanup_module

    extern printk

init_module:
    push rbp
    mov rbp, rsp

    xor rax, rax
    mov rdi, init_mess
    call printk

    xor rax, rax
    mov rsp, rbp
    pop rbp
    ret

cleanup_module:
    push rbp
    mov rbp, rsp

    xor rax, rax
    mov rdi, cleanup_mess
    call printk

    xor rax, rax
    mov rsp, rbp
    pop rbp
    ret

Please help.请帮忙。 In internets i found 1 link with equal code for 2.4, but he doesnt work too My system - archlinux with 3.6.8 kernel在互联网上我发现 1 个链接与 2.4 的相同代码,但他也不起作用我的系统 - archlinux 与 3.6.8 内核

UPDATE: in nasm forum i find thread with interesting solution.更新:在 nasm 论坛中,我找到了有趣的解决方案。 It's work for me, if my module do return 0 and exit:) But if i try add "extern printk" the insmod say me next:它对我有用,如果我的模块确实返回 0 并退出 :) 但是如果我尝试添加“extern printk”,insmod 接下来会告诉我:

ERROR: could not insert module hello.ko: Invalid parameters

What i'm doing wrong?我做错了什么? my code:我的代码:

[bits 64]

global init_module
global cleanup_module

;extern printk

section .modinfo
    __mod_description8  db   'description=Simple module',0
    align 16,db 0
    __mod_author7       db   'author=That´s me',0
    __mod_license6      db   'license=GPL',0
    align 16,db 0
    __module_depends    db   'depends=',0
    align 32,db 0
    __mod_vermagic5     db   'vermagic=3.6.8-1-ARCH SMP preempt mod_unload modversions ',0   ;from a .ko module of my system

section __versions
    ____versions      db   0xdf, 0xbc, 0xbf, 0x8c, 0, 0, 0, 0, "module_layout"   ;from a .ko module of my system
    align 64,db 0

section .data
    init_mess    db "init_module", 10, 0
    cleanup_mess db "cleanup_module", 10, 0


section .text

init_module:
    xor rax, rax
    ret

cleanup_module:
    xor rax, rax
    ret

section .gnu.linkonce.this_module
    times 24 db 0
__name:         db  'Simple', 0
    times (0x168 - 24 - 7) db 0
__init_module:      dq  init_module
    times 0x2ac db 0
__cleanup_module:   dq  cleanup_module
    times 1152 db 0

this code work with: nasm -f elf64 hello.asm -o hello.o此代码适用于:nasm -f elf64 hello.asm -o hello.o

but if i uncomment printk this no working!)但如果我取消注释 printk 这不起作用!)

What I did was write a small C wrapper using the standard module macros and link it with the main module code that's written in asm.我所做的是使用标准模块宏编写一个小型 C 包装器,并将其与用 asm 编写的主模块代码链接起来。 Use the normal kernel build system to build it.使用正常的内核构建系统来构建它。

module.c:模块.c:

    #include <linux/module.h>
    MODULE_AUTHOR("A. U. Thor");
    MODULE_DESCRIPTION("Description");
    MODULE_LICENSE("GPL");
    extern int asm_init(void);
    int main_init(void)
    {
        return asm_init();
    }
    module_init(main_init);

main.asm:主要汇编程序:

    [bits 64]
    global asm_init
    asm_init:
        xor rax, rax
        ret

Makefile:生成文件:

obj-m += test.o
test-objs := module.o main.o
$(KBUILD_EXTMOD)/main.o: main.asm
        nasm -f elf64 -o $@ $^

obj-m += memory_asm.o
memory_asm-objs := module.o main.o
$(KBUILD_EXTMOD)/main.o: $(src)/main.asm
    nasm -f elf64 -o $@ $^ && echo "" > $(src)/.main.o.cmd

Build using command: make -C <path_to_kernel_src> M=$PWD使用命令构建: make -C <path_to_kernel_src> M=$PWD

change the Makefile to:将 Makefile 更改为:

obj-m += memory_asm.o
memory_asm-objs := module.o main.o
$(KBUILD_EXTMOD)/main.o: $(src)/main.asm
    nasm -f elf64 -o $@ $^ && echo "" > $(src)/.main.o.cmd

k you have to use gcc to compile it k and link it with the kernel directory... k 你必须使用 gcc 来编译它 k 并将它链接到内核目录...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM