diff --git a/Makefile b/Makefile index ff17e1d2166b9e4026ff91454318ec1adeb3d22c..516be7a1e8bbb4ec876476a610b32ec5439d6745 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 3159f69b9336563d47719078417eb6adc743c052..24f60f95ee78f35ef0213eacafe3d108fa29881d 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 b666967fd5706208a1899334165b1f982cdcafbc..8c5601f55e67907b64d61ce310f46a2b6785564c 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 0000000000000000000000000000000000000000..51de991cf7de44f8e86fee9767c50662d66eb97b --- /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 2f4e3a14586caeb4ae9d14e8afb6b59000db7eab..a4a31b2eca06ffd92dc9286fe2ee078001482568 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 af6aa7aa455416fefb4393725fe406b12f8905f5..242da1062cf120bcde4c100781a5db2b8bd24cbc 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