1 /*
   2  * Copyright (c) 1997, 2010, 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 __ELF_FILE_HPP
  26 #define __ELF_FILE_HPP
  27 
  28 #ifndef _WINDOWS
  29 
  30 #include <elf.h>
  31 #include <stdio.h>
  32 
  33 #ifdef _LP64
  34 
  35 typedef Elf64_Half      Elf_Half;
  36 typedef Elf64_Word      Elf_Word;
  37 typedef Elf64_Off       Elf_Off;
  38 typedef Elf64_Addr      Elf_Addr;
  39 
  40 typedef Elf64_Ehdr      Elf_Ehdr;
  41 typedef Elf64_Shdr      Elf_Shdr;
  42 typedef Elf64_Sym       Elf_Sym;
  43 
  44 #define ELF_ST_TYPE ELF64_ST_TYPE
  45 
  46 #else
  47 
  48 typedef Elf32_Half      Elf_Half;
  49 typedef Elf32_Word      Elf_Word;
  50 typedef Elf32_Off       Elf_Off;
  51 typedef Elf32_Addr      Elf_Addr;
  52 
  53 
  54 typedef Elf32_Ehdr      Elf_Ehdr;
  55 typedef Elf32_Shdr      Elf_Shdr;
  56 typedef Elf32_Sym       Elf_Sym;
  57 
  58 #define ELF_ST_TYPE ELF32_ST_TYPE
  59 #endif
  60 
  61 #include "globalDefinitions.hpp"
  62 #include "memory/allocation.hpp"
  63 #include "utilities/decoder.hpp"
  64 
  65 
  66 class ElfStringTable;
  67 class ElfSymbolTable;
  68 
  69 
  70 // On Solaris/Linux platforms, libjvm.so does contain all private symbols.
  71 // ElfFile is basically an elf file parser, which can lookup the symbol
  72 // that is the nearest to the given address.
  73 // Beware, this code is called from vm error reporting code, when vm is already
  74 // in "error" state, so there are scenarios, lookup will fail. We want this
  75 // part of code to be very defensive, and bait out if anything went wrong.
  76 
  77 class ElfFile: public CHeapObj {
  78   friend class Decoder;
  79  public:
  80   ElfFile(const char* filepath);
  81   ~ElfFile();
  82 
  83   const char* decode(address addr, int* offset);
  84   const char* filepath() {
  85     return m_filepath;
  86   }
  87 
  88   bool same_elf_file(const char* filepath) {
  89     assert(filepath, "null file path");
  90     assert(m_filepath, "already out of memory");
  91     return (m_filepath && !strcmp(filepath, m_filepath));
  92   }
  93 
  94   Decoder::decoder_status get_status() {
  95     return m_status;
  96   }
  97 
  98  private:
  99   // sanity check, if the file is a real elf file
 100   bool is_elf_file(Elf_Ehdr&);
 101 
 102   // load string tables from the elf file
 103   bool load_tables();
 104 
 105   // string tables are stored in a linked list
 106   void add_string_table(ElfStringTable* table);
 107 
 108   // symbol tables are stored in a linked list
 109   void add_symbol_table(ElfSymbolTable* table);
 110 
 111   // return a string table at specified section index
 112   ElfStringTable* get_string_table(int index);
 113 
 114   // look up an address and return the nearest symbol
 115   const char* look_up(Elf_Shdr shdr, address addr, int* offset);
 116 
 117  protected:
 118     ElfFile*         m_next;
 119 
 120  private:
 121   // file
 122   const char* m_filepath;
 123   FILE* m_file;
 124 
 125   // Elf header
 126   Elf_Ehdr            m_elfHdr;
 127 
 128   // symbol tables
 129   ElfSymbolTable*     m_symbol_tables;
 130 
 131   // string tables
 132   ElfStringTable*     m_string_tables;
 133 
 134   Decoder::decoder_status  m_status;
 135 };
 136 
 137 #endif // _WINDOWS
 138 
 139 #endif // __ELF_FILE_HPP
 140