204-module_strip.patch 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. @@ -159,6 +159,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. @@ -202,12 +203,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 const typeof(name) __mod_##type##__##name##_device_table \
  35. @@ -234,7 +235,9 @@ extern const typeof(name) __mod_##type##
  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. static struct module_version_attribute ___modver_attr = { \
  45. @@ -256,7 +259,7 @@ extern const typeof(name) __mod_##type##
  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. struct notifier_block;
  52. --- a/include/linux/moduleparam.h
  53. +++ b/include/linux/moduleparam.h
  54. @@ -16,6 +16,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. #ifdef MODULE
  68. #define __MODULE_INFO(tag, name, info) \
  69. static const char __UNIQUE_ID(name)[] \
  70. @@ -23,8 +33,7 @@ static const char __UNIQUE_ID(name)[]
  71. = __stringify(tag) "=" info
  72. #else /* !MODULE */
  73. /* This struct is here for syntactic coherency, it is not used */
  74. -#define __MODULE_INFO(tag, name, info) \
  75. - struct __UNIQUE_ID(name) {}
  76. +#define __MODULE_INFO(tag, name, info) __MODULE_INFO_DISABLED(name)
  77. #endif
  78. #define __MODULE_PARM_TYPE(name, _type) \
  79. __MODULE_INFO(parmtype, name##type, #name ":" _type)
  80. @@ -32,7 +41,7 @@ static const char __UNIQUE_ID(name)[]
  81. /* One for each parameter, describing how to use it. Some files do
  82. multiple of these per line, so can't just use MODULE_INFO. */
  83. #define MODULE_PARM_DESC(_parm, desc) \
  84. - __MODULE_INFO(parm, _parm, #_parm ":" desc)
  85. + __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc)
  86. struct kernel_param;
  87. --- a/init/Kconfig
  88. +++ b/init/Kconfig
  89. @@ -2122,6 +2122,13 @@ config TRIM_UNUSED_KSYMS
  90. If unsure, or if you need to build out-of-tree modules, say N.
  91. +config MODULE_STRIPPED
  92. + bool "Reduce module size"
  93. + depends on MODULES
  94. + help
  95. + Remove module parameter descriptions, author info, version, aliases,
  96. + device tables, etc.
  97. +
  98. endif # MODULES
  99. config MODULES_TREE_LOOKUP
  100. --- a/kernel/module.c
  101. +++ b/kernel/module.c
  102. @@ -2968,9 +2968,11 @@ static struct module *setup_load_info(st
  103. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  104. {
  105. - const char *modmagic = get_modinfo(info, "vermagic");
  106. int err;
  107. +#ifndef CONFIG_MODULE_STRIPPED
  108. + const char *modmagic = get_modinfo(info, "vermagic");
  109. +
  110. if (flags & MODULE_INIT_IGNORE_VERMAGIC)
  111. modmagic = NULL;
  112. @@ -2991,6 +2993,7 @@ static int check_modinfo(struct module *
  113. mod->name);
  114. add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
  115. }
  116. +#endif
  117. check_modinfo_retpoline(mod, info);
  118. --- a/scripts/mod/modpost.c
  119. +++ b/scripts/mod/modpost.c
  120. @@ -1967,7 +1967,9 @@ static void read_symbols(char *modname)
  121. symname = remove_dot(info.strtab + sym->st_name);
  122. handle_modversions(mod, &info, sym, symname);
  123. +#ifndef CONFIG_MODULE_STRIPPED
  124. handle_moddevtable(mod, &info, sym, symname);
  125. +#endif
  126. }
  127. if (!is_vmlinux(modname) ||
  128. (is_vmlinux(modname) && vmlinux_section_warnings))
  129. @@ -2111,7 +2113,9 @@ static void add_header(struct buffer *b,
  130. buf_printf(b, "#include <linux/vermagic.h>\n");
  131. buf_printf(b, "#include <linux/compiler.h>\n");
  132. buf_printf(b, "\n");
  133. +#ifndef CONFIG_MODULE_STRIPPED
  134. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  135. +#endif
  136. buf_printf(b, "\n");
  137. buf_printf(b, "__visible struct module __this_module\n");
  138. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  139. @@ -2128,24 +2132,30 @@ static void add_header(struct buffer *b,
  140. static void add_intree_flag(struct buffer *b, int is_intree)
  141. {
  142. +#ifndef CONFIG_MODULE_STRIPPED
  143. if (is_intree)
  144. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  145. +#endif
  146. }
  147. /* Cannot check for assembler */
  148. static void add_retpoline(struct buffer *b)
  149. {
  150. +#ifndef CONFIG_MODULE_STRIPPED
  151. buf_printf(b, "\n#ifdef RETPOLINE\n");
  152. buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
  153. buf_printf(b, "#endif\n");
  154. +#endif
  155. }
  156. static void add_staging_flag(struct buffer *b, const char *name)
  157. {
  158. +#ifndef CONFIG_MODULE_STRIPPED
  159. static const char *staging_dir = "drivers/staging";
  160. if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
  161. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  162. +#endif
  163. }
  164. /* In kernel, this size is defined in linux/module.h;
  165. @@ -2249,11 +2259,13 @@ static void add_depends(struct buffer *b
  166. static void add_srcversion(struct buffer *b, struct module *mod)
  167. {
  168. +#ifndef CONFIG_MODULE_STRIPPED
  169. if (mod->srcversion[0]) {
  170. buf_printf(b, "\n");
  171. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  172. mod->srcversion);
  173. }
  174. +#endif
  175. }
  176. static void write_if_changed(struct buffer *b, const char *fname)
  177. @@ -2488,7 +2500,9 @@ int main(int argc, char **argv)
  178. add_staging_flag(&buf, mod->name);
  179. err |= add_versions(&buf, mod);
  180. add_depends(&buf, mod, modules);
  181. +#ifndef CONFIG_MODULE_STRIPPED
  182. add_moddevtable(&buf, mod);
  183. +#endif
  184. add_srcversion(&buf, mod);
  185. sprintf(fname, "%s.mod.c", mod->name);