1 // Copyright 2006 Google Inc. All Rights Reserved.
2 // Author: Satoru Takabayashi
3 // Stack-footprint reduction work done by Raksit Ashok
5 // Implementation note:
7 // We don't use heaps but only use stacks. We want to reduce the
8 // stack consumption so that the symbolizer can run on small stacks.
10 // Here are some numbers collected with GCC 4.1.0 on x86:
11 // - sizeof(Elf32_Sym) = 16
12 // - sizeof(Elf32_Shdr) = 40
13 // - sizeof(Elf64_Sym) = 24
14 // - sizeof(Elf64_Shdr) = 64
16 // This implementation is intended to be async-signal-safe but uses
17 // some functions which are not guaranteed to be so, such as memchr()
18 // and memmove(). We assume they are async-signal-safe.
21 #if defined(__ELF__) // defined by gcc on Linux
28 #include <link.h> // For ElfW() macro.
35 #include <sys/types.h>
38 #include "symbolize.h"
41 #include "utilities.h"
42 #include "glog/raw_logging.h"
44 // Re-runs fn until it doesn't cause EINTR.
45 #define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR)
47 _START_GOOGLE_NAMESPACE_
49 // We don't use assert() since it's not guaranteed to be
50 // async-signal-safe. Instead we define a minimal assertion
51 // macro. So far, we don't need pretty printing for __FILE__, etc.
53 // A wrapper for abort() to make it callable in ? :.
54 static int AssertFail() {
56 return 0; // Should not reach.
59 #define SAFE_ASSERT(expr) ((expr) ? 0 : AssertFail())
61 // Read up to "count" bytes from file descriptor "fd" into the buffer
62 // starting at "buf" while handling short reads and EINTR. On
63 // success, return the number of bytes read. Otherwise, return -1.
64 static ssize_t ReadPersistent(const int fd, void *buf, const size_t count) {
66 SAFE_ASSERT(count >= 0 && count <= SSIZE_MAX);
67 char *buf0 = reinterpret_cast<char *>(buf);
68 ssize_t num_bytes = 0;
69 while (num_bytes < count) {
71 NO_INTR(len = read(fd, buf0 + num_bytes, count - num_bytes));
72 if (len < 0) { // There was an error other than EINTR.
75 if (len == 0) { // Reached EOF.
80 SAFE_ASSERT(num_bytes <= count);
84 // Read up to "count" bytes from "offset" in the file pointed by file
85 // descriptor "fd" into the buffer starting at "buf". On success,
86 // return the number of bytes read. Otherwise, return -1.
87 static ssize_t ReadFromOffset(const int fd, void *buf,
88 const size_t count, const off_t offset) {
89 off_t off = lseek(fd, offset, SEEK_SET);
90 if (off == (off_t)-1) {
93 return ReadPersistent(fd, buf, count);
96 // Try reading exactly "count" bytes from "offset" bytes in a file
97 // pointed by "fd" into the buffer starting at "buf" while handling
98 // short reads and EINTR. On success, return true. Otherwise, return
100 static bool ReadFromOffsetExact(const int fd, void *buf,
101 const size_t count, const off_t offset) {
102 ssize_t len = ReadFromOffset(fd, buf, count, offset);
106 // Returns elf_header.e_type if the file pointed by fd is an ELF binary.
107 static int FileGetElfType(const int fd) {
108 ElfW(Ehdr) elf_header;
109 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
112 if (memcmp(elf_header.e_ident, ELFMAG, SELFMAG) != 0) {
115 return elf_header.e_type;
118 // Read the section headers in the given ELF binary, and if a section
119 // of the specified type is found, set the output to this section header
120 // and return true. Otherwise, return false.
121 // To keep stack consumption low, we would like this function to not get
123 static ATTRIBUTE_NOINLINE bool
124 GetSectionHeaderByType(const int fd, ElfW(Half) sh_num, const off_t sh_offset,
125 ElfW(Word) type, ElfW(Shdr) *out) {
126 // Read at most 16 section headers at a time to save read calls.
128 for (int i = 0; i < sh_num;) {
129 const ssize_t num_bytes_left = (sh_num - i) * sizeof(buf[0]);
130 const ssize_t num_bytes_to_read =
131 (sizeof(buf) > num_bytes_left) ? num_bytes_left : sizeof(buf);
132 const ssize_t len = ReadFromOffset(fd, buf, num_bytes_to_read,
133 sh_offset + i * sizeof(buf[0]));
134 SAFE_ASSERT(len % sizeof(buf[0]) == 0);
135 const ssize_t num_headers_in_buf = len / sizeof(buf[0]);
136 SAFE_ASSERT(num_headers_in_buf <= sizeof(buf) / sizeof(buf[0]));
137 for (int j = 0; j < num_headers_in_buf; ++j) {
138 if (buf[j].sh_type == type) {
143 i += num_headers_in_buf;
148 // There is no particular reason to limit section name to 63 characters,
149 // but there has (as yet) been no need for anything longer either.
150 const int kMaxSectionNameLen = 64;
152 // name_len should include terminating '\0'.
153 bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
155 ElfW(Ehdr) elf_header;
156 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
161 off_t shstrtab_offset = (elf_header.e_shoff +
162 elf_header.e_shentsize * elf_header.e_shstrndx);
163 if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) {
167 for (int i = 0; i < elf_header.e_shnum; ++i) {
168 off_t section_header_offset = (elf_header.e_shoff +
169 elf_header.e_shentsize * i);
170 if (!ReadFromOffsetExact(fd, out, sizeof(*out), section_header_offset)) {
173 char header_name[kMaxSectionNameLen];
174 if (sizeof(header_name) < name_len) {
175 RAW_LOG(WARNING, "Section name '%s' is too long (%"PRIuS"); "
176 "section will not be found (even if present).", name, name_len);
177 // No point in even trying.
180 off_t name_offset = shstrtab.sh_offset + out->sh_name;
181 ssize_t n_read = ReadFromOffset(fd, &header_name, name_len, name_offset);
184 } else if (n_read != name_len) {
185 // Short read -- name could be at end of file.
188 if (memcmp(header_name, name, name_len) == 0) {
195 // Read a symbol table and look for the symbol containing the
196 // pc. Iterate over symbols in a symbol table and look for the symbol
197 // containing "pc". On success, return true and write the symbol name
198 // to out. Otherwise, return false.
199 // To keep stack consumption low, we would like this function to not get
201 static ATTRIBUTE_NOINLINE bool
202 FindSymbol(uint64_t pc, const int fd, char *out, int out_size,
203 uint64_t symbol_offset, const ElfW(Shdr) *strtab,
204 const ElfW(Shdr) *symtab) {
205 if (symtab == NULL) {
208 const int num_symbols = symtab->sh_size / symtab->sh_entsize;
209 for (int i = 0; i < num_symbols;) {
210 off_t offset = symtab->sh_offset + i * symtab->sh_entsize;
212 // If we are reading Elf64_Sym's, we want to limit this array to
213 // 32 elements (to keep stack consumption low), otherwise we can
214 // have a 64 element Elf32_Sym array.
216 #define NUM_SYMBOLS 32
218 #define NUM_SYMBOLS 64
221 // Read at most NUM_SYMBOLS symbols at once to save read() calls.
222 ElfW(Sym) buf[NUM_SYMBOLS];
223 const ssize_t len = ReadFromOffset(fd, &buf, sizeof(buf), offset);
224 SAFE_ASSERT(len % sizeof(buf[0]) == 0);
225 const ssize_t num_symbols_in_buf = len / sizeof(buf[0]);
226 SAFE_ASSERT(num_symbols_in_buf <= sizeof(buf)/sizeof(buf[0]));
227 for (int j = 0; j < num_symbols_in_buf; ++j) {
228 const ElfW(Sym)& symbol = buf[j];
229 uint64_t start_address = symbol.st_value;
230 start_address += symbol_offset;
231 uint64_t end_address = start_address + symbol.st_size;
232 if (symbol.st_value != 0 && // Skip null value symbols.
233 symbol.st_shndx != 0 && // Skip undefined symbols.
234 start_address <= pc && pc < end_address) {
235 ssize_t len1 = ReadFromOffset(fd, out, out_size,
236 strtab->sh_offset + symbol.st_name);
237 if (len1 <= 0 || memchr(out, '\0', out_size) == NULL) {
240 return true; // Obtained the symbol name.
243 i += num_symbols_in_buf;
248 // Get the symbol name of "pc" from the file pointed by "fd". Process
249 // both regular and dynamic symbol tables if necessary. On success,
250 // write the symbol name to "out" and return true. Otherwise, return
252 static bool GetSymbolFromObjectFile(const int fd, uint64_t pc,
253 char *out, int out_size,
254 uint64_t map_start_address) {
255 // Read the ELF header.
256 ElfW(Ehdr) elf_header;
257 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
261 uint64_t symbol_offset = 0;
262 if (elf_header.e_type == ET_DYN) { // DSO needs offset adjustment.
263 symbol_offset = map_start_address;
266 ElfW(Shdr) symtab, strtab;
268 // Consult a regular symbol table first.
269 if (!GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
270 SHT_SYMTAB, &symtab)) {
273 if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
274 symtab.sh_link * sizeof(symtab))) {
277 if (FindSymbol(pc, fd, out, out_size, symbol_offset,
279 return true; // Found the symbol in a regular symbol table.
282 // If the symbol is not found, then consult a dynamic symbol table.
283 if (!GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
284 SHT_DYNSYM, &symtab)) {
287 if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
288 symtab.sh_link * sizeof(symtab))) {
291 if (FindSymbol(pc, fd, out, out_size, symbol_offset,
293 return true; // Found the symbol in a dynamic symbol table.
300 // Thin wrapper around a file descriptor so that the file descriptor
301 // gets closed for sure.
302 struct FileDescriptor {
304 explicit FileDescriptor(int fd) : fd_(fd) {}
310 int get() { return fd_; }
313 explicit FileDescriptor(const FileDescriptor&);
314 void operator=(const FileDescriptor&);
317 // Helper class for reading lines from file.
319 // Note: we don't use ProcMapsIterator since the object is big (it has
320 // a 5k array member) and uses async-unsafe functions such as sscanf()
324 explicit LineReader(int fd, char *buf, int buf_len) : fd_(fd),
325 buf_(buf), buf_len_(buf_len), bol_(buf), eol_(buf), eod_(buf) {
328 // Read '\n'-terminated line from file. On success, modify "bol"
329 // and "eol", then return true. Otherwise, return false.
331 // Note: if the last line doesn't end with '\n', the line will be
332 // dropped. It's an intentional behavior to make the code simple.
333 bool ReadLine(const char **bol, const char **eol) {
334 if (BufferIsEmpty()) { // First time.
335 const ssize_t num_bytes = ReadPersistent(fd_, buf_, buf_len_);
336 if (num_bytes <= 0) { // EOF or error.
339 eod_ = buf_ + num_bytes;
342 bol_ = eol_ + 1; // Advance to the next line in the buffer.
343 SAFE_ASSERT(bol_ <= eod_); // "bol_" can point to "eod_".
344 if (!HasCompleteLine()) {
345 const int incomplete_line_length = eod_ - bol_;
346 // Move the trailing incomplete line to the beginning.
347 memmove(buf_, bol_, incomplete_line_length);
348 // Read text from file and append it.
349 char * const append_pos = buf_ + incomplete_line_length;
350 const int capacity_left = buf_len_ - incomplete_line_length;
351 const ssize_t num_bytes = ReadPersistent(fd_, append_pos,
353 if (num_bytes <= 0) { // EOF or error.
356 eod_ = append_pos + num_bytes;
360 eol_ = FindLineFeed();
361 if (eol_ == NULL) { // '\n' not found. Malformed line.
364 *eol_ = '\0'; // Replace '\n' with '\0'.
371 // Beginning of line.
382 explicit LineReader(const LineReader&);
383 void operator=(const LineReader&);
385 char *FindLineFeed() {
386 return reinterpret_cast<char *>
387 (memchr(reinterpret_cast<const void *>(bol_), '\n', eod_ - bol_));
390 bool BufferIsEmpty() {
394 bool HasCompleteLine() {
395 return !BufferIsEmpty() && FindLineFeed() != NULL;
403 const char *eod_; // End of data in "buf_".
407 // Place the hex number read from "start" into "*hex". The pointer to
408 // the first non-hex character or "end" is returned.
409 static char *GetHex(const char *start, const char *end, uint64_t *hex) {
412 for (p = start; p < end; ++p) {
414 if ((ch >= '0' && ch <= '9') ||
415 (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) {
416 *hex = (*hex << 4) | (ch < 'A' ? ch - '0' : (ch & 0xF) + 9);
417 } else { // Encountered the first non-hex character.
421 SAFE_ASSERT(p <= end);
422 return const_cast<char *>(p);
425 // Search for the object file (from /proc/self/maps) that contains
426 // the specified pc. If found, open this file and return the file handle,
427 // and also set start_address to the start address of where this object
428 // file is mapped to in memory. Otherwise, return -1.
429 static ATTRIBUTE_NOINLINE int
430 OpenObjectFileContainingPcAndGetStartAddress(uint64_t pc,
431 uint64_t &start_address) {
434 // Open /proc/self/maps.
436 NO_INTR(maps_fd = open("/proc/self/maps", O_RDONLY));
437 FileDescriptor wrapped_maps_fd(maps_fd);
438 if (wrapped_maps_fd.get() < 0) {
442 // Iterate over maps and look for the map containing the pc. Then
443 // look into the symbol tables inside.
444 char buf[1024]; // Big enough for line of sane /proc/self/maps
445 LineReader reader(wrapped_maps_fd.get(), buf, sizeof(buf));
449 if (!reader.ReadLine(&cursor, &eol)) { // EOF or malformed line.
453 // Start parsing line in /proc/self/maps. Here is an example:
455 // 08048000-0804c000 r-xp 00000000 08:01 2142121 /bin/cat
457 // We want start address (08048000), end address (0804c000), flags
458 // (r-xp) and file name (/bin/cat).
460 // Read start address.
461 cursor = GetHex(cursor, eol, &start_address);
462 if (cursor == eol || *cursor != '-') {
463 return -1; // Malformed line.
465 ++cursor; // Skip '-'.
468 uint64_t end_address;
469 cursor = GetHex(cursor, eol, &end_address);
470 if (cursor == eol || *cursor != ' ') {
471 return -1; // Malformed line.
473 ++cursor; // Skip ' '.
475 // Check start and end addresses.
476 if (!(start_address <= pc && pc < end_address)) {
477 continue; // We skip this map. PC isn't in this map.
480 // Read flags. Skip flags until we encounter a space or eol.
481 const char * const flags_start = cursor;
482 while (cursor < eol && *cursor != ' ') {
485 // We expect at least four letters for flags (ex. "r-xp").
486 if (cursor == eol || cursor < flags_start + 4) {
487 return -1; // Malformed line.
490 // Check flags. We are only interested in "r-x" maps.
491 if (memcmp(flags_start, "r-x", 3) != 0) { // Not a "r-x" map.
492 continue; // We skip this map.
494 ++cursor; // Skip ' '.
496 // Skip to file name. "cursor" now points to file offset. We need to
497 // skip at least three spaces for file offset, dev, and inode.
499 while (cursor < eol) {
500 if (*cursor == ' ') {
502 } else if (num_spaces >= 3) {
503 // The first non-space character after skipping three spaces
504 // is the beginning of the file name.
510 return -1; // Malformed line.
513 // Finally, "cursor" now points to file name of our interest.
514 NO_INTR(object_fd = open(cursor, O_RDONLY));
523 SymbolizeCallback g_symbolize_callback = NULL;
524 void InstallSymbolizeCallback(SymbolizeCallback callback) {
525 g_symbolize_callback = callback;
528 // This function wraps the Demangle function to provide an interface
529 // where the input symbol is demangled in-place.
530 // To keep stack consumption low, we would like this function to not
532 static ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size) {
533 char demangled[256]; // Big enough for sane demangled symbols.
534 if (Demangle(out, demangled, sizeof(demangled))) {
535 // Demangling succeeded. Copy to out if the space allows.
536 int len = strlen(demangled);
537 if (len + 1 <= out_size) { // +1 for '\0'.
538 SAFE_ASSERT(len < sizeof(demangled));
539 memmove(out, demangled, len + 1);
544 // The implementation of our symbolization routine. If it
545 // successfully finds the symbol containing "pc" and obtains the
546 // symbol name, returns true and write the symbol name to "out".
547 // Otherwise, returns false. If Callback function is installed via
548 // InstallSymbolizeCallback(), the function is also called in this function,
549 // and "out" is used as its output.
550 // To keep stack consumption low, we would like this function to not
552 static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
554 uint64_t pc0 = reinterpret_cast<uintptr_t>(pc);
555 uint64_t start_address = 0;
557 int object_fd = OpenObjectFileContainingPcAndGetStartAddress(pc0,
559 if (object_fd == -1) {
562 FileDescriptor wrapped_object_fd(object_fd);
563 int elf_type = FileGetElfType(wrapped_object_fd.get());
564 if (elf_type == -1) {
567 if (g_symbolize_callback) {
568 // Run the call back if it's installed.
569 // Note: relocation (and much of the rest of this code) will be
570 // wrong for prelinked shared libraries and PIE executables.
571 uint64 relocation = (elf_type == ET_DYN) ? start_address : 0;
572 int num_bytes_written = g_symbolize_callback(wrapped_object_fd.get(),
575 if (num_bytes_written > 0) {
576 out += num_bytes_written;
577 out_size -= num_bytes_written;
580 if (!GetSymbolFromObjectFile(wrapped_object_fd.get(), pc0,
581 out, out_size, start_address)) {
584 // Symbolization succeeded. Now we try to demangle the symbol.
585 DemangleInplace(out, out_size);
589 bool Symbolize(void *pc, char *out, int out_size) {
590 SAFE_ASSERT(out_size >= 0);
591 return SymbolizeAndDemangle(pc, out, out_size);
594 _END_GOOGLE_NAMESPACE_
602 _START_GOOGLE_NAMESPACE_
604 // TODO: osx-port-incomplete. An alternative is Brakepad, but I don't
605 // think we want that mixed up in google3.
606 bool Symbolize(void *pc, char *out, int out_size) {
611 _END_GOOGLE_NAMESPACE_