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