Writing x86_64 linux kernel module in assembler

前端 未结 3 726
栀梦
栀梦 2021-02-06 01:29

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 f         


        
相关标签:
3条回答
  • 2021-02-06 02:06

    k you have to use gcc to compile it k and link it with the kernel directory...

    0 讨论(0)
  • 2021-02-06 02:07

    change the Makefile to:

    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
    
    0 讨论(0)
  • 2021-02-06 02:18

    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 $@ $^
    
    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

    0 讨论(0)
提交回复
热议问题