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