[PATCH v1 3/9] perf symbol: Move libelf code to its own file/header

Ian Rogers
2025-11-23T02:32:35Z

Move perf declarations depending on libelf out of symbol.h into
perf-libelf.h. Move the corresponding C code into perf-libelf.c. This
is motivated by trying to make it clearer the symbol.[ch] is generic
code without library dependencies.

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/Build | 1 +
tools/perf/util/perf-libelf.c | 32 ++++++++++++++++++++++++++++++++
tools/perf/util/perf-libelf.h | 25 +++++++++++++++++++++++++
tools/perf/util/probe-event.c | 1 +
tools/perf/util/probe-finder.c | 1 +
tools/perf/util/symbol-elf.c | 27 +--------------------------
tools/perf/util/symbol.h | 19 -------------------
7 files changed, 61 insertions(+), 45 deletions(-)
create mode 100644 tools/perf/util/perf-libelf.c
create mode 100644 tools/perf/util/perf-libelf.h

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 1c2a43e1dc68..dcac6f1e1d99 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -193,6 +193,7 @@ ifeq ($(CONFIG_LIBTRACEEVENT),y)
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_kwork_top.o
endif

+perf-util-$(CONFIG_LIBELF) += perf-libelf.o
perf-util-$(CONFIG_LIBELF) += symbol-elf.o
perf-util-$(CONFIG_LIBELF) += probe-file.o
perf-util-$(CONFIG_LIBELF) += probe-event.o
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
new file mode 100644
index 000000000000..abd55bd7f14b
--- /dev/null
+++ b/tools/perf/util/perf-libelf.c
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "perf-libelf.h"
+
+#include <stddef.h>
+#include <string.h>
+
+Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
+ GElf_Shdr *shp, const char *name, size_t *idx)
+{
+ Elf_Scn *sec = NULL;
+ size_t cnt = 1;
+
+ /* ELF is corrupted/truncated, avoid calling elf_strptr. */
+ if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
+ return NULL;
+
+ while ((sec = elf_nextscn(elf, sec)) != NULL) {
+ char *str;
+
+ gelf_getshdr(sec, shp);
+ str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
+ if (str && !strcmp(name, str)) {
+ if (idx)
+ *idx = cnt;
+ return sec;
+ }
+ ++cnt;
+ }
+
+ return NULL;
+}
+
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
new file mode 100644
index 000000000000..5d7d18daf5ff
--- /dev/null
+++ b/tools/perf/util/perf-libelf.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_LIBELF_H
+#define __PERF_LIBELF_H
+
+#ifdef HAVE_LIBELF_SUPPORT
+
+#include <libelf.h>
+#include <gelf.h>
+
+/*
+ * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
+ * for newer versions we can use mmap to reduce memory usage:
+ */
+#ifdef ELF_C_READ_MMAP
+# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
+#else
+# define PERF_ELF_C_READ_MMAP ELF_C_READ
+#endif
+
+Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name,
+ size_t *idx);
+
+#endif // defined(HAVE_LIBELF_SUPPORT)
+
+#endif /* __PERF_LIBELF_H */
diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
index 6ab2eb551b6c..c27ad9121230 100644
--- a/tools/perf/util/probe-event.c
+++ b/tools/perf/util/probe-event.c
@@ -23,6 +23,7 @@
#include "build-id.h"
#include "event.h"
#include "namespaces.h"
+#include "perf-libelf.h"
#include "strlist.h"
#include "strfilter.h"
#include "debug.h"
diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index 5ffd97ee4898..f94f742a3458 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -25,6 +25,7 @@
#include "debug.h"
#include "debuginfo.h"
#include "intlist.h"
+#include "perf-libelf.h"
#include "strbuf.h"
#include "strlist.h"
#include "symbol.h"
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 629b272e0a13..750a30b216d6 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -12,6 +12,7 @@
#include "libbfd.h"
#include "map.h"
#include "maps.h"
+#include "perf-libelf.h"
#include "symbol.h"
#include "symsrc.h"
#include "machine.h"
@@ -183,32 +184,6 @@ static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
return -1;
}

-Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
- GElf_Shdr *shp, const char *name, size_t *idx)
-{
- Elf_Scn *sec = NULL;
- size_t cnt = 1;
-
- /* ELF is corrupted/truncated, avoid calling elf_strptr. */
- if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
- return NULL;
-
- while ((sec = elf_nextscn(elf, sec)) != NULL) {
- char *str;
-
- gelf_getshdr(sec, shp);
- str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
- if (str && !strcmp(name, str)) {
- if (idx)
- *idx = cnt;
- return sec;
- }
- ++cnt;
- }
-
- return NULL;
-}
-
bool filename__has_section(const char *filename, const char *sec)
{
int fd;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 78fb2ba69f65..0aa8680cbd3e 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -14,10 +14,6 @@
#include "symbol_conf.h"
#include "spark.h"

-#ifdef HAVE_LIBELF_SUPPORT
-#include <libelf.h>
-#include <gelf.h>
-#endif
#include <elf.h>

struct dso;
@@ -26,21 +22,6 @@ struct maps;
struct option;
struct build_id;

-/*
- * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
- * for newer versions we can use mmap to reduce memory usage:
- */
-#ifdef ELF_C_READ_MMAP
-# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
-#else
-# define PERF_ELF_C_READ_MMAP ELF_C_READ
-#endif
-
-#ifdef HAVE_LIBELF_SUPPORT
-Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
- GElf_Shdr *shp, const char *name, size_t *idx);
-#endif
-
/**
* A symtab entry. When allocated this may be preceded by an annotation (see
* symbol__annotation) and/or a browser_index (see symbol__browser_index).
--
2.52.0.rc2.455.g230fcf2819-goog