Path: blob/master/src/hotspot/share/utilities/elfSymbolTable.cpp
40949 views
/*1* Copyright (c) 1997, 2018, 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* const file, Elf_Shdr& shdr) :33_next(NULL), _fd(file), _section(file, shdr) {34assert(file != NULL, "null file handle");35_status = _section.status();3637if (_section.section_header()->sh_size % sizeof(Elf_Sym) != 0) {38_status = NullDecoder::file_invalid;39}40}4142ElfSymbolTable::~ElfSymbolTable() {43if (_next != NULL) {44delete _next;45}46}4748bool ElfSymbolTable::compare(const Elf_Sym* sym, address addr, int* stringtableIndex, int* posIndex, int* offset, ElfFuncDescTable* funcDescTable) {49if (STT_FUNC == ELF_ST_TYPE(sym->st_info)) {50Elf_Word st_size = sym->st_size;51const Elf_Shdr* shdr = _section.section_header();52address sym_addr;53if (funcDescTable != NULL && funcDescTable->get_index() == sym->st_shndx) {54// We need to go another step trough the function descriptor table (currently PPC64 only)55sym_addr = funcDescTable->lookup(sym->st_value);56} else {57sym_addr = (address)sym->st_value;58}59if (sym_addr <= addr && (Elf_Word)(addr - sym_addr) < st_size) {60*offset = (int)(addr - sym_addr);61*posIndex = sym->st_name;62*stringtableIndex = shdr->sh_link;63return true;64}65}66return false;67}6869bool ElfSymbolTable::lookup(address addr, int* stringtableIndex, int* posIndex, int* offset, ElfFuncDescTable* funcDescTable) {70assert(stringtableIndex, "null string table index pointer");71assert(posIndex, "null string table offset pointer");72assert(offset, "null offset pointer");7374if (NullDecoder::is_error(get_status())) {75return false;76}7778size_t sym_size = sizeof(Elf_Sym);79int count = _section.section_header()->sh_size / sym_size;80Elf_Sym* symbols = (Elf_Sym*)_section.section_data();8182if (symbols != NULL) {83for (int index = 0; index < count; index ++) {84if (compare(&symbols[index], addr, stringtableIndex, posIndex, offset, funcDescTable)) {85return true;86}87}88} else {89MarkedFileReader mfd(_fd);9091if (!mfd.has_mark() || !mfd.set_position(_section.section_header()->sh_offset)) {92_status = NullDecoder::file_invalid;93return false;94}9596Elf_Sym sym;97for (int index = 0; index < count; index ++) {98if (!mfd.read((void*)&sym, sizeof(sym))) {99_status = NullDecoder::file_invalid;100return false;101}102103if (compare(&sym, addr, stringtableIndex, posIndex, offset, funcDescTable)) {104return true;105}106}107}108return false;109}110111#endif // !_WINDOWS && !__APPLE__112113114