abseil-cpp/absl/debugging/internal/elf_mem_image.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Allow dynamic symbol lookup in an in-memory Elf image.
16 //
17 
18 #include "absl/debugging/internal/elf_mem_image.h"
19 
20 #ifdef ABSL_HAVE_ELF_MEM_IMAGE // defined in elf_mem_image.h
21 
22 #include <string.h>
23 #include <cassert>
24 #include <cstddef>
25 #include "absl/base/config.h"
26 #include "absl/base/internal/raw_logging.h"
27 
28 // From binutils/include/elf/common.h (this doesn't appear to be documented
29 // anywhere else).
30 //
31 // /* This flag appears in a Versym structure. It means that the symbol
32 // is hidden, and is only visible with an explicit version number.
33 // This is a GNU extension. */
34 // #define VERSYM_HIDDEN 0x8000
35 //
36 // /* This is the mask for the rest of the Versym information. */
37 // #define VERSYM_VERSION 0x7fff
38 
39 #define VERSYM_VERSION 0x7fff
40 
41 namespace absl {
43 namespace debugging_internal {
44 
45 namespace {
46 
47 #if __SIZEOF_POINTER__ == 4
48 const int kElfClass = ELFCLASS32;
49 int ElfBind(const ElfW(Sym) *symbol) { return ELF32_ST_BIND(symbol->st_info); }
50 int ElfType(const ElfW(Sym) *symbol) { return ELF32_ST_TYPE(symbol->st_info); }
51 #elif __SIZEOF_POINTER__ == 8
52 const int kElfClass = ELFCLASS64;
53 int ElfBind(const ElfW(Sym) *symbol) { return ELF64_ST_BIND(symbol->st_info); }
54 int ElfType(const ElfW(Sym) *symbol) { return ELF64_ST_TYPE(symbol->st_info); }
55 #else
56 const int kElfClass = -1;
57 int ElfBind(const ElfW(Sym) *) {
58  ABSL_RAW_LOG(FATAL, "Unexpected word size");
59  return 0;
60 }
61 int ElfType(const ElfW(Sym) *) {
62  ABSL_RAW_LOG(FATAL, "Unexpected word size");
63  return 0;
64 }
65 #endif
66 
67 // Extract an element from one of the ELF tables, cast it to desired type.
68 // This is just a simple arithmetic and a glorified cast.
69 // Callers are responsible for bounds checking.
70 template <typename T>
71 const T *GetTableElement(const ElfW(Ehdr) * ehdr, ElfW(Off) table_offset,
72  ElfW(Word) element_size, size_t index) {
73  return reinterpret_cast<const T*>(reinterpret_cast<const char *>(ehdr)
74  + table_offset
75  + index * element_size);
76 }
77 
78 } // namespace
79 
80 // The value of this variable doesn't matter; it's used only for its
81 // unique address.
82 const int ElfMemImage::kInvalidBaseSentinel = 0;
83 
84 ElfMemImage::ElfMemImage(const void *base) {
85  ABSL_RAW_CHECK(base != kInvalidBase, "bad pointer");
86  Init(base);
87 }
88 
89 int ElfMemImage::GetNumSymbols() const {
90  if (!hash_) {
91  return 0;
92  }
93  // See http://www.caldera.com/developers/gabi/latest/ch5.dynamic.html#hash
94  return hash_[1];
95 }
96 
97 const ElfW(Sym) *ElfMemImage::GetDynsym(int index) const {
98  ABSL_RAW_CHECK(index < GetNumSymbols(), "index out of range");
99  return dynsym_ + index;
100 }
101 
102 const ElfW(Versym) *ElfMemImage::GetVersym(int index) const {
103  ABSL_RAW_CHECK(index < GetNumSymbols(), "index out of range");
104  return versym_ + index;
105 }
106 
107 const ElfW(Phdr) *ElfMemImage::GetPhdr(int index) const {
108  ABSL_RAW_CHECK(index < ehdr_->e_phnum, "index out of range");
109  return GetTableElement<ElfW(Phdr)>(ehdr_,
110  ehdr_->e_phoff,
111  ehdr_->e_phentsize,
112  index);
113 }
114 
115 const char *ElfMemImage::GetDynstr(ElfW(Word) offset) const {
116  ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
117  return dynstr_ + offset;
118 }
119 
120 const void *ElfMemImage::GetSymAddr(const ElfW(Sym) *sym) const {
121  if (sym->st_shndx == SHN_UNDEF || sym->st_shndx >= SHN_LORESERVE) {
122  // Symbol corresponds to "special" (e.g. SHN_ABS) section.
123  return reinterpret_cast<const void *>(sym->st_value);
124  }
125  ABSL_RAW_CHECK(link_base_ < sym->st_value, "symbol out of range");
126  return GetTableElement<char>(ehdr_, 0, 1, sym->st_value - link_base_);
127 }
128 
129 const ElfW(Verdef) *ElfMemImage::GetVerdef(int index) const {
130  ABSL_RAW_CHECK(0 <= index && static_cast<size_t>(index) <= verdefnum_,
131  "index out of range");
132  const ElfW(Verdef) *version_definition = verdef_;
133  while (version_definition->vd_ndx < index && version_definition->vd_next) {
134  const char *const version_definition_as_char =
135  reinterpret_cast<const char *>(version_definition);
136  version_definition =
137  reinterpret_cast<const ElfW(Verdef) *>(version_definition_as_char +
138  version_definition->vd_next);
139  }
140  return version_definition->vd_ndx == index ? version_definition : nullptr;
141 }
142 
143 const ElfW(Verdaux) *ElfMemImage::GetVerdefAux(
144  const ElfW(Verdef) *verdef) const {
145  return reinterpret_cast<const ElfW(Verdaux) *>(verdef+1);
146 }
147 
148 const char *ElfMemImage::GetVerstr(ElfW(Word) offset) const {
149  ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
150  return dynstr_ + offset;
151 }
152 
153 void ElfMemImage::Init(const void *base) {
154  ehdr_ = nullptr;
155  dynsym_ = nullptr;
156  dynstr_ = nullptr;
157  versym_ = nullptr;
158  verdef_ = nullptr;
159  hash_ = nullptr;
160  strsize_ = 0;
161  verdefnum_ = 0;
162  link_base_ = ~0L; // Sentinel: PT_LOAD .p_vaddr can't possibly be this.
163  if (!base) {
164  return;
165  }
166  const char *const base_as_char = reinterpret_cast<const char *>(base);
167  if (base_as_char[EI_MAG0] != ELFMAG0 || base_as_char[EI_MAG1] != ELFMAG1 ||
168  base_as_char[EI_MAG2] != ELFMAG2 || base_as_char[EI_MAG3] != ELFMAG3) {
169  assert(false);
170  return;
171  }
172  int elf_class = base_as_char[EI_CLASS];
173  if (elf_class != kElfClass) {
174  assert(false);
175  return;
176  }
177  switch (base_as_char[EI_DATA]) {
178  case ELFDATA2LSB: {
179 #ifndef ABSL_IS_LITTLE_ENDIAN
180  assert(false);
181  return;
182 #endif
183  break;
184  }
185  case ELFDATA2MSB: {
186 #ifndef ABSL_IS_BIG_ENDIAN
187  assert(false);
188  return;
189 #endif
190  break;
191  }
192  default: {
193  assert(false);
194  return;
195  }
196  }
197 
198  ehdr_ = reinterpret_cast<const ElfW(Ehdr) *>(base);
199  const ElfW(Phdr) *dynamic_program_header = nullptr;
200  for (int i = 0; i < ehdr_->e_phnum; ++i) {
201  const ElfW(Phdr) *const program_header = GetPhdr(i);
202  switch (program_header->p_type) {
203  case PT_LOAD:
204  if (!~link_base_) {
205  link_base_ = program_header->p_vaddr;
206  }
207  break;
208  case PT_DYNAMIC:
209  dynamic_program_header = program_header;
210  break;
211  }
212  }
213  if (!~link_base_ || !dynamic_program_header) {
214  assert(false);
215  // Mark this image as not present. Can not recur infinitely.
216  Init(nullptr);
217  return;
218  }
219  ptrdiff_t relocation =
220  base_as_char - reinterpret_cast<const char *>(link_base_);
221  ElfW(Dyn) *dynamic_entry =
222  reinterpret_cast<ElfW(Dyn) *>(dynamic_program_header->p_vaddr +
223  relocation);
224  for (; dynamic_entry->d_tag != DT_NULL; ++dynamic_entry) {
225  const auto value = dynamic_entry->d_un.d_val + relocation;
226  switch (dynamic_entry->d_tag) {
227  case DT_HASH:
228  hash_ = reinterpret_cast<ElfW(Word) *>(value);
229  break;
230  case DT_SYMTAB:
231  dynsym_ = reinterpret_cast<ElfW(Sym) *>(value);
232  break;
233  case DT_STRTAB:
234  dynstr_ = reinterpret_cast<const char *>(value);
235  break;
236  case DT_VERSYM:
237  versym_ = reinterpret_cast<ElfW(Versym) *>(value);
238  break;
239  case DT_VERDEF:
240  verdef_ = reinterpret_cast<ElfW(Verdef) *>(value);
241  break;
242  case DT_VERDEFNUM:
243  verdefnum_ = dynamic_entry->d_un.d_val;
244  break;
245  case DT_STRSZ:
246  strsize_ = dynamic_entry->d_un.d_val;
247  break;
248  default:
249  // Unrecognized entries explicitly ignored.
250  break;
251  }
252  }
253  if (!hash_ || !dynsym_ || !dynstr_ || !versym_ ||
254  !verdef_ || !verdefnum_ || !strsize_) {
255  assert(false); // invalid VDSO
256  // Mark this image as not present. Can not recur infinitely.
257  Init(nullptr);
258  return;
259  }
260 }
261 
262 bool ElfMemImage::LookupSymbol(const char *name,
263  const char *version,
264  int type,
265  SymbolInfo *info_out) const {
266  for (const SymbolInfo& info : *this) {
267  if (strcmp(info.name, name) == 0 && strcmp(info.version, version) == 0 &&
268  ElfType(info.symbol) == type) {
269  if (info_out) {
270  *info_out = info;
271  }
272  return true;
273  }
274  }
275  return false;
276 }
277 
278 bool ElfMemImage::LookupSymbolByAddress(const void *address,
279  SymbolInfo *info_out) const {
280  for (const SymbolInfo& info : *this) {
281  const char *const symbol_start =
282  reinterpret_cast<const char *>(info.address);
283  const char *const symbol_end = symbol_start + info.symbol->st_size;
284  if (symbol_start <= address && address < symbol_end) {
285  if (info_out) {
286  // Client wants to know details for that symbol (the usual case).
287  if (ElfBind(info.symbol) == STB_GLOBAL) {
288  // Strong symbol; just return it.
289  *info_out = info;
290  return true;
291  } else {
292  // Weak or local. Record it, but keep looking for a strong one.
293  *info_out = info;
294  }
295  } else {
296  // Client only cares if there is an overlapping symbol.
297  return true;
298  }
299  }
300  }
301  return false;
302 }
303 
304 ElfMemImage::SymbolIterator::SymbolIterator(const void *const image, int index)
305  : index_(index), image_(image) {
306 }
307 
308 const ElfMemImage::SymbolInfo *ElfMemImage::SymbolIterator::operator->() const {
309  return &info_;
310 }
311 
312 const ElfMemImage::SymbolInfo& ElfMemImage::SymbolIterator::operator*() const {
313  return info_;
314 }
315 
316 bool ElfMemImage::SymbolIterator::operator==(const SymbolIterator &rhs) const {
317  return this->image_ == rhs.image_ && this->index_ == rhs.index_;
318 }
319 
320 bool ElfMemImage::SymbolIterator::operator!=(const SymbolIterator &rhs) const {
321  return !(*this == rhs);
322 }
323 
324 ElfMemImage::SymbolIterator &ElfMemImage::SymbolIterator::operator++() {
325  this->Update(1);
326  return *this;
327 }
328 
329 ElfMemImage::SymbolIterator ElfMemImage::begin() const {
330  SymbolIterator it(this, 0);
331  it.Update(0);
332  return it;
333 }
334 
335 ElfMemImage::SymbolIterator ElfMemImage::end() const {
336  return SymbolIterator(this, GetNumSymbols());
337 }
338 
339 void ElfMemImage::SymbolIterator::Update(int increment) {
340  const ElfMemImage *image = reinterpret_cast<const ElfMemImage *>(image_);
341  ABSL_RAW_CHECK(image->IsPresent() || increment == 0, "");
342  if (!image->IsPresent()) {
343  return;
344  }
345  index_ += increment;
346  if (index_ >= image->GetNumSymbols()) {
347  index_ = image->GetNumSymbols();
348  return;
349  }
350  const ElfW(Sym) *symbol = image->GetDynsym(index_);
351  const ElfW(Versym) *version_symbol = image->GetVersym(index_);
352  ABSL_RAW_CHECK(symbol && version_symbol, "");
353  const char *const symbol_name = image->GetDynstr(symbol->st_name);
354 #if defined(__NetBSD__)
355  const int version_index = version_symbol->vs_vers & VERSYM_VERSION;
356 #else
357  const ElfW(Versym) version_index = version_symbol[0] & VERSYM_VERSION;
358 #endif
359  const ElfW(Verdef) *version_definition = nullptr;
360  const char *version_name = "";
361  if (symbol->st_shndx == SHN_UNDEF) {
362  // Undefined symbols reference DT_VERNEED, not DT_VERDEF, and
363  // version_index could well be greater than verdefnum_, so calling
364  // GetVerdef(version_index) may trigger assertion.
365  } else {
366  version_definition = image->GetVerdef(version_index);
367  }
368  if (version_definition) {
369  // I am expecting 1 or 2 auxiliary entries: 1 for the version itself,
370  // optional 2nd if the version has a parent.
372  version_definition->vd_cnt == 1 || version_definition->vd_cnt == 2,
373  "wrong number of entries");
374  const ElfW(Verdaux) *version_aux = image->GetVerdefAux(version_definition);
375  version_name = image->GetVerstr(version_aux->vda_name);
376  }
377  info_.name = symbol_name;
378  info_.version = version_name;
379  info_.address = image->GetSymAddr(symbol);
380  info_.symbol = symbol;
381 }
382 
383 } // namespace debugging_internal
385 } // namespace absl
386 
387 #endif // ABSL_HAVE_ELF_MEM_IMAGE
DT_VERSYM
#define DT_VERSYM
Definition: elf_common.h:640
ABSL_RAW_CHECK
#define ABSL_RAW_CHECK(condition, message)
Definition: abseil-cpp/absl/base/internal/raw_logging.h:59
regen-readme.it
it
Definition: regen-readme.py:15
grpc_event_engine::experimental::slice_detail::operator==
bool operator==(const BaseSlice &a, const BaseSlice &b)
Definition: include/grpc/event_engine/slice.h:117
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
ELF64_ST_BIND
#define ELF64_ST_BIND(info)
Definition: elf64.h:216
string.h
DT_VERDEF
#define DT_VERDEF
Definition: elf_common.h:644
vs_toolchain.Update
def Update(version)
Definition: vs_toolchain.py:76
PT_DYNAMIC
#define PT_DYNAMIC
Definition: elf_common.h:512
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
version
Definition: version.py:1
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
DT_STRSZ
#define DT_STRSZ
Definition: elf_common.h:565
T
#define T(upbtypeconst, upbtype, ctype, default_value)
SHN_UNDEF
#define SHN_UNDEF
Definition: elf_common.h:387
ELF32_ST_BIND
#define ELF32_ST_BIND(info)
Definition: elf32.h:209
ELFMAG3
#define ELFMAG3
Definition: elf_common.h:143
info_
experimental::ClientRpcInfo * info_
Definition: client_interceptors_end2end_test.cc:169
PT_LOAD
#define PT_LOAD
Definition: elf_common.h:511
VERSYM_VERSION
#define VERSYM_VERSION
Definition: elf_common.h:506
EI_MAG0
#define EI_MAG0
Definition: elf_common.h:126
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
EI_MAG3
#define EI_MAG3
Definition: elf_common.h:129
operator!=
bool operator!=(const Bytes &a, const Bytes &b)
Definition: boringssl-with-bazel/src/crypto/test/test_util.h:58
index_
size_t index_
Definition: xds_cluster_resolver.cc:169
EI_DATA
#define EI_DATA
Definition: elf_common.h:131
DT_NULL
#define DT_NULL
Definition: elf_common.h:555
gen_synthetic_protos.base
base
Definition: gen_synthetic_protos.py:31
ELFCLASS64
#define ELFCLASS64
Definition: elf_common.h:154
DT_SYMTAB
#define DT_SYMTAB
Definition: elf_common.h:561
re2::operator++
static void operator++(Engine &e, int unused)
Definition: bloaty/third_party/re2/re2/testing/tester.h:39
STB_GLOBAL
#define STB_GLOBAL
Definition: elf_common.h:801
value
const char * value
Definition: hpack_parser_table.cc:165
DT_STRTAB
#define DT_STRTAB
Definition: elf_common.h:560
ELFMAG2
#define ELFMAG2
Definition: elf_common.h:142
FATAL
#define FATAL(msg)
Definition: task.h:88
EI_MAG1
#define EI_MAG1
Definition: elf_common.h:127
grpc_core::operator*
Duration operator*(Duration lhs, double rhs)
Definition: src/core/lib/gprpp/time.h:257
DT_HASH
#define DT_HASH
Definition: elf_common.h:559
SHN_LORESERVE
#define SHN_LORESERVE
Definition: elf_common.h:388
DT_VERDEFNUM
#define DT_VERDEFNUM
Definition: elf_common.h:645
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
ELFMAG1
#define ELFMAG1
Definition: elf_common.h:141
ELFCLASS32
#define ELFCLASS32
Definition: elf_common.h:153
L
lua_State * L
Definition: upb/upb/bindings/lua/main.c:35
ELFMAG0
#define ELFMAG0
Definition: elf_common.h:140
ELF64_ST_TYPE
#define ELF64_ST_TYPE(info)
Definition: elf64.h:217
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
ELF32_ST_TYPE
#define ELF32_ST_TYPE(info)
Definition: elf32.h:210
create_matrix_images.image
image
Definition: create_matrix_images.py:352
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
EI_MAG2
#define EI_MAG2
Definition: elf_common.h:128
EI_CLASS
#define EI_CLASS
Definition: elf_common.h:130
ABSL_RAW_LOG
#define ABSL_RAW_LOG(severity,...)
Definition: abseil-cpp/absl/base/internal/raw_logging.h:44
ELFDATA2LSB
#define ELFDATA2LSB
Definition: elf_common.h:158
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
offset
voidpf uLong offset
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:142
ELFDATA2MSB
#define ELFDATA2MSB
Definition: elf_common.h:159


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