From nobody Tue Dec 2 01:06:35 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 D496928489E for ; Mon, 1 Dec 2025 20:55:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764622552; cv=none; b=ubqlCDDIQv5QUzhRqgXjKPw0xvR9UNGF+Rtr/Au2Z2tXqMj4g+E2kqF+G85UFaYjC96qHYkohlgSO07Ibkv1OKQknVQkEEJK2XxrqtfkOxxjf/+9+PciS4hAgKNACmhAhfMpKaQFsWzVWXqLTIn1TPnuoK0dP2dW6tbSB3C6I+w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764622552; c=relaxed/simple; bh=jrlJM+/Xgy1nehgeiFknXjuulHnnAtIGNxHD7/IQYEo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=jST4Fh2MHJIAuQLH2JL0alXTPWWVDLkXqWH0KBBW6TL+oV7Ow3cgXYV1/NbZKE5djNNer8O3td/3I4/dkIcpeADPn+9OgnbNDaQ2938EEthVDBt0S3kyYvO/iFfOPibKjgmQKTF8Bowibwpf76aelYzf6y7ObFPLNiTWq6HbmjU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ev/qTNbN; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ev/qTNbN" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b99d6bd6cc9so7672481a12.1 for ; Mon, 01 Dec 2025 12:55:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764622550; x=1765227350; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=cKo8U+r87/wZow77a+6w5wG9mxBjXhr2B97riOby7IM=; b=Ev/qTNbNmqOZiIL74zrSySwFegdLycWrZ5jj7mvEhMa2A7+GGZrbRJ2QIU+MxX08qr n4J9AvvhQdnICpsWdZtFNtp8SRc3510lhUISDFKZTa69w+64MOaauXkpceAkHHZtnUJZ R1/d2aYOIkSQFaFdkpaAW2rn4v6ap/xIzeemSspJ7jTjEJxagtt8lhZZZvhODXMYhgTU 6G8ss8dKQvEq3ptJRsvsP4eZk4vE/1UqTLB4jxQaD57IWI4/T2O9cq783/sE4a9wFCB6 Nr3pyxhUWtSeSIdvHx9BSgurKG3rTrKZOTdg/56tkTD3/j6tFVZeV7iQGj5JQnE3fnO4 czNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764622550; x=1765227350; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cKo8U+r87/wZow77a+6w5wG9mxBjXhr2B97riOby7IM=; b=HKZFKGZ3zlX/lGqzfK8FvEwZkXqmrSxp3mDwSrAryy8x0RLkBBe5/VSNIlV0O77lvx nrfXQY5JfjbathZeUMAWrPbH6o3GpCqt/rKKTEnOqARrfw2hKkBouKmYR0tG1fSlhwOe kxoSAkwH2S7U2ajvlyqz981SWXPfcBjLCKkLO87B9N28fYVIoG+NgntLb2SgS+Z1mc3f DzhmK6VwhRxktCL+fRR6E97ON0KbNtfkV2fgGoNnLGtqNZryrXl5xnQAHZQXM0a9SEMN lDExSRjvxvjzdSDahbw4ny5nDmyPOjnpWNXw598k8zAVDKIG6iS70HaI0upyHTAYIl4V EtTw== X-Forwarded-Encrypted: i=1; AJvYcCUZsiXTlOivZBUqnrbuhA47Fa0tSZVH+hSNixxaVPqSQyKumQAqmmAl8xIBp7/kj/A9SJxf4Ny+kr60fA0=@vger.kernel.org X-Gm-Message-State: AOJu0YwnRcQyJiXeyfpT+cm6ioCDxoSI7dizYYyjaEo4u39wmFoybtVT nLEfqXHJDZLXYcXrf7OIcIdm2YkVwp3tJ/s/y5ontp5/RljKna14pohuQSbbgYV6+7uWie6y1ke JIuQ/IKVZ0w== X-Google-Smtp-Source: AGHT+IFwfbvxrTUV97nEqe//dS4hmDl4mUXXnvNvY1GF2WwfaNj5WQeqU7NF6PKqtn2upo6UO60uM+9Q7iaX X-Received: from dybuh25.prod.google.com ([2002:a05:7301:7519:b0:2a4:664f:e91d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:550d:b0:2a4:3594:d552 with SMTP id 5a478bee46e88-2a719f9d8d6mr19884768eec.31.1764622549762; Mon, 01 Dec 2025 12:55:49 -0800 (PST) Date: Mon, 1 Dec 2025 12:55:08 -0800 In-Reply-To: <20251201205509.195451-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251201205509.195451-1-irogers@google.com> X-Mailer: git-send-email 2.52.0.158.g65b55ccf14-goog Message-ID: <20251201205509.195451-10-irogers@google.com> Subject: [PATCH v2 09/10] perf dso: Move type helpers out of symbol and use fallbacks From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Suzuki K Poulose , Mike Leach , James Clark , John Garry , Will Deacon , Leo Yan , Athira Rajeev , tanze , Stephen Brennan , Andi Kleen , Chun-Tse Shao , Thomas Falcon , Dapeng Mi , "Dr. David Alan Gilbert" , Christophe Leroy , "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" , "Masami Hiramatsu (Google)" , Alexandre Ghiti , Haibo Xu , Sergei Trofimovich , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Fallback between libelf and symbol-minimal versions. Remove the symbol.c code and just directly inline into the only caller in dso.c. Signed-off-by: Ian Rogers --- tools/perf/util/dso.c | 57 ++++++++++++++---------- tools/perf/util/perf-libelf.c | 33 ++++++++++++++ tools/perf/util/perf-libelf.h | 8 ++++ tools/perf/util/symbol-elf.c | 33 -------------- tools/perf/util/symbol-minimal.c | 76 ++++++++++++++++---------------- tools/perf/util/symbol-minimal.h | 3 ++ tools/perf/util/symbol.h | 2 - 7 files changed, 115 insertions(+), 97 deletions(-) diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 344e689567ee..421fa4d27d2a 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1,37 +1,42 @@ // SPDX-License-Identifier: GPL-2.0 -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include #include -#ifdef HAVE_LIBBPF_SUPPORT -#include -#include "bpf-event.h" -#include "bpf-utils.h" -#endif +#include +#include +#include +#include +#include + +#include "annotate-data.h" +#include "auxtrace.h" #include "compress.h" +#include "debug.h" +#include "dso.h" +#include "dsos.h" #include "env.h" +#include "machine.h" +#include "map.h" #include "namespaces.h" #include "path.h" -#include "map.h" -#include "symbol.h" +#include "perf-libelf.h" #include "srcline.h" -#include "dso.h" -#include "dsos.h" -#include "machine.h" -#include "auxtrace.h" -#include "util.h" /* O_CLOEXEC for older systems */ -#include "debug.h" #include "string2.h" +#include "symbol-minimal.h" +#include "symbol.h" +#include "util.h" /* O_CLOEXEC for older systems */ #include "vdso.h" -#include "annotate-data.h" + +#include +#include +#include +#include + +#ifdef HAVE_LIBBPF_SUPPORT +#include +#include "bpf-event.h" +#include "bpf-utils.h" +#endif =20 static const char * const debuglink_paths[] =3D { "%.0s%s", @@ -1747,7 +1752,11 @@ enum dso_type dso__type(struct dso *dso, struct mach= ine *machine) enum dso_type type =3D DSO__TYPE_UNKNOWN; =20 if (dso__data_get_fd(dso, machine, &fd)) { - type =3D dso__type_fd(fd); + type =3D libelf_dso__type_fd(fd); + + if (type =3D=3D DSO__TYPE_UNKNOWN) + type =3D sym_min_dso__type_fd(fd); + dso__data_put_fd(dso); } =20 diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c index 4198f4dfad9f..eed5c0317f8b 100644 --- a/tools/perf/util/perf-libelf.c +++ b/tools/perf/util/perf-libelf.c @@ -200,3 +200,36 @@ int libelf_filename__read_debuglink(const char *filena= me, char *debuglink, size_ out: return err; } + +enum dso_type libelf_dso__type_fd(int fd) +{ + enum dso_type dso_type =3D DSO__TYPE_UNKNOWN; + GElf_Ehdr ehdr; + Elf_Kind ek; + Elf *elf; + + elf =3D elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); + if (elf =3D=3D NULL) + goto out; + + ek =3D elf_kind(elf); + if (ek !=3D ELF_K_ELF) + goto out_end; + + if (gelf_getclass(elf) =3D=3D ELFCLASS64) { + dso_type =3D DSO__TYPE_64BIT; + goto out_end; + } + + if (gelf_getehdr(elf, &ehdr) =3D=3D NULL) + goto out_end; + + if (ehdr.e_machine =3D=3D EM_X86_64) + dso_type =3D DSO__TYPE_X32BIT; + else + dso_type =3D DSO__TYPE_32BIT; +out_end: + elf_end(elf); +out: + return dso_type; +} diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h index 2118325c04e5..5ca5641b9dc9 100644 --- a/tools/perf/util/perf-libelf.h +++ b/tools/perf/util/perf-libelf.h @@ -4,6 +4,8 @@ =20 #include =20 +#include "dso.h" + struct build_id; =20 #ifdef HAVE_LIBELF_SUPPORT @@ -28,6 +30,7 @@ int __libelf__read_build_id(Elf *elf, void *bf, size_t si= ze); =20 int libelf__read_build_id(int _fd, const char *filename, struct build_id *= bid); int libelf_filename__read_debuglink(const char *filename, char *debuglink,= size_t size); +enum dso_type libelf_dso__type_fd(int fd); =20 #else // !defined(HAVE_LIBELF_SUPPORT) =20 @@ -45,6 +48,11 @@ static inline int libelf_filename__read_debuglink(const = char *filename __always_ return -1; } =20 +enum dso_type libelf_dso__type_fd(int fd __always_unused) +{ + return DSO__TYPE_UNKNOWN; +} + #endif // defined(HAVE_LIBELF_SUPPORT) =20 #endif /* __PERF_LIBELF_H */ diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index cb890de31044..f483aa67f69b 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1572,39 +1572,6 @@ int file__read_maps(int fd, bool exe, mapfn_t mapfn,= void *data, return err; } =20 -enum dso_type dso__type_fd(int fd) -{ - enum dso_type dso_type =3D DSO__TYPE_UNKNOWN; - GElf_Ehdr ehdr; - Elf_Kind ek; - Elf *elf; - - elf =3D elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); - if (elf =3D=3D NULL) - goto out; - - ek =3D elf_kind(elf); - if (ek !=3D ELF_K_ELF) - goto out_end; - - if (gelf_getclass(elf) =3D=3D ELFCLASS64) { - dso_type =3D DSO__TYPE_64BIT; - goto out_end; - } - - if (gelf_getehdr(elf, &ehdr) =3D=3D NULL) - goto out_end; - - if (ehdr.e_machine =3D=3D EM_X86_64) - dso_type =3D DSO__TYPE_X32BIT; - else - dso_type =3D DSO__TYPE_32BIT; -out_end: - elf_end(elf); -out: - return dso_type; -} - static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u6= 4 len) { ssize_t r; diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-mini= mal.c index 0e3a27dae9d6..f9724448fb64 100644 --- a/tools/perf/util/symbol-minimal.c +++ b/tools/perf/util/symbol-minimal.c @@ -259,6 +259,44 @@ int sym_min_sysfs__read_build_id(const char *filename,= struct build_id *bid) return ret; } =20 +static int fd__is_64_bit(int fd) +{ + u8 e_ident[EI_NIDENT]; + + if (lseek(fd, 0, SEEK_SET)) + return -1; + + if (readn(fd, e_ident, sizeof(e_ident)) !=3D sizeof(e_ident)) + return -1; + + if (memcmp(e_ident, ELFMAG, SELFMAG) || + e_ident[EI_VERSION] !=3D EV_CURRENT) + return -1; + + return e_ident[EI_CLASS] =3D=3D ELFCLASS64; +} + +enum dso_type sym_min_dso__type_fd(int fd) +{ + Elf64_Ehdr ehdr; + int ret; + + ret =3D fd__is_64_bit(fd); + if (ret < 0) + return DSO__TYPE_UNKNOWN; + + if (ret) + return DSO__TYPE_64BIT; + + if (readn(fd, &ehdr, sizeof(ehdr)) !=3D sizeof(ehdr)) + return DSO__TYPE_UNKNOWN; + + if (ehdr.e_machine =3D=3D EM_X86_64) + return DSO__TYPE_X32BIT; + + return DSO__TYPE_32BIT; +} + #ifndef HAVE_LIBELF_SUPPORT int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name, enum dso_binary_type type) @@ -305,44 +343,6 @@ int dso__synthesize_plt_symbols(struct dso *dso __mayb= e_unused, return 0; } =20 -static int fd__is_64_bit(int fd) -{ - u8 e_ident[EI_NIDENT]; - - if (lseek(fd, 0, SEEK_SET)) - return -1; - - if (readn(fd, e_ident, sizeof(e_ident)) !=3D sizeof(e_ident)) - return -1; - - if (memcmp(e_ident, ELFMAG, SELFMAG) || - e_ident[EI_VERSION] !=3D EV_CURRENT) - return -1; - - return e_ident[EI_CLASS] =3D=3D ELFCLASS64; -} - -enum dso_type dso__type_fd(int fd) -{ - Elf64_Ehdr ehdr; - int ret; - - ret =3D fd__is_64_bit(fd); - if (ret < 0) - return DSO__TYPE_UNKNOWN; - - if (ret) - return DSO__TYPE_64BIT; - - if (readn(fd, &ehdr, sizeof(ehdr)) !=3D sizeof(ehdr)) - return DSO__TYPE_UNKNOWN; - - if (ehdr.e_machine =3D=3D EM_X86_64) - return DSO__TYPE_X32BIT; - - return DSO__TYPE_32BIT; -} - int dso__load_sym(struct dso *dso, struct map *map __maybe_unused, struct symsrc *ss, struct symsrc *runtime_ss __maybe_unused, diff --git a/tools/perf/util/symbol-minimal.h b/tools/perf/util/symbol-mini= mal.h index 5cce1f1f0f16..282466aca7d9 100644 --- a/tools/perf/util/symbol-minimal.h +++ b/tools/perf/util/symbol-minimal.h @@ -2,9 +2,12 @@ #ifndef __PERF_SYMBOL_MINIMAL_H #define __PERF_SYMBOL_MINIMAL_H =20 +#include "dso.h" + struct build_id; =20 int sym_min__read_build_id(int _fd, const char *filename, struct build_id = *bid); int sym_min_sysfs__read_build_id(const char *filename, struct build_id *bi= d); +enum dso_type sym_min_dso__type_fd(int fd); =20 #endif /* __PERF_SYMBOL_MINIMAL_H */ diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index e265e1f028be..4cc557981724 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -117,8 +117,6 @@ struct symbol *dso__first_symbol(struct dso *dso); struct symbol *dso__last_symbol(struct dso *dso); struct symbol *dso__next_symbol(struct symbol *sym); =20 -enum dso_type dso__type_fd(int fd); - int filename__read_build_id(const char *filename, struct build_id *id); int sysfs__read_build_id(const char *filename, struct build_id *bid); int modules__parse(const char *filename, void *arg, --=20 2.52.0.158.g65b55ccf14-goog