src/java.base/share/native/libjimage/imageFile.cpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 39759 jdk/src/java.base/share/native/libjimage/imageFile.cpp@427916042881
child 48565 871b8bb201ea
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
     1
/*
39759
427916042881 8161718: Copyright/License updates to corba, jdk
bchristi
parents: 36511
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
35784
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
     3
 *
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
     5
 * modification, are permitted provided that the following conditions
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
     6
 * are met:
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
     7
 *
35784
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    10
 *
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    14
 *
35784
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    17
 *     from this software without specific prior written permission.
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    18
 *
35784
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ddeed53a1c5a 8149776: BSD license for jimage code
jlaskey
parents: 34386
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    30
 */
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    31
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    32
#include <assert.h>
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    33
#include <string.h>
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    34
#include <stdlib.h>
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    35
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    36
#include "endian.hpp"
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    37
#include "imageDecompressor.hpp"
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    38
#include "imageFile.hpp"
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    39
#include "inttypes.hpp"
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    40
#include "jni.h"
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    41
#include "osSupport.hpp"
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    42
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    43
// Map the full jimage, only with 64 bit addressing.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
    44
bool ImageFileReader::memory_map_image = sizeof(void *) == 8;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    45
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    46
#ifdef WIN32
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    47
const char FileSeparator = '\\';
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    48
#else
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    49
const char FileSeparator = '/';
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    50
#endif
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    51
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    52
// Image files are an alternate file format for storing classes and resources. The
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    53
// goal is to supply file access which is faster and smaller than the jar format.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    54
//
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    55
// (More detailed nodes in the header.)
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    56
//
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    57
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    58
// Compute the Perfect Hashing hash code for the supplied UTF-8 string.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    59
s4 ImageStrings::hash_code(const char* string, s4 seed) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    60
    // Access bytes as unsigned.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    61
    u1* bytes = (u1*)string;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    62
    // Compute hash code.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    63
    for (u1 byte = *bytes++; byte; byte = *bytes++) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    64
        seed = (seed * HASH_MULTIPLIER) ^ byte;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    65
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    66
    // Ensure the result is not signed.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    67
    return seed & 0x7FFFFFFF;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    68
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    69
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    70
// Match up a string in a perfect hash table.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    71
// Returns the index where the name should be.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    72
// Result still needs validation for precise match (false positive.)
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    73
s4 ImageStrings::find(Endian* endian, const char* name, s4* redirect, u4 length) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    74
    // If the table is empty, then short cut.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    75
    if (!redirect || !length) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    76
        return NOT_FOUND;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    77
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    78
    // Compute the basic perfect hash for name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    79
    s4 hash_code = ImageStrings::hash_code(name);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    80
    // Modulo table size.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    81
    s4 index = hash_code % length;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    82
    // Get redirect entry.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    83
    //   value == 0 then not found
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    84
    //   value < 0 then -1 - value is true index
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    85
    //   value > 0 then value is seed for recomputing hash.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    86
    s4 value = endian->get(redirect[index]);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    87
    // if recompute is required.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    88
    if (value > 0 ) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    89
        // Entry collision value, need to recompute hash.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    90
        hash_code = ImageStrings::hash_code(name, value);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    91
        // Modulo table size.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    92
        return hash_code % length;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    93
    } else if (value < 0) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    94
        // Compute direct index.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    95
        return -1 - value;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    96
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    97
    // No entry found.
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    98
    return NOT_FOUND;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    99
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   100
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   101
// Test to see if UTF-8 string begins with the start UTF-8 string.  If so,
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   102
// return non-NULL address of remaining portion of string.  Otherwise, return
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   103
// NULL.    Used to test sections of a path without copying from image string
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   104
// table.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   105
const char* ImageStrings::starts_with(const char* string, const char* start) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   106
    char ch1, ch2;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   107
    // Match up the strings the best we can.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   108
    while ((ch1 = *string) && (ch2 = *start)) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   109
        if (ch1 != ch2) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   110
            // Mismatch, return NULL.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   111
            return NULL;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   112
        }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   113
        // Next characters.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   114
        string++, start++;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   115
    }
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   116
    // Return remainder of string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   117
    return string;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   118
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   119
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   120
// Inflates the attribute stream into individual values stored in the long
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   121
// array _attributes. This allows an attribute value to be quickly accessed by
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   122
// direct indexing.  Unspecified values default to zero (from constructor.)
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   123
void ImageLocation::set_data(u1* data) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   124
    // Deflate the attribute stream into an array of attributes.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   125
    u1 byte;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   126
    // Repeat until end header is found.
33645
1af4c728e3e8 8140649: imageFile should use delete[] with new[]
asmotrak
parents: 32757
diff changeset
   127
    while ((data != NULL) && (byte = *data)) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   128
        // Extract kind from header byte.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   129
        u1 kind = attribute_kind(byte);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   130
        assert(kind < ATTRIBUTE_COUNT && "invalid image location attribute");
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   131
        // Extract length of data (in bytes).
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   132
        u1 n = attribute_length(byte);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   133
        // Read value (most significant first.)
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   134
        _attributes[kind] = attribute_value(data + 1, n);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   135
        // Position to next attribute by skipping attribute header and data bytes.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   136
        data += n + 1;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   137
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   138
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   139
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   140
// Zero all attribute values.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   141
void ImageLocation::clear_data() {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   142
    // Set defaults to zero.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   143
    memset(_attributes, 0, sizeof(_attributes));
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   144
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   145
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   146
// ImageModuleData constructor maps out sub-tables for faster access.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   147
ImageModuleData::ImageModuleData(const ImageFileReader* image_file) :
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   148
        _image_file(image_file),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   149
        _endian(image_file->endian()) {
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   150
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   151
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   152
// Release module data resource.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   153
ImageModuleData::~ImageModuleData() {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   154
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   155
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   156
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   157
// Return the module in which a package resides.    Returns NULL if not found.
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   158
const char* ImageModuleData::package_to_module(const char* package_name) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   159
    // replace all '/' by '.'
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   160
    char* replaced = new char[(int) strlen(package_name) + 1];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   161
    assert(replaced != NULL && "allocation failed");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   162
    int i;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   163
    for (i = 0; package_name[i] != '\0'; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   164
      replaced[i] = package_name[i] == '/' ? '.' : package_name[i];
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   165
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   166
    replaced[i] = '\0';
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   167
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   168
    // build path /packages/<package_name>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   169
    const char* radical = "/packages/";
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   170
    char* path = new char[(int) strlen(radical) + (int) strlen(package_name) + 1];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   171
    assert(path != NULL && "allocation failed");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   172
    strcpy(path, radical);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   173
    strcat(path, replaced);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   174
    delete[] replaced;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   175
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   176
    // retrieve package location
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   177
    ImageLocation location;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   178
    bool found = _image_file->find_location(path, location);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   179
    if (!found) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   180
        delete[] path;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   181
        return NULL;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   182
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   183
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   184
    // retrieve offsets to module name
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   185
    int size = (int)location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   186
    u1* content = new u1[size];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   187
    assert(content != NULL && "allocation failed");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   188
    _image_file->get_resource(location, content);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   189
    u1* ptr = content;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   190
    // sequence of sizeof(8) isEmpty|offset. Use the first module that is not empty.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   191
    u4 offset = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   192
    for (i = 0; i < size; i+=8) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   193
        u4 isEmpty = _endian->get(*((u4*)ptr));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   194
        ptr += 4;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   195
        if (!isEmpty) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   196
            offset = _endian->get(*((u4*)ptr));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   197
            break;
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   198
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   199
        ptr += 4;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   200
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   201
    delete[] content;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   202
    return _image_file->get_strings().get(offset);
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   203
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   204
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   205
// Manage a table of open image files.  This table allows multiple access points
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   206
// to share an open image.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   207
ImageFileReaderTable::ImageFileReaderTable() : _count(0), _max(_growth) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   208
    _table = new ImageFileReader*[_max];
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   209
    assert(_table != NULL && "allocation failed");
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   210
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   211
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   212
ImageFileReaderTable::~ImageFileReaderTable() {
33645
1af4c728e3e8 8140649: imageFile should use delete[] with new[]
asmotrak
parents: 32757
diff changeset
   213
    delete[] _table;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   214
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   215
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   216
// Add a new image entry to the table.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   217
void ImageFileReaderTable::add(ImageFileReader* image) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   218
    if (_count == _max) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   219
        _max += _growth;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   220
        _table = static_cast<ImageFileReader**>(realloc(_table, _max * sizeof(ImageFileReader*)));
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   221
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   222
    _table[_count++] = image;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   223
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   224
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   225
// Remove an image entry from the table.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   226
void ImageFileReaderTable::remove(ImageFileReader* image) {
34386
80c21b82e467 8143879: jdk/internal/jimage/JImageReadTest.java crashes jvm in msvcr120.dll
rriggs
parents: 33825
diff changeset
   227
    for (u4 i = 0; i < _count; i++) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   228
        if (_table[i] == image) {
34386
80c21b82e467 8143879: jdk/internal/jimage/JImageReadTest.java crashes jvm in msvcr120.dll
rriggs
parents: 33825
diff changeset
   229
            // Swap the last element into the found slot
80c21b82e467 8143879: jdk/internal/jimage/JImageReadTest.java crashes jvm in msvcr120.dll
rriggs
parents: 33825
diff changeset
   230
            _table[i] = _table[--_count];
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   231
            break;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   232
        }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   233
    }
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   234
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   235
    if (_count != 0 && _count == _max - _growth) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   236
        _max -= _growth;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   237
        _table = static_cast<ImageFileReader**>(realloc(_table, _max * sizeof(ImageFileReader*)));
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   238
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   239
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   240
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   241
// Determine if image entry is in table.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   242
bool ImageFileReaderTable::contains(ImageFileReader* image) {
34386
80c21b82e467 8143879: jdk/internal/jimage/JImageReadTest.java crashes jvm in msvcr120.dll
rriggs
parents: 33825
diff changeset
   243
    for (u4 i = 0; i < _count; i++) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   244
        if (_table[i] == image) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   245
            return true;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   246
        }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   247
    }
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   248
    return false;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   249
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   250
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   251
// Table to manage multiple opens of an image file.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   252
ImageFileReaderTable ImageFileReader::_reader_table;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   253
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   254
SimpleCriticalSection _reader_table_lock;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   255
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   256
// Locate an image if file already open.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   257
ImageFileReader* ImageFileReader::find_image(const char* name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   258
    // Lock out _reader_table.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   259
    SimpleCriticalSectionLock cs(&_reader_table_lock);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   260
    // Search for an exist image file.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   261
    for (u4 i = 0; i < _reader_table.count(); i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   262
        // Retrieve table entry.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   263
        ImageFileReader* reader = _reader_table.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   264
        // If name matches, then reuse (bump up use count.)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   265
        assert(reader->name() != NULL && "reader->name must not be null");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   266
        if (strcmp(reader->name(), name) == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   267
            reader->inc_use();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   268
            return reader;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   269
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   270
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   271
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   272
    return NULL;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   273
}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   274
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   275
// Open an image file, reuse structure if file already open.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   276
ImageFileReader* ImageFileReader::open(const char* name, bool big_endian) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   277
    ImageFileReader* reader = find_image(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   278
    if (reader != NULL) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   279
        return reader;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   280
    }
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   281
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   282
    // Need a new image reader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   283
    reader = new ImageFileReader(name, big_endian);
33825
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   284
    if (reader == NULL || !reader->open()) {
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   285
        // Failed to open.
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   286
        delete reader;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   287
        return NULL;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   288
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   289
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   290
    // Lock to update
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   291
    SimpleCriticalSectionLock cs(&_reader_table_lock);
33825
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   292
    // Search for an existing image file.
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   293
    for (u4 i = 0; i < _reader_table.count(); i++) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   294
        // Retrieve table entry.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   295
        ImageFileReader* existing_reader = _reader_table.get(i);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   296
        // If name matches, then reuse (bump up use count.)
33825
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   297
        assert(reader->name() != NULL && "reader->name still must not be null");
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   298
        if (strcmp(existing_reader->name(), name) == 0) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   299
            existing_reader->inc_use();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   300
            reader->close();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   301
            delete reader;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   302
            return existing_reader;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   303
        }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   304
    }
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   305
    // Bump use count and add to table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   306
    reader->inc_use();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   307
    _reader_table.add(reader);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   308
    return reader;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   309
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   310
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   311
// Close an image file if the file is not in use elsewhere.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   312
void ImageFileReader::close(ImageFileReader *reader) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   313
    // Lock out _reader_table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   314
    SimpleCriticalSectionLock cs(&_reader_table_lock);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   315
    // If last use then remove from table and then close.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   316
    if (reader->dec_use()) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   317
        _reader_table.remove(reader);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   318
        delete reader;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   319
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   320
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   321
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   322
// Return an id for the specifed ImageFileReader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   323
u8 ImageFileReader::reader_to_ID(ImageFileReader *reader) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   324
    // ID is just the cloaked reader address.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   325
    return (u8)reader;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   326
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   327
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   328
// Validate the image id.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   329
bool ImageFileReader::id_check(u8 id) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   330
    // Make sure the ID is a managed (_reader_table) reader.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   331
    SimpleCriticalSectionLock cs(&_reader_table_lock);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   332
    return _reader_table.contains((ImageFileReader*)id);
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   333
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   334
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   335
// Return an id for the specifed ImageFileReader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   336
ImageFileReader* ImageFileReader::id_to_reader(u8 id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   337
    assert(id_check(id) && "invalid image id");
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   338
    return (ImageFileReader*)id;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   339
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   340
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   341
// Constructor intializes to a closed state.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   342
ImageFileReader::ImageFileReader(const char* name, bool big_endian) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   343
    // Copy the image file name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   344
     int len = (int) strlen(name) + 1;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   345
    _name = new char[len];
33825
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   346
    assert(_name != NULL  && "allocation failed");
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   347
    strncpy(_name, name, len);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   348
    // Initialize for a closed file.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   349
    _fd = -1;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   350
    _endian = Endian::get_handler(big_endian);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   351
    _index_data = NULL;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   352
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   353
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   354
// Close image and free up data structures.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   355
ImageFileReader::~ImageFileReader() {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   356
    // Ensure file is closed.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   357
    close();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   358
    // Free up name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   359
    if (_name) {
33645
1af4c728e3e8 8140649: imageFile should use delete[] with new[]
asmotrak
parents: 32757
diff changeset
   360
        delete[] _name;
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   361
        _name = NULL;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   362
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   363
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   364
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   365
// Open image file for read access.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   366
bool ImageFileReader::open() {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   367
    // If file exists open for reading.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   368
    _fd = osSupport::openReadOnly(_name);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   369
    if (_fd == -1) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   370
        return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   371
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   372
    // Retrieve the file size.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   373
    _file_size = osSupport::size(_name);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   374
    // Read image file header and verify it has a valid header.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   375
    size_t header_size = sizeof(ImageHeader);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   376
    if (_file_size < header_size ||
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   377
        !read_at((u1*)&_header, header_size, 0) ||
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   378
        _header.magic(_endian) != IMAGE_MAGIC ||
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   379
        _header.major_version(_endian) != MAJOR_VERSION ||
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   380
        _header.minor_version(_endian) != MINOR_VERSION) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   381
        close();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   382
        return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   383
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   384
    // Size of image index.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   385
    _index_size = index_size();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   386
    // Make sure file is large enough to contain the index.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   387
    if (_file_size < _index_size) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   388
        return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   389
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   390
    // Memory map image (minimally the index.)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   391
    _index_data = (u1*)osSupport::map_memory(_fd, _name, 0, (size_t)map_size());
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   392
    assert(_index_data && "image file not memory mapped");
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   393
    // Retrieve length of index perfect hash table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   394
    u4 length = table_length();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   395
    // Compute offset of the perfect hash table redirect table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   396
    u4 redirect_table_offset = (u4)header_size;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   397
    // Compute offset of index attribute offsets.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   398
    u4 offsets_table_offset = redirect_table_offset + length * (u4)sizeof(s4);
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   399
    // Compute offset of index location attribute data.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   400
    u4 location_bytes_offset = offsets_table_offset + length * (u4)sizeof(u4);
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   401
    // Compute offset of index string table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   402
    u4 string_bytes_offset = location_bytes_offset + locations_size();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   403
    // Compute address of the perfect hash table redirect table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   404
    _redirect_table = (s4*)(_index_data + redirect_table_offset);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   405
    // Compute address of index attribute offsets.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   406
    _offsets_table = (u4*)(_index_data + offsets_table_offset);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   407
    // Compute address of index location attribute data.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   408
    _location_bytes = _index_data + location_bytes_offset;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   409
    // Compute address of index string table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   410
    _string_bytes = _index_data + string_bytes_offset;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   411
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   412
    // Initialize the module data
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   413
    module_data = new ImageModuleData(this);
33825
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   414
    // Successful open (if memory allocation succeeded).
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   415
    return module_data != NULL;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   416
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   417
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   418
// Close image file.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   419
void ImageFileReader::close() {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   420
    // Deallocate the index.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   421
    if (_index_data) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   422
        osSupport::unmap_memory((char*)_index_data, _index_size);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   423
        _index_data = NULL;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   424
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   425
    // Close file.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   426
    if (_fd != -1) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   427
        osSupport::close(_fd);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   428
        _fd = -1;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   429
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   430
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   431
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   432
// Read directly from the file.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   433
bool ImageFileReader::read_at(u1* data, u8 size, u8 offset) const {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   434
    return (u8)osSupport::read(_fd, (char*)data, size, offset) == size;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   435
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   436
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   437
// Find the location attributes associated with the path.    Returns true if
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   438
// the location is found, false otherwise.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   439
bool ImageFileReader::find_location(const char* path, ImageLocation& location) const {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   440
    // Locate the entry in the index perfect hash table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   441
    s4 index = ImageStrings::find(_endian, path, _redirect_table, table_length());
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   442
    // If is found.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   443
    if (index != ImageStrings::NOT_FOUND) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   444
        // Get address of first byte of location attribute stream.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   445
        u1* data = get_location_data(index);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   446
        // Expand location attributes.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   447
        location.set_data(data);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   448
        // Make sure result is not a false positive.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   449
        return verify_location(location, path);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   450
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   451
    return false;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   452
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   453
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   454
// Find the location index and size associated with the path.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   455
// Returns the location index and size if the location is found, 0 otherwise.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   456
u4 ImageFileReader::find_location_index(const char* path, u8 *size) const {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   457
    // Locate the entry in the index perfect hash table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   458
    s4 index = ImageStrings::find(_endian, path, _redirect_table, table_length());
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   459
    // If found.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   460
    if (index != ImageStrings::NOT_FOUND) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   461
        // Get address of first byte of location attribute stream.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   462
        u4 offset = get_location_offset(index);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   463
        u1* data = get_location_offset_data(offset);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   464
        // Expand location attributes.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   465
        ImageLocation location(data);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   466
        // Make sure result is not a false positive.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   467
        if (verify_location(location, path)) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   468
                *size = (jlong)location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   469
                return offset;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   470
        }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   471
    }
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   472
    return 0;            // not found
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   473
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   474
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   475
// Assemble the location path from the string fragments indicated in the location attributes.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   476
void ImageFileReader::location_path(ImageLocation& location, char* path, size_t max) const {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   477
    // Manage the image string table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   478
    ImageStrings strings(_string_bytes, _header.strings_size(_endian));
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   479
    // Position to first character of the path buffer.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   480
    char* next = path;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   481
    // Temp for string length.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   482
    size_t length;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   483
    // Get module string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   484
    const char* module = location.get_attribute(ImageLocation::ATTRIBUTE_MODULE, strings);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   485
    // If module string is not empty string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   486
    if (*module != '\0') {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   487
        // Get length of module name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   488
        length = strlen(module);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   489
        // Make sure there is no buffer overflow.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   490
        assert(next - path + length + 2 < max && "buffer overflow");
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   491
        // Append '/module/'.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   492
        *next++ = '/';
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   493
        strncpy(next, module, length); next += length;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   494
        *next++ = '/';
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   495
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   496
    // Get parent (package) string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   497
    const char* parent = location.get_attribute(ImageLocation::ATTRIBUTE_PARENT, strings);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   498
    // If parent string is not empty string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   499
    if (*parent != '\0') {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   500
        // Get length of module string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   501
        length = strlen(parent);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   502
        // Make sure there is no buffer overflow.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   503
        assert(next - path + length + 1 < max && "buffer overflow");
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   504
        // Append 'patent/' .
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   505
        strncpy(next, parent, length); next += length;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   506
        *next++ = '/';
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   507
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   508
    // Get base name string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   509
    const char* base = location.get_attribute(ImageLocation::ATTRIBUTE_BASE, strings);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   510
    // Get length of base name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   511
    length = strlen(base);
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   512
    // Make sure there is no buffer overflow.
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   513
    assert(next - path + length < max && "buffer overflow");
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   514
    // Append base name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   515
    strncpy(next, base, length); next += length;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   516
    // Get extension string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   517
    const char* extension = location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION, strings);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   518
    // If extension string is not empty string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   519
    if (*extension != '\0') {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   520
        // Get length of extension string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   521
        length = strlen(extension);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   522
        // Make sure there is no buffer overflow.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   523
        assert(next - path + length + 1 < max && "buffer overflow");
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   524
        // Append '.extension' .
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   525
        *next++ = '.';
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   526
        strncpy(next, extension, length); next += length;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   527
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   528
    // Make sure there is no buffer overflow.
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   529
    assert((size_t)(next - path) < max && "buffer overflow");
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   530
    // Terminate string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   531
    *next = '\0';
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   532
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   533
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   534
// Verify that a found location matches the supplied path (without copying.)
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   535
bool ImageFileReader::verify_location(ImageLocation& location, const char* path) const {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   536
    // Manage the image string table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   537
    ImageStrings strings(_string_bytes, _header.strings_size(_endian));
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   538
    // Position to first character of the path string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   539
    const char* next = path;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   540
    // Get module name string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   541
    const char* module = location.get_attribute(ImageLocation::ATTRIBUTE_MODULE, strings);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   542
    // If module string is not empty.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   543
    if (*module != '\0') {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   544
        // Compare '/module/' .
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   545
        if (*next++ != '/') return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   546
        if (!(next = ImageStrings::starts_with(next, module))) return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   547
        if (*next++ != '/') return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   548
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   549
    // Get parent (package) string
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   550
    const char* parent = location.get_attribute(ImageLocation::ATTRIBUTE_PARENT, strings);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   551
    // If parent string is not empty string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   552
    if (*parent != '\0') {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   553
        // Compare 'parent/' .
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   554
        if (!(next = ImageStrings::starts_with(next, parent))) return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   555
        if (*next++ != '/') return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   556
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   557
    // Get base name string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   558
    const char* base = location.get_attribute(ImageLocation::ATTRIBUTE_BASE, strings);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   559
    // Compare with basne name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   560
    if (!(next = ImageStrings::starts_with(next, base))) return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   561
    // Get extension string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   562
    const char* extension = location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION, strings);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   563
    // If extension is not empty.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   564
    if (*extension != '\0') {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   565
        // Compare '.extension' .
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   566
        if (*next++ != '.') return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   567
        if (!(next = ImageStrings::starts_with(next, extension))) return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   568
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   569
    // True only if complete match and no more characters.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   570
    return *next == '\0';
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   571
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   572
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   573
// Return the resource for the supplied location offset.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   574
void ImageFileReader::get_resource(u4 offset, u1* uncompressed_data) const {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   575
        // Get address of first byte of location attribute stream.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   576
        u1* data = get_location_offset_data(offset);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   577
        // Expand location attributes.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   578
        ImageLocation location(data);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   579
        // Read the data
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   580
        get_resource(location, uncompressed_data);
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   581
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   582
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   583
// Return the resource for the supplied location.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   584
void ImageFileReader::get_resource(ImageLocation& location, u1* uncompressed_data) const {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   585
    // Retrieve the byte offset and size of the resource.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   586
    u8 offset = location.get_attribute(ImageLocation::ATTRIBUTE_OFFSET);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   587
    u8 uncompressed_size = location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   588
    u8 compressed_size = location.get_attribute(ImageLocation::ATTRIBUTE_COMPRESSED);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   589
    // If the resource is compressed.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   590
    if (compressed_size != 0) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   591
        u1* compressed_data;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   592
        // If not memory mapped read in bytes.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   593
        if (!memory_map_image) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   594
            // Allocate buffer for compression.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   595
            compressed_data = new u1[(size_t)compressed_size];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   596
            assert(compressed_data != NULL && "allocation failed");
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   597
            // Read bytes from offset beyond the image index.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   598
            bool is_read = read_at(compressed_data, compressed_size, _index_size + offset);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   599
            assert(is_read && "error reading from image or short read");
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   600
        } else {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   601
            compressed_data = get_data_address() + offset;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   602
        }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   603
        // Get image string table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   604
        const ImageStrings strings = get_strings();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   605
        // Decompress resource.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   606
        ImageDecompressor::decompress_resource(compressed_data, uncompressed_data, uncompressed_size,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   607
                        &strings, _endian);
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   608
        // If not memory mapped then release temporary buffer.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   609
        if (!memory_map_image) {
33645
1af4c728e3e8 8140649: imageFile should use delete[] with new[]
asmotrak
parents: 32757
diff changeset
   610
                delete[] compressed_data;
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   611
        }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   612
    } else {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   613
        // Read bytes from offset beyond the image index.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   614
        bool is_read = read_at(uncompressed_data, uncompressed_size, _index_size + offset);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   615
        assert(is_read && "error reading from image or short read");
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   616
    }
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   617
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   618
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   619
// Return the ImageModuleData for this image
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   620
ImageModuleData * ImageFileReader::get_image_module_data() {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   621
        return module_data;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   622
}