204-module_strip.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Subject: [PATCH] build: add a hack for removing non-essential module info
  3. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  4. ---
  5. --- a/include/linux/module.h
  6. +++ b/include/linux/module.h
  7. @@ -169,6 +169,7 @@ void trim_init_extable(struct module *m)
  8. /* Generic info of form tag = "info" */
  9. #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
  10. +#define MODULE_INFO_STRIP(tag, info) __MODULE_INFO_STRIP(tag, tag, info)
  11. /* For userspace: you can also call me... */
  12. #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
  13. @@ -212,12 +213,12 @@ void trim_init_extable(struct module *m)
  14. * Author(s), use "Name <email>" or just "Name", for multiple
  15. * authors use multiple MODULE_AUTHOR() statements/lines.
  16. */
  17. -#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  18. +#define MODULE_AUTHOR(_author) MODULE_INFO_STRIP(author, _author)
  19. /* What your module does. */
  20. -#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  21. +#define MODULE_DESCRIPTION(_description) MODULE_INFO_STRIP(description, _description)
  22. -#ifdef MODULE
  23. +#if defined(MODULE) && !defined(CONFIG_MODULE_STRIPPED)
  24. /* Creates an alias so file2alias.c can find device table. */
  25. #define MODULE_DEVICE_TABLE(type, name) \
  26. extern const typeof(name) __mod_##type##__##name##_device_table \
  27. @@ -244,7 +245,9 @@ extern const typeof(name) __mod_##type##
  28. */
  29. #if defined(MODULE) || !defined(CONFIG_SYSFS)
  30. -#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
  31. +#define MODULE_VERSION(_version) MODULE_INFO_STRIP(version, _version)
  32. +#elif defined(CONFIG_MODULE_STRIPPED)
  33. +#define MODULE_VERSION(_version) __MODULE_INFO_DISABLED(version)
  34. #else
  35. #define MODULE_VERSION(_version) \
  36. static struct module_version_attribute ___modver_attr = { \
  37. @@ -266,7 +269,7 @@ extern const typeof(name) __mod_##type##
  38. /* Optional firmware file (or files) needed by the module
  39. * format is simply firmware file name. Multiple firmware
  40. * files require multiple MODULE_FIRMWARE() specifiers */
  41. -#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
  42. +#define MODULE_FIRMWARE(_firmware) MODULE_INFO_STRIP(firmware, _firmware)
  43. /* Given an address, look for it in the exception tables */
  44. const struct exception_table_entry *search_exception_tables(unsigned long add);
  45. --- a/include/linux/moduleparam.h
  46. +++ b/include/linux/moduleparam.h
  47. @@ -16,6 +16,16 @@
  48. /* Chosen so that structs with an unsigned long line up. */
  49. #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
  50. +/* This struct is here for syntactic coherency, it is not used */
  51. +#define __MODULE_INFO_DISABLED(name) \
  52. + struct __UNIQUE_ID(name) {}
  53. +
  54. +#ifdef CONFIG_MODULE_STRIPPED
  55. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name)
  56. +#else
  57. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info)
  58. +#endif
  59. +
  60. #ifdef MODULE
  61. #define __MODULE_INFO(tag, name, info) \
  62. static const char __UNIQUE_ID(name)[] \
  63. @@ -23,8 +33,7 @@ static const char __UNIQUE_ID(name)[]
  64. = __stringify(tag) "=" info
  65. #else /* !MODULE */
  66. /* This struct is here for syntactic coherency, it is not used */
  67. -#define __MODULE_INFO(tag, name, info) \
  68. - struct __UNIQUE_ID(name) {}
  69. +#define __MODULE_INFO(tag, name, info) __MODULE_INFO_DISABLED(name)
  70. #endif
  71. #define __MODULE_PARM_TYPE(name, _type) \
  72. __MODULE_INFO(parmtype, name##type, #name ":" _type)
  73. @@ -32,7 +41,7 @@ static const char __UNIQUE_ID(name)[]
  74. /* One for each parameter, describing how to use it. Some files do
  75. multiple of these per line, so can't just use MODULE_INFO. */
  76. #define MODULE_PARM_DESC(_parm, desc) \
  77. - __MODULE_INFO(parm, _parm, #_parm ":" desc)
  78. + __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc)
  79. struct kernel_param;
  80. --- a/init/Kconfig
  81. +++ b/init/Kconfig
  82. @@ -2046,6 +2046,13 @@ config MODULE_COMPRESS_XZ
  83. endchoice
  84. +config MODULE_STRIPPED
  85. + bool "Reduce module size"
  86. + depends on MODULES
  87. + help
  88. + Remove module parameter descriptions, author info, version, aliases,
  89. + device tables, etc.
  90. +
  91. endif # MODULES
  92. config MODULES_TREE_LOOKUP
  93. --- a/kernel/module.c
  94. +++ b/kernel/module.c
  95. @@ -2880,6 +2880,7 @@ static void check_modinfo_retpoline(stru
  96. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  97. {
  98. +#ifndef CONFIG_MODULE_STRIPPED
  99. const char *modmagic = get_modinfo(info, "vermagic");
  100. int err;
  101. @@ -2911,6 +2912,7 @@ static int check_modinfo(struct module *
  102. pr_warn("%s: module is from the staging directory, the quality "
  103. "is unknown, you have been warned.\n", mod->name);
  104. }
  105. +#endif
  106. /* Set up license info based on the info section */
  107. set_license(mod, get_modinfo(info, "license"));
  108. --- a/scripts/mod/modpost.c
  109. +++ b/scripts/mod/modpost.c
  110. @@ -1966,7 +1966,9 @@ static void read_symbols(char *modname)
  111. symname = remove_dot(info.strtab + sym->st_name);
  112. handle_modversions(mod, &info, sym, symname);
  113. +#ifndef CONFIG_MODULE_STRIPPED
  114. handle_moddevtable(mod, &info, sym, symname);
  115. +#endif
  116. }
  117. if (!is_vmlinux(modname) ||
  118. (is_vmlinux(modname) && vmlinux_section_warnings))
  119. @@ -2110,7 +2112,9 @@ static void add_header(struct buffer *b,
  120. buf_printf(b, "#include <linux/vermagic.h>\n");
  121. buf_printf(b, "#include <linux/compiler.h>\n");
  122. buf_printf(b, "\n");
  123. +#ifndef CONFIG_MODULE_STRIPPED
  124. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  125. +#endif
  126. buf_printf(b, "\n");
  127. buf_printf(b, "__visible struct module __this_module\n");
  128. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  129. @@ -2127,8 +2131,10 @@ static void add_header(struct buffer *b,
  130. static void add_intree_flag(struct buffer *b, int is_intree)
  131. {
  132. +#ifndef CONFIG_MODULE_STRIPPED
  133. if (is_intree)
  134. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  135. +#endif
  136. }
  137. /* Cannot check for assembler */
  138. @@ -2141,10 +2147,12 @@ static void add_retpoline(struct buffer
  139. static void add_staging_flag(struct buffer *b, const char *name)
  140. {
  141. +#ifndef CONFIG_MODULE_STRIPPED
  142. static const char *staging_dir = "drivers/staging";
  143. if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
  144. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  145. +#endif
  146. }
  147. /* In kernel, this size is defined in linux/module.h;
  148. @@ -2248,11 +2256,13 @@ static void add_depends(struct buffer *b
  149. static void add_srcversion(struct buffer *b, struct module *mod)
  150. {
  151. +#ifndef CONFIG_MODULE_STRIPPED
  152. if (mod->srcversion[0]) {
  153. buf_printf(b, "\n");
  154. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  155. mod->srcversion);
  156. }
  157. +#endif
  158. }
  159. static void write_if_changed(struct buffer *b, const char *fname)
  160. @@ -2487,7 +2497,9 @@ int main(int argc, char **argv)
  161. add_staging_flag(&buf, mod->name);
  162. err |= add_versions(&buf, mod);
  163. add_depends(&buf, mod, modules);
  164. +#ifndef CONFIG_MODULE_STRIPPED
  165. add_moddevtable(&buf, mod);
  166. +#endif
  167. add_srcversion(&buf, mod);
  168. sprintf(fname, "%s.mod.c", mod->name);