src/share/vm/utilities/elfFuncDescTable.cpp

changeset 0
f90c822e73f8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/elfFuncDescTable.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,104 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright 2012, 2013 SAP AG. All rights reserved.
     1.7 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8 + *
     1.9 + * This code is free software; you can redistribute it and/or modify it
    1.10 + * under the terms of the GNU General Public License version 2 only, as
    1.11 + * published by the Free Software Foundation.
    1.12 + *
    1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.15 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.16 + * version 2 for more details (a copy is included in the LICENSE file that
    1.17 + * accompanied this code).
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License version
    1.20 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.22 + *
    1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.24 + * or visit www.oracle.com if you need additional information or have any
    1.25 + * questions.
    1.26 + *
    1.27 + */
    1.28 +
    1.29 +#include "precompiled.hpp"
    1.30 +
    1.31 +#if !defined(_WINDOWS) && !defined(__APPLE__)
    1.32 +
    1.33 +#include "memory/allocation.inline.hpp"
    1.34 +#include "utilities/elfFuncDescTable.hpp"
    1.35 +
    1.36 +ElfFuncDescTable::ElfFuncDescTable(FILE* file, Elf_Shdr shdr, int index) {
    1.37 +  assert(file, "null file handle");
    1.38 +  // The actual function address (i.e. function entry point) is always the
    1.39 +  // first value in the function descriptor (on IA64 and PPC64 they look as follows):
    1.40 +  // PPC64: [function entry point, TOC pointer, environment pointer]
    1.41 +  // IA64 : [function entry point, GP (global pointer) value]
    1.42 +  // Unfortunately 'shdr.sh_entsize' doesn't always seem to contain this size (it's zero on PPC64) so we can't assert
    1.43 +  // assert(IA64_ONLY(2) PPC64_ONLY(3) * sizeof(address) == shdr.sh_entsize, "Size mismatch for '.opd' section entries");
    1.44 +
    1.45 +  m_funcDescs = NULL;
    1.46 +  m_file = file;
    1.47 +  m_index = index;
    1.48 +  m_status = NullDecoder::no_error;
    1.49 +
    1.50 +  // try to load the function descriptor table
    1.51 +  long cur_offset = ftell(file);
    1.52 +  if (cur_offset != -1) {
    1.53 +    // call malloc so we can back up if memory allocation fails.
    1.54 +    m_funcDescs = (address*)os::malloc(shdr.sh_size, mtInternal);
    1.55 +    if (m_funcDescs) {
    1.56 +      if (fseek(file, shdr.sh_offset, SEEK_SET) ||
    1.57 +          fread((void*)m_funcDescs, shdr.sh_size, 1, file) != 1 ||
    1.58 +          fseek(file, cur_offset, SEEK_SET)) {
    1.59 +        m_status = NullDecoder::file_invalid;
    1.60 +        os::free(m_funcDescs);
    1.61 +        m_funcDescs = NULL;
    1.62 +      }
    1.63 +    }
    1.64 +    if (!NullDecoder::is_error(m_status)) {
    1.65 +      memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr));
    1.66 +    }
    1.67 +  } else {
    1.68 +    m_status = NullDecoder::file_invalid;
    1.69 +  }
    1.70 +}
    1.71 +
    1.72 +ElfFuncDescTable::~ElfFuncDescTable() {
    1.73 +  if (m_funcDescs != NULL) {
    1.74 +    os::free(m_funcDescs);
    1.75 +  }
    1.76 +}
    1.77 +
    1.78 +address ElfFuncDescTable::lookup(Elf_Word index) {
    1.79 +  if (NullDecoder::is_error(m_status)) {
    1.80 +    return NULL;
    1.81 +  }
    1.82 +
    1.83 +  if (m_funcDescs != NULL) {
    1.84 +    if (m_shdr.sh_size > 0 && m_shdr.sh_addr <= index && index <= m_shdr.sh_addr + m_shdr.sh_size) {
    1.85 +      // Notice that 'index' is a byte-offset into the function descriptor table.
    1.86 +      return m_funcDescs[(index - m_shdr.sh_addr) / sizeof(address)];
    1.87 +    }
    1.88 +    return NULL;
    1.89 +  } else {
    1.90 +    long cur_pos;
    1.91 +    address addr;
    1.92 +    if (!(m_shdr.sh_size > 0 && m_shdr.sh_addr <= index && index <= m_shdr.sh_addr + m_shdr.sh_size)) {
    1.93 +      // don't put the whole decoder in error mode if we just tried a wrong index
    1.94 +      return NULL;
    1.95 +    }
    1.96 +    if ((cur_pos = ftell(m_file)) == -1 ||
    1.97 +        fseek(m_file, m_shdr.sh_offset + index - m_shdr.sh_addr, SEEK_SET) ||
    1.98 +        fread(&addr, sizeof(addr), 1, m_file) != 1 ||
    1.99 +        fseek(m_file, cur_pos, SEEK_SET)) {
   1.100 +      m_status = NullDecoder::file_invalid;
   1.101 +      return NULL;
   1.102 +    }
   1.103 +    return addr;
   1.104 +  }
   1.105 +}
   1.106 +
   1.107 +#endif // !_WINDOWS && !__APPLE__

mercurial