From nobody Tue Dec 2 01:36:05 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 B224C213E9C for ; Sun, 23 Nov 2025 02:32:46 +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=1763865168; cv=none; b=Uy+zZNQ6etzIi5/LLyFnMr08n/UkgbE2KNkz2dcLhHQuxhSs5sreeO+0Ed71Csu36rkvJ+BIFaj5fc4HGFaHlV3YQqoYXsyHpAIbx/VeTwQLpyzwW1fADmwkkcInWtaBlxXStBU+pQQee+J42MmF3rvWaiBLNcy78iD+baEdXZc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763865168; c=relaxed/simple; bh=gmLo+tgn4IZIr717BNDUYeGmMI7UzWO3Pd0YWYVvZ0s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=WI6G6JVq1AXJ6+cq7pQK5qn/bYPZjWB9ArU8X5R4DWlQJGtT2zKwACEvaagdy8phpNzhrP4eeaN7XHlM6PuRcoeruP3x42xk9FAGWxZtxbbEYsuq7OH1sWeWS2KLqvdx2CENm2F3eAONYZ6VKYImm2PImENGgokjMsPqhSHsTj8= 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=rOXcy/dl; 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="rOXcy/dl" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b630753cc38so6036111a12.1 for ; Sat, 22 Nov 2025 18:32:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763865166; x=1764469966; 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=PJWi9LJoNL3FYJ5G98IXAS+1MZDxRsbBpxNCWiuvz3Y=; b=rOXcy/dliwqxm0O0CMdF/7SSgwPjy8bmhvMwPfHBndZZ1J3ry0i+PCMmRwmU8prfTA BGywM3lMP5ruW3VckQ2VO37w9DdfuhxdZQ4iW8PH8aghz2FcKRN6jBaCMGv39mQE6Osq n9zfqi7vjnPVoZFP+bUPnyELIGkNTiBXcjj8SEriUh4aXBcE0xSmeEhGDVSUfgqHsaPi LnZ8KsKU5xMWxnBroiOzEFZOK5TYQEbMWRbaXYYrXmqowP0jdFpsdekYJdM7VLzyNAAa 5SXp9TIk7RMJFUjgBbQQxGE2DpALxMvOrEi6ZVEdiywWHFa8FoXHP0mYd6QSoTe94QEu 04OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763865166; x=1764469966; 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=PJWi9LJoNL3FYJ5G98IXAS+1MZDxRsbBpxNCWiuvz3Y=; b=EYyrKJ/X3kXWUgZYd0sYU75uNeMJOsqdVt04J3uWjx+K3lo74ij0IpLUvwACHlzRgB ayrvEXnzn7SuxJ6ZA3jEj13e0YjNFwne2AL5dLy8VSRAEJ4oeD9s/TvEMlaVOE9inpWz zVFfwpXn6R9tNWmK4pF6Jc/U+yuuSoKgWxRzVkNoZmKEsshjF5JZz29TjdTE58yzt5ro U+sgQhuTcXuGBXUHuFgS9rJfgUlvpge9qho9DQBGM3WF6cxdBHVX/LDQDdLk2Y1Y7dtk 8QyGpYOTkijW3vfbxOx6rhHfgoTBDdoSUeNjk6JU4/5Yw4MBXQ7ZVJhwmPBnkkoVQzxx G1Lw== X-Forwarded-Encrypted: i=1; AJvYcCWcRAlrzPnhaZIxe/4N8NYpNoKMTPVSxPI5o7pEHtDYFPSVAsW5HafaHUA1cLL7PrKJe7gO1DxTysKgduo=@vger.kernel.org X-Gm-Message-State: AOJu0Yww6zOLu9i47BdUYNunRxMiUVNEAK1zAPA6V/9w787rVaFhzyht bElheNhPprPAaClg30pWtJqNn3U3O4jwGpAVH80F7dcktttelHp2UU+eioDAbk6d+u2UNkx0arn zgKHYTkixGA== X-Google-Smtp-Source: AGHT+IGlLjcaAxzInDuxNrjd4MICP4gAlip8m1PZOAGVSGoEQnG3ECgO4g4ensv3j1w62Z5aRC/CWmyas1X5 X-Received: from dlbrh6.prod.google.com ([2002:a05:7022:f306:b0:11a:26c7:567d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:f909:b0:11b:9386:a380 with SMTP id a92af1059eb24-11c9d8720c3mr2820469c88.47.1763865165987; Sat, 22 Nov 2025 18:32:45 -0800 (PST) Date: Sat, 22 Nov 2025 18:32:25 -0800 In-Reply-To: <20251123023225.8069-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: <20251123023225.8069-1-irogers@google.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog Message-ID: <20251123023225.8069-10-irogers@google.com> Subject: [PATCH v1 9/9] 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 , Aditya Bodkhe , 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 861cb7ae9b45..d36b69ccccda 100644 --- a/tools/perf/util/perf-libelf.c +++ b/tools/perf/util/perf-libelf.c @@ -251,3 +251,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 95fe8805aa41..976fe3fb5cd8 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 @@ -29,6 +31,7 @@ int __libelf__read_build_id(Elf *elf, void *bf, size_t si= ze); int libelf__read_build_id(int _fd, const char *filename, struct build_id *= bid); int libelf_sysfs__read_build_id(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 @@ -52,6 +55,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 5ddf8353e01b..3d385d749c53 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 cc9055ba2f2a..336886c2b1ff 100644 --- a/tools/perf/util/symbol-minimal.c +++ b/tools/perf/util/symbol-minimal.c @@ -225,6 +225,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) @@ -271,44 +309,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 d37d5a97bc6f..25484b2e7fa4 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, boo= l block); int sysfs__read_build_id(const char *filename, struct build_id *bid); int modules__parse(const char *filename, void *arg, --=20 2.52.0.rc2.455.g230fcf2819-goog