You've already forked linux-apfs
mirror of
https://github.com/linux-apfs/linux-apfs.git
synced 2026-05-01 15:00:59 -07:00
perf_counter: Implement dso__load using libelf
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Paul Mackerras <paulus@samba.org> Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com> Cc: Marcelo Tosatti <mtosatti@redhat.com> Cc: Thomas Gleixner <tglx@linutronix.de> LKML-Reference: <new-submission> Signed-off-by: Ingo Molnar <mingo@elte.hu>
This commit is contained in:
committed by
Ingo Molnar
parent
fd4242bb35
commit
62eb93905b
@@ -160,7 +160,7 @@ uname_V := $(shell sh -c 'uname -v 2>/dev/null || echo not')
|
|||||||
# CFLAGS and LDFLAGS are for the users to override from the command line.
|
# CFLAGS and LDFLAGS are for the users to override from the command line.
|
||||||
|
|
||||||
CFLAGS = -g -O2 -Wall
|
CFLAGS = -g -O2 -Wall
|
||||||
LDFLAGS = -lpthread -lrt
|
LDFLAGS = -lpthread -lrt -lelf
|
||||||
ALL_CFLAGS = $(CFLAGS)
|
ALL_CFLAGS = $(CFLAGS)
|
||||||
ALL_LDFLAGS = $(LDFLAGS)
|
ALL_LDFLAGS = $(LDFLAGS)
|
||||||
STRIP ?= strip
|
STRIP ?= strip
|
||||||
|
|||||||
@@ -8,6 +8,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <gelf.h>
|
||||||
|
#include <elf.h>
|
||||||
|
#include <libelf.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@@ -195,10 +198,123 @@ static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* elf_symtab__for_each_symbol - iterate thru all the symbols
|
||||||
|
*
|
||||||
|
* @self: struct elf_symtab instance to iterate
|
||||||
|
* @index: uint32_t index
|
||||||
|
* @sym: GElf_Sym iterator
|
||||||
|
*/
|
||||||
|
#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
|
||||||
|
for (index = 0, gelf_getsym(syms, index, &sym);\
|
||||||
|
index < nr_syms; \
|
||||||
|
index++, gelf_getsym(syms, index, &sym))
|
||||||
|
|
||||||
|
static inline uint8_t elf_sym__type(const GElf_Sym *sym)
|
||||||
|
{
|
||||||
|
return GELF_ST_TYPE(sym->st_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool elf_sym__is_function(const GElf_Sym *sym)
|
||||||
|
{
|
||||||
|
return elf_sym__type(sym) == STT_FUNC &&
|
||||||
|
sym->st_name != 0 &&
|
||||||
|
sym->st_shndx != SHN_UNDEF;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline const char *elf_sym__name(const GElf_Sym *sym,
|
||||||
|
const Elf_Data *symstrs)
|
||||||
|
{
|
||||||
|
return symstrs->d_buf + sym->st_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
|
||||||
|
GElf_Shdr *shp, const char *name,
|
||||||
|
size_t *index)
|
||||||
|
{
|
||||||
|
Elf_Scn *sec = NULL;
|
||||||
|
size_t cnt = 1;
|
||||||
|
|
||||||
|
while ((sec = elf_nextscn(elf, sec)) != NULL) {
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
gelf_getshdr(sec, shp);
|
||||||
|
str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
|
||||||
|
if (!strcmp(name, str)) {
|
||||||
|
if (index)
|
||||||
|
*index = cnt;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sec;
|
||||||
|
}
|
||||||
|
|
||||||
static int dso__load(struct dso *self)
|
static int dso__load(struct dso *self)
|
||||||
{
|
{
|
||||||
/* FIXME */
|
int fd = open(self->name, O_RDONLY), err = -1;
|
||||||
return 0;
|
|
||||||
|
if (fd == -1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
|
||||||
|
if (elf == NULL) {
|
||||||
|
fprintf(stderr, "%s: cannot read %s ELF file.\n",
|
||||||
|
__func__, self->name);
|
||||||
|
goto out_close;
|
||||||
|
}
|
||||||
|
|
||||||
|
GElf_Ehdr ehdr;
|
||||||
|
if (gelf_getehdr(elf, &ehdr) == NULL) {
|
||||||
|
fprintf(stderr, "%s: cannot get elf header.\n", __func__);
|
||||||
|
goto out_elf_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
GElf_Shdr shdr;
|
||||||
|
Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
|
||||||
|
if (sec == NULL)
|
||||||
|
sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
|
||||||
|
|
||||||
|
if (sec == NULL)
|
||||||
|
goto out_elf_end;
|
||||||
|
|
||||||
|
if (gelf_getshdr(sec, &shdr) == NULL)
|
||||||
|
goto out_elf_end;
|
||||||
|
|
||||||
|
Elf_Data *syms = elf_getdata(sec, NULL);
|
||||||
|
if (syms == NULL)
|
||||||
|
goto out_elf_end;
|
||||||
|
|
||||||
|
sec = elf_getscn(elf, shdr.sh_link);
|
||||||
|
if (sec == NULL)
|
||||||
|
goto out_elf_end;
|
||||||
|
|
||||||
|
Elf_Data *symstrs = elf_getdata(sec, NULL);
|
||||||
|
if (symstrs == NULL)
|
||||||
|
goto out_elf_end;
|
||||||
|
|
||||||
|
const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
|
||||||
|
|
||||||
|
GElf_Sym sym;
|
||||||
|
uint32_t index;
|
||||||
|
elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
|
||||||
|
if (!elf_sym__is_function(&sym))
|
||||||
|
continue;
|
||||||
|
struct symbol *f = symbol__new(sym.st_value, sym.st_size,
|
||||||
|
elf_sym__name(&sym, symstrs));
|
||||||
|
if (f == NULL)
|
||||||
|
goto out_elf_end;
|
||||||
|
|
||||||
|
dso__insert_symbol(self, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = 0;
|
||||||
|
out_elf_end:
|
||||||
|
elf_end(elf);
|
||||||
|
out_close:
|
||||||
|
close(fd);
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t dso__fprintf(struct dso *self, FILE *fp)
|
static size_t dso__fprintf(struct dso *self, FILE *fp)
|
||||||
@@ -614,6 +730,8 @@ int cmd_report(int argc, char **argv)
|
|||||||
int ret, rc = EXIT_FAILURE;
|
int ret, rc = EXIT_FAILURE;
|
||||||
unsigned long total = 0;
|
unsigned long total = 0;
|
||||||
|
|
||||||
|
elf_version(EV_CURRENT);
|
||||||
|
|
||||||
page_size = getpagesize();
|
page_size = getpagesize();
|
||||||
|
|
||||||
process_options(argc, argv);
|
process_options(argc, argv);
|
||||||
|
|||||||
Reference in New Issue
Block a user