src/share/vm/utilities/elfSymbolTable.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

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

mercurial