2009-03-24 20:58:44 +01:00
|
|
|
#ifndef _ELF_SYMTAB_H_
|
|
|
|
#define _ELF_SYMTAB_H_ 1
|
|
|
|
/*
|
2019-01-15 18:28:24 +01:00
|
|
|
SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
2009-03-24 20:58:44 +01:00
|
|
|
Copyright (C) 2009 Red Hat Inc.
|
|
|
|
Copyright (C) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <gelf.h>
|
|
|
|
#include <elf.h>
|
|
|
|
|
2009-03-25 21:17:25 +01:00
|
|
|
struct elf_symtab {
|
|
|
|
uint32_t nr_syms;
|
|
|
|
Elf_Data *syms;
|
|
|
|
Elf_Data *symstrs;
|
2009-03-25 22:02:48 +01:00
|
|
|
char *name;
|
2009-03-25 21:17:25 +01:00
|
|
|
};
|
2009-03-24 20:58:44 +01:00
|
|
|
|
2009-03-25 22:02:48 +01:00
|
|
|
struct elf_symtab *elf_symtab__new(const char *name, Elf *elf, GElf_Ehdr *ehdr);
|
2014-01-06 20:42:59 +01:00
|
|
|
void elf_symtab__delete(struct elf_symtab *symtab);
|
2009-03-24 20:58:44 +01:00
|
|
|
|
2014-01-06 20:42:59 +01:00
|
|
|
static inline uint32_t elf_symtab__nr_symbols(const struct elf_symtab *symtab)
|
2009-03-25 21:17:25 +01:00
|
|
|
{
|
2014-01-06 20:42:59 +01:00
|
|
|
return symtab->nr_syms;
|
2009-03-25 21:17:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *elf_sym__name(const GElf_Sym *sym,
|
|
|
|
const struct elf_symtab *symtab)
|
|
|
|
{
|
|
|
|
return symtab->symstrs->d_buf + sym->st_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint8_t elf_sym__type(const GElf_Sym *sym)
|
|
|
|
{
|
|
|
|
return GELF_ST_TYPE(sym->st_info);
|
|
|
|
}
|
|
|
|
|
2009-04-03 16:56:03 +02:00
|
|
|
static inline uint16_t elf_sym__section(const GElf_Sym *sym)
|
|
|
|
{
|
|
|
|
return sym->st_shndx;
|
|
|
|
}
|
|
|
|
|
2009-04-01 16:38:37 +02:00
|
|
|
static inline uint8_t elf_sym__bind(const GElf_Sym *sym)
|
|
|
|
{
|
|
|
|
return GELF_ST_BIND(sym->st_info);
|
|
|
|
}
|
|
|
|
|
2009-04-01 19:03:04 +02:00
|
|
|
static inline uint8_t elf_sym__visibility(const GElf_Sym *sym)
|
|
|
|
{
|
|
|
|
return GELF_ST_VISIBILITY(sym->st_other);
|
|
|
|
}
|
|
|
|
|
2009-03-25 21:26:27 +01:00
|
|
|
static inline uint32_t elf_sym__size(const GElf_Sym *sym)
|
|
|
|
{
|
|
|
|
return sym->st_size;
|
|
|
|
}
|
|
|
|
|
2009-03-25 21:17:25 +01:00
|
|
|
static inline uint64_t elf_sym__value(const GElf_Sym *sym)
|
|
|
|
{
|
|
|
|
return sym->st_value;
|
|
|
|
}
|
|
|
|
|
2009-03-28 23:27:00 +01:00
|
|
|
static inline bool elf_sym__is_local_function(const GElf_Sym *sym)
|
|
|
|
{
|
|
|
|
return elf_sym__type(sym) == STT_FUNC &&
|
|
|
|
sym->st_name != 0 &&
|
|
|
|
sym->st_shndx != SHN_UNDEF;
|
|
|
|
}
|
2009-03-24 20:58:44 +01:00
|
|
|
|
2009-03-31 21:04:27 +02:00
|
|
|
static inline bool elf_sym__is_local_object(const GElf_Sym *sym)
|
|
|
|
{
|
|
|
|
return elf_sym__type(sym) == STT_OBJECT &&
|
|
|
|
sym->st_name != 0 &&
|
|
|
|
sym->st_shndx != SHN_UNDEF;
|
|
|
|
}
|
|
|
|
|
2009-03-25 21:17:25 +01:00
|
|
|
/**
|
|
|
|
* elf_symtab__for_each_symbol - iterate thru all the symbols
|
|
|
|
*
|
2014-01-06 20:42:59 +01:00
|
|
|
* @symtab: struct elf_symtab instance to iterate
|
2009-03-25 21:17:25 +01:00
|
|
|
* @index: uint32_t index
|
|
|
|
* @sym: GElf_Sym iterator
|
|
|
|
*/
|
2014-01-06 20:42:59 +01:00
|
|
|
#define elf_symtab__for_each_symbol(symtab, index, sym) \
|
|
|
|
for (index = 0, gelf_getsym(symtab->syms, index, &sym);\
|
|
|
|
index < symtab->nr_syms; \
|
|
|
|
index++, gelf_getsym(symtab->syms, index, &sym))
|
2009-03-24 20:58:44 +01:00
|
|
|
|
|
|
|
#endif /* _ELF_SYMTAB_H_ */
|