Fixes for GCC 4.4. Thanks John for this patch!
[platform/upstream/glog.git] / src / symbolize.cc
1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Satoru Takabayashi
31 // Stack-footprint reduction work done by Raksit Ashok
32 //
33 // Implementation note:
34 //
35 // We don't use heaps but only use stacks.  We want to reduce the
36 // stack consumption so that the symbolizer can run on small stacks.
37 //
38 // Here are some numbers collected with GCC 4.1.0 on x86:
39 // - sizeof(Elf32_Sym)  = 16
40 // - sizeof(Elf32_Shdr) = 40
41 // - sizeof(Elf64_Sym)  = 24
42 // - sizeof(Elf64_Shdr) = 64
43 //
44 // This implementation is intended to be async-signal-safe but uses
45 // some functions which are not guaranteed to be so, such as memchr()
46 // and memmove().  We assume they are async-signal-safe.
47 //
48
49 #include "utilities.h"
50
51 #if defined(HAVE_SYMBOLIZE)
52
53 #include "symbolize.h"
54 #include "demangle.h"
55
56 _START_GOOGLE_NAMESPACE_
57
58 // We don't use assert() since it's not guaranteed to be
59 // async-signal-safe.  Instead we define a minimal assertion
60 // macro. So far, we don't need pretty printing for __FILE__, etc.
61
62 // A wrapper for abort() to make it callable in ? :.
63 static int AssertFail() {
64   abort();
65   return 0;  // Should not reach.
66 }
67
68 #define SAFE_ASSERT(expr) ((expr) ? 0 : AssertFail())
69
70 static SymbolizeCallback g_symbolize_callback = NULL;
71 void InstallSymbolizeCallback(SymbolizeCallback callback) {
72   g_symbolize_callback = callback;
73 }
74
75 // This function wraps the Demangle function to provide an interface
76 // where the input symbol is demangled in-place.
77 // To keep stack consumption low, we would like this function to not
78 // get inlined.
79 static ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size) {
80   char demangled[256];  // Big enough for sane demangled symbols.
81   if (Demangle(out, demangled, sizeof(demangled))) {
82     // Demangling succeeded. Copy to out if the space allows.
83     int len = strlen(demangled);
84     if (len + 1 <= out_size) {  // +1 for '\0'.
85       SAFE_ASSERT(len < sizeof(demangled));
86       memmove(out, demangled, len + 1);
87     }
88   }
89 }
90
91 _END_GOOGLE_NAMESPACE_
92
93 #if defined(__ELF__)
94
95 #include <dlfcn.h>
96 #include <elf.h>
97 #include <errno.h>
98 #include <fcntl.h>
99 #include <limits.h>
100 #include <link.h>  // For ElfW() macro.
101 #include <stdint.h>
102 #include <stdio.h>
103 #include <stdlib.h>
104 #include <stddef.h>
105 #include <string.h>
106 #include <sys/stat.h>
107 #include <sys/types.h>
108 #include <unistd.h>
109
110 #include "symbolize.h"
111 #include "config.h"
112 #include "glog/raw_logging.h"
113
114 // Re-runs fn until it doesn't cause EINTR.
115 #define NO_INTR(fn)   do {} while ((fn) < 0 && errno == EINTR)
116
117 _START_GOOGLE_NAMESPACE_
118
119 // Read up to "count" bytes from file descriptor "fd" into the buffer
120 // starting at "buf" while handling short reads and EINTR.  On
121 // success, return the number of bytes read.  Otherwise, return -1.
122 static ssize_t ReadPersistent(const int fd, void *buf, const size_t count) {
123   SAFE_ASSERT(fd >= 0);
124   SAFE_ASSERT(count >= 0 && count <= SSIZE_MAX);
125   char *buf0 = reinterpret_cast<char *>(buf);
126   ssize_t num_bytes = 0;
127   while (num_bytes < count) {
128     ssize_t len;
129     NO_INTR(len = read(fd, buf0 + num_bytes, count - num_bytes));
130     if (len < 0) {  // There was an error other than EINTR.
131       return -1;
132     }
133     if (len == 0) {  // Reached EOF.
134       break;
135     }
136     num_bytes += len;
137   }
138   SAFE_ASSERT(num_bytes <= count);
139   return num_bytes;
140 }
141
142 // Read up to "count" bytes from "offset" in the file pointed by file
143 // descriptor "fd" into the buffer starting at "buf".  On success,
144 // return the number of bytes read.  Otherwise, return -1.
145 static ssize_t ReadFromOffset(const int fd, void *buf,
146                               const size_t count, const off_t offset) {
147   off_t off = lseek(fd, offset, SEEK_SET);
148   if (off == (off_t)-1) {
149     return -1;
150   }
151   return ReadPersistent(fd, buf, count);
152 }
153
154 // Try reading exactly "count" bytes from "offset" bytes in a file
155 // pointed by "fd" into the buffer starting at "buf" while handling
156 // short reads and EINTR.  On success, return true. Otherwise, return
157 // false.
158 static bool ReadFromOffsetExact(const int fd, void *buf,
159                                 const size_t count, const off_t offset) {
160   ssize_t len = ReadFromOffset(fd, buf, count, offset);
161   return len == count;
162 }
163
164 // Returns elf_header.e_type if the file pointed by fd is an ELF binary.
165 static int FileGetElfType(const int fd) {
166   ElfW(Ehdr) elf_header;
167   if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
168     return -1;
169   }
170   if (memcmp(elf_header.e_ident, ELFMAG, SELFMAG) != 0) {
171     return -1;
172   }
173   return elf_header.e_type;
174 }
175
176 // Read the section headers in the given ELF binary, and if a section
177 // of the specified type is found, set the output to this section header
178 // and return true.  Otherwise, return false.
179 // To keep stack consumption low, we would like this function to not get
180 // inlined.
181 static ATTRIBUTE_NOINLINE bool
182 GetSectionHeaderByType(const int fd, ElfW(Half) sh_num, const off_t sh_offset,
183                        ElfW(Word) type, ElfW(Shdr) *out) {
184   // Read at most 16 section headers at a time to save read calls.
185   ElfW(Shdr) buf[16];
186   for (int i = 0; i < sh_num;) {
187     const ssize_t num_bytes_left = (sh_num - i) * sizeof(buf[0]);
188     const ssize_t num_bytes_to_read =
189         (sizeof(buf) > num_bytes_left) ? num_bytes_left : sizeof(buf);
190     const ssize_t len = ReadFromOffset(fd, buf, num_bytes_to_read,
191                                        sh_offset + i * sizeof(buf[0]));
192     SAFE_ASSERT(len % sizeof(buf[0]) == 0);
193     const ssize_t num_headers_in_buf = len / sizeof(buf[0]);
194     SAFE_ASSERT(num_headers_in_buf <= sizeof(buf) / sizeof(buf[0]));
195     for (int j = 0; j < num_headers_in_buf; ++j) {
196       if (buf[j].sh_type == type) {
197         *out = buf[j];
198         return true;
199       }
200     }
201     i += num_headers_in_buf;
202   }
203   return false;
204 }
205
206 // There is no particular reason to limit section name to 63 characters,
207 // but there has (as yet) been no need for anything longer either.
208 const int kMaxSectionNameLen = 64;
209
210 // name_len should include terminating '\0'.
211 bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
212                             ElfW(Shdr) *out) {
213   ElfW(Ehdr) elf_header;
214   if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
215     return false;
216   }
217
218   ElfW(Shdr) shstrtab;
219   off_t shstrtab_offset = (elf_header.e_shoff +
220                            elf_header.e_shentsize * elf_header.e_shstrndx);
221   if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) {
222     return false;
223   }
224
225   for (int i = 0; i < elf_header.e_shnum; ++i) {
226     off_t section_header_offset = (elf_header.e_shoff +
227                                    elf_header.e_shentsize * i);
228     if (!ReadFromOffsetExact(fd, out, sizeof(*out), section_header_offset)) {
229       return false;
230     }
231     char header_name[kMaxSectionNameLen];
232     if (sizeof(header_name) < name_len) {
233       RAW_LOG(WARNING, "Section name '%s' is too long (%"PRIuS"); "
234               "section will not be found (even if present).", name, name_len);
235       // No point in even trying.
236       return false;
237     }
238     off_t name_offset = shstrtab.sh_offset + out->sh_name;
239     ssize_t n_read = ReadFromOffset(fd, &header_name, name_len, name_offset);
240     if (n_read == -1) {
241       return false;
242     } else if (n_read != name_len) {
243       // Short read -- name could be at end of file.
244       continue;
245     }
246     if (memcmp(header_name, name, name_len) == 0) {
247       return true;
248     }
249   }
250   return false;
251 }
252
253 // Read a symbol table and look for the symbol containing the
254 // pc. Iterate over symbols in a symbol table and look for the symbol
255 // containing "pc".  On success, return true and write the symbol name
256 // to out.  Otherwise, return false.
257 // To keep stack consumption low, we would like this function to not get
258 // inlined.
259 static ATTRIBUTE_NOINLINE bool
260 FindSymbol(uint64_t pc, const int fd, char *out, int out_size,
261            uint64_t symbol_offset, const ElfW(Shdr) *strtab,
262            const ElfW(Shdr) *symtab) {
263   if (symtab == NULL) {
264     return false;
265   }
266   const int num_symbols = symtab->sh_size / symtab->sh_entsize;
267   for (int i = 0; i < num_symbols;) {
268     off_t offset = symtab->sh_offset + i * symtab->sh_entsize;
269
270     // If we are reading Elf64_Sym's, we want to limit this array to
271     // 32 elements (to keep stack consumption low), otherwise we can
272     // have a 64 element Elf32_Sym array.
273 #if __WORDSIZE == 64
274 #define NUM_SYMBOLS 32
275 #else
276 #define NUM_SYMBOLS 64
277 #endif
278
279     // Read at most NUM_SYMBOLS symbols at once to save read() calls.
280     ElfW(Sym) buf[NUM_SYMBOLS];
281     const ssize_t len = ReadFromOffset(fd, &buf, sizeof(buf), offset);
282     SAFE_ASSERT(len % sizeof(buf[0]) == 0);
283     const ssize_t num_symbols_in_buf = len / sizeof(buf[0]);
284     SAFE_ASSERT(num_symbols_in_buf <= sizeof(buf)/sizeof(buf[0]));
285     for (int j = 0; j < num_symbols_in_buf; ++j) {
286       const ElfW(Sym)& symbol = buf[j];
287       uint64_t start_address = symbol.st_value;
288       start_address += symbol_offset;
289       uint64_t end_address = start_address + symbol.st_size;
290       if (symbol.st_value != 0 &&  // Skip null value symbols.
291           symbol.st_shndx != 0 &&  // Skip undefined symbols.
292           start_address <= pc && pc < end_address) {
293         ssize_t len1 = ReadFromOffset(fd, out, out_size,
294                                       strtab->sh_offset + symbol.st_name);
295         if (len1 <= 0 || memchr(out, '\0', out_size) == NULL) {
296           return false;
297         }
298         return true;  // Obtained the symbol name.
299       }
300     }
301     i += num_symbols_in_buf;
302   }
303   return false;
304 }
305
306 // Get the symbol name of "pc" from the file pointed by "fd".  Process
307 // both regular and dynamic symbol tables if necessary.  On success,
308 // write the symbol name to "out" and return true.  Otherwise, return
309 // false.
310 static bool GetSymbolFromObjectFile(const int fd, uint64_t pc,
311                                     char *out, int out_size,
312                                     uint64_t map_start_address) {
313   // Read the ELF header.
314   ElfW(Ehdr) elf_header;
315   if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
316     return false;
317   }
318
319   uint64_t symbol_offset = 0;
320   if (elf_header.e_type == ET_DYN) {  // DSO needs offset adjustment.
321     symbol_offset = map_start_address;
322   }
323
324   ElfW(Shdr) symtab, strtab;
325
326   // Consult a regular symbol table first.
327   if (!GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
328                               SHT_SYMTAB, &symtab)) {
329     return false;
330   }
331   if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
332                            symtab.sh_link * sizeof(symtab))) {
333     return false;
334   }
335   if (FindSymbol(pc, fd, out, out_size, symbol_offset,
336                  &strtab, &symtab)) {
337     return true;  // Found the symbol in a regular symbol table.
338   }
339
340   // If the symbol is not found, then consult a dynamic symbol table.
341   if (!GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
342                               SHT_DYNSYM, &symtab)) {
343     return false;
344   }
345   if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
346                            symtab.sh_link * sizeof(symtab))) {
347     return false;
348   }
349   if (FindSymbol(pc, fd, out, out_size, symbol_offset,
350                  &strtab, &symtab)) {
351     return true;  // Found the symbol in a dynamic symbol table.
352   }
353
354   return false;
355 }
356
357 namespace {
358 // Thin wrapper around a file descriptor so that the file descriptor
359 // gets closed for sure.
360 struct FileDescriptor {
361   const int fd_;
362   explicit FileDescriptor(int fd) : fd_(fd) {}
363   ~FileDescriptor() {
364     if (fd_ >= 0) {
365       NO_INTR(close(fd_));
366     }
367   }
368   int get() { return fd_; }
369
370  private:
371   explicit FileDescriptor(const FileDescriptor&);
372   void operator=(const FileDescriptor&);
373 };
374
375 // Helper class for reading lines from file.
376 //
377 // Note: we don't use ProcMapsIterator since the object is big (it has
378 // a 5k array member) and uses async-unsafe functions such as sscanf()
379 // and snprintf().
380 class LineReader {
381  public:
382   explicit LineReader(int fd, char *buf, int buf_len) : fd_(fd),
383     buf_(buf), buf_len_(buf_len), bol_(buf), eol_(buf), eod_(buf) {
384   }
385
386   // Read '\n'-terminated line from file.  On success, modify "bol"
387   // and "eol", then return true.  Otherwise, return false.
388   //
389   // Note: if the last line doesn't end with '\n', the line will be
390   // dropped.  It's an intentional behavior to make the code simple.
391   bool ReadLine(const char **bol, const char **eol) {
392     if (BufferIsEmpty()) {  // First time.
393       const ssize_t num_bytes = ReadPersistent(fd_, buf_, buf_len_);
394       if (num_bytes <= 0) {  // EOF or error.
395         return false;
396       }
397       eod_ = buf_ + num_bytes;
398       bol_ = buf_;
399     } else {
400       bol_ = eol_ + 1;  // Advance to the next line in the buffer.
401       SAFE_ASSERT(bol_ <= eod_);  // "bol_" can point to "eod_".
402       if (!HasCompleteLine()) {
403         const int incomplete_line_length = eod_ - bol_;
404         // Move the trailing incomplete line to the beginning.
405         memmove(buf_, bol_, incomplete_line_length);
406         // Read text from file and append it.
407         char * const append_pos = buf_ + incomplete_line_length;
408         const int capacity_left = buf_len_ - incomplete_line_length;
409         const ssize_t num_bytes = ReadPersistent(fd_, append_pos,
410                                                  capacity_left);
411         if (num_bytes <= 0) {  // EOF or error.
412           return false;
413         }
414         eod_ = append_pos + num_bytes;
415         bol_ = buf_;
416       }
417     }
418     eol_ = FindLineFeed();
419     if (eol_ == NULL) {  // '\n' not found.  Malformed line.
420       return false;
421     }
422     *eol_ = '\0';  // Replace '\n' with '\0'.
423
424     *bol = bol_;
425     *eol = eol_;
426     return true;
427   }
428
429   // Beginning of line.
430   const char *bol() {
431     return bol_;
432   }
433
434   // End of line.
435   const char *eol() {
436     return eol_;
437   }
438
439  private:
440   explicit LineReader(const LineReader&);
441   void operator=(const LineReader&);
442
443   char *FindLineFeed() {
444     return reinterpret_cast<char *>(memchr(bol_, '\n', eod_ - bol_));
445   }
446
447   bool BufferIsEmpty() {
448     return buf_ == eod_;
449   }
450
451   bool HasCompleteLine() {
452     return !BufferIsEmpty() && FindLineFeed() != NULL;
453   }
454
455   const int fd_;
456   char * const buf_;
457   const int buf_len_;
458   char *bol_;
459   char *eol_;
460   const char *eod_;  // End of data in "buf_".
461 };
462 }  // namespace
463
464 // Place the hex number read from "start" into "*hex".  The pointer to
465 // the first non-hex character or "end" is returned.
466 static char *GetHex(const char *start, const char *end, uint64_t *hex) {
467   *hex = 0;
468   const char *p;
469   for (p = start; p < end; ++p) {
470     int ch = *p;
471     if ((ch >= '0' && ch <= '9') ||
472         (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) {
473       *hex = (*hex << 4) | (ch < 'A' ? ch - '0' : (ch & 0xF) + 9);
474     } else {  // Encountered the first non-hex character.
475       break;
476     }
477   }
478   SAFE_ASSERT(p <= end);
479   return const_cast<char *>(p);
480 }
481
482 // Search for the object file (from /proc/self/maps) that contains
483 // the specified pc. If found, open this file and return the file handle,
484 // and also set start_address to the start address of where this object
485 // file is mapped to in memory. Otherwise, return -1.
486 static ATTRIBUTE_NOINLINE int
487 OpenObjectFileContainingPcAndGetStartAddress(uint64_t pc,
488                                              uint64_t &start_address) {
489   int object_fd;
490
491   // Open /proc/self/maps.
492   int maps_fd;
493   NO_INTR(maps_fd = open("/proc/self/maps", O_RDONLY));
494   FileDescriptor wrapped_maps_fd(maps_fd);
495   if (wrapped_maps_fd.get() < 0) {
496     return -1;
497   }
498
499   // Iterate over maps and look for the map containing the pc.  Then
500   // look into the symbol tables inside.
501   char buf[1024];  // Big enough for line of sane /proc/self/maps
502   LineReader reader(wrapped_maps_fd.get(), buf, sizeof(buf));
503   while (true) {
504     const char *cursor;
505     const char *eol;
506     if (!reader.ReadLine(&cursor, &eol)) {  // EOF or malformed line.
507       return -1;
508     }
509
510     // Start parsing line in /proc/self/maps.  Here is an example:
511     //
512     // 08048000-0804c000 r-xp 00000000 08:01 2142121    /bin/cat
513     //
514     // We want start address (08048000), end address (0804c000), flags
515     // (r-xp) and file name (/bin/cat).
516
517     // Read start address.
518     cursor = GetHex(cursor, eol, &start_address);
519     if (cursor == eol || *cursor != '-') {
520       return -1;  // Malformed line.
521     }
522     ++cursor;  // Skip '-'.
523
524     // Read end address.
525     uint64_t end_address;
526     cursor = GetHex(cursor, eol, &end_address);
527     if (cursor == eol || *cursor != ' ') {
528       return -1;  // Malformed line.
529     }
530     ++cursor;  // Skip ' '.
531
532     // Check start and end addresses.
533     if (!(start_address <= pc && pc < end_address)) {
534       continue;  // We skip this map.  PC isn't in this map.
535     }
536
537     // Read flags.  Skip flags until we encounter a space or eol.
538     const char * const flags_start = cursor;
539     while (cursor < eol && *cursor != ' ') {
540       ++cursor;
541     }
542     // We expect at least four letters for flags (ex. "r-xp").
543     if (cursor == eol || cursor < flags_start + 4) {
544       return -1;  // Malformed line.
545     }
546
547     // Check flags.  We are only interested in "r-x" maps.
548     if (memcmp(flags_start, "r-x", 3) != 0) {  // Not a "r-x" map.
549       continue;  // We skip this map.
550     }
551     ++cursor;  // Skip ' '.
552
553     // Skip to file name.  "cursor" now points to file offset.  We need to
554     // skip at least three spaces for file offset, dev, and inode.
555     int num_spaces = 0;
556     while (cursor < eol) {
557       if (*cursor == ' ') {
558         ++num_spaces;
559       } else if (num_spaces >= 3) {
560         // The first non-space character after  skipping three spaces
561         // is the beginning of the file name.
562         break;
563       }
564       ++cursor;
565     }
566     if (cursor == eol) {
567       return -1;  // Malformed line.
568     }
569
570     // Finally, "cursor" now points to file name of our interest.
571     NO_INTR(object_fd = open(cursor, O_RDONLY));
572     if (object_fd < 0) {
573       return -1;
574     }
575     return object_fd;
576   }
577 }
578
579 // The implementation of our symbolization routine.  If it
580 // successfully finds the symbol containing "pc" and obtains the
581 // symbol name, returns true and write the symbol name to "out".
582 // Otherwise, returns false. If Callback function is installed via
583 // InstallSymbolizeCallback(), the function is also called in this function,
584 // and "out" is used as its output.
585 // To keep stack consumption low, we would like this function to not
586 // get inlined.
587 static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
588                                                     int out_size) {
589   uint64_t pc0 = reinterpret_cast<uintptr_t>(pc);
590   uint64_t start_address = 0;
591
592   int object_fd = OpenObjectFileContainingPcAndGetStartAddress(pc0,
593                                                                start_address);
594   if (object_fd == -1) {
595     return false;
596   }
597   FileDescriptor wrapped_object_fd(object_fd);
598   int elf_type = FileGetElfType(wrapped_object_fd.get());
599   if (elf_type == -1) {
600     return false;
601   }
602   if (g_symbolize_callback) {
603     // Run the call back if it's installed.
604     // Note: relocation (and much of the rest of this code) will be
605     // wrong for prelinked shared libraries and PIE executables.
606     uint64 relocation = (elf_type == ET_DYN) ? start_address : 0;
607     int num_bytes_written = g_symbolize_callback(wrapped_object_fd.get(),
608                                                  pc, out, out_size,
609                                                  relocation);
610     if (num_bytes_written > 0) {
611       out += num_bytes_written;
612       out_size -= num_bytes_written;
613     }
614   }
615   if (!GetSymbolFromObjectFile(wrapped_object_fd.get(), pc0,
616                                out, out_size, start_address)) {
617     return false;
618   }
619
620   // Symbolization succeeded.  Now we try to demangle the symbol.
621   DemangleInplace(out, out_size);
622   return true;
623 }
624
625 _END_GOOGLE_NAMESPACE_
626
627 #elif defined(OS_MACOSX) && defined(HAVE_DLADDR)
628
629 #include <dlfcn.h>
630 #include <string.h>
631
632 _START_GOOGLE_NAMESPACE_
633
634 static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
635                                                     int out_size) {
636   Dl_info info;
637   if (dladdr(pc, &info)) {
638     if (strlen(info.dli_sname) < out_size) {
639       strcpy(out, info.dli_sname);
640       // Symbolization succeeded.  Now we try to demangle the symbol.
641       DemangleInplace(out, out_size);
642       return true;
643     }
644   }
645   return false;
646 }
647
648 _END_GOOGLE_NAMESPACE_
649
650 #else
651 # error BUG: HAVE_SYMBOLIZE was wrongly set
652 #endif
653
654 _START_GOOGLE_NAMESPACE_
655
656 bool Symbolize(void *pc, char *out, int out_size) {
657   SAFE_ASSERT(out_size >= 0);
658   return SymbolizeAndDemangle(pc, out, out_size);
659 }
660
661 _END_GOOGLE_NAMESPACE_
662
663 #else  /* HAVE_SYMBOLIZE */
664
665 #include <assert.h>
666
667 #include "config.h"
668
669 _START_GOOGLE_NAMESPACE_
670
671 // TODO: Support other environments.
672 bool Symbolize(void *pc, char *out, int out_size) {
673   assert(0);
674   return false;
675 }
676
677 _END_GOOGLE_NAMESPACE_
678
679 #endif