Maybe I was not clear - I have no responses of my functions ( init_module, cleanup_module) anywere, because they are not executed - what else ?
ok, seems my english is so crappy that you don't understand me
The problem
IS IN OFFSETS of init/exit references in
.gnu.linkonce.this_module.
This section very-very-very sensitive to kernel configuration. So that every new configuration may not allow
you to load module being compiled for same kernel but with different configuration.
Lets simplify the things. Check out any existing module you have in your /lib/modules/. For example on my 64bit custom
linux system I have /lib/modules/2.6.34/kernel/drivers/leds/ledtrig-default-on.ko.
So I eat it to objdump -x to find offsets of init/exit functions in .gnu.linkonce.this_module. For my particular kernel they are placed
to
RELOCATION RECORDS FOR [.gnu.linkonce.this_module]:
OFFSET TYPE VALUE
0000000000000148 R_X86_64_64 init_module
0000000000000238 R_X86_64_64 cleanup_module
So for init it's 0x148 and for exit 0x238. Now I can modify the source code of our asm file this way:
section .gnu.linkonce.this_module
__this:
pad 24
__name: db 'Simple', 0
pad 0x148
__init_module: dq init_module
pad 0x238
__cleanup_module: dq cleanup_module
pad 1152
The "pad" here is a macro which yields 0x0 up to aligned value. Ie it's
%imacro pad 1-2.nolist
%ifnempty %2
%define __PAD_VALUE %2
%else
%define __PAD_VALUE 0
%endif
%if ($-$$) == 0
times (%1) db __PAD_VALUE
%else
times (((%1) - (($-$$) % (%1))) % (%1)) db __PAD_VALUE
%endif
%endmacro
So after this modification I see in dmesg output the module's init/exit being called
[12213.055821] init_module
[12217.929404] cleanup_module
And eventually the whole source code.
%imacro pad 1-2.nolist
%ifnempty %2
%define __PAD_VALUE %2
%else
%define __PAD_VALUE 0
%endif
%if ($-$$) == 0
times (%1) db __PAD_VALUE
%else
times (((%1) - (($-$$) % (%1))) % (%1)) db __PAD_VALUE
%endif
%endmacro
[bits 64]
extern printk
section .modinfo
__mod_description8 db 'description=Simple module',0
pad 16
__mod_author7 db 'author=That´s me',0
__mod_license6 db 'license=GPL',0
pad 16
__module_depends db 'depends=',0
pad 32
__mod_vermagic5 db 'vermagic=2.6.34 SMP preempt mod_unload ',0 ;from a .ko module of my system
section __versions
____versions db 0x9d,'V',0x1e,'$struct_module',0 ;from a .ko module of my system
pad 64
section .data
fmt db '%s', 0
init_t db '<0> init_module', 0xA, 0
exit_t db '<0> cleanup_module', 0xA, 0
__this_module dq __this
section .exit.text exec
global cleanup_module
cleanup_module:
mov eax, 0
mov rdx, 2
mov rdi, exit_t
mov rsi, fmt
call printk
mov eax, 0
ret
section .init.text exec
global init_module
init_module:
mov eax, 0
mov rdx, 2
mov rdi, init_t
mov rsi, fmt
call printk
mov eax, 0
ret
section .gnu.linkonce.this_module
__this:
pad 24
__name: db 'Simple', 0
pad 0x148
__init_module: dq init_module
pad 0x238
__cleanup_module: dq cleanup_module
pad 1152
Note that it's for x86-64 system.
Hope this helps.