Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

resolve_btfids: Introduce finalize_btf() step

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>

authored by

Ihor Solodrai and committed by
Alexei Starovoitov
2583e81f 64e13605

+48 -21
+48 -21
tools/bpf/resolve_btfids/main.c
··· 563 563 obj->base_btf = base_btf; 564 564 obj->btf = btf; 565 565 566 - if (obj->base_btf && obj->distill_base) { 567 - err = btf__distill_base(obj->btf, &base_btf, &btf); 568 - if (err) { 569 - pr_err("FAILED to distill base BTF: %s\n", strerror(errno)); 570 - goto out_err; 571 - } 572 - 573 - btf__free(obj->base_btf); 574 - btf__free(obj->btf); 575 - obj->base_btf = base_btf; 576 - obj->btf = btf; 577 - } 578 - 579 566 return 0; 580 567 581 568 out_err: ··· 898 911 return err; 899 912 } 900 913 914 + static int finalize_btf(struct object *obj) 915 + { 916 + struct btf *base_btf = obj->base_btf, *btf = obj->btf; 917 + int err; 918 + 919 + if (obj->base_btf && obj->distill_base) { 920 + err = btf__distill_base(obj->btf, &base_btf, &btf); 921 + if (err) { 922 + pr_err("FAILED to distill base BTF: %s\n", strerror(errno)); 923 + goto out_err; 924 + } 925 + 926 + btf__free(obj->base_btf); 927 + btf__free(obj->btf); 928 + obj->base_btf = base_btf; 929 + obj->btf = btf; 930 + } 931 + 932 + err = sort_btf_by_name(obj->btf); 933 + if (err) { 934 + pr_err("FAILED to sort BTF: %s\n", strerror(errno)); 935 + goto out_err; 936 + } 937 + 938 + return 0; 939 + 940 + out_err: 941 + btf__free(base_btf); 942 + btf__free(btf); 943 + obj->base_btf = NULL; 944 + obj->btf = NULL; 945 + 946 + return err; 947 + } 948 + 901 949 static inline int make_out_path(char *buf, u32 buf_sz, const char *in_path, const char *suffix) 902 950 { 903 951 int len = snprintf(buf, buf_sz, "%s%s", in_path, suffix); ··· 1076 1054 }; 1077 1055 const char *btfids_path = NULL; 1078 1056 bool fatal_warnings = false; 1057 + bool resolve_btfids = true; 1079 1058 char out_path[PATH_MAX]; 1080 1059 1081 1060 struct option btfid_options[] = { ··· 1106 1083 if (btfids_path) 1107 1084 return patch_btfids(btfids_path, obj.path); 1108 1085 1109 - if (load_btf(&obj)) 1110 - goto out; 1111 - 1112 - if (sort_btf_by_name(obj.btf)) 1113 - goto out; 1114 - 1115 1086 if (elf_collect(&obj)) 1116 1087 goto out; 1117 1088 ··· 1116 1099 if (obj.efile.idlist_shndx == -1 || 1117 1100 obj.efile.symbols_shndx == -1) { 1118 1101 pr_debug("Cannot find .BTF_ids or symbols sections, skip symbols resolution\n"); 1119 - goto dump_btf; 1102 + resolve_btfids = false; 1120 1103 } 1121 1104 1122 - if (symbols_collect(&obj)) 1105 + if (resolve_btfids) 1106 + if (symbols_collect(&obj)) 1107 + goto out; 1108 + 1109 + if (load_btf(&obj)) 1123 1110 goto out; 1111 + 1112 + if (finalize_btf(&obj)) 1113 + goto out; 1114 + 1115 + if (!resolve_btfids) 1116 + goto dump_btf; 1124 1117 1125 1118 if (symbols_resolve(&obj)) 1126 1119 goto out;