src/share/vm/utilities/elfStringTable.cpp

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/elfStringTable.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,90 @@
     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 +#include "precompiled.hpp"
    1.29 +
    1.30 +#if !defined(_WINDOWS) && !defined(__APPLE__)
    1.31 +
    1.32 +#include "memory/allocation.inline.hpp"
    1.33 +#include "runtime/os.hpp"
    1.34 +#include "utilities/elfStringTable.hpp"
    1.35 +
    1.36 +// We will try to load whole string table into memory if we can.
    1.37 +// Otherwise, fallback to more expensive file operation.
    1.38 +ElfStringTable::ElfStringTable(FILE* file, Elf_Shdr shdr, int index) {
    1.39 +  assert(file, "null file handle");
    1.40 +  m_table = NULL;
    1.41 +  m_index = index;
    1.42 +  m_next = NULL;
    1.43 +  m_file = file;
    1.44 +  m_status = NullDecoder::no_error;
    1.45 +
    1.46 +  // try to load the string table
    1.47 +  long cur_offset = ftell(file);
    1.48 +  m_table = (char*)os::malloc(sizeof(char) * shdr.sh_size, mtInternal);
    1.49 +  if (m_table != NULL) {
    1.50 +    // if there is an error, mark the error
    1.51 +    if (fseek(file, shdr.sh_offset, SEEK_SET) ||
    1.52 +      fread((void*)m_table, shdr.sh_size, 1, file) != 1 ||
    1.53 +      fseek(file, cur_offset, SEEK_SET)) {
    1.54 +      m_status = NullDecoder::file_invalid;
    1.55 +      os::free((void*)m_table);
    1.56 +      m_table = NULL;
    1.57 +    }
    1.58 +  } else {
    1.59 +    memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr));
    1.60 +  }
    1.61 +}
    1.62 +
    1.63 +ElfStringTable::~ElfStringTable() {
    1.64 +  if (m_table != NULL) {
    1.65 +    os::free((void*)m_table);
    1.66 +  }
    1.67 +
    1.68 +  if (m_next != NULL) {
    1.69 +    delete m_next;
    1.70 +  }
    1.71 +}
    1.72 +
    1.73 +bool ElfStringTable::string_at(int pos, char* buf, int buflen) {
    1.74 +  if (NullDecoder::is_error(m_status)) {
    1.75 +    return false;
    1.76 +  }
    1.77 +  if (m_table != NULL) {
    1.78 +    jio_snprintf(buf, buflen, "%s", (const char*)(m_table + pos));
    1.79 +    return true;
    1.80 +  } else {
    1.81 +    long cur_pos = ftell(m_file);
    1.82 +    if (cur_pos == -1 ||
    1.83 +      fseek(m_file, m_shdr.sh_offset + pos, SEEK_SET) ||
    1.84 +      fread(buf, 1, buflen, m_file) <= 0 ||
    1.85 +      fseek(m_file, cur_pos, SEEK_SET)) {
    1.86 +      m_status = NullDecoder::file_invalid;
    1.87 +      return false;
    1.88 +    }
    1.89 +    return true;
    1.90 +  }
    1.91 +}
    1.92 +
    1.93 +#endif // !_WINDOWS && !__APPLE__

mercurial