kernel: split patches folder up into backport, pending and hack folders
[openwrt/staging/chunkeey.git] / target / linux / generic / hack-4.9 / 204-module_strip.patch
1 From a779a482fb9b9f8fcdf8b2519c789b4b9bb5dd05 Mon Sep 17 00:00:00 2001
2 From: Felix Fietkau <nbd@nbd.name>
3 Date: Fri, 7 Jul 2017 16:56:48 +0200
4 Subject: build: add a hack for removing non-essential module info
5
6 Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 ---
8 include/linux/module.h | 13 ++++++++-----
9 include/linux/moduleparam.h | 15 ++++++++++++---
10 init/Kconfig | 7 +++++++
11 kernel/module.c | 5 ++++-
12 scripts/mod/modpost.c | 12 ++++++++++++
13 5 files changed, 43 insertions(+), 9 deletions(-)
14
15 diff --git a/include/linux/module.h b/include/linux/module.h
16 index 0c3207d26ac0..08b58474bfd2 100644
17 --- a/include/linux/module.h
18 +++ b/include/linux/module.h
19 @@ -159,6 +159,7 @@ extern void cleanup_module(void);
20
21 /* Generic info of form tag = "info" */
22 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
23 +#define MODULE_INFO_STRIP(tag, info) __MODULE_INFO_STRIP(tag, tag, info)
24
25 /* For userspace: you can also call me... */
26 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
27 @@ -202,12 +203,12 @@ extern void cleanup_module(void);
28 * Author(s), use "Name <email>" or just "Name", for multiple
29 * authors use multiple MODULE_AUTHOR() statements/lines.
30 */
31 -#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
32 +#define MODULE_AUTHOR(_author) MODULE_INFO_STRIP(author, _author)
33
34 /* What your module does. */
35 -#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
36 +#define MODULE_DESCRIPTION(_description) MODULE_INFO_STRIP(description, _description)
37
38 -#ifdef MODULE
39 +#if defined(MODULE) && !defined(CONFIG_MODULE_STRIPPED)
40 /* Creates an alias so file2alias.c can find device table. */
41 #define MODULE_DEVICE_TABLE(type, name) \
42 extern const typeof(name) __mod_##type##__##name##_device_table \
43 @@ -234,7 +235,9 @@ extern const typeof(name) __mod_##type##__##name##_device_table \
44 */
45
46 #if defined(MODULE) || !defined(CONFIG_SYSFS)
47 -#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
48 +#define MODULE_VERSION(_version) MODULE_INFO_STRIP(version, _version)
49 +#elif defined(CONFIG_MODULE_STRIPPED)
50 +#define MODULE_VERSION(_version) __MODULE_INFO_DISABLED(version)
51 #else
52 #define MODULE_VERSION(_version) \
53 static struct module_version_attribute ___modver_attr = { \
54 @@ -256,7 +259,7 @@ extern const typeof(name) __mod_##type##__##name##_device_table \
55 /* Optional firmware file (or files) needed by the module
56 * format is simply firmware file name. Multiple firmware
57 * files require multiple MODULE_FIRMWARE() specifiers */
58 -#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
59 +#define MODULE_FIRMWARE(_firmware) MODULE_INFO_STRIP(firmware, _firmware)
60
61 struct notifier_block;
62
63 diff --git a/include/linux/moduleparam.h b/include/linux/moduleparam.h
64 index 52666d90ca94..f0db3592906a 100644
65 --- a/include/linux/moduleparam.h
66 +++ b/include/linux/moduleparam.h
67 @@ -16,6 +16,16 @@
68 /* Chosen so that structs with an unsigned long line up. */
69 #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
70
71 +/* This struct is here for syntactic coherency, it is not used */
72 +#define __MODULE_INFO_DISABLED(name) \
73 + struct __UNIQUE_ID(name) {}
74 +
75 +#ifdef CONFIG_MODULE_STRIPPED
76 +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name)
77 +#else
78 +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info)
79 +#endif
80 +
81 #ifdef MODULE
82 #define __MODULE_INFO(tag, name, info) \
83 static const char __UNIQUE_ID(name)[] \
84 @@ -23,8 +33,7 @@ static const char __UNIQUE_ID(name)[] \
85 = __stringify(tag) "=" info
86 #else /* !MODULE */
87 /* This struct is here for syntactic coherency, it is not used */
88 -#define __MODULE_INFO(tag, name, info) \
89 - struct __UNIQUE_ID(name) {}
90 +#define __MODULE_INFO(tag, name, info) __MODULE_INFO_DISABLED(name)
91 #endif
92 #define __MODULE_PARM_TYPE(name, _type) \
93 __MODULE_INFO(parmtype, name##type, #name ":" _type)
94 @@ -32,7 +41,7 @@ static const char __UNIQUE_ID(name)[] \
95 /* One for each parameter, describing how to use it. Some files do
96 multiple of these per line, so can't just use MODULE_INFO. */
97 #define MODULE_PARM_DESC(_parm, desc) \
98 - __MODULE_INFO(parm, _parm, #_parm ":" desc)
99 + __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc)
100
101 struct kernel_param;
102
103 diff --git a/init/Kconfig b/init/Kconfig
104 index 34407f15e6d3..9f2c0cabbc97 100644
105 --- a/init/Kconfig
106 +++ b/init/Kconfig
107 @@ -2095,6 +2095,13 @@ config TRIM_UNUSED_KSYMS
108
109 If unsure, or if you need to build out-of-tree modules, say N.
110
111 +config MODULE_STRIPPED
112 + bool "Reduce module size"
113 + depends on MODULES
114 + help
115 + Remove module parameter descriptions, author info, version, aliases,
116 + device tables, etc.
117 +
118 endif # MODULES
119
120 config MODULES_TREE_LOOKUP
121 diff --git a/kernel/module.c b/kernel/module.c
122 index 0e54d5bf0097..80b5ac4181c6 100644
123 --- a/kernel/module.c
124 +++ b/kernel/module.c
125 @@ -2945,9 +2945,11 @@ static struct module *setup_load_info(struct load_info *info, int flags)
126
127 static int check_modinfo(struct module *mod, struct load_info *info, int flags)
128 {
129 - const char *modmagic = get_modinfo(info, "vermagic");
130 int err;
131
132 +#ifndef CONFIG_MODULE_STRIPPED
133 + const char *modmagic = get_modinfo(info, "vermagic");
134 +
135 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
136 modmagic = NULL;
137
138 @@ -2968,6 +2970,7 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
139 mod->name);
140 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
141 }
142 +#endif
143
144 if (get_modinfo(info, "staging")) {
145 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
146 diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
147 index bd8349759095..80f214adbe44 100644
148 --- a/scripts/mod/modpost.c
149 +++ b/scripts/mod/modpost.c
150 @@ -1964,7 +1964,9 @@ static void read_symbols(char *modname)
151 symname = remove_dot(info.strtab + sym->st_name);
152
153 handle_modversions(mod, &info, sym, symname);
154 +#ifndef CONFIG_MODULE_STRIPPED
155 handle_moddevtable(mod, &info, sym, symname);
156 +#endif
157 }
158 if (!is_vmlinux(modname) ||
159 (is_vmlinux(modname) && vmlinux_section_warnings))
160 @@ -2108,7 +2110,9 @@ static void add_header(struct buffer *b, struct module *mod)
161 buf_printf(b, "#include <linux/vermagic.h>\n");
162 buf_printf(b, "#include <linux/compiler.h>\n");
163 buf_printf(b, "\n");
164 +#ifndef CONFIG_MODULE_STRIPPED
165 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
166 +#endif
167 buf_printf(b, "\n");
168 buf_printf(b, "__visible struct module __this_module\n");
169 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
170 @@ -2125,16 +2129,20 @@ static void add_header(struct buffer *b, struct module *mod)
171
172 static void add_intree_flag(struct buffer *b, int is_intree)
173 {
174 +#ifndef CONFIG_MODULE_STRIPPED
175 if (is_intree)
176 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
177 +#endif
178 }
179
180 static void add_staging_flag(struct buffer *b, const char *name)
181 {
182 +#ifndef CONFIG_MODULE_STRIPPED
183 static const char *staging_dir = "drivers/staging";
184
185 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
186 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
187 +#endif
188 }
189
190 /* In kernel, this size is defined in linux/module.h;
191 @@ -2238,11 +2246,13 @@ static void add_depends(struct buffer *b, struct module *mod,
192
193 static void add_srcversion(struct buffer *b, struct module *mod)
194 {
195 +#ifndef CONFIG_MODULE_STRIPPED
196 if (mod->srcversion[0]) {
197 buf_printf(b, "\n");
198 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
199 mod->srcversion);
200 }
201 +#endif
202 }
203
204 static void write_if_changed(struct buffer *b, const char *fname)
205 @@ -2476,7 +2486,9 @@ int main(int argc, char **argv)
206 add_staging_flag(&buf, mod->name);
207 err |= add_versions(&buf, mod);
208 add_depends(&buf, mod, modules);
209 +#ifndef CONFIG_MODULE_STRIPPED
210 add_moddevtable(&buf, mod);
211 +#endif
212 add_srcversion(&buf, mod);
213
214 sprintf(fname, "%s.mod.c", mod->name);
215 --
216 2.11.0
217