src/share/vm/utilities/elfSymbolTable.cpp

Tue, 25 Feb 2014 15:11:18 -0800

author
kvn
date
Tue, 25 Feb 2014 15:11:18 -0800
changeset 6507
752ba2e5f6d0
parent 6491
e7cbc95179c4
child 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

zgu@2364 1 /*
simonis@6491 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
zgu@2364 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
zgu@2364 4 *
zgu@2364 5 * This code is free software; you can redistribute it and/or modify it
zgu@2364 6 * under the terms of the GNU General Public License version 2 only, as
zgu@2364 7 * published by the Free Software Foundation.
zgu@2364 8 *
zgu@2364 9 * This code is distributed in the hope that it will be useful, but WITHOUT
zgu@2364 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
zgu@2364 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
zgu@2364 12 * version 2 for more details (a copy is included in the LICENSE file that
zgu@2364 13 * accompanied this code).
zgu@2364 14 *
zgu@2364 15 * You should have received a copy of the GNU General Public License version
zgu@2364 16 * 2 along with this work; if not, write to the Free Software Foundation,
zgu@2364 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
zgu@2364 18 *
zgu@2364 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
zgu@2364 20 * or visit www.oracle.com if you need additional information or have any
zgu@2364 21 * questions.
zgu@2364 22 *
zgu@2364 23 */
zgu@2364 24
zgu@2364 25 #include "precompiled.hpp"
zgu@2364 26
never@3156 27 #if !defined(_WINDOWS) && !defined(__APPLE__)
zgu@2364 28
zgu@2364 29 #include "memory/allocation.inline.hpp"
simonis@6491 30 #include "utilities/elfFuncDescTable.hpp"
zgu@2364 31 #include "utilities/elfSymbolTable.hpp"
zgu@2364 32
zgu@2364 33 ElfSymbolTable::ElfSymbolTable(FILE* file, Elf_Shdr shdr) {
zgu@2364 34 assert(file, "null file handle");
zgu@2364 35 m_symbols = NULL;
zgu@2364 36 m_next = NULL;
zgu@2364 37 m_file = file;
zgu@3430 38 m_status = NullDecoder::no_error;
zgu@2364 39
zgu@2364 40 // try to load the string table
zgu@2364 41 long cur_offset = ftell(file);
zgu@2364 42 if (cur_offset != -1) {
coleenp@2512 43 // call malloc so we can back up if memory allocation fails.
zgu@3900 44 m_symbols = (Elf_Sym*)os::malloc(shdr.sh_size, mtInternal);
zgu@2364 45 if (m_symbols) {
zgu@2364 46 if (fseek(file, shdr.sh_offset, SEEK_SET) ||
zgu@2364 47 fread((void*)m_symbols, shdr.sh_size, 1, file) != 1 ||
zgu@2364 48 fseek(file, cur_offset, SEEK_SET)) {
zgu@3430 49 m_status = NullDecoder::file_invalid;
coleenp@2512 50 os::free(m_symbols);
zgu@2364 51 m_symbols = NULL;
zgu@2364 52 }
zgu@2364 53 }
zgu@3430 54 if (!NullDecoder::is_error(m_status)) {
zgu@2364 55 memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr));
zgu@2364 56 }
zgu@2364 57 } else {
zgu@3430 58 m_status = NullDecoder::file_invalid;
zgu@2364 59 }
zgu@2364 60 }
zgu@2364 61
zgu@2364 62 ElfSymbolTable::~ElfSymbolTable() {
zgu@2364 63 if (m_symbols != NULL) {
coleenp@2512 64 os::free(m_symbols);
zgu@2364 65 }
zgu@2364 66
zgu@2364 67 if (m_next != NULL) {
zgu@2364 68 delete m_next;
zgu@2364 69 }
zgu@2364 70 }
zgu@2364 71
simonis@6491 72 bool ElfSymbolTable::lookup(address addr, int* stringtableIndex, int* posIndex, int* offset, ElfFuncDescTable* funcDescTable) {
zgu@2364 73 assert(stringtableIndex, "null string table index pointer");
zgu@2364 74 assert(posIndex, "null string table offset pointer");
zgu@2364 75 assert(offset, "null offset pointer");
zgu@2364 76
zgu@3430 77 if (NullDecoder::is_error(m_status)) {
zgu@3430 78 return false;
zgu@2364 79 }
zgu@2364 80
zgu@2364 81 size_t sym_size = sizeof(Elf_Sym);
zgu@2364 82 assert((m_shdr.sh_size % sym_size) == 0, "check size");
zgu@2364 83 int count = m_shdr.sh_size / sym_size;
zgu@2364 84 if (m_symbols != NULL) {
zgu@2364 85 for (int index = 0; index < count; index ++) {
zgu@2364 86 if (STT_FUNC == ELF_ST_TYPE(m_symbols[index].st_info)) {
simonis@6491 87 Elf_Word st_size = m_symbols[index].st_size;
simonis@6491 88 address sym_addr;
simonis@6491 89 if (funcDescTable != NULL && funcDescTable->get_index() == m_symbols[index].st_shndx) {
simonis@6491 90 // We need to go another step trough the function descriptor table (currently PPC64 only)
simonis@6491 91 sym_addr = funcDescTable->lookup(m_symbols[index].st_value);
simonis@6491 92 } else {
simonis@6491 93 sym_addr = (address)m_symbols[index].st_value;
simonis@6491 94 }
simonis@6491 95 if (sym_addr <= addr && (Elf_Word)(addr - sym_addr) < st_size) {
simonis@6491 96 *offset = (int)(addr - sym_addr);
zgu@2364 97 *posIndex = m_symbols[index].st_name;
zgu@2364 98 *stringtableIndex = m_shdr.sh_link;
simonis@6491 99 return true;
zgu@2364 100 }
zgu@2364 101 }
zgu@2364 102 }
zgu@2364 103 } else {
zgu@2364 104 long cur_pos;
zgu@2364 105 if ((cur_pos = ftell(m_file)) == -1 ||
zgu@2364 106 fseek(m_file, m_shdr.sh_offset, SEEK_SET)) {
zgu@3430 107 m_status = NullDecoder::file_invalid;
zgu@3430 108 return false;
zgu@2364 109 }
zgu@2364 110
zgu@2364 111 Elf_Sym sym;
zgu@2364 112 for (int index = 0; index < count; index ++) {
zgu@2364 113 if (fread(&sym, sym_size, 1, m_file) == 1) {
zgu@2364 114 if (STT_FUNC == ELF_ST_TYPE(sym.st_info)) {
simonis@6491 115 Elf_Word st_size = sym.st_size;
simonis@6491 116 address sym_addr;
simonis@6491 117 if (funcDescTable != NULL && funcDescTable->get_index() == sym.st_shndx) {
simonis@6491 118 // We need to go another step trough the function descriptor table (currently PPC64 only)
simonis@6491 119 sym_addr = funcDescTable->lookup(sym.st_value);
simonis@6491 120 } else {
simonis@6491 121 sym_addr = (address)sym.st_value;
simonis@6491 122 }
simonis@6491 123 if (sym_addr <= addr && (Elf_Word)(addr - sym_addr) < st_size) {
simonis@6491 124 *offset = (int)(addr - sym_addr);
zgu@2364 125 *posIndex = sym.st_name;
zgu@2364 126 *stringtableIndex = m_shdr.sh_link;
simonis@6491 127 return true;
zgu@2364 128 }
zgu@2364 129 }
zgu@2364 130 } else {
zgu@3430 131 m_status = NullDecoder::file_invalid;
zgu@3430 132 return false;
zgu@2364 133 }
zgu@2364 134 }
zgu@2364 135 fseek(m_file, cur_pos, SEEK_SET);
zgu@2364 136 }
zgu@3430 137 return true;
zgu@2364 138 }
zgu@2364 139
simonis@6491 140 #endif // !_WINDOWS && !__APPLE__

mercurial