Coverage Report

Created: 2024-09-23 06:29

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