From 9cb9d0e0bd348a03960e0d7f7fb6ef1a76166d56 Mon Sep 17 00:00:00 2001 From: Alan Maguire 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
:: Use the raw file size and SHA-256 digest to populate the corresponding
.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 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 #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