From: Alan Maguire <alan.maguire@oracle.com>
To: "Wang, Jay" <wanjay@amazon.com>,
"bpf@vger.kernel.org" <bpf@vger.kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nicolas Schier <nsc@kernel.org>,
"linux-kbuild@vger.kernel.org" <linux-kbuild@vger.kernel.org>,
Luis Chamberlain <mcgrof@kernel.org>,
Petr Pavlu <petr.pavlu@suse.com>,
Sami Tolvanen <samitolvanen@google.com>,
"linux-modules@vger.kernel.org" <linux-modules@vger.kernel.org>,
Miguel Ojeda <ojeda@kernel.org>,
"rust-for-linux@vger.kernel.org" <rust-for-linux@vger.kernel.org>,
Arnd Bergmann <arnd@arndb.de>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"Mohamed Abuelfotoh, Hazem" <abuehaze@amazon.com>,
"Doebel, Bjoern" <doebel@amazon.de>,
"Pohlack, Martin" <mpohlack@amazon.de>,
"jay.wang.upstream@gmail.com" <jay.wang.upstream@gmail.com>
Subject: Re: [PATCH bpf-next v3 9/9] kbuild, bpf: allow building the vmlinux BTF as a module
Date: Tue, 29 Sep 2026 19:25:25 +0100 [thread overview]
Message-ID: <dbbb6cde-9889-4445-abf6-0486c380925d@oracle.com> (raw)
In-Reply-To: <DM6PR18MB2666F6B235D5B356AA157A7DA98D2@DM6PR18MB2666.namprd18.prod.outlook.com>
[-- Attachment #1: Type: text/plain, Size: 2367 bytes --]
On 28/09/2026 19:04, Wang, Jay wrote:
> Alan and Alexei, I’ll address both of your comments in V4 and provide more detailed responses later as well.
Sounds good. To expand on what I had in mind, as I've tried putting together a generic approach
that will work both for your use case - BTF vmlinux delivered as a module - and mine - inline
vmlinux BTF delivered as a module. They'd need to be separate modules as some folks might want
vmlinux BTF as a module but not want inline BTF.
The idea is close to what you had; we have a .BTF.link / .BTF.inline.link ELF section which consists of:
#define BTF_LINK_SHA256_LEN 32
/*
* A .BTF.link or .BTF.inline.link section identifies the module which
* carries a vmlinux BTF section. The module name is NUL terminated and the
* field is padded to __MODULE_NAME_LEN.
*/
struct btf_link {
char module_name[__MODULE_NAME_LEN];
u8 sha256[BTF_LINK_SHA256_LEN];
u32 btf_size;
} __packed;
These sections .BTF.link / .BTF.inline.link live in vmlinux and tell it
- the module that delivers the payload
- the SHA; and
- the size
again similar to what you had.
The link section is populated at the same time resolve_btfids populates the .BTF_ids section;
we add support to resolve_btfids for a --btf_link option of the form
--btf_link <section>:<module>:<raw-btf-file>
So in your case it will be
--btf_link .BTF:btf_vmlinux:vmlinux.BTF
and in mine
--btf_link .BTF.inline:btf_vmlinux_inline:vmlinux.BTF.inline
The attached patch adds resolve_btfids support for this to give an idea of the approach,
and at the final vmlinux linking where we patch BTF ids, we would add the above options.
resolve_btfids is written to support multiple --btf_link options, so whichever work lands
first could add the support that the other series uses. Hope this helps,
Alan
[-- Attachment #2: 0002-resolve_btfids-add-generic-BTF-link-section-patching.patch --]
[-- Type: text/x-patch, Size: 8262 bytes --]
From 9cb9d0e0bd348a03960e0d7f7fb6ef1a76166d56 Mon Sep 17 00:00:00 2001
From: Alan Maguire <alan.maguire@oracle.com>
Date: Tue, 29 Sep 2026 18:50:14 +0100
Subject: [PATCH v3 bpf-next 2/4] resolve_btfids: Populate BTF link section
Allow the final BTF patch pass to populate one or more BTF link
records. A link identifies the BTF section to be delivered, its
carrier module, and the raw finalized BTF payload:
--btf_link <section>:<module>:<raw-btf-file>
Use the raw file size and SHA-256 digest to populate the corresponding
<section>.link record. This avoids reparsing BTF during final ELF
patching and supports both inline BTF and a future module-delivered
vmlinux BTF.
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Assisted-by: OpenAI Codex (GPT 5.6)
---
tools/bpf/resolve_btfids/main.c | 187 +++++++++++++++++++++++++++++++-
1 file changed, 183 insertions(+), 4 deletions(-)
diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
index 571f63c4cb82..3baa01f14f33 100644
--- a/tools/bpf/resolve_btfids/main.c
+++ b/tools/bpf/resolve_btfids/main.c
@@ -93,8 +93,13 @@
#include <subcmd/parse-options.h>
#define BTF_IDS_SECTION ".BTF_ids"
+#define BTF_LINK_MODULE_NAME_MAX 64
+#define LIBBPF_SHA256_DIGEST_LENGTH 32
#define BTF_ID_PREFIX "__BTF_ID__"
+void libbpf_sha256(const void *data, size_t len,
+ __u8 out[LIBBPF_SHA256_DIGEST_LENGTH]);
+
#define BTF_STRUCT "struct"
#define BTF_UNION "union"
#define BTF_TYPEDEF "typedef"
@@ -157,6 +162,7 @@ struct object {
size_t strtabidx;
unsigned long idlist_addr;
int encoding;
+ int elf_class;
const char **func_symbols;
u32 func_symbols_cnt;
u32 func_symbols_cap;
@@ -179,6 +185,19 @@ struct object {
u32 addr_syms_cap;
};
+struct btf_link {
+ char *value;
+ char *section;
+ char *module;
+ char *btf_path;
+};
+
+struct btf_links {
+ struct btf_link *links;
+ u32 cnt;
+ u32 cap;
+};
+
#define DECL_TAG_FASTCALL "bpf_fastcall"
#define DECL_TAG_KFUNC "bpf_kfunc"
@@ -266,6 +285,49 @@ static int __ensure_mem(void **data, u32 *cap, u32 cnt, size_t elem_sz)
#define ensure_mem(arr_ptr, cap_ptr, cnt) \
__ensure_mem((void **)(arr_ptr), (cap_ptr), (cnt), sizeof(**(arr_ptr)))
+static int parse_btf_link(const struct option *opt, const char *arg, int unset)
+{
+ struct btf_links *links = opt->value;
+ struct btf_link *link;
+ char *separator;
+
+ if (unset)
+ return -EINVAL;
+ if (__ensure_mem((void **)&links->links, &links->cap, links->cnt + 1,
+ sizeof(*links->links)))
+ return -ENOMEM;
+ link = &links->links[links->cnt];
+ memset(link, 0, sizeof(*link));
+ link->value = strdup(arg);
+ if (!link->value)
+ return -ENOMEM;
+ link->section = link->value;
+ separator = strchr(link->section, ':');
+ if (!separator || separator == link->section)
+ goto err_value;
+ *separator++ = '\0';
+ link->module = separator;
+ separator = strchr(link->module, ':');
+ if (!separator || separator == link->module || !separator[1])
+ goto err_value;
+ *separator++ = '\0';
+ link->btf_path = separator;
+ links->cnt++;
+ return 0;
+err_value:
+ free(link->value);
+ return -EINVAL;
+}
+
+static void free_btf_links(struct btf_links *links)
+{
+ u32 i;
+
+ for (i = 0; i < links->cnt; i++)
+ free(links->links[i].value);
+ free(links->links);
+}
+
static bool is_btf_id(const char *name)
{
return name && !strncmp(name, BTF_ID_PREFIX, sizeof(BTF_ID_PREFIX) - 1);
@@ -487,6 +549,7 @@ static int elf_collect(struct object *obj)
return -1;
}
obj->efile.encoding = ehdr.e_ident[EI_DATA];
+ obj->efile.elf_class = ehdr.e_ident[EI_CLASS];
/*
* Scan all the elf sections and look for save data
@@ -1036,6 +1099,106 @@ static int dump_raw_btf(struct btf *btf, const char *out_path)
return 0;
}
+static int patch_btf_link(struct object *obj, const struct btf_link *link)
+{
+ void *raw_btf_data;
+ Elf_Scn *scn = NULL;
+ Elf_Data *data;
+ GElf_Shdr sh;
+ char section[128];
+ char *name;
+ size_t shdrstrndx, module_name_len;
+ int fd, err = -1;
+ struct stat st;
+ FILE *btf_file;
+ u32 raw_btf_size;
+ Elf *elf;
+ u8 *size;
+
+ if (stat(link->btf_path, &st) || st.st_size > UINT_MAX)
+ return -EINVAL;
+ raw_btf_size = st.st_size;
+ raw_btf_data = malloc(raw_btf_size);
+ if (!raw_btf_data)
+ return -ENOMEM;
+ btf_file = fopen(link->btf_path, "rb");
+ if (!btf_file)
+ goto out_data;
+ if (fread(raw_btf_data, raw_btf_size, 1, btf_file) != 1)
+ goto out_file;
+ fclose(btf_file);
+ if (obj->efile.elf_class == ELFCLASS32)
+ module_name_len = BTF_LINK_MODULE_NAME_MAX - 4;
+ else if (obj->efile.elf_class == ELFCLASS64)
+ module_name_len = BTF_LINK_MODULE_NAME_MAX - 8;
+ else
+ goto out_data;
+ if (strlen(link->module) >= module_name_len)
+ goto out_data;
+
+ snprintf(section, sizeof(section), "%s.link", link->section);
+ fd = open(obj->path, O_RDWR);
+ if (fd < 0)
+ goto out_data;
+ elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);
+ if (!elf) {
+ err = -EINVAL;
+ goto out_close;
+ }
+ elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
+ if (elf_getshdrstrndx(elf, &shdrstrndx))
+ goto out_elf;
+ while ((scn = elf_nextscn(elf, scn))) {
+ if (gelf_getshdr(scn, &sh) != &sh)
+ goto out_elf;
+ name = elf_strptr(elf, shdrstrndx, sh.sh_name);
+ if (name && !strcmp(name, section))
+ break;
+ }
+ if (!scn) {
+ pr_err("FAILED: section %s not found in %s\n", section, obj->path);
+ goto out_elf;
+ }
+ data = elf_getdata(scn, NULL);
+ if (!data || data->d_size != module_name_len +
+ LIBBPF_SHA256_DIGEST_LENGTH + sizeof(u32)) {
+ pr_err("FAILED: section %s has unexpected size\n", section);
+ goto out_elf;
+ }
+ memset(data->d_buf, 0, data->d_size);
+ memcpy(data->d_buf, link->module, strlen(link->module) + 1);
+ libbpf_sha256(raw_btf_data, raw_btf_size,
+ (u8 *)data->d_buf + module_name_len);
+ size = (u8 *)data->d_buf + module_name_len + LIBBPF_SHA256_DIGEST_LENGTH;
+ if (obj->efile.encoding == ELFDATA2LSB) {
+ size[0] = raw_btf_size;
+ size[1] = raw_btf_size >> 8;
+ size[2] = raw_btf_size >> 16;
+ size[3] = raw_btf_size >> 24;
+ } else if (obj->efile.encoding == ELFDATA2MSB) {
+ size[0] = raw_btf_size >> 24;
+ size[1] = raw_btf_size >> 16;
+ size[2] = raw_btf_size >> 8;
+ size[3] = raw_btf_size;
+ } else {
+ goto out_elf;
+ }
+ elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
+ if (elf_update(elf, ELF_C_WRITE) >= 0)
+ err = 0;
+out_elf:
+ elf_end(elf);
+out_close:
+ close(fd);
+ free(raw_btf_data);
+ return err;
+out_file:
+ fclose(btf_file);
+out_data:
+ free(raw_btf_data);
+ return err;
+}
+
static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf, s32 type_id)
{
const struct btf_type *t = btf__type_by_id(btf, type_id);
@@ -1908,6 +2071,7 @@ int main(int argc, const char **argv)
.sets = RB_ROOT,
};
const char *btfids_path = NULL;
+ struct btf_links btf_links = {};
bool fatal_warnings = false;
bool resolve_btfids = true;
char out_path[PATH_MAX];
@@ -1925,6 +2089,8 @@ int main(int argc, const char **argv)
"distill --btf_base and emit .BTF.base section data"),
OPT_BOOLEAN(0, "inline", &obj.extract_inline,
"extract location BTF into a .BTF.inline file"),
+ OPT_CALLBACK(0, "btf_link", &btf_links, "section:module:btf-file",
+ "patch a BTF link", parse_btf_link),
OPT_STRING(0, "patch_btfids", &btfids_path, "file",
"path to .BTF_ids section data blob to patch into ELF file"),
OPT_END()
@@ -1938,9 +2104,22 @@ int main(int argc, const char **argv)
obj.path = argv[0];
- if (btfids_path)
- return patch_btfids(btfids_path, obj.path);
-
+ if (btfids_path) {
+ err = patch_btfids(btfids_path, obj.path);
+ if (err || !btf_links.cnt)
+ goto out;
+ if (elf_collect(&obj)) {
+ err = -EINVAL;
+ goto out;
+ }
+ for (u32 i = 0; i < btf_links.cnt; i++) {
+ struct btf_link *link = &btf_links.links[i];
+ err = patch_btf_link(&obj, link);
+ if (err)
+ break;
+ }
+ goto out;
+ }
if (elf_collect(&obj))
goto out;
if (obj.extract_inline && collect_func_symbols(&obj))
@@ -2001,10 +2180,10 @@ int main(int argc, const char **argv)
if (err)
goto out;
}
-
if (!(fatal_warnings && warnings))
err = 0;
out:
+ free_btf_links(&btf_links);
btf__free(obj.inline_btf);
btf__free(obj.btf);
btf__free(obj.base_btf);
--
2.43.5
prev parent reply other threads:[~2026-09-29 18:26 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-25 22:42 [PATCH bpf-next v3 0/9] bpf: make the vmlinux BTF an on-demand loadable module (CONFIG_DEBUG_INFO_BTF=m) to save ~5.4 MB memory Jay Wang
2026-09-25 22:42 ` [PATCH bpf-next v3 1/9] bpf: pass the vmlinux BTF to btf_parse_module() and let it adopt the data Jay Wang
2026-09-25 22:42 ` [PATCH bpf-next v3 2/9] bpf: split the kfunc, dtor kfunc and struct_ops registration bodies Jay Wang
2026-09-25 22:42 ` [PATCH bpf-next v3 3/9] bpf: fetch the vmlinux BTF where kernel types enter a program Jay Wang
2026-09-25 22:42 ` [PATCH bpf-next v3 4/9] bpf: take the vmlinux BTF from the btf_vmlinux module Jay Wang
2026-09-25 23:23 ` bot+bpf-ci
2026-09-26 8:29 ` Alexei Starovoitov
2026-09-25 22:42 ` [PATCH bpf-next v3 5/9] bpf: defer vmlinux kfunc and struct_ops registrations Jay Wang
2026-09-25 23:34 ` bot+bpf-ci
2026-09-25 22:42 ` [PATCH bpf-next v3 6/9] bpf: keep module BTF until the vmlinux BTF is available Jay Wang
2026-09-25 22:42 ` [PATCH bpf-next v3 7/9] bpf: expose deferred .BTF.base module BTF in sysfs from module load Jay Wang
2026-09-25 23:23 ` bot+bpf-ci
2026-09-25 22:42 ` [PATCH bpf-next v3 8/9] bpf, trace, net: prepare CONFIG_DEBUG_INFO_BTF checks for a tristate Jay Wang
2026-09-25 23:23 ` bot+bpf-ci
2026-09-25 22:42 ` [PATCH bpf-next v3 9/9] kbuild, bpf: allow building the vmlinux BTF as a module Jay Wang
2026-09-25 23:34 ` bot+bpf-ci
2026-09-28 10:00 ` Alan Maguire
[not found] ` <DM6PR18MB2666F6B235D5B356AA157A7DA98D2@DM6PR18MB2666.namprd18.prod.outlook.com>
2026-09-29 18:25 ` Alan Maguire [this message]
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=dbbb6cde-9889-4445-abf6-0486c380925d@oracle.com \
--to=alan.maguire@oracle.com \
--cc=abuehaze@amazon.com \
--cc=andrii@kernel.org \
--cc=arnd@arndb.de \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=doebel@amazon.de \
--cc=eddyz87@gmail.com \
--cc=jay.wang.upstream@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mcgrof@kernel.org \
--cc=memxor@gmail.com \
--cc=mpohlack@amazon.de \
--cc=nathan@kernel.org \
--cc=nsc@kernel.org \
--cc=ojeda@kernel.org \
--cc=petr.pavlu@suse.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=samitolvanen@google.com \
--cc=wanjay@amazon.com \
--cc=yonghong.song@linux.dev \
/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
all inboxes | Powered by JetHome®