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 #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_Sym       Elf_Sym;
  47 
  48 #if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
  49 #define ELF_ST_TYPE ELF64_ST_TYPE
  50 #endif
  51 
  52 #else
  53 
  54 typedef Elf32_Half      Elf_Half;
  55 typedef Elf32_Word      Elf_Word;
  56 typedef Elf32_Off       Elf_Off;
  57 typedef Elf32_Addr      Elf_Addr;
  58 
  59 
  60 typedef Elf32_Ehdr      Elf_Ehdr;
  61 typedef Elf32_Shdr      Elf_Shdr;
  62 typedef Elf32_Sym       Elf_Sym;
  63 
  64 #if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
  65 #define ELF_ST_TYPE ELF32_ST_TYPE
  66 #endif
  67 #endif
  68 
  69 #include "globalDefinitions.hpp"
  70 #include "memory/allocation.hpp"
  71 #include "utilities/decoder.hpp"
  72 
  73 
  74 class ElfStringTable;
  75 class ElfSymbolTable;
  76 
  77 
  78 // On Solaris/Linux platforms, libjvm.so does contain all private symbols.
  79 // ElfFile is basically an elf file parser, which can lookup the symbol
  80 // that is the nearest to the given address.
  81 // Beware, this code is called from vm error reporting code, when vm is already
  82 // in "error" state, so there are scenarios, lookup will fail. We want this
  83 // part of code to be very defensive, and bait out if anything went wrong.
  84 
  85 class ElfFile: public CHeapObj {
  86   friend class Decoder;
  87  public:
  88   ElfFile(const char* filepath);
  89   ~ElfFile();
  90 
  91   const char* decode(address addr, int* offset);
  92   const char* filepath() {
  93     return m_filepath;
  94   }
  95 
  96   bool same_elf_file(const char* filepath) {
  97     assert(filepath, "null file path");
  98     assert(m_filepath, "already out of memory");
  99     return (m_filepath && !strcmp(filepath, m_filepath));
 100   }
 101 
 102   Decoder::decoder_status get_status() {
 103     return m_status;
 104   }
 105 
 106  private:
 107   // sanity check, if the file is a real elf file
 108   bool is_elf_file(Elf_Ehdr&);
 109 
 110   // load string tables from the elf file
 111   bool load_tables();
 112 
 113   // string tables are stored in a linked list
 114   void add_string_table(ElfStringTable* table);
 115 
 116   // symbol tables are stored in a linked list
 117   void add_symbol_table(ElfSymbolTable* table);
 118 
 119   // return a string table at specified section index
 120   ElfStringTable* get_string_table(int index);
 121 
 122   // look up an address and return the nearest symbol
 123   const char* look_up(Elf_Shdr shdr, address addr, int* offset);

 124 
 125  protected:
 126     ElfFile*         m_next;
 127 
 128  private:
 129   // file
 130   const char* m_filepath;
 131   FILE* m_file;
 132 
 133   // Elf header
 134   Elf_Ehdr            m_elfHdr;
 135 
 136   // symbol tables
 137   ElfSymbolTable*     m_symbol_tables;
 138 
 139   // string tables
 140   ElfStringTable*     m_string_tables;
 141 
 142   Decoder::decoder_status  m_status;
 143 };
 144 
 145 #endif // _WINDOWS
 146 
 147 #endif // __ELF_FILE_HPP
--- EOF ---