zgu@2364: /* mikael@6198: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. zgu@2364: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. zgu@2364: * zgu@2364: * This code is free software; you can redistribute it and/or modify it zgu@2364: * under the terms of the GNU General Public License version 2 only, as zgu@2364: * published by the Free Software Foundation. zgu@2364: * zgu@2364: * This code is distributed in the hope that it will be useful, but WITHOUT zgu@2364: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or zgu@2364: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License zgu@2364: * version 2 for more details (a copy is included in the LICENSE file that zgu@2364: * accompanied this code). zgu@2364: * zgu@2364: * You should have received a copy of the GNU General Public License version zgu@2364: * 2 along with this work; if not, write to the Free Software Foundation, zgu@2364: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. zgu@2364: * zgu@2364: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA zgu@2364: * or visit www.oracle.com if you need additional information or have any zgu@2364: * questions. zgu@2364: * zgu@2364: */ zgu@2364: zgu@3430: #ifndef SHARE_VM_UTILITIES_ELF_FILE_HPP zgu@3430: #define SHARE_VM_UTILITIES_ELF_FILE_HPP zgu@2364: never@3156: #if !defined(_WINDOWS) && !defined(__APPLE__) zgu@2364: never@3156: #if defined(__OpenBSD__) never@3156: #include never@3156: #else zgu@2364: #include never@3156: #endif zgu@2364: #include zgu@2364: zgu@2364: #ifdef _LP64 zgu@2364: zgu@2364: typedef Elf64_Half Elf_Half; zgu@2364: typedef Elf64_Word Elf_Word; zgu@2364: typedef Elf64_Off Elf_Off; zgu@2364: typedef Elf64_Addr Elf_Addr; zgu@2364: zgu@2364: typedef Elf64_Ehdr Elf_Ehdr; zgu@2364: typedef Elf64_Shdr Elf_Shdr; iklam@4710: typedef Elf64_Phdr Elf_Phdr; zgu@2364: typedef Elf64_Sym Elf_Sym; zgu@2364: never@3156: #if !defined(_ALLBSD_SOURCE) || defined(__APPLE__) zgu@2364: #define ELF_ST_TYPE ELF64_ST_TYPE never@3156: #endif zgu@2364: zgu@2364: #else zgu@2364: zgu@2364: typedef Elf32_Half Elf_Half; zgu@2364: typedef Elf32_Word Elf_Word; zgu@2364: typedef Elf32_Off Elf_Off; zgu@2364: typedef Elf32_Addr Elf_Addr; zgu@2364: zgu@2364: zgu@2364: typedef Elf32_Ehdr Elf_Ehdr; zgu@2364: typedef Elf32_Shdr Elf_Shdr; iklam@4710: typedef Elf32_Phdr Elf_Phdr; zgu@2364: typedef Elf32_Sym Elf_Sym; zgu@2364: never@3156: #if !defined(_ALLBSD_SOURCE) || defined(__APPLE__) zgu@2364: #define ELF_ST_TYPE ELF32_ST_TYPE zgu@2364: #endif never@3156: #endif zgu@2364: zgu@2364: #include "globalDefinitions.hpp" zgu@2364: #include "memory/allocation.hpp" zgu@2364: #include "utilities/decoder.hpp" zgu@2364: zgu@2364: zgu@2364: class ElfStringTable; zgu@2364: class ElfSymbolTable; simonis@6491: class ElfFuncDescTable; zgu@2364: zgu@2364: zgu@2364: // On Solaris/Linux platforms, libjvm.so does contain all private symbols. zgu@2364: // ElfFile is basically an elf file parser, which can lookup the symbol zgu@2364: // that is the nearest to the given address. zgu@2364: // Beware, this code is called from vm error reporting code, when vm is already zgu@2364: // in "error" state, so there are scenarios, lookup will fail. We want this zgu@2364: // part of code to be very defensive, and bait out if anything went wrong. zgu@2364: zgu@3900: class ElfFile: public CHeapObj { zgu@3430: friend class ElfDecoder; zgu@2364: public: zgu@2364: ElfFile(const char* filepath); zgu@2364: ~ElfFile(); zgu@2364: zgu@3430: bool decode(address addr, char* buf, int buflen, int* offset); zgu@2364: const char* filepath() { zgu@2364: return m_filepath; zgu@2364: } zgu@2364: zgu@2364: bool same_elf_file(const char* filepath) { zgu@2364: assert(filepath, "null file path"); zgu@2364: assert(m_filepath, "already out of memory"); zgu@2364: return (m_filepath && !strcmp(filepath, m_filepath)); zgu@2364: } zgu@2364: zgu@3430: NullDecoder::decoder_status get_status() { zgu@2364: return m_status; zgu@2364: } zgu@2364: zgu@2364: private: zgu@2364: // sanity check, if the file is a real elf file zgu@2364: bool is_elf_file(Elf_Ehdr&); zgu@2364: zgu@2364: // load string tables from the elf file zgu@2364: bool load_tables(); zgu@2364: zgu@2364: // string tables are stored in a linked list zgu@2364: void add_string_table(ElfStringTable* table); zgu@2364: zgu@2364: // symbol tables are stored in a linked list zgu@2364: void add_symbol_table(ElfSymbolTable* table); zgu@2364: zgu@2364: // return a string table at specified section index zgu@2364: ElfStringTable* get_string_table(int index); zgu@2364: zgu@3430: protected: zgu@3430: ElfFile* next() const { return m_next; } zgu@3430: void set_next(ElfFile* file) { m_next = file; } zgu@2364: iklam@4710: public: iklam@4710: // Returns true if the elf file is marked NOT to require an executable stack, iklam@4710: // or if the file could not be opened. iklam@4710: // Returns false if the elf file requires an executable stack, the stack flag iklam@4710: // is not set at all, or if the file can not be read. iklam@4710: // On systems other than linux it always returns false. iklam@4710: bool specifies_noexecstack() NOT_LINUX({ return false; }); iklam@4710: zgu@2364: protected: zgu@2364: ElfFile* m_next; zgu@2364: zgu@2364: private: zgu@2364: // file zgu@2364: const char* m_filepath; zgu@2364: FILE* m_file; zgu@2364: zgu@2364: // Elf header zgu@3430: Elf_Ehdr m_elfHdr; zgu@2364: zgu@2364: // symbol tables zgu@3430: ElfSymbolTable* m_symbol_tables; zgu@2364: zgu@2364: // string tables zgu@3430: ElfStringTable* m_string_tables; zgu@2364: simonis@6491: // function descriptors table simonis@6491: ElfFuncDescTable* m_funcDesc_table; simonis@6491: zgu@3430: NullDecoder::decoder_status m_status; zgu@2364: }; zgu@2364: simonis@6491: #endif // !_WINDOWS && !__APPLE__ zgu@2364: zgu@3430: #endif // SHARE_VM_UTILITIES_ELF_FILE_HPP