1 /*
   2  * Copyright (c) 1997, 2012, 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 <string.h>
  30 #include <stdio.h>
  31 #include <limits.h>
  32 #include <new>
  33 
  34 #include "memory/allocation.inline.hpp"
  35 #include "utilities/decoder.hpp"
  36 #include "utilities/elfFile.hpp"
  37 #include "utilities/elfStringTable.hpp"
  38 #include "utilities/elfSymbolTable.hpp"
  39 
  40 
  41 ElfFile::ElfFile(const char* filepath) {
  42   assert(filepath, "null file path");
  43   memset(&m_elfHdr, 0, sizeof(m_elfHdr));
  44   m_string_tables = NULL;
  45   m_symbol_tables = NULL;
  46   m_next = NULL;
  47   m_status = NullDecoder::no_error;
  48 
  49   int len = strlen(filepath) + 1;
  50   m_filepath = (const char*)os::malloc(len * sizeof(char), mtInternal);
  51   if (m_filepath != NULL) {
  52     strcpy((char*)m_filepath, filepath);
  53     m_file = fopen(filepath, "r");
  54     if (m_file != NULL) {
  55       load_tables();
  56     } else {
  57       m_status = NullDecoder::file_not_found;
  58     }
  59   } else {
  60     m_status = NullDecoder::out_of_memory;
  61   }
  62 }
  63 
  64 ElfFile::~ElfFile() {
  65   if (m_string_tables != NULL) {
  66     delete m_string_tables;
  67   }
  68 
  69   if (m_symbol_tables != NULL) {
  70     delete m_symbol_tables;
  71   }
  72 
  73   if (m_file != NULL) {
  74     fclose(m_file);
  75   }
  76 
  77   if (m_filepath != NULL) {
  78     os::free((void*)m_filepath);
  79   }
  80 
  81   if (m_next != NULL) {
  82     delete m_next;
  83   }
  84 };
  85 
  86 
  87 //Check elf header to ensure the file is valid.
  88 bool ElfFile::is_elf_file(Elf_Ehdr& hdr) {
  89   return (ELFMAG0 == hdr.e_ident[EI_MAG0] &&
  90       ELFMAG1 == hdr.e_ident[EI_MAG1] &&
  91       ELFMAG2 == hdr.e_ident[EI_MAG2] &&
  92       ELFMAG3 == hdr.e_ident[EI_MAG3] &&
  93       ELFCLASSNONE != hdr.e_ident[EI_CLASS] &&
  94       ELFDATANONE != hdr.e_ident[EI_DATA]);
  95 }
  96 
  97 bool ElfFile::load_tables() {
  98   assert(m_file, "file not open");
  99   assert(!NullDecoder::is_error(m_status), "already in error");
 100 
 101   // read elf file header
 102   if (fread(&m_elfHdr, sizeof(m_elfHdr), 1, m_file) != 1) {
 103     m_status = NullDecoder::file_invalid;
 104     return false;
 105   }
 106 
 107   if (!is_elf_file(m_elfHdr)) {
 108     m_status = NullDecoder::file_invalid;
 109     return false;
 110   }
 111 
 112   // walk elf file's section headers, and load string tables
 113   Elf_Shdr shdr;
 114   if (!fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
 115     if (NullDecoder::is_error(m_status)) return false;
 116 
 117     for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
 118       if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
 119         m_status = NullDecoder::file_invalid;
 120         return false;
 121       }
 122       // string tables
 123       if (shdr.sh_type == SHT_STRTAB) {
 124         ElfStringTable* table = new (std::nothrow) ElfStringTable(m_file, shdr, index);
 125         if (table == NULL) {
 126           m_status = NullDecoder::out_of_memory;
 127           return false;
 128         }
 129         add_string_table(table);
 130       }
 131       // symbol tables
 132       else if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
 133         ElfSymbolTable* table = new (std::nothrow) ElfSymbolTable(m_file, shdr);
 134         if (table == NULL) {
 135           m_status = NullDecoder::out_of_memory;
 136           return false;
 137         }
 138         add_symbol_table(table);
 139       }
 140     }
 141 
 142 #if defined(PPC64)
 143     // Now read the .opd section wich contains the PPC64 function descriptor table.
 144     // This is only possible after we have successfully read in the string tables in te previous loop.
 145     m_funcDesc_table = NULL;
 146     // Reset the file pointer
 147     if (fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
 148       m_status = NullDecoder::file_invalid;
 149       return false;
 150     }
 151     for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
 152       if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
 153         m_status = NullDecoder::file_invalid;
 154         return false;
 155       }
 156       if (m_elfHdr.e_shstrndx != SHN_UNDEF && shdr.sh_type == SHT_PROGBITS) {
 157         ElfStringTable* string_table = get_string_table(m_elfHdr.e_shstrndx);
 158         if (string_table == NULL) {
 159           m_status = NullDecoder::file_invalid;
 160           return false;
 161         }
 162         char buf[8]; // We only want to read ".opd"
 163         if (string_table->string_at(shdr.sh_name, buf, sizeof(buf)) && !strncmp(".opd", buf, 4)) {
 164           m_funcDesc_table = new (std::nothrow) ElfFuncDescTable(m_file, shdr);
 165         }
 166       }
 167     }
 168 #endif
 169 
 170   }
 171   return true;
 172 }
 173 
 174 bool ElfFile::decode(address addr, char* buf, int buflen, int* offset) {
 175   // something already went wrong, just give up
 176   if (NullDecoder::is_error(m_status)) {
 177     return false;
 178   }
 179   ElfSymbolTable* symbol_table = m_symbol_tables;
 180   int string_table_index;
 181   int pos_in_string_table;
 182   int off = INT_MAX;
 183   bool found_symbol = false;
 184   while (symbol_table != NULL) {
 185 #if defined(PPC64)
 186     if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off, m_funcDesc_table)) {
 187 #else
 188     if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off)) {
 189 #endif
 190       found_symbol = true;
 191       break;
 192     }
 193     symbol_table = symbol_table->m_next;
 194   }
 195   if (!found_symbol) return false;
 196 
 197   ElfStringTable* string_table = get_string_table(string_table_index);
 198 
 199   if (string_table == NULL) {
 200     m_status = NullDecoder::file_invalid;
 201     return false;
 202   }
 203   if (offset) *offset = off;
 204 
 205   return string_table->string_at(pos_in_string_table, buf, buflen);
 206 }
 207 
 208 
 209 void ElfFile::add_symbol_table(ElfSymbolTable* table) {
 210   if (m_symbol_tables == NULL) {
 211     m_symbol_tables = table;
 212   } else {
 213     table->m_next = m_symbol_tables;
 214     m_symbol_tables = table;
 215   }
 216 }
 217 
 218 void ElfFile::add_string_table(ElfStringTable* table) {
 219   if (m_string_tables == NULL) {
 220     m_string_tables = table;
 221   } else {
 222     table->m_next = m_string_tables;
 223     m_string_tables = table;
 224   }
 225 }
 226 
 227 ElfStringTable* ElfFile::get_string_table(int index) {
 228   ElfStringTable* p = m_string_tables;
 229   while (p != NULL) {
 230     if (p->index() == index) return p;
 231     p = p->m_next;
 232   }
 233   return NULL;
 234 }
 235 
 236 #ifdef LINUX
 237 bool ElfFile::specifies_noexecstack() {
 238   Elf_Phdr phdr;
 239   if (!m_file)  return true;
 240 
 241   if (!fseek(m_file, m_elfHdr.e_phoff, SEEK_SET)) {
 242     for (int index = 0; index < m_elfHdr.e_phnum; index ++) {
 243       if (fread((void*)&phdr, sizeof(Elf_Phdr), 1, m_file) != 1) {
 244         m_status = NullDecoder::file_invalid;
 245         return false;
 246       }
 247       if (phdr.p_type == PT_GNU_STACK) {
 248         if (phdr.p_flags == (PF_R | PF_W))  {
 249           return true;
 250         } else {
 251           return false;
 252         }
 253       }
 254     }
 255   }
 256   return false;
 257 }
 258 #endif
 259 
 260 #endif // _WINDOWS