204-module_strip.patch 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. @@ -82,7 +82,7 @@ void sort_extable(struct exception_table
  8. void sort_main_extable(void);
  9. void trim_init_extable(struct module *m);
  10. -#ifdef MODULE
  11. +#if defined(MODULE) && !defined(CONFIG_MODULE_STRIPPED)
  12. #define MODULE_GENERIC_TABLE(gtype, name) \
  13. extern const struct gtype##_id __mod_##gtype##_table \
  14. __attribute__ ((unused, alias(__stringify(name))))
  15. @@ -93,9 +93,10 @@ extern const struct gtype##_id __mod_##g
  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. +#define MODULE_ALIAS(_alias) MODULE_INFO_STRIP(alias, _alias)
  22. /* Soft module dependencies. See man modprobe.d for details.
  23. * Example: MODULE_SOFTDEP("pre: module-foo module-bar post: module-baz")
  24. @@ -136,10 +137,10 @@ extern const struct gtype##_id __mod_##g
  25. * Author(s), use "Name <email>" or just "Name", for multiple
  26. * authors use multiple MODULE_AUTHOR() statements/lines.
  27. */
  28. -#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  29. +#define MODULE_AUTHOR(_author) MODULE_INFO_STRIP(author, _author)
  30. /* What your module does. */
  31. -#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  32. +#define MODULE_DESCRIPTION(_description) MODULE_INFO_STRIP(description, _description)
  33. #define MODULE_DEVICE_TABLE(type, name) \
  34. MODULE_GENERIC_TABLE(type##_device, name)
  35. @@ -162,7 +163,9 @@ extern const struct gtype##_id __mod_##g
  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. @@ -184,7 +187,7 @@ extern const struct gtype##_id __mod_##g
  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. /* Given an address, look for it in the exception tables */
  52. const struct exception_table_entry *search_exception_tables(unsigned long add);
  53. --- a/include/linux/moduleparam.h
  54. +++ b/include/linux/moduleparam.h
  55. @@ -16,6 +16,16 @@
  56. /* Chosen so that structs with an unsigned long line up. */
  57. #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
  58. +/* This struct is here for syntactic coherency, it is not used */
  59. +#define __MODULE_INFO_DISABLED(name) \
  60. + struct __UNIQUE_ID(name) {}
  61. +
  62. +#ifdef CONFIG_MODULE_STRIPPED
  63. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name)
  64. +#else
  65. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info)
  66. +#endif
  67. +
  68. #ifdef MODULE
  69. #define __MODULE_INFO(tag, name, info) \
  70. static const char __UNIQUE_ID(name)[] \
  71. @@ -23,8 +33,7 @@ static const char __UNIQUE_ID(name)[]
  72. = __stringify(tag) "=" info
  73. #else /* !MODULE */
  74. /* This struct is here for syntactic coherency, it is not used */
  75. -#define __MODULE_INFO(tag, name, info) \
  76. - struct __UNIQUE_ID(name) {}
  77. +#define __MODULE_INFO(tag, name, info) __MODULE_INFO_DISABLED(name)
  78. #endif
  79. #define __MODULE_PARM_TYPE(name, _type) \
  80. __MODULE_INFO(parmtype, name##type, #name ":" _type)
  81. @@ -32,7 +41,7 @@ static const char __UNIQUE_ID(name)[]
  82. /* One for each parameter, describing how to use it. Some files do
  83. multiple of these per line, so can't just use MODULE_INFO. */
  84. #define MODULE_PARM_DESC(_parm, desc) \
  85. - __MODULE_INFO(parm, _parm, #_parm ":" desc)
  86. + __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc)
  87. struct kernel_param;
  88. --- a/init/Kconfig
  89. +++ b/init/Kconfig
  90. @@ -1831,6 +1831,13 @@ config MODULE_SIG_HASH
  91. default "sha384" if MODULE_SIG_SHA384
  92. default "sha512" if MODULE_SIG_SHA512
  93. +config MODULE_STRIPPED
  94. + bool "Reduce module size"
  95. + depends on MODULES
  96. + help
  97. + Remove module parameter descriptions, author info, version, aliases,
  98. + device tables, etc.
  99. +
  100. endif # MODULES
  101. config INIT_ALL_POSSIBLE
  102. --- a/kernel/module.c
  103. +++ b/kernel/module.c
  104. @@ -2663,6 +2663,7 @@ static struct module *setup_load_info(st
  105. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  106. {
  107. +#ifndef CONFIG_MODULE_STRIPPED
  108. const char *modmagic = get_modinfo(info, "vermagic");
  109. int err;
  110. @@ -2688,6 +2689,7 @@ static int check_modinfo(struct module *
  111. pr_warn("%s: module is from the staging directory, the quality "
  112. "is unknown, you have been warned.\n", mod->name);
  113. }
  114. +#endif
  115. /* Set up license info based on the info section */
  116. set_license(mod, get_modinfo(info, "license"));
  117. --- a/scripts/mod/modpost.c
  118. +++ b/scripts/mod/modpost.c
  119. @@ -1734,7 +1734,9 @@ static void read_symbols(char *modname)
  120. symname = info.strtab + sym->st_name;
  121. handle_modversions(mod, &info, sym, symname);
  122. +#ifndef CONFIG_MODULE_STRIPPED
  123. handle_moddevtable(mod, &info, sym, symname);
  124. +#endif
  125. }
  126. if (!is_vmlinux(modname) ||
  127. (is_vmlinux(modname) && vmlinux_section_warnings))
  128. @@ -1878,7 +1880,9 @@ static void add_header(struct buffer *b,
  129. buf_printf(b, "#include <linux/vermagic.h>\n");
  130. buf_printf(b, "#include <linux/compiler.h>\n");
  131. buf_printf(b, "\n");
  132. +#ifndef CONFIG_MODULE_STRIPPED
  133. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  134. +#endif
  135. buf_printf(b, "\n");
  136. buf_printf(b, "__visible struct module __this_module\n");
  137. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  138. @@ -1895,16 +1899,20 @@ static void add_header(struct buffer *b,
  139. static void add_intree_flag(struct buffer *b, int is_intree)
  140. {
  141. +#ifndef CONFIG_MODULE_STRIPPED
  142. if (is_intree)
  143. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  144. +#endif
  145. }
  146. static void add_staging_flag(struct buffer *b, const char *name)
  147. {
  148. +#ifndef CONFIG_MODULE_STRIPPED
  149. static const char *staging_dir = "drivers/staging";
  150. if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
  151. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  152. +#endif
  153. }
  154. /**
  155. @@ -1997,11 +2005,13 @@ static void add_depends(struct buffer *b
  156. static void add_srcversion(struct buffer *b, struct module *mod)
  157. {
  158. +#ifndef CONFIG_MODULE_STRIPPED
  159. if (mod->srcversion[0]) {
  160. buf_printf(b, "\n");
  161. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  162. mod->srcversion);
  163. }
  164. +#endif
  165. }
  166. static void write_if_changed(struct buffer *b, const char *fname)
  167. @@ -2230,7 +2240,9 @@ int main(int argc, char **argv)
  168. add_staging_flag(&buf, mod->name);
  169. err |= add_versions(&buf, mod);
  170. add_depends(&buf, mod, modules);
  171. +#ifndef CONFIG_MODULE_STRIPPED
  172. add_moddevtable(&buf, mod);
  173. +#endif
  174. add_srcversion(&buf, mod);
  175. sprintf(fname, "%s.mod.c", mod->name);