Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/utilities/elfSymbolTable.cpp
32285 views
/*1* Copyright (c) 1997, 2013, 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#if !defined(_WINDOWS) && !defined(__APPLE__)2728#include "memory/allocation.inline.hpp"29#include "utilities/elfFuncDescTable.hpp"30#include "utilities/elfSymbolTable.hpp"3132ElfSymbolTable::ElfSymbolTable(FILE* file, Elf_Shdr shdr) {33assert(file, "null file handle");34m_symbols = NULL;35m_next = NULL;36m_file = file;37m_status = NullDecoder::no_error;3839// try to load the string table40long cur_offset = ftell(file);41if (cur_offset != -1) {42// call malloc so we can back up if memory allocation fails.43m_symbols = (Elf_Sym*)os::malloc(shdr.sh_size, mtInternal);44if (m_symbols) {45if (fseek(file, shdr.sh_offset, SEEK_SET) ||46fread((void*)m_symbols, shdr.sh_size, 1, file) != 1 ||47fseek(file, cur_offset, SEEK_SET)) {48m_status = NullDecoder::file_invalid;49os::free(m_symbols);50m_symbols = NULL;51}52}53if (!NullDecoder::is_error(m_status)) {54memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr));55}56} else {57m_status = NullDecoder::file_invalid;58}59}6061ElfSymbolTable::~ElfSymbolTable() {62if (m_symbols != NULL) {63os::free(m_symbols);64}6566if (m_next != NULL) {67delete m_next;68}69}7071bool ElfSymbolTable::lookup(address addr, int* stringtableIndex, int* posIndex, int* offset, ElfFuncDescTable* funcDescTable) {72assert(stringtableIndex, "null string table index pointer");73assert(posIndex, "null string table offset pointer");74assert(offset, "null offset pointer");7576if (NullDecoder::is_error(m_status)) {77return false;78}7980size_t sym_size = sizeof(Elf_Sym);81assert((m_shdr.sh_size % sym_size) == 0, "check size");82int count = m_shdr.sh_size / sym_size;83if (m_symbols != NULL) {84for (int index = 0; index < count; index ++) {85if (STT_FUNC == ELF_ST_TYPE(m_symbols[index].st_info)) {86Elf_Word st_size = m_symbols[index].st_size;87address sym_addr;88if (funcDescTable != NULL && funcDescTable->get_index() == m_symbols[index].st_shndx) {89// We need to go another step trough the function descriptor table (currently PPC64 only)90sym_addr = funcDescTable->lookup(m_symbols[index].st_value);91} else {92sym_addr = (address)m_symbols[index].st_value;93}94if (sym_addr <= addr && (Elf_Word)(addr - sym_addr) < st_size) {95*offset = (int)(addr - sym_addr);96*posIndex = m_symbols[index].st_name;97*stringtableIndex = m_shdr.sh_link;98return true;99}100}101}102} else {103long cur_pos;104if ((cur_pos = ftell(m_file)) == -1 ||105fseek(m_file, m_shdr.sh_offset, SEEK_SET)) {106m_status = NullDecoder::file_invalid;107return false;108}109110Elf_Sym sym;111for (int index = 0; index < count; index ++) {112if (fread(&sym, sym_size, 1, m_file) == 1) {113if (STT_FUNC == ELF_ST_TYPE(sym.st_info)) {114Elf_Word st_size = sym.st_size;115address sym_addr;116if (funcDescTable != NULL && funcDescTable->get_index() == sym.st_shndx) {117// We need to go another step trough the function descriptor table (currently PPC64 only)118sym_addr = funcDescTable->lookup(sym.st_value);119} else {120sym_addr = (address)sym.st_value;121}122if (sym_addr <= addr && (Elf_Word)(addr - sym_addr) < st_size) {123*offset = (int)(addr - sym_addr);124*posIndex = sym.st_name;125*stringtableIndex = m_shdr.sh_link;126return true;127}128}129} else {130m_status = NullDecoder::file_invalid;131return false;132}133}134fseek(m_file, cur_pos, SEEK_SET);135}136return true;137}138139#endif // !_WINDOWS && !__APPLE__140141142