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