elf_mem_image.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 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 < ehdr_->e_phnum, "index out of range");
  92. return GetTableElement<ElfW(Phdr)>(ehdr_,
  93. ehdr_->e_phoff,
  94. ehdr_->e_phentsize,
  95. index);
  96. }
  97. const char *ElfMemImage::GetDynstr(ElfW(Word) offset) const {
  98. ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
  99. return dynstr_ + offset;
  100. }
  101. const void *ElfMemImage::GetSymAddr(const ElfW(Sym) *sym) const {
  102. if (sym->st_shndx == SHN_UNDEF || sym->st_shndx >= SHN_LORESERVE) {
  103. // Symbol corresponds to "special" (e.g. SHN_ABS) section.
  104. return reinterpret_cast<const void *>(sym->st_value);
  105. }
  106. ABSL_RAW_CHECK(link_base_ < sym->st_value, "symbol out of range");
  107. return GetTableElement<char>(ehdr_, 0, 1, sym->st_value - link_base_);
  108. }
  109. const ElfW(Verdef) *ElfMemImage::GetVerdef(int index) const {
  110. ABSL_RAW_CHECK(0 <= index && static_cast<size_t>(index) <= verdefnum_,
  111. "index out of range");
  112. const ElfW(Verdef) *version_definition = verdef_;
  113. while (version_definition->vd_ndx < index && version_definition->vd_next) {
  114. const char *const version_definition_as_char =
  115. reinterpret_cast<const char *>(version_definition);
  116. version_definition =
  117. reinterpret_cast<const ElfW(Verdef) *>(version_definition_as_char +
  118. version_definition->vd_next);
  119. }
  120. return version_definition->vd_ndx == index ? version_definition : nullptr;
  121. }
  122. const ElfW(Verdaux) *ElfMemImage::GetVerdefAux(
  123. const ElfW(Verdef) *verdef) const {
  124. return reinterpret_cast<const ElfW(Verdaux) *>(verdef+1);
  125. }
  126. const char *ElfMemImage::GetVerstr(ElfW(Word) offset) const {
  127. ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
  128. return dynstr_ + offset;
  129. }
  130. void ElfMemImage::Init(const void *base) {
  131. ehdr_ = nullptr;
  132. dynsym_ = nullptr;
  133. dynstr_ = nullptr;
  134. versym_ = nullptr;
  135. verdef_ = nullptr;
  136. hash_ = nullptr;
  137. strsize_ = 0;
  138. verdefnum_ = 0;
  139. link_base_ = ~0L; // Sentinel: PT_LOAD .p_vaddr can't possibly be this.
  140. if (!base) {
  141. return;
  142. }
  143. const char *const base_as_char = reinterpret_cast<const char *>(base);
  144. if (base_as_char[EI_MAG0] != ELFMAG0 || base_as_char[EI_MAG1] != ELFMAG1 ||
  145. base_as_char[EI_MAG2] != ELFMAG2 || base_as_char[EI_MAG3] != ELFMAG3) {
  146. assert(false);
  147. return;
  148. }
  149. int elf_class = base_as_char[EI_CLASS];
  150. if (elf_class != kElfClass) {
  151. assert(false);
  152. return;
  153. }
  154. switch (base_as_char[EI_DATA]) {
  155. case ELFDATA2LSB: {
  156. #ifndef ABSL_IS_LITTLE_ENDIAN
  157. assert(false);
  158. return;
  159. #endif
  160. break;
  161. }
  162. case ELFDATA2MSB: {
  163. #ifndef ABSL_IS_BIG_ENDIAN
  164. assert(false);
  165. return;
  166. #endif
  167. break;
  168. }
  169. default: {
  170. assert(false);
  171. return;
  172. }
  173. }
  174. ehdr_ = reinterpret_cast<const ElfW(Ehdr) *>(base);
  175. const ElfW(Phdr) *dynamic_program_header = nullptr;
  176. for (int i = 0; i < ehdr_->e_phnum; ++i) {
  177. const ElfW(Phdr) *const program_header = GetPhdr(i);
  178. switch (program_header->p_type) {
  179. case PT_LOAD:
  180. if (!~link_base_) {
  181. link_base_ = program_header->p_vaddr;
  182. }
  183. break;
  184. case PT_DYNAMIC:
  185. dynamic_program_header = program_header;
  186. break;
  187. }
  188. }
  189. if (!~link_base_ || !dynamic_program_header) {
  190. assert(false);
  191. // Mark this image as not present. Can not recur infinitely.
  192. Init(nullptr);
  193. return;
  194. }
  195. ptrdiff_t relocation =
  196. base_as_char - reinterpret_cast<const char *>(link_base_);
  197. ElfW(Dyn) *dynamic_entry =
  198. reinterpret_cast<ElfW(Dyn) *>(dynamic_program_header->p_vaddr +
  199. relocation);
  200. for (; dynamic_entry->d_tag != DT_NULL; ++dynamic_entry) {
  201. const auto value = dynamic_entry->d_un.d_val + relocation;
  202. switch (dynamic_entry->d_tag) {
  203. case DT_HASH:
  204. hash_ = reinterpret_cast<ElfW(Word) *>(value);
  205. break;
  206. case DT_SYMTAB:
  207. dynsym_ = reinterpret_cast<ElfW(Sym) *>(value);
  208. break;
  209. case DT_STRTAB:
  210. dynstr_ = reinterpret_cast<const char *>(value);
  211. break;
  212. case DT_VERSYM:
  213. versym_ = reinterpret_cast<ElfW(Versym) *>(value);
  214. break;
  215. case DT_VERDEF:
  216. verdef_ = reinterpret_cast<ElfW(Verdef) *>(value);
  217. break;
  218. case DT_VERDEFNUM:
  219. verdefnum_ = dynamic_entry->d_un.d_val;
  220. break;
  221. case DT_STRSZ:
  222. strsize_ = dynamic_entry->d_un.d_val;
  223. break;
  224. default:
  225. // Unrecognized entries explicitly ignored.
  226. break;
  227. }
  228. }
  229. if (!hash_ || !dynsym_ || !dynstr_ || !versym_ ||
  230. !verdef_ || !verdefnum_ || !strsize_) {
  231. assert(false); // invalid VDSO
  232. // Mark this image as not present. Can not recur infinitely.
  233. Init(nullptr);
  234. return;
  235. }
  236. }
  237. bool ElfMemImage::LookupSymbol(const char *name,
  238. const char *version,
  239. int type,
  240. SymbolInfo *info_out) const {
  241. for (const SymbolInfo& info : *this) {
  242. if (strcmp(info.name, name) == 0 && strcmp(info.version, version) == 0 &&
  243. ElfType(info.symbol) == type) {
  244. if (info_out) {
  245. *info_out = info;
  246. }
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. bool ElfMemImage::LookupSymbolByAddress(const void *address,
  253. SymbolInfo *info_out) const {
  254. for (const SymbolInfo& info : *this) {
  255. const char *const symbol_start =
  256. reinterpret_cast<const char *>(info.address);
  257. const char *const symbol_end = symbol_start + info.symbol->st_size;
  258. if (symbol_start <= address && address < symbol_end) {
  259. if (info_out) {
  260. // Client wants to know details for that symbol (the usual case).
  261. if (ElfBind(info.symbol) == STB_GLOBAL) {
  262. // Strong symbol; just return it.
  263. *info_out = info;
  264. return true;
  265. } else {
  266. // Weak or local. Record it, but keep looking for a strong one.
  267. *info_out = info;
  268. }
  269. } else {
  270. // Client only cares if there is an overlapping symbol.
  271. return true;
  272. }
  273. }
  274. }
  275. return false;
  276. }
  277. ElfMemImage::SymbolIterator::SymbolIterator(const void *const image, int index)
  278. : index_(index), image_(image) {
  279. }
  280. const ElfMemImage::SymbolInfo *ElfMemImage::SymbolIterator::operator->() const {
  281. return &info_;
  282. }
  283. const ElfMemImage::SymbolInfo& ElfMemImage::SymbolIterator::operator*() const {
  284. return info_;
  285. }
  286. bool ElfMemImage::SymbolIterator::operator==(const SymbolIterator &rhs) const {
  287. return this->image_ == rhs.image_ && this->index_ == rhs.index_;
  288. }
  289. bool ElfMemImage::SymbolIterator::operator!=(const SymbolIterator &rhs) const {
  290. return !(*this == rhs);
  291. }
  292. ElfMemImage::SymbolIterator &ElfMemImage::SymbolIterator::operator++() {
  293. this->Update(1);
  294. return *this;
  295. }
  296. ElfMemImage::SymbolIterator ElfMemImage::begin() const {
  297. SymbolIterator it(this, 0);
  298. it.Update(0);
  299. return it;
  300. }
  301. ElfMemImage::SymbolIterator ElfMemImage::end() const {
  302. return SymbolIterator(this, GetNumSymbols());
  303. }
  304. void ElfMemImage::SymbolIterator::Update(int increment) {
  305. const ElfMemImage *image = reinterpret_cast<const ElfMemImage *>(image_);
  306. ABSL_RAW_CHECK(image->IsPresent() || increment == 0, "");
  307. if (!image->IsPresent()) {
  308. return;
  309. }
  310. index_ += increment;
  311. if (index_ >= image->GetNumSymbols()) {
  312. index_ = image->GetNumSymbols();
  313. return;
  314. }
  315. const ElfW(Sym) *symbol = image->GetDynsym(index_);
  316. const ElfW(Versym) *version_symbol = image->GetVersym(index_);
  317. ABSL_RAW_CHECK(symbol && version_symbol, "");
  318. const char *const symbol_name = image->GetDynstr(symbol->st_name);
  319. const ElfW(Versym) version_index = version_symbol[0] & VERSYM_VERSION;
  320. const ElfW(Verdef) *version_definition = nullptr;
  321. const char *version_name = "";
  322. if (symbol->st_shndx == SHN_UNDEF) {
  323. // Undefined symbols reference DT_VERNEED, not DT_VERDEF, and
  324. // version_index could well be greater than verdefnum_, so calling
  325. // GetVerdef(version_index) may trigger assertion.
  326. } else {
  327. version_definition = image->GetVerdef(version_index);
  328. }
  329. if (version_definition) {
  330. // I am expecting 1 or 2 auxiliary entries: 1 for the version itself,
  331. // optional 2nd if the version has a parent.
  332. ABSL_RAW_CHECK(
  333. version_definition->vd_cnt == 1 || version_definition->vd_cnt == 2,
  334. "wrong number of entries");
  335. const ElfW(Verdaux) *version_aux = image->GetVerdefAux(version_definition);
  336. version_name = image->GetVerstr(version_aux->vda_name);
  337. }
  338. info_.name = symbol_name;
  339. info_.version = version_name;
  340. info_.address = image->GetSymAddr(symbol);
  341. info_.symbol = symbol;
  342. }
  343. } // namespace debugging_internal
  344. ABSL_NAMESPACE_END
  345. } // namespace absl
  346. #endif // ABSL_HAVE_ELF_MEM_IMAGE