From nobody Sun Feb 8 17:43:23 2026 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 946853A4AD8 for ; Fri, 16 Jan 2026 20:17:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768594664; cv=none; b=dwcZXSMZDvsGwCNhjQIRqxINOCYsztFHd5zhtp5JO9zBPq5R5FTcf5zHciCnxaY7UyR1uY7dvAx6ePumJY4g9BvtssQ7/v/Ie14bXbu1+uzMOikCLi36ghR7LDk0770PDsTU0dOHtT7uIU3p/qL9QTNp6WIlo5zHx+cyHS5ZQks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768594664; c=relaxed/simple; bh=CPAA3gY2QB9GNzfqq6/eeY2mCprs3fUSvuy03pJBlPs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Oban01dJVv8KR0FsaC344vzi/fk3/eCtRrqOF1xjcXPqHNp5rgjsJ1H/FrBTupo9WUjw2Z8zNTqUTpMRc54BOOx24LBdFGoKa+r73b99kyqmbviXT4XGHuen+Ci1EtP8YZj1+M/xRaeJl3Vn59GC92+3FyN0cH5FBoNsiGHsJiI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=q+pd9Ocr; arc=none smtp.client-ip=95.215.58.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="q+pd9Ocr" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1768594659; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8rMnbmbYZ8Ww7o1IYuKhcfISB8bXYaOAEGc1eum/Jik=; b=q+pd9OcrBldSBgR71GQ6zrsxMbvEXAO+fewXQnonJcDS8zwCTDp3Kw3xRjC/u3hLfdZdmI WmxIhRxTTdfkRkSlEqpFf9EjUA4k6sNozUo0/GxsN6h8TWtq377Cyo5gciDpTmopbvs1nQ 4dPjdDH+WUlPXGmrLfXstevSxfgLN+Q= From: Ihor Solodrai To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman Cc: Mykyta Yatsenko , Tejun Heo , Alan Maguire , Benjamin Tissoires , Jiri Kosina , Amery Hung , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, sched-ext@lists.linux.dev Subject: [PATCH bpf-next v2 04/13] resolve_btfids: Introduce finalize_btf() step Date: Fri, 16 Jan 2026 12:16:51 -0800 Message-ID: <20260116201700.864797-5-ihor.solodrai@linux.dev> In-Reply-To: <20260116201700.864797-1-ihor.solodrai@linux.dev> References: <20260116201700.864797-1-ihor.solodrai@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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@linu= x.dev/ [2] https://lore.kernel.org/bpf/20260109130003.3313716-1-dolinux.peng@gmail= .com/ Signed-off-by: Ihor Solodrai Acked-by: Eduard Zingerman --- tools/bpf/resolve_btfids/main.c | 69 +++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/mai= n.c index 343d08050116..1fcf37af6764 100644 --- a/tools/bpf/resolve_btfids/main.c +++ b/tools/bpf/resolve_btfids/main.c @@ -563,19 +563,6 @@ static int load_btf(struct object *obj) obj->base_btf =3D base_btf; obj->btf =3D btf; =20 - if (obj->base_btf && obj->distill_base) { - err =3D 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 =3D base_btf; - obj->btf =3D btf; - } - return 0; =20 out_err: @@ -911,6 +898,41 @@ static int sort_btf_by_name(struct btf *btf) return err; } =20 +static int finalize_btf(struct object *obj) +{ + struct btf *base_btf =3D obj->base_btf, *btf =3D obj->btf; + int err; + + if (obj->base_btf && obj->distill_base) { + err =3D 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 =3D base_btf; + obj->btf =3D btf; + } + + err =3D 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 =3D NULL; + obj->btf =3D NULL; + + return err; +} + static inline int make_out_path(char *buf, u32 buf_sz, const char *in_path= , const char *suffix) { int len =3D snprintf(buf, buf_sz, "%s%s", in_path, suffix); @@ -1054,6 +1076,7 @@ int main(int argc, const char **argv) }; const char *btfids_path =3D NULL; bool fatal_warnings =3D false; + bool resolve_btfids =3D true; char out_path[PATH_MAX]; =20 struct option btfid_options[] =3D { @@ -1083,12 +1106,6 @@ int main(int argc, const char **argv) if (btfids_path) return patch_btfids(btfids_path, obj.path); =20 - if (load_btf(&obj)) - goto out; - - if (sort_btf_by_name(obj.btf)) - goto out; - if (elf_collect(&obj)) goto out; =20 @@ -1099,12 +1116,22 @@ int main(int argc, const char **argv) if (obj.efile.idlist_shndx =3D=3D -1 || obj.efile.symbols_shndx =3D=3D -1) { pr_debug("Cannot find .BTF_ids or symbols sections, skip symbols resolut= ion\n"); - goto dump_btf; + resolve_btfids =3D false; } =20 - if (symbols_collect(&obj)) + if (resolve_btfids) + if (symbols_collect(&obj)) + goto out; + + if (load_btf(&obj)) goto out; =20 + if (finalize_btf(&obj)) + goto out; + + if (!resolve_btfids) + goto dump_btf; + if (symbols_resolve(&obj)) goto out; =20 --=20 2.52.0