elfStringTable.cpp revision 3465:d2a62e0f25eb
11553Srgrimes/*
21553Srgrimes * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
31553Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41553Srgrimes *
51553Srgrimes * This code is free software; you can redistribute it and/or modify it
61553Srgrimes * under the terms of the GNU General Public License version 2 only, as
71553Srgrimes * published by the Free Software Foundation.
81553Srgrimes *
91553Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101553Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111553Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121553Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131553Srgrimes * accompanied this code).
141553Srgrimes *
151553Srgrimes * You should have received a copy of the GNU General Public License version
161553Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171553Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181553Srgrimes *
191553Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201553Srgrimes * or visit www.oracle.com if you need additional information or have any
211553Srgrimes * questions.
221553Srgrimes *
231553Srgrimes */
241553Srgrimes
251553Srgrimes#include "precompiled.hpp"
261553Srgrimes
271553Srgrimes#if !defined(_WINDOWS) && !defined(__APPLE__)
281553Srgrimes
291553Srgrimes#include "memory/allocation.inline.hpp"
301553Srgrimes#include "runtime/os.hpp"
311553Srgrimes#include "utilities/elfStringTable.hpp"
321553Srgrimes
331553Srgrimes// We will try to load whole string table into memory if we can.
341553Srgrimes// Otherwise, fallback to more expensive file operation.
351553SrgrimesElfStringTable::ElfStringTable(FILE* file, Elf_Shdr shdr, int index) {
361553Srgrimes  assert(file, "null file handle");
371553Srgrimes  m_table = NULL;
3813977Sphk  m_index = index;
391553Srgrimes  m_next = NULL;
401553Srgrimes  m_file = file;
411553Srgrimes  m_status = NullDecoder::no_error;
421553Srgrimes
431553Srgrimes  // try to load the string table
4429060Scharnier  long cur_offset = ftell(file);
4513977Sphk  m_table = (char*)os::malloc(sizeof(char) * shdr.sh_size, mtInternal);
4629060Scharnier  if (m_table != NULL) {
4729060Scharnier    // if there is an error, mark the error
4850479Speter    if (fseek(file, shdr.sh_offset, SEEK_SET) ||
491553Srgrimes      fread((void*)m_table, shdr.sh_size, 1, file) != 1 ||
501553Srgrimes      fseek(file, cur_offset, SEEK_SET)) {
511553Srgrimes      m_status = NullDecoder::file_invalid;
521553Srgrimes      os::free((void*)m_table);
531553Srgrimes      m_table = NULL;
541553Srgrimes    }
551553Srgrimes  } else {
561553Srgrimes    memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr));
571553Srgrimes  }
581553Srgrimes}
5913977Sphk
601553SrgrimesElfStringTable::~ElfStringTable() {
6113977Sphk  if (m_table != NULL) {
6220287Swollman    os::free((void*)m_table);
631553Srgrimes  }
641553Srgrimes
651553Srgrimes  if (m_next != NULL) {
661553Srgrimes    delete m_next;
6796235Swpaul  }
6896202Skbyanc}
691553Srgrimes
701553Srgrimesbool ElfStringTable::string_at(int pos, char* buf, int buflen) {
711553Srgrimes  if (NullDecoder::is_error(m_status)) {
721553Srgrimes    return false;
731553Srgrimes  }
741553Srgrimes  if (m_table != NULL) {
75108314Sru    jio_snprintf(buf, buflen, "%s", (const char*)(m_table + pos));
7629060Scharnier    return true;
7729060Scharnier  } else {
781553Srgrimes    long cur_pos = ftell(m_file);
791553Srgrimes    if (cur_pos == -1 ||
8029060Scharnier      fseek(m_file, m_shdr.sh_offset + pos, SEEK_SET) ||
811553Srgrimes      fread(buf, 1, buflen, m_file) <= 0 ||
8213977Sphk      fseek(m_file, cur_pos, SEEK_SET)) {
8393590Smike      m_status = NullDecoder::file_invalid;
8429060Scharnier      return false;
8513977Sphk    }
861553Srgrimes    return true;
8731145Sjulian  }
8831145Sjulian}
8931145Sjulian
9087598Smikeh#endif // _WINDOWS
9131145Sjulian