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