Files
unkernel/kernel/patches/linux/kbuild-modpost-read-Wasm-relocatable-objects.patch

977 lines
28 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Pooya Parsa <pooya@pi0.io>
Date: Mon, 30 Nov 2026 08:42:27 +0000
Subject: [PATCH] kbuild: modpost: read Wasm relocatable objects
modpost reads ELF objects only, and on Wasm it skipped vmlinux.o. Turn a
Wasm relocatable object into an in-memory ELF image that carries the same
information (sections, symbols, relocations), so that the rest of modpost
runs unchanged: the exports of vmlinux.o and of modules, modinfo, the
.mod.c of a module. mk_elfconfig yields ELFCLASS64 for wasm64.
---
scripts/mod/Makefile | 4 +-
scripts/mod/mk_elfconfig.c | 19 +-
scripts/mod/modpost.c | 75 +++-
scripts/mod/modpost.h | 3 +
scripts/mod/wasm.c | 720 +++++++++++++++++++++++++++++++++++++
scripts/mod/wasm.h | 14 +
6 files changed, 820 insertions(+), 15 deletions(-)
create mode 100644 scripts/mod/wasm.c
create mode 100644 scripts/mod/wasm.h
diff --git a/scripts/mod/Makefile b/scripts/mod/Makefile
--- a/scripts/mod/Makefile
+++ b/scripts/mod/Makefile
@@ -4,7 +4,7 @@ CFLAGS_REMOVE_empty.o += $(CC_FLAGS_LTO)
hostprogs-always-y += modpost mk_elfconfig
always-y += empty.o
-modpost-objs := modpost.o file2alias.o sumversion.o symsearch.o
+modpost-objs := modpost.o file2alias.o sumversion.o symsearch.o wasm.o
devicetable-offsets-file := devicetable-offsets.h
@@ -15,7 +15,7 @@ targets += $(devicetable-offsets-file) devicetable-offsets.s
# dependencies on generated files need to be listed explicitly
-$(obj)/modpost.o $(obj)/file2alias.o $(obj)/sumversion.o $(obj)/symsearch.o: $(obj)/elfconfig.h
+$(obj)/modpost.o $(obj)/file2alias.o $(obj)/sumversion.o $(obj)/symsearch.o $(obj)/wasm.o: $(obj)/elfconfig.h
$(obj)/file2alias.o: $(obj)/$(devicetable-offsets-file)
quiet_cmd_elfconfig = MKELF $@
diff --git a/scripts/mod/mk_elfconfig.c b/scripts/mod/mk_elfconfig.c
--- a/scripts/mod/mk_elfconfig.c
+++ b/scripts/mod/mk_elfconfig.c
@@ -15,8 +15,23 @@ main(int argc, char **argv)
}
if (memcmp(ei, "\x00" "asm", 4UL) == 0) {
- /* Wasm (not ELF) - provide some emulation. */
- printf("#define KERNEL_ELFCLASS ELFCLASS32\n");
+ /*
+ * Wasm (not ELF): modpost reads Wasm objects as ELF of this
+ * class (scripts/mod/wasm.c). An object built for wasm64 lists
+ * the memory64 feature in its target_features section.
+ */
+ static char buf[1 << 16];
+ size_t n = fread(buf, 1, sizeof(buf), stdin);
+ int is64 = 0;
+ size_t i;
+
+ for (i = 0; i + 10 <= n; i++)
+ if (!memcmp(buf + i, "+\x08memory64", 10)) {
+ is64 = 1;
+ break;
+ }
+ printf("#define KERNEL_ELFCLASS %s\n", is64 ? "ELFCLASS64" : "ELFCLASS32");
+ return 0;
} else if (memcmp(ei, ELFMAG, SELFMAG) != 0) {
fprintf(stderr, "Error: not ELF, nor Wasm\n");
return 1;
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -26,6 +26,7 @@
#include <list.h>
#include <xalloc.h>
#include "modpost.h"
+#include "wasm.h"
#include "../../include/linux/license.h"
#define MODULE_NS_PREFIX "module:"
@@ -74,8 +75,6 @@ static unsigned int nr_unresolved;
#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
-static void write_vmlinux_export_c_file(struct module *mod);
-
void modpost_log(bool is_error, struct module *mod, const char *fmt, ...)
{
va_list arglist;
@@ -434,14 +433,15 @@ static int parse_elf(struct elf_info *info, const char *filename)
exit(1);
}
- /* Generate a dummy for vmlinux on Wasm. */
- if (strcmp(filename, "vmlinux.o") == 0 &&
- info->size >= 4UL &&
- memcmp(hdr, "\x00" "asm", 4UL) == 0) {
- struct module *mod =
- new_module(filename, strlen(filename) - strlen(".o"));
- write_vmlinux_export_c_file(mod);
- exit(0);
+ /* A Wasm object is read as an ELF image of it (wasm.c). */
+ if (wasm_is_object(hdr, info->size)) {
+ size_t size;
+ void *elf = wasm_to_elf(filename, hdr, info->size, &size);
+
+ release_file(hdr, info->size);
+ hdr = elf;
+ info->size = size;
+ info->is_wasm = true;
}
info->hdr = hdr;
@@ -601,7 +601,10 @@ static int parse_elf(struct elf_info *info, const char *filename)
static void parse_elf_finish(struct elf_info *info)
{
symsearch_finish(info);
- release_file(info->hdr, info->size);
+ if (info->is_wasm)
+ free(info->hdr);
+ else
+ release_file(info->hdr, info->size);
}
static int ignore_undef_symbol(struct elf_info *info, const char *symname)
@@ -1620,6 +1623,12 @@ static void read_symbols(const char *modname)
/* strip trailing .o */
mod = new_module(modname, strlen(modname) - strlen(".o"));
+ if (info.is_wasm) {
+ mod->is_wasm = true;
+ for (unsigned int i = 1; i < info.num_sections; i++)
+ if (!strcmp(sec_name(&info, i), "__param"))
+ mod->wasm_has_param = true;
+ }
/* save .no_trim_symbol section for later use */
if (info.no_trim_symbol_len) {
@@ -1871,6 +1880,41 @@ static void check_modname_len(struct module *mod)
mod_error(mod, "module name is too long\n");
}
+/*
+ * On Wasm, THIS_MODULE is imported and the loader reads where the rest is
+ * from __kw_module (struct kw_module_info, arch/wasm/include/asm/module.h).
+ */
+static void add_header_wasm(struct buffer *b, struct module *mod)
+{
+ bool exports = !list_empty(&mod->exported_symbols);
+
+ buf_printf(b, "/*\n"
+ " * Wasm: THIS_MODULE is the loader's struct module, which this\n"
+ " * module imports; the loader reads the rest from __kw_module\n"
+ " * (asm/module.h).\n"
+ " */\n");
+ if (mod->wasm_has_param)
+ buf_printf(b, "extern const struct kernel_param __start___param[] __kw_hidden,\n"
+ "\t__stop___param[] __kw_hidden;\n");
+ if (exports)
+ buf_printf(b, "extern const struct kw_ksym __start___kw_ksymtab[] __kw_hidden,\n"
+ "\t__stop___kw_ksymtab[] __kw_hidden;\n");
+ buf_printf(b, "\n__visible const struct kw_module_info __kw_module __kw_default = {\n");
+ if (mod->has_init)
+ buf_printf(b, "\t.init = init_module,\n");
+ if (mod->has_cleanup)
+ buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
+ "\t.exit = cleanup_module,\n"
+ "#endif\n");
+ if (mod->wasm_has_param)
+ buf_printf(b, "\t.kp = __start___param,\n"
+ "\t.kp_end = __stop___param,\n");
+ if (exports)
+ buf_printf(b, "\t.ksyms = __start___kw_ksymtab,\n"
+ "\t.ksyms_end = __stop___kw_ksymtab,\n");
+ buf_printf(b, "};\n");
+}
+
/**
* Header for the generated file
**/
@@ -1882,6 +1926,10 @@ static void add_header(struct buffer *b, struct module *mod)
buf_printf(b, "\n");
buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
buf_printf(b, "\n");
+ if (mod->is_wasm) {
+ add_header_wasm(b, mod);
+ goto info;
+ }
buf_printf(b, "__visible struct module __this_module\n");
buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
@@ -1894,6 +1942,7 @@ static void add_header(struct buffer *b, struct module *mod)
buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
buf_printf(b, "};\n");
+info:
if (!external_module)
buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
@@ -1908,6 +1957,10 @@ static void add_exported_symbols(struct buffer *buf, struct module *mod)
{
struct symbol *sym;
+ /* On Wasm, EXPORT_SYMBOL emits the ksymtab (linux/export.h). */
+ if (mod->is_wasm)
+ return;
+
/* generate struct for exported symbols */
buf_printf(buf, "\n");
list_for_each_entry(sym, &mod->exported_symbols, list) {
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -126,6 +126,8 @@ struct module {
bool seen;
bool has_init;
bool has_cleanup;
+ bool is_wasm; /* read from a Wasm object (wasm.c) */
+ bool wasm_has_param; /* a Wasm object with __param */
char srcversion[25];
// Missing namespace dependencies
struct list_head missing_namespaces;
@@ -139,6 +141,7 @@ struct module {
struct elf_info {
size_t size;
+ bool is_wasm; /* hdr is wasm_to_elf()'s image */
Elf_Ehdr *hdr;
Elf_Shdr *sechdrs;
Elf_Sym *symtab_start;
diff --git a/scripts/mod/wasm.c b/scripts/mod/wasm.c
new file mode 100644
--- /dev/null
+++ b/scripts/mod/wasm.c
@@ -0,0 +1,720 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Wasm relocatable objects for modpost.
+ *
+ * clang --target=wasm32/wasm64 -c and wasm-ld -r write WebAssembly modules
+ * with the "linking" and "reloc.*" custom sections of the WebAssembly tool
+ * conventions (Linking.md) instead of ELF. wasm_to_elf() reads one and
+ * builds an ELF relocatable image of what modpost reads from an object:
+ *
+ * - one section per data segment name (".modinfo", ".export_symbol",
+ * "__param", ...), its segments laid out in order at their alignment;
+ * ".bss*" ones are SHT_NOBITS;
+ * - a ".text" section that every defined function symbol is in;
+ * - the function and data symbols, defined (in those sections, at their
+ * offsets) and undefined; globals, tables and tags are left out, as
+ * they are not linked through the kernel's symbol tables;
+ * - a ".rela.export_symbol" section with the data relocations that point
+ * into ".export_symbol", which is how modpost finds what an
+ * EXPORT_SYMBOL record exports (no other relocations, so there are no
+ * section mismatch checks).
+ *
+ * The image is in the host's byte order, so modpost's TO_NATIVE() leaves
+ * it alone, and of the class KERNEL_ELFCLASS (mk_elfconfig).
+ */
+
+#include <stdint.h>
+#include <xalloc.h>
+#include "modpost.h"
+#include "wasm.h"
+
+#define WASM_SEC_CUSTOM 0
+#define WASM_SEC_IMPORT 2
+#define WASM_SEC_DATA 11
+
+#define WASM_EXT_FUNCTION 0
+#define WASM_EXT_TABLE 1
+#define WASM_EXT_MEMORY 2
+#define WASM_EXT_GLOBAL 3
+#define WASM_EXT_TAG 4
+
+/* Subsections of the linking section. */
+#define WASM_SEGMENT_INFO 5
+#define WASM_SYMBOL_TABLE 8
+
+#define WASM_SYM_FUNCTION 0
+#define WASM_SYM_DATA 1
+#define WASM_SYM_GLOBAL 2
+#define WASM_SYM_SECTION 3
+#define WASM_SYM_TAG 4
+#define WASM_SYM_TABLE 5
+
+#define WASM_SYM_BINDING_WEAK 0x1
+#define WASM_SYM_BINDING_LOCAL 0x2
+#define WASM_SYM_VISIBILITY_HIDDEN 0x4
+#define WASM_SYM_UNDEFINED 0x10
+#define WASM_SYM_EXPLICIT_NAME 0x40
+#define WASM_SYM_ABSOLUTE 0x200
+
+struct wreader {
+ const unsigned char *p, *end;
+ const char *file;
+};
+
+struct wseg {
+ const char *name;
+ unsigned int name_len;
+ unsigned int align; /* log2 */
+ const unsigned char *data;
+ uint64_t size;
+ uint64_t secoff; /* of its bytes in the data section's payload */
+ unsigned int shndx; /* ELF section */
+ uint64_t base; /* offset in that section */
+};
+
+struct wsym {
+ unsigned int kind, flags;
+ const char *name;
+ unsigned int name_len;
+ uint64_t index; /* function: index; data: segment */
+ uint64_t offset, size;
+ unsigned int elf_index; /* 0: not in the ELF symbol table */
+};
+
+struct wreloc {
+ unsigned int type;
+ uint64_t offset;
+ unsigned int index;
+ int64_t addend;
+};
+
+struct wsec {
+ const char *name;
+ unsigned int name_len;
+ unsigned int sh_name, type, flags, link, info;
+ uint64_t align, size, offset, entsize;
+};
+
+struct wfile {
+ /* function imports: the field name of each, in import order */
+ const char **fimp_name;
+ unsigned int *fimp_len;
+ bool *fimp_host; /* not from "env": the host's own */
+ unsigned int nfimp;
+ struct wseg *seg;
+ unsigned int nseg;
+ struct wsym *sym;
+ unsigned int nsym;
+ struct wreloc *rel;
+ unsigned int nrel;
+ int data_secidx; /* index of the data section */
+};
+
+/* A growable byte buffer (modpost's struct buffer is for text). */
+struct bytes {
+ char *p;
+ size_t len, cap;
+};
+
+static size_t bytes_add(struct bytes *b, const void *s, size_t len)
+{
+ size_t off = b->len;
+
+ if (b->cap - b->len < len) {
+ b->cap = (b->cap + len) * 2;
+ b->p = xrealloc(b->p, b->cap);
+ }
+ memcpy(b->p + b->len, s, len);
+ b->len += len;
+ return off;
+}
+
+static unsigned int add_str(struct bytes *b, const char *s, unsigned int len)
+{
+ size_t off = bytes_add(b, s, len);
+
+ bytes_add(b, "", 1);
+ return off;
+}
+
+static void __attribute__((noreturn)) wfatal(struct wreader *r, const char *what)
+{
+ fatal("%s: malformed Wasm object (%s)\n", r->file, what);
+}
+
+static uint64_t uleb(struct wreader *r)
+{
+ uint64_t v = 0;
+ unsigned int shift = 0;
+ unsigned char b;
+
+ do {
+ if (r->p >= r->end || shift > 63)
+ wfatal(r, "LEB128");
+ b = *r->p++;
+ v |= (uint64_t)(b & 0x7f) << shift;
+ shift += 7;
+ } while (b & 0x80);
+ return v;
+}
+
+static int64_t sleb(struct wreader *r)
+{
+ uint64_t v = 0;
+ unsigned int shift = 0;
+ unsigned char b;
+
+ do {
+ if (r->p >= r->end || shift > 63)
+ wfatal(r, "LEB128");
+ b = *r->p++;
+ v |= (uint64_t)(b & 0x7f) << shift;
+ shift += 7;
+ } while (b & 0x80);
+ if (shift < 64 && (b & 0x40))
+ v |= ~(uint64_t)0 << shift;
+ return (int64_t)v;
+}
+
+static unsigned char byte(struct wreader *r)
+{
+ if (r->p >= r->end)
+ wfatal(r, "truncated");
+ return *r->p++;
+}
+
+static const char *wstr(struct wreader *r, unsigned int *len)
+{
+ uint64_t n = uleb(r);
+ const char *s = (const char *)r->p;
+
+ if (n > (uint64_t)(r->end - r->p))
+ wfatal(r, "string");
+ r->p += n;
+ *len = n;
+ return s;
+}
+
+/* Skip the constant expression of an active data segment. */
+static void skip_init_expr(struct wreader *r)
+{
+ for (;;) {
+ switch (byte(r)) {
+ case 0x0b: /* end */
+ return;
+ case 0x41: /* i32.const */
+ case 0x42: /* i64.const */
+ sleb(r);
+ break;
+ case 0x23: /* global.get */
+ uleb(r);
+ break;
+ default:
+ wfatal(r, "init expression");
+ }
+ }
+}
+
+static bool name_is(const char *s, unsigned int len, const char *lit)
+{
+ return len == strlen(lit) && !memcmp(s, lit, len);
+}
+
+static void read_imports(struct wfile *w, struct wreader *r)
+{
+ uint64_t n = uleb(r), i;
+
+ w->fimp_name = xmalloc(n * sizeof(*w->fimp_name) + 1);
+ w->fimp_len = xmalloc(n * sizeof(*w->fimp_len) + 1);
+ w->fimp_host = xmalloc(n * sizeof(*w->fimp_host) + 1);
+ for (i = 0; i < n; i++) {
+ unsigned int ml, fl;
+ const char *field, *module;
+ unsigned char flags;
+
+ module = wstr(r, &ml);
+ field = wstr(r, &fl);
+ switch (byte(r)) {
+ case WASM_EXT_FUNCTION:
+ uleb(r);
+ w->fimp_host[w->nfimp] = !name_is(module, ml, "env");
+ w->fimp_name[w->nfimp] = field;
+ w->fimp_len[w->nfimp++] = fl;
+ break;
+ case WASM_EXT_TABLE:
+ byte(r); /* reftype */
+ flags = byte(r);
+ uleb(r);
+ if (flags & 1)
+ uleb(r);
+ break;
+ case WASM_EXT_MEMORY:
+ flags = byte(r);
+ uleb(r);
+ if (flags & 1)
+ uleb(r);
+ break;
+ case WASM_EXT_GLOBAL:
+ byte(r); /* valtype */
+ byte(r); /* mutability */
+ break;
+ case WASM_EXT_TAG:
+ byte(r);
+ uleb(r);
+ break;
+ default:
+ wfatal(r, "import kind");
+ }
+ }
+}
+
+static void read_data(struct wfile *w, struct wreader *r)
+{
+ const unsigned char *payload = r->p;
+ uint64_t n = uleb(r), i;
+
+ if (n != w->nseg)
+ wfatal(r, "data segments do not match the segment info");
+ for (i = 0; i < n; i++) {
+ uint64_t flags = uleb(r);
+
+ if (flags == 2)
+ uleb(r); /* memory index */
+ if (!(flags & 1))
+ skip_init_expr(r);
+ w->seg[i].size = uleb(r);
+ if (w->seg[i].size > (uint64_t)(r->end - r->p))
+ wfatal(r, "data segment size");
+ w->seg[i].data = r->p;
+ w->seg[i].secoff = r->p - payload;
+ r->p += w->seg[i].size;
+ }
+}
+
+static void read_symbol(struct wreader *r, struct wsym *s)
+{
+ s->kind = byte(r);
+ s->flags = uleb(r);
+ switch (s->kind) {
+ case WASM_SYM_FUNCTION:
+ case WASM_SYM_GLOBAL:
+ case WASM_SYM_TAG:
+ case WASM_SYM_TABLE:
+ s->index = uleb(r);
+ if (!(s->flags & WASM_SYM_UNDEFINED) ||
+ (s->flags & WASM_SYM_EXPLICIT_NAME))
+ s->name = wstr(r, &s->name_len);
+ break;
+ case WASM_SYM_DATA:
+ s->name = wstr(r, &s->name_len);
+ if (!(s->flags & WASM_SYM_UNDEFINED)) {
+ s->index = uleb(r);
+ s->offset = uleb(r);
+ s->size = uleb(r);
+ }
+ break;
+ case WASM_SYM_SECTION:
+ s->index = uleb(r);
+ break;
+ default:
+ wfatal(r, "symbol kind");
+ }
+}
+
+static void read_linking(struct wfile *w, struct wreader *r)
+{
+ if (uleb(r) != 2)
+ wfatal(r, "linking section version");
+ while (r->p < r->end) {
+ unsigned int type = byte(r);
+ uint64_t len = uleb(r), i, n;
+ struct wreader sub = { r->p, r->p + len, r->file };
+
+ if (len > (uint64_t)(r->end - r->p))
+ wfatal(r, "linking subsection");
+ r->p += len;
+ if (type == WASM_SEGMENT_INFO) {
+ n = uleb(&sub);
+ w->seg = xmalloc(n * sizeof(*w->seg) + 1);
+ memset(w->seg, 0, n * sizeof(*w->seg));
+ w->nseg = n;
+ for (i = 0; i < n; i++) {
+ w->seg[i].name = wstr(&sub, &w->seg[i].name_len);
+ w->seg[i].align = uleb(&sub);
+ uleb(&sub); /* flags */
+ }
+ } else if (type == WASM_SYMBOL_TABLE) {
+ n = uleb(&sub);
+ w->sym = xmalloc(n * sizeof(*w->sym) + 1);
+ memset(w->sym, 0, n * sizeof(*w->sym));
+ w->nsym = n;
+ for (i = 0; i < n; i++)
+ read_symbol(&sub, &w->sym[i]);
+ }
+ }
+}
+
+static void read_relocs(struct wfile *w, struct wreader *r)
+{
+ uint64_t n, i;
+
+ if ((int)uleb(r) != w->data_secidx)
+ return;
+ n = uleb(r);
+ w->rel = xmalloc(n * sizeof(*w->rel) + 1);
+ w->nrel = n;
+ for (i = 0; i < n; i++) {
+ struct wreloc *x = &w->rel[i];
+
+ x->type = byte(r);
+ x->offset = uleb(r);
+ x->index = uleb(r);
+ x->addend = 0;
+ switch (x->type) {
+ case 3: case 4: case 5: /* MEMORY_ADDR_LEB, _SLEB, _I32 */
+ case 8: case 9: /* FUNCTION_OFFSET_I32, SECTION_OFFSET_I32 */
+ case 11: /* MEMORY_ADDR_REL_SLEB */
+ case 14: case 15: case 16: case 17: /* MEMORY_ADDR_LEB64, _SLEB64, _I64, _REL_SLEB64 */
+ case 21: case 22: case 23: /* MEMORY_ADDR_TLS_SLEB, FUNCTION_OFFSET_I64, _LOCREL_I32 */
+ case 25: /* MEMORY_ADDR_TLS_SLEB64 */
+ x->addend = sleb(r);
+ break;
+ }
+ }
+}
+
+bool wasm_is_object(const void *buf, size_t size)
+{
+ return size >= 8 && !memcmp(buf, "\0asm\1\0\0\0", 8);
+}
+
+
+static bool name_starts(const char *s, unsigned int len, const char *lit)
+{
+ return len >= strlen(lit) && !memcmp(s, lit, strlen(lit));
+}
+
+static uint32_t name_hash(const char *s, unsigned int len)
+{
+ uint32_t h = 2166136261u;
+
+ while (len--)
+ h = (h ^ (unsigned char)*s++) * 16777619u;
+ return h;
+}
+
+/* The segment that holds data section payload offset @off, or NULL. */
+static struct wseg *seg_at(struct wfile *w, uint64_t off)
+{
+ unsigned int lo = 0, hi = w->nseg;
+
+ while (lo < hi) {
+ unsigned int mid = lo + (hi - lo) / 2;
+ struct wseg *s = &w->seg[mid];
+
+ if (off < s->secoff)
+ hi = mid;
+ else if (off >= s->secoff + s->size)
+ lo = mid + 1;
+ else
+ return s;
+ }
+ return NULL;
+}
+
+void *wasm_to_elf(const char *filename, const void *buf, size_t size,
+ size_t *elf_size)
+{
+ struct wfile w = { .data_secidx = -1 };
+ struct wreader r = { (const unsigned char *)buf + 8,
+ (const unsigned char *)buf + size, filename };
+ struct wreader data = { NULL, NULL, filename };
+ struct bytes shstr = { }, str = { };
+ struct wsec *sec;
+ unsigned int nsec, i, nelfsym, nlocal = 1, nrela = 0, *slot, nslot;
+ unsigned int export_sec = 0, rela_sec = 0, symtab_sec, text_sec = 1;
+ Elf_Sym *syms;
+ Elf_Rela *rela;
+ Elf_Ehdr *eh;
+ Elf_Shdr *sh;
+ size_t off;
+ char *out;
+ int secidx = 0;
+
+ /* The data section is read once the linking section has been. */
+ while (r.p < r.end) {
+ unsigned int id = byte(&r);
+ uint64_t len = uleb(&r);
+ struct wreader s = { r.p, r.p + len, filename };
+
+ if (len > (uint64_t)(r.end - r.p))
+ wfatal(&r, "section size");
+ r.p += len;
+ if (id == WASM_SEC_IMPORT) {
+ read_imports(&w, &s);
+ } else if (id == WASM_SEC_DATA) {
+ data = s;
+ w.data_secidx = secidx;
+ } else if (id == WASM_SEC_CUSTOM) {
+ unsigned int nl;
+ const char *name = wstr(&s, &nl);
+
+ if (name_is(name, nl, "linking"))
+ read_linking(&w, &s);
+ else if (name_is(name, nl, "reloc.DATA"))
+ read_relocs(&w, &s);
+ }
+ secidx++;
+ }
+ if (data.p)
+ read_data(&w, &data);
+ else if (w.nseg)
+ wfatal(&r, "segment info without a data section");
+
+ /*
+ * Sections: [0] null, [1] .text, one per segment name, then
+ * .rela.export_symbol, .symtab, .strtab and .shstrtab.
+ */
+ sec = xmalloc((w.nseg + 6) * sizeof(*sec));
+ memset(sec, 0, (w.nseg + 6) * sizeof(*sec));
+ nslot = 1;
+ while (nslot < 2 * w.nseg + 2)
+ nslot <<= 1;
+ slot = xmalloc(nslot * sizeof(*slot));
+ memset(slot, 0, nslot * sizeof(*slot));
+ add_str(&shstr, "", 0);
+ sec[text_sec].sh_name = add_str(&shstr, ".text", 5);
+ sec[text_sec].type = SHT_PROGBITS;
+ sec[text_sec].flags = SHF_ALLOC | SHF_EXECINSTR;
+ sec[text_sec].align = 1;
+ nsec = 2;
+ for (i = 0; i < w.nseg; i++) {
+ struct wseg *s = &w.seg[i];
+ uint64_t a = (uint64_t)1 << s->align;
+ struct wsec *x;
+ uint32_t h;
+
+ /*
+ * Every variable has a segment of its own (.data.NAME), as
+ * wasm-ld -r keeps them: put them together as wasm-ld does in
+ * a final link, but keep the special ones (.data..percpu).
+ */
+ static const char *const merged[] = {
+ ".data.", ".rodata.", ".bss.", ".tdata.", ".tbss."
+ };
+ for (unsigned int k = 0; k < ARRAY_SIZE(merged); k++) {
+ size_t l = strlen(merged[k]);
+
+ if (s->name_len > l && name_starts(s->name, s->name_len, merged[k]) &&
+ s->name[l] != '.') {
+ s->name_len = l - 1;
+ break;
+ }
+ }
+ h = name_hash(s->name, s->name_len) & (nslot - 1);
+
+ while (slot[h]) {
+ x = &sec[slot[h]];
+ if (x->name_len == s->name_len &&
+ !memcmp(x->name, s->name, s->name_len))
+ break;
+ h = (h + 1) & (nslot - 1);
+ }
+ if (!slot[h]) {
+ slot[h] = nsec;
+ x = &sec[nsec++];
+ x->name = s->name;
+ x->name_len = s->name_len;
+ x->sh_name = add_str(&shstr, s->name, s->name_len);
+ x->type = name_starts(s->name, s->name_len, ".bss") ?
+ SHT_NOBITS : SHT_PROGBITS;
+ x->flags = SHF_ALLOC;
+ if (x->type == SHT_NOBITS ||
+ name_starts(s->name, s->name_len, ".data"))
+ x->flags |= SHF_WRITE;
+ x->align = 1;
+ if (name_is(s->name, s->name_len, ".export_symbol"))
+ export_sec = slot[h];
+ }
+ s->shndx = slot[h];
+ x = &sec[s->shndx];
+ if (a > x->align)
+ x->align = a;
+ s->base = (x->size + a - 1) & ~(a - 1);
+ x->size = s->base + s->size;
+ }
+ free(slot);
+
+ /* Symbols: null, the local ones, the others (ELF wants this order). */
+ syms = xmalloc((w.nsym + 1) * sizeof(*syms));
+ memset(syms, 0, (w.nsym + 1) * sizeof(*syms));
+ add_str(&str, "", 0);
+ nelfsym = 1;
+ for (int pass = 0; pass < 2; pass++) {
+ for (i = 0; i < w.nsym; i++) {
+ struct wsym *s = &w.sym[i];
+ bool local = s->flags & WASM_SYM_BINDING_LOCAL;
+ const char *name = s->name;
+ unsigned int name_len = s->name_len, bind, type;
+ Elf_Sym *e;
+
+ if (s->kind != WASM_SYM_FUNCTION && s->kind != WASM_SYM_DATA)
+ continue;
+ /* A host import (unkernel.*, WASI) is no kernel symbol. */
+ if (s->kind == WASM_SYM_FUNCTION && (s->flags & WASM_SYM_UNDEFINED) &&
+ s->index < w.nfimp && w.fimp_host[s->index])
+ continue;
+ if (local != (pass == 0))
+ continue;
+ if (!name && s->index < w.nfimp) {
+ name = w.fimp_name[s->index];
+ name_len = w.fimp_len[s->index];
+ }
+ if (!name)
+ continue;
+ e = &syms[nelfsym];
+ s->elf_index = nelfsym++;
+ e->st_name = add_str(&str, name, name_len);
+ bind = local ? STB_LOCAL :
+ (s->flags & WASM_SYM_BINDING_WEAK) ? STB_WEAK : STB_GLOBAL;
+ type = s->kind == WASM_SYM_FUNCTION ? STT_FUNC : STT_OBJECT;
+ e->st_info = (bind << 4) | type;
+ e->st_other = (s->flags & WASM_SYM_VISIBILITY_HIDDEN) ?
+ STV_HIDDEN : STV_DEFAULT;
+ if (s->flags & WASM_SYM_UNDEFINED) {
+ e->st_shndx = SHN_UNDEF;
+ } else if (s->kind == WASM_SYM_FUNCTION) {
+ e->st_shndx = text_sec;
+ } else if (s->flags & WASM_SYM_ABSOLUTE) {
+ e->st_shndx = SHN_ABS;
+ e->st_value = s->offset;
+ e->st_size = s->size;
+ } else {
+ if (s->index >= w.nseg)
+ wfatal(&r, "data symbol segment");
+ e->st_shndx = w.seg[s->index].shndx;
+ e->st_value = w.seg[s->index].base + s->offset;
+ e->st_size = s->size;
+ }
+ }
+ if (pass == 0)
+ nlocal = nelfsym;
+ }
+
+ /* The relocations of .export_symbol's bytes. */
+ rela = xmalloc((w.nrel + 1) * sizeof(*rela));
+ for (i = 0; export_sec && i < w.nrel; i++) {
+ struct wreloc *x = &w.rel[i];
+ struct wseg *s = seg_at(&w, x->offset);
+
+ if (!s || s->shndx != export_sec)
+ continue;
+ if (x->index >= w.nsym || !w.sym[x->index].elf_index)
+ continue;
+ rela[nrela].r_offset = s->base + (x->offset - s->secoff);
+#if KERNEL_ELFCLASS == ELFCLASS32
+ rela[nrela].r_info = ELF32_R_INFO(w.sym[x->index].elf_index, 1);
+#else
+ rela[nrela].r_info = ELF64_R_INFO(w.sym[x->index].elf_index, 1);
+#endif
+ rela[nrela].r_addend = x->addend;
+ nrela++;
+ }
+ if (nrela) {
+ rela_sec = nsec++;
+ sec[rela_sec].sh_name = add_str(&shstr, ".rela.export_symbol", 19);
+ sec[rela_sec].type = SHT_RELA;
+ sec[rela_sec].align = 8;
+ sec[rela_sec].info = export_sec;
+ sec[rela_sec].entsize = sizeof(Elf_Rela);
+ sec[rela_sec].size = nrela * sizeof(Elf_Rela);
+ }
+ if (nsec + 3 >= SHN_LORESERVE)
+ fatal("%s: too many data segment names for modpost (%u)\n", filename, nsec);
+ symtab_sec = nsec++;
+ sec[symtab_sec].sh_name = add_str(&shstr, ".symtab", 7);
+ sec[symtab_sec].type = SHT_SYMTAB;
+ sec[symtab_sec].align = 8;
+ sec[symtab_sec].entsize = sizeof(Elf_Sym);
+ sec[symtab_sec].size = nelfsym * sizeof(Elf_Sym);
+ sec[symtab_sec].info = nlocal;
+ sec[symtab_sec].link = nsec;
+ if (rela_sec)
+ sec[rela_sec].link = symtab_sec;
+ sec[nsec].sh_name = add_str(&shstr, ".strtab", 7);
+ sec[nsec].type = SHT_STRTAB;
+ sec[nsec].align = 1;
+ sec[nsec].size = str.len;
+ nsec++;
+ sec[nsec].sh_name = add_str(&shstr, ".shstrtab", 9);
+ sec[nsec].type = SHT_STRTAB;
+ sec[nsec].align = 1;
+ sec[nsec].size = shstr.len;
+ nsec++;
+
+ /* File layout: header, the sections in order, section headers. */
+ off = sizeof(Elf_Ehdr);
+ for (i = 1; i < nsec; i++) {
+ uint64_t a = sec[i].align > 8 ? sec[i].align : 8;
+
+ off = (off + a - 1) & ~(a - 1);
+ sec[i].offset = off;
+ if (sec[i].type != SHT_NOBITS)
+ off += sec[i].size;
+ }
+ off = (off + 7) & ~(size_t)7;
+ *elf_size = off + nsec * sizeof(Elf_Shdr);
+ out = xmalloc(*elf_size);
+ memset(out, 0, *elf_size);
+
+ for (i = 0; i < w.nseg; i++) {
+ struct wseg *s = &w.seg[i];
+
+ if (sec[s->shndx].type != SHT_NOBITS)
+ memcpy(out + sec[s->shndx].offset + s->base, s->data, s->size);
+ }
+ if (rela_sec)
+ memcpy(out + sec[rela_sec].offset, rela, sec[rela_sec].size);
+ memcpy(out + sec[symtab_sec].offset, syms, sec[symtab_sec].size);
+ memcpy(out + sec[nsec - 2].offset, str.p, str.len);
+ memcpy(out + sec[nsec - 1].offset, shstr.p, shstr.len);
+
+ eh = (Elf_Ehdr *)out;
+ memcpy(eh->e_ident, ELFMAG, SELFMAG);
+ eh->e_ident[EI_CLASS] = KERNEL_ELFCLASS;
+ eh->e_ident[EI_DATA] = host_is_big_endian ? ELFDATA2MSB : ELFDATA2LSB;
+ eh->e_ident[EI_VERSION] = EV_CURRENT;
+ eh->e_type = ET_REL;
+ eh->e_machine = EM_NONE;
+ eh->e_version = EV_CURRENT;
+ eh->e_ehsize = sizeof(Elf_Ehdr);
+ eh->e_shentsize = sizeof(Elf_Shdr);
+ eh->e_shnum = nsec;
+ eh->e_shstrndx = nsec - 1;
+ eh->e_shoff = off;
+ sh = (Elf_Shdr *)(out + off);
+ for (i = 1; i < nsec; i++) {
+ sh[i].sh_name = sec[i].sh_name;
+ sh[i].sh_type = sec[i].type;
+ sh[i].sh_flags = sec[i].flags;
+ sh[i].sh_offset = sec[i].offset;
+ sh[i].sh_size = sec[i].size;
+ sh[i].sh_link = sec[i].link;
+ sh[i].sh_info = sec[i].info;
+ sh[i].sh_addralign = sec[i].align;
+ sh[i].sh_entsize = sec[i].entsize;
+ }
+
+ free(str.p);
+ free(shstr.p);
+ free(syms);
+ free(rela);
+ free(sec);
+ free(w.fimp_name);
+ free(w.fimp_len);
+ free(w.fimp_host);
+ free(w.seg);
+ free(w.sym);
+ free(w.rel);
+ return out;
+}
diff --git a/scripts/mod/wasm.h b/scripts/mod/wasm.h
new file mode 100644
--- /dev/null
+++ b/scripts/mod/wasm.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Wasm relocatable objects as ELF for modpost (wasm.c). */
+#ifndef _MODPOST_WASM_H
+#define _MODPOST_WASM_H
+
+#include <stdbool.h>
+#include <stddef.h>
+
+bool wasm_is_object(const void *buf, size_t size);
+/* An ELF image of the Wasm object @buf (xmalloc'd; free() it). */
+void *wasm_to_elf(const char *filename, const void *buf, size_t size,
+ size_t *elf_size);
+
+#endif