linux-riscv.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
To: "Masahiro Yamada" <masahiroy@kernel.org>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Willy Tarreau" <w@1wt.eu>,
	"Thomas Weißschuh" <linux@weissschuh.net>,
	"Brendan Higgins" <brendan.higgins@linux.dev>,
	"David Gow" <davidgow@google.com>, "Rae Moar" <rmoar@google.com>,
	"Shuah Khan" <shuah@kernel.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Nicolas Schier" <nicolas.schier@linux.dev>,
	"Paul Walmsley" <paul.walmsley@sifive.com>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Albert Ou" <aou@eecs.berkeley.edu>,
	"Alexandre Ghiti" <alex@ghiti.fr>,
	"Nicolas Schier" <nicolas.schier@linux.dev>
Cc: "Christophe Leroy" <christophe.leroy@csgroup.eu>,
	linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
	linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org,
	workflows@vger.kernel.org,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
	"Nicolas Schier" <nicolas.schier@linux.dev>
Subject: [PATCH v3 07/16] kbuild: introduce blob framework
Date: Wed, 11 Jun 2025 09:38:13 +0200	[thread overview]
Message-ID: <20250611-kunit-kselftests-v3-7-55e3d148cbc6@linutronix.de> (raw)
In-Reply-To: <20250611-kunit-kselftests-v3-0-55e3d148cbc6@linutronix.de>

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.

Reviewed-by: Nicolas Schier <n.schier@avm.de>
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               | 31 +++++++++++++++++++++++++++++++
 scripts/Makefile.blobs             | 19 +++++++++++++++++++
 scripts/Makefile.build             |  6 ++++++
 scripts/Makefile.clean             |  2 +-
 scripts/blob-wrap.c                | 27 +++++++++++++++++++++++++++
 7 files changed, 107 insertions(+), 3 deletions(-)

diff --git a/Documentation/kbuild/makefiles.rst b/Documentation/kbuild/makefiles.rst
index 2adea36ac6ebf6c292e01a3e04c0b633e3c1b8ad..5d158780948ab6d7d21287231e310dc87d5e1dc7 100644
--- a/Documentation/kbuild/makefiles.rst
+++ b/Documentation/kbuild/makefiles.rst
@@ -525,8 +525,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
@@ -1019,6 +1019,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
 ===========================
 
diff --git a/MAINTAINERS b/MAINTAINERS
index a92290fffa163f9fe8fe3f04bf66426f9a894409..435f8af750d40d859b48dd4f93f7991768f218e1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13063,11 +13063,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/
diff --git a/include/linux/blob.h b/include/linux/blob.h
new file mode 100644
index 0000000000000000000000000000000000000000..4104d04e036fadce220e05fd2d9b996323dd06e8
--- /dev/null
+++ b/include/linux/blob.h
@@ -0,0 +1,31 @@
+/* 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/types.h>
+
+struct blob {
+	const char *const path;
+	const u8 *data;
+	const u8 *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 blob->end - blob->data;
+}
+
+#endif /* _LINUX_BLOB_H */
diff --git a/scripts/Makefile.blobs b/scripts/Makefile.blobs
new file mode 100644
index 0000000000000000000000000000000000000000..fd20ebb41c1d6509750debf7896a08a143d28759
--- /dev/null
+++ b/scripts/Makefile.blobs
@@ -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)
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index a6461ea411f7a95a9dd156897bec43cb22ef1092..de000268a538875596aae597efaf06058474b17d 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -529,6 +529,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
 # ---------------------------------------------------------------------------
 
diff --git a/scripts/Makefile.clean b/scripts/Makefile.clean
index 6ead00ec7313b3e4330a8de5f1342f2da1d6eb84..536972b0a528d117e17296da9936825c3903af6e 100644
--- a/scripts/Makefile.clean
+++ b/scripts/Makefile.clean
@@ -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-) \
diff --git a/scripts/blob-wrap.c b/scripts/blob-wrap.c
new file mode 100644
index 0000000000000000000000000000000000000000..82ab3bc641bd69ec35029c2f4a9dd6d6c6720a02
--- /dev/null
+++ b/scripts/blob-wrap.c
@@ -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,
+};

-- 
2.49.0


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  parent reply	other threads:[~2025-06-11  8:02 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-11  7:38 [PATCH v3 00/16] kunit: Introduce UAPI testing framework Thomas Weißschuh
2025-06-11  7:38 ` [PATCH v3 01/16] kbuild: userprogs: avoid duplicating of flags inherited from kernel Thomas Weißschuh
2025-06-11 13:52   ` Nicolas Schier
2025-06-11  7:38 ` [PATCH v3 02/16] kbuild: userprogs: also inherit byte order and ABI " Thomas Weißschuh
2025-06-11 13:53   ` Nicolas Schier
2025-06-16 14:49   ` Masahiro Yamada
2025-06-17  7:39     ` Thomas Weißschuh
2025-06-18  1:14       ` Masahiro Yamada
2025-06-11  7:38 ` [PATCH v3 03/16] init: re-add CONFIG_CC_CAN_LINK_STATIC Thomas Weißschuh
2025-06-11 14:04   ` Nicolas Schier
2025-06-11  7:38 ` [PATCH v3 04/16] kbuild: userprogs: add nolibc support Thomas Weißschuh
2025-06-11 14:09   ` Nicolas Schier
2025-06-16 15:35   ` Masahiro Yamada
2025-06-17  7:59     ` Thomas Weißschuh
2025-06-18  1:15       ` Masahiro Yamada
2025-06-11  7:38 ` [PATCH v3 05/16] kbuild: introduce CONFIG_ARCH_HAS_NOLIBC Thomas Weißschuh
2025-06-11  7:38 ` [PATCH v3 06/16] kbuild: doc: add label for userprogs section Thomas Weißschuh
2025-06-11  7:38 ` Thomas Weißschuh [this message]
2025-06-16 15:38   ` [PATCH v3 07/16] kbuild: introduce blob framework Masahiro Yamada
2025-06-17  7:50     ` Thomas Weißschuh
2025-06-11  7:38 ` [PATCH v3 08/16] kunit: tool: Add test for nested test result reporting Thomas Weißschuh
2025-06-20  9:37   ` David Gow
2025-06-20 13:20     ` Thomas Weißschuh
2025-06-11  7:38 ` [PATCH v3 09/16] kunit: tool: Don't overwrite test status based on subtest counts Thomas Weißschuh
2025-06-20  9:37   ` David Gow
2025-06-20 13:23     ` Thomas Weißschuh
2025-06-11  7:38 ` [PATCH v3 10/16] kunit: tool: Parse skipped tests from kselftest.h Thomas Weißschuh
2025-06-20  9:37   ` David Gow
2025-06-11  7:38 ` [PATCH v3 11/16] kunit: Always descend into kunit directory during build Thomas Weißschuh
2025-06-20  9:47   ` David Gow
2025-06-11  7:38 ` [PATCH v3 12/16] kunit: qemu_configs: loongarch: Enable LSX/LSAX Thomas Weißschuh
2025-06-20  9:37   ` David Gow
2025-06-11  7:38 ` [PATCH v3 13/16] kunit: Introduce UAPI testing framework Thomas Weißschuh
2025-06-20  9:47   ` David Gow
2025-06-20 13:43     ` Thomas Weißschuh
2025-06-11  7:38 ` [PATCH v3 14/16] kunit: uapi: Add example for UAPI tests Thomas Weißschuh
2025-06-20  9:47   ` David Gow
2025-06-11  7:38 ` [PATCH v3 15/16] kunit: uapi: Introduce preinit executable Thomas Weißschuh
2025-06-20  9:48   ` David Gow
2025-06-11  7:38 ` [PATCH v3 16/16] kunit: uapi: Validate usability of /proc Thomas Weißschuh
2025-06-20  9:48   ` David Gow
2025-06-20 13:50     ` Thomas Weißschuh
2025-06-20  9:37 ` [PATCH v3 00/16] kunit: Introduce UAPI testing framework David Gow
2025-06-20 13:18   ` Thomas Weißschuh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250611-kunit-kselftests-v3-7-55e3d148cbc6@linutronix.de \
    --to=thomas.weissschuh@linutronix.de \
    --cc=akpm@linux-foundation.org \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=brendan.higgins@linux.dev \
    --cc=christophe.leroy@csgroup.eu \
    --cc=corbet@lwn.net \
    --cc=davidgow@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux@weissschuh.net \
    --cc=masahiroy@kernel.org \
    --cc=nathan@kernel.org \
    --cc=nicolas.schier@linux.dev \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=rmoar@google.com \
    --cc=shuah@kernel.org \
    --cc=w@1wt.eu \
    --cc=workflows@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).