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