src/java.base/share/classes/jdk/internal/jimage/ImageHeader.java
author erikj
Wed, 07 Mar 2018 13:26:15 -0800
changeset 49357 aaedb8343784
parent 47216 71c04702a3d5
permissions -rw-r--r--
8198243: Add build time check for global operator new/delete in object files Reviewed-by: tbell, kbarrett, dholmes, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     1
/*
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     4
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    10
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    15
 * accompanied this code).
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    16
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    20
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    23
 * questions.
39759
427916042881 8161718: Copyright/License updates to corba, jdk
bchristi
parents: 38582
diff changeset
    24
 */
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    25
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    26
package jdk.internal.jimage;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    27
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    28
import java.nio.ByteBuffer;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    29
import java.nio.IntBuffer;
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    30
import java.util.Objects;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    31
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    32
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    33
 * @implNote This class needs to maintain JDK 8 source compatibility.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    34
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    35
 * It is used internally in the JDK to implement jimage/jrtfs access,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    36
 * but also compiled and delivered as part of the jrtfs.jar to support access
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    37
 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    38
 */
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    39
public final class ImageHeader {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    40
    public static final int MAGIC = 0xCAFEDADA;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    41
    public static final int MAJOR_VERSION = 1;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    42
    public static final int MINOR_VERSION = 0;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    43
    private static final int HEADER_SLOTS = 7;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    44
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    45
    private final int magic;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    46
    private final int majorVersion;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    47
    private final int minorVersion;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    48
    private final int flags;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    49
    private final int resourceCount;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    50
    private final int tableLength;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    51
    private final int locationsSize;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    52
    private final int stringsSize;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    53
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    54
    public ImageHeader(int resourceCount, int tableCount,
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    55
            int locationsSize, int stringsSize) {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    56
        this(MAGIC, MAJOR_VERSION, MINOR_VERSION, 0, resourceCount,
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    57
                tableCount, locationsSize, stringsSize);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    58
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    59
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    60
    public ImageHeader(int magic, int majorVersion, int minorVersion,
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    61
                int flags, int resourceCount,
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    62
                int tableLength, int locationsSize, int stringsSize)
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    63
    {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    64
        this.magic = magic;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    65
        this.majorVersion = majorVersion;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    66
        this.minorVersion = minorVersion;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    67
        this.flags = flags;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    68
        this.resourceCount = resourceCount;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    69
        this.tableLength = tableLength;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    70
        this.locationsSize = locationsSize;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    71
        this.stringsSize = stringsSize;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    72
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    73
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    74
    public static int getHeaderSize() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    75
       return HEADER_SLOTS * 4;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    76
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    77
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    78
    static ImageHeader readFrom(IntBuffer buffer) {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    79
        Objects.requireNonNull(buffer);
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    80
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    81
        if (buffer.capacity() != HEADER_SLOTS) {
41121
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 39759
diff changeset
    82
            throw new InternalError(
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 39759
diff changeset
    83
                "jimage header not the correct size: " + buffer.capacity());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    84
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    85
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    86
        int magic = buffer.get(0);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    87
        int version = buffer.get(1);
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    88
        int majorVersion = version >>> 16;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    89
        int minorVersion = version & 0xFFFF;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    90
        int flags = buffer.get(2);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    91
        int resourceCount = buffer.get(3);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    92
        int tableLength = buffer.get(4);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    93
        int locationsSize = buffer.get(5);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    94
        int stringsSize = buffer.get(6);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    95
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    96
        return new ImageHeader(magic, majorVersion, minorVersion, flags,
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
    97
            resourceCount, tableLength, locationsSize, stringsSize);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    98
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    99
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   100
    public void writeTo(ImageStream stream) {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
   101
        Objects.requireNonNull(stream);
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   102
        stream.ensure(getHeaderSize());
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   103
        writeTo(stream.getBuffer());
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   104
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   105
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   106
    public void writeTo(ByteBuffer buffer) {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
   107
        Objects.requireNonNull(buffer);
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   108
        buffer.putInt(magic);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   109
        buffer.putInt(majorVersion << 16 | minorVersion);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   110
        buffer.putInt(flags);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   111
        buffer.putInt(resourceCount);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   112
        buffer.putInt(tableLength);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   113
        buffer.putInt(locationsSize);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   114
        buffer.putInt(stringsSize);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   115
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   116
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   117
    public int getMagic() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   118
        return magic;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   119
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   120
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   121
    public int getMajorVersion() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   122
        return majorVersion;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   123
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   124
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   125
    public int getMinorVersion() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   126
        return minorVersion;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   127
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   128
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   129
    public int getFlags() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   130
        return flags;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   131
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   132
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   133
    public int getResourceCount() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   134
        return resourceCount;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   135
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   136
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   137
    public int getTableLength() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   138
        return tableLength;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   139
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   140
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   141
    public int getRedirectSize() {
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   142
        return tableLength * 4;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   143
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   144
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   145
    public int getOffsetsSize() {
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   146
        return tableLength * 4;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   147
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   148
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   149
    public int getLocationsSize() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   150
        return locationsSize;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   151
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   152
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   153
    public int getStringsSize() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   154
        return stringsSize;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   155
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   156
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   157
    public int getIndexSize() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   158
        return getHeaderSize() +
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   159
               getRedirectSize() +
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   160
               getOffsetsSize() +
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   161
               getLocationsSize() +
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   162
               getStringsSize();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   163
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   164
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   165
    int getRedirectOffset() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   166
        return getHeaderSize();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   167
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   168
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   169
    int getOffsetsOffset() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   170
        return getRedirectOffset() +
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   171
               getRedirectSize();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   172
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   173
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   174
    int getLocationsOffset() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   175
        return getOffsetsOffset() +
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   176
               getOffsetsSize();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   177
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   178
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   179
    int getStringsOffset() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   180
        return getLocationsOffset() +
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   181
               getLocationsSize();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   182
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   183
}