perf symbol: Add filename__has_section()

The filename__has_section() is to check if the given section name is in
the binary.  It'd be used for checking debug info for srcline.

Committer notes:

Added missing  __maybe_unused to the unused filename__has_section()
arguments in tools/perf/util/symbol-minimal.c.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Milian Wolff <milian.wolff@kdab.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20221215192817.2734573-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Namhyung Kim 2022-12-15 11:28:11 -08:00 committed by Arnaldo Carvalho de Melo
parent ea335ef3dd
commit 06ea72a42d
3 changed files with 34 additions and 0 deletions

View file

@ -233,6 +233,34 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
return NULL;
}
bool filename__has_section(const char *filename, const char *sec)
{
int fd;
Elf *elf;
GElf_Ehdr ehdr;
GElf_Shdr shdr;
bool found = false;
fd = open(filename, O_RDONLY);
if (fd < 0)
return false;
elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
if (elf == NULL)
goto out;
if (gelf_getehdr(elf, &ehdr) == NULL)
goto elf_out;
found = !!elf_section_by_name(elf, &ehdr, &shdr, sec, NULL);
elf_out:
elf_end(elf);
out:
close(fd);
return found;
}
static int elf_read_program_header(Elf *elf, u64 vaddr, GElf_Phdr *phdr)
{
size_t i, phdrnum;

View file

@ -385,3 +385,8 @@ char *dso__demangle_sym(struct dso *dso __maybe_unused,
{
return NULL;
}
bool filename__has_section(const char *filename __maybe_unused, const char *sec __maybe_unused)
{
return false;
}

View file

@ -165,6 +165,7 @@ int modules__parse(const char *filename, void *arg,
u64 start, u64 size));
int filename__read_debuglink(const char *filename, char *debuglink,
size_t size);
bool filename__has_section(const char *filename, const char *sec);
struct perf_env;
int symbol__init(struct perf_env *env);