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