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