elfFile.cpp 5.4 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 25 26
 * 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.
 *
 */

#include "precompiled.hpp"

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

#include <string.h>
#include <stdio.h>
#include <limits.h>
32
#include <new>
33 34 35 36 37 38 39 40 41 42 43 44 45 46

#include "memory/allocation.inline.hpp"
#include "utilities/decoder.hpp"
#include "utilities/elfFile.hpp"
#include "utilities/elfStringTable.hpp"
#include "utilities/elfSymbolTable.hpp"


ElfFile::ElfFile(const char* filepath) {
  assert(filepath, "null file path");
  memset(&m_elfHdr, 0, sizeof(m_elfHdr));
  m_string_tables = NULL;
  m_symbol_tables = NULL;
  m_next = NULL;
Z
zgu 已提交
47
  m_status = NullDecoder::no_error;
48 49

  int len = strlen(filepath) + 1;
Z
zgu 已提交
50
  m_filepath = (const char*)os::malloc(len * sizeof(char), mtInternal);
51 52 53 54 55 56
  if (m_filepath != NULL) {
    strcpy((char*)m_filepath, filepath);
    m_file = fopen(filepath, "r");
    if (m_file != NULL) {
      load_tables();
    } else {
Z
zgu 已提交
57
      m_status = NullDecoder::file_not_found;
58 59
    }
  } else {
Z
zgu 已提交
60
    m_status = NullDecoder::out_of_memory;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  }
}

ElfFile::~ElfFile() {
  if (m_string_tables != NULL) {
    delete m_string_tables;
  }

  if (m_symbol_tables != NULL) {
    delete m_symbol_tables;
  }

  if (m_file != NULL) {
    fclose(m_file);
  }

  if (m_filepath != NULL) {
78
    os::free((void*)m_filepath);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  }

  if (m_next != NULL) {
    delete m_next;
  }
};


//Check elf header to ensure the file is valid.
bool ElfFile::is_elf_file(Elf_Ehdr& hdr) {
  return (ELFMAG0 == hdr.e_ident[EI_MAG0] &&
      ELFMAG1 == hdr.e_ident[EI_MAG1] &&
      ELFMAG2 == hdr.e_ident[EI_MAG2] &&
      ELFMAG3 == hdr.e_ident[EI_MAG3] &&
      ELFCLASSNONE != hdr.e_ident[EI_CLASS] &&
      ELFDATANONE != hdr.e_ident[EI_DATA]);
}

bool ElfFile::load_tables() {
  assert(m_file, "file not open");
Z
zgu 已提交
99
  assert(!NullDecoder::is_error(m_status), "already in error");
100 101 102

  // read elf file header
  if (fread(&m_elfHdr, sizeof(m_elfHdr), 1, m_file) != 1) {
Z
zgu 已提交
103
    m_status = NullDecoder::file_invalid;
104 105 106 107
    return false;
  }

  if (!is_elf_file(m_elfHdr)) {
Z
zgu 已提交
108
    m_status = NullDecoder::file_invalid;
109 110 111 112 113 114
    return false;
  }

  // walk elf file's section headers, and load string tables
  Elf_Shdr shdr;
  if (!fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
Z
zgu 已提交
115
    if (NullDecoder::is_error(m_status)) return false;
116 117 118

    for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
      if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
Z
zgu 已提交
119
        m_status = NullDecoder::file_invalid;
120 121 122 123
        return false;
      }
      // string table
      if (shdr.sh_type == SHT_STRTAB) {
124
        ElfStringTable* table = new (std::nothrow) ElfStringTable(m_file, shdr, index);
125
        if (table == NULL) {
Z
zgu 已提交
126
          m_status = NullDecoder::out_of_memory;
127 128 129 130
          return false;
        }
        add_string_table(table);
      } else if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
131
        ElfSymbolTable* table = new (std::nothrow) ElfSymbolTable(m_file, shdr);
132
        if (table == NULL) {
Z
zgu 已提交
133
          m_status = NullDecoder::out_of_memory;
134 135 136 137 138 139 140 141 142
          return false;
        }
        add_symbol_table(table);
      }
    }
  }
  return true;
}

Z
zgu 已提交
143
bool ElfFile::decode(address addr, char* buf, int buflen, int* offset) {
144
  // something already went wrong, just give up
Z
zgu 已提交
145 146
  if (NullDecoder::is_error(m_status)) {
    return false;
147 148 149 150 151 152 153
  }
  ElfSymbolTable* symbol_table = m_symbol_tables;
  int string_table_index;
  int pos_in_string_table;
  int off = INT_MAX;
  bool found_symbol = false;
  while (symbol_table != NULL) {
Z
zgu 已提交
154
    if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off)) {
155 156 157 158
      found_symbol = true;
    }
    symbol_table = symbol_table->m_next;
  }
Z
zgu 已提交
159
  if (!found_symbol) return false;
160 161

  ElfStringTable* string_table = get_string_table(string_table_index);
Z
zgu 已提交
162

163
  if (string_table == NULL) {
Z
zgu 已提交
164 165
    m_status = NullDecoder::file_invalid;
    return false;
166 167
  }
  if (offset) *offset = off;
Z
zgu 已提交
168 169

  return string_table->string_at(pos_in_string_table, buf, buflen);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
}


void ElfFile::add_symbol_table(ElfSymbolTable* table) {
  if (m_symbol_tables == NULL) {
    m_symbol_tables = table;
  } else {
    table->m_next = m_symbol_tables;
    m_symbol_tables = table;
  }
}

void ElfFile::add_string_table(ElfStringTable* table) {
  if (m_string_tables == NULL) {
    m_string_tables = table;
  } else {
    table->m_next = m_string_tables;
    m_string_tables = table;
  }
}

ElfStringTable* ElfFile::get_string_table(int index) {
  ElfStringTable* p = m_string_tables;
  while (p != NULL) {
    if (p->index() == index) return p;
    p = p->m_next;
  }
  return NULL;
}

#endif // _WINDOWS