[RFC PATCH 1/2] perf dso: Use libbfd to read build_id and .gnu_debuglink section

From: RÃmi Bernon
Date: Mon Jun 01 2020 - 07:41:08 EST


Wine generates PE binaries for most of its modules and perf is unable
to parse these files to get build_id or .gnu_debuglink section.

Using libbfd when available, instead of libelf, makes it possible to
resolve debug file location regardless of the dso binary format.

Signed-off-by: RÃmi Bernon <rbernon@xxxxxxxxxxxxxxx>
Cc: Alexander Shishkin <alexander.shishkin@xxxxxxxxxxxxxxx>
Cc: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Mark Rutland <mark.rutland@xxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Jacek Caban <jacek@xxxxxxxxxxxxxxx>
---
tools/perf/util/symbol-elf.c | 65 ++++++++++++++++++++++++++++++++++--
1 file changed, 62 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index be5b493f8284..85bbc1ec9fe5 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -50,6 +50,10 @@ typedef Elf64_Nhdr GElf_Nhdr;
#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
#endif

+#ifdef HAVE_LIBBFD_SUPPORT
+#define PACKAGE 'perf'
+#include <bfd.h>
+#else
#ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
extern char *cplus_demangle(const char *, int);

@@ -65,9 +69,7 @@ static inline char *bfd_demangle(void __maybe_unused *v,
{
return NULL;
}
-#else
-#define PACKAGE 'perf'
-#include <bfd.h>
+#endif
#endif
#endif

@@ -532,6 +534,30 @@ static int elf_read_build_id(Elf *elf, void *bf, size_t size)

int filename__read_build_id(const char *filename, void *bf, size_t size)
{
+#ifdef HAVE_LIBBFD_SUPPORT
+ int err = -1;
+ bfd *abfd;
+
+ abfd = bfd_openr(filename, NULL);
+ if (!abfd)
+ return -1;
+
+ if (!bfd_check_format(abfd, bfd_object)) {
+ pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
+ goto out_close;
+ }
+
+ if (!abfd->build_id || abfd->build_id->size > size)
+ goto out_close;
+
+ memcpy(bf, abfd->build_id->data, abfd->build_id->size);
+ memset(bf + abfd->build_id->size, 0, size - abfd->build_id->size);
+ err = 0;
+
+out_close:
+ bfd_close(abfd);
+ return err;
+#else
int fd, err = -1;
Elf *elf;

@@ -555,6 +581,7 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
close(fd);
out:
return err;
+#endif
}

int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
@@ -611,6 +638,37 @@ int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
int filename__read_debuglink(const char *filename, char *debuglink,
size_t size)
{
+#ifdef HAVE_LIBBFD_SUPPORT
+ int err = -1;
+ asection *section;
+ bfd *abfd;
+
+ abfd = bfd_openr(filename, NULL);
+ if (!abfd)
+ return -1;
+
+ if (!bfd_check_format(abfd, bfd_object)) {
+ pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
+ goto out_close;
+ }
+
+ section = bfd_get_section_by_name(abfd, ".gnu_debuglink");
+ if (!section)
+ goto out_close;
+
+ if (section->size > size)
+ goto out_close;
+
+ if (!bfd_get_section_contents(abfd, section, debuglink, 0,
+ section->size))
+ goto out_close;
+
+ err = 0;
+
+out_close:
+ bfd_close(abfd);
+ return err;
+#else
int fd, err = -1;
Elf *elf;
GElf_Ehdr ehdr;
@@ -658,6 +716,7 @@ int filename__read_debuglink(const char *filename, char *debuglink,
close(fd);
out:
return err;
+#endif
}

static int dso__swap_init(struct dso *dso, unsigned char eidata)
--
2.26.1