| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
// Copyright (c) 2008, Google Inc. |
| 3 |
// All rights reserved. |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without |
| 6 |
// modification, are permitted provided that the following conditions are |
| 7 |
// met: |
| 8 |
// |
| 9 |
// * Redistributions of source code must retain the above copyright |
| 10 |
// notice, this list of conditions and the following disclaimer. |
| 11 |
// * Redistributions in binary form must reproduce the above |
| 12 |
// copyright notice, this list of conditions and the following disclaimer |
| 13 |
// in the documentation and/or other materials provided with the |
| 14 |
// distribution. |
| 15 |
// * Neither the name of Google Inc. nor the names of its |
| 16 |
// contributors may be used to endorse or promote products derived from |
| 17 |
// this software without specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
// --- |
| 32 |
// Author: Paul Pluzhnikov |
| 33 |
// |
| 34 |
// Allow dynamic symbol lookup for in-memory Elf images. |
| 35 |
|
| 36 |
#ifndef BASE_ELF_MEM_IMAGE_H_ |
| 37 |
#define BASE_ELF_MEM_IMAGE_H_ |
| 38 |
|
| 39 |
#include <config.h> |
| 40 |
#ifdef HAVE_FEATURES_H |
| 41 |
#include <features.h> // for __GLIBC__ |
| 42 |
#endif |
| 43 |
|
| 44 |
// Maybe one day we can rewrite this file not to require the elf |
| 45 |
// symbol extensions in glibc, but for right now we need them. |
| 46 |
#if defined(__ELF__) && defined(__GLIBC__) && !defined(__native_client__) |
| 47 |
|
| 48 |
#define HAVE_ELF_MEM_IMAGE 1 |
| 49 |
|
| 50 |
#include <stdlib.h> |
| 51 |
#include <link.h> // for ElfW |
| 52 |
|
| 53 |
namespace base { |
| 54 |
|
| 55 |
// An in-memory ELF image (may not exist on disk). |
| 56 |
class ElfMemImage { |
| 57 |
public: |
| 58 |
// Sentinel: there could never be an elf image at this address. |
| 59 |
static const void *const kInvalidBase; |
| 60 |
|
| 61 |
// Information about a single vdso symbol. |
| 62 |
// All pointers are into .dynsym, .dynstr, or .text of the VDSO. |
| 63 |
// Do not free() them or modify through them. |
| 64 |
struct SymbolInfo { |
| 65 |
const char *name; // E.g. "__vdso_getcpu" |
| 66 |
const char *version; // E.g. "LINUX_2.6", could be "" |
| 67 |
// for unversioned symbol. |
| 68 |
const void *address; // Relocated symbol address. |
| 69 |
const ElfW(Sym) *symbol; // Symbol in the dynamic symbol table. |
| 70 |
}; |
| 71 |
|
| 72 |
// Supports iteration over all dynamic symbols. |
| 73 |
class SymbolIterator { |
| 74 |
public: |
| 75 |
friend class ElfMemImage; |
| 76 |
const SymbolInfo *operator->() const; |
| 77 |
const SymbolInfo &operator*() const; |
| 78 |
SymbolIterator& operator++(); |
| 79 |
bool operator!=(const SymbolIterator &rhs) const; |
| 80 |
bool operator==(const SymbolIterator &rhs) const; |
| 81 |
private: |
| 82 |
SymbolIterator(const void *const image, int index); |
| 83 |
void Update(int incr); |
| 84 |
SymbolInfo info_; |
| 85 |
int index_; |
| 86 |
const void *const image_; |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
explicit ElfMemImage(const void *base); |
| 91 |
void Init(const void *base); |
| 92 |
bool IsPresent() const { return ehdr_ != NULL; } |
| 93 |
const ElfW(Phdr)* GetPhdr(int index) const; |
| 94 |
const ElfW(Sym)* GetDynsym(int index) const; |
| 95 |
const ElfW(Versym)* GetVersym(int index) const; |
| 96 |
const ElfW(Verdef)* GetVerdef(int index) const; |
| 97 |
const ElfW(Verdaux)* GetVerdefAux(const ElfW(Verdef) *verdef) const; |
| 98 |
const char* GetDynstr(ElfW(Word) offset) const; |
| 99 |
const void* GetSymAddr(const ElfW(Sym) *sym) const; |
| 100 |
const char* GetVerstr(ElfW(Word) offset) const; |
| 101 |
int GetNumSymbols() const; |
| 102 |
|
| 103 |
SymbolIterator begin() const; |
| 104 |
SymbolIterator end() const; |
| 105 |
|
| 106 |
// Look up versioned dynamic symbol in the image. |
| 107 |
// Returns false if image is not present, or doesn't contain given |
| 108 |
// symbol/version/type combination. |
| 109 |
// If info_out != NULL, additional details are filled in. |
| 110 |
bool LookupSymbol(const char *name, const char *version, |
| 111 |
int symbol_type, SymbolInfo *info_out) const; |
| 112 |
|
| 113 |
// Find info about symbol (if any) which overlaps given address. |
| 114 |
// Returns true if symbol was found; false if image isn't present |
| 115 |
// or doesn't have a symbol overlapping given address. |
| 116 |
// If info_out != NULL, additional details are filled in. |
| 117 |
bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const; |
| 118 |
|
| 119 |
private: |
| 120 |
const ElfW(Ehdr) *ehdr_; |
| 121 |
const ElfW(Sym) *dynsym_; |
| 122 |
const ElfW(Versym) *versym_; |
| 123 |
const ElfW(Verdef) *verdef_; |
| 124 |
const ElfW(Word) *hash_; |
| 125 |
const char *dynstr_; |
| 126 |
size_t strsize_; |
| 127 |
size_t verdefnum_; |
| 128 |
ElfW(Addr) link_base_; // Link-time base (p_vaddr of first PT_LOAD). |
| 129 |
}; |
| 130 |
|
| 131 |
} // namespace base |
| 132 |
|
| 133 |
#endif // __ELF__ and __GLIBC__ and !__native_client__ |
| 134 |
|
| 135 |
#endif // BASE_ELF_MEM_IMAGE_H_ |