Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
06ea72a4 ea335ef3

+34
+28
tools/perf/util/symbol-elf.c
··· 233 233 return NULL; 234 234 } 235 235 236 + bool filename__has_section(const char *filename, const char *sec) 237 + { 238 + int fd; 239 + Elf *elf; 240 + GElf_Ehdr ehdr; 241 + GElf_Shdr shdr; 242 + bool found = false; 243 + 244 + fd = open(filename, O_RDONLY); 245 + if (fd < 0) 246 + return false; 247 + 248 + elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 249 + if (elf == NULL) 250 + goto out; 251 + 252 + if (gelf_getehdr(elf, &ehdr) == NULL) 253 + goto elf_out; 254 + 255 + found = !!elf_section_by_name(elf, &ehdr, &shdr, sec, NULL); 256 + 257 + elf_out: 258 + elf_end(elf); 259 + out: 260 + close(fd); 261 + return found; 262 + } 263 + 236 264 static int elf_read_program_header(Elf *elf, u64 vaddr, GElf_Phdr *phdr) 237 265 { 238 266 size_t i, phdrnum;
+5
tools/perf/util/symbol-minimal.c
··· 385 385 { 386 386 return NULL; 387 387 } 388 + 389 + bool filename__has_section(const char *filename __maybe_unused, const char *sec __maybe_unused) 390 + { 391 + return false; 392 + }
+1
tools/perf/util/symbol.h
··· 165 165 u64 start, u64 size)); 166 166 int filename__read_debuglink(const char *filename, char *debuglink, 167 167 size_t size); 168 + bool filename__has_section(const char *filename, const char *sec); 168 169 169 170 struct perf_env; 170 171 int symbol__init(struct perf_env *env);