1 /* 2 * Copyright 2017 The Abseil Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // Allow dynamic symbol lookup for in-memory Elf images. 18 19 #ifndef ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_ 20 #define ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_ 21 22 // Including this will define the __GLIBC__ macro if glibc is being 23 // used. 24 #include <climits> 25 26 #include "absl/base/config.h" 27 28 // Maybe one day we can rewrite this file not to require the elf 29 // symbol extensions in glibc, but for right now we need them. 30 #ifdef ABSL_HAVE_ELF_MEM_IMAGE 31 #error ABSL_HAVE_ELF_MEM_IMAGE cannot be directly set 32 #endif 33 34 #if defined(__ELF__) && !defined(__OpenBSD__) && !defined(__QNX__) && \ 35 !defined(__native_client__) && !defined(__asmjs__) && \ 36 !defined(__wasm__) && !defined(__HAIKU__) && !defined(__sun) && \ 37 !defined(__VXWORKS__) && !defined(__hexagon__) 38 #define ABSL_HAVE_ELF_MEM_IMAGE 1 39 #endif 40 41 #ifdef ABSL_HAVE_ELF_MEM_IMAGE 42 43 #include <link.h> // for ElfW 44 45 #if defined(__FreeBSD__) && !defined(ElfW) 46 #define ElfW(x) __ElfN(x) 47 #endif 48 49 namespace absl { 50 ABSL_NAMESPACE_BEGIN 51 namespace debugging_internal { 52 53 // An in-memory ELF image (may not exist on disk). 54 class ElfMemImage { 55 private: 56 // Sentinel: there could never be an elf image at &kInvalidBaseSentinel. 57 static const int kInvalidBaseSentinel; 58 59 public: 60 // Sentinel: there could never be an elf image at this address. 61 static constexpr const void *const kInvalidBase = 62 static_cast<const void*>(&kInvalidBaseSentinel); 63 64 // Information about a single vdso symbol. 65 // All pointers are into .dynsym, .dynstr, or .text of the VDSO. 66 // Do not free() them or modify through them. 67 struct SymbolInfo { 68 const char *name; // E.g. "__vdso_getcpu" 69 const char *version; // E.g. "LINUX_2.6", could be "" 70 // for unversioned symbol. 71 const void *address; // Relocated symbol address. 72 const ElfW(Sym) *symbol; // Symbol in the dynamic symbol table. 73 }; 74 75 // Supports iteration over all dynamic symbols. 76 class SymbolIterator { 77 public: 78 friend class ElfMemImage; 79 const SymbolInfo *operator->() const; 80 const SymbolInfo &operator*() const; 81 SymbolIterator& operator++(); 82 bool operator!=(const SymbolIterator &rhs) const; 83 bool operator==(const SymbolIterator &rhs) const; 84 private: 85 SymbolIterator(const void *const image, int index); 86 void Update(int incr); 87 SymbolInfo info_; 88 int index_; 89 const void *const image_; 90 }; 91 92 93 explicit ElfMemImage(const void *base); 94 void Init(const void *base); IsPresent()95 bool IsPresent() const { return ehdr_ != nullptr; } 96 const ElfW(Phdr)* GetPhdr(int index) const; 97 const ElfW(Sym)* GetDynsym(int index) const; 98 const ElfW(Versym)* GetVersym(int index) const; 99 const ElfW(Verdef)* GetVerdef(int index) const; 100 const ElfW(Verdaux)* GetVerdefAux(const ElfW(Verdef) *verdef) const; 101 const char* GetDynstr(ElfW(Word) offset) const; 102 const void* GetSymAddr(const ElfW(Sym) *sym) const; 103 const char* GetVerstr(ElfW(Word) offset) const; 104 int GetNumSymbols() const; 105 106 SymbolIterator begin() const; 107 SymbolIterator end() const; 108 109 // Look up versioned dynamic symbol in the image. 110 // Returns false if image is not present, or doesn't contain given 111 // symbol/version/type combination. 112 // If info_out is non-null, additional details are filled in. 113 bool LookupSymbol(const char *name, const char *version, 114 int symbol_type, SymbolInfo *info_out) const; 115 116 // Find info about symbol (if any) which overlaps given address. 117 // Returns true if symbol was found; false if image isn't present 118 // or doesn't have a symbol overlapping given address. 119 // If info_out is non-null, additional details are filled in. 120 bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const; 121 122 private: 123 const ElfW(Ehdr) *ehdr_; 124 const ElfW(Sym) *dynsym_; 125 const ElfW(Versym) *versym_; 126 const ElfW(Verdef) *verdef_; 127 const ElfW(Word) *hash_; 128 const char *dynstr_; 129 size_t strsize_; 130 size_t verdefnum_; 131 ElfW(Addr) link_base_; // Link-time base (p_vaddr of first PT_LOAD). 132 }; 133 134 } // namespace debugging_internal 135 ABSL_NAMESPACE_END 136 } // namespace absl 137 138 #endif // ABSL_HAVE_ELF_MEM_IMAGE 139 140 #endif // ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_ 141