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