From 04d249c05140b17276991eebc7090c8b593f4249 Mon Sep 17 00:00:00 2001
From: Armin Luntzer <armin.luntzer@univie.ac.at>
Date: Tue, 31 Jan 2017 12:43:59 +0100
Subject: [PATCH] add option to embed modules image in kernel binary

---
 Makefile               | 10 ++++++++++
 init/Kconfig           | 11 +++++------
 init/Makefile          |  1 +
 init/modules-image.c   |  8 ++++++++
 scripts/Makefile.build |  2 +-
 scripts/link-leanos.sh | 23 ++++++++++++++++++++++-
 6 files changed, 47 insertions(+), 8 deletions(-)
 create mode 100644 init/modules-image.c

diff --git a/Makefile b/Makefile
index ff17e1d..516be7a 100644
--- a/Makefile
+++ b/Makefile
@@ -730,7 +730,11 @@ ifdef CONFIG_MODULES
 
 # By default, build modules as well
 
+ifeq ($(CONFIG_EMBED_MODULES_IMAGE), y)
+all: modules_embed_image
+else
 all: modules
+endif
 
 # Build modules
 #
@@ -751,6 +755,12 @@ modules.builtin: $(leanos-dirs:%=%/modules.builtin)
 	$(Q)$(MAKE) $(modbuiltin)=$*
 
 
+PHONY += modules_embed_image
+modules_embed_image: modules
+	@$(kecho) '  Embedding module image, stage 3.';
+	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/link-leanos.sh embed
+
+
 # Target to prepare building external modules
 PHONY += modules_prepare
 modules_prepare: prepare scripts
diff --git a/init/Kconfig b/init/Kconfig
index 3159f69..24f60f9 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -69,13 +69,12 @@ menuconfig MODULES
 
 if MODULES
 
-config MODULE_UNLOAD
-	bool "Module unloading"
+config EMBED_MODULES_IMAGE
+	bool "Embed an image with all generated loadable modules"
 	help
-	  Without this option you will not be able to unload any
-	  modules (note that some modules may not be unloadable
-	  anyway), which makes your kernel smaller, faster
-	  and simpler.  If unsure, say Y.
+	  Enable this option in order to embed an image containing all
+	  generated modules into the final kernel binary.
+	  This is a convenience option, if unsure, say Y.
 
 endif # MODULES
 
diff --git a/init/Makefile b/init/Makefile
index b666967..8c5601f 100644
--- a/init/Makefile
+++ b/init/Makefile
@@ -1 +1,2 @@
 obj-y += main.o
+obj-$(CONFIG_EMBED_MODULES_IMAGE) += modules-image.o
diff --git a/init/modules-image.c b/init/modules-image.c
new file mode 100644
index 0000000..51de991
--- /dev/null
+++ b/init/modules-image.c
@@ -0,0 +1,8 @@
+/**
+ * linker references to embedded modules.image
+ */
+
+extern unsigned char _binary_modules_image_start;
+extern unsigned char _binary_modules_image_end;
+extern unsigned char _binary_modules_image_size;
+
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 2f4e3a1..a4a31b2 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -358,7 +358,7 @@ endif # builtin-target
 modorder-cmds =						\
 	$(foreach m, $(modorder),			\
 		$(if $(filter %/modules.order, $m),	\
-			cat $m;, echo kernel/$m;))
+			cat $m;, echo $m;))
 
 $(modorder-target): $(subdir-ym) FORCE
 	$(Q)(cat /dev/null; $(modorder-cmds)) > $@
diff --git a/scripts/link-leanos.sh b/scripts/link-leanos.sh
index af6aa7a..242da10 100755
--- a/scripts/link-leanos.sh
+++ b/scripts/link-leanos.sh
@@ -76,6 +76,7 @@ modpost_link()
 # Link of leanos
 # ${1} - optional extra .o files
 # ${2} - output file
+# ${3} - extra flags
 leanos_link()
 {
 	local lds="${objtree}/${KBUILD_LDS}"
@@ -84,7 +85,7 @@ leanos_link()
 
 	# since we link against the BCC libc at this time, we'll just
 	# call $CC instead of LD
-	${CC} ${LDFLAGS} ${LDFLAGS_leanos} -o ${2}		\
+	${CC} ${LDFLAGS} ${LDFLAGS_leanos} ${3} -o ${2}		\
 		${KBUILD_LEANOS_INIT} ${KBUILD_LEANOS_MAIN} ${1}
 
 #	if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
@@ -289,5 +290,25 @@ if [ -n "${CONFIG_KALLSYMS}" ]; then
 	fi
 fi
 
+
+# this is a 3rd pass option, we need modules.order beforehand
+if [ "$1" = "embed" ]; then
+
+	if [ ! -s ${srctree}/modules.order ]; then
+		echo >&2
+		echo >&2 modules.order empty or nonexistant, cannot embed image.
+		echo >&2 Maybe you have no loadable modules configured?
+		echo >&2 Kernel image unchanged.
+		echo >&2
+		exit
+	fi
+
+	embedflags="-Wl,--format=binary -Wl,modules.image -Wl,--format=default"
+	rm -f modules.image
+	${AR} rcs ${srctree}/modules.image $(tr '\n' ' ' < ${srctree}/modules.order)
+	leanos_link "${kallsymso}" leanos "${embedflags}"
+	exit
+fi
+
 # We made a new kernel - delete old version file
 rm -f .old_version
-- 
GitLab