jdk

Форк
0
/
decoder_machO.cpp 
171 строка · 5.7 Кб
1
/*
2
 * Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
3
 * 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__
28
#include "decoder_machO.hpp"
29
#include "jvm.h"
30
#include "memory/allocation.inline.hpp"
31

32
#include <cxxabi.h>
33
#include <mach-o/loader.h>
34
#include <mach-o/nlist.h>
35

36

37
bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) {
38
  int   status;
39
  char* result;
40
  size_t size = (size_t)buflen;
41
  // Don't pass buf to __cxa_demangle. In case of the 'buf' is too small,
42
  // __cxa_demangle will call system "realloc" for additional memory, which
43
  // may use different malloc/realloc mechanism that allocates 'buf'.
44
  if ((result = abi::__cxa_demangle(symbol, nullptr, nullptr, &status)) != nullptr) {
45
    jio_snprintf(buf, buflen, "%s", result);
46
      // call c library's free
47
      ::free(result);
48
      return true;
49
  }
50
  return false;
51
}
52

53
bool MachODecoder::decode(address addr, char *buf,
54
      int buflen, int *offset, const void *mach_base) {
55
  if (addr == (address)(intptr_t)-1) {
56
    // dladdr() in macOS12/Monterey returns success for -1, but that addr value
57
    // won't work in this function. Should have been handled by the caller.
58
    ShouldNotReachHere();
59
    return false;
60
  }
61
  struct symtab_command * symt = (struct symtab_command *)
62
    mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB);
63
  if (symt == nullptr) {
64
    DEBUG_ONLY(tty->print_cr("no symtab in mach file at 0x%lx", p2i(mach_base)));
65
    return false;
66
  }
67
  uint32_t off = symt->symoff;          /* symbol table offset (within this mach file) */
68
  uint32_t nsyms = symt->nsyms;         /* number of symbol table entries */
69
  uint32_t stroff = symt->stroff;       /* string table offset */
70
  uint32_t strsize = symt->strsize;     /* string table size in bytes */
71

72
  // iterate through symbol table trying to match our offset
73

74
  uint32_t addr_relative = (uintptr_t) mach_base - (uintptr_t) addr; // offset we seek in the symtab
75
  void * symtab_addr = (void*) ((uintptr_t) mach_base + off);
76
  struct nlist_64 *cur_nlist = (struct nlist_64 *) symtab_addr;
77
  struct nlist_64 *last_nlist = cur_nlist;  // no size stored in an entry, so keep previously seen nlist
78

79
  int32_t found_strx = 0;
80
  int32_t found_symval = 0;
81

82
  for (uint32_t i=0; i < nsyms; i++) {
83
    uint32_t this_value = cur_nlist->n_value;
84

85
    if (addr_relative == this_value) {
86
      found_strx =  cur_nlist->n_un.n_strx;
87
      found_symval = this_value;
88
      break;
89
    } else if (addr_relative > this_value) {
90
      // gone past it, use previously seen nlist:
91
      found_strx = last_nlist->n_un.n_strx;
92
      found_symval = last_nlist->n_value;
93
      break;
94
    }
95
    last_nlist = cur_nlist;
96
    cur_nlist = cur_nlist + sizeof(struct nlist_64);
97
  }
98
  if (found_strx == 0) {
99
    return false;
100
  }
101
  // write the offset:
102
  *offset = addr_relative - found_symval;
103

104
  // lookup found_strx in the string table
105
  char * symname = mach_find_in_stringtable((char*) ((uintptr_t)mach_base + stroff), strsize, found_strx);
106
  if (symname) {
107
      strncpy(buf, symname, buflen);
108
      buf[buflen - 1] = '\0';
109
      return true;
110
  }
111
  DEBUG_ONLY(tty->print_cr("no string or null string found."));
112
  return false;
113
}
114

115
void* MachODecoder::mach_find_command(struct mach_header_64 * mach_base, uint32_t command_wanted) {
116
  // possibly verify it is a mach_header, use magic number.
117
  // commands begin immediately after the header.
118
  struct load_command *pos = (struct load_command *) mach_base + sizeof(struct mach_header_64);
119
  for (uint32_t i = 0; i < mach_base->ncmds; i++) {
120
    struct load_command *this_cmd = (struct load_command *) pos;
121
    if (this_cmd->cmd == command_wanted) {
122
       return pos;
123
    }
124
    int cmdsize = this_cmd->cmdsize;
125
    pos += cmdsize;
126
  }
127
  return nullptr;
128
}
129

130
char* MachODecoder::mach_find_in_stringtable(char *strtab, uint32_t tablesize, int strx_wanted) {
131

132
  if (strx_wanted == 0) {
133
    return nullptr;
134
  }
135
  char *strtab_end = strtab + tablesize;
136

137
  // find the first string, skip over the space char
138
  // (or the four zero bytes we see e.g. in libclient)
139
  if (*strtab == ' ') {
140
      strtab++;
141
      if (*strtab != 0) {
142
          DEBUG_ONLY(tty->print_cr("string table has leading space but no following zero."));
143
          return nullptr;
144
      }
145
      strtab++;
146
  } else {
147
      if ((uint32_t) *strtab != 0) {
148
          DEBUG_ONLY(tty->print_cr("string table without leading space or leading int of zero."));
149
          return nullptr;
150
      }
151
      strtab+=4;
152
  }
153
  // read the real strings starting at index 1
154
  int cur_strx = 1;
155
  while (strtab < strtab_end) {
156
    if (cur_strx == strx_wanted) {
157
        return strtab;
158
    }
159
    // find start of next string
160
    while (*strtab != 0) {
161
        strtab++;
162
    }
163
    strtab++; // skip the terminating zero
164
    cur_strx++;
165
  }
166
  DEBUG_ONLY(tty->print_cr("string number %d not found.", strx_wanted));
167
  return nullptr;
168
}
169

170

171
#endif
172

173

174

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.