Linux Kernel Architecture

(Jacob Rumans) #1

Chapter7:Modules


#ifdef MODULE
static int __init xyz_init(void) {
/* Initialization code */
}

static void __exit xyz_cleanup (void) {
/* Cleanup code */
}

module_init(xyz_init);
module_exit(xyz_exit);
#endif

The__initand__exitprefixes help place the two functions in the right sections of the binary code:


#define __init __attribute__ ((__section__ (".init.text"))) __cold
#define __initdata __attribute__ ((__section__ (".init.data")))
#define __exitdata __attribute__ ((__section__(".exit.data")))
#define __exit_call __attribute_used__ __attribute__ ((__section__ (".exitcall.exit")))
Thedatavariants are used to place data (in contrast to functions) in the.initand.exitsection.

Exporting Symbols


The kernel provides two macros for exporting symbols —EXPORT_SYMBOLandEXPORT_SYMBOL_GPL.As
their names suggest, a distinction is made between exporting general symbols and exporting symbols
that may be used only by GPL-compatible code. Again, their purpose is to place the symbols in the
appropriate section of the module binary image:

<module.h>
/* For every exported symbol, place a struct in the __ksymtab section */
#define __EXPORT_SYMBOL(sym, sec) \
extern typeof(sym) sym; \
__CRC_SYMBOL(sym, sec) \
static const char __kstrtab_##sym[] \
__attribute__((section("__ksymtab_strings"))) \
= MODULE_SYMBOL_PREFIX #sym; \
static const struct kernel_symbol __ksymtab_##sym \
__attribute_used__ \
__attribute__((section("__ksymtab" sec), unused)) \

#define EXPORT_SYMBOL(sym) \
__EXPORT_SYMBOL(sym, "")

#define EXPORT_SYMBOL_GPL(sym) \
__EXPORT_SYMBOL(sym, "_gpl")

#define EXPORT_SYMBOL_GPL_FUTURE(sym) \
__EXPORT_SYMBOL(sym, "_gpl_future")
Free download pdf