Skip to content

Commit d2930ed

Browse files
rbernonacmel
authored andcommitted
perf symbol: Look for ImageBase in PE file to compute .text offset
Instead of using the file offset in the debug file. This fixes a regression from 00a3423 ("perf symbols: Make dso__load_bfd_symbols() load PE files from debug cache only"), causing incorrect symbol resolution when debug file have been stripped from non-debug sections (in which case its .text section is empty and doesn't have any file position). The debug files could also be created with a different file alignment, and have different file positions from the mmap-ed binary, or have the section reordered. This instead looks for the file image base, using the corresponding bfd *ABS* symbols. As PE symbols only have 4 bytes, it also needs to keep .text section vma high bits. Signed-off-by: Remi Bernon <rbernon@codeweavers.com> Fixes: 00a3423 ("perf symbols: Make dso__load_bfd_symbols() load PE files from debug cache only") Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Nicholas Fraser <nfraser@codeweavers.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lore.kernel.org/lkml/20210909192637.4139125-1-rbernon@codeweavers.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 51ae7fa commit d2930ed

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

tools/perf/util/symbol.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,10 +1581,6 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)
15811581
if (bfd_get_flavour(abfd) == bfd_target_elf_flavour)
15821582
goto out_close;
15831583

1584-
section = bfd_get_section_by_name(abfd, ".text");
1585-
if (section)
1586-
dso->text_offset = section->vma - section->filepos;
1587-
15881584
symbols_size = bfd_get_symtab_upper_bound(abfd);
15891585
if (symbols_size == 0) {
15901586
bfd_close(abfd);
@@ -1602,6 +1598,22 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)
16021598
if (symbols_count < 0)
16031599
goto out_free;
16041600

1601+
section = bfd_get_section_by_name(abfd, ".text");
1602+
if (section) {
1603+
for (i = 0; i < symbols_count; ++i) {
1604+
if (!strcmp(bfd_asymbol_name(symbols[i]), "__ImageBase") ||
1605+
!strcmp(bfd_asymbol_name(symbols[i]), "__image_base__"))
1606+
break;
1607+
}
1608+
if (i < symbols_count) {
1609+
/* PE symbols can only have 4 bytes, so use .text high bits */
1610+
dso->text_offset = section->vma - (u32)section->vma;
1611+
dso->text_offset += (u32)bfd_asymbol_value(symbols[i]);
1612+
} else {
1613+
dso->text_offset = section->vma - section->filepos;
1614+
}
1615+
}
1616+
16051617
qsort(symbols, symbols_count, sizeof(asymbol *), bfd_symbols__cmpvalue);
16061618

16071619
#ifdef bfd_get_section

0 commit comments

Comments
 (0)