openwrtv4/target/linux/generic/pending-3.18/204-module_strip.patch

191 lines
6.6 KiB
Diff
Raw Normal View History

From: Felix Fietkau <nbd@nbd.name>
Subject: [PATCH] build: add a hack for removing non-essential module info
Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
--- a/include/linux/module.h
+++ b/include/linux/module.h
generic: keep module aliases inside .modinfo It seems module aliases are actually essential info. E.g. other modules may call xt_request_find_match(NFPROTO_IPV{4,6}, "hashlimit", 1) and the kernel will request user mode modprobe helper for ipt_hashlimit and ip6t_hashlimit respectively which are aliases of xt_hashlimit.ko Compile-tested for armvirt, there is nearly no size impact. Before -rw-r--r-- 1 yousong yousong 304 Jan 10 17:37 config.seed -rw-r--r-- 1 yousong yousong 1634 Jan 11 11:07 lede-armvirt-default.manifest -rw-r--r-- 1 yousong yousong 1533647 Jan 11 11:07 lede-armvirt-default-rootfs.tar.gz -rw-r--r-- 1 yousong yousong 1802711 Jan 11 11:07 lede-armvirt-root.ext4.gz -rw-r--r-- 1 yousong yousong 1544597 Jan 11 11:07 lede-armvirt-rootfs.cpio.gz -rw-r--r-- 1 yousong yousong 1174984 Jan 11 11:07 lede-armvirt-root.squashfs.gz -rwxr-xr-x 1 yousong yousong 1654712 Jan 11 11:07 lede-armvirt-zImage -rwxr-xr-x 1 yousong yousong 2738296 Jan 11 11:07 lede-armvirt-zImage-initramfs drwxr-xr-x 2 yousong yousong 4096 Jan 10 17:35 packages -rw-r--r-- 1 yousong yousong 739 Jan 10 17:37 sha256sums After -rw-r--r-- 1 yousong yousong 304 Jan 10 17:37 config.seed -rw-r--r-- 1 yousong yousong 1634 Jan 11 11:10 lede-armvirt-default.manifest -rw-r--r-- 1 yousong yousong 1533646 Jan 11 11:10 lede-armvirt-default-rootfs.tar.gz -rw-r--r-- 1 yousong yousong 1802711 Jan 11 11:10 lede-armvirt-root.ext4.gz -rw-r--r-- 1 yousong yousong 1544593 Jan 11 11:10 lede-armvirt-rootfs.cpio.gz -rw-r--r-- 1 yousong yousong 1174984 Jan 11 11:10 lede-armvirt-root.squashfs.gz -rwxr-xr-x 1 yousong yousong 1654736 Jan 11 11:10 lede-armvirt-zImage -rwxr-xr-x 1 yousong yousong 2738144 Jan 11 11:10 lede-armvirt-zImage-initramfs drwxr-xr-x 2 yousong yousong 4096 Jan 10 17:35 packages -rw-r--r-- 1 yousong yousong 739 Jan 10 17:37 sha256sums Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2017-01-11 11:57:24 +00:00
@@ -84,6 +84,7 @@ void trim_init_extable(struct module *m)
/* Generic info of form tag = "info" */
#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
+#define MODULE_INFO_STRIP(tag, info) __MODULE_INFO_STRIP(tag, tag, info)
/* For userspace: you can also call me... */
generic: keep module aliases inside .modinfo It seems module aliases are actually essential info. E.g. other modules may call xt_request_find_match(NFPROTO_IPV{4,6}, "hashlimit", 1) and the kernel will request user mode modprobe helper for ipt_hashlimit and ip6t_hashlimit respectively which are aliases of xt_hashlimit.ko Compile-tested for armvirt, there is nearly no size impact. Before -rw-r--r-- 1 yousong yousong 304 Jan 10 17:37 config.seed -rw-r--r-- 1 yousong yousong 1634 Jan 11 11:07 lede-armvirt-default.manifest -rw-r--r-- 1 yousong yousong 1533647 Jan 11 11:07 lede-armvirt-default-rootfs.tar.gz -rw-r--r-- 1 yousong yousong 1802711 Jan 11 11:07 lede-armvirt-root.ext4.gz -rw-r--r-- 1 yousong yousong 1544597 Jan 11 11:07 lede-armvirt-rootfs.cpio.gz -rw-r--r-- 1 yousong yousong 1174984 Jan 11 11:07 lede-armvirt-root.squashfs.gz -rwxr-xr-x 1 yousong yousong 1654712 Jan 11 11:07 lede-armvirt-zImage -rwxr-xr-x 1 yousong yousong 2738296 Jan 11 11:07 lede-armvirt-zImage-initramfs drwxr-xr-x 2 yousong yousong 4096 Jan 10 17:35 packages -rw-r--r-- 1 yousong yousong 739 Jan 10 17:37 sha256sums After -rw-r--r-- 1 yousong yousong 304 Jan 10 17:37 config.seed -rw-r--r-- 1 yousong yousong 1634 Jan 11 11:10 lede-armvirt-default.manifest -rw-r--r-- 1 yousong yousong 1533646 Jan 11 11:10 lede-armvirt-default-rootfs.tar.gz -rw-r--r-- 1 yousong yousong 1802711 Jan 11 11:10 lede-armvirt-root.ext4.gz -rw-r--r-- 1 yousong yousong 1544593 Jan 11 11:10 lede-armvirt-rootfs.cpio.gz -rw-r--r-- 1 yousong yousong 1174984 Jan 11 11:10 lede-armvirt-root.squashfs.gz -rwxr-xr-x 1 yousong yousong 1654736 Jan 11 11:10 lede-armvirt-zImage -rwxr-xr-x 1 yousong yousong 2738144 Jan 11 11:10 lede-armvirt-zImage-initramfs drwxr-xr-x 2 yousong yousong 4096 Jan 10 17:35 packages -rw-r--r-- 1 yousong yousong 739 Jan 10 17:37 sha256sums Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2017-01-11 11:57:24 +00:00
#define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
@@ -127,12 +128,12 @@ void trim_init_extable(struct module *m)
* Author(s), use "Name <email>" or just "Name", for multiple
* authors use multiple MODULE_AUTHOR() statements/lines.
*/
-#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
+#define MODULE_AUTHOR(_author) MODULE_INFO_STRIP(author, _author)
/* What your module does. */
-#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
+#define MODULE_DESCRIPTION(_description) MODULE_INFO_STRIP(description, _description)
-#ifdef MODULE
+#if defined(MODULE) && !defined(CONFIG_MODULE_STRIPPED)
/* Creates an alias so file2alias.c can find device table. */
#define MODULE_DEVICE_TABLE(type, name) \
extern const struct type##_device_id __mod_##type##__##name##_device_table \
@@ -159,7 +160,9 @@ void trim_init_extable(struct module *m)
*/
#if defined(MODULE) || !defined(CONFIG_SYSFS)
-#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
+#define MODULE_VERSION(_version) MODULE_INFO_STRIP(version, _version)
+#elif defined(CONFIG_MODULE_STRIPPED)
+#define MODULE_VERSION(_version) __MODULE_INFO_DISABLED(version)
#else
#define MODULE_VERSION(_version) \
static struct module_version_attribute ___modver_attr = { \
@@ -181,7 +184,7 @@ void trim_init_extable(struct module *m)
/* Optional firmware file (or files) needed by the module
* format is simply firmware file name. Multiple firmware
* files require multiple MODULE_FIRMWARE() specifiers */
-#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
+#define MODULE_FIRMWARE(_firmware) MODULE_INFO_STRIP(firmware, _firmware)
/* Given an address, look for it in the exception tables */
const struct exception_table_entry *search_exception_tables(unsigned long add);
--- a/include/linux/moduleparam.h
+++ b/include/linux/moduleparam.h
@@ -16,6 +16,16 @@
/* Chosen so that structs with an unsigned long line up. */
#define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
+/* This struct is here for syntactic coherency, it is not used */
+#define __MODULE_INFO_DISABLED(name) \
+ struct __UNIQUE_ID(name) {}
+
+#ifdef CONFIG_MODULE_STRIPPED
+#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name)
+#else
+#define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info)
+#endif
+
#ifdef MODULE
#define __MODULE_INFO(tag, name, info) \
static const char __UNIQUE_ID(name)[] \
@@ -23,8 +33,7 @@ static const char __UNIQUE_ID(name)[]
= __stringify(tag) "=" info
#else /* !MODULE */
/* This struct is here for syntactic coherency, it is not used */
-#define __MODULE_INFO(tag, name, info) \
- struct __UNIQUE_ID(name) {}
+#define __MODULE_INFO(tag, name, info) __MODULE_INFO_DISABLED(name)
#endif
#define __MODULE_PARM_TYPE(name, _type) \
__MODULE_INFO(parmtype, name##type, #name ":" _type)
@@ -32,7 +41,7 @@ static const char __UNIQUE_ID(name)[]
/* One for each parameter, describing how to use it. Some files do
multiple of these per line, so can't just use MODULE_INFO. */
#define MODULE_PARM_DESC(_parm, desc) \
- __MODULE_INFO(parm, _parm, #_parm ":" desc)
+ __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc)
struct kernel_param;
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1987,6 +1987,13 @@ config MODULE_COMPRESS_XZ
endchoice
+config MODULE_STRIPPED
+ bool "Reduce module size"
+ depends on MODULES
+ help
+ Remove module parameter descriptions, author info, version, aliases,
+ device tables, etc.
+
endif # MODULES
config INIT_ALL_POSSIBLE
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2699,6 +2699,7 @@ static struct module *setup_load_info(st
static int check_modinfo(struct module *mod, struct load_info *info, int flags)
{
+#ifndef CONFIG_MODULE_STRIPPED
const char *modmagic = get_modinfo(info, "vermagic");
int err;
@@ -2724,6 +2725,7 @@ static int check_modinfo(struct module *
pr_warn("%s: module is from the staging directory, the quality "
"is unknown, you have been warned.\n", mod->name);
}
+#endif
/* Set up license info based on the info section */
set_license(mod, get_modinfo(info, "license"));
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1726,7 +1726,9 @@ static void read_symbols(char *modname)
symname = remove_dot(info.strtab + sym->st_name);
handle_modversions(mod, &info, sym, symname);
+#ifndef CONFIG_MODULE_STRIPPED
handle_moddevtable(mod, &info, sym, symname);
+#endif
}
if (!is_vmlinux(modname) ||
(is_vmlinux(modname) && vmlinux_section_warnings))
@@ -1870,7 +1872,9 @@ static void add_header(struct buffer *b,
buf_printf(b, "#include <linux/vermagic.h>\n");
buf_printf(b, "#include <linux/compiler.h>\n");
buf_printf(b, "\n");
+#ifndef CONFIG_MODULE_STRIPPED
buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
+#endif
buf_printf(b, "\n");
buf_printf(b, "__visible struct module __this_module\n");
buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
@@ -1887,16 +1891,20 @@ static void add_header(struct buffer *b,
static void add_intree_flag(struct buffer *b, int is_intree)
{
+#ifndef CONFIG_MODULE_STRIPPED
if (is_intree)
buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
+#endif
}
static void add_staging_flag(struct buffer *b, const char *name)
{
+#ifndef CONFIG_MODULE_STRIPPED
static const char *staging_dir = "drivers/staging";
if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
+#endif
}
/**
@@ -1989,11 +1997,13 @@ static void add_depends(struct buffer *b
static void add_srcversion(struct buffer *b, struct module *mod)
{
+#ifndef CONFIG_MODULE_STRIPPED
if (mod->srcversion[0]) {
buf_printf(b, "\n");
buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
mod->srcversion);
}
+#endif
}
static void write_if_changed(struct buffer *b, const char *fname)
@@ -2224,7 +2234,9 @@ int main(int argc, char **argv)
add_staging_flag(&buf, mod->name);
err |= add_versions(&buf, mod);
add_depends(&buf, mod, modules);
+#ifndef CONFIG_MODULE_STRIPPED
add_moddevtable(&buf, mod);
+#endif
add_srcversion(&buf, mod);
sprintf(fname, "%s.mod.c", mod->name);