From nobody Tue Dec 16 21:26:51 2025 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1CCDB31283C for ; Mon, 8 Dec 2025 06:24:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765175051; cv=none; b=MNlTUQntxo9EtPLBGoatN+VmLzxHOn4GBIh2ydH7zcbhFgbNY0LJDKPsd+z2NqDXJP3TKsyvAFXlpHpTBaxti35AySCP7/nSwtZ/c4iwNtJbDvlZtSiDqor9v1uWs31obw2ZOmDJldJo3Wb1LqClMaC9wzDMGEMuo3pgRUOI8Z0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765175051; c=relaxed/simple; bh=3rLB0CW9qfHiGp6ErABQKSZNXCfm5echOS1kCj5vYuQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JPQiBIsFpfotiJSeCTNKyeslu9RFHhxUBqMblNBr/roSb98WJl73hILW7T+DUB6NCyNTrWph5gMEr2F3slkqG0VCj5uiNjN+98xAy7vaak+1uqMVSAkbF9tPrwK4vCrAFpk1kBgKYAZ4UWE5YmKexCD8UBcC2ZFR7RTIiLz+sxY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fj7VFaED; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fj7VFaED" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-349bb6f9c86so1782764a91.0 for ; Sun, 07 Dec 2025 22:24:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765175049; x=1765779849; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EY1TWHNFPcsVIc12Ea6xmmHpfxEDNasl0yTuoxu3Vmk=; b=fj7VFaEDprC/BgNDn2oPTo+YhqQNO3X60sVic1QNd//WmYPx/Dna4sSwphu6QaeQiJ KdK/fS63i4IeCrTusVXkFt+4ZVTaCOTGlseGu99rJjAVVwPWQLnJI/LsCj8VgIXH7Idd bE/5lJbwErc8t3NcsAaORlbVIzgo1cJG62P16hgTHfgjr/G4GeE3/4pGQ5d/g6A8ZFP0 10ibizOq1nBBFyiLF+e+k+FCx+PRV/+BdBc/qhgTXNEsNMSgdbCzN6UfWRiT8nTL3sus eqiZRBtpr88tYr8TMUwT8co65FlcbR3GjnvwC82Qv3YHwbTbGeIkC6kJmMX47LuAKk/r 12Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765175049; x=1765779849; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=EY1TWHNFPcsVIc12Ea6xmmHpfxEDNasl0yTuoxu3Vmk=; b=FgTzhpopChEE1E7bGcbpe15VL7Tfu56ffWA2K1GqB04Me/FHpZgUxq3qTBEZ7O034A j22gtlAugJ7bV5+fRuftlh0Ft458AJpSboQXnmSs0wS8E5Ub/h9eiP0Ir5oaGeSGr3xR uVNAFskWn347uE+lbvi8vFfUcbbvj3M0NYnoiVpxyrullyoxtfUxacr21qEBd0Co8M3d qoRjJUuEjjUopmMo5HNV+pkfDzmXf5aUX2pDEFNN4XehyLBEYbySY2Ek4rFdMmkiqaLl 5krDdl2ToaZp5c3Rp850IwVmDk+fXccmMPhLjurdHHztE6QXVLP+NFGdxYJ5XXNFkCPW 946Q== X-Forwarded-Encrypted: i=1; AJvYcCXglprFE9Rb0+QHT6/yHxdhXUPqAUvICA5WaifgdBmf9meI7SmUeY37DYkDYFxC6DRHE8ZPqGBMiOCJD/0=@vger.kernel.org X-Gm-Message-State: AOJu0YyM69+7YUhsY44b+mDFzXJbNoB4i9fmKw/GbpysrnkFQEb7YceY w0D3iEyMsVCoACDqm+dZFFl2p6nN8C340AKxsuArJ8imWTVJ9tOIotWi X-Gm-Gg: ASbGnctdaFgRVxS03A/bUd295XIMoC5xCTFz/NcWnKsWsJ+QrIt0Tv/oerw8ewbPsfH QmJ3oqtA7M6D3G8Uj+r+fPqlF10v4nk/Re8xu2OFs+3e5lYWISfLFhJMruw6hNPFVae6DA3ZMdJ eRqOvwMNHNYgbVH4v6ASt9D3MrHTX5GmmJglfDI3jYgeD4AE36MSgHnD5JOGkxW5JqzT4K+xitN kXLwqAfXHemkGIk5tqtukWRuKXFhAhU2q9Wzfa/X8AvX/shmI1WcD855/1JCOO4xheG9T9L+qM8 i+5jpYM3V5BEMjm/4x4Juse1vVXlJOPZmDAUrlvZvp/DiNGUaPYJg7dRINyRTmD4BgcBag4sSs8 TjAea03IRLMoGd00lh1LxVdVbA8MhOV+s78Ed8Nc+sjYmJzp2jR3UoUvmIa1sSauw6VjUtEFpA2 D8+QWGi0pzMN0IoUaKJHH6sgOLxzU= X-Google-Smtp-Source: AGHT+IF9+KuXLJhhdEAvE3hYuv26uDkEHScuCmf/MnF4W8DJO/REbJbcGF8slRlk9j+3UTPcjBmJhw== X-Received: by 2002:a17:90b:5144:b0:340:d81d:7874 with SMTP id 98e67ed59e1d1-349a262ddaemr5369756a91.26.1765175049325; Sun, 07 Dec 2025 22:24:09 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29dae49ca1esm112555855ad.2.2025.12.07.22.24.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Dec 2025 22:24:08 -0800 (PST) From: Donglin Peng To: ast@kernel.org, andrii.nakryiko@gmail.com Cc: eddyz87@gmail.com, zhangxiaoqin@xiaomi.com, ihor.solodrai@linux.dev, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, pengdonglin , Alan Maguire Subject: [PATCH bpf-next v9 03/10] tools/resolve_btfids: Support BTF sorting feature Date: Mon, 8 Dec 2025 14:23:46 +0800 Message-Id: <20251208062353.1702672-4-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251208062353.1702672-1-dolinux.peng@gmail.com> References: <20251208062353.1702672-1-dolinux.peng@gmail.com> 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 Content-Type: text/plain; charset="utf-8" From: pengdonglin This introduces a new BTF sorting phase that specifically sorts BTF types by name in ascending order, so that the binary search can be used to look up types. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Ihor Solodrai Cc: Xiaoqin Zhang Signed-off-by: pengdonglin --- tools/bpf/resolve_btfids/main.c | 68 +++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/mai= n.c index e0e792017e77..b4ec3b556518 100644 --- a/tools/bpf/resolve_btfids/main.c +++ b/tools/bpf/resolve_btfids/main.c @@ -846,6 +846,71 @@ static int dump_raw_btf(struct btf *btf, const char *o= ut_path) return 0; } =20 +/* + * Sort types by name in ascending order resulting in all + * anonymous types being placed before named types. + */ +static int cmp_type_names(const void *a, const void *b, void *priv) +{ + struct btf *btf =3D (struct btf *)priv; + const struct btf_type *ta =3D btf__type_by_id(btf, *(__u32 *)a); + const struct btf_type *tb =3D btf__type_by_id(btf, *(__u32 *)b); + const char *na, *nb; + + na =3D btf__str_by_offset(btf, ta->name_off); + nb =3D btf__str_by_offset(btf, tb->name_off); + return strcmp(na, nb); +} + +static int sort_btf_by_name(struct btf *btf) +{ + __u32 *permute_ids =3D NULL, *id_map =3D NULL; + int nr_types, i, err =3D 0; + __u32 start_id =3D 1, id; + + if (btf__base_btf(btf)) + start_id =3D btf__type_cnt(btf__base_btf(btf)); + nr_types =3D btf__type_cnt(btf) - start_id; + if (nr_types < 2) + goto out; + + permute_ids =3D calloc(nr_types, sizeof(*permute_ids)); + if (!permute_ids) { + err =3D -ENOMEM; + goto out; + } + + id_map =3D calloc(nr_types, sizeof(*id_map)); + if (!id_map) { + err =3D -ENOMEM; + goto out; + } + + for (i =3D 0, id =3D start_id; i < nr_types; i++, id++) + permute_ids[i] =3D id; + + qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names, btf); + + for (i =3D 0; i < nr_types; i++) { + id =3D permute_ids[i] - start_id; + id_map[id] =3D i + start_id; + } + + err =3D btf__permute(btf, id_map, nr_types, NULL); + if (err) + pr_err("FAILED: btf permute: %s\n", strerror(-err)); + +out: + free(permute_ids); + free(id_map); + return err; +} + +static int btf2btf(struct object *obj) +{ + return sort_btf_by_name(obj->btf); +} + static inline int make_out_path(char *buf, const char *in_path, const char= *suffix) { int len =3D snprintf(buf, PATH_MAX, "%s%s", in_path, suffix); @@ -904,6 +969,9 @@ int main(int argc, const char **argv) if (load_btf(&obj)) goto out; =20 + if (btf2btf(&obj)) + goto out; + if (elf_collect(&obj)) goto out; =20 --=20 2.34.1