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