src/java.base/share/native/libjimage/imageFile.cpp
author mikael
Wed, 20 Feb 2019 09:43:01 -0800
changeset 53851 cc4f5bf6b26b
parent 52920 100818c5ddd0
permissions -rw-r--r--
8219142: Remove unused JIMAGE_ResourcePath Reviewed-by: dholmes, alanb
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
/*
53851
cc4f5bf6b26b 8219142: Remove unused JIMAGE_ResourcePath
mikael
parents: 52920
diff changeset
     2
 * Copyright (c) 2015, 2019, 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) {
50125
2d9dd2b876a0 8203223: Signed integer overflow in ImageStrings::hash_code (libjimage.so)
sgehwolf
parents: 48565
diff changeset
    60
    assert(seed > 0 && "invariant");
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    61
    // Access bytes as unsigned.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    62
    u1* bytes = (u1*)string;
50125
2d9dd2b876a0 8203223: Signed integer overflow in ImageStrings::hash_code (libjimage.so)
sgehwolf
parents: 48565
diff changeset
    63
    u4 useed = (u4)seed;
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    64
    // Compute hash code.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    65
    for (u1 byte = *bytes++; byte; byte = *bytes++) {
50125
2d9dd2b876a0 8203223: Signed integer overflow in ImageStrings::hash_code (libjimage.so)
sgehwolf
parents: 48565
diff changeset
    66
        useed = (useed * HASH_MULTIPLIER) ^ byte;
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    67
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    68
    // Ensure the result is not signed.
50125
2d9dd2b876a0 8203223: Signed integer overflow in ImageStrings::hash_code (libjimage.so)
sgehwolf
parents: 48565
diff changeset
    69
    return (s4)(useed & 0x7FFFFFFF);
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    70
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    71
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    72
// 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
    73
// Returns the index where the name should be.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    74
// 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
    75
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
    76
    // 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
    77
    if (!redirect || !length) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    78
        return NOT_FOUND;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    79
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    80
    // Compute the basic perfect hash for name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    81
    s4 hash_code = ImageStrings::hash_code(name);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    82
    // Modulo table size.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    83
    s4 index = hash_code % length;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    84
    // Get redirect entry.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    85
    //   value == 0 then not found
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    86
    //   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
    87
    //   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
    88
    s4 value = endian->get(redirect[index]);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    89
    // if recompute is required.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    90
    if (value > 0 ) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    91
        // Entry collision value, need to recompute hash.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    92
        hash_code = ImageStrings::hash_code(name, value);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    93
        // Modulo table size.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    94
        return hash_code % length;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    95
    } else if (value < 0) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    96
        // Compute direct index.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    97
        return -1 - value;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    98
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    99
    // No entry found.
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   100
    return NOT_FOUND;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   101
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   102
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   103
// 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
   104
// 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
   105
// 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
   106
// table.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   107
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
   108
    char ch1, ch2;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   109
    // 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
   110
    while ((ch1 = *string) && (ch2 = *start)) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   111
        if (ch1 != ch2) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   112
            // Mismatch, return NULL.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   113
            return NULL;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   114
        }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   115
        // Next characters.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   116
        string++, start++;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   117
    }
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   118
    // Return remainder of string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   119
    return string;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   120
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   121
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   122
// 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
   123
// 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
   124
// 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
   125
void ImageLocation::set_data(u1* data) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   126
    // 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
   127
    u1 byte;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   128
    // Repeat until end header is found.
33645
1af4c728e3e8 8140649: imageFile should use delete[] with new[]
asmotrak
parents: 32757
diff changeset
   129
    while ((data != NULL) && (byte = *data)) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   130
        // Extract kind from header byte.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   131
        u1 kind = attribute_kind(byte);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   132
        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
   133
        // Extract length of data (in bytes).
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   134
        u1 n = attribute_length(byte);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   135
        // Read value (most significant first.)
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   136
        _attributes[kind] = attribute_value(data + 1, n);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   137
        // 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
   138
        data += n + 1;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   139
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   140
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   141
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   142
// Zero all attribute values.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   143
void ImageLocation::clear_data() {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   144
    // Set defaults to zero.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   145
    memset(_attributes, 0, sizeof(_attributes));
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   146
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   147
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   148
// ImageModuleData constructor maps out sub-tables for faster access.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   149
ImageModuleData::ImageModuleData(const ImageFileReader* image_file) :
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   150
        _image_file(image_file),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   151
        _endian(image_file->endian()) {
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   152
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   153
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   154
// Release module data resource.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   155
ImageModuleData::~ImageModuleData() {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   156
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   157
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   158
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   159
// 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
   160
const char* ImageModuleData::package_to_module(const char* package_name) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   161
    // replace all '/' by '.'
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   162
    char* replaced = new char[(int) strlen(package_name) + 1];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   163
    assert(replaced != NULL && "allocation failed");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   164
    int i;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   165
    for (i = 0; package_name[i] != '\0'; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   166
      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
   167
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   168
    replaced[i] = '\0';
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   169
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   170
    // build path /packages/<package_name>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   171
    const char* radical = "/packages/";
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   172
    char* path = new char[(int) strlen(radical) + (int) strlen(package_name) + 1];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   173
    assert(path != NULL && "allocation failed");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   174
    strcpy(path, radical);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   175
    strcat(path, replaced);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   176
    delete[] replaced;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   177
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   178
    // retrieve package location
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   179
    ImageLocation location;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   180
    bool found = _image_file->find_location(path, location);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   181
    if (!found) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   182
        delete[] path;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   183
        return NULL;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   184
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   185
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   186
    // retrieve offsets to module name
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   187
    int size = (int)location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   188
    u1* content = new u1[size];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   189
    assert(content != NULL && "allocation failed");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   190
    _image_file->get_resource(location, content);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   191
    u1* ptr = content;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   192
    // sequence of sizeof(8) isEmpty|offset. Use the first module that is not empty.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   193
    u4 offset = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   194
    for (i = 0; i < size; i+=8) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   195
        u4 isEmpty = _endian->get(*((u4*)ptr));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   196
        ptr += 4;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   197
        if (!isEmpty) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   198
            offset = _endian->get(*((u4*)ptr));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   199
            break;
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   200
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   201
        ptr += 4;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   202
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   203
    delete[] content;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   204
    return _image_file->get_strings().get(offset);
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   205
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   206
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   207
// 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
   208
// to share an open image.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   209
ImageFileReaderTable::ImageFileReaderTable() : _count(0), _max(_growth) {
48565
871b8bb201ea 8180869: Cleaner image file reading handling
jlaskey
parents: 47216
diff changeset
   210
    _table = static_cast<ImageFileReader**>(calloc(_max, sizeof(ImageFileReader*)));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   211
    assert(_table != NULL && "allocation failed");
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   212
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   213
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   214
ImageFileReaderTable::~ImageFileReaderTable() {
48565
871b8bb201ea 8180869: Cleaner image file reading handling
jlaskey
parents: 47216
diff changeset
   215
    for (u4 i = 0; i < _count; i++) {
871b8bb201ea 8180869: Cleaner image file reading handling
jlaskey
parents: 47216
diff changeset
   216
        ImageFileReader* image = _table[i];
871b8bb201ea 8180869: Cleaner image file reading handling
jlaskey
parents: 47216
diff changeset
   217
871b8bb201ea 8180869: Cleaner image file reading handling
jlaskey
parents: 47216
diff changeset
   218
        if (image != NULL) {
871b8bb201ea 8180869: Cleaner image file reading handling
jlaskey
parents: 47216
diff changeset
   219
            delete image;
871b8bb201ea 8180869: Cleaner image file reading handling
jlaskey
parents: 47216
diff changeset
   220
        }
871b8bb201ea 8180869: Cleaner image file reading handling
jlaskey
parents: 47216
diff changeset
   221
    }
871b8bb201ea 8180869: Cleaner image file reading handling
jlaskey
parents: 47216
diff changeset
   222
    free(_table);
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
// Add a new image entry to the table.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   226
void ImageFileReaderTable::add(ImageFileReader* image) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   227
    if (_count == _max) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   228
        _max += _growth;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   229
        _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
   230
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   231
    _table[_count++] = image;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   232
}
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
// Remove an image entry from the table.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   235
void ImageFileReaderTable::remove(ImageFileReader* image) {
34386
80c21b82e467 8143879: jdk/internal/jimage/JImageReadTest.java crashes jvm in msvcr120.dll
rriggs
parents: 33825
diff changeset
   236
    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
   237
        if (_table[i] == image) {
34386
80c21b82e467 8143879: jdk/internal/jimage/JImageReadTest.java crashes jvm in msvcr120.dll
rriggs
parents: 33825
diff changeset
   238
            // 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
   239
            _table[i] = _table[--_count];
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   240
            break;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   241
        }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   242
    }
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   243
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   244
    if (_count != 0 && _count == _max - _growth) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   245
        _max -= _growth;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   246
        _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
   247
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   248
}
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
// Determine if image entry is in table.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   251
bool ImageFileReaderTable::contains(ImageFileReader* image) {
34386
80c21b82e467 8143879: jdk/internal/jimage/JImageReadTest.java crashes jvm in msvcr120.dll
rriggs
parents: 33825
diff changeset
   252
    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
   253
        if (_table[i] == image) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   254
            return true;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   255
        }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   256
    }
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   257
    return false;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   258
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   259
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   260
// 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
   261
ImageFileReaderTable ImageFileReader::_reader_table;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   262
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   263
SimpleCriticalSection _reader_table_lock;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   264
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   265
// Locate an image if file already open.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   266
ImageFileReader* ImageFileReader::find_image(const char* name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   267
    // Lock out _reader_table.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   268
    SimpleCriticalSectionLock cs(&_reader_table_lock);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   269
    // Search for an exist image file.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   270
    for (u4 i = 0; i < _reader_table.count(); i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   271
        // Retrieve table entry.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   272
        ImageFileReader* reader = _reader_table.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   273
        // If name matches, then reuse (bump up use count.)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   274
        assert(reader->name() != NULL && "reader->name must not be null");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   275
        if (strcmp(reader->name(), name) == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   276
            reader->inc_use();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   277
            return reader;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   278
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   279
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   280
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   281
    return NULL;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   282
}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   283
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   284
// 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
   285
ImageFileReader* ImageFileReader::open(const char* name, bool big_endian) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   286
    ImageFileReader* reader = find_image(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   287
    if (reader != NULL) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   288
        return reader;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   289
    }
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   290
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   291
    // Need a new image reader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   292
    reader = new ImageFileReader(name, big_endian);
33825
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   293
    if (reader == NULL || !reader->open()) {
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   294
        // Failed to open.
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   295
        delete reader;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   296
        return NULL;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   297
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   298
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   299
    // Lock to update
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   300
    SimpleCriticalSectionLock cs(&_reader_table_lock);
33825
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   301
    // Search for an existing image file.
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   302
    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
   303
        // Retrieve table entry.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   304
        ImageFileReader* existing_reader = _reader_table.get(i);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   305
        // 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
   306
        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
   307
        if (strcmp(existing_reader->name(), name) == 0) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   308
            existing_reader->inc_use();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   309
            reader->close();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   310
            delete reader;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   311
            return existing_reader;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   312
        }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   313
    }
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   314
    // Bump use count and add to table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   315
    reader->inc_use();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   316
    _reader_table.add(reader);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   317
    return reader;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   318
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   319
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   320
// 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
   321
void ImageFileReader::close(ImageFileReader *reader) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   322
    // Lock out _reader_table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   323
    SimpleCriticalSectionLock cs(&_reader_table_lock);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   324
    // 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
   325
    if (reader->dec_use()) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   326
        _reader_table.remove(reader);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   327
        delete reader;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   328
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   329
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   330
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   331
// Return an id for the specifed ImageFileReader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   332
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
   333
    // ID is just the cloaked reader address.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   334
    return (u8)reader;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   335
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   336
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   337
// Validate the image id.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   338
bool ImageFileReader::id_check(u8 id) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   339
    // 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
   340
    SimpleCriticalSectionLock cs(&_reader_table_lock);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   341
    return _reader_table.contains((ImageFileReader*)id);
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   342
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   343
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   344
// Return an id for the specifed ImageFileReader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   345
ImageFileReader* ImageFileReader::id_to_reader(u8 id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   346
    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
   347
    return (ImageFileReader*)id;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   348
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   349
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   350
// Constructor intializes to a closed state.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   351
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
   352
    // Copy the image file name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   353
     int len = (int) strlen(name) + 1;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   354
    _name = new char[len];
33825
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   355
    assert(_name != NULL  && "allocation failed");
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   356
    strncpy(_name, name, len);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   357
    // Initialize for a closed file.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   358
    _fd = -1;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   359
    _endian = Endian::get_handler(big_endian);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   360
    _index_data = NULL;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   361
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   362
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   363
// Close image and free up data structures.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   364
ImageFileReader::~ImageFileReader() {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   365
    // Ensure file is closed.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   366
    close();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   367
    // Free up name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   368
    if (_name) {
33645
1af4c728e3e8 8140649: imageFile should use delete[] with new[]
asmotrak
parents: 32757
diff changeset
   369
        delete[] _name;
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   370
        _name = NULL;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   371
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   372
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   373
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   374
// Open image file for read access.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   375
bool ImageFileReader::open() {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   376
    // If file exists open for reading.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   377
    _fd = osSupport::openReadOnly(_name);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   378
    if (_fd == -1) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   379
        return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   380
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   381
    // Retrieve the file size.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   382
    _file_size = osSupport::size(_name);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   383
    // 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
   384
    size_t header_size = sizeof(ImageHeader);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   385
    if (_file_size < header_size ||
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   386
        !read_at((u1*)&_header, header_size, 0) ||
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   387
        _header.magic(_endian) != IMAGE_MAGIC ||
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   388
        _header.major_version(_endian) != MAJOR_VERSION ||
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   389
        _header.minor_version(_endian) != MINOR_VERSION) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   390
        close();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   391
        return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   392
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   393
    // Size of image index.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   394
    _index_size = index_size();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   395
    // 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
   396
    if (_file_size < _index_size) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   397
        return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   398
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   399
    // Memory map image (minimally the index.)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   400
    _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
   401
    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
   402
    // Retrieve length of index perfect hash table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   403
    u4 length = table_length();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   404
    // 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
   405
    u4 redirect_table_offset = (u4)header_size;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   406
    // Compute offset of index attribute offsets.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   407
    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
   408
    // Compute offset of index location attribute data.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   409
    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
   410
    // Compute offset of index string table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   411
    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
   412
    // 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
   413
    _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
   414
    // Compute address of index attribute offsets.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   415
    _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
   416
    // Compute address of index location attribute data.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   417
    _location_bytes = _index_data + location_bytes_offset;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   418
    // Compute address of index string table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   419
    _string_bytes = _index_data + string_bytes_offset;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   420
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   421
    // Initialize the module data
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   422
    module_data = new ImageModuleData(this);
33825
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   423
    // Successful open (if memory allocation succeeded).
e7ed5c869dc7 8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll
rriggs
parents: 33645
diff changeset
   424
    return module_data != NULL;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   425
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   426
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   427
// Close image file.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   428
void ImageFileReader::close() {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   429
    // Deallocate the index.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   430
    if (_index_data) {
52920
100818c5ddd0 8215026: Incorrect amount of memory unmapped with ImageFileReader::close()
jgeorge
parents: 50125
diff changeset
   431
        osSupport::unmap_memory((char*)_index_data, (size_t)map_size());
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   432
        _index_data = NULL;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   433
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   434
    // Close file.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   435
    if (_fd != -1) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   436
        osSupport::close(_fd);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   437
        _fd = -1;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   438
    }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   439
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   440
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   441
// Read directly from the file.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   442
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
   443
    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
   444
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   445
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   446
// 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
   447
// the location is found, false otherwise.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   448
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
   449
    // 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
   450
    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
   451
    // If is found.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   452
    if (index != ImageStrings::NOT_FOUND) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   453
        // 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
   454
        u1* data = get_location_data(index);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   455
        // Expand location attributes.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   456
        location.set_data(data);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   457
        // 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
   458
        return verify_location(location, path);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   459
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   460
    return false;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   461
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   462
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   463
// 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
   464
// 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
   465
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
   466
    // 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
   467
    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
   468
    // If found.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   469
    if (index != ImageStrings::NOT_FOUND) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   470
        // 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
   471
        u4 offset = get_location_offset(index);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   472
        u1* data = get_location_offset_data(offset);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   473
        // Expand location attributes.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   474
        ImageLocation location(data);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   475
        // 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
   476
        if (verify_location(location, path)) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   477
                *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
   478
                return offset;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   479
        }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   480
    }
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   481
    return 0;            // not found
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   482
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   483
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   484
// 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
   485
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
   486
    // Manage the image string table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   487
    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
   488
    // 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
   489
    const char* next = path;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   490
    // Get module name string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   491
    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
   492
    // If module string is not empty.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   493
    if (*module != '\0') {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   494
        // Compare '/module/' .
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   495
        if (*next++ != '/') return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   496
        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
   497
        if (*next++ != '/') return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   498
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   499
    // Get parent (package) string
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   500
    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
   501
    // If parent string is not empty string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   502
    if (*parent != '\0') {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   503
        // Compare 'parent/' .
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   504
        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
   505
        if (*next++ != '/') return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   506
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   507
    // Get base name string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   508
    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
   509
    // Compare with basne name.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   510
    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
   511
    // Get extension string.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   512
    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
   513
    // If extension is not empty.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   514
    if (*extension != '\0') {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   515
        // Compare '.extension' .
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   516
        if (*next++ != '.') return false;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   517
        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
   518
    }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   519
    // 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
   520
    return *next == '\0';
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   521
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   522
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   523
// Return the resource for the supplied location offset.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   524
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
   525
        // 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
   526
        u1* data = get_location_offset_data(offset);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   527
        // Expand location attributes.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   528
        ImageLocation location(data);
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   529
        // Read the data
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   530
        get_resource(location, uncompressed_data);
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   531
}
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
// Return the resource for the supplied location.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   534
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
   535
    // 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
   536
    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
   537
    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
   538
    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
   539
    // If the resource is compressed.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   540
    if (compressed_size != 0) {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   541
        u1* compressed_data;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   542
        // If not memory mapped read in bytes.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   543
        if (!memory_map_image) {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   544
            // Allocate buffer for compression.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   545
            compressed_data = new u1[(size_t)compressed_size];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   546
            assert(compressed_data != NULL && "allocation failed");
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   547
            // 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
   548
            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
   549
            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
   550
        } else {
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   551
            compressed_data = get_data_address() + offset;
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   552
        }
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   553
        // Get image string table.
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   554
        const ImageStrings strings = get_strings();
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   555
        // Decompress resource.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   556
        ImageDecompressor::decompress_resource(compressed_data, uncompressed_data, uncompressed_size,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   557
                        &strings, _endian);
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   558
        // If not memory mapped then release temporary buffer.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35784
diff changeset
   559
        if (!memory_map_image) {
33645
1af4c728e3e8 8140649: imageFile should use delete[] with new[]
asmotrak
parents: 32757
diff changeset
   560
                delete[] compressed_data;
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   561
        }
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   562
    } else {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   563
        // 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
   564
        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
   565
        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
   566
    }
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   567
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   568
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   569
// Return the ImageModuleData for this image
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   570
ImageModuleData * ImageFileReader::get_image_module_data() {
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   571
        return module_data;
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   572
}