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 #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 #if defined(PPC64)
  79 class ElfFuncDescTable;
  80 #endif
  81 
  82 
  83 // On Solaris/Linux platforms, libjvm.so does contain all private symbols.
  84 // ElfFile is basically an elf file parser, which can lookup the symbol
  85 // that is the nearest to the given address.
  86 // Beware, this code is called from vm error reporting code, when vm is already
  87 // in "error" state, so there are scenarios, lookup will fail. We want this
  88 // part of code to be very defensive, and bait out if anything went wrong.
  89 
  90 class ElfFile: public CHeapObj<mtInternal> {
  91   friend class ElfDecoder;
  92  public:
  93   ElfFile(const char* filepath);
  94   ~ElfFile();
  95 
  96   bool decode(address addr, char* buf, int buflen, int* offset);
  97   const char* filepath() {
  98     return m_filepath;
  99   }
 100 
 101   bool same_elf_file(const char* filepath) {
 102     assert(filepath, "null file path");
 103     assert(m_filepath, "already out of memory");
 104     return (m_filepath && !strcmp(filepath, m_filepath));
 105   }
 106 
 107   NullDecoder::decoder_status get_status() {
 108     return m_status;
 109   }
 110 
 111  private:
 112   // sanity check, if the file is a real elf file
 113   bool is_elf_file(Elf_Ehdr&);
 114 
 115   // load string tables from the elf file
 116   bool load_tables();
 117 
 118   // string tables are stored in a linked list
 119   void add_string_table(ElfStringTable* table);
 120 
 121   // symbol tables are stored in a linked list
 122   void add_symbol_table(ElfSymbolTable* table);
 123 
 124   // return a string table at specified section index
 125   ElfStringTable* get_string_table(int index);
 126 
 127 protected:
 128    ElfFile*  next() const { return m_next; }
 129    void set_next(ElfFile* file) { m_next = file; }
 130 
 131  public:
 132   // Returns true if the elf file is marked NOT to require an executable stack,
 133   // or if the file could not be opened.
 134   // Returns false if the elf file requires an executable stack, the stack flag
 135   // is not set at all, or if the file can not be read.
 136   // On systems other than linux it always returns false.
 137   bool specifies_noexecstack() NOT_LINUX({ return false; });
 138 
 139  protected:
 140     ElfFile*         m_next;
 141 
 142  private:
 143   // file
 144   const char* m_filepath;
 145   FILE* m_file;
 146 
 147   // Elf header
 148   Elf_Ehdr                     m_elfHdr;
 149 
 150   // symbol tables
 151   ElfSymbolTable*              m_symbol_tables;
 152 
 153   // string tables
 154   ElfStringTable*              m_string_tables;
 155 
 156 #if defined(PPC64)
 157   // function descriptors table
 158   ElfFuncDescTable*            m_funcDesc_table;
 159 #endif
 160 
 161   NullDecoder::decoder_status  m_status;
 162 };
 163 
 164 #endif // _WINDOWS
 165 
 166 #endif // SHARE_VM_UTILITIES_ELF_FILE_HPP