1 /*
   2  * Copyright (c) 1997, 2018, 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 typedef Elf64_Nhdr      Elf_Nhdr;
  49 
  50 #if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
  51 #define ELF_ST_TYPE ELF64_ST_TYPE
  52 #endif
  53 
  54 #else
  55 
  56 typedef Elf32_Half      Elf_Half;
  57 typedef Elf32_Word      Elf_Word;
  58 typedef Elf32_Off       Elf_Off;
  59 typedef Elf32_Addr      Elf_Addr;
  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 typedef Elf32_Nhdr      Elf_Nhdr;
  66 
  67 #if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
  68 #define ELF_ST_TYPE ELF32_ST_TYPE
  69 #endif
  70 #endif
  71 
  72 #include "globalDefinitions.hpp"
  73 #include "memory/allocation.hpp"
  74 #include "utilities/decoder.hpp"
  75 
  76 class ElfStringTable;
  77 class ElfSymbolTable;
  78 class ElfFuncDescTable;
  79 
  80 // ELF section, may or may not have cached data
  81 class ElfSection VALUE_OBJ_CLASS_SPEC {
  82 private:
  83   Elf_Shdr      _section_hdr;
  84   void*         _section_data;
  85   NullDecoder::decoder_status _stat;
  86 public:
  87   ElfSection(FILE* fd, const Elf_Shdr& hdr);
  88   ~ElfSection();
  89 
  90   NullDecoder::decoder_status status() const { return _stat; }
  91 
  92   const Elf_Shdr* section_header() const { return &_section_hdr; }
  93   const void*     section_data()   const { return (const void*)_section_data; }
  94 private:
  95   // load this section.
  96   // it return no_error, when it fails to cache the section data due to lack of memory
  97   NullDecoder::decoder_status load_section(FILE* const file, const Elf_Shdr& hdr);
  98 };
  99 
 100 class FileReader : public StackObj {
 101 protected:
 102   FILE* const _fd;
 103 public:
 104   FileReader(FILE* const fd) : _fd(fd) {};
 105   bool read(void* buf, size_t size);
 106   int  read_buffer(void* buf, size_t size);
 107   bool set_position(long offset);
 108 };
 109 
 110 // Mark current position, so we can get back to it after
 111 // reads.
 112 class MarkedFileReader : public FileReader {
 113 private:
 114   long  _marked_pos;
 115 public:
 116   MarkedFileReader(FILE* const fd);
 117   ~MarkedFileReader();
 118 
 119   bool has_mark() const { return _marked_pos >= 0; }
 120 };
 121 
 122 // Interface to external debug info file
 123 class ElfDebugInfo : public CHeapObj<mtInternal> {
 124 public:
 125   virtual bool decode(address addr, char* const buf, size_t buflen, int* offset) = 0;
 126   virtual bool has_error() const = 0;
 127 };
 128 
 129 // ElfFile is basically an elf file parser, which can lookup the symbol
 130 // that is the nearest to the given address.
 131 // Beware, this code is called from vm error reporting code, when vm is already
 132 // in "error" state, so there are scenarios, lookup will fail. We want this
 133 // part of code to be very defensive, and bait out if anything went wrong.
 134 class ElfFile: public CHeapObj<mtInternal> {
 135   friend class ElfDecoder;
 136 
 137 private:
 138   // link ElfFiles
 139   ElfFile*          _next;
 140 
 141   // Elf file
 142   char*             _filepath;
 143   FILE*             _file;
 144 
 145   // Elf header
 146   Elf_Ehdr          _elfHdr;
 147 
 148   // symbol tables
 149   ElfSymbolTable*   _symbol_tables;
 150 
 151   // regular string tables
 152   ElfStringTable*   _string_tables;
 153 
 154   // section header string table, used for finding section name
 155   ElfStringTable*   _shdr_string_table;
 156 
 157   // function descriptors table
 158   ElfFuncDescTable* _funcDesc_table;
 159 
 160   // external debug info
 161   ElfDebugInfo*     _debuginfo;
 162 
 163   NullDecoder::decoder_status  _status;
 164 
 165 public:
 166   ElfFile(const char* filepath, bool load_debug_info = true);
 167   ~ElfFile();
 168 
 169   bool decode(address addr, char* buf, int buflen, int* offset);
 170 
 171   const char* filepath() const {
 172     return _filepath;
 173   }
 174 
 175   bool same_elf_file(const char* filepath) const {
 176     assert(filepath != NULL, "null file path");
 177     return (_filepath != NULL && !strcmp(filepath, _filepath));
 178   }
 179 
 180   NullDecoder::decoder_status get_status() const {
 181     return _status;
 182   }
 183 
 184   // Returns true if the elf file is marked NOT to require an executable stack,
 185   // or if the file could not be opened.
 186   // Returns false if the elf file requires an executable stack, the stack flag
 187   // is not set at all, or if the file can not be read.
 188   // On systems other than linux it always returns false.
 189   static bool specifies_noexecstack(const char* filepath) NOT_LINUX({ return false; });
 190 private:
 191   // sanity check, if the file is a real elf file
 192   static bool is_elf_file(Elf_Ehdr&);
 193 
 194   // parse this elf file
 195   NullDecoder::decoder_status parse_elf(const char* filename);
 196 
 197   // load external debuginfo file for this file.
 198   ElfDebugInfo* load_debuginfo() NOT_LINUX({ return NULL; });
 199 
 200   // internal implementation
 201   bool decode_impl(address addr, char* buf, int buflen, int* offset);  
 202 
 203   // load string, symbol and function descriptor tables from the elf file
 204   NullDecoder::decoder_status load_tables();
 205 
 206   ElfFile*  next() const { return _next; }
 207   void set_next(ElfFile* file) { _next = file; }
 208 
 209   // find a section by name, return section index
 210   // if there is no such section, return -1
 211   int section_by_name(const char* name, Elf_Shdr& hdr);
 212 
 213   // string tables are stored in a linked list
 214   void add_string_table(ElfStringTable* table);
 215 
 216   // symbol tables are stored in a linked list
 217   void add_symbol_table(ElfSymbolTable* table);
 218 
 219   // return a string table at specified section index
 220   ElfStringTable* get_string_table(int index);
 221 
 222 
 223   FILE* const fd() const { return _file; }
 224 
 225   // Cleanup string, symbol and function descriptor tables
 226   void cleanup_tables();
 227 
 228 public:
 229   // For whitebox test
 230   static bool _do_not_cache_elf_section;
 231 };
 232 
 233 #endif // !_WINDOWS && !__APPLE__
 234 
 235 #endif // SHARE_VM_UTILITIES_ELF_FILE_HPP