Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/utilities/elfStringTable.cpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"2526#if !defined(_WINDOWS) && !defined(__APPLE__)2728#include "memory/allocation.inline.hpp"29#include "runtime/os.hpp"30#include "utilities/elfStringTable.hpp"3132// We will try to load whole string table into memory if we can.33// Otherwise, fallback to more expensive file operation.34ElfStringTable::ElfStringTable(FILE* file, Elf_Shdr shdr, int index) {35assert(file, "null file handle");36m_table = NULL;37m_index = index;38m_next = NULL;39m_file = file;40m_status = NullDecoder::no_error;4142// try to load the string table43long cur_offset = ftell(file);44m_table = (char*)os::malloc(sizeof(char) * shdr.sh_size, mtInternal);45if (m_table != NULL) {46// if there is an error, mark the error47if (fseek(file, shdr.sh_offset, SEEK_SET) ||48fread((void*)m_table, shdr.sh_size, 1, file) != 1 ||49fseek(file, cur_offset, SEEK_SET)) {50m_status = NullDecoder::file_invalid;51os::free((void*)m_table);52m_table = NULL;53}54} else {55memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr));56}57}5859ElfStringTable::~ElfStringTable() {60if (m_table != NULL) {61os::free((void*)m_table);62}6364if (m_next != NULL) {65delete m_next;66}67}6869bool ElfStringTable::string_at(int pos, char* buf, int buflen) {70if (NullDecoder::is_error(m_status)) {71return false;72}73if (m_table != NULL) {74jio_snprintf(buf, buflen, "%s", (const char*)(m_table + pos));75return true;76} else {77long cur_pos = ftell(m_file);78if (cur_pos == -1 ||79fseek(m_file, m_shdr.sh_offset + pos, SEEK_SET) ||80fread(buf, 1, buflen, m_file) <= 0 ||81fseek(m_file, cur_pos, SEEK_SET)) {82m_status = NullDecoder::file_invalid;83return false;84}85return true;86}87}8889#endif // !_WINDOWS && !__APPLE__909192