Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: third_party/tcmalloc/vendor/src/base/elf_mem_image.cc

Issue 7430002: Update the tcmalloc vendor branch to r111 (perftools version 1.8) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2008, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 // ---
31 // Author: Paul Pluzhnikov
32 //
33 // Allow dynamic symbol lookup in an in-memory Elf image.
34 //
35
36 #include "base/elf_mem_image.h"
37
38 #ifdef HAVE_ELF_MEM_IMAGE // defined in elf_mem_image.h
39
40 #include "base/logging.h"
41
42 // From binutils/include/elf/common.h (this doesn't appear to be documented
43 // anywhere else).
44 //
45 // /* This flag appears in a Versym structure. It means that the symbol
46 // is hidden, and is only visible with an explicit version number.
47 // This is a GNU extension. */
48 // #define VERSYM_HIDDEN 0x8000
49 //
50 // /* This is the mask for the rest of the Versym information. */
51 // #define VERSYM_VERSION 0x7fff
52
53 #define VERSYM_VERSION 0x7fff
54
55 namespace base {
56
57 namespace {
58 template <int N> class ElfClass {
59 public:
60 static const int kElfClass = -1;
61 static int ElfBind(const ElfW(Sym) *) {
62 CHECK(false); // << "Unexpected word size";
63 return 0;
64 }
65 static int ElfType(const ElfW(Sym) *) {
66 CHECK(false); // << "Unexpected word size";
67 return 0;
68 }
69 };
70
71 template <> class ElfClass<32> {
72 public:
73 static const int kElfClass = ELFCLASS32;
74 static int ElfBind(const ElfW(Sym) *symbol) {
75 return ELF32_ST_BIND(symbol->st_info);
76 }
77 static int ElfType(const ElfW(Sym) *symbol) {
78 return ELF32_ST_TYPE(symbol->st_info);
79 }
80 };
81
82 template <> class ElfClass<64> {
83 public:
84 static const int kElfClass = ELFCLASS64;
85 static int ElfBind(const ElfW(Sym) *symbol) {
86 return ELF64_ST_BIND(symbol->st_info);
87 }
88 static int ElfType(const ElfW(Sym) *symbol) {
89 return ELF64_ST_TYPE(symbol->st_info);
90 }
91 };
92
93 typedef ElfClass<__WORDSIZE> CurrentElfClass;
94
95 // Extract an element from one of the ELF tables, cast it to desired type.
96 // This is just a simple arithmetic and a glorified cast.
97 // Callers are responsible for bounds checking.
98 template <class T>
99 const T* GetTableElement(const ElfW(Ehdr) *ehdr,
100 ElfW(Off) table_offset,
101 ElfW(Word) element_size,
102 size_t index) {
103 return reinterpret_cast<const T*>(reinterpret_cast<const char *>(ehdr)
104 + table_offset
105 + index * element_size);
106 }
107 } // namespace
108
109 const void *const ElfMemImage::kInvalidBase =
110 reinterpret_cast<const void *>(~0L);
111
112 ElfMemImage::ElfMemImage(const void *base) {
113 CHECK(base != kInvalidBase);
114 Init(base);
115 }
116
117 int ElfMemImage::GetNumSymbols() const {
118 if (!hash_) {
119 return 0;
120 }
121 // See http://www.caldera.com/developers/gabi/latest/ch5.dynamic.html#hash
122 return hash_[1];
123 }
124
125 const ElfW(Sym) *ElfMemImage::GetDynsym(int index) const {
126 CHECK_LT(index, GetNumSymbols());
127 return dynsym_ + index;
128 }
129
130 const ElfW(Versym) *ElfMemImage::GetVersym(int index) const {
131 CHECK_LT(index, GetNumSymbols());
132 return versym_ + index;
133 }
134
135 const ElfW(Phdr) *ElfMemImage::GetPhdr(int index) const {
136 CHECK_LT(index, ehdr_->e_phnum);
137 return GetTableElement<ElfW(Phdr)>(ehdr_,
138 ehdr_->e_phoff,
139 ehdr_->e_phentsize,
140 index);
141 }
142
143 const char *ElfMemImage::GetDynstr(ElfW(Word) offset) const {
144 CHECK_LT(offset, strsize_);
145 return dynstr_ + offset;
146 }
147
148 const void *ElfMemImage::GetSymAddr(const ElfW(Sym) *sym) const {
149 if (sym->st_shndx == SHN_UNDEF || sym->st_shndx >= SHN_LORESERVE) {
150 // Symbol corresponds to "special" (e.g. SHN_ABS) section.
151 return reinterpret_cast<const void *>(sym->st_value);
152 }
153 CHECK_LT(link_base_, sym->st_value);
154 return GetTableElement<char>(ehdr_, 0, 1, sym->st_value) - link_base_;
155 }
156
157 const ElfW(Verdef) *ElfMemImage::GetVerdef(int index) const {
158 CHECK_LE(index, verdefnum_);
159 const ElfW(Verdef) *version_definition = verdef_;
160 while (version_definition->vd_ndx < index && version_definition->vd_next) {
161 const char *const version_definition_as_char =
162 reinterpret_cast<const char *>(version_definition);
163 version_definition =
164 reinterpret_cast<const ElfW(Verdef) *>(version_definition_as_char +
165 version_definition->vd_next);
166 }
167 return version_definition->vd_ndx == index ? version_definition : NULL;
168 }
169
170 const ElfW(Verdaux) *ElfMemImage::GetVerdefAux(
171 const ElfW(Verdef) *verdef) const {
172 return reinterpret_cast<const ElfW(Verdaux) *>(verdef+1);
173 }
174
175 const char *ElfMemImage::GetVerstr(ElfW(Word) offset) const {
176 CHECK_LT(offset, strsize_);
177 return dynstr_ + offset;
178 }
179
180 void ElfMemImage::Init(const void *base) {
181 ehdr_ = NULL;
182 dynsym_ = NULL;
183 dynstr_ = NULL;
184 versym_ = NULL;
185 verdef_ = NULL;
186 hash_ = NULL;
187 strsize_ = 0;
188 verdefnum_ = 0;
189 link_base_ = ~0L; // Sentinel: PT_LOAD .p_vaddr can't possibly be this.
190 if (!base) {
191 return;
192 }
193 const intptr_t base_as_uintptr_t = reinterpret_cast<uintptr_t>(base);
194 // Fake VDSO has low bit set.
195 const bool fake_vdso = ((base_as_uintptr_t & 1) != 0);
196 base = reinterpret_cast<const void *>(base_as_uintptr_t & ~1);
197 const char *const base_as_char = reinterpret_cast<const char *>(base);
198 if (base_as_char[EI_MAG0] != ELFMAG0 || base_as_char[EI_MAG1] != ELFMAG1 ||
199 base_as_char[EI_MAG2] != ELFMAG2 || base_as_char[EI_MAG3] != ELFMAG3) {
200 RAW_DCHECK(false, "no ELF magic"); // at %p", base);
201 return;
202 }
203 int elf_class = base_as_char[EI_CLASS];
204 if (elf_class != CurrentElfClass::kElfClass) {
205 DCHECK_EQ(elf_class, CurrentElfClass::kElfClass);
206 return;
207 }
208 switch (base_as_char[EI_DATA]) {
209 case ELFDATA2LSB: {
210 if (__LITTLE_ENDIAN != __BYTE_ORDER) {
211 DCHECK_EQ(__LITTLE_ENDIAN, __BYTE_ORDER); // << ": wrong byte order";
212 return;
213 }
214 break;
215 }
216 case ELFDATA2MSB: {
217 if (__BIG_ENDIAN != __BYTE_ORDER) {
218 DCHECK_EQ(__BIG_ENDIAN, __BYTE_ORDER); // << ": wrong byte order";
219 return;
220 }
221 break;
222 }
223 default: {
224 RAW_DCHECK(false, "unexpected data encoding"); // << base_as_char[EI_DATA] ;
225 return;
226 }
227 }
228
229 ehdr_ = reinterpret_cast<const ElfW(Ehdr) *>(base);
230 const ElfW(Phdr) *dynamic_program_header = NULL;
231 for (int i = 0; i < ehdr_->e_phnum; ++i) {
232 const ElfW(Phdr) *const program_header = GetPhdr(i);
233 switch (program_header->p_type) {
234 case PT_LOAD:
235 if (link_base_ == ~0L) {
236 link_base_ = program_header->p_vaddr;
237 }
238 break;
239 case PT_DYNAMIC:
240 dynamic_program_header = program_header;
241 break;
242 }
243 }
244 if (link_base_ == ~0L || !dynamic_program_header) {
245 RAW_DCHECK(~0L != link_base_, "no PT_LOADs in VDSO");
246 RAW_DCHECK(dynamic_program_header, "no PT_DYNAMIC in VDSO");
247 // Mark this image as not present. Can not recur infinitely.
248 Init(0);
249 return;
250 }
251 ptrdiff_t relocation =
252 base_as_char - reinterpret_cast<const char *>(link_base_);
253 ElfW(Dyn) *dynamic_entry =
254 reinterpret_cast<ElfW(Dyn) *>(dynamic_program_header->p_vaddr +
255 relocation);
256 for (; dynamic_entry->d_tag != DT_NULL; ++dynamic_entry) {
257 ElfW(Xword) value = dynamic_entry->d_un.d_val;
258 if (fake_vdso) {
259 // A complication: in the real VDSO, dynamic entries are not relocated
260 // (it wasn't loaded by a dynamic loader). But when testing with a
261 // "fake" dlopen()ed vdso library, the loader relocates some (but
262 // not all!) of them before we get here.
263 if (dynamic_entry->d_tag == DT_VERDEF) {
264 // The only dynamic entry (of the ones we care about) libc-2.3.6
265 // loader doesn't relocate.
266 value += relocation;
267 }
268 } else {
269 // Real VDSO. Everything needs to be relocated.
270 value += relocation;
271 }
272 switch (dynamic_entry->d_tag) {
273 case DT_HASH:
274 hash_ = reinterpret_cast<ElfW(Word) *>(value);
275 break;
276 case DT_SYMTAB:
277 dynsym_ = reinterpret_cast<ElfW(Sym) *>(value);
278 break;
279 case DT_STRTAB:
280 dynstr_ = reinterpret_cast<const char *>(value);
281 break;
282 case DT_VERSYM:
283 versym_ = reinterpret_cast<ElfW(Versym) *>(value);
284 break;
285 case DT_VERDEF:
286 verdef_ = reinterpret_cast<ElfW(Verdef) *>(value);
287 break;
288 case DT_VERDEFNUM:
289 verdefnum_ = dynamic_entry->d_un.d_val;
290 break;
291 case DT_STRSZ:
292 strsize_ = dynamic_entry->d_un.d_val;
293 break;
294 default:
295 // Unrecognized entries explicitly ignored.
296 break;
297 }
298 }
299 if (!hash_ || !dynsym_ || !dynstr_ || !versym_ ||
300 !verdef_ || !verdefnum_ || !strsize_) {
301 RAW_DCHECK(hash_, "invalid VDSO (no DT_HASH)");
302 RAW_DCHECK(dynsym_, "invalid VDSO (no DT_SYMTAB)");
303 RAW_DCHECK(dynstr_, "invalid VDSO (no DT_STRTAB)");
304 RAW_DCHECK(versym_, "invalid VDSO (no DT_VERSYM)");
305 RAW_DCHECK(verdef_, "invalid VDSO (no DT_VERDEF)");
306 RAW_DCHECK(verdefnum_, "invalid VDSO (no DT_VERDEFNUM)");
307 RAW_DCHECK(strsize_, "invalid VDSO (no DT_STRSZ)");
308 // Mark this image as not present. Can not recur infinitely.
309 Init(0);
310 return;
311 }
312 }
313
314 bool ElfMemImage::LookupSymbol(const char *name,
315 const char *version,
316 int type,
317 SymbolInfo *info) const {
318 for (SymbolIterator it = begin(); it != end(); ++it) {
319 if (strcmp(it->name, name) == 0 && strcmp(it->version, version) == 0 &&
320 CurrentElfClass::ElfType(it->symbol) == type) {
321 if (info) {
322 *info = *it;
323 }
324 return true;
325 }
326 }
327 return false;
328 }
329
330 bool ElfMemImage::LookupSymbolByAddress(const void *address,
331 SymbolInfo *info_out) const {
332 for (SymbolIterator it = begin(); it != end(); ++it) {
333 const char *const symbol_start =
334 reinterpret_cast<const char *>(it->address);
335 const char *const symbol_end = symbol_start + it->symbol->st_size;
336 if (symbol_start <= address && address < symbol_end) {
337 if (info_out) {
338 // Client wants to know details for that symbol (the usual case).
339 if (CurrentElfClass::ElfBind(it->symbol) == STB_GLOBAL) {
340 // Strong symbol; just return it.
341 *info_out = *it;
342 return true;
343 } else {
344 // Weak or local. Record it, but keep looking for a strong one.
345 *info_out = *it;
346 }
347 } else {
348 // Client only cares if there is an overlapping symbol.
349 return true;
350 }
351 }
352 }
353 return false;
354 }
355
356 ElfMemImage::SymbolIterator::SymbolIterator(const void *const image, int index)
357 : index_(index), image_(image) {
358 }
359
360 const ElfMemImage::SymbolInfo *ElfMemImage::SymbolIterator::operator->() const {
361 return &info_;
362 }
363
364 const ElfMemImage::SymbolInfo& ElfMemImage::SymbolIterator::operator*() const {
365 return info_;
366 }
367
368 bool ElfMemImage::SymbolIterator::operator==(const SymbolIterator &rhs) const {
369 return this->image_ == rhs.image_ && this->index_ == rhs.index_;
370 }
371
372 bool ElfMemImage::SymbolIterator::operator!=(const SymbolIterator &rhs) const {
373 return !(*this == rhs);
374 }
375
376 ElfMemImage::SymbolIterator &ElfMemImage::SymbolIterator::operator++() {
377 this->Update(1);
378 return *this;
379 }
380
381 ElfMemImage::SymbolIterator ElfMemImage::begin() const {
382 SymbolIterator it(this, 0);
383 it.Update(0);
384 return it;
385 }
386
387 ElfMemImage::SymbolIterator ElfMemImage::end() const {
388 return SymbolIterator(this, GetNumSymbols());
389 }
390
391 void ElfMemImage::SymbolIterator::Update(int increment) {
392 const ElfMemImage *image = reinterpret_cast<const ElfMemImage *>(image_);
393 CHECK(image->IsPresent() || increment == 0);
394 if (!image->IsPresent()) {
395 return;
396 }
397 index_ += increment;
398 if (index_ >= image->GetNumSymbols()) {
399 index_ = image->GetNumSymbols();
400 return;
401 }
402 const ElfW(Sym) *symbol = image->GetDynsym(index_);
403 const ElfW(Versym) *version_symbol = image->GetVersym(index_);
404 CHECK(symbol && version_symbol);
405 const char *const symbol_name = image->GetDynstr(symbol->st_name);
406 const ElfW(Versym) version_index = version_symbol[0] & VERSYM_VERSION;
407 const ElfW(Verdef) *version_definition = NULL;
408 const char *version_name = "";
409 if (symbol->st_shndx == SHN_UNDEF) {
410 // Undefined symbols reference DT_VERNEED, not DT_VERDEF, and
411 // version_index could well be greater than verdefnum_, so calling
412 // GetVerdef(version_index) may trigger assertion.
413 } else {
414 version_definition = image->GetVerdef(version_index);
415 }
416 if (version_definition) {
417 // I am expecting 1 or 2 auxiliary entries: 1 for the version itself,
418 // optional 2nd if the version has a parent.
419 CHECK_LE(1, version_definition->vd_cnt);
420 CHECK_LE(version_definition->vd_cnt, 2);
421 const ElfW(Verdaux) *version_aux = image->GetVerdefAux(version_definition);
422 version_name = image->GetVerstr(version_aux->vda_name);
423 }
424 info_.name = symbol_name;
425 info_.version = version_name;
426 info_.address = image->GetSymAddr(symbol);
427 info_.symbol = symbol;
428 }
429
430 } // namespace base
431
432 #endif // HAVE_ELF_MEM_IMAGE
OLDNEW
« no previous file with comments | « third_party/tcmalloc/vendor/src/base/elf_mem_image.h ('k') | third_party/tcmalloc/vendor/src/base/low_level_alloc.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698