MODULE_INFO in Linux Device Driver
'modinfo' section in the .ko (ELF) file stores the Module information. MODULE_INFO is the macro which can be used in the source code to add information to this section
#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
#define __MODULE_INFO(tag, name, info) \
static const char __UNIQUE_ID(name)[] \
__used __attribute__((section(".modinfo"), unused, aligned(1))) \
= __stringify(tag) "=" info
modinfo utility lists each attribute of the module in form fieldname:value, for easy reading from the Linux Kernel modules given on the command line
#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
#define __MODULE_INFO(tag, name, info) \
static const char __UNIQUE_ID(name)[] \
__used __attribute__((section(".modinfo"), unused, aligned(1))) \
= __stringify(tag) "=" info
MODULE_INFO is used in several places by other macros such as
- license,
- alias,
- author,
- vermagic.
Some of the fields can be retrieved through the THIS_MODULE (struct module)
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/module.h> | |
#include <linux/kernel.h> | |
static int __init mod_init(void) | |
{ | |
/* Set by default based on the module file name. */ | |
pr_info("name = %s\n", THIS_MODULE->name); | |
pr_info("version = %s\n", THIS_MODULE->version); | |
/* ERROR: nope, not part of struct module. */ | |
/*pr_info("name = %s\n", THIS_MODULE->name);*/ | |
return 0; | |
} | |
static void __exit mod_exit(void) {} | |
module_init(mod_init) | |
module_exit(mod_exit) | |
MODULE_INFO(name, "Embedded"); | |
MODULE_INFO(OS, "Linux"); | |
MODULE_VERSION("1.0"); | |
MODULE_LICENSE("GPL"); |
Output:
modinfo utility lists each attribute of the module in form fieldname:value, for easy reading from the Linux Kernel modules given on the command line
In the above screenshot, we used readelf, to dump the contents of the modinfo section.
Comments
Post a Comment