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