204-module_strip.patch 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. @@ -163,6 +163,7 @@ extern void cleanup_module(void);
  16. /* Generic info of form tag = "info" */
  17. #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
  18. +#define MODULE_INFO_STRIP(tag, info) __MODULE_INFO_STRIP(tag, tag, info)
  19. /* For userspace: you can also call me... */
  20. #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
  21. @@ -232,12 +233,12 @@ extern void cleanup_module(void);
  22. * Author(s), use "Name <email>" or just "Name", for multiple
  23. * authors use multiple MODULE_AUTHOR() statements/lines.
  24. */
  25. -#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  26. +#define MODULE_AUTHOR(_author) MODULE_INFO_STRIP(author, _author)
  27. /* What your module does. */
  28. -#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  29. +#define MODULE_DESCRIPTION(_description) MODULE_INFO_STRIP(description, _description)
  30. -#ifdef MODULE
  31. +#if defined(MODULE) && !defined(CONFIG_MODULE_STRIPPED)
  32. /* Creates an alias so file2alias.c can find device table. */
  33. #define MODULE_DEVICE_TABLE(type, name) \
  34. extern typeof(name) __mod_##type##__##name##_device_table \
  35. @@ -264,7 +265,9 @@ extern typeof(name) __mod_##type##__##na
  36. */
  37. #if defined(MODULE) || !defined(CONFIG_SYSFS)
  38. -#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
  39. +#define MODULE_VERSION(_version) MODULE_INFO_STRIP(version, _version)
  40. +#elif defined(CONFIG_MODULE_STRIPPED)
  41. +#define MODULE_VERSION(_version) __MODULE_INFO_DISABLED(version)
  42. #else
  43. #define MODULE_VERSION(_version) \
  44. MODULE_INFO(version, _version); \
  45. @@ -287,7 +290,7 @@ extern typeof(name) __mod_##type##__##na
  46. /* Optional firmware file (or files) needed by the module
  47. * format is simply firmware file name. Multiple firmware
  48. * files require multiple MODULE_FIRMWARE() specifiers */
  49. -#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
  50. +#define MODULE_FIRMWARE(_firmware) MODULE_INFO_STRIP(firmware, _firmware)
  51. #define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, __stringify(ns))
  52. --- a/include/linux/moduleparam.h
  53. +++ b/include/linux/moduleparam.h
  54. @@ -20,6 +20,16 @@
  55. /* Chosen so that structs with an unsigned long line up. */
  56. #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
  57. +/* This struct is here for syntactic coherency, it is not used */
  58. +#define __MODULE_INFO_DISABLED(name) \
  59. + struct __UNIQUE_ID(name) {}
  60. +
  61. +#ifdef CONFIG_MODULE_STRIPPED
  62. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name)
  63. +#else
  64. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info)
  65. +#endif
  66. +
  67. #define __MODULE_INFO(tag, name, info) \
  68. static const char __UNIQUE_ID(name)[] \
  69. __used __section(".modinfo") __aligned(1) \
  70. @@ -31,7 +41,7 @@
  71. /* One for each parameter, describing how to use it. Some files do
  72. multiple of these per line, so can't just use MODULE_INFO. */
  73. #define MODULE_PARM_DESC(_parm, desc) \
  74. - __MODULE_INFO(parm, _parm, #_parm ":" desc)
  75. + __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc)
  76. struct kernel_param;
  77. --- a/kernel/module/Kconfig
  78. +++ b/kernel/module/Kconfig
  79. @@ -290,4 +290,11 @@ config MODULES_TREE_LOOKUP
  80. def_bool y
  81. depends on PERF_EVENTS || TRACING || CFI_CLANG
  82. +config MODULE_STRIPPED
  83. + bool "Reduce module size"
  84. + depends on MODULES
  85. + help
  86. + Remove module parameter descriptions, author info, version, aliases,
  87. + device tables, etc.
  88. +
  89. endif # MODULES
  90. --- a/kernel/module/main.c
  91. +++ b/kernel/module/main.c
  92. @@ -988,6 +988,7 @@ size_t modinfo_attrs_count = ARRAY_SIZE(
  93. static const char vermagic[] = VERMAGIC_STRING;
  94. +#if defined(CONFIG_MODVERSIONS) || !defined(CONFIG_MODULE_STRIPPED)
  95. int try_to_force_load(struct module *mod, const char *reason)
  96. {
  97. #ifdef CONFIG_MODULE_FORCE_LOAD
  98. @@ -999,6 +1000,7 @@ int try_to_force_load(struct module *mod
  99. return -ENOEXEC;
  100. #endif
  101. }
  102. +#endif
  103. static char *get_modinfo(const struct load_info *info, const char *tag);
  104. static char *get_next_modinfo(const struct load_info *info, const char *tag,
  105. @@ -1950,9 +1952,11 @@ static int setup_load_info(struct load_i
  106. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  107. {
  108. - const char *modmagic = get_modinfo(info, "vermagic");
  109. int err;
  110. +#ifndef CONFIG_MODULE_STRIPPED
  111. + const char *modmagic = get_modinfo(info, "vermagic");
  112. +
  113. if (flags & MODULE_INIT_IGNORE_VERMAGIC)
  114. modmagic = NULL;
  115. @@ -1973,6 +1977,7 @@ static int check_modinfo(struct module *
  116. mod->name);
  117. add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
  118. }
  119. +#endif
  120. check_modinfo_retpoline(mod, info);
  121. --- a/scripts/mod/modpost.c
  122. +++ b/scripts/mod/modpost.c
  123. @@ -1817,7 +1817,9 @@ static void read_symbols(const char *mod
  124. symname = remove_dot(info.strtab + sym->st_name);
  125. handle_symbol(mod, &info, sym, symname);
  126. +#ifndef CONFIG_MODULE_STRIPPED
  127. handle_moddevtable(mod, &info, sym, symname);
  128. +#endif
  129. }
  130. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  131. @@ -1980,8 +1982,10 @@ static void add_header(struct buffer *b,
  132. buf_printf(b, "BUILD_SALT;\n");
  133. buf_printf(b, "BUILD_LTO_INFO;\n");
  134. buf_printf(b, "\n");
  135. +#ifndef CONFIG_MODULE_STRIPPED
  136. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  137. buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
  138. +#endif
  139. buf_printf(b, "\n");
  140. buf_printf(b, "__visible struct module __this_module\n");
  141. buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
  142. @@ -1995,8 +1999,10 @@ static void add_header(struct buffer *b,
  143. buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
  144. buf_printf(b, "};\n");
  145. +#ifndef CONFIG_MODULE_STRIPPED
  146. if (!external_module)
  147. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  148. +#endif
  149. buf_printf(b,
  150. "\n"
  151. @@ -2004,8 +2010,10 @@ static void add_header(struct buffer *b,
  152. "MODULE_INFO(retpoline, \"Y\");\n"
  153. "#endif\n");
  154. +#ifndef CONFIG_MODULE_STRIPPED
  155. if (strstarts(mod->name, "drivers/staging"))
  156. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  157. +#endif
  158. if (strstarts(mod->name, "tools/testing"))
  159. buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n");
  160. @@ -2101,11 +2109,13 @@ static void add_depends(struct buffer *b
  161. static void add_srcversion(struct buffer *b, struct module *mod)
  162. {
  163. +#ifndef CONFIG_MODULE_STRIPPED
  164. if (mod->srcversion[0]) {
  165. buf_printf(b, "\n");
  166. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  167. mod->srcversion);
  168. }
  169. +#endif
  170. }
  171. static void write_buf(struct buffer *b, const char *fname)
  172. @@ -2191,7 +2201,9 @@ static void write_mod_c_file(struct modu
  173. add_exported_symbols(&buf, mod);
  174. add_versions(&buf, mod);
  175. add_depends(&buf, mod);
  176. +#ifndef CONFIG_MODULE_STRIPPED
  177. add_moddevtable(&buf, mod);
  178. +#endif
  179. add_srcversion(&buf, mod);
  180. ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);