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