assembly – 在汇编程序中编写x86_64 linux内核模块
发布时间:2020-12-14 02:21:35  所属栏目:Linux  来源:网络整理 
            导读:我尝试在nasm中编写简单的内核模块(v3.6),但insmod说我: $sudo insmod ./hello.koinsmod: ERROR: could not insert module ./hello.ko: Invalid module format$echo $?1 我编译我的代码: $nasm -f elf64 -o hello.m hello.asm$ld -m elf_x86_64 -r -o hell
                
                
                
            | 
                         我尝试在nasm中编写简单的内核模块(v3.6),但insmod说我: 
  
  
  
$sudo insmod ./hello.ko insmod: ERROR: could not insert module ./hello.ko: Invalid module format $echo $? 1 我编译我的代码: $nasm -f elf64 -o hello.m hello.asm $ld -m elf_x86_64 -r -o hello.ko hello.m 和我的模块代码: 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",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,cleanup_mess
    call printk
    xor rax,rbp
    pop rbp
    ret 
 请帮忙.在互联网中,我找到了1个与2.4相同代码的链接,但他也没有工作 更新: ERROR: could not insert module hello.ko: Invalid parameters 我做错了什么?我的代码: [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',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,"module_layout"   ;from a .ko module of my system
    align 64,db 0
section .data
    init_mess    db "init_module",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 
 此代码适用于: 但如果我取消注释printk这没有用!) 解决方法
 我所做的是使用标准模块宏编写一个小的C包装器,并将其与用asm编写的主模块代码链接起来.使用普通的内核构建系统来构建它. 
  
  
        的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 $@ $^ 
 使用命令构建:make -C< path_to_kernel_src> M = $PWD (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
相关内容
- 在ZSH中设置环境变量给出了预期的数量
 - Cannot create GC thread. Out of system resources.
 - linux – 有没有办法杀死在某个目录中运行的所有进程?
 - [i.MX]飞思卡尔IMX6处理器的GPIO-IOMUX_PAD说明
 - 如何获取使用PowerShell创建的新Azure Linux虚拟机的ssh主机
 - linux – 使用postfix的Google Cloud Computing实例VM上的a
 - linux – 允许用户只对特定帐户进行sudo
 - nginx的The page you are looking for is temporarily unav
 - 如何在命令行中阻止zsh扩展*(星号)?
 - linux – 在VSFTP上载中设置文件权限
 
