写x86_64的Linux内核模块汇编 [英] Writing x86_64 linux kernel module in assembler

查看:282
本文介绍了写x86_64的Linux内核模块汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写在NASM简单的内核模块(V3.6),但insmod的说我:

I try write simple kernel module (v3.6) in nasm, but insmod say me:

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

我编译我的code有:

I compile my code with:

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

和我的模块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

请帮忙。在互联网络我发现等于code 1链接2.4,但他不工作太
我的系统 - 的archlinux与3.6.8内核

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

更新:
在NASM论坛上,我找到有趣的解决方案线程。这对我的工作,如果我的模块做回0并退出:)但如果我尝试添加外部的printk的insmod的说我接下来的:

UPDATE: in nasm forum i find thread with interesting solution. 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:

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

我在做什么错了?我的code:

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

本code的工作:
    NASM -f ELF64 hello.asm -o hello.o

this code work with: nasm -f elf64 hello.asm -o hello.o

但如果我取消printk该文件没有工作!)

but if i uncomment printk this no working!)

推荐答案

我所做的是使用标准的模块宏写一个小C包装并就该写在ASM主模块code链接。正常使用的内核构建系统来构建它。

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. Use the normal kernel build system to build it.

的module.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 $@ $^

构建使用命令:让-C&LT; path_to_kernel_src&GT; M = $ PWD

这篇关于写x86_64的Linux内核模块汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆