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 #ifndef SHARE_VM_UTILITIES_ELF_FILE_HPP
  26 #define SHARE_VM_UTILITIES_ELF_FILE_HPP
  27 
  28 #if !defined(_WINDOWS) && !defined(__APPLE__)
  29 
  30 #if defined(__OpenBSD__)
  31 #include <sys/exec_elf.h>
  32 #else
  33 #include <elf.h>
  34 #endif
  35 #include <stdio.h>
  36 
  37 #ifdef _LP64
  38 
  39 typedef Elf64_Half      Elf_Half;
  40 typedef Elf64_Word      Elf_Word;
  41 typedef Elf64_Off       Elf_Off;
  42 typedef Elf64_Addr      Elf_Addr;
  43 
  44 typedef Elf64_Ehdr      Elf_Ehdr;
  45 typedef Elf64_Shdr      Elf_Shdr;
  46 typedef Elf64_Phdr      Elf_Phdr;
  47 typedef Elf64_Sym       Elf_Sym;
  48 
  49 #if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
  50 #define ELF_ST_TYPE ELF64_ST_TYPE
  51 #endif
  52 
  53 #else
  54 
  55 typedef Elf32_Half      Elf_Half;
  56 typedef Elf32_Word      Elf_Word;
  57 typedef Elf32_Off       Elf_Off;
  58 typedef Elf32_Addr      Elf_Addr;
  59 
  60 
  61 typedef Elf32_Ehdr      Elf_Ehdr;
  62 typedef Elf32_Shdr      Elf_Shdr;
  63 typedef Elf32_Phdr      Elf_Phdr;
  64 typedef Elf32_Sym       Elf_Sym;
  65 
  66 #if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
  67 #define ELF_ST_TYPE ELF32_ST_TYPE
  68 #endif
  69 #endif
  70 
  71 #include "globalDefinitions.hpp"
  72 #include "memory/allocation.hpp"
  73 #include "utilities/decoder.hpp"
  74 
  75 
  76 class ElfStringTable;
  77 class ElfSymbolTable;
  78 class ElfFuncDescTable;
  79 
  80 
  81 // On Solaris/Linux platforms, libjvm.so does contain all private symbols.
  82 // ElfFile is basically an elf file parser, which can lookup the symbol
  83 // that is the nearest to the given address.
  84 // Beware, this code is called from vm error reporting code, when vm is already
  85 // in "error" state, so there are scenarios, lookup will fail. We want this
  86 // part of code to be very defensive, and bait out if anything went wrong.
  87 
  88 class ElfFile: public CHeapObj<mtInternal> {
  89   friend class ElfDecoder;
  90  public:
  91   ElfFile(const char* filepath, bool load_tables);
  92   ~ElfFile();
  93 
  94   bool decode(address addr, char* buf, int buflen, int* offset);
  95   const char* filepath() {
  96     return m_filepath;
  97   }
  98 
  99   bool same_elf_file(const char* filepath) {
 100     assert(filepath, "null file path");
 101     assert(m_filepath, "already out of memory");
 102     return (m_filepath && !strcmp(filepath, m_filepath));
 103   }
 104 
 105   NullDecoder::decoder_status get_status() {
 106     return m_status;
 107   }
 108 
 109  private:
 110   // sanity check, if the file is a real elf file
 111   bool is_elf_file(Elf_Ehdr&);
 112 
 113   // load string tables from the elf file
 114   bool load_tables();
 115 
 116   // string tables are stored in a linked list
 117   void add_string_table(ElfStringTable* table);
 118 
 119   // symbol tables are stored in a linked list
 120   void add_symbol_table(ElfSymbolTable* table);
 121 
 122   // return a string table at specified section index
 123   ElfStringTable* get_string_table(int index);
 124 
 125 protected:
 126    ElfFile*  next() const { return m_next; }
 127    void set_next(ElfFile* file) { m_next = file; }
 128 
 129  public:
 130   // Returns true if the elf file is marked NOT to require an executable stack,
 131   // or if the file could not be opened.
 132   // Returns false if the elf file requires an executable stack, the stack flag
 133   // is not set at all, or if the file can not be read.
 134   // On systems other than linux it always returns false.
 135   bool specifies_noexecstack() NOT_LINUX({ return false; });
 136 
 137  protected:
 138     ElfFile*         m_next;
 139 
 140  private:
 141   // file
 142   const char* m_filepath;
 143   FILE* m_file;
 144 
 145   // Elf header
 146   Elf_Ehdr                     m_elfHdr;
 147 
 148   // symbol tables
 149   ElfSymbolTable*              m_symbol_tables;
 150 
 151   // string tables
 152   ElfStringTable*              m_string_tables;
 153 
 154   // function descriptors table
 155   ElfFuncDescTable*            m_funcDesc_table;
 156 
 157   NullDecoder::decoder_status  m_status;
 158 };
 159 
 160 #endif // !_WINDOWS && !__APPLE__
 161 
 162 #endif // SHARE_VM_UTILITIES_ELF_FILE_HPP