jimage.hpp revision 13698:cc3862068425
1/* 2 * Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include "jni.h" 33 34// Opaque reference to a JImage file. 35class JImageFile; 36// Opaque reference to an image file resource location. 37typedef jlong JImageLocationRef; 38 39// Max path length limit independent of platform. Windows max path is 1024, 40// other platforms use 4096. The JCK fails several tests when 1024 is used. 41#define JIMAGE_MAX_PATH 4096 42 43// JImage Error Codes 44 45// The image file is not prefixed with 0xCAFEDADA 46#define JIMAGE_BAD_MAGIC (-1) 47// The image file does not have a compatible (translatable) version 48#define JIMAGE_BAD_VERSION (-2) 49// The image file content is malformed 50#define JIMAGE_CORRUPTED (-3) 51 52/* 53 * JImageOpen - Given the supplied full path file name, open an image file. This 54 * function will also initialize tables and retrieve meta-data necessary to 55 * satisfy other functions in the API. If the image file has been previously 56 * open, a new open request will share memory and resources used by the previous 57 * open. A call to JImageOpen should be balanced by a call to JImageClose, to 58 * release memory and resources used. If the image file is not found or cannot 59 * be open, then NULL is returned and error will contain a reason for the 60 * failure; a positive value for a system error number, negative for a jimage 61 * specific error (see JImage Error Codes.) 62 * 63 * Ex. 64 * jint error; 65 * JImageFile* jimage = (*JImageOpen)(JAVA_HOME "lib/modules/bootmodules.jimage", &error); 66 * if (image == NULL) { 67 * tty->print_cr("JImage failed to open: %d", error); 68 * ... 69 * } 70 * ... 71 */ 72 73extern "C" JImageFile* JIMAGE_Open(const char *name, jint* error); 74 75typedef JImageFile* (*JImageOpen_t)(const char *name, jint* error); 76 77/* 78 * JImageClose - Given the supplied open image file (see JImageOpen), release 79 * memory and resources used by the open file and close the file. If the image 80 * file is shared by other uses, release and close is deferred until the last use 81 * is also closed. 82 * 83 * Ex. 84 * (*JImageClose)(image); 85 */ 86 87extern "C" void JIMAGE_Close(JImageFile* jimage); 88 89typedef void (*JImageClose_t)(JImageFile* jimage); 90 91 92/* 93 * JImagePackageToModule - Given an open image file (see JImageOpen) and the name 94 * of a package, return the name of module where the package resides. If the 95 * package does not exist in the image file, the function returns NULL. 96 * The resulting string does/should not have to be released. All strings are 97 * utf-8, zero byte terminated. 98 * 99 * Ex. 100 * const char* package = (*JImagePackageToModule)(image, "java/lang"); 101 * tty->print_cr(package); 102 * ���> java.base 103 */ 104 105extern "C" const char * JIMAGE_PackageToModule(JImageFile* jimage, const char* package_name); 106 107typedef const char* (*JImagePackageToModule_t)(JImageFile* jimage, const char* package_name); 108 109 110/* 111 * JImageFindResource - Given an open image file (see JImageOpen), a module 112 * name, a version string and the name of a class/resource, return location 113 * information describing the resource and its size. If no resource is found, the 114 * function returns JIMAGE_NOT_FOUND and the value of size is undefined. 115 * The version number should be "9.0" and is not used in locating the resource. 116 * The resulting location does/should not have to be released. 117 * All strings are utf-8, zero byte terminated. 118 * 119 * Ex. 120 * jlong size; 121 * JImageLocationRef location = (*JImageFindResource)(image, 122 * "java.base", "9.0", "java/lang/String.class", &size); 123 */ 124extern "C" JImageLocationRef JIMAGE_FindResource(JImageFile* jimage, 125 const char* module_name, const char* version, const char* name, 126 jlong* size); 127 128typedef JImageLocationRef(*JImageFindResource_t)(JImageFile* jimage, 129 const char* module_name, const char* version, const char* name, 130 jlong* size); 131 132 133/* 134 * JImageGetResource - Given an open image file (see JImageOpen), a resource���s 135 * location information (see JImageFindResource), a buffer of appropriate 136 * size and the size, retrieve the bytes associated with the 137 * resource. If the size is less than the resource size then the read is truncated. 138 * If the size is greater than the resource size then the remainder of the buffer 139 * is zero filled. The function will return the actual size of the resource. 140 * 141 * Ex. 142 * jlong size; 143 * JImageLocationRef location = (*JImageFindResource)(image, 144 * "java.base", "9.0", "java/lang/String.class", &size); 145 * char* buffer = new char[size]; 146 * (*JImageGetResource)(image, location, buffer, size); 147 */ 148extern "C" jlong JIMAGE_GetResource(JImageFile* jimage, JImageLocationRef location, 149 char* buffer, jlong size); 150 151typedef jlong(*JImageGetResource_t)(JImageFile* jimage, JImageLocationRef location, 152 char* buffer, jlong size); 153 154 155/* 156 * JImageResourceIterator - Given an open image file (see JImageOpen), a visitor 157 * function and a visitor argument, iterator through each of the image's resources. 158 * The visitor function is called with the image file, the module name, the 159 * package name, the base name, the extension and the visitor argument. The return 160 * value of the visitor function should be true, unless an early iteration exit is 161 * required. All strings are utf-8, zero byte terminated.file. 162 * 163 * Ex. 164 * bool ctw_visitor(JImageFile* jimage, const char* module_name, const char* version, 165 * const char* package, const char* name, const char* extension, void* arg) { 166 * if (strcmp(extension, ���class���) == 0) { 167 * char path[JIMAGE_MAX_PATH]; 168 * Thread* THREAD = Thread::current(); 169 * jio_snprintf(path, JIMAGE_MAX_PATH - 1, "/%s/%s", package, name); 170 * ClassLoader::compile_the_world_in(path, (Handle)arg, THREAD); 171 * return !HAS_PENDING_EXCEPTION; 172 * } 173 * return true; 174 * } 175 * (*JImageResourceIterator)(image, ctw_visitor, loader); 176 */ 177 178typedef bool (*JImageResourceVisitor_t)(JImageFile* jimage, 179 const char* module_name, const char* version, const char* package, 180 const char* name, const char* extension, void* arg); 181 182extern "C" void JIMAGE_ResourceIterator(JImageFile* jimage, 183 JImageResourceVisitor_t visitor, void *arg); 184 185typedef void (*JImageResourceIterator_t)(JImageFile* jimage, 186 JImageResourceVisitor_t visitor, void* arg); 187