Path: blob/master/src/hotspot/os/bsd/decoder_machO.cpp
40949 views
/*1* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"2526#ifdef __APPLE__27#include "jvm.h"28#include "decoder_machO.hpp"29#include "memory/allocation.inline.hpp"3031#include <cxxabi.h>32#include <mach-o/loader.h>33#include <mach-o/nlist.h>343536bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) {37int status;38char* result;39size_t size = (size_t)buflen;40// Don't pass buf to __cxa_demangle. In case of the 'buf' is too small,41// __cxa_demangle will call system "realloc" for additional memory, which42// may use different malloc/realloc mechanism that allocates 'buf'.43if ((result = abi::__cxa_demangle(symbol, NULL, NULL, &status)) != NULL) {44jio_snprintf(buf, buflen, "%s", result);45// call c library's free46::free(result);47return true;48}49return false;50}5152bool MachODecoder::decode(address addr, char *buf,53int buflen, int *offset, const void *mach_base) {54struct symtab_command * symt = (struct symtab_command *)55mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB);56if (symt == NULL) {57DEBUG_ONLY(tty->print_cr("no symtab in mach file at 0x%lx", p2i(mach_base)));58return false;59}60uint32_t off = symt->symoff; /* symbol table offset (within this mach file) */61uint32_t nsyms = symt->nsyms; /* number of symbol table entries */62uint32_t stroff = symt->stroff; /* string table offset */63uint32_t strsize = symt->strsize; /* string table size in bytes */6465// iterate through symbol table trying to match our offset6667uint32_t addr_relative = (uintptr_t) mach_base - (uintptr_t) addr; // offset we seek in the symtab68void * symtab_addr = (void*) ((uintptr_t) mach_base + off);69struct nlist_64 *cur_nlist = (struct nlist_64 *) symtab_addr;70struct nlist_64 *last_nlist = cur_nlist; // no size stored in an entry, so keep previously seen nlist7172int32_t found_strx = 0;73int32_t found_symval = 0;7475for (uint32_t i=0; i < nsyms; i++) {76uint32_t this_value = cur_nlist->n_value;7778if (addr_relative == this_value) {79found_strx = cur_nlist->n_un.n_strx;80found_symval = this_value;81break;82} else if (addr_relative > this_value) {83// gone past it, use previously seen nlist:84found_strx = last_nlist->n_un.n_strx;85found_symval = last_nlist->n_value;86break;87}88last_nlist = cur_nlist;89cur_nlist = cur_nlist + sizeof(struct nlist_64);90}91if (found_strx == 0) {92return false;93}94// write the offset:95*offset = addr_relative - found_symval;9697// lookup found_strx in the string table98char * symname = mach_find_in_stringtable((char*) ((uintptr_t)mach_base + stroff), strsize, found_strx);99if (symname) {100strncpy(buf, symname, buflen);101buf[buflen - 1] = '\0';102return true;103}104DEBUG_ONLY(tty->print_cr("no string or null string found."));105return false;106}107108void* MachODecoder::mach_find_command(struct mach_header_64 * mach_base, uint32_t command_wanted) {109// possibly verify it is a mach_header, use magic number.110// commands begin immediately after the header.111struct load_command *pos = (struct load_command *) mach_base + sizeof(struct mach_header_64);112for (uint32_t i = 0; i < mach_base->ncmds; i++) {113struct load_command *this_cmd = (struct load_command *) pos;114if (this_cmd->cmd == command_wanted) {115return pos;116}117int cmdsize = this_cmd->cmdsize;118pos += cmdsize;119}120return NULL;121}122123char* MachODecoder::mach_find_in_stringtable(char *strtab, uint32_t tablesize, int strx_wanted) {124125if (strx_wanted == 0) {126return NULL;127}128char *strtab_end = strtab + tablesize;129130// find the first string, skip over the space char131// (or the four zero bytes we see e.g. in libclient)132if (*strtab == ' ') {133strtab++;134if (*strtab != 0) {135DEBUG_ONLY(tty->print_cr("string table has leading space but no following zero."));136return NULL;137}138strtab++;139} else {140if ((uint32_t) *strtab != 0) {141DEBUG_ONLY(tty->print_cr("string table without leading space or leading int of zero."));142return NULL;143}144strtab+=4;145}146// read the real strings starting at index 1147int cur_strx = 1;148while (strtab < strtab_end) {149if (cur_strx == strx_wanted) {150return strtab;151}152// find start of next string153while (*strtab != 0) {154strtab++;155}156strtab++; // skip the terminating zero157cur_strx++;158}159DEBUG_ONLY(tty->print_cr("string number %d not found.", strx_wanted));160return NULL;161}162163164#endif165166167168169