]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
resolve_btfids: Introduce finalize_btf() step
authorIhor Solodrai <ihor.solodrai@linux.dev>
Tue, 20 Jan 2026 22:26:29 +0000 (14:26 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 21 Jan 2026 00:15:56 +0000 (16:15 -0800)
Since recently [1][2] resolve_btfids executes final adjustments to the
kernel/module BTF before it's embedded into the target binary.

To keep the implementation simple, a clear and stable "pipeline" of
how BTF data flows through resolve_btfids would be helpful. Some BTF
modifications may change the ids of the types, so it is important to
maintain correct order of operations with respect to .BTF_ids
resolution too.

This patch refactors the BTF handling to establish the following
sequence:
  - load target ELF sections
  - load .BTF_ids symbols
    - this will be a dependency of btf2btf transformations in
      subsequent patches
  - load BTF and its base as is
  - (*) btf2btf transformations will happen here
  - finalize_btf(), introduced in this patch
    - does distill base and sort BTF
  - resolve and patch .BTF_ids

This approach helps to avoid fixups in .BTF_ids data in case the ids
change at any point of BTF processing, because symbol resolution
happens on the finalized, ready to dump, BTF data.

This also gives flexibility in BTF transformations, because they will
happen on BTF that is not distilled and/or sorted yet, allowing to
freely add, remove and modify BTF types.

[1] https://lore.kernel.org/bpf/20251219181321.1283664-1-ihor.solodrai@linux.dev/
[2] https://lore.kernel.org/bpf/20260109130003.3313716-1-dolinux.peng@gmail.com/

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Link: https://lore.kernel.org/r/20260120222638.3976562-5-ihor.solodrai@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/bpf/resolve_btfids/main.c

index 343d080501169adceee5e2d32987a7b8212a8ac1..1fcf37af67649f33c156847603cc979ffd571bb8 100644 (file)
@@ -563,19 +563,6 @@ static int load_btf(struct object *obj)
        obj->base_btf = base_btf;
        obj->btf = btf;
 
-       if (obj->base_btf && obj->distill_base) {
-               err = btf__distill_base(obj->btf, &base_btf, &btf);
-               if (err) {
-                       pr_err("FAILED to distill base BTF: %s\n", strerror(errno));
-                       goto out_err;
-               }
-
-               btf__free(obj->base_btf);
-               btf__free(obj->btf);
-               obj->base_btf = base_btf;
-               obj->btf = btf;
-       }
-
        return 0;
 
 out_err:
@@ -911,6 +898,41 @@ out:
        return err;
 }
 
+static int finalize_btf(struct object *obj)
+{
+       struct btf *base_btf = obj->base_btf, *btf = obj->btf;
+       int err;
+
+       if (obj->base_btf && obj->distill_base) {
+               err = btf__distill_base(obj->btf, &base_btf, &btf);
+               if (err) {
+                       pr_err("FAILED to distill base BTF: %s\n", strerror(errno));
+                       goto out_err;
+               }
+
+               btf__free(obj->base_btf);
+               btf__free(obj->btf);
+               obj->base_btf = base_btf;
+               obj->btf = btf;
+       }
+
+       err = sort_btf_by_name(obj->btf);
+       if (err) {
+               pr_err("FAILED to sort BTF: %s\n", strerror(errno));
+               goto out_err;
+       }
+
+       return 0;
+
+out_err:
+       btf__free(base_btf);
+       btf__free(btf);
+       obj->base_btf = NULL;
+       obj->btf = NULL;
+
+       return err;
+}
+
 static inline int make_out_path(char *buf, u32 buf_sz, const char *in_path, const char *suffix)
 {
        int len = snprintf(buf, buf_sz, "%s%s", in_path, suffix);
@@ -1054,6 +1076,7 @@ int main(int argc, const char **argv)
        };
        const char *btfids_path = NULL;
        bool fatal_warnings = false;
+       bool resolve_btfids = true;
        char out_path[PATH_MAX];
 
        struct option btfid_options[] = {
@@ -1083,12 +1106,6 @@ int main(int argc, const char **argv)
        if (btfids_path)
                return patch_btfids(btfids_path, obj.path);
 
-       if (load_btf(&obj))
-               goto out;
-
-       if (sort_btf_by_name(obj.btf))
-               goto out;
-
        if (elf_collect(&obj))
                goto out;
 
@@ -1099,12 +1116,22 @@ int main(int argc, const char **argv)
        if (obj.efile.idlist_shndx == -1 ||
            obj.efile.symbols_shndx == -1) {
                pr_debug("Cannot find .BTF_ids or symbols sections, skip symbols resolution\n");
-               goto dump_btf;
+               resolve_btfids = false;
        }
 
-       if (symbols_collect(&obj))
+       if (resolve_btfids)
+               if (symbols_collect(&obj))
+                       goto out;
+
+       if (load_btf(&obj))
                goto out;
 
+       if (finalize_btf(&obj))
+               goto out;
+
+       if (!resolve_btfids)
+               goto dump_btf;
+
        if (symbols_resolve(&obj))
                goto out;