20 #ifdef ABSL_HAVE_ELF_MEM_IMAGE // defined in elf_mem_image.h 38 #define VERSYM_VERSION 0x7fff 41 namespace debugging_internal {
46 const int kElfClass = ELFCLASS32;
47 int ElfBind(
const ElfW(Sym) *symbol) {
return ELF32_ST_BIND(symbol->st_info); }
48 int ElfType(
const ElfW(Sym) *symbol) {
return ELF32_ST_TYPE(symbol->st_info); }
49 #elif __WORDSIZE == 64 50 const int kElfClass = ELFCLASS64;
51 int ElfBind(
const ElfW(Sym) *symbol) {
return ELF64_ST_BIND(symbol->st_info); }
52 int ElfType(
const ElfW(Sym) *symbol) {
return ELF64_ST_TYPE(symbol->st_info); }
54 const int kElfClass = -1;
55 int ElfBind(
const ElfW(Sym) *) {
59 int ElfType(
const ElfW(Sym) *) {
69 const T *GetTableElement(
const ElfW(Ehdr) * ehdr, ElfW(Off) table_offset,
70 ElfW(Word) element_size,
size_t index) {
71 return reinterpret_cast<const T*
>(
reinterpret_cast<const char *
>(ehdr)
73 + index * element_size);
80 const int ElfMemImage::kInvalidBaseSentinel = 0;
82 ElfMemImage::ElfMemImage(
const void *base) {
87 int ElfMemImage::GetNumSymbols()
const {
95 const ElfW(Sym) *ElfMemImage::GetDynsym(
int index)
const {
97 return dynsym_ + index;
100 const ElfW(Versym) *ElfMemImage::GetVersym(
int index)
const {
102 return versym_ + index;
105 const ElfW(Phdr) *ElfMemImage::GetPhdr(
int index)
const {
107 return GetTableElement<ElfW(Phdr)>(ehdr_,
113 const char *ElfMemImage::GetDynstr(ElfW(Word) offset)
const {
115 return dynstr_ + offset;
118 const void *ElfMemImage::GetSymAddr(
const ElfW(Sym) *sym)
const {
119 if (sym->st_shndx == SHN_UNDEF || sym->st_shndx >= SHN_LORESERVE) {
121 return reinterpret_cast<const void *
>(sym->st_value);
123 ABSL_RAW_CHECK(link_base_ < sym->st_value,
"symbol out of range");
124 return GetTableElement<char>(ehdr_, 0, 1, sym->st_value - link_base_);
127 const ElfW(Verdef) *ElfMemImage::GetVerdef(
int index)
const {
128 ABSL_RAW_CHECK(0 <= index && static_cast<size_t>(index) <= verdefnum_,
129 "index out of range");
130 const ElfW(Verdef) *version_definition = verdef_;
131 while (version_definition->vd_ndx < index && version_definition->vd_next) {
132 const char *
const version_definition_as_char =
133 reinterpret_cast<const char *
>(version_definition);
135 reinterpret_cast<const ElfW(Verdef) *
>(version_definition_as_char +
136 version_definition->vd_next);
138 return version_definition->vd_ndx == index ? version_definition :
nullptr;
141 const ElfW(Verdaux) *ElfMemImage::GetVerdefAux(
142 const ElfW(Verdef) *verdef)
const {
143 return reinterpret_cast<const ElfW(Verdaux) *
>(verdef+1);
146 const char *ElfMemImage::GetVerstr(ElfW(Word) offset)
const {
148 return dynstr_ + offset;
151 void ElfMemImage::Init(
const void *base) {
164 const char *
const base_as_char =
reinterpret_cast<const char *
>(base);
165 if (base_as_char[EI_MAG0] != ELFMAG0 || base_as_char[EI_MAG1] != ELFMAG1 ||
166 base_as_char[EI_MAG2] != ELFMAG2 || base_as_char[EI_MAG3] != ELFMAG3) {
170 int elf_class = base_as_char[EI_CLASS];
171 if (elf_class != kElfClass) {
175 switch (base_as_char[EI_DATA]) {
177 if (__LITTLE_ENDIAN != __BYTE_ORDER) {
184 if (__BIG_ENDIAN != __BYTE_ORDER) {
196 ehdr_ =
reinterpret_cast<const ElfW(Ehdr) *
>(base);
197 const ElfW(Phdr) *dynamic_program_header =
nullptr;
198 for (
int i = 0;
i < ehdr_->e_phnum; ++
i) {
199 const ElfW(Phdr) *
const program_header = GetPhdr(
i);
200 switch (program_header->p_type) {
203 link_base_ = program_header->p_vaddr;
207 dynamic_program_header = program_header;
211 if (!~link_base_ || !dynamic_program_header) {
217 ptrdiff_t relocation =
218 base_as_char -
reinterpret_cast<const char *
>(link_base_);
219 ElfW(Dyn) *dynamic_entry =
220 reinterpret_cast<ElfW(Dyn) *
>(dynamic_program_header->p_vaddr +
222 for (; dynamic_entry->d_tag != DT_NULL; ++dynamic_entry) {
223 const ElfW(Xword)
value = dynamic_entry->d_un.d_val + relocation;
224 switch (dynamic_entry->d_tag) {
226 hash_ =
reinterpret_cast<ElfW(Word) *
>(
value);
229 dynsym_ =
reinterpret_cast<ElfW(Sym) *
>(
value);
232 dynstr_ =
reinterpret_cast<const char *
>(
value);
235 versym_ =
reinterpret_cast<ElfW(Versym) *
>(
value);
238 verdef_ =
reinterpret_cast<ElfW(Verdef) *
>(
value);
241 verdefnum_ = dynamic_entry->d_un.d_val;
244 strsize_ = dynamic_entry->d_un.d_val;
251 if (!hash_ || !dynsym_ || !dynstr_ || !versym_ ||
252 !verdef_ || !verdefnum_ || !strsize_) {
260 bool ElfMemImage::LookupSymbol(
const char *
name,
263 SymbolInfo *info_out)
const {
264 for (
const SymbolInfo& info : *
this) {
265 if (strcmp(info.name, name) == 0 && strcmp(info.version, version) == 0 &&
266 ElfType(info.symbol) == type) {
276 bool ElfMemImage::LookupSymbolByAddress(
const void *address,
277 SymbolInfo *info_out)
const {
278 for (
const SymbolInfo& info : *
this) {
279 const char *
const symbol_start =
280 reinterpret_cast<const char *
>(info.address);
281 const char *
const symbol_end = symbol_start + info.symbol->st_size;
282 if (symbol_start <= address && address < symbol_end) {
285 if (ElfBind(info.symbol) == STB_GLOBAL) {
302 ElfMemImage::SymbolIterator::SymbolIterator(
const void *
const image,
int index)
303 : index_(index), image_(image) {
306 const ElfMemImage::SymbolInfo *ElfMemImage::SymbolIterator::operator->()
const {
315 return this->image_ == rhs.image_ && this->index_ == rhs.index_;
319 return !(*
this == rhs);
322 ElfMemImage::SymbolIterator &ElfMemImage::SymbolIterator::operator++() {
328 SymbolIterator it(
this, 0);
334 return SymbolIterator(
this, GetNumSymbols());
337 void ElfMemImage::SymbolIterator::Update(
int increment) {
338 const ElfMemImage *image =
reinterpret_cast<const ElfMemImage *
>(image_);
340 if (!image->IsPresent()) {
344 if (index_ >= image->GetNumSymbols()) {
345 index_ = image->GetNumSymbols();
348 const ElfW(Sym) *symbol = image->GetDynsym(index_);
349 const ElfW(Versym) *version_symbol = image->GetVersym(index_);
351 const char *
const symbol_name = image->GetDynstr(symbol->st_name);
352 const ElfW(Versym) version_index = version_symbol[0] & VERSYM_VERSION;
353 const ElfW(Verdef) *version_definition =
nullptr;
354 const
char *version_name = "";
355 if (symbol->st_shndx == SHN_UNDEF) {
360 version_definition = image->GetVerdef(version_index);
362 if (version_definition) {
366 version_definition->vd_cnt == 1 || version_definition->vd_cnt == 2,
367 "wrong number of entries");
368 const ElfW(Verdaux) *version_aux = image->GetVerdefAux(version_definition);
369 version_name = image->GetVerstr(version_aux->vda_name);
371 info_.name = symbol_name;
372 info_.version = version_name;
373 info_.address = image->GetSymAddr(symbol);
374 info_.symbol = symbol;
380 #endif // ABSL_HAVE_ELF_MEM_IMAGE
#define ABSL_RAW_LOG(severity,...)
uint128 operator*(uint128 lhs, uint128 rhs)
#define ABSL_RAW_CHECK(condition, message)
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
bool operator!=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)