zgu@2364: /* mikael@4153: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. zgu@2364: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. zgu@2364: * zgu@2364: * This code is free software; you can redistribute it and/or modify it zgu@2364: * under the terms of the GNU General Public License version 2 only, as zgu@2364: * published by the Free Software Foundation. zgu@2364: * zgu@2364: * This code is distributed in the hope that it will be useful, but WITHOUT zgu@2364: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or zgu@2364: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License zgu@2364: * version 2 for more details (a copy is included in the LICENSE file that zgu@2364: * accompanied this code). zgu@2364: * zgu@2364: * You should have received a copy of the GNU General Public License version zgu@2364: * 2 along with this work; if not, write to the Free Software Foundation, zgu@2364: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. zgu@2364: * zgu@2364: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA zgu@2364: * or visit www.oracle.com if you need additional information or have any zgu@2364: * questions. zgu@2364: * zgu@2364: */ zgu@2364: zgu@2364: #include "precompiled.hpp" zgu@2364: never@3156: #if !defined(_WINDOWS) && !defined(__APPLE__) zgu@2364: zgu@2364: #include "memory/allocation.inline.hpp" zgu@2834: #include "runtime/os.hpp" zgu@2364: #include "utilities/elfStringTable.hpp" zgu@2364: zgu@2364: // We will try to load whole string table into memory if we can. zgu@2364: // Otherwise, fallback to more expensive file operation. zgu@2364: ElfStringTable::ElfStringTable(FILE* file, Elf_Shdr shdr, int index) { zgu@2364: assert(file, "null file handle"); zgu@2364: m_table = NULL; zgu@2364: m_index = index; zgu@2364: m_next = NULL; zgu@2364: m_file = file; zgu@3430: m_status = NullDecoder::no_error; zgu@2364: zgu@2364: // try to load the string table zgu@2364: long cur_offset = ftell(file); zgu@3900: m_table = (char*)os::malloc(sizeof(char) * shdr.sh_size, mtInternal); zgu@2364: if (m_table != NULL) { zgu@2364: // if there is an error, mark the error zgu@2364: if (fseek(file, shdr.sh_offset, SEEK_SET) || zgu@2364: fread((void*)m_table, shdr.sh_size, 1, file) != 1 || zgu@2364: fseek(file, cur_offset, SEEK_SET)) { zgu@3430: m_status = NullDecoder::file_invalid; zgu@2834: os::free((void*)m_table); zgu@2364: m_table = NULL; zgu@2364: } zgu@2364: } else { zgu@2364: memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr)); zgu@2364: } zgu@2364: } zgu@2364: zgu@2364: ElfStringTable::~ElfStringTable() { zgu@2364: if (m_table != NULL) { zgu@2834: os::free((void*)m_table); zgu@2364: } zgu@2364: zgu@2364: if (m_next != NULL) { zgu@2364: delete m_next; zgu@2364: } zgu@2364: } zgu@2364: zgu@3430: bool ElfStringTable::string_at(int pos, char* buf, int buflen) { zgu@3430: if (NullDecoder::is_error(m_status)) { zgu@3430: return false; zgu@2364: } zgu@2364: if (m_table != NULL) { zgu@3430: jio_snprintf(buf, buflen, "%s", (const char*)(m_table + pos)); zgu@3430: return true; zgu@2364: } else { zgu@2364: long cur_pos = ftell(m_file); zgu@2364: if (cur_pos == -1 || zgu@2364: fseek(m_file, m_shdr.sh_offset + pos, SEEK_SET) || zgu@3430: fread(buf, 1, buflen, m_file) <= 0 || zgu@2364: fseek(m_file, cur_pos, SEEK_SET)) { zgu@3430: m_status = NullDecoder::file_invalid; zgu@3430: return false; zgu@2364: } zgu@3430: return true; zgu@2364: } zgu@2364: } zgu@2364: zgu@2364: #endif // _WINDOWS