src/os/bsd/vm/decoder_machO.cpp

changeset 3961
3b01d0321dfa
parent 3430
d7e3846464d0
child 4153
b9a9ed0f8eeb
     1.1 --- a/src/os/bsd/vm/decoder_machO.cpp	Thu Jul 19 06:24:46 2012 -0700
     1.2 +++ b/src/os/bsd/vm/decoder_machO.cpp	Mon Jul 30 10:25:52 2012 -0400
     1.3 @@ -26,6 +26,139 @@
     1.4  
     1.5  #ifdef __APPLE__
     1.6  #include "decoder_machO.hpp"
     1.7 +
     1.8 +#include <cxxabi.h>
     1.9 +#include <mach-o/loader.h>
    1.10 +#include <mach-o/nlist.h>
    1.11 +
    1.12 +
    1.13 +bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) {
    1.14 +  int   status;
    1.15 +  char* result;
    1.16 +  size_t size = (size_t)buflen;
    1.17 +  // Don't pass buf to __cxa_demangle. In case of the 'buf' is too small,
    1.18 +  // __cxa_demangle will call system "realloc" for additional memory, which
    1.19 +  // may use different malloc/realloc mechanism that allocates 'buf'.
    1.20 +  if ((result = abi::__cxa_demangle(symbol, NULL, NULL, &status)) != NULL) {
    1.21 +    jio_snprintf(buf, buflen, "%s", result);
    1.22 +      // call c library's free
    1.23 +      ::free(result);
    1.24 +      return true;
    1.25 +  }
    1.26 +  return false;
    1.27 +}
    1.28 +
    1.29 +bool MachODecoder::decode(address addr, char *buf,
    1.30 +      int buflen, int *offset, const void *mach_base) {
    1.31 +  struct symtab_command * symt = (struct symtab_command *)
    1.32 +    mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB);
    1.33 +  if (symt == NULL) {
    1.34 +    DEBUG_ONLY(tty->print_cr("no symtab in mach file at 0x%lx", mach_base));
    1.35 +    return false;
    1.36 +  }
    1.37 +  uint32_t off = symt->symoff;          /* symbol table offset (within this mach file) */
    1.38 +  uint32_t nsyms = symt->nsyms;         /* number of symbol table entries */
    1.39 +  uint32_t stroff = symt->stroff;       /* string table offset */
    1.40 +  uint32_t strsize = symt->strsize;     /* string table size in bytes */
    1.41 +
    1.42 +  // iterate through symbol table trying to match our offset
    1.43 +
    1.44 +  uint32_t addr_relative = (uintptr_t) mach_base - (uintptr_t) addr; // offset we seek in the symtab
    1.45 +  void * symtab_addr = (void*) ((uintptr_t) mach_base + off);
    1.46 +  struct nlist_64 *cur_nlist = (struct nlist_64 *) symtab_addr;
    1.47 +  struct nlist_64 *last_nlist = cur_nlist;  // no size stored in an entry, so keep previously seen nlist
    1.48 +
    1.49 +  int32_t found_strx = 0;
    1.50 +  int32_t found_symval = 0;
    1.51 +
    1.52 +  for (uint32_t i=0; i < nsyms; i++) {
    1.53 +    uint32_t this_value = cur_nlist->n_value;
    1.54 +
    1.55 +    if (addr_relative == this_value) {
    1.56 +      found_strx =  cur_nlist->n_un.n_strx;
    1.57 +      found_symval = this_value;
    1.58 +      break;
    1.59 +    } else if (addr_relative > this_value) {
    1.60 +      // gone past it, use previously seen nlist:
    1.61 +      found_strx = last_nlist->n_un.n_strx;
    1.62 +      found_symval = last_nlist->n_value;
    1.63 +      break;
    1.64 +    }
    1.65 +    last_nlist = cur_nlist;
    1.66 +    cur_nlist = cur_nlist + sizeof(struct nlist_64);
    1.67 +  }
    1.68 +  if (found_strx == 0) {
    1.69 +    return false;
    1.70 +  }
    1.71 +  // write the offset:
    1.72 +  *offset = addr_relative - found_symval;
    1.73 +
    1.74 +  // lookup found_strx in the string table
    1.75 +  char * symname = mach_find_in_stringtable((char*) ((uintptr_t)mach_base + stroff), strsize, found_strx);
    1.76 +  if (symname) {
    1.77 +      strncpy(buf, symname, buflen);
    1.78 +      return true;
    1.79 +  }
    1.80 +  DEBUG_ONLY(tty->print_cr("no string or null string found."));
    1.81 +  return false;
    1.82 +}
    1.83 +
    1.84 +void* MachODecoder::mach_find_command(struct mach_header_64 * mach_base, uint32_t command_wanted) {
    1.85 +  // possibly verify it is a mach_header, use magic number.
    1.86 +  // commands begin immediately after the header.
    1.87 +  struct load_command *pos = (struct load_command *) mach_base + sizeof(struct mach_header_64);
    1.88 +  for (uint32_t i = 0; i < mach_base->ncmds; i++) {
    1.89 +    struct load_command *this_cmd = (struct load_command *) pos;
    1.90 +    if (this_cmd->cmd == command_wanted) {
    1.91 +       return pos;
    1.92 +    }
    1.93 +    int cmdsize = this_cmd->cmdsize;
    1.94 +    pos += cmdsize;
    1.95 +  }
    1.96 +  return NULL;
    1.97 +}
    1.98 +
    1.99 +char* MachODecoder::mach_find_in_stringtable(char *strtab, uint32_t tablesize, int strx_wanted) {
   1.100 +
   1.101 +  if (strx_wanted == 0) {
   1.102 +    return NULL;
   1.103 +  }
   1.104 +  char *strtab_end = strtab + tablesize;
   1.105 +
   1.106 +  // find the first string, skip over the space char
   1.107 +  // (or the four zero bytes we see e.g. in libclient)
   1.108 +  if (*strtab == ' ') {
   1.109 +      strtab++;
   1.110 +      if (*strtab != 0) {
   1.111 +          DEBUG_ONLY(tty->print_cr("string table has leading space but no following zero."));
   1.112 +          return NULL;
   1.113 +      }
   1.114 +      strtab++;
   1.115 +  } else {
   1.116 +      if ((uint32_t) *strtab != 0) {
   1.117 +          DEBUG_ONLY(tty->print_cr("string table without leading space or leading int of zero."));
   1.118 +          return NULL;
   1.119 +      }
   1.120 +      strtab+=4;
   1.121 +  }
   1.122 +  // read the real strings starting at index 1
   1.123 +  int cur_strx = 1;
   1.124 +  while (strtab < strtab_end) {
   1.125 +    if (cur_strx == strx_wanted) {
   1.126 +        return strtab;
   1.127 +    }
   1.128 +    // find start of next string
   1.129 +    while (*strtab != 0) {
   1.130 +        strtab++;
   1.131 +    }
   1.132 +    strtab++; // skip the terminating zero
   1.133 +    cur_strx++;
   1.134 +  }
   1.135 +  DEBUG_ONLY(tty->print_cr("string number %d not found.", strx_wanted));
   1.136 +  return NULL;
   1.137 +}
   1.138 +
   1.139 +
   1.140  #endif
   1.141  
   1.142  

mercurial