最終更新:2015-10-27 (火) 14:42:17 (3097d)  

module_init
Top / module_init

insmod時に呼ばれる関数を変更するマクロ

Linux 4.1

  • linux/init.h
  • #ifndef MODULE?
  • yのとき (カーネルにスタティックリンク)
    /**
     * module_init() - driver initialization entry point
     * @x: function to be run at kernel boot time or module insertion
     * 
     * module_init() will either be called during do_initcalls() (if
     * builtin) or at module insertion time (if a module).  There can only
     * be one per module.
     */
    #define module_init(x)  __initcall(x);
  • initcallなんとかというセクションに関数のアドレスを格納した変数が並べれられる
  • #else /* MODULE */
  • mのとき (カーネルモジュール)
    /* Each module must use one module_init(). */
    #define module_init(initfn)                                     \
            static inline initcall_t __inittest(void)               \
            { return initfn; }                                      \
            int init_module(void) __attribute__((alias(#initfn)));

Linux 4.2

呼ばれ方

  • The module_init() macro defines which function is to be called at module insertion time (if the file is compiled as a module), or at boot time.
  • if the file is not compiled as a module the module_init() macro becomes equivalent to __initcall(), which through linker magic ensures that the function is called on boot.

メモ

  • モジュールとしてコンパイルされた時はinsertion timeに呼ばれる
  • モジュールじゃないときは__initcallになる(initcall)
    #define __initcall(fn) device_initcall(fn)

関連

参考