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