1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/utilities/elfSymbolTable.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,140 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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 +#if !defined(_WINDOWS) && !defined(__APPLE__) 1.31 + 1.32 +#include "memory/allocation.inline.hpp" 1.33 +#include "utilities/elfFuncDescTable.hpp" 1.34 +#include "utilities/elfSymbolTable.hpp" 1.35 + 1.36 +ElfSymbolTable::ElfSymbolTable(FILE* file, Elf_Shdr shdr) { 1.37 + assert(file, "null file handle"); 1.38 + m_symbols = NULL; 1.39 + m_next = NULL; 1.40 + m_file = file; 1.41 + m_status = NullDecoder::no_error; 1.42 + 1.43 + // try to load the string table 1.44 + long cur_offset = ftell(file); 1.45 + if (cur_offset != -1) { 1.46 + // call malloc so we can back up if memory allocation fails. 1.47 + m_symbols = (Elf_Sym*)os::malloc(shdr.sh_size, mtInternal); 1.48 + if (m_symbols) { 1.49 + if (fseek(file, shdr.sh_offset, SEEK_SET) || 1.50 + fread((void*)m_symbols, shdr.sh_size, 1, file) != 1 || 1.51 + fseek(file, cur_offset, SEEK_SET)) { 1.52 + m_status = NullDecoder::file_invalid; 1.53 + os::free(m_symbols); 1.54 + m_symbols = NULL; 1.55 + } 1.56 + } 1.57 + if (!NullDecoder::is_error(m_status)) { 1.58 + memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr)); 1.59 + } 1.60 + } else { 1.61 + m_status = NullDecoder::file_invalid; 1.62 + } 1.63 +} 1.64 + 1.65 +ElfSymbolTable::~ElfSymbolTable() { 1.66 + if (m_symbols != NULL) { 1.67 + os::free(m_symbols); 1.68 + } 1.69 + 1.70 + if (m_next != NULL) { 1.71 + delete m_next; 1.72 + } 1.73 +} 1.74 + 1.75 +bool ElfSymbolTable::lookup(address addr, int* stringtableIndex, int* posIndex, int* offset, ElfFuncDescTable* funcDescTable) { 1.76 + assert(stringtableIndex, "null string table index pointer"); 1.77 + assert(posIndex, "null string table offset pointer"); 1.78 + assert(offset, "null offset pointer"); 1.79 + 1.80 + if (NullDecoder::is_error(m_status)) { 1.81 + return false; 1.82 + } 1.83 + 1.84 + size_t sym_size = sizeof(Elf_Sym); 1.85 + assert((m_shdr.sh_size % sym_size) == 0, "check size"); 1.86 + int count = m_shdr.sh_size / sym_size; 1.87 + if (m_symbols != NULL) { 1.88 + for (int index = 0; index < count; index ++) { 1.89 + if (STT_FUNC == ELF_ST_TYPE(m_symbols[index].st_info)) { 1.90 + Elf_Word st_size = m_symbols[index].st_size; 1.91 + address sym_addr; 1.92 + if (funcDescTable != NULL && funcDescTable->get_index() == m_symbols[index].st_shndx) { 1.93 + // We need to go another step trough the function descriptor table (currently PPC64 only) 1.94 + sym_addr = funcDescTable->lookup(m_symbols[index].st_value); 1.95 + } else { 1.96 + sym_addr = (address)m_symbols[index].st_value; 1.97 + } 1.98 + if (sym_addr <= addr && (Elf_Word)(addr - sym_addr) < st_size) { 1.99 + *offset = (int)(addr - sym_addr); 1.100 + *posIndex = m_symbols[index].st_name; 1.101 + *stringtableIndex = m_shdr.sh_link; 1.102 + return true; 1.103 + } 1.104 + } 1.105 + } 1.106 + } else { 1.107 + long cur_pos; 1.108 + if ((cur_pos = ftell(m_file)) == -1 || 1.109 + fseek(m_file, m_shdr.sh_offset, SEEK_SET)) { 1.110 + m_status = NullDecoder::file_invalid; 1.111 + return false; 1.112 + } 1.113 + 1.114 + Elf_Sym sym; 1.115 + for (int index = 0; index < count; index ++) { 1.116 + if (fread(&sym, sym_size, 1, m_file) == 1) { 1.117 + if (STT_FUNC == ELF_ST_TYPE(sym.st_info)) { 1.118 + Elf_Word st_size = sym.st_size; 1.119 + address sym_addr; 1.120 + if (funcDescTable != NULL && funcDescTable->get_index() == sym.st_shndx) { 1.121 + // We need to go another step trough the function descriptor table (currently PPC64 only) 1.122 + sym_addr = funcDescTable->lookup(sym.st_value); 1.123 + } else { 1.124 + sym_addr = (address)sym.st_value; 1.125 + } 1.126 + if (sym_addr <= addr && (Elf_Word)(addr - sym_addr) < st_size) { 1.127 + *offset = (int)(addr - sym_addr); 1.128 + *posIndex = sym.st_name; 1.129 + *stringtableIndex = m_shdr.sh_link; 1.130 + return true; 1.131 + } 1.132 + } 1.133 + } else { 1.134 + m_status = NullDecoder::file_invalid; 1.135 + return false; 1.136 + } 1.137 + } 1.138 + fseek(m_file, cur_pos, SEEK_SET); 1.139 + } 1.140 + return true; 1.141 +} 1.142 + 1.143 +#endif // !_WINDOWS && !__APPLE__