204-module_strip.patch 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. From a779a482fb9b9f8fcdf8b2519c789b4b9bb5dd05 Mon Sep 17 00:00:00 2001
  2. From: Felix Fietkau <[email protected]>
  3. Date: Fri, 7 Jul 2017 16:56:48 +0200
  4. Subject: build: add a hack for removing non-essential module info
  5. Signed-off-by: Felix Fietkau <[email protected]>
  6. ---
  7. include/linux/module.h | 13 ++++++++-----
  8. include/linux/moduleparam.h | 15 ++++++++++++---
  9. init/Kconfig | 7 +++++++
  10. kernel/module.c | 5 ++++-
  11. scripts/mod/modpost.c | 12 ++++++++++++
  12. 5 files changed, 43 insertions(+), 9 deletions(-)
  13. --- a/include/linux/module.h
  14. +++ b/include/linux/module.h
  15. @@ -239,10 +239,10 @@ struct module_kobject *lookup_or_create_
  16. * Author(s), use "Name <email>" or just "Name", for multiple
  17. * authors use multiple MODULE_AUTHOR() statements/lines.
  18. */
  19. -#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  20. +#define MODULE_AUTHOR(_author) MODULE_INFO_STRIP(author, _author)
  21. /* What your module does. */
  22. -#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  23. +#define MODULE_DESCRIPTION(_description) MODULE_INFO_STRIP(description, _description)
  24. /*
  25. * Format: __mod_device_table__kmod_<modname>__<type>__<name>
  26. @@ -279,7 +279,9 @@ static typeof(name) __mod_device_table(t
  27. */
  28. #if defined(MODULE) || !defined(CONFIG_SYSFS)
  29. -#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
  30. +#define MODULE_VERSION(_version) MODULE_INFO_STRIP(version, _version)
  31. +#elif defined(CONFIG_MODULE_STRIPPED)
  32. +#define MODULE_VERSION(_version) __MODULE_INFO_DISABLED(version)
  33. #else
  34. #define MODULE_VERSION(_version) \
  35. MODULE_INFO(version, _version); \
  36. @@ -302,7 +304,7 @@ static typeof(name) __mod_device_table(t
  37. /* Optional firmware file (or files) needed by the module
  38. * format is simply firmware file name. Multiple firmware
  39. * files require multiple MODULE_FIRMWARE() specifiers */
  40. -#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
  41. +#define MODULE_FIRMWARE(_firmware) MODULE_INFO_STRIP(firmware, _firmware)
  42. #define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, ns)
  43. --- a/include/linux/moduleparam.h
  44. +++ b/include/linux/moduleparam.h
  45. @@ -24,6 +24,16 @@
  46. #define __MODULE_INFO_PREFIX KBUILD_MODNAME "."
  47. #endif
  48. +/* This struct is here for syntactic coherency, it is not used */
  49. +#define __MODULE_INFO_DISABLED(name) \
  50. + struct __UNIQUE_ID(name) {}
  51. +
  52. +#ifdef CONFIG_MODULE_STRIPPED
  53. +#define MODULE_INFO_STRIP(tag, info) __MODULE_INFO_DISABLED(tag)
  54. +#else
  55. +#define MODULE_INFO_STRIP(tag, info) MODULE_INFO(tag, info)
  56. +#endif
  57. +
  58. /* Generic info of form tag = "info" */
  59. #define MODULE_INFO(tag, info) \
  60. static const char __UNIQUE_ID(modinfo)[] \
  61. @@ -36,7 +46,7 @@
  62. /* One for each parameter, describing how to use it. Some files do
  63. multiple of these per line, so can't just use MODULE_INFO. */
  64. #define MODULE_PARM_DESC(_parm, desc) \
  65. - MODULE_INFO(parm, #_parm ":" desc)
  66. + MODULE_INFO_STRIP(parm, #_parm ":" desc)
  67. struct kernel_param;
  68. --- a/kernel/module/Kconfig
  69. +++ b/kernel/module/Kconfig
  70. @@ -462,4 +462,11 @@ config MODULES_TREE_LOOKUP
  71. def_bool y
  72. depends on PERF_EVENTS || TRACING || CFI
  73. +config MODULE_STRIPPED
  74. + bool "Reduce module size"
  75. + depends on MODULES
  76. + help
  77. + Remove module parameter descriptions, author info, version, aliases,
  78. + device tables, etc.
  79. +
  80. endif # MODULES
  81. --- a/kernel/module/main.c
  82. +++ b/kernel/module/main.c
  83. @@ -1075,6 +1075,7 @@ const size_t modinfo_attrs_count = ARRAY
  84. static const char vermagic[] = VERMAGIC_STRING;
  85. +#if defined(CONFIG_MODVERSIONS) || !defined(CONFIG_MODULE_STRIPPED)
  86. int try_to_force_load(struct module *mod, const char *reason)
  87. {
  88. #ifdef CONFIG_MODULE_FORCE_LOAD
  89. @@ -1086,6 +1087,7 @@ int try_to_force_load(struct module *mod
  90. return -ENOEXEC;
  91. #endif
  92. }
  93. +#endif
  94. /* Parse tag=value strings from .modinfo section */
  95. char *module_next_tag_pair(char *string, unsigned long *secsize)
  96. @@ -2579,9 +2581,11 @@ static void module_augment_kernel_taints
  97. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  98. {
  99. - const char *modmagic = get_modinfo(info, "vermagic");
  100. int err;
  101. +#ifndef CONFIG_MODULE_STRIPPED
  102. + const char *modmagic = get_modinfo(info, "vermagic");
  103. +
  104. if (flags & MODULE_INIT_IGNORE_VERMAGIC)
  105. modmagic = NULL;
  106. @@ -2595,6 +2599,7 @@ static int check_modinfo(struct module *
  107. info->name, modmagic, vermagic);
  108. return -ENOEXEC;
  109. }
  110. +#endif
  111. err = check_modinfo_livepatch(mod, info);
  112. if (err)
  113. --- a/scripts/mod/modpost.c
  114. +++ b/scripts/mod/modpost.c
  115. @@ -1619,7 +1619,9 @@ static void read_symbols(const char *mod
  116. symname = remove_dot(info.strtab + sym->st_name);
  117. handle_symbol(mod, &info, sym, symname);
  118. +#ifndef CONFIG_MODULE_STRIPPED
  119. handle_moddevtable(mod, &info, sym, symname);
  120. +#endif
  121. }
  122. check_sec_ref(mod, &info);
  123. @@ -1832,7 +1834,9 @@ static void add_header(struct buffer *b,
  124. buf_printf(b, "#include <linux/export-internal.h>\n");
  125. buf_printf(b, "#include <linux/compiler.h>\n");
  126. buf_printf(b, "\n");
  127. +#ifndef CONFIG_MODULE_STRIPPED
  128. buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
  129. +#endif
  130. buf_printf(b, "\n");
  131. buf_printf(b, "__visible struct module __this_module\n");
  132. buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
  133. @@ -1846,11 +1850,13 @@ static void add_header(struct buffer *b,
  134. buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
  135. buf_printf(b, "};\n");
  136. +#ifndef CONFIG_MODULE_STRIPPED
  137. if (!external_module)
  138. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  139. if (strstarts(mod->name, "drivers/staging"))
  140. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  141. +#endif
  142. if (strstarts(mod->name, "tools/testing"))
  143. buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n");
  144. @@ -2004,11 +2010,13 @@ static void add_depends(struct buffer *b
  145. static void add_srcversion(struct buffer *b, struct module *mod)
  146. {
  147. +#ifndef CONFIG_MODULE_STRIPPED
  148. if (mod->srcversion[0]) {
  149. buf_printf(b, "\n");
  150. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  151. mod->srcversion);
  152. }
  153. +#endif
  154. }
  155. static void write_buf(struct buffer *b, const char *fname)
  156. @@ -2109,12 +2117,14 @@ static void write_mod_c_file(struct modu
  157. add_extended_versions(&buf, mod);
  158. add_depends(&buf, mod);
  159. +#ifndef CONFIG_MODULE_STRIPPED
  160. buf_printf(&buf, "\n");
  161. list_for_each_entry_safe(alias, next, &mod->aliases, node) {
  162. buf_printf(&buf, "MODULE_ALIAS(\"%s\");\n", alias->str);
  163. list_del(&alias->node);
  164. free(alias);
  165. }
  166. +#endif
  167. add_srcversion(&buf, mod);