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 for in-memory Elf images.
34
35#ifndef BASE_ELF_MEM_IMAGE_H_
36#define BASE_ELF_MEM_IMAGE_H_
37
38#include <config.h>
39#ifdef HAVE_FEATURES_H
40#include <features.h>   // for __GLIBC__
41#endif
42
43// Maybe one day we can rewrite this file not to require the elf
44// symbol extensions in glibc, but for right now we need them.
45#if defined(__ELF__) && defined(__GLIBC__) && !defined(__native_client__)
46
47#define HAVE_ELF_MEM_IMAGE 1
48
49#include <stdlib.h>
50#include <link.h>  // for ElfW
51
52namespace base {
53
54// An in-memory ELF image (may not exist on disk).
55class ElfMemImage {
56 public:
57  // Sentinel: there could never be an elf image at this address.
58  static const void *const kInvalidBase;
59
60  // Information about a single vdso symbol.
61  // All pointers are into .dynsym, .dynstr, or .text of the VDSO.
62  // Do not free() them or modify through them.
63  struct SymbolInfo {
64    const char      *name;      // E.g. "__vdso_getcpu"
65    const char      *version;   // E.g. "LINUX_2.6", could be ""
66                                // for unversioned symbol.
67    const void      *address;   // Relocated symbol address.
68    const ElfW(Sym) *symbol;    // Symbol in the dynamic symbol table.
69  };
70
71  // Supports iteration over all dynamic symbols.
72  class SymbolIterator {
73   public:
74    friend class ElfMemImage;
75    const SymbolInfo *operator->() const;
76    const SymbolInfo &operator*() const;
77    SymbolIterator& operator++();
78    bool operator!=(const SymbolIterator &rhs) const;
79    bool operator==(const SymbolIterator &rhs) const;
80   private:
81    SymbolIterator(const void *const image, int index);
82    void Update(int incr);
83    SymbolInfo info_;
84    int index_;
85    const void *const image_;
86  };
87
88
89  explicit ElfMemImage(const void *base);
90  void                 Init(const void *base);
91  bool                 IsPresent() const { return ehdr_ != NULL; }
92  const ElfW(Phdr)*    GetPhdr(int index) const;
93  const ElfW(Sym)*     GetDynsym(int index) const;
94  const ElfW(Versym)*  GetVersym(int index) const;
95  const ElfW(Verdef)*  GetVerdef(int index) const;
96  const ElfW(Verdaux)* GetVerdefAux(const ElfW(Verdef) *verdef) const;
97  const char*          GetDynstr(ElfW(Word) offset) const;
98  const void*          GetSymAddr(const ElfW(Sym) *sym) const;
99  const char*          GetVerstr(ElfW(Word) offset) const;
100  int                  GetNumSymbols() const;
101
102  SymbolIterator begin() const;
103  SymbolIterator end() const;
104
105  // Look up versioned dynamic symbol in the image.
106  // Returns false if image is not present, or doesn't contain given
107  // symbol/version/type combination.
108  // If info_out != NULL, additional details are filled in.
109  bool LookupSymbol(const char *name, const char *version,
110                    int symbol_type, SymbolInfo *info_out) const;
111
112  // Find info about symbol (if any) which overlaps given address.
113  // Returns true if symbol was found; false if image isn't present
114  // or doesn't have a symbol overlapping given address.
115  // If info_out != NULL, additional details are filled in.
116  bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const;
117
118 private:
119  const ElfW(Ehdr) *ehdr_;
120  const ElfW(Sym) *dynsym_;
121  const ElfW(Versym) *versym_;
122  const ElfW(Verdef) *verdef_;
123  const ElfW(Word) *hash_;
124  const char *dynstr_;
125  size_t strsize_;
126  size_t verdefnum_;
127  ElfW(Addr) link_base_;     // Link-time base (p_vaddr of first PT_LOAD).
128};
129
130}  // namespace base
131
132#endif  // __ELF__ and __GLIBC__ and !__native_client__
133
134#endif  // BASE_ELF_MEM_IMAGE_H_
135