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


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:18