[PATCH 2/9] perf symbol: Fix the build when demangling with libbfd
From: Ian Rogers
Date: Wed Sep 16 2026 - 02:14:14 EST
cxx_demangle_sym() calls bfd_demangle() with the DMGL_PARAMS and
DMGL_ANSI flags, but nothing declares them. The
"#define PACKAGE 'perf'", "#include <bfd.h>" and the DMGL_ definitions
were left behind in symbol-elf.c when the demangling code moved to
symbol.c, and libiberty's demangle.h that defines the flags isn't
installed by every binutils package. The cplus_demangle() variant is
missing a declaration for the same reason.
The code is only built when the C++ ABI's __cxa_demangle() is
unavailable, so most builds never compile it:
$ make BUILD_NONDISTRO=1 NO_DEMANGLE=1
util/symbol.c:2714: error: 'DMGL_PARAMS' undeclared
util/symbol.c:2714: error: 'DMGL_ANSI' undeclared
util/symbol.c:2716: error: implicit declaration of 'bfd_demangle'
Move the libbfd demangling into libbfd.c, which already includes bfd.h,
and declare in symbol.c the libiberty interfaces the cplus_demangle()
variant needs.
Fixes: 4d9b5146f0d9 ("perf symbol: Move demangling code out of symbol-elf.c")
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
Assisted-by: Antigravity:gemini-3.1-pro
---
tools/perf/util/libbfd.c | 17 +++++++++++++++++
tools/perf/util/libbfd.h | 9 +++++++++
tools/perf/util/symbol.c | 18 +++++++++++++++---
3 files changed, 41 insertions(+), 3 deletions(-)
diff --git a/tools/perf/util/libbfd.c b/tools/perf/util/libbfd.c
index 8ac6670a39aa..131bfedf48cd 100644
--- a/tools/perf/util/libbfd.c
+++ b/tools/perf/util/libbfd.c
@@ -72,6 +72,23 @@ static void ensure_bfd_init(void)
pthread_once(&bfd_init_once, perf_bfd_init);
}
+/*
+ * Flags from libiberty's demangle.h. bfd.h declares bfd_demangle but not the
+ * flags to pass to it, and demangle.h isn't installed by every binutils
+ * package.
+ */
+#ifndef DMGL_PARAMS
+#define DMGL_PARAMS (1 << 0) /* Include function arguments. */
+#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc. */
+#endif
+
+char *libbfd__demangle_sym(const char *str, bool params, bool modifiers)
+{
+ int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
+
+ return bfd_demangle(/*abfd=*/NULL, str, flags);
+}
+
static int bfd_error(const char *string)
{
const char *errmsg;
diff --git a/tools/perf/util/libbfd.h b/tools/perf/util/libbfd.h
index 953886f3d62f..14171db8b112 100644
--- a/tools/perf/util/libbfd.h
+++ b/tools/perf/util/libbfd.h
@@ -31,6 +31,8 @@ int libbfd_filename__read_debuglink(const char *filename, char *debuglink, size_
int symbol__disassemble_bpf_libbfd(struct symbol *sym, struct annotate_args *args);
+char *libbfd__demangle_sym(const char *str, bool params, bool modifiers);
+
#else // !defined(HAVE_LIBBFD_SUPPORT)
#include "annotate.h"
@@ -77,6 +79,13 @@ static inline int symbol__disassemble_bpf_libbfd(struct symbol *sym __always_unu
return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
}
+static inline char *libbfd__demangle_sym(const char *str __always_unused,
+ bool params __always_unused,
+ bool modifiers __always_unused)
+{
+ return NULL;
+}
+
#endif // defined(HAVE_LIBBFD_SUPPORT)
#endif /* __PERF_LIBBFD_H */
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 3587ad243159..3206929473a2 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -27,6 +27,7 @@
#include "dso.h"
#include "util.h" // lsdir()
#include "event.h"
+#include "libbfd.h"
#include "machine.h"
#include "map.h"
#include "symbol.h"
@@ -2707,13 +2708,24 @@ static bool want_demangle(bool is_kernel_sym)
* version.
*/
#ifndef HAVE_CXA_DEMANGLE_SUPPORT
+#ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
+/*
+ * Declarations from libiberty's demangle.h, the header isn't installed by
+ * every binutils package.
+ */
+#ifndef DMGL_PARAMS
+#define DMGL_PARAMS (1 << 0) /* Include function arguments. */
+#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc. */
+#endif
+
+char *cplus_demangle(const char *mangled, int options);
+#endif
+
char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe_unused,
bool modifiers __maybe_unused)
{
#ifdef HAVE_LIBBFD_SUPPORT
- int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
-
- return bfd_demangle(NULL, str, flags);
+ return libbfd__demangle_sym(str, params, modifiers);
#elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
--
2.55.0.1032.g73a4cd73de-goog