perf dso: Use libbfd to read build_id and .gnu_debuglink section
PATCH 1/3 perf dso: Use libbfd to read build_id and .gnu_debuglink section
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.
---
As a follow up to https://lkml.org/lkml/2020/6/1/402, I believe the 5.8 merge window is about to close (or is closed?), so I felt like it was maybe the right time for an update.
This new version includes:
* a few style changes that were previously requested,
* a regression fix where the bfd filename__read_build_id did not return the correct size on success,
* a small test case with sample PE file and separate debug file.
Cheers,
Remi
tools/perf/util/symbol-elf.c | 80 ++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-)
tools/perf/util/symbol-elf.c +77 -3
@@ -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
@@ -530,6 +532,36 @@ static int elf_read_build_id(Elf *elf, void *bf, size_t size)return err; } +#ifdef HAVE_LIBBFD_SUPPORT + +int filename__read_build_id (const char *filename,void *bf,size_t size) +{ +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 = abfd->build_id->size; + +out_close: +bfd_close (abfd); +return err; +} + +#else +int filename__read_build_id (const char *filename,void *bf,size_t size) {int fd, err =-1 ;
@@ -557,6 +589,8 @@ int filename__read_build_id(const char *filename, void *bf, size_t size) return err; } +#endif +int sysfs__read_build_id (const char *filename,void *build_id,size_t size) {int fd, err =-1 ;
@@ -608,6 +642,44 @@ int sysfs__read_build_id(const char *filename, void *build_id, size_t size)return err; } +#ifdef HAVE_LIBBFD_SUPPORT + +int filename__read_debuglink (const char *filename,char *debuglink, +size_t size) +{ +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 filename__read_debuglink (const char *filename,char *debuglink,size_t size) {
@@ -660,6 +732,8 @@ int filename__read_debuglink(const char *filename, char *debuglink, return err; } +#endif +static int dso__swap_init (struct dso *dso,unsigned char eidata) {static unsigned int const endian =1 ;
Signature · 1 line
2.27.0
PATCH 2/3 perf symbols: Try reading the symbol table with libbfd
Wine generates PE binaries for its code modules and also generates debug files in PE or PDB formats, which perf cannot parse either.
Trying libbfd, when supported, if the default libelf symbol parsing failed, makes it possible to read the symbol table from any binary format supported by it, and lets perf report symbols and annotations for Windows applications running under Wine.
Because libbfd doesn't provide symbol size (probably because of some backends not supporting it), we compute it by first sorting the symbols by addresses and then considering that they are sequential in a given section.
--- tools/perf/util/symbol.c | 129 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/symbol.h | 4 ++ 2 files changed, 133 insertions(+)
tools/perf/util/symbol.c +129 -0
@@ -1523,6 +1523,127 @@ static int dso__load_perf_map(const char *map_path, struct dso *dso)return -1 ; } +#ifdef HAVE_LIBBFD_SUPPORT +#define PACKAGE 'perf' +#include <bfd.h> + +static int bfd_symbols__cmpvalue (const void *a,const void *b) +{ +const asymbol *as = *(const asymbol **)a, *bs = *(const asymbol **)b; + +if (bfd_asymbol_value (as) !=bfd_asymbol_value (bs)) +return bfd_asymbol_value (as) -bfd_asymbol_value (bs); + +return bfd_asymbol_name (as)[0 ] -bfd_asymbol_name (bs)[0 ]; +} + +int dso__load_bfd_symbols (struct dso *dso,const char *debugfile) +{ +int err =-1 ; +long symbols_size, symbols_count; +asection *section; +asymbol **symbols, *sym; +struct symbol *symbol; +bfd *abfd; +u_int i; +u64 start, len; + + abfd =bfd_openr (dso->long_name,NULL ); +if (!abfd) +return -1 ; + +if (!bfd_check_format (abfd, bfd_object)) { +pr_debug2 ("%s: cannot read %s bfd file.\n" , __func__, + dso->long_name); + goto out_close; + } + +if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) + goto out_close; + + section =bfd_get_section_by_name (abfd,".text" ); +if (section) + dso->text_offset = section->vma - section->filepos; + +bfd_close (abfd); + + abfd =bfd_openr (debugfile,NULL ); +if (!abfd) +return -1 ; + +if (!bfd_check_format (abfd, bfd_object)) { +pr_debug2 ("%s: cannot read %s bfd file.\n" , __func__, + debugfile); + goto out_close; + } + +if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) + goto out_close; + + symbols_size =bfd_get_symtab_upper_bound (abfd); +if (symbols_size ==0 ) { +bfd_close (abfd); +return 0 ; + } + +if (symbols_size <0 ) + goto out_close; + + symbols =malloc (symbols_size); +if (!symbols) + goto out_close; + + symbols_count =bfd_canonicalize_symtab (abfd, symbols); +if (symbols_count <0 ) + goto out_free; + +qsort (symbols, symbols_count,sizeof (asymbol *), bfd_symbols__cmpvalue); + +#ifdef bfd_get_section +#define bfd_asymbol_section bfd_get_section +#endif +for (i =0 ; i < symbols_count; ++i) { + sym = symbols[i]; + section =bfd_asymbol_section (sym); +if (!(sym->flags & (BSF_GLOBAL ))) +continue ; + +while (i +1 < symbols_count && +bfd_asymbol_section (symbols[i +1 ]) == section && + !(symbols[i +1 ]->flags &BSF_GLOBAL )) + i++; + +if (i +1 < symbols_count && +bfd_asymbol_section (symbols[i +1 ]) == section) + len = symbols[i +1 ]->value - sym->value; +else + len = section->size - sym->value; + + start =bfd_asymbol_value (sym) - dso->text_offset; + symbol =symbol__new (start, len,STB_GLOBAL ,STT_FUNC , +bfd_asymbol_name (sym)); +if (!symbol) + goto out_free; + +symbols__insert (&dso->symbols, symbol); + } +#ifdef bfd_get_section +#undef bfd_asymbol_section +#endif + +symbols__fixup_end (&dso->symbols); +symbols__fixup_duplicate (&dso->symbols); + dso->adjust_symbols =1 ; + + err =0 ; +out_free: +free (symbols); +out_close: +bfd_close (abfd); +return err; +} +#endif +static bool dso__is_compatible_symtab_type (struct dso *dso,bool kmod,enum dso_binary_type type) {
@@ -1695,6 +1816,7 @@ int dso__load(struct dso *dso, struct map *map)bool next_slot = false;bool is_reg;bool nsexit; +int bfdrc =-1 ;int sirc =-1 ;enum dso_binary_type symtab_type = binary_type_symtab[i];
@@ -1713,12 +1835,19 @@ int dso__load(struct dso *dso, struct map *map)nsinfo__mountns_exit (&nsc); is_reg =is_regular_file (name); +#ifdef HAVE_LIBBFD_SUPPORT if (is_reg) + bfdrc =dso__load_bfd_symbols (dso, name); +#endif +if (is_reg && bfdrc <0 ) sirc =symsrc__init (ss, dso, name, symtab_type);if (nsexit)nsinfo__mountns_enter (dso->nsinfo, &nsc); +if (bfdrc ==0 ) +break ; +if (!is_reg || sirc <0 )continue ;
tools/perf/util/symbol.h +4 -0
@@ -175,6 +175,10 @@ int symbol__config_symfs(const struct option *opt __maybe_unused,struct symsrc ; +#ifdef HAVE_LIBBFD_SUPPORT +int dso__load_bfd_symbols (struct dso *dso,const char *debugfile); +#endif +int dso__load_sym (struct dso *dso,struct map *map,struct symsrc *syms_ss,struct symsrc *runtime_ss,int kmodule);int dso__synthesize_plt_symbols (struct dso *dso,struct symsrc *ss);
Signature · 1 line
2.27.0
Re: [PATCH 2/3] perf symbols: Try reading the symbol table with libbfd
On Wed, Jun 24, 2020 at 12:30:40PM +0200, Remi Bernon wrote:
SNIP
Quoted 32 lines
tools/perf/util/symbol.c +21
-0+ + symbols__insert(&dso->symbols, symbol); + } +#ifdef bfd_get_section +#undef bfd_asymbol_section +#endif + +symbols__fixup_end (&dso->symbols); +symbols__fixup_duplicate (&dso->symbols); + dso->adjust_symbols =1 ; + + err =0 ; +out_free: +free (symbols); +out_close: +bfd_close (abfd); +return err; +} +#endif +static bool dso__is_compatible_symtab_type (struct dso *dso,bool kmod,enum dso_binary_type type) {@@ -1695,6 +1816,7 @@ int dso__load(struct dso *dso, struct map *map)bool next_slot = false;bool is_reg;bool nsexit; +int bfdrc =-1 ;int sirc =-1 ;enum dso_binary_type symtab_type = binary_type_symtab[i];
tools/perf/util/symbol.c int dso__load(struct dso *dso, struct map *map) · line 1841
Quoted 7 lines
tools/perf/util/symbol.c +3
-0@@ -1713,12 +1835,19 @@ int dso__load(struct dso *dso, struct map *map)nsinfo__mountns_exit (&nsc); is_reg =is_regular_file (name); +#ifdef HAVE_LIBBFD_SUPPORT if (is_reg) + bfdrc =dso__load_bfd_symbols (dso, name); +#endif
so the comment says that we try bfd when the libelf fails, but the code seems to do the oposite.. starts with bfd and skip libelf if it's ok
jirka
Quoted 30 lines
tools/perf/util/symbol.c +4
-0+if (is_reg && bfdrc <0 ) sirc =symsrc__init (ss, dso, name, symtab_type);if (nsexit)nsinfo__mountns_enter (dso->nsinfo, &nsc); +if (bfdrc ==0 ) +break ; +if (!is_reg || sirc <0 )continue ;tools/perf/util/symbol.h +4
-0@@ -175,6 +175,10 @@ int symbol__config_symfs(const struct option *opt __maybe_unused,struct symsrc ; +#ifdef HAVE_LIBBFD_SUPPORT +int dso__load_bfd_symbols (struct dso *dso,const char *debugfile); +#endif +int dso__load_sym (struct dso *dso,struct map *map,struct symsrc *syms_ss,struct symsrc *runtime_ss,int kmodule);int dso__synthesize_plt_symbols (struct dso *dso,struct symsrc *ss);Signature · 1 line
2.27.0
Re: [PATCH 2/3] perf symbols: Try reading the symbol table with libbfd
Quoted 48 lines · Jiri Olsa
On Wed, Jun 24, 2020 at 12:30:40PM +0200, Remi Bernon wrote:
SNIP
Quoted 32 lines
tools/perf/util/symbol.c +21
-0+ + symbols__insert(&dso->symbols, symbol); + } +#ifdef bfd_get_section +#undef bfd_asymbol_section +#endif + +symbols__fixup_end (&dso->symbols); +symbols__fixup_duplicate (&dso->symbols); + dso->adjust_symbols =1 ; + + err =0 ; +out_free: +free (symbols); +out_close: +bfd_close (abfd); +return err; +} +#endif +static bool dso__is_compatible_symtab_type (struct dso *dso,bool kmod,enum dso_binary_type type) {@@ -1695,6 +1816,7 @@ int dso__load(struct dso *dso, struct map *map)bool next_slot = false;bool is_reg;bool nsexit; +int bfdrc =-1 ;int sirc =-1 ;enum dso_binary_type symtab_type = binary_type_symtab[i];tools/perf/util/symbol.c int dso__load(struct dso *dso, struct map *map) · line 1841
Quoted 7 lines
tools/perf/util/symbol.c +3
-0@@ -1713,12 +1835,19 @@ int dso__load(struct dso *dso, struct map *map)nsinfo__mountns_exit (&nsc); is_reg =is_regular_file (name); +#ifdef HAVE_LIBBFD_SUPPORT if (is_reg) + bfdrc =dso__load_bfd_symbols (dso, name); +#endif so the comment says that we try bfd when the libelf fails, but the code seems to do the oposite.. starts with bfd and skip libelf if it's ok
Quoted 2 lines
jirka
Ah, sorry I forgot to reword the commit comment to match the changes, thanks for noticing. It should probably be something like that:
Trying to read symbols on non-ELF binaries with libbfd, when supported, makes it possible for perf to report symbols and annotations for Windows applications running under Wine.
As you suggested, the libbfd code now tries to detect the binary format,
and early rejects if ELF is detected, so it then falls back to the
libelf code path.
Signature · 1 line
Rémi Bernon <[email protected]>
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 2/3] perf symbols: Try reading the symbol table with libbfd
Quoted 58 lines · Rémi Bernon
Quoted 57 lines · Rémi Bernon
Quoted 46 lines · Jiri Olsa
On Wed, Jun 24, 2020 at 12:30:40PM +0200, Remi Bernon wrote:
SNIP
Quoted 31 lines
tools/perf/util/symbol.c +20
-0+ + symbols__insert(&dso->symbols, symbol); + } +#ifdef bfd_get_section +#undef bfd_asymbol_section +#endif + + symbols__fixup_end(&dso->symbols); + symbols__fixup_duplicate(&dso->symbols); + dso->adjust_symbols = 1; + + err = 0; +out_free: + free(symbols); +out_close: + bfd_close(abfd); + return err; +} +#endif +static bool
dso__is_compatible_symtab_type(struct dso *dso, bool kmod, enum dso_binary_type type)
{tools/perf/util/symbol.c +1
-0@@ -1695,6 +1816,7 @@ int dso__load(struct dso *dso, struct map *map) bool next_slot = false; bool is_reg; bool nsexit; + int bfdrc = -1; int sirc = -1; enum dso_binary_type symtab_type = binary_type_symtab[i];tools/perf/util/symbol.c int dso__load(struct dso *dso, struct map *map) · line 1840
Quoted 6 lines
tools/perf/util/symbol.c +3
-0@@ -1713,12 +1835,19 @@ int dso__load(struct dso *dso, struct map *map) nsinfo__mountns_exit(&nsc); is_reg = is_regular_file(name); +#ifdef HAVE_LIBBFD_SUPPORT if (is_reg) + bfdrc = dso__load_bfd_symbols(dso, name); +#endif so the comment says that we try bfd when the libelf fails, but the code seems to do the oposite.. starts with bfd and skip libelf if it's ok
Quoted 2 lines
jirka
Ah, sorry I forgot to reword the commit comment to match the changes, thanks for noticing. It should probably be something like that:
Trying to read symbols on non-ELF binaries with libbfd, when supported, makes it possible for perf to report symbols and annotations for Windows applications running under Wine.
Quoted 6 lines
Quoted 3 lines
As you suggested, the libbfd code now tries to detect the binary format, and early rejects if ELF is detected, so it then falls back to the libelf code path.
Hi, it doesn't seem there's more comments, should I just resend the series with the updated commit message?
yes, please fix the changelog.. patches look good to me, but I'd like somebody else with better bfd knowledge to review them ;-)
jirka
PATCH 3/3 perf tests: Add test for PE binary format support
In reply to Remi BernonThis adds a precompiled file in PE binary format, with split debug file, and tries to read its build_id and .gnu_debuglink sections, as well as looking up the main symbol from the debug file. This should succeed if libbfd is supported.
--- tools/perf/Makefile.perf | 1 + tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 4 ++ tools/perf/tests/pe-file-parsing.c | 96 +++++++++++++++++++++++++++++ tools/perf/tests/pe-file.c | 14 +++++ tools/perf/tests/pe-file.exe | Bin 0 -> 75595 bytes tools/perf/tests/pe-file.exe.debug | Bin 0 -> 141644 bytes tools/perf/tests/tests.h | 1 + 8 files changed, 117 insertions(+) create mode 100644 tools/perf/tests/pe-file-parsing.c create mode 100644 tools/perf/tests/pe-file.c create mode 100644 tools/perf/tests/pe-file.exe create mode 100644 tools/perf/tests/pe-file.exe.debug
tools/perf/Makefile.perf +1 -0
@@ -951,6 +951,7 @@ install-tests: all install-gtk $(call QUIET_INSTALL, tests) \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ $(INSTALL) tests/attr.py '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ + $(INSTALL) tests/pe-file.exe* '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr'; \ $(INSTALL) tests/attr/* '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr'; \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/shell'; \
tools/perf/tests/Build +1 -0
@@ -59,6 +59,7 @@ perf-y += genelf.o perf-y += api-io.o perf-y += demangle-java-test.o perf-y += pfm.o +perf-y += pe-file-parsing.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)
tools/perf/tests/builtin-test.c +4 -0
@@ -337,6 +337,10 @@ static struct test generic_tests[] = { .desc = "Demangle Java", .func = test__demangle_java, }, + { + .desc = "PE file support", + .func = test__pe_file_parsing, + }, { .func = NULL, },
tools/perf/tests/pe-file-parsing.c new file +96 -0
96 lines not on this page · raw message
tools/perf/tests/pe-file.c new file +14 -0
@@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 + +// pe-file.exe and pe-file.exe.debug built with; +// x86_64-w64-mingw32-gcc -o pe-file.exe pe-file.c +// -Wl,--file-alignment,4096 -Wl,--build-id +// x86_64-w64-mingw32-objcopy --only-keep-debug +// --compress-debug-sections pe-file.exe pe-file.exe.debug +// x86_64-w64-mingw32-objcopy --strip-debug +// --add-gnu-debuglink=pe-file.exe.debug pe-file.exe + +int main (int argc,char const *argv[]) +{ +return 0 ; +}
tools/perf/tests/pe-file.exe new file binary +0 -0
tools/perf/tests/pe-file.exe.debug new file binary +0 -0
tools/perf/tests/tests.h +1 -0
@@ -121,6 +121,7 @@ int test__demangle_java(struct test *test, int subtest);int test__pfm (struct test *test,int subtest);const char *test__pfm_subtest_get_desc (int subtest);int test__pfm_subtest_get_nr (void ); +int test__pe_file_parsing (struct test *test,int subtest);bool test__bp_signal_is_supported (void );bool test__bp_account_is_supported (void );
Signature · 1 line
2.27.0
Re: [PATCH 3/3] perf tests: Add test for PE binary format support
Quoted 4 lines · Remi Bernon
This adds a precompiled file in PE binary format, with split debug file, and tries to read its
build_idand .gnu_debuglink sections, as well as looking up the main symbol from the debug file. This should succeed if libbfd is supported.
SNIP
tools/perf/tests/pe-file.exe
Quoted 2 lines
tools/perf/tests/pe-file.exe new file +0
-0
dreams coming true.. windows.exe file in perf sources ;-)
awesome, thanks for the test
jirka