jdk/src/java.base/share/native/libjimage/jimage.cpp
author rriggs
Fri, 30 Oct 2015 11:12:20 -0400
changeset 33492 051df634418c
parent 32879 7b999f099d87
child 35784 ddeed53a1c5a
permissions -rw-r--r--
8139390: Very long classname in jimage causes SIGSEGV Summary: Correct issues with ImageNativeSubstrate and JImageReadTest Reviewed-by: mchung
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
/*
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
     4
 *
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    10
 *
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    16
 *
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    20
 *
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    23
 * questions.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    24
 */
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    25
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    26
#include <string.h>
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    27
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    28
#include "jimage.hpp"
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    29
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    30
#include "imageFile.hpp"
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
/*
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    33
 * JImageOpen - Given the supplied full path file name, open an image file. This
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    34
 * function will also initialize tables and retrieve meta-data necessary to
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    35
 * satisfy other functions in the API. If the image file has been previously
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    36
 * open, a new open request will share memory and resources used by the previous
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    37
 * open. A call to JImageOpen should be balanced by a call to JImageClose, to
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    38
 * release memory and resources used. If the image file is not found or cannot
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    39
 * be open, then NULL is returned and error will contain a reason for the
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    40
 * failure; a positive value for a system error number, negative for a jimage
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    41
 * specific error (see JImage Error Codes.)
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
 *  Ex.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    44
 *   jint error;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    45
 *   JImageFile* jimage = (*JImageOpen)(JAVA_HOME "lib/modules/bootmodules.jimage", &error);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    46
 *   if (image == NULL) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    47
 *     tty->print_cr("JImage failed to open: %d", error);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    48
 *     ...
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    49
 *   }
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    50
 *   ...
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
extern "C" JImageFile* JIMAGE_Open(const char *name, jint* error) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    53
    // TODO - return a meaningful error code
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    54
    *error = 0;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    55
    ImageFileReader* jfile = ImageFileReader::open(name);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    56
    return (JImageFile*) jfile;
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
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    59
/*
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    60
 * JImageClose - Given the supplied open image file (see JImageOpen), release
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    61
 * memory and resources used by the open file and close the file. If the image
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    62
 * file is shared by other uses, release and close is deferred until the last use
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    63
 * is also closed.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    64
 *
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    65
 * Ex.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    66
 *  (*JImageClose)(image);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    67
 */
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    68
extern "C" void JIMAGE_Close(JImageFile* image) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    69
    ImageFileReader::close((ImageFileReader*) image);
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
/*
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    73
 * JImagePackageToModule - Given an open image file (see JImageOpen) and the name
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    74
 * of a package, return the name of module where the package resides. If the
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    75
 * package does not exist in the image file, the function returns NULL.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    76
 * The resulting string does/should not have to be released. All strings are
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    77
 * utf-8, zero byte terminated.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    78
 *
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    79
 * Ex.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    80
 *  const char* package = (*JImagePackageToModule)(image, "java/lang");
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    81
 *  tty->print_cr(package);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    82
 *  —> java.base
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    83
 */
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    84
extern "C" const char* JIMAGE_PackageToModule(JImageFile* image, const char* package_name) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    85
    return ((ImageFileReader*) image)->get_image_module_data()->package_to_module(package_name);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    86
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    87
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    88
/*
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    89
 * JImageFindResource - Given an open image file (see JImageOpen), a module
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    90
 * name, a version string and the name of a class/resource, return location
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    91
 * information describing the resource and its size. If no resource is found, the
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    92
 * function returns JIMAGE_NOT_FOUND and the value of size is undefined.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    93
 * The version number should be "9.0" and is not used in locating the resource.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    94
 * The resulting location does/should not have to be released.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    95
 * All strings are utf-8, zero byte terminated.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    96
 *
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    97
 *  Ex.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
    98
 *   jlong size;
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
    99
 *   JImageLocationRef location = (*JImageFindResource)(image,
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   100
 *                                 "java.base", "9.0", "java/lang/String.class", &size);
32641
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
extern "C" JImageLocationRef JIMAGE_FindResource(JImageFile* image,
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   103
        const char* module_name, const char* version, const char* name,
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   104
        jlong* size) {
33492
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   105
    // Concatenate to get full path
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   106
    char fullpath[IMAGE_MAX_PATH];
33492
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   107
    size_t moduleNameLen = strlen(module_name);
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   108
    size_t nameLen = strlen(name);
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   109
    size_t index;
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   110
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   111
    // TBD:   assert(moduleNameLen > 0 && "module name must be non-empty");
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   112
    assert(nameLen > 0 && "name must non-empty");
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   113
33492
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   114
    // If the concatenated string is too long for the buffer, return not found
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   115
    if (1 + moduleNameLen + 1 + nameLen + 1 > IMAGE_MAX_PATH) {
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   116
        return 0L;
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   117
    }
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   118
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   119
    index = 0;
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   120
    fullpath[index++] = '/';
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   121
    memcpy(&fullpath[index], module_name, moduleNameLen);
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   122
    index += moduleNameLen;
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   123
    fullpath[index++] = '/';
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   124
    memcpy(&fullpath[index], name, nameLen);
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   125
    index += nameLen;
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   126
    fullpath[index++] = '\0';
051df634418c 8139390: Very long classname in jimage causes SIGSEGV
rriggs
parents: 32879
diff changeset
   127
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   128
    JImageLocationRef loc =
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   129
            (JImageLocationRef) ((ImageFileReader*) image)->find_location_index(fullpath, (u8*) size);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   130
    return loc;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   131
}
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   132
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   133
/*
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   134
 * JImageGetResource - Given an open image file (see JImageOpen), a resource’s
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   135
 * location information (see JImageFindResource), a buffer of appropriate
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   136
 * size and the size, retrieve the bytes associated with the
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   137
 * resource. If the size is less than the resource size then the read is truncated.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   138
 * If the size is greater than the resource size then the remainder of the buffer
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   139
 * is zero filled.  The function will return the actual size of the resource.
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
 * Ex.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   142
 *  jlong size;
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   143
 *   JImageLocationRef location = (*JImageFindResource)(image,
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   144
 *                                 "java.base", "9.0", "java/lang/String.class", &size);
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   145
 *  char* buffer = new char[size];
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   146
 *  (*JImageGetResource)(image, location, buffer, size);
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
extern "C" jlong JIMAGE_GetResource(JImageFile* image, JImageLocationRef location,
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   149
        char* buffer, jlong size) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   150
    ((ImageFileReader*) image)->get_resource((u4) location, (u1*) buffer);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   151
    return size;
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
/*
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   155
 * JImageResourceIterator - Given an open image file (see JImageOpen), a visitor
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   156
 * function and a visitor argument, iterator through each of the image's resources.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   157
 * The visitor function is called with the image file, the module name, the
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   158
 * package name, the base name, the extension and the visitor argument. The return
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   159
 * value of the visitor function should be true, unless an early iteration exit is
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   160
 * required. All strings are utf-8, zero byte terminated.file.
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   161
 *
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   162
 * Ex.
32757
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   163
 *   bool ctw_visitor(JImageFile* jimage, const char* module_name, const char* version,
79d34d4b9627 8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents: 32641
diff changeset
   164
 *                  const char* package, const char* name, const char* extension, void* arg) {
32641
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   165
 *     if (strcmp(extension, “class”) == 0) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   166
 *       char path[JIMAGE_MAX_PATH];
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   167
 *       Thread* THREAD = Thread::current();
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   168
 *       jio_snprintf(path, JIMAGE_MAX_PATH - 1, "/%s/%s", package, name);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   169
 *       ClassLoader::compile_the_world_in(path, (Handle)arg, THREAD);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   170
 *       return !HAS_PENDING_EXCEPTION;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   171
 *     }
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   172
 *     return true;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   173
 *   }
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   174
 *   (*JImageResourceIterator)(image, ctw_visitor, loader);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   175
 */
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   176
extern "C" void JIMAGE_ResourceIterator(JImageFile* image,
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   177
        JImageResourceVisitor_t visitor, void* arg) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   178
    ImageFileReader* imageFile = (ImageFileReader*) image;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   179
    u4 nEntries = imageFile->table_length();
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   180
    const ImageStrings strings = imageFile->get_strings();
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   181
    for (u4 i = 0; i < nEntries; i++) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   182
        ImageLocation location(imageFile->get_location_data(i));
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   183
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   184
        u4 moduleOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_MODULE);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   185
        if (moduleOffset == 0) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   186
            continue; // skip non-modules
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   187
        }
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   188
        const char *module = strings.get(moduleOffset);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   189
        if (strcmp(module, "modules") == 0
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   190
            || strcmp(module, "packages") == 0) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   191
            continue; // always skip
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   192
        }
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   193
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   194
        u4 parentOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_PARENT);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   195
        const char *parent = strings.get(parentOffset);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   196
        u4 baseOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_BASE);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   197
        const char *base = strings.get(baseOffset);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   198
        u4 extOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   199
        const char *extension = strings.get(extOffset);
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   200
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   201
        if (!(*visitor)(image, module, "9", parent, base, extension, arg)) {
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   202
            break;
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   203
        }
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   204
ac2c73b45253 8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff changeset
   205
    }
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