elfFile.hpp 3.9 KB
Newer Older
1
/*
2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

Z
zgu 已提交
25 26
#ifndef SHARE_VM_UTILITIES_ELF_FILE_HPP
#define SHARE_VM_UTILITIES_ELF_FILE_HPP
27

N
never 已提交
28
#if !defined(_WINDOWS) && !defined(__APPLE__)
29

N
never 已提交
30 31 32
#if defined(__OpenBSD__)
#include <sys/exec_elf.h>
#else
33
#include <elf.h>
N
never 已提交
34
#endif
35 36 37 38 39 40 41 42 43 44 45 46 47
#include <stdio.h>

#ifdef _LP64

typedef Elf64_Half      Elf_Half;
typedef Elf64_Word      Elf_Word;
typedef Elf64_Off       Elf_Off;
typedef Elf64_Addr      Elf_Addr;

typedef Elf64_Ehdr      Elf_Ehdr;
typedef Elf64_Shdr      Elf_Shdr;
typedef Elf64_Sym       Elf_Sym;

N
never 已提交
48
#if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
49
#define ELF_ST_TYPE ELF64_ST_TYPE
N
never 已提交
50
#endif
51 52 53 54 55 56 57 58 59 60 61 62 63

#else

typedef Elf32_Half      Elf_Half;
typedef Elf32_Word      Elf_Word;
typedef Elf32_Off       Elf_Off;
typedef Elf32_Addr      Elf_Addr;


typedef Elf32_Ehdr      Elf_Ehdr;
typedef Elf32_Shdr      Elf_Shdr;
typedef Elf32_Sym       Elf_Sym;

N
never 已提交
64
#if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
65 66
#define ELF_ST_TYPE ELF32_ST_TYPE
#endif
N
never 已提交
67
#endif
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

#include "globalDefinitions.hpp"
#include "memory/allocation.hpp"
#include "utilities/decoder.hpp"


class ElfStringTable;
class ElfSymbolTable;


// On Solaris/Linux platforms, libjvm.so does contain all private symbols.
// ElfFile is basically an elf file parser, which can lookup the symbol
// that is the nearest to the given address.
// Beware, this code is called from vm error reporting code, when vm is already
// in "error" state, so there are scenarios, lookup will fail. We want this
// part of code to be very defensive, and bait out if anything went wrong.

Z
zgu 已提交
85
class ElfFile: public CHeapObj<mtInternal> {
Z
zgu 已提交
86
  friend class ElfDecoder;
87 88 89 90
 public:
  ElfFile(const char* filepath);
  ~ElfFile();

Z
zgu 已提交
91
  bool decode(address addr, char* buf, int buflen, int* offset);
92 93 94 95 96 97 98 99 100 101
  const char* filepath() {
    return m_filepath;
  }

  bool same_elf_file(const char* filepath) {
    assert(filepath, "null file path");
    assert(m_filepath, "already out of memory");
    return (m_filepath && !strcmp(filepath, m_filepath));
  }

Z
zgu 已提交
102
  NullDecoder::decoder_status get_status() {
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    return m_status;
  }

 private:
  // sanity check, if the file is a real elf file
  bool is_elf_file(Elf_Ehdr&);

  // load string tables from the elf file
  bool load_tables();

  // string tables are stored in a linked list
  void add_string_table(ElfStringTable* table);

  // symbol tables are stored in a linked list
  void add_symbol_table(ElfSymbolTable* table);

  // return a string table at specified section index
  ElfStringTable* get_string_table(int index);

Z
zgu 已提交
122 123 124
protected:
   ElfFile*  next() const { return m_next; }
   void set_next(ElfFile* file) { m_next = file; }
125 126 127 128 129 130 131 132 133 134

 protected:
    ElfFile*         m_next;

 private:
  // file
  const char* m_filepath;
  FILE* m_file;

  // Elf header
Z
zgu 已提交
135
  Elf_Ehdr                     m_elfHdr;
136 137

  // symbol tables
Z
zgu 已提交
138
  ElfSymbolTable*              m_symbol_tables;
139 140

  // string tables
Z
zgu 已提交
141
  ElfStringTable*              m_string_tables;
142

Z
zgu 已提交
143
  NullDecoder::decoder_status  m_status;
144 145 146 147
};

#endif // _WINDOWS

Z
zgu 已提交
148
#endif // SHARE_VM_UTILITIES_ELF_FILE_HPP