aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: aoqi@0: #if !defined(_WINDOWS) && !defined(__APPLE__) aoqi@0: aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "utilities/elfFuncDescTable.hpp" aoqi@0: #include "utilities/elfSymbolTable.hpp" aoqi@0: aoqi@0: ElfSymbolTable::ElfSymbolTable(FILE* file, Elf_Shdr shdr) { aoqi@0: assert(file, "null file handle"); aoqi@0: m_symbols = NULL; aoqi@0: m_next = NULL; aoqi@0: m_file = file; aoqi@0: m_status = NullDecoder::no_error; aoqi@0: aoqi@0: // try to load the string table aoqi@0: long cur_offset = ftell(file); aoqi@0: if (cur_offset != -1) { aoqi@0: // call malloc so we can back up if memory allocation fails. aoqi@0: m_symbols = (Elf_Sym*)os::malloc(shdr.sh_size, mtInternal); aoqi@0: if (m_symbols) { aoqi@0: if (fseek(file, shdr.sh_offset, SEEK_SET) || aoqi@0: fread((void*)m_symbols, shdr.sh_size, 1, file) != 1 || aoqi@0: fseek(file, cur_offset, SEEK_SET)) { aoqi@0: m_status = NullDecoder::file_invalid; aoqi@0: os::free(m_symbols); aoqi@0: m_symbols = NULL; aoqi@0: } aoqi@0: } aoqi@0: if (!NullDecoder::is_error(m_status)) { aoqi@0: memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr)); aoqi@0: } aoqi@0: } else { aoqi@0: m_status = NullDecoder::file_invalid; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ElfSymbolTable::~ElfSymbolTable() { aoqi@0: if (m_symbols != NULL) { aoqi@0: os::free(m_symbols); aoqi@0: } aoqi@0: aoqi@0: if (m_next != NULL) { aoqi@0: delete m_next; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool ElfSymbolTable::lookup(address addr, int* stringtableIndex, int* posIndex, int* offset, ElfFuncDescTable* funcDescTable) { aoqi@0: assert(stringtableIndex, "null string table index pointer"); aoqi@0: assert(posIndex, "null string table offset pointer"); aoqi@0: assert(offset, "null offset pointer"); aoqi@0: aoqi@0: if (NullDecoder::is_error(m_status)) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: size_t sym_size = sizeof(Elf_Sym); aoqi@0: assert((m_shdr.sh_size % sym_size) == 0, "check size"); aoqi@0: int count = m_shdr.sh_size / sym_size; aoqi@0: if (m_symbols != NULL) { aoqi@0: for (int index = 0; index < count; index ++) { aoqi@0: if (STT_FUNC == ELF_ST_TYPE(m_symbols[index].st_info)) { aoqi@0: Elf_Word st_size = m_symbols[index].st_size; aoqi@0: address sym_addr; aoqi@0: if (funcDescTable != NULL && funcDescTable->get_index() == m_symbols[index].st_shndx) { aoqi@0: // We need to go another step trough the function descriptor table (currently PPC64 only) aoqi@0: sym_addr = funcDescTable->lookup(m_symbols[index].st_value); aoqi@0: } else { aoqi@0: sym_addr = (address)m_symbols[index].st_value; aoqi@0: } aoqi@0: if (sym_addr <= addr && (Elf_Word)(addr - sym_addr) < st_size) { aoqi@0: *offset = (int)(addr - sym_addr); aoqi@0: *posIndex = m_symbols[index].st_name; aoqi@0: *stringtableIndex = m_shdr.sh_link; aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: long cur_pos; aoqi@0: if ((cur_pos = ftell(m_file)) == -1 || aoqi@0: fseek(m_file, m_shdr.sh_offset, SEEK_SET)) { aoqi@0: m_status = NullDecoder::file_invalid; aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: Elf_Sym sym; aoqi@0: for (int index = 0; index < count; index ++) { aoqi@0: if (fread(&sym, sym_size, 1, m_file) == 1) { aoqi@0: if (STT_FUNC == ELF_ST_TYPE(sym.st_info)) { aoqi@0: Elf_Word st_size = sym.st_size; aoqi@0: address sym_addr; aoqi@0: if (funcDescTable != NULL && funcDescTable->get_index() == sym.st_shndx) { aoqi@0: // We need to go another step trough the function descriptor table (currently PPC64 only) aoqi@0: sym_addr = funcDescTable->lookup(sym.st_value); aoqi@0: } else { aoqi@0: sym_addr = (address)sym.st_value; aoqi@0: } aoqi@0: if (sym_addr <= addr && (Elf_Word)(addr - sym_addr) < st_size) { aoqi@0: *offset = (int)(addr - sym_addr); aoqi@0: *posIndex = sym.st_name; aoqi@0: *stringtableIndex = m_shdr.sh_link; aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: m_status = NullDecoder::file_invalid; aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: fseek(m_file, cur_pos, SEEK_SET); aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: #endif // !_WINDOWS && !__APPLE__