perf dso: Move read_symbol() from llvm/capstone to dso

Move the read_symbol function to dso.h, make the return type const and
add a mutable out_buf out parameter.

In future changes this will allow a code pointer to be returned without
necessary allocating memory.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexandre Ghiti <alexghiti@rivosinc.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.ibm.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Charlie Jenkins <charlie@rivosinc.com>
Cc: Collin Funk <collin.funk1@gmail.com>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Eric Biggers <ebiggers@kernel.org>
Cc: Haibo Xu <haibo1.xu@intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Li Huafei <lihuafei1@huawei.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <nick.desaulniers+lkml@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <song@kernel.org>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Ian Rogers
2025-10-05 14:22:04 -07:00
committed by Arnaldo Carvalho de Melo
parent 0e52f3f9f1
commit 9518e10c2b
4 changed files with 97 additions and 128 deletions

View File

@@ -1798,3 +1798,70 @@ bool is_perf_pid_map_name(const char *dso_name)
return perf_pid_map_tid(dso_name, &tid);
}
struct find_file_offset_data {
u64 ip;
u64 offset;
};
/* This will be called for each PHDR in an ELF binary */
static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
{
struct find_file_offset_data *data = arg;
if (start <= data->ip && data->ip < start + len) {
data->offset = pgoff + data->ip - start;
return 1;
}
return 0;
}
const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
const struct map *map, const struct symbol *sym,
u8 **out_buf, u64 *out_buf_len, bool *is_64bit)
{
struct nscookie nsc;
u64 start = map__rip_2objdump(map, sym->start);
u64 end = map__rip_2objdump(map, sym->end);
int fd, count;
u8 *buf = NULL;
size_t len;
struct find_file_offset_data data = {
.ip = start,
};
*out_buf = NULL;
*out_buf_len = 0;
*is_64bit = false;
nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
fd = open(symfs_filename, O_RDONLY);
nsinfo__mountns_exit(&nsc);
if (fd < 0)
return NULL;
if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, is_64bit) == 0)
goto err;
len = end - start;
buf = malloc(len);
if (buf == NULL)
goto err;
count = pread(fd, buf, len, data.offset);
close(fd);
fd = -1;
if ((u64)count != len)
goto err;
*out_buf = buf;
*out_buf_len = len;
return buf;
err:
if (fd >= 0)
close(fd);
free(buf);
return NULL;
}