Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "absl/debugging/internal/elf_mem_image.h"
00019
00020 #ifdef ABSL_HAVE_ELF_MEM_IMAGE // defined in elf_mem_image.h
00021
00022 #include <string.h>
00023 #include <cassert>
00024 #include <cstddef>
00025 #include "absl/base/internal/raw_logging.h"
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #define VERSYM_VERSION 0x7fff
00039
00040 namespace absl {
00041 namespace debugging_internal {
00042
00043 namespace {
00044
00045 #if __WORDSIZE == 32
00046 const int kElfClass = ELFCLASS32;
00047 int ElfBind(const ElfW(Sym) *symbol) { return ELF32_ST_BIND(symbol->st_info); }
00048 int ElfType(const ElfW(Sym) *symbol) { return ELF32_ST_TYPE(symbol->st_info); }
00049 #elif __WORDSIZE == 64
00050 const int kElfClass = ELFCLASS64;
00051 int ElfBind(const ElfW(Sym) *symbol) { return ELF64_ST_BIND(symbol->st_info); }
00052 int ElfType(const ElfW(Sym) *symbol) { return ELF64_ST_TYPE(symbol->st_info); }
00053 #else
00054 const int kElfClass = -1;
00055 int ElfBind(const ElfW(Sym) *) {
00056 ABSL_RAW_LOG(FATAL, "Unexpected word size");
00057 return 0;
00058 }
00059 int ElfType(const ElfW(Sym) *) {
00060 ABSL_RAW_LOG(FATAL, "Unexpected word size");
00061 return 0;
00062 }
00063 #endif
00064
00065
00066
00067
00068 template <typename T>
00069 const T *GetTableElement(const ElfW(Ehdr) * ehdr, ElfW(Off) table_offset,
00070 ElfW(Word) element_size, size_t index) {
00071 return reinterpret_cast<const T*>(reinterpret_cast<const char *>(ehdr)
00072 + table_offset
00073 + index * element_size);
00074 }
00075
00076 }
00077
00078
00079
00080 const int ElfMemImage::kInvalidBaseSentinel = 0;
00081
00082 ElfMemImage::ElfMemImage(const void *base) {
00083 ABSL_RAW_CHECK(base != kInvalidBase, "bad pointer");
00084 Init(base);
00085 }
00086
00087 int ElfMemImage::GetNumSymbols() const {
00088 if (!hash_) {
00089 return 0;
00090 }
00091
00092 return hash_[1];
00093 }
00094
00095 const ElfW(Sym) *ElfMemImage::GetDynsym(int index) const {
00096 ABSL_RAW_CHECK(index < GetNumSymbols(), "index out of range");
00097 return dynsym_ + index;
00098 }
00099
00100 const ElfW(Versym) *ElfMemImage::GetVersym(int index) const {
00101 ABSL_RAW_CHECK(index < GetNumSymbols(), "index out of range");
00102 return versym_ + index;
00103 }
00104
00105 const ElfW(Phdr) *ElfMemImage::GetPhdr(int index) const {
00106 ABSL_RAW_CHECK(index < ehdr_->e_phnum, "index out of range");
00107 return GetTableElement<ElfW(Phdr)>(ehdr_,
00108 ehdr_->e_phoff,
00109 ehdr_->e_phentsize,
00110 index);
00111 }
00112
00113 const char *ElfMemImage::GetDynstr(ElfW(Word) offset) const {
00114 ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
00115 return dynstr_ + offset;
00116 }
00117
00118 const void *ElfMemImage::GetSymAddr(const ElfW(Sym) *sym) const {
00119 if (sym->st_shndx == SHN_UNDEF || sym->st_shndx >= SHN_LORESERVE) {
00120
00121 return reinterpret_cast<const void *>(sym->st_value);
00122 }
00123 ABSL_RAW_CHECK(link_base_ < sym->st_value, "symbol out of range");
00124 return GetTableElement<char>(ehdr_, 0, 1, sym->st_value - link_base_);
00125 }
00126
00127 const ElfW(Verdef) *ElfMemImage::GetVerdef(int index) const {
00128 ABSL_RAW_CHECK(0 <= index && static_cast<size_t>(index) <= verdefnum_,
00129 "index out of range");
00130 const ElfW(Verdef) *version_definition = verdef_;
00131 while (version_definition->vd_ndx < index && version_definition->vd_next) {
00132 const char *const version_definition_as_char =
00133 reinterpret_cast<const char *>(version_definition);
00134 version_definition =
00135 reinterpret_cast<const ElfW(Verdef) *>(version_definition_as_char +
00136 version_definition->vd_next);
00137 }
00138 return version_definition->vd_ndx == index ? version_definition : nullptr;
00139 }
00140
00141 const ElfW(Verdaux) *ElfMemImage::GetVerdefAux(
00142 const ElfW(Verdef) *verdef) const {
00143 return reinterpret_cast<const ElfW(Verdaux) *>(verdef+1);
00144 }
00145
00146 const char *ElfMemImage::GetVerstr(ElfW(Word) offset) const {
00147 ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
00148 return dynstr_ + offset;
00149 }
00150
00151 void ElfMemImage::Init(const void *base) {
00152 ehdr_ = nullptr;
00153 dynsym_ = nullptr;
00154 dynstr_ = nullptr;
00155 versym_ = nullptr;
00156 verdef_ = nullptr;
00157 hash_ = nullptr;
00158 strsize_ = 0;
00159 verdefnum_ = 0;
00160 link_base_ = ~0L;
00161 if (!base) {
00162 return;
00163 }
00164 const char *const base_as_char = reinterpret_cast<const char *>(base);
00165 if (base_as_char[EI_MAG0] != ELFMAG0 || base_as_char[EI_MAG1] != ELFMAG1 ||
00166 base_as_char[EI_MAG2] != ELFMAG2 || base_as_char[EI_MAG3] != ELFMAG3) {
00167 assert(false);
00168 return;
00169 }
00170 int elf_class = base_as_char[EI_CLASS];
00171 if (elf_class != kElfClass) {
00172 assert(false);
00173 return;
00174 }
00175 switch (base_as_char[EI_DATA]) {
00176 case ELFDATA2LSB: {
00177 if (__LITTLE_ENDIAN != __BYTE_ORDER) {
00178 assert(false);
00179 return;
00180 }
00181 break;
00182 }
00183 case ELFDATA2MSB: {
00184 if (__BIG_ENDIAN != __BYTE_ORDER) {
00185 assert(false);
00186 return;
00187 }
00188 break;
00189 }
00190 default: {
00191 assert(false);
00192 return;
00193 }
00194 }
00195
00196 ehdr_ = reinterpret_cast<const ElfW(Ehdr) *>(base);
00197 const ElfW(Phdr) *dynamic_program_header = nullptr;
00198 for (int i = 0; i < ehdr_->e_phnum; ++i) {
00199 const ElfW(Phdr) *const program_header = GetPhdr(i);
00200 switch (program_header->p_type) {
00201 case PT_LOAD:
00202 if (!~link_base_) {
00203 link_base_ = program_header->p_vaddr;
00204 }
00205 break;
00206 case PT_DYNAMIC:
00207 dynamic_program_header = program_header;
00208 break;
00209 }
00210 }
00211 if (!~link_base_ || !dynamic_program_header) {
00212 assert(false);
00213
00214 Init(nullptr);
00215 return;
00216 }
00217 ptrdiff_t relocation =
00218 base_as_char - reinterpret_cast<const char *>(link_base_);
00219 ElfW(Dyn) *dynamic_entry =
00220 reinterpret_cast<ElfW(Dyn) *>(dynamic_program_header->p_vaddr +
00221 relocation);
00222 for (; dynamic_entry->d_tag != DT_NULL; ++dynamic_entry) {
00223 const ElfW(Xword) value = dynamic_entry->d_un.d_val + relocation;
00224 switch (dynamic_entry->d_tag) {
00225 case DT_HASH:
00226 hash_ = reinterpret_cast<ElfW(Word) *>(value);
00227 break;
00228 case DT_SYMTAB:
00229 dynsym_ = reinterpret_cast<ElfW(Sym) *>(value);
00230 break;
00231 case DT_STRTAB:
00232 dynstr_ = reinterpret_cast<const char *>(value);
00233 break;
00234 case DT_VERSYM:
00235 versym_ = reinterpret_cast<ElfW(Versym) *>(value);
00236 break;
00237 case DT_VERDEF:
00238 verdef_ = reinterpret_cast<ElfW(Verdef) *>(value);
00239 break;
00240 case DT_VERDEFNUM:
00241 verdefnum_ = dynamic_entry->d_un.d_val;
00242 break;
00243 case DT_STRSZ:
00244 strsize_ = dynamic_entry->d_un.d_val;
00245 break;
00246 default:
00247
00248 break;
00249 }
00250 }
00251 if (!hash_ || !dynsym_ || !dynstr_ || !versym_ ||
00252 !verdef_ || !verdefnum_ || !strsize_) {
00253 assert(false);
00254
00255 Init(nullptr);
00256 return;
00257 }
00258 }
00259
00260 bool ElfMemImage::LookupSymbol(const char *name,
00261 const char *version,
00262 int type,
00263 SymbolInfo *info_out) const {
00264 for (const SymbolInfo& info : *this) {
00265 if (strcmp(info.name, name) == 0 && strcmp(info.version, version) == 0 &&
00266 ElfType(info.symbol) == type) {
00267 if (info_out) {
00268 *info_out = info;
00269 }
00270 return true;
00271 }
00272 }
00273 return false;
00274 }
00275
00276 bool ElfMemImage::LookupSymbolByAddress(const void *address,
00277 SymbolInfo *info_out) const {
00278 for (const SymbolInfo& info : *this) {
00279 const char *const symbol_start =
00280 reinterpret_cast<const char *>(info.address);
00281 const char *const symbol_end = symbol_start + info.symbol->st_size;
00282 if (symbol_start <= address && address < symbol_end) {
00283 if (info_out) {
00284
00285 if (ElfBind(info.symbol) == STB_GLOBAL) {
00286
00287 *info_out = info;
00288 return true;
00289 } else {
00290
00291 *info_out = info;
00292 }
00293 } else {
00294
00295 return true;
00296 }
00297 }
00298 }
00299 return false;
00300 }
00301
00302 ElfMemImage::SymbolIterator::SymbolIterator(const void *const image, int index)
00303 : index_(index), image_(image) {
00304 }
00305
00306 const ElfMemImage::SymbolInfo *ElfMemImage::SymbolIterator::operator->() const {
00307 return &info_;
00308 }
00309
00310 const ElfMemImage::SymbolInfo& ElfMemImage::SymbolIterator::operator*() const {
00311 return info_;
00312 }
00313
00314 bool ElfMemImage::SymbolIterator::operator==(const SymbolIterator &rhs) const {
00315 return this->image_ == rhs.image_ && this->index_ == rhs.index_;
00316 }
00317
00318 bool ElfMemImage::SymbolIterator::operator!=(const SymbolIterator &rhs) const {
00319 return !(*this == rhs);
00320 }
00321
00322 ElfMemImage::SymbolIterator &ElfMemImage::SymbolIterator::operator++() {
00323 this->Update(1);
00324 return *this;
00325 }
00326
00327 ElfMemImage::SymbolIterator ElfMemImage::begin() const {
00328 SymbolIterator it(this, 0);
00329 it.Update(0);
00330 return it;
00331 }
00332
00333 ElfMemImage::SymbolIterator ElfMemImage::end() const {
00334 return SymbolIterator(this, GetNumSymbols());
00335 }
00336
00337 void ElfMemImage::SymbolIterator::Update(int increment) {
00338 const ElfMemImage *image = reinterpret_cast<const ElfMemImage *>(image_);
00339 ABSL_RAW_CHECK(image->IsPresent() || increment == 0, "");
00340 if (!image->IsPresent()) {
00341 return;
00342 }
00343 index_ += increment;
00344 if (index_ >= image->GetNumSymbols()) {
00345 index_ = image->GetNumSymbols();
00346 return;
00347 }
00348 const ElfW(Sym) *symbol = image->GetDynsym(index_);
00349 const ElfW(Versym) *version_symbol = image->GetVersym(index_);
00350 ABSL_RAW_CHECK(symbol && version_symbol, "");
00351 const char *const symbol_name = image->GetDynstr(symbol->st_name);
00352 const ElfW(Versym) version_index = version_symbol[0] & VERSYM_VERSION;
00353 const ElfW(Verdef) *version_definition = nullptr;
00354 const char *version_name = "";
00355 if (symbol->st_shndx == SHN_UNDEF) {
00356
00357
00358
00359 } else {
00360 version_definition = image->GetVerdef(version_index);
00361 }
00362 if (version_definition) {
00363
00364
00365 ABSL_RAW_CHECK(
00366 version_definition->vd_cnt == 1 || version_definition->vd_cnt == 2,
00367 "wrong number of entries");
00368 const ElfW(Verdaux) *version_aux = image->GetVerdefAux(version_definition);
00369 version_name = image->GetVerstr(version_aux->vda_name);
00370 }
00371 info_.name = symbol_name;
00372 info_.version = version_name;
00373 info_.address = image->GetSymAddr(symbol);
00374 info_.symbol = symbol;
00375 }
00376
00377 }
00378 }
00379
00380 #endif // ABSL_HAVE_ELF_MEM_IMAGE