comparison src/os/bsd/vm/decoder_machO.cpp @ 6258:3b01d0321dfa

7186778: MachO decoder implementation for MacOSX Summary: Implementation of decoder for Apple's MacOSX. The implementation is based on the patch provided by Kevin Walls. Reviewed-by: coleenp, kamg, kevinw
author zgu
date Mon, 30 Jul 2012 10:25:52 -0400
parents d7e3846464d0
children b9a9ed0f8eeb
comparison
equal deleted inserted replaced
6236:950ed41429e5 6258:3b01d0321dfa
24 24
25 #include "precompiled.hpp" 25 #include "precompiled.hpp"
26 26
27 #ifdef __APPLE__ 27 #ifdef __APPLE__
28 #include "decoder_machO.hpp" 28 #include "decoder_machO.hpp"
29
30 #include <cxxabi.h>
31 #include <mach-o/loader.h>
32 #include <mach-o/nlist.h>
33
34
35 bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) {
36 int status;
37 char* result;
38 size_t size = (size_t)buflen;
39 // Don't pass buf to __cxa_demangle. In case of the 'buf' is too small,
40 // __cxa_demangle will call system "realloc" for additional memory, which
41 // may use different malloc/realloc mechanism that allocates 'buf'.
42 if ((result = abi::__cxa_demangle(symbol, NULL, NULL, &status)) != NULL) {
43 jio_snprintf(buf, buflen, "%s", result);
44 // call c library's free
45 ::free(result);
46 return true;
47 }
48 return false;
49 }
50
51 bool MachODecoder::decode(address addr, char *buf,
52 int buflen, int *offset, const void *mach_base) {
53 struct symtab_command * symt = (struct symtab_command *)
54 mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB);
55 if (symt == NULL) {
56 DEBUG_ONLY(tty->print_cr("no symtab in mach file at 0x%lx", mach_base));
57 return false;
58 }
59 uint32_t off = symt->symoff; /* symbol table offset (within this mach file) */
60 uint32_t nsyms = symt->nsyms; /* number of symbol table entries */
61 uint32_t stroff = symt->stroff; /* string table offset */
62 uint32_t strsize = symt->strsize; /* string table size in bytes */
63
64 // iterate through symbol table trying to match our offset
65
66 uint32_t addr_relative = (uintptr_t) mach_base - (uintptr_t) addr; // offset we seek in the symtab
67 void * symtab_addr = (void*) ((uintptr_t) mach_base + off);
68 struct nlist_64 *cur_nlist = (struct nlist_64 *) symtab_addr;
69 struct nlist_64 *last_nlist = cur_nlist; // no size stored in an entry, so keep previously seen nlist
70
71 int32_t found_strx = 0;
72 int32_t found_symval = 0;
73
74 for (uint32_t i=0; i < nsyms; i++) {
75 uint32_t this_value = cur_nlist->n_value;
76
77 if (addr_relative == this_value) {
78 found_strx = cur_nlist->n_un.n_strx;
79 found_symval = this_value;
80 break;
81 } else if (addr_relative > this_value) {
82 // gone past it, use previously seen nlist:
83 found_strx = last_nlist->n_un.n_strx;
84 found_symval = last_nlist->n_value;
85 break;
86 }
87 last_nlist = cur_nlist;
88 cur_nlist = cur_nlist + sizeof(struct nlist_64);
89 }
90 if (found_strx == 0) {
91 return false;
92 }
93 // write the offset:
94 *offset = addr_relative - found_symval;
95
96 // lookup found_strx in the string table
97 char * symname = mach_find_in_stringtable((char*) ((uintptr_t)mach_base + stroff), strsize, found_strx);
98 if (symname) {
99 strncpy(buf, symname, buflen);
100 return true;
101 }
102 DEBUG_ONLY(tty->print_cr("no string or null string found."));
103 return false;
104 }
105
106 void* MachODecoder::mach_find_command(struct mach_header_64 * mach_base, uint32_t command_wanted) {
107 // possibly verify it is a mach_header, use magic number.
108 // commands begin immediately after the header.
109 struct load_command *pos = (struct load_command *) mach_base + sizeof(struct mach_header_64);
110 for (uint32_t i = 0; i < mach_base->ncmds; i++) {
111 struct load_command *this_cmd = (struct load_command *) pos;
112 if (this_cmd->cmd == command_wanted) {
113 return pos;
114 }
115 int cmdsize = this_cmd->cmdsize;
116 pos += cmdsize;
117 }
118 return NULL;
119 }
120
121 char* MachODecoder::mach_find_in_stringtable(char *strtab, uint32_t tablesize, int strx_wanted) {
122
123 if (strx_wanted == 0) {
124 return NULL;
125 }
126 char *strtab_end = strtab + tablesize;
127
128 // find the first string, skip over the space char
129 // (or the four zero bytes we see e.g. in libclient)
130 if (*strtab == ' ') {
131 strtab++;
132 if (*strtab != 0) {
133 DEBUG_ONLY(tty->print_cr("string table has leading space but no following zero."));
134 return NULL;
135 }
136 strtab++;
137 } else {
138 if ((uint32_t) *strtab != 0) {
139 DEBUG_ONLY(tty->print_cr("string table without leading space or leading int of zero."));
140 return NULL;
141 }
142 strtab+=4;
143 }
144 // read the real strings starting at index 1
145 int cur_strx = 1;
146 while (strtab < strtab_end) {
147 if (cur_strx == strx_wanted) {
148 return strtab;
149 }
150 // find start of next string
151 while (*strtab != 0) {
152 strtab++;
153 }
154 strtab++; // skip the terminating zero
155 cur_strx++;
156 }
157 DEBUG_ONLY(tty->print_cr("string number %d not found.", strx_wanted));
158 return NULL;
159 }
160
161
29 #endif 162 #endif
30 163
31 164