Wed, 03 Jul 2019 20:42:37 +0800
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_UTILITIES_ELF_FILE_HPP |
aoqi@0 | 26 | #define SHARE_VM_UTILITIES_ELF_FILE_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #if !defined(_WINDOWS) && !defined(__APPLE__) |
aoqi@0 | 29 | |
aoqi@0 | 30 | #if defined(__OpenBSD__) |
aoqi@0 | 31 | #include <sys/exec_elf.h> |
aoqi@0 | 32 | #else |
aoqi@0 | 33 | #include <elf.h> |
aoqi@0 | 34 | #endif |
aoqi@0 | 35 | #include <stdio.h> |
aoqi@0 | 36 | |
aoqi@0 | 37 | #ifdef _LP64 |
aoqi@0 | 38 | |
aoqi@0 | 39 | typedef Elf64_Half Elf_Half; |
aoqi@0 | 40 | typedef Elf64_Word Elf_Word; |
aoqi@0 | 41 | typedef Elf64_Off Elf_Off; |
aoqi@0 | 42 | typedef Elf64_Addr Elf_Addr; |
aoqi@0 | 43 | |
aoqi@0 | 44 | typedef Elf64_Ehdr Elf_Ehdr; |
aoqi@0 | 45 | typedef Elf64_Shdr Elf_Shdr; |
aoqi@0 | 46 | typedef Elf64_Phdr Elf_Phdr; |
aoqi@0 | 47 | typedef Elf64_Sym Elf_Sym; |
aoqi@0 | 48 | |
aoqi@0 | 49 | #if !defined(_ALLBSD_SOURCE) || defined(__APPLE__) |
aoqi@0 | 50 | #define ELF_ST_TYPE ELF64_ST_TYPE |
aoqi@0 | 51 | #endif |
aoqi@0 | 52 | |
aoqi@0 | 53 | #else |
aoqi@0 | 54 | |
aoqi@0 | 55 | typedef Elf32_Half Elf_Half; |
aoqi@0 | 56 | typedef Elf32_Word Elf_Word; |
aoqi@0 | 57 | typedef Elf32_Off Elf_Off; |
aoqi@0 | 58 | typedef Elf32_Addr Elf_Addr; |
aoqi@0 | 59 | |
aoqi@0 | 60 | |
aoqi@0 | 61 | typedef Elf32_Ehdr Elf_Ehdr; |
aoqi@0 | 62 | typedef Elf32_Shdr Elf_Shdr; |
aoqi@0 | 63 | typedef Elf32_Phdr Elf_Phdr; |
aoqi@0 | 64 | typedef Elf32_Sym Elf_Sym; |
aoqi@0 | 65 | |
aoqi@0 | 66 | #if !defined(_ALLBSD_SOURCE) || defined(__APPLE__) |
aoqi@0 | 67 | #define ELF_ST_TYPE ELF32_ST_TYPE |
aoqi@0 | 68 | #endif |
aoqi@0 | 69 | #endif |
aoqi@0 | 70 | |
aoqi@0 | 71 | #include "globalDefinitions.hpp" |
aoqi@0 | 72 | #include "memory/allocation.hpp" |
aoqi@0 | 73 | #include "utilities/decoder.hpp" |
aoqi@0 | 74 | |
aoqi@0 | 75 | |
aoqi@0 | 76 | class ElfStringTable; |
aoqi@0 | 77 | class ElfSymbolTable; |
aoqi@0 | 78 | class ElfFuncDescTable; |
aoqi@0 | 79 | |
aoqi@0 | 80 | |
aoqi@0 | 81 | // On Solaris/Linux platforms, libjvm.so does contain all private symbols. |
aoqi@0 | 82 | // ElfFile is basically an elf file parser, which can lookup the symbol |
aoqi@0 | 83 | // that is the nearest to the given address. |
aoqi@0 | 84 | // Beware, this code is called from vm error reporting code, when vm is already |
aoqi@0 | 85 | // in "error" state, so there are scenarios, lookup will fail. We want this |
aoqi@0 | 86 | // part of code to be very defensive, and bait out if anything went wrong. |
aoqi@0 | 87 | |
aoqi@0 | 88 | class ElfFile: public CHeapObj<mtInternal> { |
aoqi@0 | 89 | friend class ElfDecoder; |
aoqi@0 | 90 | public: |
aoqi@0 | 91 | ElfFile(const char* filepath); |
aoqi@0 | 92 | ~ElfFile(); |
aoqi@0 | 93 | |
aoqi@0 | 94 | bool decode(address addr, char* buf, int buflen, int* offset); |
aoqi@0 | 95 | const char* filepath() { |
aoqi@0 | 96 | return m_filepath; |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | bool same_elf_file(const char* filepath) { |
aoqi@0 | 100 | assert(filepath, "null file path"); |
aoqi@0 | 101 | assert(m_filepath, "already out of memory"); |
aoqi@0 | 102 | return (m_filepath && !strcmp(filepath, m_filepath)); |
aoqi@0 | 103 | } |
aoqi@0 | 104 | |
aoqi@0 | 105 | NullDecoder::decoder_status get_status() { |
aoqi@0 | 106 | return m_status; |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | private: |
aoqi@0 | 110 | // sanity check, if the file is a real elf file |
aoqi@0 | 111 | bool is_elf_file(Elf_Ehdr&); |
aoqi@0 | 112 | |
aoqi@0 | 113 | // load string tables from the elf file |
aoqi@0 | 114 | bool load_tables(); |
aoqi@0 | 115 | |
aoqi@0 | 116 | // string tables are stored in a linked list |
aoqi@0 | 117 | void add_string_table(ElfStringTable* table); |
aoqi@0 | 118 | |
aoqi@0 | 119 | // symbol tables are stored in a linked list |
aoqi@0 | 120 | void add_symbol_table(ElfSymbolTable* table); |
aoqi@0 | 121 | |
aoqi@0 | 122 | // return a string table at specified section index |
aoqi@0 | 123 | ElfStringTable* get_string_table(int index); |
aoqi@0 | 124 | |
aoqi@0 | 125 | protected: |
aoqi@0 | 126 | ElfFile* next() const { return m_next; } |
aoqi@0 | 127 | void set_next(ElfFile* file) { m_next = file; } |
aoqi@0 | 128 | |
aoqi@0 | 129 | public: |
aoqi@0 | 130 | // Returns true if the elf file is marked NOT to require an executable stack, |
aoqi@0 | 131 | // or if the file could not be opened. |
aoqi@0 | 132 | // Returns false if the elf file requires an executable stack, the stack flag |
aoqi@0 | 133 | // is not set at all, or if the file can not be read. |
aoqi@0 | 134 | // On systems other than linux it always returns false. |
aoqi@0 | 135 | bool specifies_noexecstack() NOT_LINUX({ return false; }); |
aoqi@0 | 136 | |
aoqi@0 | 137 | protected: |
aoqi@0 | 138 | ElfFile* m_next; |
aoqi@0 | 139 | |
aoqi@0 | 140 | private: |
aoqi@0 | 141 | // file |
aoqi@0 | 142 | const char* m_filepath; |
aoqi@0 | 143 | FILE* m_file; |
aoqi@0 | 144 | |
aoqi@0 | 145 | // Elf header |
aoqi@0 | 146 | Elf_Ehdr m_elfHdr; |
aoqi@0 | 147 | |
aoqi@0 | 148 | // symbol tables |
aoqi@0 | 149 | ElfSymbolTable* m_symbol_tables; |
aoqi@0 | 150 | |
aoqi@0 | 151 | // string tables |
aoqi@0 | 152 | ElfStringTable* m_string_tables; |
aoqi@0 | 153 | |
aoqi@0 | 154 | // function descriptors table |
aoqi@0 | 155 | ElfFuncDescTable* m_funcDesc_table; |
aoqi@0 | 156 | |
aoqi@0 | 157 | NullDecoder::decoder_status m_status; |
aoqi@0 | 158 | }; |
aoqi@0 | 159 | |
aoqi@0 | 160 | #endif // !_WINDOWS && !__APPLE__ |
aoqi@0 | 161 | |
aoqi@0 | 162 | #endif // SHARE_VM_UTILITIES_ELF_FILE_HPP |