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