2
0

204-module_strip.patch 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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,6 +84,7 @@ 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. @@ -127,12 +128,12 @@ void trim_init_extable(struct module *m)
  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 struct type##_device_id __mod_##type##__##name##_device_table \
  27. @@ -159,7 +160,9 @@ void trim_init_extable(struct module *m)
  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. @@ -181,7 +184,7 @@ void trim_init_extable(struct module *m)
  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. /* Given an address, look for it in the exception tables */
  44. const struct exception_table_entry *search_exception_tables(unsigned long add);
  45. --- a/include/linux/moduleparam.h
  46. +++ b/include/linux/moduleparam.h
  47. @@ -16,6 +16,16 @@
  48. /* Chosen so that structs with an unsigned long line up. */
  49. #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
  50. +/* This struct is here for syntactic coherency, it is not used */
  51. +#define __MODULE_INFO_DISABLED(name) \
  52. + struct __UNIQUE_ID(name) {}
  53. +
  54. +#ifdef CONFIG_MODULE_STRIPPED
  55. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name)
  56. +#else
  57. +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info)
  58. +#endif
  59. +
  60. #ifdef MODULE
  61. #define __MODULE_INFO(tag, name, info) \
  62. static const char __UNIQUE_ID(name)[] \
  63. @@ -23,8 +33,7 @@ static const char __UNIQUE_ID(name)[]
  64. = __stringify(tag) "=" info
  65. #else /* !MODULE */
  66. /* This struct is here for syntactic coherency, it is not used */
  67. -#define __MODULE_INFO(tag, name, info) \
  68. - struct __UNIQUE_ID(name) {}
  69. +#define __MODULE_INFO(tag, name, info) __MODULE_INFO_DISABLED(name)
  70. #endif
  71. #define __MODULE_PARM_TYPE(name, _type) \
  72. __MODULE_INFO(parmtype, name##type, #name ":" _type)
  73. @@ -32,7 +41,7 @@ static const char __UNIQUE_ID(name)[]
  74. /* One for each parameter, describing how to use it. Some files do
  75. multiple of these per line, so can't just use MODULE_INFO. */
  76. #define MODULE_PARM_DESC(_parm, desc) \
  77. - __MODULE_INFO(parm, _parm, #_parm ":" desc)
  78. + __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc)
  79. struct kernel_param;
  80. --- a/init/Kconfig
  81. +++ b/init/Kconfig
  82. @@ -1987,6 +1987,13 @@ config MODULE_COMPRESS_XZ
  83. endchoice
  84. +config MODULE_STRIPPED
  85. + bool "Reduce module size"
  86. + depends on MODULES
  87. + help
  88. + Remove module parameter descriptions, author info, version, aliases,
  89. + device tables, etc.
  90. +
  91. endif # MODULES
  92. config INIT_ALL_POSSIBLE
  93. --- a/kernel/module.c
  94. +++ b/kernel/module.c
  95. @@ -2699,6 +2699,7 @@ static struct module *setup_load_info(st
  96. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  97. {
  98. +#ifndef CONFIG_MODULE_STRIPPED
  99. const char *modmagic = get_modinfo(info, "vermagic");
  100. int err;
  101. @@ -2724,6 +2725,7 @@ static int check_modinfo(struct module *
  102. pr_warn("%s: module is from the staging directory, the quality "
  103. "is unknown, you have been warned.\n", mod->name);
  104. }
  105. +#endif
  106. /* Set up license info based on the info section */
  107. set_license(mod, get_modinfo(info, "license"));
  108. --- a/scripts/mod/modpost.c
  109. +++ b/scripts/mod/modpost.c
  110. @@ -1726,7 +1726,9 @@ static void read_symbols(char *modname)
  111. symname = remove_dot(info.strtab + sym->st_name);
  112. handle_modversions(mod, &info, sym, symname);
  113. +#ifndef CONFIG_MODULE_STRIPPED
  114. handle_moddevtable(mod, &info, sym, symname);
  115. +#endif
  116. }
  117. if (!is_vmlinux(modname) ||
  118. (is_vmlinux(modname) && vmlinux_section_warnings))
  119. @@ -1870,7 +1872,9 @@ static void add_header(struct buffer *b,
  120. buf_printf(b, "#include <linux/vermagic.h>\n");
  121. buf_printf(b, "#include <linux/compiler.h>\n");
  122. buf_printf(b, "\n");
  123. +#ifndef CONFIG_MODULE_STRIPPED
  124. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  125. +#endif
  126. buf_printf(b, "\n");
  127. buf_printf(b, "__visible struct module __this_module\n");
  128. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  129. @@ -1887,16 +1891,20 @@ static void add_header(struct buffer *b,
  130. static void add_intree_flag(struct buffer *b, int is_intree)
  131. {
  132. +#ifndef CONFIG_MODULE_STRIPPED
  133. if (is_intree)
  134. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  135. +#endif
  136. }
  137. static void add_staging_flag(struct buffer *b, const char *name)
  138. {
  139. +#ifndef CONFIG_MODULE_STRIPPED
  140. static const char *staging_dir = "drivers/staging";
  141. if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
  142. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  143. +#endif
  144. }
  145. /**
  146. @@ -1989,11 +1997,13 @@ static void add_depends(struct buffer *b
  147. static void add_srcversion(struct buffer *b, struct module *mod)
  148. {
  149. +#ifndef CONFIG_MODULE_STRIPPED
  150. if (mod->srcversion[0]) {
  151. buf_printf(b, "\n");
  152. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  153. mod->srcversion);
  154. }
  155. +#endif
  156. }
  157. static void write_if_changed(struct buffer *b, const char *fname)
  158. @@ -2224,7 +2234,9 @@ int main(int argc, char **argv)
  159. add_staging_flag(&buf, mod->name);
  160. err |= add_versions(&buf, mod);
  161. add_depends(&buf, mod, modules);
  162. +#ifndef CONFIG_MODULE_STRIPPED
  163. add_moddevtable(&buf, mod);
  164. +#endif
  165. add_srcversion(&buf, mod);
  166. sprintf(fname, "%s.mod.c", mod->name);