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
6 Signed-off-by: Felix Fietkau <nbd@nbd.name>
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(-)
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);
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)
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.
31 -#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
32 +#define MODULE_AUTHOR(_author) MODULE_INFO_STRIP(author, _author)
34 /* What your module does. */
35 -#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
36 +#define MODULE_DESCRIPTION(_description) MODULE_INFO_STRIP(description, _description)
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 \
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)
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)
61 struct notifier_block;
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
68 /* Chosen so that structs with an unsigned long line up. */
69 #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
71 +/* This struct is here for syntactic coherency, it is not used */
72 +#define __MODULE_INFO_DISABLED(name) \
73 + struct __UNIQUE_ID(name) {}
75 +#ifdef CONFIG_MODULE_STRIPPED
76 +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name)
78 +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info)
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
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)
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)
103 diff --git a/init/Kconfig b/init/Kconfig
104 index 34407f15e6d3..9f2c0cabbc97 100644
107 @@ -2095,6 +2095,13 @@ config TRIM_UNUSED_KSYMS
109 If unsure, or if you need to build out-of-tree modules, say N.
111 +config MODULE_STRIPPED
112 + bool "Reduce module size"
115 + Remove module parameter descriptions, author info, version, aliases,
116 + device tables, etc.
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)
127 static int check_modinfo(struct module *mod, struct load_info *info, int flags)
129 - const char *modmagic = get_modinfo(info, "vermagic");
132 +#ifndef CONFIG_MODULE_STRIPPED
133 + const char *modmagic = get_modinfo(info, "vermagic");
135 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
138 @@ -2968,6 +2970,7 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
140 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
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);
153 handle_modversions(mod, &info, sym, symname);
154 +#ifndef CONFIG_MODULE_STRIPPED
155 handle_moddevtable(mod, &info, sym, symname);
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");
164 +#ifndef CONFIG_MODULE_STRIPPED
165 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\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)
172 static void add_intree_flag(struct buffer *b, int is_intree)
174 +#ifndef CONFIG_MODULE_STRIPPED
176 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
180 static void add_staging_flag(struct buffer *b, const char *name)
182 +#ifndef CONFIG_MODULE_STRIPPED
183 static const char *staging_dir = "drivers/staging";
185 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
186 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
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,
193 static void add_srcversion(struct buffer *b, struct module *mod)
195 +#ifndef CONFIG_MODULE_STRIPPED
196 if (mod->srcversion[0]) {
198 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
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);
212 add_srcversion(&buf, mod);
214 sprintf(fname, "%s.mod.c", mod->name);