abseil-cpp/absl/debugging/internal/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 #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 {
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);
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
135 } // namespace absl
136 
137 #endif // ABSL_HAVE_ELF_MEM_IMAGE
138 
139 #endif // ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
vs_toolchain.Update
def Update(version)
Definition: vs_toolchain.py:76
google::protobuf::python::cmessage::Init
static int Init(CMessage *self, PyObject *args, PyObject *kwargs)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:1287
setup.name
name
Definition: setup.py:542
google::protobuf.internal::IsPresent
bool IsPresent(const void *base, uint32 hasbit)
Definition: bloaty/third_party/protobuf/src/google/protobuf/generated_message_util.h:121
version
Definition: version.py:1
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
info_
experimental::ClientRpcInfo * info_
Definition: client_interceptors_end2end_test.cc:169
absl::operator*
uint128 operator*(uint128 lhs, uint128 rhs)
Definition: abseil-cpp/absl/numeric/int128.h:977
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
conf.version
string version
Definition: doc/python/sphinx/conf.py:36
index_
size_t index_
Definition: xds_cluster_resolver.cc:169
gen_synthetic_protos.base
base
Definition: gen_synthetic_protos.py:31
absl::operator==
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
Definition: abseil-cpp/absl/container/inlined_vector.h:794
absl::operator!=
bool operator!=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
Definition: abseil-cpp/absl/container/inlined_vector.h:805
re2::operator++
static void operator++(Engine &e, int unused)
Definition: bloaty/third_party/re2/re2/testing/tester.h:39
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
create_matrix_images.image
image
Definition: create_matrix_images.py:352
offset
voidpf uLong offset
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:142


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:18