src/share/vm/utilities/elfSymbolTable.cpp

changeset 2364
2d4762ec74af
child 2512
d28def44457d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/elfSymbolTable.cpp	Sat Dec 11 13:20:56 2010 -0500
     1.3 @@ -0,0 +1,125 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +
    1.30 +#ifndef _WINDOWS
    1.31 +
    1.32 +#include "memory/allocation.inline.hpp"
    1.33 +#include "utilities/elfSymbolTable.hpp"
    1.34 +
    1.35 +ElfSymbolTable::ElfSymbolTable(FILE* file, Elf_Shdr shdr) {
    1.36 +  assert(file, "null file handle");
    1.37 +  m_symbols = NULL;
    1.38 +  m_next = NULL;
    1.39 +  m_file = file;
    1.40 +  m_status = Decoder::no_error;
    1.41 +
    1.42 +  // try to load the string table
    1.43 +  long cur_offset = ftell(file);
    1.44 +  if (cur_offset != -1) {
    1.45 +    m_symbols = (Elf_Sym*)NEW_C_HEAP_ARRAY(char, shdr.sh_size);
    1.46 +    if (m_symbols) {
    1.47 +      if (fseek(file, shdr.sh_offset, SEEK_SET) ||
    1.48 +        fread((void*)m_symbols, shdr.sh_size, 1, file) != 1 ||
    1.49 +        fseek(file, cur_offset, SEEK_SET)) {
    1.50 +        m_status = Decoder::file_invalid;
    1.51 +        FREE_C_HEAP_ARRAY(char, m_symbols);
    1.52 +        m_symbols = NULL;
    1.53 +      }
    1.54 +    }
    1.55 +    if (m_status == Decoder::no_error) {
    1.56 +      memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr));
    1.57 +    }
    1.58 +  } else {
    1.59 +    m_status = Decoder::file_invalid;
    1.60 +  }
    1.61 +}
    1.62 +
    1.63 +ElfSymbolTable::~ElfSymbolTable() {
    1.64 +  if (m_symbols != NULL) {
    1.65 +    FREE_C_HEAP_ARRAY(char, m_symbols);
    1.66 +  }
    1.67 +
    1.68 +  if (m_next != NULL) {
    1.69 +    delete m_next;
    1.70 +  }
    1.71 +}
    1.72 +
    1.73 +Decoder::decoder_status ElfSymbolTable::lookup(address addr, int* stringtableIndex, int* posIndex, int* offset) {
    1.74 +  assert(stringtableIndex, "null string table index pointer");
    1.75 +  assert(posIndex, "null string table offset pointer");
    1.76 +  assert(offset, "null offset pointer");
    1.77 +
    1.78 +  if (m_status != Decoder::no_error) {
    1.79 +    return m_status;
    1.80 +  }
    1.81 +
    1.82 +  address pc = 0;
    1.83 +  size_t  sym_size = sizeof(Elf_Sym);
    1.84 +  assert((m_shdr.sh_size % sym_size) == 0, "check size");
    1.85 +  int count = m_shdr.sh_size / sym_size;
    1.86 +  if (m_symbols != NULL) {
    1.87 +    for (int index = 0; index < count; index ++) {
    1.88 +      if (STT_FUNC == ELF_ST_TYPE(m_symbols[index].st_info)) {
    1.89 +        address sym_addr = (address)m_symbols[index].st_value;
    1.90 +        if (sym_addr < addr && (addr - sym_addr) < *offset) {
    1.91 +          pc = (address)m_symbols[index].st_value;
    1.92 +          *offset = (int)(addr - pc);
    1.93 +          *posIndex = m_symbols[index].st_name;
    1.94 +          *stringtableIndex = m_shdr.sh_link;
    1.95 +        }
    1.96 +      }
    1.97 +    }
    1.98 +  } else {
    1.99 +    long cur_pos;
   1.100 +    if ((cur_pos = ftell(m_file)) == -1 ||
   1.101 +      fseek(m_file, m_shdr.sh_offset, SEEK_SET)) {
   1.102 +      m_status = Decoder::file_invalid;
   1.103 +      return m_status;
   1.104 +    }
   1.105 +
   1.106 +    Elf_Sym sym;
   1.107 +    for (int index = 0; index < count; index ++) {
   1.108 +      if (fread(&sym, sym_size, 1, m_file) == 1) {
   1.109 +        if (STT_FUNC == ELF_ST_TYPE(sym.st_info)) {
   1.110 +          address sym_addr = (address)sym.st_value;
   1.111 +          if (sym_addr < addr && (addr - sym_addr) < *offset) {
   1.112 +            pc = (address)sym.st_value;
   1.113 +            *offset = (int)(addr - pc);
   1.114 +            *posIndex = sym.st_name;
   1.115 +            *stringtableIndex = m_shdr.sh_link;
   1.116 +          }
   1.117 +        }
   1.118 +      } else {
   1.119 +        m_status = Decoder::file_invalid;
   1.120 +        return m_status;
   1.121 +      }
   1.122 +    }
   1.123 +    fseek(m_file, cur_pos, SEEK_SET);
   1.124 +  }
   1.125 +  return m_status;
   1.126 +}
   1.127 +
   1.128 +#endif // _WINDOWS

mercurial