src/os/bsd/vm/decoder_machO.cpp

Tue, 29 Apr 2014 15:17:27 +0200

author
goetz
date
Tue, 29 Apr 2014 15:17:27 +0200
changeset 6911
ce8f6bb717c9
parent 6680
78bbf4d43a14
child 6876
710a3c8b516e
child 9920
3a3803a0c789
permissions
-rw-r--r--

8042195: Introduce umbrella header orderAccess.inline.hpp.
Reviewed-by: dholmes, kvn, stefank, twisti

zgu@3430 1 /*
drchase@6680 2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
zgu@3430 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
zgu@3430 4 *
zgu@3430 5 * This code is free software; you can redistribute it and/or modify it
zgu@3430 6 * under the terms of the GNU General Public License version 2 only, as
zgu@3430 7 * published by the Free Software Foundation.
zgu@3430 8 *
zgu@3430 9 * This code is distributed in the hope that it will be useful, but WITHOUT
zgu@3430 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
zgu@3430 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
zgu@3430 12 * version 2 for more details (a copy is included in the LICENSE file that
zgu@3430 13 * accompanied this code).
zgu@3430 14 *
zgu@3430 15 * You should have received a copy of the GNU General Public License version
zgu@3430 16 * 2 along with this work; if not, write to the Free Software Foundation,
zgu@3430 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
zgu@3430 18 *
zgu@3430 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
zgu@3430 20 * or visit www.oracle.com if you need additional information or have any
zgu@3430 21 * questions.
zgu@3430 22 *
zgu@3430 23 */
zgu@3430 24
zgu@3430 25 #include "precompiled.hpp"
zgu@3430 26
zgu@3430 27 #ifdef __APPLE__
zgu@3430 28 #include "decoder_machO.hpp"
zgu@3961 29
zgu@3961 30 #include <cxxabi.h>
zgu@3961 31 #include <mach-o/loader.h>
zgu@3961 32 #include <mach-o/nlist.h>
zgu@3961 33
zgu@3961 34
zgu@3961 35 bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) {
zgu@3961 36 int status;
zgu@3961 37 char* result;
zgu@3961 38 size_t size = (size_t)buflen;
zgu@3961 39 // Don't pass buf to __cxa_demangle. In case of the 'buf' is too small,
zgu@3961 40 // __cxa_demangle will call system "realloc" for additional memory, which
zgu@3961 41 // may use different malloc/realloc mechanism that allocates 'buf'.
zgu@3961 42 if ((result = abi::__cxa_demangle(symbol, NULL, NULL, &status)) != NULL) {
zgu@3961 43 jio_snprintf(buf, buflen, "%s", result);
zgu@3961 44 // call c library's free
zgu@3961 45 ::free(result);
zgu@3961 46 return true;
zgu@3961 47 }
zgu@3961 48 return false;
zgu@3961 49 }
zgu@3961 50
zgu@3961 51 bool MachODecoder::decode(address addr, char *buf,
zgu@3961 52 int buflen, int *offset, const void *mach_base) {
zgu@3961 53 struct symtab_command * symt = (struct symtab_command *)
zgu@3961 54 mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB);
zgu@3961 55 if (symt == NULL) {
drchase@6680 56 DEBUG_ONLY(tty->print_cr("no symtab in mach file at 0x%lx", p2i(mach_base)));
zgu@3961 57 return false;
zgu@3961 58 }
zgu@3961 59 uint32_t off = symt->symoff; /* symbol table offset (within this mach file) */
zgu@3961 60 uint32_t nsyms = symt->nsyms; /* number of symbol table entries */
zgu@3961 61 uint32_t stroff = symt->stroff; /* string table offset */
zgu@3961 62 uint32_t strsize = symt->strsize; /* string table size in bytes */
zgu@3961 63
zgu@3961 64 // iterate through symbol table trying to match our offset
zgu@3961 65
zgu@3961 66 uint32_t addr_relative = (uintptr_t) mach_base - (uintptr_t) addr; // offset we seek in the symtab
zgu@3961 67 void * symtab_addr = (void*) ((uintptr_t) mach_base + off);
zgu@3961 68 struct nlist_64 *cur_nlist = (struct nlist_64 *) symtab_addr;
zgu@3961 69 struct nlist_64 *last_nlist = cur_nlist; // no size stored in an entry, so keep previously seen nlist
zgu@3961 70
zgu@3961 71 int32_t found_strx = 0;
zgu@3961 72 int32_t found_symval = 0;
zgu@3961 73
zgu@3961 74 for (uint32_t i=0; i < nsyms; i++) {
zgu@3961 75 uint32_t this_value = cur_nlist->n_value;
zgu@3961 76
zgu@3961 77 if (addr_relative == this_value) {
zgu@3961 78 found_strx = cur_nlist->n_un.n_strx;
zgu@3961 79 found_symval = this_value;
zgu@3961 80 break;
zgu@3961 81 } else if (addr_relative > this_value) {
zgu@3961 82 // gone past it, use previously seen nlist:
zgu@3961 83 found_strx = last_nlist->n_un.n_strx;
zgu@3961 84 found_symval = last_nlist->n_value;
zgu@3961 85 break;
zgu@3961 86 }
zgu@3961 87 last_nlist = cur_nlist;
zgu@3961 88 cur_nlist = cur_nlist + sizeof(struct nlist_64);
zgu@3961 89 }
zgu@3961 90 if (found_strx == 0) {
zgu@3961 91 return false;
zgu@3961 92 }
zgu@3961 93 // write the offset:
zgu@3961 94 *offset = addr_relative - found_symval;
zgu@3961 95
zgu@3961 96 // lookup found_strx in the string table
zgu@3961 97 char * symname = mach_find_in_stringtable((char*) ((uintptr_t)mach_base + stroff), strsize, found_strx);
zgu@3961 98 if (symname) {
zgu@3961 99 strncpy(buf, symname, buflen);
zgu@3961 100 return true;
zgu@3961 101 }
zgu@3961 102 DEBUG_ONLY(tty->print_cr("no string or null string found."));
zgu@3961 103 return false;
zgu@3961 104 }
zgu@3961 105
zgu@3961 106 void* MachODecoder::mach_find_command(struct mach_header_64 * mach_base, uint32_t command_wanted) {
zgu@3961 107 // possibly verify it is a mach_header, use magic number.
zgu@3961 108 // commands begin immediately after the header.
zgu@3961 109 struct load_command *pos = (struct load_command *) mach_base + sizeof(struct mach_header_64);
zgu@3961 110 for (uint32_t i = 0; i < mach_base->ncmds; i++) {
zgu@3961 111 struct load_command *this_cmd = (struct load_command *) pos;
zgu@3961 112 if (this_cmd->cmd == command_wanted) {
zgu@3961 113 return pos;
zgu@3961 114 }
zgu@3961 115 int cmdsize = this_cmd->cmdsize;
zgu@3961 116 pos += cmdsize;
zgu@3961 117 }
zgu@3961 118 return NULL;
zgu@3961 119 }
zgu@3961 120
zgu@3961 121 char* MachODecoder::mach_find_in_stringtable(char *strtab, uint32_t tablesize, int strx_wanted) {
zgu@3961 122
zgu@3961 123 if (strx_wanted == 0) {
zgu@3961 124 return NULL;
zgu@3961 125 }
zgu@3961 126 char *strtab_end = strtab + tablesize;
zgu@3961 127
zgu@3961 128 // find the first string, skip over the space char
zgu@3961 129 // (or the four zero bytes we see e.g. in libclient)
zgu@3961 130 if (*strtab == ' ') {
zgu@3961 131 strtab++;
zgu@3961 132 if (*strtab != 0) {
zgu@3961 133 DEBUG_ONLY(tty->print_cr("string table has leading space but no following zero."));
zgu@3961 134 return NULL;
zgu@3961 135 }
zgu@3961 136 strtab++;
zgu@3961 137 } else {
zgu@3961 138 if ((uint32_t) *strtab != 0) {
zgu@3961 139 DEBUG_ONLY(tty->print_cr("string table without leading space or leading int of zero."));
zgu@3961 140 return NULL;
zgu@3961 141 }
zgu@3961 142 strtab+=4;
zgu@3961 143 }
zgu@3961 144 // read the real strings starting at index 1
zgu@3961 145 int cur_strx = 1;
zgu@3961 146 while (strtab < strtab_end) {
zgu@3961 147 if (cur_strx == strx_wanted) {
zgu@3961 148 return strtab;
zgu@3961 149 }
zgu@3961 150 // find start of next string
zgu@3961 151 while (*strtab != 0) {
zgu@3961 152 strtab++;
zgu@3961 153 }
zgu@3961 154 strtab++; // skip the terminating zero
zgu@3961 155 cur_strx++;
zgu@3961 156 }
zgu@3961 157 DEBUG_ONLY(tty->print_cr("string number %d not found.", strx_wanted));
zgu@3961 158 return NULL;
zgu@3961 159 }
zgu@3961 160
zgu@3961 161
zgu@3430 162 #endif
zgu@3430 163
zgu@3430 164

mercurial