204-module_strip.patch 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. From: Felix Fietkau <[email protected]>
  2. Subject: [PATCH] build: add a hack for removing non-essential module info
  3. Signed-off-by: Felix Fietkau <[email protected]>
  4. ---
  5. --- a/include/linux/module.h
  6. +++ b/include/linux/module.h
  7. @@ -159,6 +159,7 @@ extern void cleanup_module(void);
  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. @@ -202,12 +203,12 @@ extern void cleanup_module(void);
  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. @@ -234,7 +235,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. @@ -256,7 +259,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. struct notifier_block;
  44. --- a/include/linux/moduleparam.h
  45. +++ b/include/linux/moduleparam.h
  46. @@ -16,6 +16,16 @@
  47. /* Chosen so that structs with an unsigned long line up. */
  48. #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
  49. +/* This struct is here for syntactic coherency, it is not used */
  50. +#define __MODULE_INFO_DISABLED(name) \
  51. + struct __UNIQUE_ID(name) {}
  52. +
  53. +#ifdef CONFIG_MODULE_STRIPPED
  54. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name)
  55. +#else
  56. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info)
  57. +#endif
  58. +
  59. #ifdef MODULE
  60. #define __MODULE_INFO(tag, name, info) \
  61. static const char __UNIQUE_ID(name)[] \
  62. @@ -23,8 +33,7 @@ static const char __UNIQUE_ID(name)[]
  63. = __stringify(tag) "=" info
  64. #else /* !MODULE */
  65. /* This struct is here for syntactic coherency, it is not used */
  66. -#define __MODULE_INFO(tag, name, info) \
  67. - struct __UNIQUE_ID(name) {}
  68. +#define __MODULE_INFO(tag, name, info) __MODULE_INFO_DISABLED(name)
  69. #endif
  70. #define __MODULE_PARM_TYPE(name, _type) \
  71. __MODULE_INFO(parmtype, name##type, #name ":" _type)
  72. @@ -32,7 +41,7 @@ static const char __UNIQUE_ID(name)[]
  73. /* One for each parameter, describing how to use it. Some files do
  74. multiple of these per line, so can't just use MODULE_INFO. */
  75. #define MODULE_PARM_DESC(_parm, desc) \
  76. - __MODULE_INFO(parm, _parm, #_parm ":" desc)
  77. + __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc)
  78. struct kernel_param;
  79. --- a/init/Kconfig
  80. +++ b/init/Kconfig
  81. @@ -2106,6 +2106,13 @@ config TRIM_UNUSED_KSYMS
  82. If unsure, or if you need to build out-of-tree modules, say N.
  83. +config MODULE_STRIPPED
  84. + bool "Reduce module size"
  85. + depends on MODULES
  86. + help
  87. + Remove module parameter descriptions, author info, version, aliases,
  88. + device tables, etc.
  89. +
  90. endif # MODULES
  91. config MODULES_TREE_LOOKUP
  92. --- a/kernel/module.c
  93. +++ b/kernel/module.c
  94. @@ -2945,9 +2945,11 @@ static struct module *setup_load_info(st
  95. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  96. {
  97. - const char *modmagic = get_modinfo(info, "vermagic");
  98. int err;
  99. +#ifndef CONFIG_MODULE_STRIPPED
  100. + const char *modmagic = get_modinfo(info, "vermagic");
  101. +
  102. if (flags & MODULE_INIT_IGNORE_VERMAGIC)
  103. modmagic = NULL;
  104. @@ -2968,6 +2970,7 @@ static int check_modinfo(struct module *
  105. mod->name);
  106. add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
  107. }
  108. +#endif
  109. if (get_modinfo(info, "staging")) {
  110. add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
  111. --- a/scripts/mod/modpost.c
  112. +++ b/scripts/mod/modpost.c
  113. @@ -1964,7 +1964,9 @@ static void read_symbols(char *modname)
  114. symname = remove_dot(info.strtab + sym->st_name);
  115. handle_modversions(mod, &info, sym, symname);
  116. +#ifndef CONFIG_MODULE_STRIPPED
  117. handle_moddevtable(mod, &info, sym, symname);
  118. +#endif
  119. }
  120. if (!is_vmlinux(modname) ||
  121. (is_vmlinux(modname) && vmlinux_section_warnings))
  122. @@ -2108,7 +2110,9 @@ static void add_header(struct buffer *b,
  123. buf_printf(b, "#include <linux/vermagic.h>\n");
  124. buf_printf(b, "#include <linux/compiler.h>\n");
  125. buf_printf(b, "\n");
  126. +#ifndef CONFIG_MODULE_STRIPPED
  127. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  128. +#endif
  129. buf_printf(b, "\n");
  130. buf_printf(b, "__visible struct module __this_module\n");
  131. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  132. @@ -2125,16 +2129,20 @@ static void add_header(struct buffer *b,
  133. static void add_intree_flag(struct buffer *b, int is_intree)
  134. {
  135. +#ifndef CONFIG_MODULE_STRIPPED
  136. if (is_intree)
  137. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  138. +#endif
  139. }
  140. static void add_staging_flag(struct buffer *b, const char *name)
  141. {
  142. +#ifndef CONFIG_MODULE_STRIPPED
  143. static const char *staging_dir = "drivers/staging";
  144. if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
  145. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  146. +#endif
  147. }
  148. /* In kernel, this size is defined in linux/module.h;
  149. @@ -2238,11 +2246,13 @@ static void add_depends(struct buffer *b
  150. static void add_srcversion(struct buffer *b, struct module *mod)
  151. {
  152. +#ifndef CONFIG_MODULE_STRIPPED
  153. if (mod->srcversion[0]) {
  154. buf_printf(b, "\n");
  155. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  156. mod->srcversion);
  157. }
  158. +#endif
  159. }
  160. static void write_if_changed(struct buffer *b, const char *fname)
  161. @@ -2476,7 +2486,9 @@ int main(int argc, char **argv)
  162. add_staging_flag(&buf, mod->name);
  163. err |= add_versions(&buf, mod);
  164. add_depends(&buf, mod, modules);
  165. +#ifndef CONFIG_MODULE_STRIPPED
  166. add_moddevtable(&buf, mod);
  167. +#endif
  168. add_srcversion(&buf, mod);
  169. sprintf(fname, "%s.mod.c", mod->name);