Coverage Report

Created: 2026-10-28 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/debugging/symbolize_elf.inc
Line
Count
Source
1
// Copyright 2018 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
// This library provides Symbolize() function that symbolizes program
16
// counters to their corresponding symbol names on linux platforms.
17
// This library has a minimal implementation of an ELF symbol table
18
// reader (i.e. it doesn't depend on libelf, etc.).
19
//
20
// The algorithm used in Symbolize() is as follows.
21
//
22
//   1. Go through a list of maps in /proc/self/maps and find the map
23
//   containing the program counter.
24
//
25
//   2. Open the mapped file and find a regular symbol table inside.
26
//   Iterate over symbols in the symbol table and look for the symbol
27
//   containing the program counter.  If such a symbol is found,
28
//   obtain the symbol name, and demangle the symbol if possible.
29
//   If the symbol isn't found in the regular symbol table (binary is
30
//   stripped), try the same thing with a dynamic symbol table.
31
//
32
// Note that Symbolize() is originally implemented to be used in
33
// signal handlers, hence it doesn't use malloc() and other unsafe
34
// operations.  It should be both thread-safe and async-signal-safe.
35
//
36
// Implementation note:
37
//
38
// We don't use heaps but only use stacks.  We want to reduce the
39
// stack consumption so that the symbolizer can run on small stacks.
40
//
41
// Here are some numbers collected with GCC 4.1.0 on x86:
42
// - sizeof(Elf32_Sym)  = 16
43
// - sizeof(Elf32_Shdr) = 40
44
// - sizeof(Elf64_Sym)  = 24
45
// - sizeof(Elf64_Shdr) = 64
46
//
47
// This implementation is intended to be async-signal-safe but uses some
48
// functions which are not guaranteed to be so, such as memchr() and
49
// memmove().  We assume they are async-signal-safe.
50
51
#include <dlfcn.h>
52
#include <elf.h>
53
#include <fcntl.h>
54
#include <link.h>  // For ElfW() macro.
55
#include <sys/resource.h>
56
#include <sys/stat.h>
57
#include <sys/types.h>
58
#include <unistd.h>
59
60
#include <algorithm>
61
#include <array>
62
#include <atomic>
63
#include <cerrno>
64
#include <cinttypes>
65
#include <climits>
66
#include <cstdint>
67
#include <cstdio>
68
#include <cstdlib>
69
#include <cstring>
70
#include <iterator>
71
#include <memory>
72
73
#include "absl/base/casts.h"
74
#include "absl/base/dynamic_annotations.h"
75
#include "absl/base/internal/low_level_alloc.h"
76
#include "absl/base/internal/raw_logging.h"
77
#include "absl/base/internal/spinlock.h"
78
#include "absl/base/port.h"
79
#include "absl/debugging/internal/demangle.h"
80
#include "absl/debugging/internal/vdso_support.h"
81
#include "absl/strings/string_view.h"
82
83
namespace absl {
84
ABSL_NAMESPACE_BEGIN
85
86
// Value of argv[0]. Used by MaybeInitializeObjFile().
87
static char *argv0_value = nullptr;
88
89
0
void InitializeSymbolizer(const char *argv0) {
90
0
#ifdef ABSL_HAVE_VDSO_SUPPORT
91
  // We need to make sure VDSOSupport::Init() is called before any setuid or
92
  // chroot calls, so InitializeSymbolizer() should be called very early in the
93
  // life of a program.
94
0
  absl::debugging_internal::VDSOSupport::Init();
95
0
#endif
96
0
  if (argv0_value != nullptr) {
97
0
    free(argv0_value);
98
0
    argv0_value = nullptr;
99
0
  }
100
0
  if (argv0 != nullptr && argv0[0] != '\0') {
101
0
    argv0_value = strdup(argv0);
102
0
  }
103
0
}
104
105
namespace debugging_internal {
106
namespace {
107
108
// Re-runs fn until it doesn't cause EINTR.
109
#define NO_INTR(fn) \
110
0
  do {              \
111
0
  } while ((fn) < 0 && errno == EINTR)
112
113
// On Linux, ELF_ST_* are defined in <linux/elf.h>.  To make this portable
114
// we define our own ELF_ST_BIND and ELF_ST_TYPE if not available.
115
#ifndef ELF_ST_BIND
116
0
#define ELF_ST_BIND(info) (((unsigned char)(info)) >> 4)
117
#endif
118
119
#ifndef ELF_ST_TYPE
120
0
#define ELF_ST_TYPE(info) (((unsigned char)(info)) & 0xF)
121
#endif
122
123
// Some platforms use a special .opd section to store function pointers.
124
const char kOpdSectionName[] = ".opd";
125
126
#if defined(__powerpc64__) && defined(_CALL_ELF)
127
#if _CALL_ELF <= 1
128
#define ABSL_INTERNAL_HAVE_PPC64_ELFV1_ABI 1
129
#endif
130
#endif
131
#if defined(ABSL_INTERNAL_HAVE_PPC64_ELFV1_ABI) || defined(__ia64)
132
// Use opd section for function descriptors on these platforms, the function
133
// address is the first word of the descriptor.
134
//
135
// https://maskray.me/blog/2023-02-26-linker-notes-on-power-isa notes that
136
// opd sections are used on 64-bit PowerPC with the ELFv1 ABI.
137
inline constexpr bool kPlatformUsesOPDSections = true;
138
#else
139
inline constexpr bool kPlatformUsesOPDSections = false;
140
#endif
141
142
// This works for PowerPC & IA64 only.  A function descriptor consist of two
143
// pointers and the first one is the function's entry.
144
const size_t kFunctionDescriptorSize = sizeof(void *) * 2;
145
146
std::atomic<SymbolDecorator::Factory*> g_decorator_factory = nullptr;
147
148
struct FileMappingHint {
149
  const void *start;
150
  const void *end;
151
  uint64_t offset;
152
  const char *filename;
153
};
154
155
const int kMaxFileMappingHints = 8;
156
int g_num_file_mapping_hints;
157
FileMappingHint g_file_mapping_hints[kMaxFileMappingHints];
158
// Protects g_file_mapping_hints.
159
ABSL_CONST_INIT absl::base_internal::SpinLock g_file_mapping_mu(
160
    absl::base_internal::SCHEDULE_KERNEL_ONLY);
161
162
// Async-signal-safe function to zero a buffer.
163
// memset() is not guaranteed to be async-signal-safe.
164
0
static void SafeMemZero(void *p, size_t size) {
165
0
  unsigned char *c = static_cast<unsigned char *>(p);
166
0
  while (size--) {
167
0
    *c++ = 0;
168
0
  }
169
0
}
170
171
struct ObjFile {
172
  char *filename = nullptr;
173
  const void *start_addr = nullptr;
174
  const void *end_addr = nullptr;
175
  uint64_t offset = 0;
176
177
  // The following fields are initialized on the first access to the
178
  // object file.
179
  int fd = -1;
180
  int elf_type = -1;
181
  ElfW(Ehdr) elf_header = {};
182
183
  // PT_LOAD program header describing executable code.
184
  // Normally we expect just one, but SWIFT binaries have two.
185
  // CUDA binaries have 3 (see cr/473913254 description).
186
  std::array<ElfW(Phdr), 4> phdr = {};
187
188
  SymbolDecoratorPtr decorator = {};
189
};
190
191
// Build 4-way associative cache for symbols. Within each cache line, symbols
192
// are replaced in LRU order.
193
enum {
194
  ASSOCIATIVITY = 4,
195
};
196
struct SymbolCacheLine {
197
  const void *pc[ASSOCIATIVITY];
198
  char *name[ASSOCIATIVITY];
199
200
  // age[i] is incremented when a line is accessed. it's reset to zero if the
201
  // i'th entry is read.
202
  uint32_t age[ASSOCIATIVITY];
203
};
204
205
// ---------------------------------------------------------------
206
// An AddrMap is a vector of ObjFile, using SigSafeArena() for allocation.
207
208
class AddrMap {
209
 public:
210
0
  AddrMap() : size_(0), allocated_(0), obj_(nullptr) {}
211
0
  ~AddrMap() { base_internal::LowLevelAlloc::Free(obj_); }
212
0
  size_t Size() const { return size_; }
213
0
  ObjFile *At(size_t i) { return &obj_[i]; }
214
  ObjFile *Add();
215
  void Clear();
216
217
 private:
218
  size_t size_;       // count of valid elements (<= allocated_)
219
  size_t allocated_;  // count of allocated elements
220
  ObjFile *obj_;      // array of allocated_ elements
221
  AddrMap(const AddrMap &) = delete;
222
  AddrMap &operator=(const AddrMap &) = delete;
223
};
224
225
0
void AddrMap::Clear() {
226
0
  for (size_t i = 0; i != size_; i++) {
227
0
    At(i)->~ObjFile();
228
0
  }
229
0
  size_ = 0;
230
0
}
231
232
0
ObjFile *AddrMap::Add() {
233
0
  if (size_ == allocated_) {
234
0
    size_t new_allocated = allocated_ * 2 + 50;
235
0
    ObjFile *new_obj_ =
236
0
        static_cast<ObjFile *>(base_internal::LowLevelAlloc::AllocWithArena(
237
0
            new_allocated * sizeof(*new_obj_), base_internal::SigSafeArena()));
238
0
    if (obj_) {
239
0
      std::uninitialized_move_n(obj_, size_, new_obj_);
240
0
      std::destroy_n(obj_, size_);
241
0
      base_internal::LowLevelAlloc::Free(obj_);
242
0
    }
243
0
    obj_ = new_obj_;
244
0
    allocated_ = new_allocated;
245
0
  }
246
0
  return new (&obj_[size_++]) ObjFile;
247
0
}
248
249
class CachingFile {
250
 public:
251
  // Setup reader for fd that uses buf[0, buf_size-1] as a cache.
252
  CachingFile(int fd, char *buf, size_t buf_size)
253
0
      : fd_(fd),
254
0
        cache_(buf),
255
0
        cache_size_(buf_size),
256
0
        cache_start_(0),
257
0
        cache_limit_(0) {}
258
259
0
  int fd() const { return fd_; }
260
  ssize_t ReadFromOffset(void *buf, size_t count, off_t offset);
261
  bool ReadFromOffsetExact(void *buf, size_t count, off_t offset);
262
263
 private:
264
  // Bytes [cache_start_, cache_limit_-1] from fd_ are stored in
265
  // a prefix of cache_[0, cache_size_-1].
266
  int fd_;
267
  char *cache_;
268
  size_t cache_size_;
269
  off_t cache_start_;
270
  off_t cache_limit_;
271
};
272
273
// ---------------------------------------------------------------
274
275
enum FindSymbolResult { SYMBOL_NOT_FOUND = 1, SYMBOL_TRUNCATED, SYMBOL_FOUND };
276
277
class Symbolizer {
278
 public:
279
  Symbolizer();
280
  ~Symbolizer();
281
  const char *GetSymbol(const void *const pc);
282
283
  void UpdateDecoratorsIfNeeded(SymbolDecorator::Factory* decorator_factory);
284
285
 private:
286
0
  char *CopyString(const char *s) {
287
0
    size_t len = strlen(s);
288
0
    char *dst =
289
0
        static_cast<char *>(base_internal::LowLevelAlloc::AllocWithArena(
290
0
            len + 1, base_internal::SigSafeArena()));
291
0
    ABSL_RAW_CHECK(dst != nullptr, "out of memory");
292
0
    memcpy(dst, s, len + 1);
293
0
    return dst;
294
0
  }
295
  ABSL_ATTRIBUTE_NOINLINE ObjFile* FindObjFile(const void* const start,
296
                                               size_t size);
297
  static bool RegisterObjFile(const char *filename,
298
                              const void *const start_addr,
299
                              const void *const end_addr, uint64_t offset,
300
                              void *arg);
301
  SymbolCacheLine *GetCacheLine(const void *const pc);
302
  const char *FindSymbolInCache(const void *const pc);
303
  const char *InsertSymbolInCache(const void *const pc, const char *name);
304
  void AgeSymbols(SymbolCacheLine *line);
305
  void ClearAddrMap();
306
  FindSymbolResult GetSymbolFromObjectFile(const ObjFile &obj,
307
                                           const void *const pc,
308
                                           const ptrdiff_t relocation,
309
                                           char *out, size_t out_size,
310
                                           char *tmp_buf, size_t tmp_buf_size);
311
  const char *GetUncachedSymbol(const void *pc);
312
313
  enum {
314
    SYMBOL_BUF_SIZE = 3072,
315
    TMP_BUF_SIZE = 1024,
316
    SYMBOL_CACHE_LINES = 128,
317
    FILE_CACHE_SIZE = 8192,
318
  };
319
320
  AddrMap addr_map_;
321
  SymbolDecorator::Factory* decorator_factory_ = nullptr;
322
323
  bool ok_;
324
  bool addr_map_read_;
325
326
  char symbol_buf_[SYMBOL_BUF_SIZE];
327
  char file_cache_[FILE_CACHE_SIZE];
328
329
  // tmp_buf_ will be used to store arrays of ElfW(Shdr) and ElfW(Sym)
330
  // so we ensure that tmp_buf_ is properly aligned to store either.
331
  alignas(16) char tmp_buf_[TMP_BUF_SIZE];
332
  static_assert(alignof(ElfW(Shdr)) <= 16,
333
                "alignment of tmp buf too small for Shdr");
334
  static_assert(alignof(ElfW(Sym)) <= 16,
335
                "alignment of tmp buf too small for Sym");
336
337
  SymbolCacheLine symbol_cache_[SYMBOL_CACHE_LINES];
338
};
339
340
// Protect against client code closing low-valued file descriptors it doesn't
341
// actually own.
342
0
int OpenReadOnlyWithHighFD(const char *fname) {
343
0
  static int high_fd = [] {
344
0
    struct rlimit rlim{};
345
0
    const int rc = getrlimit(RLIMIT_NOFILE, &rlim);
346
0
    if (rc == 0 && rlim.rlim_cur >= 2000) {
347
0
      const int max_fd = static_cast<int>(rlim.rlim_cur);
348
349
      // This will return 2000 on reasonably-configured systems.
350
0
      return std::min<int>(2000, max_fd - 1000);
351
0
    }
352
0
    ABSL_RAW_LOG(WARNING, "Unable to get high fd: rc=%d, limit=%ld",  //
353
0
                 rc, static_cast<long>(rlim.rlim_cur));
354
0
    return -1;
355
0
  }();
356
0
  constexpr int kOpenFlags = O_RDONLY | O_CLOEXEC;
357
0
  if (high_fd >= 1000) {
358
0
    const int fd = open(fname, kOpenFlags);
359
0
    if (fd != -1 && fd < high_fd) {
360
      // Try to relocate fd to high range.
361
0
      static_assert(kOpenFlags & O_CLOEXEC,
362
0
                    "F_DUPFD_CLOEXEC assumes O_CLOEXEC");
363
0
      const int fd2 = fcntl(fd, F_DUPFD_CLOEXEC, high_fd);
364
0
      if (fd2 != -1) {
365
        // Successfully obtained high fd. Use it.
366
0
        close(fd);
367
0
        return fd2;
368
0
      } else {
369
0
        ABSL_RAW_LOG(WARNING, "Unable to dup fd=%d above %d, errno=%d", fd,
370
0
                     high_fd, errno);
371
0
      }
372
0
    }
373
    // Either open failed and fd==-1, or fd is already above high_fd, or fcntl
374
    // failed and fd is valid (but low).
375
0
    return fd;
376
0
  }
377
0
  return open(fname, kOpenFlags);
378
0
}
379
380
static std::atomic<Symbolizer *> g_cached_symbolizer;
381
382
}  // namespace
383
384
0
static size_t SymbolizerSize() {
385
0
  auto pagesize = static_cast<size_t>(sysconf(_SC_PAGESIZE));
386
0
  return ((sizeof(Symbolizer) - 1) / pagesize + 1) * pagesize;
387
0
}
388
389
// Return (and set null) g_cached_symbolized_state if it is not null.
390
// Otherwise return a new symbolizer.
391
0
static Symbolizer *AllocateSymbolizer() {
392
0
  base_internal::InitSigSafeArena();
393
0
  Symbolizer *symbolizer =
394
0
      g_cached_symbolizer.exchange(nullptr, std::memory_order_acquire);
395
0
  if (symbolizer == nullptr) {
396
0
    void* ptr = base_internal::LowLevelAlloc::AllocWithArena(
397
0
        SymbolizerSize(), base_internal::SigSafeArena());
398
0
    symbolizer = new (ptr) Symbolizer();
399
0
  }
400
0
  symbolizer->UpdateDecoratorsIfNeeded(
401
0
      g_decorator_factory.load(std::memory_order_acquire));
402
0
  return symbolizer;
403
0
}
404
405
// Set g_cached_symbolize_state to s if it is null, otherwise
406
// delete s.
407
0
static void FreeSymbolizer(Symbolizer *s) {
408
0
  Symbolizer *old_cached_symbolizer = nullptr;
409
0
  if (!g_cached_symbolizer.compare_exchange_strong(old_cached_symbolizer, s,
410
0
                                                   std::memory_order_release,
411
0
                                                   std::memory_order_relaxed)) {
412
0
    s->~Symbolizer();
413
0
    base_internal::LowLevelAlloc::Free(s);
414
0
  }
415
0
}
416
417
0
Symbolizer::Symbolizer() : ok_(true), addr_map_read_(false) {
418
0
  for (SymbolCacheLine &symbol_cache_line : symbol_cache_) {
419
0
    for (size_t j = 0; j < std::size(symbol_cache_line.name); ++j) {
420
0
      symbol_cache_line.pc[j] = nullptr;
421
0
      symbol_cache_line.name[j] = nullptr;
422
0
      symbol_cache_line.age[j] = 0;
423
0
    }
424
0
  }
425
0
}
426
427
0
Symbolizer::~Symbolizer() {
428
0
  for (SymbolCacheLine &symbol_cache_line : symbol_cache_) {
429
0
    for (char *s : symbol_cache_line.name) {
430
0
      base_internal::LowLevelAlloc::Free(s);
431
0
    }
432
0
  }
433
0
  ClearAddrMap();
434
0
}
435
436
void Symbolizer::UpdateDecoratorsIfNeeded(
437
0
    SymbolDecorator::Factory* decorator_factory) {
438
0
  if (decorator_factory_ == decorator_factory) return;
439
440
0
  for (SymbolCacheLine& symbol_cache_line : symbol_cache_) {
441
0
    for (size_t j = 0; j < std::size(symbol_cache_line.name); ++j) {
442
0
      symbol_cache_line.pc[j] = nullptr;
443
0
      base_internal::LowLevelAlloc::Free(symbol_cache_line.name[j]);
444
0
      symbol_cache_line.name[j] = nullptr;
445
0
      symbol_cache_line.age[j] = 0;
446
0
    }
447
0
  }
448
449
0
  decorator_factory_ = decorator_factory;
450
0
  for (size_t i = 0, end = addr_map_.Size(); i < end; ++i) {
451
0
    ObjFile* obj = addr_map_.At(i);
452
0
    obj->decorator.reset();
453
0
  }
454
0
}
455
456
// We don't use assert() since it's not guaranteed to be
457
// async-signal-safe.  Instead we define a minimal assertion
458
// macro. So far, we don't need pretty printing for __FILE__, etc.
459
0
#define SAFE_ASSERT(expr) ((expr) ? static_cast<void>(0) : abort())
460
461
// Read up to "count" bytes from file descriptor "fd" into the buffer
462
// starting at "buf" while handling short reads and EINTR.  On
463
// success, return the number of bytes read.  Otherwise, return -1.
464
0
static ssize_t ReadPersistent(int fd, void *buf, size_t count) {
465
0
  SAFE_ASSERT(fd >= 0);
466
0
  SAFE_ASSERT(count <= SSIZE_MAX);
467
0
  char *buf0 = reinterpret_cast<char *>(buf);
468
0
  size_t num_bytes = 0;
469
0
  while (num_bytes < count) {
470
0
    ssize_t len;
471
0
    NO_INTR(len = read(fd, buf0 + num_bytes, count - num_bytes));
472
0
    if (len < 0) {  // There was an error other than EINTR.
473
0
      ABSL_RAW_LOG(WARNING, "read failed: errno=%d", errno);
474
0
      return -1;
475
0
    }
476
0
    if (len == 0) {  // Reached EOF.
477
0
      break;
478
0
    }
479
0
    num_bytes += static_cast<size_t>(len);
480
0
  }
481
0
  SAFE_ASSERT(num_bytes <= count);
482
0
  return static_cast<ssize_t>(num_bytes);
483
0
}
484
485
// Read up to "count" bytes from "offset" into the buffer starting at "buf",
486
// while handling short reads and EINTR.  On success, return the number of bytes
487
// read.  Otherwise, return -1.
488
0
ssize_t CachingFile::ReadFromOffset(void *buf, size_t count, off_t offset) {
489
0
  char *dst = static_cast<char *>(buf);
490
0
  size_t read = 0;
491
0
  while (read < count) {
492
    // Look in cache first.
493
0
    if (offset >= cache_start_ && offset < cache_limit_) {
494
0
      const char *hit_start = &cache_[offset - cache_start_];
495
0
      const size_t n =
496
0
          std::min(count - read, static_cast<size_t>(cache_limit_ - offset));
497
0
      memcpy(dst, hit_start, n);
498
0
      dst += n;
499
0
      read += static_cast<size_t>(n);
500
0
      offset += static_cast<off_t>(n);
501
0
      continue;
502
0
    }
503
504
0
    cache_start_ = 0;
505
0
    cache_limit_ = 0;
506
0
    ssize_t n = pread(fd_, cache_, cache_size_, offset);
507
0
    if (n < 0) {
508
0
      if (errno == EINTR) {
509
0
        continue;
510
0
      }
511
0
      ABSL_RAW_LOG(WARNING, "read failed: errno=%d", errno);
512
0
      return -1;
513
0
    }
514
0
    if (n == 0) {  // Reached EOF.
515
0
      break;
516
0
    }
517
518
0
    cache_start_ = offset;
519
0
    cache_limit_ = offset + static_cast<off_t>(n);
520
    // Next iteration will copy from cache into dst.
521
0
  }
522
0
  return static_cast<ssize_t>(read);
523
0
}
524
525
// Try reading exactly "count" bytes from "offset" bytes into the buffer
526
// starting at "buf" while handling short reads and EINTR.  On success, return
527
// true. Otherwise, return false.
528
0
bool CachingFile::ReadFromOffsetExact(void *buf, size_t count, off_t offset) {
529
0
  ssize_t len = ReadFromOffset(buf, count, offset);
530
0
  return len >= 0 && static_cast<size_t>(len) == count;
531
0
}
532
533
// Returns elf_header.e_type if the file pointed by fd is an ELF binary.
534
0
static int FileGetElfType(CachingFile *file) {
535
0
  ElfW(Ehdr) elf_header;
536
0
  if (!file->ReadFromOffsetExact(&elf_header, sizeof(elf_header), 0)) {
537
0
    return -1;
538
0
  }
539
0
  if (memcmp(elf_header.e_ident, ELFMAG, SELFMAG) != 0) {
540
0
    return -1;
541
0
  }
542
0
  return elf_header.e_type;
543
0
}
544
545
// Read the section headers in the given ELF binary, and if a section
546
// of the specified type is found, set the output to this section header
547
// and return true.  Otherwise, return false.
548
// To keep stack consumption low, we would like this function to not get
549
// inlined.
550
ABSL_ATTRIBUTE_NOINLINE static bool GetSectionHeaderByType(
551
    CachingFile* file, ElfW(Half) sh_num, const off_t sh_offset,
552
0
    ElfW(Word) type, ElfW(Shdr) * out, char* tmp_buf, size_t tmp_buf_size) {
553
0
  ElfW(Shdr) *buf = reinterpret_cast<ElfW(Shdr) *>(tmp_buf);
554
0
  const size_t buf_entries = tmp_buf_size / sizeof(buf[0]);
555
0
  const size_t buf_bytes = buf_entries * sizeof(buf[0]);
556
557
0
  for (size_t i = 0; static_cast<int>(i) < sh_num;) {
558
0
    const size_t num_bytes_left =
559
0
        (static_cast<size_t>(sh_num) - i) * sizeof(buf[0]);
560
0
    const size_t num_bytes_to_read =
561
0
        (buf_bytes > num_bytes_left) ? num_bytes_left : buf_bytes;
562
0
    const off_t offset = sh_offset + static_cast<off_t>(i * sizeof(buf[0]));
563
0
    const ssize_t len = file->ReadFromOffset(buf, num_bytes_to_read, offset);
564
0
    if (len <= 0) {
565
0
      ABSL_RAW_LOG(WARNING, "Reading %zu bytes from offset %ju returned %zd.",
566
0
                   num_bytes_to_read, static_cast<intmax_t>(offset), len);
567
0
      return false;
568
0
    }
569
0
    if (static_cast<size_t>(len) % sizeof(buf[0]) != 0) {
570
0
      ABSL_RAW_LOG(
571
0
          WARNING,
572
0
          "Reading %zu bytes from offset %jd returned %zd which is not a "
573
0
          "multiple of %zu.",
574
0
          num_bytes_to_read, static_cast<intmax_t>(offset), len,
575
0
          sizeof(buf[0]));
576
0
      return false;
577
0
    }
578
0
    const size_t num_headers_in_buf = static_cast<size_t>(len) / sizeof(buf[0]);
579
0
    SAFE_ASSERT(num_headers_in_buf <= buf_entries);
580
0
    for (size_t j = 0; j < num_headers_in_buf; ++j) {
581
0
      if (buf[j].sh_type == type) {
582
0
        *out = buf[j];
583
0
        return true;
584
0
      }
585
0
    }
586
0
    i += num_headers_in_buf;
587
0
  }
588
0
  return false;
589
0
}
590
591
// There is no particular reason to limit section name to 63 characters,
592
// but there has (as yet) been no need for anything longer either.
593
const int kMaxSectionNameLen = 64;
594
595
// Small cache to use for miscellaneous file reads.
596
const int kSmallFileCacheSize = 100;
597
598
bool ForEachSection(int fd,
599
                    const std::function<bool(absl::string_view name,
600
0
                                             const ElfW(Shdr) &)> &callback) {
601
0
  char buf[kSmallFileCacheSize];
602
0
  CachingFile file(fd, buf, sizeof(buf));
603
604
0
  ElfW(Ehdr) elf_header;
605
0
  if (!file.ReadFromOffsetExact(&elf_header, sizeof(elf_header), 0)) {
606
0
    return false;
607
0
  }
608
609
  // Technically it can be larger, but in practice this never happens.
610
0
  if (elf_header.e_shentsize != sizeof(ElfW(Shdr))) {
611
0
    return false;
612
0
  }
613
614
0
  ElfW(Shdr) shstrtab;
615
0
  off_t shstrtab_offset = static_cast<off_t>(elf_header.e_shoff) +
616
0
                          elf_header.e_shentsize * elf_header.e_shstrndx;
617
0
  if (!file.ReadFromOffsetExact(&shstrtab, sizeof(shstrtab), shstrtab_offset)) {
618
0
    return false;
619
0
  }
620
621
0
  for (int i = 0; i < elf_header.e_shnum; ++i) {
622
0
    ElfW(Shdr) out;
623
0
    off_t section_header_offset =
624
0
        static_cast<off_t>(elf_header.e_shoff) + elf_header.e_shentsize * i;
625
0
    if (!file.ReadFromOffsetExact(&out, sizeof(out), section_header_offset)) {
626
0
      return false;
627
0
    }
628
0
    off_t name_offset = static_cast<off_t>(shstrtab.sh_offset) + out.sh_name;
629
0
    char header_name[kMaxSectionNameLen];
630
0
    ssize_t n_read =
631
0
        file.ReadFromOffset(&header_name, kMaxSectionNameLen, name_offset);
632
0
    if (n_read < 0) {
633
0
      return false;
634
0
    } else if (n_read > kMaxSectionNameLen) {
635
      // Long read?
636
0
      return false;
637
0
    }
638
639
0
    absl::string_view name(header_name,
640
0
                           strnlen(header_name, static_cast<size_t>(n_read)));
641
0
    if (!callback(name, out)) {
642
0
      break;
643
0
    }
644
0
  }
645
0
  return true;
646
0
}
647
648
// name_len should include terminating '\0'.
649
bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
650
0
                            ElfW(Shdr) * out) {
651
0
  char header_name[kMaxSectionNameLen];
652
0
  if (sizeof(header_name) < name_len) {
653
0
    ABSL_RAW_LOG(WARNING,
654
0
                 "Section name '%s' is too long (%zu); "
655
0
                 "section will not be found (even if present).",
656
0
                 name, name_len);
657
    // No point in even trying.
658
0
    return false;
659
0
  }
660
661
0
  char buf[kSmallFileCacheSize];
662
0
  CachingFile file(fd, buf, sizeof(buf));
663
0
  ElfW(Ehdr) elf_header;
664
0
  if (!file.ReadFromOffsetExact(&elf_header, sizeof(elf_header), 0)) {
665
0
    return false;
666
0
  }
667
668
  // Technically it can be larger, but in practice this never happens.
669
0
  if (elf_header.e_shentsize != sizeof(ElfW(Shdr))) {
670
0
    return false;
671
0
  }
672
673
0
  ElfW(Shdr) shstrtab;
674
0
  off_t shstrtab_offset = static_cast<off_t>(elf_header.e_shoff) +
675
0
                          elf_header.e_shentsize * elf_header.e_shstrndx;
676
0
  if (!file.ReadFromOffsetExact(&shstrtab, sizeof(shstrtab), shstrtab_offset)) {
677
0
    return false;
678
0
  }
679
680
0
  for (int i = 0; i < elf_header.e_shnum; ++i) {
681
0
    off_t section_header_offset =
682
0
        static_cast<off_t>(elf_header.e_shoff) + elf_header.e_shentsize * i;
683
0
    if (!file.ReadFromOffsetExact(out, sizeof(*out), section_header_offset)) {
684
0
      return false;
685
0
    }
686
0
    off_t name_offset = static_cast<off_t>(shstrtab.sh_offset) + out->sh_name;
687
0
    ssize_t n_read = file.ReadFromOffset(&header_name, name_len, name_offset);
688
0
    if (n_read < 0) {
689
0
      return false;
690
0
    } else if (static_cast<size_t>(n_read) != name_len) {
691
      // Short read -- name could be at end of file.
692
0
      continue;
693
0
    }
694
0
    if (memcmp(header_name, name, name_len) == 0) {
695
0
      return true;
696
0
    }
697
0
  }
698
0
  return false;
699
0
}
700
701
// Compare symbols at in the same address.
702
// Return true if we should pick symbol1.
703
static bool ShouldPickFirstSymbol(const ElfW(Sym) & symbol1,
704
0
                                  const ElfW(Sym) & symbol2) {
705
  // If one of the symbols is weak and the other is not, pick the one
706
  // this is not a weak symbol.
707
0
  char bind1 = ELF_ST_BIND(symbol1.st_info);
708
0
  char bind2 = ELF_ST_BIND(symbol2.st_info);
709
0
  if (bind1 == STB_WEAK && bind2 != STB_WEAK) return false;
710
0
  if (bind2 == STB_WEAK && bind1 != STB_WEAK) return true;
711
712
  // If one of the symbols has zero size and the other is not, pick the
713
  // one that has non-zero size.
714
0
  if (symbol1.st_size != 0 && symbol2.st_size == 0) {
715
0
    return true;
716
0
  }
717
0
  if (symbol1.st_size == 0 && symbol2.st_size != 0) {
718
0
    return false;
719
0
  }
720
721
  // If one of the symbols has no type and the other is not, pick the
722
  // one that has a type.
723
0
  char type1 = ELF_ST_TYPE(symbol1.st_info);
724
0
  char type2 = ELF_ST_TYPE(symbol2.st_info);
725
0
  if (type1 != STT_NOTYPE && type2 == STT_NOTYPE) {
726
0
    return true;
727
0
  }
728
0
  if (type1 == STT_NOTYPE && type2 != STT_NOTYPE) {
729
0
    return false;
730
0
  }
731
732
  // Pick the first one, if we still cannot decide.
733
0
  return true;
734
0
}
735
736
// Return true if an address is inside a section.
737
static bool InSection(const void *address, ptrdiff_t relocation,
738
0
                      const ElfW(Shdr) * section) {
739
0
  const char *start = reinterpret_cast<const char *>(
740
0
      section->sh_addr + static_cast<ElfW(Addr)>(relocation));
741
0
  size_t size = static_cast<size_t>(section->sh_size);
742
0
  return start <= address && address < (start + size);
743
0
}
744
745
0
static const char *ComputeOffset(const char *base, ptrdiff_t offset) {
746
  // Note: cast to intptr_t to avoid undefined behavior when base evaluates to
747
  // zero and offset is non-zero.
748
0
  return reinterpret_cast<const char *>(reinterpret_cast<intptr_t>(base) +
749
0
                                        offset);
750
0
}
751
752
// Read a symbol table and look for the symbol containing the
753
// pc. Iterate over symbols in a symbol table and look for the symbol
754
// containing "pc".  If the symbol is found, and its name fits in
755
// out_size, the name is written into out and SYMBOL_FOUND is returned.
756
// If the name does not fit, truncated name is written into out,
757
// and SYMBOL_TRUNCATED is returned. Out is NUL-terminated.
758
// If the symbol is not found, SYMBOL_NOT_FOUND is returned;
759
// To keep stack consumption low, we would like this function to not get
760
// inlined.
761
ABSL_ATTRIBUTE_NOINLINE static FindSymbolResult FindSymbol(
762
    const void* const pc, CachingFile* file, char* out, size_t out_size,
763
    ptrdiff_t relocation, const ElfW(Shdr) * strtab, const ElfW(Shdr) * symtab,
764
0
    const ElfW(Shdr) * opd, char* tmp_buf, size_t tmp_buf_size) {
765
0
  if (symtab == nullptr) {
766
0
    return SYMBOL_NOT_FOUND;
767
0
  }
768
769
  // In the ELF specification, sh_entsize is a generic section header field that
770
  // holds the entry size in bytes for sections containing fixed-size entries.
771
  // For symbol tables (SHT_SYMTAB and SHT_DYNSYM), the ELF specification
772
  // mandates that each entry is an ElfW(Sym), so sh_entsize should always equal
773
  // sizeof(ElfW(Sym)).
774
  //
775
  // However, because sh_entsize is read directly from the untrusted object
776
  // file, a crafted or corrupt file can specify an unexpected value:
777
  // 1. Setting sh_entsize to 0 would cause a division-by-zero (SIGFPE) in the
778
  //    num_symbols calculation below.
779
  // 2. Setting sh_entsize to a value other than sizeof(ElfW(Sym)) would
780
  //    desynchronize our fixed-size ElfW(Sym) buffer reads from the per-entry
781
  //    stride in the file.
782
  // Reject such malformed tables, mirroring the e_shentsize check we apply to
783
  // section headers.
784
0
  if (symtab->sh_entsize != sizeof(ElfW(Sym))) {
785
0
    return SYMBOL_NOT_FOUND;
786
0
  }
787
788
  // Read multiple symbols at once to save read() calls.
789
0
  ElfW(Sym) *buf = reinterpret_cast<ElfW(Sym) *>(tmp_buf);
790
0
  const size_t buf_entries = tmp_buf_size / sizeof(buf[0]);
791
792
0
  const size_t num_symbols = symtab->sh_size / symtab->sh_entsize;
793
794
  // On platforms using an .opd section (PowerPC & IA64), a function symbol
795
  // has the address of a function descriptor, which contains the real
796
  // starting address.  However, we do not always want to use the real
797
  // starting address because we sometimes want to symbolize a function
798
  // pointer into the .opd section, e.g. FindSymbol(&foo,...).
799
0
  const bool pc_in_opd = kPlatformUsesOPDSections && opd != nullptr &&
800
0
                         InSection(pc, relocation, opd);
801
0
  const bool deref_function_descriptor_pointer =
802
0
      kPlatformUsesOPDSections && opd != nullptr && !pc_in_opd;
803
804
0
  ElfW(Sym) best_match;
805
0
  SafeMemZero(&best_match, sizeof(best_match));
806
0
  bool found_match = false;
807
0
  for (size_t i = 0; i < num_symbols;) {
808
0
    off_t offset =
809
0
        static_cast<off_t>(symtab->sh_offset + i * symtab->sh_entsize);
810
0
    const size_t num_remaining_symbols = num_symbols - i;
811
0
    const size_t entries_in_chunk =
812
0
        std::min(num_remaining_symbols, buf_entries);
813
0
    const size_t bytes_in_chunk = entries_in_chunk * sizeof(buf[0]);
814
0
    const ssize_t len = file->ReadFromOffset(buf, bytes_in_chunk, offset);
815
0
    SAFE_ASSERT(len > 0);
816
0
    SAFE_ASSERT(static_cast<size_t>(len) % sizeof(buf[0]) == 0);
817
0
    const size_t num_symbols_in_buf = static_cast<size_t>(len) / sizeof(buf[0]);
818
0
    SAFE_ASSERT(num_symbols_in_buf <= entries_in_chunk);
819
0
    for (size_t j = 0; j < num_symbols_in_buf; ++j) {
820
0
      const ElfW(Sym) &symbol = buf[j];
821
822
      // For a DSO, a symbol address is relocated by the loading address.
823
      // We keep the original address for opd redirection below.
824
0
      const char *const original_start_address =
825
0
          reinterpret_cast<const char *>(symbol.st_value);
826
0
      const char *start_address =
827
0
          ComputeOffset(original_start_address, relocation);
828
829
#ifdef __arm__
830
      // ARM functions are always aligned to multiples of two bytes; the
831
      // lowest-order bit in start_address is ignored by the CPU and indicates
832
      // whether the function contains ARM (0) or Thumb (1) code. We don't care
833
      // about what encoding is being used; we just want the real start address
834
      // of the function.
835
      start_address = reinterpret_cast<const char *>(
836
          reinterpret_cast<uintptr_t>(start_address) & ~1u);
837
#endif
838
839
0
      if (deref_function_descriptor_pointer &&
840
0
          InSection(original_start_address, /*relocation=*/0, opd)) {
841
        // The opd section is mapped into memory.  Just dereference
842
        // start_address to get the first double word, which points to the
843
        // function entry.
844
0
        start_address = *reinterpret_cast<const char *const *>(start_address);
845
0
      }
846
847
      // If pc is inside the .opd section, it points to a function descriptor.
848
0
      const size_t size = pc_in_opd ? kFunctionDescriptorSize : symbol.st_size;
849
0
      const void *const end_address =
850
0
          ComputeOffset(start_address, static_cast<ptrdiff_t>(size));
851
0
      if (symbol.st_value != 0 &&  // Skip null value symbols.
852
0
          symbol.st_shndx != 0 &&  // Skip undefined symbols.
853
0
#ifdef STT_TLS
854
0
          ELF_ST_TYPE(symbol.st_info) != STT_TLS &&  // Skip thread-local data.
855
0
#endif                                               // STT_TLS
856
0
          ((start_address <= pc && pc < end_address) ||
857
0
           (start_address == pc && pc == end_address))) {
858
0
        if (!found_match || ShouldPickFirstSymbol(symbol, best_match)) {
859
0
          found_match = true;
860
0
          best_match = symbol;
861
0
        }
862
0
      }
863
0
    }
864
0
    i += num_symbols_in_buf;
865
0
  }
866
867
0
  if (found_match) {
868
0
    const off_t off =
869
0
        static_cast<off_t>(strtab->sh_offset) + best_match.st_name;
870
0
    const ssize_t n_read = file->ReadFromOffset(out, out_size, off);
871
0
    if (n_read <= 0) {
872
      // This should never happen.
873
0
      ABSL_RAW_LOG(WARNING,
874
0
                   "Unable to read from fd %d at offset %lld: n_read = %zd",
875
0
                   file->fd(), static_cast<long long>(off), n_read);
876
0
      return SYMBOL_NOT_FOUND;
877
0
    }
878
0
    ABSL_RAW_CHECK(static_cast<size_t>(n_read) <= out_size,
879
0
                   "ReadFromOffset read too much data.");
880
881
    // strtab->sh_offset points into .strtab-like section that contains
882
    // NUL-terminated strings: '\0foo\0barbaz\0...".
883
    //
884
    // sh_offset+st_name points to the start of symbol name, but we don't know
885
    // how long the symbol is, so we try to read as much as we have space for,
886
    // and usually over-read (i.e. there is a NUL somewhere before n_read).
887
0
    if (memchr(out, '\0', static_cast<size_t>(n_read)) == nullptr) {
888
      // Either out_size was too small (n_read == out_size and no NUL), or
889
      // we tried to read past the EOF (n_read < out_size) and .strtab is
890
      // corrupt (missing terminating NUL; should never happen for valid ELF).
891
0
      out[n_read - 1] = '\0';
892
0
      return SYMBOL_TRUNCATED;
893
0
    }
894
0
    return SYMBOL_FOUND;
895
0
  }
896
897
0
  return SYMBOL_NOT_FOUND;
898
0
}
899
900
// Get the symbol name of "pc" from the file pointed by "fd".  Process
901
// both regular and dynamic symbol tables if necessary.
902
// See FindSymbol() comment for description of return value.
903
FindSymbolResult Symbolizer::GetSymbolFromObjectFile(
904
    const ObjFile &obj, const void *const pc, const ptrdiff_t relocation,
905
0
    char *out, size_t out_size, char *tmp_buf, size_t tmp_buf_size) {
906
0
  ElfW(Shdr) symtab;
907
0
  ElfW(Shdr) strtab;
908
0
  ElfW(Shdr) opd;
909
0
  ElfW(Shdr) *opd_ptr = nullptr;
910
911
  // On platforms using an .opd sections for function descriptor, read
912
  // the section header.  The .opd section is in data segment and should be
913
  // loaded but we check that it is mapped just to be extra careful.
914
0
  if (kPlatformUsesOPDSections) {
915
0
    if (GetSectionHeaderByName(obj.fd, kOpdSectionName,
916
0
                               sizeof(kOpdSectionName) - 1, &opd) &&
917
0
        FindObjFile(reinterpret_cast<const char *>(opd.sh_addr) + relocation,
918
0
                    opd.sh_size) != nullptr) {
919
0
      opd_ptr = &opd;
920
0
    } else {
921
0
      return SYMBOL_NOT_FOUND;
922
0
    }
923
0
  }
924
925
0
  CachingFile file(obj.fd, file_cache_, sizeof(file_cache_));
926
927
  // Consult a regular symbol table, then fall back to the dynamic symbol table.
928
0
  for (const auto symbol_table_type : {SHT_SYMTAB, SHT_DYNSYM}) {
929
0
    if (!GetSectionHeaderByType(&file, obj.elf_header.e_shnum,
930
0
                                static_cast<off_t>(obj.elf_header.e_shoff),
931
0
                                static_cast<ElfW(Word)>(symbol_table_type),
932
0
                                &symtab, tmp_buf, tmp_buf_size)) {
933
0
      continue;
934
0
    }
935
0
    if (!file.ReadFromOffsetExact(
936
0
            &strtab, sizeof(strtab),
937
0
            static_cast<off_t>(obj.elf_header.e_shoff +
938
0
                               symtab.sh_link * sizeof(symtab)))) {
939
0
      continue;
940
0
    }
941
0
    const FindSymbolResult rc =
942
0
        FindSymbol(pc, &file, out, out_size, relocation, &strtab, &symtab,
943
0
                   opd_ptr, tmp_buf, tmp_buf_size);
944
0
    if (rc != SYMBOL_NOT_FOUND) {
945
0
      return rc;
946
0
    }
947
0
  }
948
949
0
  return SYMBOL_NOT_FOUND;
950
0
}
951
952
namespace {
953
// Thin wrapper around a file descriptor so that the file descriptor
954
// gets closed for sure.
955
class FileDescriptor {
956
 public:
957
0
  explicit FileDescriptor(int fd) : fd_(fd) {}
958
  FileDescriptor(const FileDescriptor &) = delete;
959
  FileDescriptor &operator=(const FileDescriptor &) = delete;
960
961
0
  ~FileDescriptor() {
962
0
    if (fd_ >= 0) {
963
0
      close(fd_);
964
0
    }
965
0
  }
966
967
0
  int get() const { return fd_; }
968
969
 private:
970
  const int fd_;
971
};
972
973
// Helper class for reading lines from file.
974
//
975
// Note: we don't use ProcMapsIterator since the object is big (it has
976
// a 5k array member) and uses async-unsafe functions such as sscanf()
977
// and snprintf().
978
class LineReader {
979
 public:
980
  explicit LineReader(int fd, char *buf, size_t buf_len)
981
0
      : fd_(fd),
982
0
        buf_len_(buf_len),
983
0
        buf_(buf),
984
0
        bol_(buf),
985
0
        eol_(buf),
986
0
        eod_(buf) {}
987
988
  LineReader(const LineReader &) = delete;
989
  LineReader &operator=(const LineReader &) = delete;
990
991
  // Read '\n'-terminated line from file.  On success, modify "bol"
992
  // and "eol", then return true.  Otherwise, return false.
993
  //
994
  // Note: if the last line doesn't end with '\n', the line will be
995
  // dropped.  It's an intentional behavior to make the code simple.
996
0
  bool ReadLine(const char **bol, const char **eol) {
997
0
    if (BufferIsEmpty()) {  // First time.
998
0
      const ssize_t num_bytes = ReadPersistent(fd_, buf_, buf_len_);
999
0
      if (num_bytes <= 0) {  // EOF or error.
1000
0
        return false;
1001
0
      }
1002
0
      eod_ = buf_ + num_bytes;
1003
0
      bol_ = buf_;
1004
0
    } else {
1005
0
      bol_ = eol_ + 1;            // Advance to the next line in the buffer.
1006
0
      SAFE_ASSERT(bol_ <= eod_);  // "bol_" can point to "eod_".
1007
0
      if (!HasCompleteLine()) {
1008
0
        const auto incomplete_line_length = static_cast<size_t>(eod_ - bol_);
1009
        // Move the trailing incomplete line to the beginning.
1010
0
        memmove(buf_, bol_, incomplete_line_length);
1011
        // Read text from file and append it.
1012
0
        char *const append_pos = buf_ + incomplete_line_length;
1013
0
        const size_t capacity_left = buf_len_ - incomplete_line_length;
1014
0
        const ssize_t num_bytes =
1015
0
            ReadPersistent(fd_, append_pos, capacity_left);
1016
0
        if (num_bytes <= 0) {  // EOF or error.
1017
0
          return false;
1018
0
        }
1019
0
        eod_ = append_pos + num_bytes;
1020
0
        bol_ = buf_;
1021
0
      }
1022
0
    }
1023
0
    eol_ = FindLineFeed();
1024
0
    if (eol_ == nullptr) {  // '\n' not found.  Malformed line.
1025
0
      return false;
1026
0
    }
1027
0
    *eol_ = '\0';  // Replace '\n' with '\0'.
1028
1029
0
    *bol = bol_;
1030
0
    *eol = eol_;
1031
0
    return true;
1032
0
  }
1033
1034
 private:
1035
0
  char *FindLineFeed() const {
1036
0
    return reinterpret_cast<char *>(
1037
0
        memchr(bol_, '\n', static_cast<size_t>(eod_ - bol_)));
1038
0
  }
1039
1040
0
  bool BufferIsEmpty() const { return buf_ == eod_; }
1041
1042
0
  bool HasCompleteLine() const {
1043
0
    return !BufferIsEmpty() && FindLineFeed() != nullptr;
1044
0
  }
1045
1046
  const int fd_;
1047
  const size_t buf_len_;
1048
  char *const buf_;
1049
  char *bol_;
1050
  char *eol_;
1051
  const char *eod_;  // End of data in "buf_".
1052
};
1053
}  // namespace
1054
1055
// Place the hex number read from "start" into "*hex".  The pointer to
1056
// the first non-hex character or "end" is returned.
1057
static const char *GetHex(const char *start, const char *end,
1058
0
                          uint64_t *const value) {
1059
0
  uint64_t hex = 0;
1060
0
  const char *p;
1061
0
  for (p = start; p < end; ++p) {
1062
0
    int ch = *p;
1063
0
    if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') ||
1064
0
        (ch >= 'a' && ch <= 'f')) {
1065
0
      hex = (hex << 4) |
1066
0
            static_cast<uint64_t>(ch < 'A' ? ch - '0' : (ch & 0xF) + 9);
1067
0
    } else {  // Encountered the first non-hex character.
1068
0
      break;
1069
0
    }
1070
0
  }
1071
0
  SAFE_ASSERT(p <= end);
1072
0
  *value = hex;
1073
0
  return p;
1074
0
}
1075
1076
static const char *GetHex(const char *start, const char *end,
1077
0
                          const void **const addr) {
1078
0
  uint64_t hex = 0;
1079
0
  const char *p = GetHex(start, end, &hex);
1080
0
  *addr = reinterpret_cast<void *>(hex);
1081
0
  return p;
1082
0
}
1083
1084
// Normally we are only interested in "r?x" maps.
1085
// On the PowerPC, function pointers point to descriptors in the .opd
1086
// section.  The descriptors themselves are not executable code, so
1087
// we need to relax the check below to "r??".
1088
0
static bool ShouldUseMapping(const char *const flags) {
1089
0
  return flags[0] == 'r' && (kPlatformUsesOPDSections || flags[2] == 'x');
1090
0
}
1091
1092
// Read /proc/self/maps and run "callback" for each mmapped file found.  If
1093
// "callback" returns false, stop scanning and return true. Else continue
1094
// scanning /proc/self/maps. Return true if no parse error is found.
1095
ABSL_ATTRIBUTE_NOINLINE static bool ReadAddrMap(
1096
    bool (*callback)(const char* filename, const void* const start_addr,
1097
                     const void* const end_addr, uint64_t offset, void* arg),
1098
0
    void* arg, void* tmp_buf, size_t tmp_buf_size) {
1099
  // Use /proc/self/task/<pid>/maps instead of /proc/self/maps. The latter
1100
  // requires kernel to stop all threads, and is significantly slower when there
1101
  // are 1000s of threads.
1102
0
  char maps_path[80];
1103
0
  snprintf(maps_path, sizeof(maps_path), "/proc/self/task/%d/maps", getpid());
1104
1105
0
  int maps_fd;
1106
0
  NO_INTR(maps_fd = OpenReadOnlyWithHighFD(maps_path));
1107
0
  FileDescriptor wrapped_maps_fd(maps_fd);
1108
0
  if (wrapped_maps_fd.get() < 0) {
1109
0
    ABSL_RAW_LOG(WARNING, "%s: errno=%d", maps_path, errno);
1110
0
    return false;
1111
0
  }
1112
1113
  // Iterate over maps and look for the map containing the pc.  Then
1114
  // look into the symbol tables inside.
1115
0
  LineReader reader(wrapped_maps_fd.get(), static_cast<char *>(tmp_buf),
1116
0
                    tmp_buf_size);
1117
0
  while (true) {
1118
0
    const char *cursor = nullptr;
1119
0
    const char *eol = nullptr;
1120
0
    if (!reader.ReadLine(&cursor, &eol)) {  // EOF or malformed line.
1121
0
      break;
1122
0
    }
1123
1124
0
    const char *line = cursor;
1125
0
    const void *start_address;
1126
    // Start parsing line in /proc/self/maps.  Here is an example:
1127
    //
1128
    // 08048000-0804c000 r-xp 00000000 08:01 2142121    /bin/cat
1129
    //
1130
    // We want start address (08048000), end address (0804c000), flags
1131
    // (r-xp) and file name (/bin/cat).
1132
1133
    // Read start address.
1134
0
    cursor = GetHex(cursor, eol, &start_address);
1135
0
    if (cursor == eol || *cursor != '-') {
1136
0
      ABSL_RAW_LOG(WARNING, "Corrupt /proc/self/maps line: %s", line);
1137
0
      return false;
1138
0
    }
1139
0
    ++cursor;  // Skip '-'.
1140
1141
    // Read end address.
1142
0
    const void *end_address;
1143
0
    cursor = GetHex(cursor, eol, &end_address);
1144
0
    if (cursor == eol || *cursor != ' ') {
1145
0
      ABSL_RAW_LOG(WARNING, "Corrupt /proc/self/maps line: %s", line);
1146
0
      return false;
1147
0
    }
1148
0
    ++cursor;  // Skip ' '.
1149
1150
    // Read flags.  Skip flags until we encounter a space or eol.
1151
0
    const char *const flags_start = cursor;
1152
0
    while (cursor < eol && *cursor != ' ') {
1153
0
      ++cursor;
1154
0
    }
1155
    // We expect at least four letters for flags (ex. "r-xp").
1156
0
    if (cursor == eol || cursor < flags_start + 4) {
1157
0
      ABSL_RAW_LOG(WARNING, "Corrupt /proc/self/maps: %s", line);
1158
0
      return false;
1159
0
    }
1160
1161
    // Check flags.
1162
0
    if (!ShouldUseMapping(flags_start)) {
1163
0
      continue;  // We skip this map.
1164
0
    }
1165
0
    ++cursor;  // Skip ' '.
1166
1167
    // Read file offset.
1168
0
    uint64_t offset;
1169
0
    cursor = GetHex(cursor, eol, &offset);
1170
0
    ++cursor;  // Skip ' '.
1171
1172
    // Skip to file name.  "cursor" now points to dev.  We need to skip at least
1173
    // two spaces for dev and inode.
1174
0
    int num_spaces = 0;
1175
0
    while (cursor < eol) {
1176
0
      if (*cursor == ' ') {
1177
0
        ++num_spaces;
1178
0
      } else if (num_spaces >= 2) {
1179
        // The first non-space character after  skipping two spaces
1180
        // is the beginning of the file name.
1181
0
        break;
1182
0
      }
1183
0
      ++cursor;
1184
0
    }
1185
1186
    // Check whether this entry corresponds to our hint table for the true
1187
    // filename.
1188
0
    bool hinted =
1189
0
        GetFileMappingHint(&start_address, &end_address, &offset, &cursor);
1190
0
    if (!hinted && (cursor == eol || cursor[0] == '[')) {
1191
      // not an object file, typically [vdso] or [vsyscall]
1192
0
      continue;
1193
0
    }
1194
0
    if (!callback(cursor, start_address, end_address, offset, arg)) break;
1195
0
  }
1196
0
  return true;
1197
0
}
1198
1199
// Find the objfile mapped in address region containing [addr, addr + len).
1200
0
ObjFile *Symbolizer::FindObjFile(const void *const addr, size_t len) {
1201
0
  for (int i = 0; i < 2; ++i) {
1202
0
    if (!ok_) return nullptr;
1203
1204
    // Read /proc/self/maps if necessary
1205
0
    if (!addr_map_read_) {
1206
0
      addr_map_read_ = true;
1207
0
      if (!ReadAddrMap(RegisterObjFile, this, tmp_buf_, TMP_BUF_SIZE)) {
1208
0
        ok_ = false;
1209
0
        return nullptr;
1210
0
      }
1211
0
    }
1212
1213
0
    size_t lo = 0;
1214
0
    size_t hi = addr_map_.Size();
1215
0
    while (lo < hi) {
1216
0
      size_t mid = (lo + hi) / 2;
1217
0
      if (addr < addr_map_.At(mid)->end_addr) {
1218
0
        hi = mid;
1219
0
      } else {
1220
0
        lo = mid + 1;
1221
0
      }
1222
0
    }
1223
0
    if (lo != addr_map_.Size()) {
1224
0
      ObjFile *obj = addr_map_.At(lo);
1225
0
      SAFE_ASSERT(obj->end_addr > addr);
1226
0
      if (addr >= obj->start_addr &&
1227
0
          len <= static_cast<size_t>(static_cast<const char*>(obj->end_addr) -
1228
0
                                     static_cast<const char*>(addr)))
1229
0
        return obj;
1230
0
    }
1231
1232
    // The address mapping may have changed since it was last read.  Retry.
1233
0
    ClearAddrMap();
1234
0
  }
1235
0
  return nullptr;
1236
0
}
1237
1238
0
void Symbolizer::ClearAddrMap() {
1239
0
  for (size_t i = 0; i != addr_map_.Size(); i++) {
1240
0
    ObjFile *o = addr_map_.At(i);
1241
0
    base_internal::LowLevelAlloc::Free(o->filename);
1242
0
    if (o->fd >= 0) {
1243
0
      close(o->fd);
1244
0
    }
1245
0
  }
1246
0
  addr_map_.Clear();
1247
0
  addr_map_read_ = false;
1248
0
}
1249
1250
// Callback for ReadAddrMap to register objfiles in an in-memory table.
1251
bool Symbolizer::RegisterObjFile(const char *filename,
1252
                                 const void *const start_addr,
1253
                                 const void *const end_addr, uint64_t offset,
1254
0
                                 void *arg) {
1255
0
  Symbolizer *impl = static_cast<Symbolizer *>(arg);
1256
1257
  // Files are supposed to be added in the increasing address order.  Make
1258
  // sure that's the case.
1259
0
  size_t addr_map_size = impl->addr_map_.Size();
1260
0
  if (addr_map_size != 0) {
1261
0
    ObjFile *old = impl->addr_map_.At(addr_map_size - 1);
1262
0
    if (old->end_addr > end_addr) {
1263
0
      ABSL_RAW_LOG(ERROR,
1264
0
                   "Unsorted addr map entry: 0x%" PRIxPTR ": %s <-> 0x%" PRIxPTR
1265
0
                   ": %s",
1266
0
                   reinterpret_cast<uintptr_t>(end_addr), filename,
1267
0
                   reinterpret_cast<uintptr_t>(old->end_addr), old->filename);
1268
0
      return true;
1269
0
    } else if (old->end_addr == end_addr) {
1270
      // The same entry appears twice. This sometimes happens for [vdso].
1271
0
      if (old->start_addr != start_addr ||
1272
0
          strcmp(old->filename, filename) != 0) {
1273
0
        ABSL_RAW_LOG(ERROR,
1274
0
                     "Duplicate addr 0x%" PRIxPTR ": %s <-> 0x%" PRIxPTR ": %s",
1275
0
                     reinterpret_cast<uintptr_t>(end_addr), filename,
1276
0
                     reinterpret_cast<uintptr_t>(old->end_addr), old->filename);
1277
0
      }
1278
0
      return true;
1279
0
    } else if (old->end_addr == start_addr &&
1280
0
               reinterpret_cast<uintptr_t>(old->start_addr) - old->offset ==
1281
0
                   reinterpret_cast<uintptr_t>(start_addr) - offset &&
1282
0
               strcmp(old->filename, filename) == 0) {
1283
      // Two contiguous map entries that span a contiguous region of the file,
1284
      // perhaps because some part of the file was mlock()ed. Combine them.
1285
0
      old->end_addr = end_addr;
1286
0
      return true;
1287
0
    }
1288
0
  }
1289
0
  ObjFile *obj = impl->addr_map_.Add();
1290
0
  obj->filename = impl->CopyString(filename);
1291
0
  obj->start_addr = start_addr;
1292
0
  obj->end_addr = end_addr;
1293
0
  obj->offset = offset;
1294
0
  obj->elf_type = -1;  // filled on demand
1295
0
  obj->fd = -1;        // opened on demand
1296
0
  return true;
1297
0
}
1298
1299
// This function wraps the Demangle function to provide an interface
1300
// where the input symbol is demangled in-place.
1301
// To keep stack consumption low, we would like this function to not
1302
// get inlined.
1303
ABSL_ATTRIBUTE_NOINLINE static void DemangleInplace(char* out, size_t out_size,
1304
                                                    char* tmp_buf,
1305
0
                                                    size_t tmp_buf_size) {
1306
0
  if (Demangle(out, tmp_buf, tmp_buf_size)) {
1307
    // Demangling succeeded. Copy to out if the space allows.
1308
0
    size_t len = strlen(tmp_buf);
1309
0
    if (len + 1 <= out_size) {  // +1 for '\0'.
1310
0
      SAFE_ASSERT(len < tmp_buf_size);
1311
0
      memmove(out, tmp_buf, len + 1);
1312
0
    }
1313
0
  }
1314
0
}
1315
1316
0
SymbolCacheLine *Symbolizer::GetCacheLine(const void *const pc) {
1317
0
  uintptr_t pc0 = reinterpret_cast<uintptr_t>(pc);
1318
0
  pc0 >>= 3;  // drop the low 3 bits
1319
1320
  // Shuffle bits.
1321
0
  pc0 ^= (pc0 >> 6) ^ (pc0 >> 12) ^ (pc0 >> 18);
1322
0
  return &symbol_cache_[pc0 % SYMBOL_CACHE_LINES];
1323
0
}
1324
1325
0
void Symbolizer::AgeSymbols(SymbolCacheLine *line) {
1326
0
  for (uint32_t &age : line->age) {
1327
0
    ++age;
1328
0
  }
1329
0
}
1330
1331
0
const char *Symbolizer::FindSymbolInCache(const void *const pc) {
1332
0
  if (pc == nullptr) return nullptr;
1333
1334
0
  SymbolCacheLine *line = GetCacheLine(pc);
1335
0
  for (size_t i = 0; i < std::size(line->pc); ++i) {
1336
0
    if (line->pc[i] == pc) {
1337
0
      AgeSymbols(line);
1338
0
      line->age[i] = 0;
1339
0
      return line->name[i];
1340
0
    }
1341
0
  }
1342
0
  return nullptr;
1343
0
}
1344
1345
const char *Symbolizer::InsertSymbolInCache(const void *const pc,
1346
0
                                            const char *name) {
1347
0
  SAFE_ASSERT(pc != nullptr);
1348
1349
0
  SymbolCacheLine *line = GetCacheLine(pc);
1350
0
  uint32_t max_age = 0;
1351
0
  size_t oldest_index = 0;
1352
0
  bool found_oldest_index = false;
1353
0
  for (size_t i = 0; i < std::size(line->pc); ++i) {
1354
0
    if (line->pc[i] == nullptr) {
1355
0
      AgeSymbols(line);
1356
0
      line->pc[i] = pc;
1357
0
      line->name[i] = CopyString(name);
1358
0
      line->age[i] = 0;
1359
0
      return line->name[i];
1360
0
    }
1361
0
    if (line->age[i] >= max_age) {
1362
0
      max_age = line->age[i];
1363
0
      oldest_index = i;
1364
0
      found_oldest_index = true;
1365
0
    }
1366
0
  }
1367
1368
0
  AgeSymbols(line);
1369
0
  ABSL_RAW_CHECK(found_oldest_index, "Corrupt cache");
1370
0
  base_internal::LowLevelAlloc::Free(line->name[oldest_index]);
1371
0
  line->pc[oldest_index] = pc;
1372
0
  line->name[oldest_index] = CopyString(name);
1373
0
  line->age[oldest_index] = 0;
1374
0
  return line->name[oldest_index];
1375
0
}
1376
1377
0
static void MaybeOpenFdFromSelfExe(ObjFile *obj) {
1378
0
  if (memcmp(obj->start_addr, ELFMAG, SELFMAG) != 0) {
1379
0
    return;
1380
0
  }
1381
0
  int fd = OpenReadOnlyWithHighFD("/proc/self/exe");
1382
0
  if (fd == -1) {
1383
0
    return;
1384
0
  }
1385
  // Verify that contents of /proc/self/exe matches in-memory image of
1386
  // the binary. This can fail if the "deleted" binary is in fact not
1387
  // the main executable, or for binaries that have the first PT_LOAD
1388
  // segment smaller than 4K. We do it in four steps so that the
1389
  // buffer is smaller and we don't consume too much stack space.
1390
0
  const char *mem = reinterpret_cast<const char *>(obj->start_addr);
1391
0
  for (int i = 0; i < 4; ++i) {
1392
0
    char buf[1024];
1393
0
    ssize_t n = read(fd, buf, sizeof(buf));
1394
0
    if (n != sizeof(buf) || memcmp(buf, mem, sizeof(buf)) != 0) {
1395
0
      close(fd);
1396
0
      return;
1397
0
    }
1398
0
    mem += sizeof(buf);
1399
0
  }
1400
0
  obj->fd = fd;
1401
0
}
1402
1403
static bool MaybeInitializeObjFile(
1404
0
    ObjFile* obj, SymbolDecorator::Factory* decorator_factory) {
1405
0
  if (obj->fd < 0) {
1406
0
    obj->fd = OpenReadOnlyWithHighFD(obj->filename);
1407
1408
0
    if (obj->fd < 0) {
1409
      // Getting /proc/self/exe here means that we were hinted.
1410
0
      if (strcmp(obj->filename, "/proc/self/exe") == 0) {
1411
        // /proc/self/exe may be inaccessible (due to setuid, etc.), so try
1412
        // accessing the binary via argv0.
1413
0
        if (argv0_value != nullptr) {
1414
0
          obj->fd = OpenReadOnlyWithHighFD(argv0_value);
1415
0
        }
1416
0
      } else {
1417
0
        MaybeOpenFdFromSelfExe(obj);
1418
0
      }
1419
0
    }
1420
1421
0
    if (obj->fd < 0) {
1422
0
      ABSL_RAW_LOG(WARNING, "%s: open failed: errno=%d", obj->filename, errno);
1423
0
      return false;
1424
0
    }
1425
1426
0
    char buf[kSmallFileCacheSize];
1427
0
    CachingFile file(obj->fd, buf, sizeof(buf));
1428
1429
0
    obj->elf_type = FileGetElfType(&file);
1430
0
    if (obj->elf_type < 0) {
1431
0
      ABSL_RAW_LOG(WARNING, "%s: wrong elf type: %d", obj->filename,
1432
0
                   obj->elf_type);
1433
0
      return false;
1434
0
    }
1435
1436
0
    if (!file.ReadFromOffsetExact(&obj->elf_header, sizeof(obj->elf_header),
1437
0
                                  0)) {
1438
0
      ABSL_RAW_LOG(WARNING, "%s: failed to read elf header", obj->filename);
1439
0
      return false;
1440
0
    }
1441
0
    const int phnum = obj->elf_header.e_phnum;
1442
0
    const int phentsize = obj->elf_header.e_phentsize;
1443
0
    auto phoff = static_cast<off_t>(obj->elf_header.e_phoff);
1444
0
    size_t num_interesting_load_segments = 0;
1445
0
    for (int j = 0; j < phnum; j++) {
1446
0
      ElfW(Phdr) phdr;
1447
0
      if (!file.ReadFromOffsetExact(&phdr, sizeof(phdr), phoff)) {
1448
0
        ABSL_RAW_LOG(WARNING, "%s: failed to read program header %d",
1449
0
                     obj->filename, j);
1450
0
        return false;
1451
0
      }
1452
0
      phoff += phentsize;
1453
1454
#ifdef ABSL_INTERNAL_HAVE_PPC64_ELFV1_ABI
1455
      // On the PowerPC 64-bit ELFv1 ABI, function pointers actually point to
1456
      // function descriptors. These descriptors are stored in an .opd section,
1457
      // which is mapped read-only. We thus need to look at all readable
1458
      // segments, not just the executable ones.
1459
      constexpr int interesting = PF_R;
1460
#else
1461
0
      constexpr int interesting = PF_X | PF_R;
1462
0
#endif
1463
1464
0
      if (phdr.p_type != PT_LOAD ||
1465
0
          (phdr.p_flags & interesting) != interesting) {
1466
        // Not a LOAD segment, not executable code, and not a function
1467
        // descriptor.
1468
0
        continue;
1469
0
      }
1470
0
      if (num_interesting_load_segments < obj->phdr.size()) {
1471
0
        memcpy(&obj->phdr[num_interesting_load_segments++], &phdr,
1472
0
               sizeof(phdr));
1473
0
      } else {
1474
0
        ABSL_RAW_LOG(
1475
0
            WARNING, "%s: too many interesting LOAD segments: %zu >= %zu",
1476
0
            obj->filename, num_interesting_load_segments, obj->phdr.size());
1477
0
        break;
1478
0
      }
1479
0
    }
1480
0
    if (num_interesting_load_segments == 0) {
1481
      // This object has no interesting LOAD segments. That's unexpected.
1482
0
      ABSL_RAW_LOG(WARNING, "%s: no interesting LOAD segments", obj->filename);
1483
0
      return false;
1484
0
    }
1485
0
  }
1486
0
  if (decorator_factory != nullptr && obj->decorator == nullptr) {
1487
0
    obj->decorator = decorator_factory(obj->fd);
1488
0
  }
1489
0
  return true;
1490
0
}
1491
1492
// The implementation of our symbolization routine.  If it
1493
// successfully finds the symbol containing "pc" and obtains the
1494
// symbol name, returns pointer to that symbol. Otherwise, returns nullptr.
1495
// If any symbol decorators have been installed via InstallSymbolDecorator(),
1496
// they are called here as well.
1497
// To keep stack consumption low, we would like this function to not
1498
// get inlined.
1499
0
const char *Symbolizer::GetUncachedSymbol(const void *pc) {
1500
0
  ObjFile *const obj = FindObjFile(pc, 1);
1501
0
  ptrdiff_t relocation = 0;
1502
0
  if (obj != nullptr) {
1503
0
    if (MaybeInitializeObjFile(obj, decorator_factory_)) {
1504
0
      const size_t start_addr = reinterpret_cast<size_t>(obj->start_addr);
1505
0
      if (obj->elf_type == ET_DYN && start_addr >= obj->offset) {
1506
        // This object was relocated.
1507
        //
1508
        // For obj->offset > 0, adjust the relocation since a mapping at offset
1509
        // X in the file will have a start address of [true relocation]+X.
1510
0
        relocation = static_cast<ptrdiff_t>(start_addr - obj->offset);
1511
1512
        // Note: some binaries have multiple LOAD segments that can contain
1513
        // function pointers. We must find the right one.
1514
0
        ElfW(Phdr) *phdr = nullptr;
1515
0
        for (size_t j = 0; j < obj->phdr.size(); j++) {
1516
0
          ElfW(Phdr) &p = obj->phdr[j];
1517
0
          if (p.p_type != PT_LOAD) {
1518
            // We only expect PT_LOADs. This must be PT_NULL that we didn't
1519
            // write over (i.e. we exhausted all interesting PT_LOADs).
1520
0
            ABSL_RAW_CHECK(p.p_type == PT_NULL, "unexpected p_type");
1521
0
            break;
1522
0
          }
1523
0
          if (pc <
1524
0
              reinterpret_cast<void *>(start_addr + p.p_vaddr + p.p_memsz)) {
1525
0
            phdr = &p;
1526
0
            break;
1527
0
          }
1528
0
        }
1529
0
        if (phdr == nullptr) {
1530
          // That's unexpected. Hope for the best.
1531
0
          ABSL_RAW_LOG(
1532
0
              WARNING,
1533
0
              "%s: unable to find LOAD segment for pc: %p, start_addr: %zx",
1534
0
              obj->filename, pc, start_addr);
1535
0
        } else {
1536
          // Adjust relocation in case phdr.p_vaddr != 0.
1537
          // This happens for binaries linked with `lld --rosegment`, and for
1538
          // binaries linked with BFD `ld -z separate-code`.
1539
0
          relocation -= phdr->p_vaddr - phdr->p_offset;
1540
0
        }
1541
0
      }
1542
1543
0
      if (GetSymbolFromObjectFile(*obj, pc, relocation, symbol_buf_,
1544
0
                                  sizeof(symbol_buf_), tmp_buf_,
1545
0
                                  sizeof(tmp_buf_)) == SYMBOL_FOUND) {
1546
        // Only try to demangle the symbol name if it fit into symbol_buf_.
1547
0
        DemangleInplace(symbol_buf_, sizeof(symbol_buf_), tmp_buf_,
1548
0
                        sizeof(tmp_buf_));
1549
0
      }
1550
0
    }
1551
0
  } else {
1552
0
#if ABSL_HAVE_VDSO_SUPPORT
1553
0
    VDSOSupport vdso;
1554
0
    if (vdso.IsPresent()) {
1555
0
      VDSOSupport::SymbolInfo symbol_info;
1556
0
      if (vdso.LookupSymbolByAddress(pc, &symbol_info)) {
1557
        // All VDSO symbols are known to be short.
1558
0
        size_t len = strlen(symbol_info.name);
1559
0
        ABSL_RAW_CHECK(len + 1 < sizeof(symbol_buf_),
1560
0
                       "VDSO symbol unexpectedly long");
1561
0
        memcpy(symbol_buf_, symbol_info.name, len + 1);
1562
0
      }
1563
0
    }
1564
0
#endif
1565
0
  }
1566
1567
0
  if (obj != nullptr && obj->decorator != nullptr) {
1568
0
    obj->decorator->Decorate(pc, relocation, symbol_buf_, sizeof(symbol_buf_),
1569
0
                             tmp_buf_, sizeof(tmp_buf_));
1570
0
  }
1571
0
  if (symbol_buf_[0] == '\0') {
1572
0
    return nullptr;
1573
0
  }
1574
0
  symbol_buf_[sizeof(symbol_buf_) - 1] = '\0';  // Paranoia.
1575
0
  return InsertSymbolInCache(pc, symbol_buf_);
1576
0
}
1577
1578
0
const char *Symbolizer::GetSymbol(const void *pc) {
1579
0
  const char *entry = FindSymbolInCache(pc);
1580
0
  if (entry != nullptr) {
1581
0
    return entry;
1582
0
  }
1583
0
  symbol_buf_[0] = '\0';
1584
1585
#ifdef __hppa__
1586
  {
1587
    // In some contexts (e.g., return addresses), PA-RISC uses the lowest two
1588
    // bits of the address to indicate the privilege level. Clear those bits
1589
    // before trying to symbolize.
1590
    const auto pc_bits = reinterpret_cast<uintptr_t>(pc);
1591
    const auto address = pc_bits & ~0x3;
1592
    entry = GetUncachedSymbol(reinterpret_cast<const void *>(address));
1593
    if (entry != nullptr) {
1594
      return entry;
1595
    }
1596
1597
    // In some contexts, PA-RISC also uses bit 1 of the address to indicate that
1598
    // this is a cross-DSO function pointer. Such function pointers actually
1599
    // point to a procedure label, a struct whose first 32-bit (pointer) element
1600
    // actually points to the function text. With no symbol found for this
1601
    // address so far, try interpreting it as a cross-DSO function pointer and
1602
    // see how that goes.
1603
    if (pc_bits & 0x2) {
1604
      return GetUncachedSymbol(*reinterpret_cast<const void *const *>(address));
1605
    }
1606
1607
    return nullptr;
1608
  }
1609
#else
1610
0
  return GetUncachedSymbol(pc);
1611
0
#endif
1612
0
}
1613
1614
SymbolDecorator::Factory* SetSymbolDecoratorFactory(
1615
0
    SymbolDecorator::Factory* factory) {
1616
0
  return g_decorator_factory.exchange(factory, std::memory_order_acq_rel);
1617
0
}
1618
1619
bool RegisterFileMappingHint(const void *start, const void *end,
1620
0
                             uint64_t offset, const char *filename) {
1621
0
  SAFE_ASSERT(start <= end);
1622
0
  SAFE_ASSERT(filename != nullptr);
1623
1624
0
  base_internal::InitSigSafeArena();
1625
1626
0
  if (!g_file_mapping_mu.try_lock()) {
1627
0
    return false;
1628
0
  }
1629
1630
0
  bool ret = true;
1631
0
  if (g_num_file_mapping_hints >= kMaxFileMappingHints) {
1632
0
    ret = false;
1633
0
  } else {
1634
    // TODO(ckennelly): Move this into a string copy routine.
1635
0
    size_t len = strlen(filename);
1636
0
    char *dst =
1637
0
        static_cast<char *>(base_internal::LowLevelAlloc::AllocWithArena(
1638
0
            len + 1, base_internal::SigSafeArena()));
1639
0
    ABSL_RAW_CHECK(dst != nullptr, "out of memory");
1640
0
    memcpy(dst, filename, len + 1);
1641
1642
0
    auto &hint = g_file_mapping_hints[g_num_file_mapping_hints++];
1643
0
    hint.start = start;
1644
0
    hint.end = end;
1645
0
    hint.offset = offset;
1646
0
    hint.filename = dst;
1647
0
  }
1648
1649
0
  g_file_mapping_mu.unlock();
1650
0
  return ret;
1651
0
}
1652
1653
bool GetFileMappingHint(const void **start, const void **end, uint64_t *offset,
1654
0
                        const char **filename) {
1655
0
  if (!g_file_mapping_mu.try_lock()) {
1656
0
    return false;
1657
0
  }
1658
0
  bool found = false;
1659
0
  for (int i = 0; i < g_num_file_mapping_hints; i++) {
1660
0
    if (g_file_mapping_hints[i].start <= *start &&
1661
0
        *end <= g_file_mapping_hints[i].end) {
1662
      // We assume that the start_address for the mapping is the base
1663
      // address of the ELF section, but when [start_address,end_address) is
1664
      // not strictly equal to [hint.start, hint.end), that assumption is
1665
      // invalid.
1666
      //
1667
      // This uses the hint's start address (even though hint.start is not
1668
      // necessarily equal to start_address) to ensure the correct
1669
      // relocation is computed later.
1670
0
      *start = g_file_mapping_hints[i].start;
1671
0
      *end = g_file_mapping_hints[i].end;
1672
0
      *offset = g_file_mapping_hints[i].offset;
1673
0
      *filename = g_file_mapping_hints[i].filename;
1674
0
      found = true;
1675
0
      break;
1676
0
    }
1677
0
  }
1678
0
  g_file_mapping_mu.unlock();
1679
0
  return found;
1680
0
}
1681
1682
}  // namespace debugging_internal
1683
1684
0
bool Symbolize(const void *pc, char *out, int out_size) {
1685
  // Symbolization is very slow under tsan.
1686
0
  ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN();
1687
0
  SAFE_ASSERT(out_size >= 0);
1688
0
  debugging_internal::Symbolizer *s = debugging_internal::AllocateSymbolizer();
1689
0
  const char *name = s->GetSymbol(pc);
1690
0
  bool ok = false;
1691
0
  if (name != nullptr && out_size > 0) {
1692
0
    strncpy(out, name, static_cast<size_t>(out_size));
1693
0
    ok = true;
1694
0
    if (out[static_cast<size_t>(out_size) - 1] != '\0') {
1695
      // strncpy() does not '\0' terminate when it truncates.  Do so, with
1696
      // trailing ellipsis.
1697
0
      static constexpr char kEllipsis[] = "...";
1698
0
      size_t ellipsis_size =
1699
0
          std::min(strlen(kEllipsis), static_cast<size_t>(out_size) - 1);
1700
0
      memcpy(out + static_cast<size_t>(out_size) - ellipsis_size - 1, kEllipsis,
1701
0
             ellipsis_size);
1702
0
      out[static_cast<size_t>(out_size) - 1] = '\0';
1703
0
    }
1704
0
  }
1705
0
  debugging_internal::FreeSymbolizer(s);
1706
0
  ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_END();
1707
0
  return ok;
1708
0
}
1709
1710
ABSL_NAMESPACE_END
1711
}  // namespace absl
1712
1713
extern "C" bool ABSL_INTERNAL_C_SYMBOL(AbslInternalGetFileMappingHint)(
1714
    const void** start, const void** end, uint64_t* offset,
1715
0
    const char** filename) {
1716
0
  return absl::debugging_internal::GetFileMappingHint(start, end, offset,
1717
0
                                                      filename);
1718
0
}
1719
1720
#undef ABSL_INTERNAL_HAVE_PPC64_ELFV1_ABI