elf_mem_image.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Allow dynamic symbol lookup in an in-memory Elf image.
  15. //
  16. #include "absl/debugging/internal/elf_mem_image.h"
  17. #ifdef ABSL_HAVE_ELF_MEM_IMAGE // defined in elf_mem_image.h
  18. #include <string.h>
  19. #include <cassert>
  20. #include <cstddef>
  21. #include "absl/base/config.h"
  22. #include "absl/base/internal/raw_logging.h"
  23. // From binutils/include/elf/common.h (this doesn't appear to be documented
  24. // anywhere else).
  25. //
  26. // /* This flag appears in a Versym structure. It means that the symbol
  27. // is hidden, and is only visible with an explicit version number.
  28. // This is a GNU extension. */
  29. // #define VERSYM_HIDDEN 0x8000
  30. //
  31. // /* This is the mask for the rest of the Versym information. */
  32. // #define VERSYM_VERSION 0x7fff
  33. #define VERSYM_VERSION 0x7fff
  34. namespace absl {
  35. ABSL_NAMESPACE_BEGIN
  36. namespace debugging_internal {
  37. namespace {
  38. #if __SIZEOF_POINTER__ == 4
  39. const int kElfClass = ELFCLASS32;
  40. int ElfBind(const ElfW(Sym) *symbol) { return ELF32_ST_BIND(symbol->st_info); }
  41. int ElfType(const ElfW(Sym) *symbol) { return ELF32_ST_TYPE(symbol->st_info); }
  42. #elif __SIZEOF_POINTER__ == 8
  43. const int kElfClass = ELFCLASS64;
  44. int ElfBind(const ElfW(Sym) *symbol) { return ELF64_ST_BIND(symbol->st_info); }
  45. int ElfType(const ElfW(Sym) *symbol) { return ELF64_ST_TYPE(symbol->st_info); }
  46. #else
  47. const int kElfClass = -1;
  48. int ElfBind(const ElfW(Sym) *) {
  49. ABSL_RAW_LOG(FATAL, "Unexpected word size");
  50. return 0;
  51. }
  52. int ElfType(const ElfW(Sym) *) {
  53. ABSL_RAW_LOG(FATAL, "Unexpected word size");
  54. return 0;
  55. }
  56. #endif
  57. // Extract an element from one of the ELF tables, cast it to desired type.
  58. // This is just a simple arithmetic and a glorified cast.
  59. // Callers are responsible for bounds checking.
  60. template <typename T>
  61. const T *GetTableElement(const ElfW(Ehdr) * ehdr, ElfW(Off) table_offset,
  62. ElfW(Word) element_size, size_t index) {
  63. return reinterpret_cast<const T*>(reinterpret_cast<const char *>(ehdr)
  64. + table_offset
  65. + index * element_size);
  66. }
  67. } // namespace
  68. // The value of this variable doesn't matter; it's used only for its
  69. // unique address.
  70. const int ElfMemImage::kInvalidBaseSentinel = 0;
  71. ElfMemImage::ElfMemImage(const void *base) {
  72. ABSL_RAW_CHECK(base != kInvalidBase, "bad pointer");
  73. Init(base);
  74. }
  75. int ElfMemImage::GetNumSymbols() const {
  76. if (!hash_) {
  77. return 0;
  78. }
  79. // See http://www.caldera.com/developers/gabi/latest/ch5.dynamic.html#hash
  80. return static_cast<int>(hash_[1]);
  81. }
  82. const ElfW(Sym) *ElfMemImage::GetDynsym(int index) const {
  83. ABSL_RAW_CHECK(index < GetNumSymbols(), "index out of range");
  84. return dynsym_ + index;
  85. }
  86. const ElfW(Versym) *ElfMemImage::GetVersym(int index) const {
  87. ABSL_RAW_CHECK(index < GetNumSymbols(), "index out of range");
  88. return versym_ + index;
  89. }
  90. const ElfW(Phdr) *ElfMemImage::GetPhdr(int index) const {
  91. ABSL_RAW_CHECK(index >= 0 && index < ehdr_->e_phnum, "index out of range");
  92. return GetTableElement<ElfW(Phdr)>(ehdr_, ehdr_->e_phoff, ehdr_->e_phentsize,
  93. static_cast<size_t>(index));
  94. }
  95. const char *ElfMemImage::GetDynstr(ElfW(Word) offset) const {
  96. ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
  97. return dynstr_ + offset;
  98. }
  99. const void *ElfMemImage::GetSymAddr(const ElfW(Sym) *sym) const {
  100. if (sym->st_shndx == SHN_UNDEF || sym->st_shndx >= SHN_LORESERVE) {
  101. // Symbol corresponds to "special" (e.g. SHN_ABS) section.
  102. return reinterpret_cast<const void *>(sym->st_value);
  103. }
  104. ABSL_RAW_CHECK(link_base_ < sym->st_value, "symbol out of range");
  105. return GetTableElement<char>(ehdr_, 0, 1, sym->st_value - link_base_);
  106. }
  107. const ElfW(Verdef) *ElfMemImage::GetVerdef(int index) const {
  108. ABSL_RAW_CHECK(0 <= index && static_cast<size_t>(index) <= verdefnum_,
  109. "index out of range");
  110. const ElfW(Verdef) *version_definition = verdef_;
  111. while (version_definition->vd_ndx < index && version_definition->vd_next) {
  112. const char *const version_definition_as_char =
  113. reinterpret_cast<const char *>(version_definition);
  114. version_definition =
  115. reinterpret_cast<const ElfW(Verdef) *>(version_definition_as_char +
  116. version_definition->vd_next);
  117. }
  118. return version_definition->vd_ndx == index ? version_definition : nullptr;
  119. }
  120. const ElfW(Verdaux) *ElfMemImage::GetVerdefAux(
  121. const ElfW(Verdef) *verdef) const {
  122. return reinterpret_cast<const ElfW(Verdaux) *>(verdef+1);
  123. }
  124. const char *ElfMemImage::GetVerstr(ElfW(Word) offset) const {
  125. ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
  126. return dynstr_ + offset;
  127. }
  128. void ElfMemImage::Init(const void *base) {
  129. ehdr_ = nullptr;
  130. dynsym_ = nullptr;
  131. dynstr_ = nullptr;
  132. versym_ = nullptr;
  133. verdef_ = nullptr;
  134. hash_ = nullptr;
  135. strsize_ = 0;
  136. verdefnum_ = 0;
  137. // Sentinel: PT_LOAD .p_vaddr can't possibly be this.
  138. link_base_ = ~ElfW(Addr){0}; // NOLINT(readability/braces)
  139. if (!base) {
  140. return;
  141. }
  142. const char *const base_as_char = reinterpret_cast<const char *>(base);
  143. if (base_as_char[EI_MAG0] != ELFMAG0 || base_as_char[EI_MAG1] != ELFMAG1 ||
  144. base_as_char[EI_MAG2] != ELFMAG2 || base_as_char[EI_MAG3] != ELFMAG3) {
  145. assert(false);
  146. return;
  147. }
  148. int elf_class = base_as_char[EI_CLASS];
  149. if (elf_class != kElfClass) {
  150. assert(false);
  151. return;
  152. }
  153. switch (base_as_char[EI_DATA]) {
  154. case ELFDATA2LSB: {
  155. #ifndef ABSL_IS_LITTLE_ENDIAN
  156. assert(false);
  157. return;
  158. #endif
  159. break;
  160. }
  161. case ELFDATA2MSB: {
  162. #ifndef ABSL_IS_BIG_ENDIAN
  163. assert(false);
  164. return;
  165. #endif
  166. break;
  167. }
  168. default: {
  169. assert(false);
  170. return;
  171. }
  172. }
  173. ehdr_ = reinterpret_cast<const ElfW(Ehdr) *>(base);
  174. const ElfW(Phdr) *dynamic_program_header = nullptr;
  175. for (int i = 0; i < ehdr_->e_phnum; ++i) {
  176. const ElfW(Phdr) *const program_header = GetPhdr(i);
  177. switch (program_header->p_type) {
  178. case PT_LOAD:
  179. if (!~link_base_) {
  180. link_base_ = program_header->p_vaddr;
  181. }
  182. break;
  183. case PT_DYNAMIC:
  184. dynamic_program_header = program_header;
  185. break;
  186. }
  187. }
  188. if (!~link_base_ || !dynamic_program_header) {
  189. assert(false);
  190. // Mark this image as not present. Can not recur infinitely.
  191. Init(nullptr);
  192. return;
  193. }
  194. ptrdiff_t relocation =
  195. base_as_char - reinterpret_cast<const char *>(link_base_);
  196. ElfW(Dyn)* dynamic_entry = reinterpret_cast<ElfW(Dyn)*>(
  197. static_cast<intptr_t>(dynamic_program_header->p_vaddr) + relocation);
  198. for (; dynamic_entry->d_tag != DT_NULL; ++dynamic_entry) {
  199. const auto value =
  200. static_cast<intptr_t>(dynamic_entry->d_un.d_val) + relocation;
  201. switch (dynamic_entry->d_tag) {
  202. case DT_HASH:
  203. hash_ = reinterpret_cast<ElfW(Word) *>(value);
  204. break;
  205. case DT_SYMTAB:
  206. dynsym_ = reinterpret_cast<ElfW(Sym) *>(value);
  207. break;
  208. case DT_STRTAB:
  209. dynstr_ = reinterpret_cast<const char *>(value);
  210. break;
  211. case DT_VERSYM:
  212. versym_ = reinterpret_cast<ElfW(Versym) *>(value);
  213. break;
  214. case DT_VERDEF:
  215. verdef_ = reinterpret_cast<ElfW(Verdef) *>(value);
  216. break;
  217. case DT_VERDEFNUM:
  218. verdefnum_ = static_cast<size_t>(dynamic_entry->d_un.d_val);
  219. break;
  220. case DT_STRSZ:
  221. strsize_ = static_cast<size_t>(dynamic_entry->d_un.d_val);
  222. break;
  223. default:
  224. // Unrecognized entries explicitly ignored.
  225. break;
  226. }
  227. }
  228. if (!hash_ || !dynsym_ || !dynstr_ || !versym_ ||
  229. !verdef_ || !verdefnum_ || !strsize_) {
  230. assert(false); // invalid VDSO
  231. // Mark this image as not present. Can not recur infinitely.
  232. Init(nullptr);
  233. return;
  234. }
  235. }
  236. bool ElfMemImage::LookupSymbol(const char *name,
  237. const char *version,
  238. int type,
  239. SymbolInfo *info_out) const {
  240. for (const SymbolInfo& info : *this) {
  241. if (strcmp(info.name, name) == 0 && strcmp(info.version, version) == 0 &&
  242. ElfType(info.symbol) == type) {
  243. if (info_out) {
  244. *info_out = info;
  245. }
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. bool ElfMemImage::LookupSymbolByAddress(const void *address,
  252. SymbolInfo *info_out) const {
  253. for (const SymbolInfo& info : *this) {
  254. const char *const symbol_start =
  255. reinterpret_cast<const char *>(info.address);
  256. const char *const symbol_end = symbol_start + info.symbol->st_size;
  257. if (symbol_start <= address && address < symbol_end) {
  258. if (info_out) {
  259. // Client wants to know details for that symbol (the usual case).
  260. if (ElfBind(info.symbol) == STB_GLOBAL) {
  261. // Strong symbol; just return it.
  262. *info_out = info;
  263. return true;
  264. } else {
  265. // Weak or local. Record it, but keep looking for a strong one.
  266. *info_out = info;
  267. }
  268. } else {
  269. // Client only cares if there is an overlapping symbol.
  270. return true;
  271. }
  272. }
  273. }
  274. return false;
  275. }
  276. ElfMemImage::SymbolIterator::SymbolIterator(const void *const image, int index)
  277. : index_(index), image_(image) {
  278. }
  279. const ElfMemImage::SymbolInfo *ElfMemImage::SymbolIterator::operator->() const {
  280. return &info_;
  281. }
  282. const ElfMemImage::SymbolInfo& ElfMemImage::SymbolIterator::operator*() const {
  283. return info_;
  284. }
  285. bool ElfMemImage::SymbolIterator::operator==(const SymbolIterator &rhs) const {
  286. return this->image_ == rhs.image_ && this->index_ == rhs.index_;
  287. }
  288. bool ElfMemImage::SymbolIterator::operator!=(const SymbolIterator &rhs) const {
  289. return !(*this == rhs);
  290. }
  291. ElfMemImage::SymbolIterator &ElfMemImage::SymbolIterator::operator++() {
  292. this->Update(1);
  293. return *this;
  294. }
  295. ElfMemImage::SymbolIterator ElfMemImage::begin() const {
  296. SymbolIterator it(this, 0);
  297. it.Update(0);
  298. return it;
  299. }
  300. ElfMemImage::SymbolIterator ElfMemImage::end() const {
  301. return SymbolIterator(this, GetNumSymbols());
  302. }
  303. void ElfMemImage::SymbolIterator::Update(int increment) {
  304. const ElfMemImage *image = reinterpret_cast<const ElfMemImage *>(image_);
  305. ABSL_RAW_CHECK(image->IsPresent() || increment == 0, "");
  306. if (!image->IsPresent()) {
  307. return;
  308. }
  309. index_ += increment;
  310. if (index_ >= image->GetNumSymbols()) {
  311. index_ = image->GetNumSymbols();
  312. return;
  313. }
  314. const ElfW(Sym) *symbol = image->GetDynsym(index_);
  315. const ElfW(Versym) *version_symbol = image->GetVersym(index_);
  316. ABSL_RAW_CHECK(symbol && version_symbol, "");
  317. const char *const symbol_name = image->GetDynstr(symbol->st_name);
  318. #if defined(__NetBSD__)
  319. const int version_index = version_symbol->vs_vers & VERSYM_VERSION;
  320. #else
  321. const ElfW(Versym) version_index = version_symbol[0] & VERSYM_VERSION;
  322. #endif
  323. const ElfW(Verdef) *version_definition = nullptr;
  324. const char *version_name = "";
  325. if (symbol->st_shndx == SHN_UNDEF) {
  326. // Undefined symbols reference DT_VERNEED, not DT_VERDEF, and
  327. // version_index could well be greater than verdefnum_, so calling
  328. // GetVerdef(version_index) may trigger assertion.
  329. } else {
  330. version_definition = image->GetVerdef(version_index);
  331. }
  332. if (version_definition) {
  333. // I am expecting 1 or 2 auxiliary entries: 1 for the version itself,
  334. // optional 2nd if the version has a parent.
  335. ABSL_RAW_CHECK(
  336. version_definition->vd_cnt == 1 || version_definition->vd_cnt == 2,
  337. "wrong number of entries");
  338. const ElfW(Verdaux) *version_aux = image->GetVerdefAux(version_definition);
  339. version_name = image->GetVerstr(version_aux->vda_name);
  340. }
  341. info_.name = symbol_name;
  342. info_.version = version_name;
  343. info_.address = image->GetSymAddr(symbol);
  344. info_.symbol = symbol;
  345. }
  346. } // namespace debugging_internal
  347. ABSL_NAMESPACE_END
  348. } // namespace absl
  349. #endif // ABSL_HAVE_ELF_MEM_IMAGE