Path: blob/master/src/hotspot/share/utilities/elfStringTable.cpp
40949 views
/*1* Copyright (c) 1997, 2018, 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 "jvm.h"29#include "memory/allocation.inline.hpp"30#include "runtime/os.hpp"31#include "utilities/elfStringTable.hpp"3233// We will try to load whole string table into memory if we can.34// Otherwise, fallback to more expensive file operation.35ElfStringTable::ElfStringTable(FILE* const file, Elf_Shdr& shdr, int index) :36_next(NULL), _index(index), _section(file, shdr), _fd(file) {37_status = _section.status();38}3940ElfStringTable::~ElfStringTable() {41if (_next != NULL) {42delete _next;43}44}4546bool ElfStringTable::string_at(size_t pos, char* buf, int buflen) {47if (NullDecoder::is_error(get_status())) {48return false;49}5051assert(buflen > 0, "no buffer");52if (pos >= _section.section_header()->sh_size) {53return false;54}5556const char* data = (const char*)_section.section_data();57if (data != NULL) {58jio_snprintf(buf, buflen, "%s", data + pos);59return true;60} else { // no cache data, read from file instead61const Elf_Shdr* const shdr = _section.section_header();62MarkedFileReader mfd(_fd);63if (mfd.has_mark() &&64mfd.set_position(shdr->sh_offset + pos) &&65mfd.read((void*)buf, size_t(buflen))) {66buf[buflen - 1] = '\0';67return true;68} else {69// put it in error state to avoid retry70_status = NullDecoder::file_invalid;71return false;72}73}74}7576#endif // !_WINDOWS && !__APPLE__777879