@@ -521,8 +521,8 @@ otherwise the command line check will fail, and the target will
always be built.
If the target is already listed in the recognized syntax such as
-obj-y/m, lib-y/m, extra-y/m, always-y/m, hostprogs, userprogs, Kbuild
-automatically adds it to $(targets). Otherwise, the target must be
+obj-y/m, lib-y/m, extra-y/m, always-y/m, hostprogs, userprogs, blobs,
+Kbuild automatically adds it to $(targets). Otherwise, the target must be
explicitly added to $(targets).
Assignments to $(targets) are without $(obj)/ prefix. if_changed may be
@@ -1014,6 +1014,25 @@ There are two ways to do this.
This will tell Kbuild to build binderfs_example when it visits this
Makefile.
+.. _kbuild_blobs:
+
+Blob framework
+==============
+
+Kbuild supports wrapping source or generated files into object files which are linked
+into the kernel and then accessed at runtime through ``include/linux/blob.h``.
+
+Example::
+
+ obj-m := some-module.o
+ userprogs := some-userprog
+ blobs := some-userprog.blob.o
+ some-userprog.blob-symbol := some_userprog
+ some-module-y += some-userprog.blob.o
+
+Kbuild will build the :ref:`userprog <kbuild_userprogs>` ``some-userprog`` and
+link it into ``some-module`` from where it can be accessed as ``BLOB(some_userprog)``.
+
Kbuild clean infrastructure
===========================
@@ -12847,11 +12847,13 @@ Q: https://patchwork.kernel.org/project/linux-kbuild/list/
T: git git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild.git
F: Documentation/kbuild/
F: Makefile
+F: include/linux/blob.h
F: scripts/*vmlinux*
F: scripts/Kbuild*
F: scripts/Makefile*
F: scripts/bash-completion/
F: scripts/basic/
+F: scripts/blob-wrap.c
F: scripts/clang-tools/
F: scripts/dummy-tools/
F: scripts/include/
new file mode 100644
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Linkable blob API.
+ *
+ * Copyright (C) 2025, Linutronix GmbH.
+ * Author: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
+ */
+
+#ifndef _LINUX_BLOB_H
+#define _LINUX_BLOB_H
+
+#include <linux/args.h>
+#include <linux/compiler_types.h>
+#include <linux/types.h>
+
+struct blob {
+ const char *const path;
+ const u8 *data;
+ const u8 __private *end;
+};
+
+#define BLOB(_symbol) ({ \
+ extern const struct blob CONCATENATE(__blob_, _symbol); \
+ &CONCATENATE(__blob_, _symbol); \
+})
+
+static inline size_t blob_size(const struct blob *blob)
+{
+ return ACCESS_PRIVATE(blob, end) - blob->data;
+}
+
+#endif /* _LINUX_BLOB_H */
new file mode 100644
@@ -0,0 +1,19 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Build linkable blobs
+#
+
+blobs := $(addprefix $(obj)/, $(blobs))
+
+blob-stem = $(subst -,_,$(subst .blob,,$(basename $(patsubst $(obj)/%,%,$@))))
+blob-symbol = $(or $($(target-stem)-symbol),$(blob-stem))
+
+blob-flags = -DBLOB_SYMBOL="$(blob-symbol)" -DBLOB_INPUT=$<
+
+quiet_cmd_blob = BLOB $@
+ cmd_blob = $(CC) $(c_flags) $(blob-flags) -c -o $@ $(srctree)/scripts/blob-wrap.c
+
+$(blobs): $(obj)/%.blob.o: $(obj)/% $(srctree)/scripts/blob-wrap.c FORCE
+ $(call if_changed_dep,blob)
+
+targets += $(blobs)
@@ -436,6 +436,12 @@ ifneq ($(need-dtbslist)$(dtb-y)$(dtb-)$(filter %.dtb %.dtb.o %.dtbo.o,$(targets)
include $(srctree)/scripts/Makefile.dtbs
endif
+# $(sort ...) is used here to remove duplicated words and excessive spaces.
+blobs := $(sort $(blobs))
+ifneq ($(blobs),)
+include $(srctree)/scripts/Makefile.blobs
+endif
+
# Build
# ---------------------------------------------------------------------------
@@ -25,7 +25,7 @@ subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn))
# directory
__clean-files := \
- $(clean-files) $(targets) $(hostprogs) $(userprogs) \
+ $(clean-files) $(targets) $(hostprogs) $(userprogs) $(blobs) \
$(extra-y) $(extra-m) $(extra-) \
$(always-y) $(always-m) $(always-) \
$(hostprogs-always-y) $(hostprogs-always-m) $(hostprogs-always-) \
new file mode 100644
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/args.h>
+#include <linux/blob.h>
+#include <linux/stringify.h>
+
+#define BLOB_SYMBOL_DATA CONCATENATE(__blob_data_, BLOB_SYMBOL)
+#define BLOB_SYMBOL_END CONCATENATE(__blob_end_, BLOB_SYMBOL)
+
+asm (
+" .pushsection .rodata, \"a\"\n"
+" .global " __stringify(BLOB_SYMBOL_DATA) "\n"
+__stringify(BLOB_SYMBOL_DATA) ":\n"
+" .incbin \"" __stringify(BLOB_INPUT) "\"\n"
+" .global " __stringify(BLOB_SYMBOL_END) "\n"
+__stringify(BLOB_SYMBOL_END) ":\n"
+" .popsection\n"
+);
+
+extern const u8 BLOB_SYMBOL_DATA;
+extern const u8 BLOB_SYMBOL_END;
+
+const struct blob CONCATENATE(__blob_, BLOB_SYMBOL) = {
+ .path = __stringify(BLOB_INPUT),
+ .data = &BLOB_SYMBOL_DATA,
+ .end = &BLOB_SYMBOL_END,
+};
Various subsystems embed non-code build artifacts into the kernel, for example the initramfs, /proc/config.gz, vDSO image, etc. Currently each user has their own implementation for that. Add a common "blob" framework to provide this functionality. It provides standard kbuild and C APIs to embed and later access non-code build artifacts into the kernel image or modules. Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de> --- Due to its closeness to kbuild this is currently added to its MAINTAINER entry. But I can also maintain it on its own. --- Documentation/kbuild/makefiles.rst | 23 +++++++++++++++++++++-- MAINTAINERS | 2 ++ include/linux/blob.h | 32 ++++++++++++++++++++++++++++++++ scripts/Makefile.blobs | 19 +++++++++++++++++++ scripts/Makefile.build | 6 ++++++ scripts/Makefile.clean | 2 +- scripts/blob-wrap.c | 27 +++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 3 deletions(-)