elfSymbolTable.cpp revision 1929:2d4762ec74af
1/*
2 * Copyright (c) 1997, 2010, 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#ifndef _WINDOWS
28
29#include "memory/allocation.inline.hpp"
30#include "utilities/elfSymbolTable.hpp"
31
32ElfSymbolTable::ElfSymbolTable(FILE* file, Elf_Shdr shdr) {
33  assert(file, "null file handle");
34  m_symbols = NULL;
35  m_next = NULL;
36  m_file = file;
37  m_status = Decoder::no_error;
38
39  // try to load the string table
40  long cur_offset = ftell(file);
41  if (cur_offset != -1) {
42    m_symbols = (Elf_Sym*)NEW_C_HEAP_ARRAY(char, shdr.sh_size);
43    if (m_symbols) {
44      if (fseek(file, shdr.sh_offset, SEEK_SET) ||
45        fread((void*)m_symbols, shdr.sh_size, 1, file) != 1 ||
46        fseek(file, cur_offset, SEEK_SET)) {
47        m_status = Decoder::file_invalid;
48        FREE_C_HEAP_ARRAY(char, m_symbols);
49        m_symbols = NULL;
50      }
51    }
52    if (m_status == Decoder::no_error) {
53      memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr));
54    }
55  } else {
56    m_status = Decoder::file_invalid;
57  }
58}
59
60ElfSymbolTable::~ElfSymbolTable() {
61  if (m_symbols != NULL) {
62    FREE_C_HEAP_ARRAY(char, m_symbols);
63  }
64
65  if (m_next != NULL) {
66    delete m_next;
67  }
68}
69
70Decoder::decoder_status ElfSymbolTable::lookup(address addr, int* stringtableIndex, int* posIndex, int* offset) {
71  assert(stringtableIndex, "null string table index pointer");
72  assert(posIndex, "null string table offset pointer");
73  assert(offset, "null offset pointer");
74
75  if (m_status != Decoder::no_error) {
76    return m_status;
77  }
78
79  address pc = 0;
80  size_t  sym_size = sizeof(Elf_Sym);
81  assert((m_shdr.sh_size % sym_size) == 0, "check size");
82  int count = m_shdr.sh_size / sym_size;
83  if (m_symbols != NULL) {
84    for (int index = 0; index < count; index ++) {
85      if (STT_FUNC == ELF_ST_TYPE(m_symbols[index].st_info)) {
86        address sym_addr = (address)m_symbols[index].st_value;
87        if (sym_addr < addr && (addr - sym_addr) < *offset) {
88          pc = (address)m_symbols[index].st_value;
89          *offset = (int)(addr - pc);
90          *posIndex = m_symbols[index].st_name;
91          *stringtableIndex = m_shdr.sh_link;
92        }
93      }
94    }
95  } else {
96    long cur_pos;
97    if ((cur_pos = ftell(m_file)) == -1 ||
98      fseek(m_file, m_shdr.sh_offset, SEEK_SET)) {
99      m_status = Decoder::file_invalid;
100      return m_status;
101    }
102
103    Elf_Sym sym;
104    for (int index = 0; index < count; index ++) {
105      if (fread(&sym, sym_size, 1, m_file) == 1) {
106        if (STT_FUNC == ELF_ST_TYPE(sym.st_info)) {
107          address sym_addr = (address)sym.st_value;
108          if (sym_addr < addr && (addr - sym_addr) < *offset) {
109            pc = (address)sym.st_value;
110            *offset = (int)(addr - pc);
111            *posIndex = sym.st_name;
112            *stringtableIndex = m_shdr.sh_link;
113          }
114        }
115      } else {
116        m_status = Decoder::file_invalid;
117        return m_status;
118      }
119    }
120    fseek(m_file, cur_pos, SEEK_SET);
121  }
122  return m_status;
123}
124
125#endif // _WINDOWS
126