src/os/bsd/vm/decoder_machO.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

merge

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

mercurial