204-module_strip.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Subject: [PATCH] build: add a hack for removing non-essential module info
  3. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  4. ---
  5. --- a/include/linux/module.h
  6. +++ b/include/linux/module.h
  7. @@ -84,9 +84,10 @@ void trim_init_extable(struct module *m)
  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. +#define MODULE_ALIAS(_alias) MODULE_INFO_STRIP(alias, _alias)
  14. /* Soft module dependencies. See man modprobe.d for details.
  15. * Example: MODULE_SOFTDEP("pre: module-foo module-bar post: module-baz")
  16. @@ -127,12 +128,12 @@ void trim_init_extable(struct module *m)
  17. * Author(s), use "Name <email>" or just "Name", for multiple
  18. * authors use multiple MODULE_AUTHOR() statements/lines.
  19. */
  20. -#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  21. +#define MODULE_AUTHOR(_author) MODULE_INFO_STRIP(author, _author)
  22. /* What your module does. */
  23. -#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  24. +#define MODULE_DESCRIPTION(_description) MODULE_INFO_STRIP(description, _description)
  25. -#ifdef MODULE
  26. +#if defined(MODULE) && !defined(CONFIG_MODULE_STRIPPED)
  27. /* Creates an alias so file2alias.c can find device table. */
  28. #define MODULE_DEVICE_TABLE(type, name) \
  29. extern const typeof(name) __mod_##type##__##name##_device_table \
  30. @@ -159,7 +160,9 @@ extern const typeof(name) __mod_##type##
  31. */
  32. #if defined(MODULE) || !defined(CONFIG_SYSFS)
  33. -#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
  34. +#define MODULE_VERSION(_version) MODULE_INFO_STRIP(version, _version)
  35. +#elif defined(CONFIG_MODULE_STRIPPED)
  36. +#define MODULE_VERSION(_version) __MODULE_INFO_DISABLED(version)
  37. #else
  38. #define MODULE_VERSION(_version) \
  39. static struct module_version_attribute ___modver_attr = { \
  40. @@ -181,7 +184,7 @@ extern const typeof(name) __mod_##type##
  41. /* Optional firmware file (or files) needed by the module
  42. * format is simply firmware file name. Multiple firmware
  43. * files require multiple MODULE_FIRMWARE() specifiers */
  44. -#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
  45. +#define MODULE_FIRMWARE(_firmware) MODULE_INFO_STRIP(firmware, _firmware)
  46. /* Given an address, look for it in the exception tables */
  47. const struct exception_table_entry *search_exception_tables(unsigned long add);
  48. --- a/include/linux/moduleparam.h
  49. +++ b/include/linux/moduleparam.h
  50. @@ -16,6 +16,16 @@
  51. /* Chosen so that structs with an unsigned long line up. */
  52. #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
  53. +/* This struct is here for syntactic coherency, it is not used */
  54. +#define __MODULE_INFO_DISABLED(name) \
  55. + struct __UNIQUE_ID(name) {}
  56. +
  57. +#ifdef CONFIG_MODULE_STRIPPED
  58. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name)
  59. +#else
  60. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info)
  61. +#endif
  62. +
  63. #ifdef MODULE
  64. #define __MODULE_INFO(tag, name, info) \
  65. static const char __UNIQUE_ID(name)[] \
  66. @@ -23,8 +33,7 @@ static const char __UNIQUE_ID(name)[]
  67. = __stringify(tag) "=" info
  68. #else /* !MODULE */
  69. /* This struct is here for syntactic coherency, it is not used */
  70. -#define __MODULE_INFO(tag, name, info) \
  71. - struct __UNIQUE_ID(name) {}
  72. +#define __MODULE_INFO(tag, name, info) __MODULE_INFO_DISABLED(name)
  73. #endif
  74. #define __MODULE_PARM_TYPE(name, _type) \
  75. __MODULE_INFO(parmtype, name##type, #name ":" _type)
  76. @@ -32,7 +41,7 @@ static const char __UNIQUE_ID(name)[]
  77. /* One for each parameter, describing how to use it. Some files do
  78. multiple of these per line, so can't just use MODULE_INFO. */
  79. #define MODULE_PARM_DESC(_parm, desc) \
  80. - __MODULE_INFO(parm, _parm, #_parm ":" desc)
  81. + __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc)
  82. struct kernel_param;
  83. --- a/init/Kconfig
  84. +++ b/init/Kconfig
  85. @@ -1998,6 +1998,13 @@ config MODULE_COMPRESS_XZ
  86. endchoice
  87. +config MODULE_STRIPPED
  88. + bool "Reduce module size"
  89. + depends on MODULES
  90. + help
  91. + Remove module parameter descriptions, author info, version, aliases,
  92. + device tables, etc.
  93. +
  94. endif # MODULES
  95. config INIT_ALL_POSSIBLE
  96. --- a/kernel/module.c
  97. +++ b/kernel/module.c
  98. @@ -2709,6 +2709,7 @@ static struct module *setup_load_info(st
  99. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  100. {
  101. +#ifndef CONFIG_MODULE_STRIPPED
  102. const char *modmagic = get_modinfo(info, "vermagic");
  103. int err;
  104. @@ -2734,6 +2735,7 @@ static int check_modinfo(struct module *
  105. pr_warn("%s: module is from the staging directory, the quality "
  106. "is unknown, you have been warned.\n", mod->name);
  107. }
  108. +#endif
  109. /* Set up license info based on the info section */
  110. set_license(mod, get_modinfo(info, "license"));
  111. --- a/scripts/mod/modpost.c
  112. +++ b/scripts/mod/modpost.c
  113. @@ -1959,7 +1959,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. @@ -2103,7 +2105,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. @@ -2120,16 +2124,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. /**
  149. @@ -2222,11 +2230,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. @@ -2457,7 +2467,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);