Path: blob/master/src/hotspot/os/bsd/decoder_machO.cpp
64440 views
/*1* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"2526#ifdef __APPLE__27#include "jvm.h"28#include "decoder_machO.hpp"29#include "memory/allocation.inline.hpp"3031#include <cxxabi.h>32#include <mach-o/loader.h>33#include <mach-o/nlist.h>343536bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) {37int status;38char* result;39size_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, which42// may use different malloc/realloc mechanism that allocates 'buf'.43if ((result = abi::__cxa_demangle(symbol, NULL, NULL, &status)) != NULL) {44jio_snprintf(buf, buflen, "%s", result);45// call c library's free46::free(result);47return true;48}49return false;50}5152bool MachODecoder::decode(address addr, char *buf,53int buflen, int *offset, const void *mach_base) {54if (addr == (address)(intptr_t)-1) {55// dladdr() in macOS12/Monterey returns success for -1, but that addr value56// won't work in this function. Should have been handled by the caller.57ShouldNotReachHere();58return false;59}60struct symtab_command * symt = (struct symtab_command *)61mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB);62if (symt == NULL) {63DEBUG_ONLY(tty->print_cr("no symtab in mach file at 0x%lx", p2i(mach_base)));64return false;65}66uint32_t off = symt->symoff; /* symbol table offset (within this mach file) */67uint32_t nsyms = symt->nsyms; /* number of symbol table entries */68uint32_t stroff = symt->stroff; /* string table offset */69uint32_t strsize = symt->strsize; /* string table size in bytes */7071// iterate through symbol table trying to match our offset7273uint32_t addr_relative = (uintptr_t) mach_base - (uintptr_t) addr; // offset we seek in the symtab74void * symtab_addr = (void*) ((uintptr_t) mach_base + off);75struct nlist_64 *cur_nlist = (struct nlist_64 *) symtab_addr;76struct nlist_64 *last_nlist = cur_nlist; // no size stored in an entry, so keep previously seen nlist7778int32_t found_strx = 0;79int32_t found_symval = 0;8081for (uint32_t i=0; i < nsyms; i++) {82uint32_t this_value = cur_nlist->n_value;8384if (addr_relative == this_value) {85found_strx = cur_nlist->n_un.n_strx;86found_symval = this_value;87break;88} else if (addr_relative > this_value) {89// gone past it, use previously seen nlist:90found_strx = last_nlist->n_un.n_strx;91found_symval = last_nlist->n_value;92break;93}94last_nlist = cur_nlist;95cur_nlist = cur_nlist + sizeof(struct nlist_64);96}97if (found_strx == 0) {98return false;99}100// write the offset:101*offset = addr_relative - found_symval;102103// lookup found_strx in the string table104char * symname = mach_find_in_stringtable((char*) ((uintptr_t)mach_base + stroff), strsize, found_strx);105if (symname) {106strncpy(buf, symname, buflen);107buf[buflen - 1] = '\0';108return true;109}110DEBUG_ONLY(tty->print_cr("no string or null string found."));111return false;112}113114void* MachODecoder::mach_find_command(struct mach_header_64 * mach_base, uint32_t command_wanted) {115// possibly verify it is a mach_header, use magic number.116// commands begin immediately after the header.117struct load_command *pos = (struct load_command *) mach_base + sizeof(struct mach_header_64);118for (uint32_t i = 0; i < mach_base->ncmds; i++) {119struct load_command *this_cmd = (struct load_command *) pos;120if (this_cmd->cmd == command_wanted) {121return pos;122}123int cmdsize = this_cmd->cmdsize;124pos += cmdsize;125}126return NULL;127}128129char* MachODecoder::mach_find_in_stringtable(char *strtab, uint32_t tablesize, int strx_wanted) {130131if (strx_wanted == 0) {132return NULL;133}134char *strtab_end = strtab + tablesize;135136// find the first string, skip over the space char137// (or the four zero bytes we see e.g. in libclient)138if (*strtab == ' ') {139strtab++;140if (*strtab != 0) {141DEBUG_ONLY(tty->print_cr("string table has leading space but no following zero."));142return NULL;143}144strtab++;145} else {146if ((uint32_t) *strtab != 0) {147DEBUG_ONLY(tty->print_cr("string table without leading space or leading int of zero."));148return NULL;149}150strtab+=4;151}152// read the real strings starting at index 1153int cur_strx = 1;154while (strtab < strtab_end) {155if (cur_strx == strx_wanted) {156return strtab;157}158// find start of next string159while (*strtab != 0) {160strtab++;161}162strtab++; // skip the terminating zero163cur_strx++;164}165DEBUG_ONLY(tty->print_cr("string number %d not found.", strx_wanted));166return NULL;167}168169170#endif171172173174175