b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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 | --- a/include/linux/module.h |
| 16 | +++ b/include/linux/module.h |
| 17 | @@ -157,6 +157,7 @@ extern void cleanup_module(void); |
| 18 | |
| 19 | /* Generic info of form tag = "info" */ |
| 20 | #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info) |
| 21 | +#define MODULE_INFO_STRIP(tag, info) __MODULE_INFO_STRIP(tag, tag, info) |
| 22 | |
| 23 | /* For userspace: you can also call me... */ |
| 24 | #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias) |
| 25 | @@ -216,12 +217,12 @@ extern void cleanup_module(void); |
| 26 | * Author(s), use "Name <email>" or just "Name", for multiple |
| 27 | * authors use multiple MODULE_AUTHOR() statements/lines. |
| 28 | */ |
| 29 | -#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author) |
| 30 | +#define MODULE_AUTHOR(_author) MODULE_INFO_STRIP(author, _author) |
| 31 | |
| 32 | /* What your module does. */ |
| 33 | -#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description) |
| 34 | +#define MODULE_DESCRIPTION(_description) MODULE_INFO_STRIP(description, _description) |
| 35 | |
| 36 | -#ifdef MODULE |
| 37 | +#if defined(MODULE) && !defined(CONFIG_MODULE_STRIPPED) |
| 38 | /* Creates an alias so file2alias.c can find device table. */ |
| 39 | #define MODULE_DEVICE_TABLE(type, name) \ |
| 40 | extern typeof(name) __mod_##type##__##name##_device_table \ |
| 41 | @@ -248,7 +249,9 @@ extern typeof(name) __mod_##type##__##na |
| 42 | */ |
| 43 | |
| 44 | #if defined(MODULE) || !defined(CONFIG_SYSFS) |
| 45 | -#define MODULE_VERSION(_version) MODULE_INFO(version, _version) |
| 46 | +#define MODULE_VERSION(_version) MODULE_INFO_STRIP(version, _version) |
| 47 | +#elif defined(CONFIG_MODULE_STRIPPED) |
| 48 | +#define MODULE_VERSION(_version) __MODULE_INFO_DISABLED(version) |
| 49 | #else |
| 50 | #define MODULE_VERSION(_version) \ |
| 51 | MODULE_INFO(version, _version); \ |
| 52 | @@ -271,7 +274,7 @@ extern typeof(name) __mod_##type##__##na |
| 53 | /* Optional firmware file (or files) needed by the module |
| 54 | * format is simply firmware file name. Multiple firmware |
| 55 | * files require multiple MODULE_FIRMWARE() specifiers */ |
| 56 | -#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware) |
| 57 | +#define MODULE_FIRMWARE(_firmware) MODULE_INFO_STRIP(firmware, _firmware) |
| 58 | |
| 59 | #define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, #ns) |
| 60 | |
| 61 | --- a/include/linux/moduleparam.h |
| 62 | +++ b/include/linux/moduleparam.h |
| 63 | @@ -20,10 +20,24 @@ |
| 64 | /* Chosen so that structs with an unsigned long line up. */ |
| 65 | #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long)) |
| 66 | |
| 67 | +/* This struct is here for syntactic coherency, it is not used */ |
| 68 | +#define __MODULE_INFO_DISABLED(name) \ |
| 69 | + struct __UNIQUE_ID(name) {} |
| 70 | + |
| 71 | +#ifdef CONFIG_MODULE_STRIPPED |
| 72 | +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name) |
| 73 | +#else |
| 74 | +#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info) |
| 75 | +#endif |
| 76 | + |
| 77 | +#ifdef MODULE |
| 78 | #define __MODULE_INFO(tag, name, info) \ |
| 79 | static const char __UNIQUE_ID(name)[] \ |
| 80 | __used __attribute__((section(".modinfo"), unused, aligned(1))) \ |
| 81 | = __MODULE_INFO_PREFIX __stringify(tag) "=" info |
| 82 | +#else |
| 83 | +#define __MODULE_INFO(tag, name, info) __MODULE_INFO_DISABLED(name) |
| 84 | +#endif |
| 85 | |
| 86 | #define __MODULE_PARM_TYPE(name, _type) \ |
| 87 | __MODULE_INFO(parmtype, name##type, #name ":" _type) |
| 88 | @@ -31,7 +45,7 @@ static const char __UNIQUE_ID(name)[] |
| 89 | /* One for each parameter, describing how to use it. Some files do |
| 90 | multiple of these per line, so can't just use MODULE_INFO. */ |
| 91 | #define MODULE_PARM_DESC(_parm, desc) \ |
| 92 | - __MODULE_INFO(parm, _parm, #_parm ":" desc) |
| 93 | + __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc) |
| 94 | |
| 95 | struct kernel_param; |
| 96 | |
| 97 | --- a/init/Kconfig |
| 98 | +++ b/init/Kconfig |
| 99 | @@ -2217,6 +2217,13 @@ config TRIM_UNUSED_KSYMS |
| 100 | |
| 101 | If unsure, or if you need to build out-of-tree modules, say N. |
| 102 | |
| 103 | +config MODULE_STRIPPED |
| 104 | + bool "Reduce module size" |
| 105 | + depends on MODULES |
| 106 | + help |
| 107 | + Remove module parameter descriptions, author info, version, aliases, |
| 108 | + device tables, etc. |
| 109 | + |
| 110 | endif # MODULES |
| 111 | |
| 112 | config MODULES_TREE_LOOKUP |
| 113 | --- a/kernel/module.c |
| 114 | +++ b/kernel/module.c |
| 115 | @@ -3256,9 +3256,11 @@ static int setup_load_info(struct load_i |
| 116 | |
| 117 | static int check_modinfo(struct module *mod, struct load_info *info, int flags) |
| 118 | { |
| 119 | - const char *modmagic = get_modinfo(info, "vermagic"); |
| 120 | int err; |
| 121 | |
| 122 | +#ifndef CONFIG_MODULE_STRIPPED |
| 123 | + const char *modmagic = get_modinfo(info, "vermagic"); |
| 124 | + |
| 125 | if (flags & MODULE_INIT_IGNORE_VERMAGIC) |
| 126 | modmagic = NULL; |
| 127 | |
| 128 | @@ -3279,6 +3281,7 @@ static int check_modinfo(struct module * |
| 129 | mod->name); |
| 130 | add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK); |
| 131 | } |
| 132 | +#endif |
| 133 | |
| 134 | check_modinfo_retpoline(mod, info); |
| 135 | |
| 136 | --- a/scripts/mod/modpost.c |
| 137 | +++ b/scripts/mod/modpost.c |
| 138 | @@ -2057,7 +2057,9 @@ static void read_symbols(const char *mod |
| 139 | symname = remove_dot(info.strtab + sym->st_name); |
| 140 | |
| 141 | handle_modversions(mod, &info, sym, symname); |
| 142 | +#ifndef CONFIG_MODULE_STRIPPED |
| 143 | handle_moddevtable(mod, &info, sym, symname); |
| 144 | +#endif |
| 145 | } |
| 146 | |
| 147 | /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */ |
| 148 | @@ -2271,8 +2273,10 @@ static void add_header(struct buffer *b, |
| 149 | buf_printf(b, "\n"); |
| 150 | buf_printf(b, "BUILD_SALT;\n"); |
| 151 | buf_printf(b, "\n"); |
| 152 | +#ifndef CONFIG_MODULE_STRIPPED |
| 153 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); |
| 154 | buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n"); |
| 155 | +#endif |
| 156 | buf_printf(b, "\n"); |
| 157 | buf_printf(b, "__visible struct module __this_module\n"); |
| 158 | buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n"); |
| 159 | @@ -2289,8 +2293,10 @@ static void add_header(struct buffer *b, |
| 160 | |
| 161 | static void add_intree_flag(struct buffer *b, int is_intree) |
| 162 | { |
| 163 | +#ifndef CONFIG_MODULE_STRIPPED |
| 164 | if (is_intree) |
| 165 | buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n"); |
| 166 | +#endif |
| 167 | } |
| 168 | |
| 169 | /* Cannot check for assembler */ |
| 170 | @@ -2303,8 +2309,10 @@ static void add_retpoline(struct buffer |
| 171 | |
| 172 | static void add_staging_flag(struct buffer *b, const char *name) |
| 173 | { |
| 174 | +#ifndef CONFIG_MODULE_STRIPPED |
| 175 | if (strstarts(name, "drivers/staging")) |
| 176 | buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n"); |
| 177 | +#endif |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | @@ -2388,11 +2396,13 @@ static void add_depends(struct buffer *b |
| 182 | |
| 183 | static void add_srcversion(struct buffer *b, struct module *mod) |
| 184 | { |
| 185 | +#ifndef CONFIG_MODULE_STRIPPED |
| 186 | if (mod->srcversion[0]) { |
| 187 | buf_printf(b, "\n"); |
| 188 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", |
| 189 | mod->srcversion); |
| 190 | } |
| 191 | +#endif |
| 192 | } |
| 193 | |
| 194 | static void write_if_changed(struct buffer *b, const char *fname) |
| 195 | @@ -2662,7 +2672,9 @@ int main(int argc, char **argv) |
| 196 | add_staging_flag(&buf, mod->name); |
| 197 | err |= add_versions(&buf, mod); |
| 198 | add_depends(&buf, mod); |
| 199 | +#ifndef CONFIG_MODULE_STRIPPED |
| 200 | add_moddevtable(&buf, mod); |
| 201 | +#endif |
| 202 | add_srcversion(&buf, mod); |
| 203 | |
| 204 | sprintf(fname, "%s.mod.c", mod->name); |