Coverage Report

Created: 2023-09-25 06:27

/src/abseil-cpp/absl/debugging/internal/elf_mem_image.cc
Line
Count
Source (jump to first uncovered line)
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
0
#define VERSYM_VERSION 0x7fff
40
41
namespace absl {
42
ABSL_NAMESPACE_BEGIN
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
0
int ElfBind(const ElfW(Sym) *symbol) { return ELF64_ST_BIND(symbol->st_info); }
54
0
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
0
                         ElfW(Word) element_size, size_t index) {
73
0
  return reinterpret_cast<const T*>(reinterpret_cast<const char *>(ehdr)
74
0
                                    + table_offset
75
0
                                    + index * element_size);
76
0
}
Unexecuted instantiation: elf_mem_image.cc:Elf64_Phdr const* absl::debugging_internal::(anonymous namespace)::GetTableElement<Elf64_Phdr>(Elf64_Ehdr const*, unsigned long, unsigned int, unsigned long)
Unexecuted instantiation: elf_mem_image.cc:char const* absl::debugging_internal::(anonymous namespace)::GetTableElement<char>(Elf64_Ehdr const*, unsigned long, unsigned int, unsigned long)
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
0
ElfMemImage::ElfMemImage(const void *base) {
85
0
  ABSL_RAW_CHECK(base != kInvalidBase, "bad pointer");
86
0
  Init(base);
87
0
}
88
89
0
int ElfMemImage::GetNumSymbols() const {
90
0
  if (!hash_) {
91
0
    return 0;
92
0
  }
93
  // See http://www.caldera.com/developers/gabi/latest/ch5.dynamic.html#hash
94
0
  return static_cast<int>(hash_[1]);
95
0
}
96
97
0
const ElfW(Sym) *ElfMemImage::GetDynsym(int index) const {
98
0
  ABSL_RAW_CHECK(index < GetNumSymbols(), "index out of range");
99
0
  return dynsym_ + index;
100
0
}
101
102
0
const ElfW(Versym) *ElfMemImage::GetVersym(int index) const {
103
0
  ABSL_RAW_CHECK(index < GetNumSymbols(), "index out of range");
104
0
  return versym_ + index;
105
0
}
106
107
0
const ElfW(Phdr) *ElfMemImage::GetPhdr(int index) const {
108
0
  ABSL_RAW_CHECK(index >= 0 && index < ehdr_->e_phnum, "index out of range");
109
0
  return GetTableElement<ElfW(Phdr)>(ehdr_, ehdr_->e_phoff, ehdr_->e_phentsize,
110
0
                                     static_cast<size_t>(index));
111
0
}
112
113
0
const char *ElfMemImage::GetDynstr(ElfW(Word) offset) const {
114
0
  ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
115
0
  return dynstr_ + offset;
116
0
}
117
118
0
const void *ElfMemImage::GetSymAddr(const ElfW(Sym) *sym) const {
119
0
  if (sym->st_shndx == SHN_UNDEF || sym->st_shndx >= SHN_LORESERVE) {
120
    // Symbol corresponds to "special" (e.g. SHN_ABS) section.
121
0
    return reinterpret_cast<const void *>(sym->st_value);
122
0
  }
123
0
  ABSL_RAW_CHECK(link_base_ < sym->st_value, "symbol out of range");
124
0
  return GetTableElement<char>(ehdr_, 0, 1, sym->st_value - link_base_);
125
0
}
126
127
0
const ElfW(Verdef) *ElfMemImage::GetVerdef(int index) const {
128
0
  ABSL_RAW_CHECK(0 <= index && static_cast<size_t>(index) <= verdefnum_,
129
0
                 "index out of range");
130
0
  const ElfW(Verdef) *version_definition = verdef_;
131
0
  while (version_definition->vd_ndx < index && version_definition->vd_next) {
132
0
    const char *const version_definition_as_char =
133
0
        reinterpret_cast<const char *>(version_definition);
134
0
    version_definition =
135
0
        reinterpret_cast<const ElfW(Verdef) *>(version_definition_as_char +
136
0
                                               version_definition->vd_next);
137
0
  }
138
0
  return version_definition->vd_ndx == index ? version_definition : nullptr;
139
0
}
140
141
const ElfW(Verdaux) *ElfMemImage::GetVerdefAux(
142
0
    const ElfW(Verdef) *verdef) const {
143
0
  return reinterpret_cast<const ElfW(Verdaux) *>(verdef+1);
144
0
}
145
146
0
const char *ElfMemImage::GetVerstr(ElfW(Word) offset) const {
147
0
  ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
148
0
  return dynstr_ + offset;
149
0
}
150
151
0
void ElfMemImage::Init(const void *base) {
152
0
  ehdr_      = nullptr;
153
0
  dynsym_    = nullptr;
154
0
  dynstr_    = nullptr;
155
0
  versym_    = nullptr;
156
0
  verdef_    = nullptr;
157
0
  hash_      = nullptr;
158
0
  strsize_   = 0;
159
0
  verdefnum_ = 0;
160
  // Sentinel: PT_LOAD .p_vaddr can't possibly be this.
161
0
  link_base_ = ~ElfW(Addr){0};  // NOLINT(readability/braces)
162
0
  if (!base) {
163
0
    return;
164
0
  }
165
0
  const char *const base_as_char = reinterpret_cast<const char *>(base);
166
0
  if (base_as_char[EI_MAG0] != ELFMAG0 || base_as_char[EI_MAG1] != ELFMAG1 ||
167
0
      base_as_char[EI_MAG2] != ELFMAG2 || base_as_char[EI_MAG3] != ELFMAG3) {
168
0
    assert(false);
169
0
    return;
170
0
  }
171
0
  int elf_class = base_as_char[EI_CLASS];
172
0
  if (elf_class != kElfClass) {
173
0
    assert(false);
174
0
    return;
175
0
  }
176
0
  switch (base_as_char[EI_DATA]) {
177
0
    case ELFDATA2LSB: {
178
#ifndef ABSL_IS_LITTLE_ENDIAN
179
      assert(false);
180
      return;
181
#endif
182
0
      break;
183
0
    }
184
0
    case ELFDATA2MSB: {
185
0
#ifndef ABSL_IS_BIG_ENDIAN
186
0
      assert(false);
187
0
      return;
188
0
#endif
189
0
      break;
190
0
    }
191
0
    default: {
192
0
      assert(false);
193
0
      return;
194
0
    }
195
0
  }
196
197
0
  ehdr_ = reinterpret_cast<const ElfW(Ehdr) *>(base);
198
0
  const ElfW(Phdr) *dynamic_program_header = nullptr;
199
0
  for (int i = 0; i < ehdr_->e_phnum; ++i) {
200
0
    const ElfW(Phdr) *const program_header = GetPhdr(i);
201
0
    switch (program_header->p_type) {
202
0
      case PT_LOAD:
203
0
        if (!~link_base_) {
204
0
          link_base_ = program_header->p_vaddr;
205
0
        }
206
0
        break;
207
0
      case PT_DYNAMIC:
208
0
        dynamic_program_header = program_header;
209
0
        break;
210
0
    }
211
0
  }
212
0
  if (!~link_base_ || !dynamic_program_header) {
213
0
    assert(false);
214
    // Mark this image as not present. Can not recur infinitely.
215
0
    Init(nullptr);
216
0
    return;
217
0
  }
218
0
  ptrdiff_t relocation =
219
0
      base_as_char - reinterpret_cast<const char *>(link_base_);
220
0
  ElfW(Dyn)* dynamic_entry = reinterpret_cast<ElfW(Dyn)*>(
221
0
      static_cast<intptr_t>(dynamic_program_header->p_vaddr) + relocation);
222
0
  for (; dynamic_entry->d_tag != DT_NULL; ++dynamic_entry) {
223
0
    const auto value =
224
0
        static_cast<intptr_t>(dynamic_entry->d_un.d_val) + relocation;
225
0
    switch (dynamic_entry->d_tag) {
226
0
      case DT_HASH:
227
0
        hash_ = reinterpret_cast<ElfW(Word) *>(value);
228
0
        break;
229
0
      case DT_SYMTAB:
230
0
        dynsym_ = reinterpret_cast<ElfW(Sym) *>(value);
231
0
        break;
232
0
      case DT_STRTAB:
233
0
        dynstr_ = reinterpret_cast<const char *>(value);
234
0
        break;
235
0
      case DT_VERSYM:
236
0
        versym_ = reinterpret_cast<ElfW(Versym) *>(value);
237
0
        break;
238
0
      case DT_VERDEF:
239
0
        verdef_ = reinterpret_cast<ElfW(Verdef) *>(value);
240
0
        break;
241
0
      case DT_VERDEFNUM:
242
0
        verdefnum_ = static_cast<size_t>(dynamic_entry->d_un.d_val);
243
0
        break;
244
0
      case DT_STRSZ:
245
0
        strsize_ = static_cast<size_t>(dynamic_entry->d_un.d_val);
246
0
        break;
247
0
      default:
248
        // Unrecognized entries explicitly ignored.
249
0
        break;
250
0
    }
251
0
  }
252
0
  if (!hash_ || !dynsym_ || !dynstr_ || !versym_ ||
253
0
      !verdef_ || !verdefnum_ || !strsize_) {
254
0
    assert(false);  // invalid VDSO
255
    // Mark this image as not present. Can not recur infinitely.
256
0
    Init(nullptr);
257
0
    return;
258
0
  }
259
0
}
260
261
bool ElfMemImage::LookupSymbol(const char *name,
262
                               const char *version,
263
                               int type,
264
0
                               SymbolInfo *info_out) const {
265
0
  for (const SymbolInfo& info : *this) {
266
0
    if (strcmp(info.name, name) == 0 && strcmp(info.version, version) == 0 &&
267
0
        ElfType(info.symbol) == type) {
268
0
      if (info_out) {
269
0
        *info_out = info;
270
0
      }
271
0
      return true;
272
0
    }
273
0
  }
274
0
  return false;
275
0
}
276
277
bool ElfMemImage::LookupSymbolByAddress(const void *address,
278
0
                                        SymbolInfo *info_out) const {
279
0
  for (const SymbolInfo& info : *this) {
280
0
    const char *const symbol_start =
281
0
        reinterpret_cast<const char *>(info.address);
282
0
    const char *const symbol_end = symbol_start + info.symbol->st_size;
283
0
    if (symbol_start <= address && address < symbol_end) {
284
0
      if (info_out) {
285
        // Client wants to know details for that symbol (the usual case).
286
0
        if (ElfBind(info.symbol) == STB_GLOBAL) {
287
          // Strong symbol; just return it.
288
0
          *info_out = info;
289
0
          return true;
290
0
        } else {
291
          // Weak or local. Record it, but keep looking for a strong one.
292
0
          *info_out = info;
293
0
        }
294
0
      } else {
295
        // Client only cares if there is an overlapping symbol.
296
0
        return true;
297
0
      }
298
0
    }
299
0
  }
300
0
  return false;
301
0
}
302
303
ElfMemImage::SymbolIterator::SymbolIterator(const void *const image, int index)
304
0
    : index_(index), image_(image) {
305
0
}
306
307
0
const ElfMemImage::SymbolInfo *ElfMemImage::SymbolIterator::operator->() const {
308
0
  return &info_;
309
0
}
310
311
0
const ElfMemImage::SymbolInfo& ElfMemImage::SymbolIterator::operator*() const {
312
0
  return info_;
313
0
}
314
315
0
bool ElfMemImage::SymbolIterator::operator==(const SymbolIterator &rhs) const {
316
0
  return this->image_ == rhs.image_ && this->index_ == rhs.index_;
317
0
}
318
319
0
bool ElfMemImage::SymbolIterator::operator!=(const SymbolIterator &rhs) const {
320
0
  return !(*this == rhs);
321
0
}
322
323
0
ElfMemImage::SymbolIterator &ElfMemImage::SymbolIterator::operator++() {
324
0
  this->Update(1);
325
0
  return *this;
326
0
}
327
328
0
ElfMemImage::SymbolIterator ElfMemImage::begin() const {
329
0
  SymbolIterator it(this, 0);
330
0
  it.Update(0);
331
0
  return it;
332
0
}
333
334
0
ElfMemImage::SymbolIterator ElfMemImage::end() const {
335
0
  return SymbolIterator(this, GetNumSymbols());
336
0
}
337
338
0
void ElfMemImage::SymbolIterator::Update(int increment) {
339
0
  const ElfMemImage *image = reinterpret_cast<const ElfMemImage *>(image_);
340
0
  ABSL_RAW_CHECK(image->IsPresent() || increment == 0, "");
341
0
  if (!image->IsPresent()) {
342
0
    return;
343
0
  }
344
0
  index_ += increment;
345
0
  if (index_ >= image->GetNumSymbols()) {
346
0
    index_ = image->GetNumSymbols();
347
0
    return;
348
0
  }
349
0
  const ElfW(Sym)    *symbol = image->GetDynsym(index_);
350
0
  const ElfW(Versym) *version_symbol = image->GetVersym(index_);
351
0
  ABSL_RAW_CHECK(symbol && version_symbol, "");
352
0
  const char *const symbol_name = image->GetDynstr(symbol->st_name);
353
#if defined(__NetBSD__)
354
  const int version_index = version_symbol->vs_vers & VERSYM_VERSION;
355
#else
356
0
  const ElfW(Versym) version_index = version_symbol[0] & VERSYM_VERSION;
357
0
#endif
358
0
  const ElfW(Verdef) *version_definition = nullptr;
359
0
  const char *version_name = "";
360
0
  if (symbol->st_shndx == SHN_UNDEF) {
361
    // Undefined symbols reference DT_VERNEED, not DT_VERDEF, and
362
    // version_index could well be greater than verdefnum_, so calling
363
    // GetVerdef(version_index) may trigger assertion.
364
0
  } else {
365
0
    version_definition = image->GetVerdef(version_index);
366
0
  }
367
0
  if (version_definition) {
368
    // I am expecting 1 or 2 auxiliary entries: 1 for the version itself,
369
    // optional 2nd if the version has a parent.
370
0
    ABSL_RAW_CHECK(
371
0
        version_definition->vd_cnt == 1 || version_definition->vd_cnt == 2,
372
0
        "wrong number of entries");
373
0
    const ElfW(Verdaux) *version_aux = image->GetVerdefAux(version_definition);
374
0
    version_name = image->GetVerstr(version_aux->vda_name);
375
0
  }
376
0
  info_.name    = symbol_name;
377
0
  info_.version = version_name;
378
0
  info_.address = image->GetSymAddr(symbol);
379
0
  info_.symbol  = symbol;
380
0
}
381
382
}  // namespace debugging_internal
383
ABSL_NAMESPACE_END
384
}  // namespace absl
385
386
#endif  // ABSL_HAVE_ELF_MEM_IMAGE