aoqi@0: /* aoqi@0: * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: aoqi@0: #ifdef __APPLE__ aoqi@0: #include "decoder_machO.hpp" aoqi@0: aoqi@0: #include aoqi@0: #include aoqi@0: #include aoqi@0: aoqi@0: aoqi@0: bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) { aoqi@0: int status; aoqi@0: char* result; aoqi@0: size_t size = (size_t)buflen; aoqi@0: // Don't pass buf to __cxa_demangle. In case of the 'buf' is too small, aoqi@0: // __cxa_demangle will call system "realloc" for additional memory, which aoqi@0: // may use different malloc/realloc mechanism that allocates 'buf'. aoqi@0: if ((result = abi::__cxa_demangle(symbol, NULL, NULL, &status)) != NULL) { aoqi@0: jio_snprintf(buf, buflen, "%s", result); aoqi@0: // call c library's free aoqi@0: ::free(result); aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: bool MachODecoder::decode(address addr, char *buf, aoqi@0: int buflen, int *offset, const void *mach_base) { aoqi@0: struct symtab_command * symt = (struct symtab_command *) aoqi@0: mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB); aoqi@0: if (symt == NULL) { aoqi@0: DEBUG_ONLY(tty->print_cr("no symtab in mach file at 0x%lx", p2i(mach_base))); aoqi@0: return false; aoqi@0: } aoqi@0: uint32_t off = symt->symoff; /* symbol table offset (within this mach file) */ aoqi@0: uint32_t nsyms = symt->nsyms; /* number of symbol table entries */ aoqi@0: uint32_t stroff = symt->stroff; /* string table offset */ aoqi@0: uint32_t strsize = symt->strsize; /* string table size in bytes */ aoqi@0: aoqi@0: // iterate through symbol table trying to match our offset aoqi@0: aoqi@0: uint32_t addr_relative = (uintptr_t) mach_base - (uintptr_t) addr; // offset we seek in the symtab aoqi@0: void * symtab_addr = (void*) ((uintptr_t) mach_base + off); aoqi@0: struct nlist_64 *cur_nlist = (struct nlist_64 *) symtab_addr; aoqi@0: struct nlist_64 *last_nlist = cur_nlist; // no size stored in an entry, so keep previously seen nlist aoqi@0: aoqi@0: int32_t found_strx = 0; aoqi@0: int32_t found_symval = 0; aoqi@0: aoqi@0: for (uint32_t i=0; i < nsyms; i++) { aoqi@0: uint32_t this_value = cur_nlist->n_value; aoqi@0: aoqi@0: if (addr_relative == this_value) { aoqi@0: found_strx = cur_nlist->n_un.n_strx; aoqi@0: found_symval = this_value; aoqi@0: break; aoqi@0: } else if (addr_relative > this_value) { aoqi@0: // gone past it, use previously seen nlist: aoqi@0: found_strx = last_nlist->n_un.n_strx; aoqi@0: found_symval = last_nlist->n_value; aoqi@0: break; aoqi@0: } aoqi@0: last_nlist = cur_nlist; aoqi@0: cur_nlist = cur_nlist + sizeof(struct nlist_64); aoqi@0: } aoqi@0: if (found_strx == 0) { aoqi@0: return false; aoqi@0: } aoqi@0: // write the offset: aoqi@0: *offset = addr_relative - found_symval; aoqi@0: aoqi@0: // lookup found_strx in the string table aoqi@0: char * symname = mach_find_in_stringtable((char*) ((uintptr_t)mach_base + stroff), strsize, found_strx); aoqi@0: if (symname) { aoqi@0: strncpy(buf, symname, buflen); aoqi@0: return true; aoqi@0: } aoqi@0: DEBUG_ONLY(tty->print_cr("no string or null string found.")); aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: void* MachODecoder::mach_find_command(struct mach_header_64 * mach_base, uint32_t command_wanted) { aoqi@0: // possibly verify it is a mach_header, use magic number. aoqi@0: // commands begin immediately after the header. aoqi@0: struct load_command *pos = (struct load_command *) mach_base + sizeof(struct mach_header_64); aoqi@0: for (uint32_t i = 0; i < mach_base->ncmds; i++) { aoqi@0: struct load_command *this_cmd = (struct load_command *) pos; aoqi@0: if (this_cmd->cmd == command_wanted) { aoqi@0: return pos; aoqi@0: } aoqi@0: int cmdsize = this_cmd->cmdsize; aoqi@0: pos += cmdsize; aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: char* MachODecoder::mach_find_in_stringtable(char *strtab, uint32_t tablesize, int strx_wanted) { aoqi@0: aoqi@0: if (strx_wanted == 0) { aoqi@0: return NULL; aoqi@0: } aoqi@0: char *strtab_end = strtab + tablesize; aoqi@0: aoqi@0: // find the first string, skip over the space char aoqi@0: // (or the four zero bytes we see e.g. in libclient) aoqi@0: if (*strtab == ' ') { aoqi@0: strtab++; aoqi@0: if (*strtab != 0) { aoqi@0: DEBUG_ONLY(tty->print_cr("string table has leading space but no following zero.")); aoqi@0: return NULL; aoqi@0: } aoqi@0: strtab++; aoqi@0: } else { aoqi@0: if ((uint32_t) *strtab != 0) { aoqi@0: DEBUG_ONLY(tty->print_cr("string table without leading space or leading int of zero.")); aoqi@0: return NULL; aoqi@0: } aoqi@0: strtab+=4; aoqi@0: } aoqi@0: // read the real strings starting at index 1 aoqi@0: int cur_strx = 1; aoqi@0: while (strtab < strtab_end) { aoqi@0: if (cur_strx == strx_wanted) { aoqi@0: return strtab; aoqi@0: } aoqi@0: // find start of next string aoqi@0: while (*strtab != 0) { aoqi@0: strtab++; aoqi@0: } aoqi@0: strtab++; // skip the terminating zero aoqi@0: cur_strx++; aoqi@0: } aoqi@0: DEBUG_ONLY(tty->print_cr("string number %d not found.", strx_wanted)); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #endif aoqi@0: aoqi@0: