src/share/vm/utilities/elfStringTable.cpp

Sun, 25 Sep 2011 16:03:29 -0700

author
never
date
Sun, 25 Sep 2011 16:03:29 -0700
changeset 3156
f08d439fab8c
parent 2834
2a3da7eaf4a6
child 3430
d7e3846464d0
permissions
-rw-r--r--

7089790: integrate bsd-port changes
Reviewed-by: kvn, twisti, jrose
Contributed-by: Kurt Miller <kurt@intricatesoftware.com>, Greg Lewis <glewis@eyesbeyond.com>, Jung-uk Kim <jkim@freebsd.org>, Christos Zoulas <christos@zoulas.com>, Landon Fuller <landonf@plausible.coop>, The FreeBSD Foundation <board@freebsdfoundation.org>, Michael Franz <mvfranz@gmail.com>, Roger Hoover <rhoover@apple.com>, Alexander Strange <astrange@apple.com>

     1 /*
     2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    27 #if !defined(_WINDOWS) && !defined(__APPLE__)
    29 #include "memory/allocation.inline.hpp"
    30 #include "runtime/os.hpp"
    31 #include "utilities/elfStringTable.hpp"
    33 // We will try to load whole string table into memory if we can.
    34 // Otherwise, fallback to more expensive file operation.
    35 ElfStringTable::ElfStringTable(FILE* file, Elf_Shdr shdr, int index) {
    36   assert(file, "null file handle");
    37   m_table = NULL;
    38   m_index = index;
    39   m_next = NULL;
    40   m_file = file;
    41   m_status = Decoder::no_error;
    43   // try to load the string table
    44   long cur_offset = ftell(file);
    45   m_table = (char*)os::malloc(sizeof(char) * shdr.sh_size);
    46   if (m_table != NULL) {
    47     // if there is an error, mark the error
    48     if (fseek(file, shdr.sh_offset, SEEK_SET) ||
    49       fread((void*)m_table, shdr.sh_size, 1, file) != 1 ||
    50       fseek(file, cur_offset, SEEK_SET)) {
    51       m_status = Decoder::file_invalid;
    52       os::free((void*)m_table);
    53       m_table = NULL;
    54     }
    55   } else {
    56     memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr));
    57   }
    58 }
    60 ElfStringTable::~ElfStringTable() {
    61   if (m_table != NULL) {
    62     os::free((void*)m_table);
    63   }
    65   if (m_next != NULL) {
    66     delete m_next;
    67   }
    68 }
    70 const char* ElfStringTable::string_at(int pos) {
    71   if (m_status != Decoder::no_error) {
    72     return NULL;
    73   }
    74   if (m_table != NULL) {
    75     return (const char*)(m_table + pos);
    76   } else {
    77     long cur_pos = ftell(m_file);
    78     if (cur_pos == -1 ||
    79       fseek(m_file, m_shdr.sh_offset + pos, SEEK_SET) ||
    80       fread(m_symbol, 1, MAX_SYMBOL_LEN, m_file) <= 0 ||
    81       fseek(m_file, cur_pos, SEEK_SET)) {
    82       m_status = Decoder::file_invalid;
    83       return NULL;
    84     }
    85     return (const char*)m_symbol;
    86   }
    87 }
    89 #endif // _WINDOWS

mercurial