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