src/share/vm/utilities/elfFile.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/elfFile.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,162 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_UTILITIES_ELF_FILE_HPP
    1.29 +#define SHARE_VM_UTILITIES_ELF_FILE_HPP
    1.30 +
    1.31 +#if !defined(_WINDOWS) && !defined(__APPLE__)
    1.32 +
    1.33 +#if defined(__OpenBSD__)
    1.34 +#include <sys/exec_elf.h>
    1.35 +#else
    1.36 +#include <elf.h>
    1.37 +#endif
    1.38 +#include <stdio.h>
    1.39 +
    1.40 +#ifdef _LP64
    1.41 +
    1.42 +typedef Elf64_Half      Elf_Half;
    1.43 +typedef Elf64_Word      Elf_Word;
    1.44 +typedef Elf64_Off       Elf_Off;
    1.45 +typedef Elf64_Addr      Elf_Addr;
    1.46 +
    1.47 +typedef Elf64_Ehdr      Elf_Ehdr;
    1.48 +typedef Elf64_Shdr      Elf_Shdr;
    1.49 +typedef Elf64_Phdr      Elf_Phdr;
    1.50 +typedef Elf64_Sym       Elf_Sym;
    1.51 +
    1.52 +#if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
    1.53 +#define ELF_ST_TYPE ELF64_ST_TYPE
    1.54 +#endif
    1.55 +
    1.56 +#else
    1.57 +
    1.58 +typedef Elf32_Half      Elf_Half;
    1.59 +typedef Elf32_Word      Elf_Word;
    1.60 +typedef Elf32_Off       Elf_Off;
    1.61 +typedef Elf32_Addr      Elf_Addr;
    1.62 +
    1.63 +
    1.64 +typedef Elf32_Ehdr      Elf_Ehdr;
    1.65 +typedef Elf32_Shdr      Elf_Shdr;
    1.66 +typedef Elf32_Phdr      Elf_Phdr;
    1.67 +typedef Elf32_Sym       Elf_Sym;
    1.68 +
    1.69 +#if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
    1.70 +#define ELF_ST_TYPE ELF32_ST_TYPE
    1.71 +#endif
    1.72 +#endif
    1.73 +
    1.74 +#include "globalDefinitions.hpp"
    1.75 +#include "memory/allocation.hpp"
    1.76 +#include "utilities/decoder.hpp"
    1.77 +
    1.78 +
    1.79 +class ElfStringTable;
    1.80 +class ElfSymbolTable;
    1.81 +class ElfFuncDescTable;
    1.82 +
    1.83 +
    1.84 +// On Solaris/Linux platforms, libjvm.so does contain all private symbols.
    1.85 +// ElfFile is basically an elf file parser, which can lookup the symbol
    1.86 +// that is the nearest to the given address.
    1.87 +// Beware, this code is called from vm error reporting code, when vm is already
    1.88 +// in "error" state, so there are scenarios, lookup will fail. We want this
    1.89 +// part of code to be very defensive, and bait out if anything went wrong.
    1.90 +
    1.91 +class ElfFile: public CHeapObj<mtInternal> {
    1.92 +  friend class ElfDecoder;
    1.93 + public:
    1.94 +  ElfFile(const char* filepath);
    1.95 +  ~ElfFile();
    1.96 +
    1.97 +  bool decode(address addr, char* buf, int buflen, int* offset);
    1.98 +  const char* filepath() {
    1.99 +    return m_filepath;
   1.100 +  }
   1.101 +
   1.102 +  bool same_elf_file(const char* filepath) {
   1.103 +    assert(filepath, "null file path");
   1.104 +    assert(m_filepath, "already out of memory");
   1.105 +    return (m_filepath && !strcmp(filepath, m_filepath));
   1.106 +  }
   1.107 +
   1.108 +  NullDecoder::decoder_status get_status() {
   1.109 +    return m_status;
   1.110 +  }
   1.111 +
   1.112 + private:
   1.113 +  // sanity check, if the file is a real elf file
   1.114 +  bool is_elf_file(Elf_Ehdr&);
   1.115 +
   1.116 +  // load string tables from the elf file
   1.117 +  bool load_tables();
   1.118 +
   1.119 +  // string tables are stored in a linked list
   1.120 +  void add_string_table(ElfStringTable* table);
   1.121 +
   1.122 +  // symbol tables are stored in a linked list
   1.123 +  void add_symbol_table(ElfSymbolTable* table);
   1.124 +
   1.125 +  // return a string table at specified section index
   1.126 +  ElfStringTable* get_string_table(int index);
   1.127 +
   1.128 +protected:
   1.129 +   ElfFile*  next() const { return m_next; }
   1.130 +   void set_next(ElfFile* file) { m_next = file; }
   1.131 +
   1.132 + public:
   1.133 +  // Returns true if the elf file is marked NOT to require an executable stack,
   1.134 +  // or if the file could not be opened.
   1.135 +  // Returns false if the elf file requires an executable stack, the stack flag
   1.136 +  // is not set at all, or if the file can not be read.
   1.137 +  // On systems other than linux it always returns false.
   1.138 +  bool specifies_noexecstack() NOT_LINUX({ return false; });
   1.139 +
   1.140 + protected:
   1.141 +    ElfFile*         m_next;
   1.142 +
   1.143 + private:
   1.144 +  // file
   1.145 +  const char* m_filepath;
   1.146 +  FILE* m_file;
   1.147 +
   1.148 +  // Elf header
   1.149 +  Elf_Ehdr                     m_elfHdr;
   1.150 +
   1.151 +  // symbol tables
   1.152 +  ElfSymbolTable*              m_symbol_tables;
   1.153 +
   1.154 +  // string tables
   1.155 +  ElfStringTable*              m_string_tables;
   1.156 +
   1.157 +  // function descriptors table
   1.158 +  ElfFuncDescTable*            m_funcDesc_table;
   1.159 +
   1.160 +  NullDecoder::decoder_status  m_status;
   1.161 +};
   1.162 +
   1.163 +#endif // !_WINDOWS && !__APPLE__
   1.164 +
   1.165 +#endif // SHARE_VM_UTILITIES_ELF_FILE_HPP

mercurial