jimage.hpp revision 8948:262b86c271b0
1/* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25#include "jni.h" 26 27// Opaque reference to a JImage file. 28class JImageFile; 29// Opaque reference to an image file resource location. 30typedef jlong JImageLocationRef; 31 32// Max path length limit independent of platform. Windows max path is 1024, 33// other platforms use 4096. The JCK fails several tests when 1024 is used. 34#define JIMAGE_MAX_PATH 4096 35 36// JImage Error Codes 37 38// The image file is not prefixed with 0xCAFEDADA 39#define JIMAGE_BAD_MAGIC (-1) 40// The image file does not have a compatible (translatable) version 41#define JIMAGE_BAD_VERSION (-2) 42// The image file content is malformed 43#define JIMAGE_CORRUPTED (-3) 44 45/* 46 * JImageOpen - Given the supplied full path file name, open an image file. This 47 * function will also initialize tables and retrieve meta-data necessary to 48 * satisfy other functions in the API. If the image file has been previously 49 * open, a new open request will share memory and resources used by the previous 50 * open. A call to JImageOpen should be balanced by a call to JImageClose, to 51 * release memory and resources used. If the image file is not found or cannot 52 * be open, then NULL is returned and error will contain a reason for the 53 * failure; a positive value for a system error number, negative for a jimage 54 * specific error (see JImage Error Codes.) 55 * 56 * Ex. 57 * jint error; 58 * JImageFile* jimage = (*JImageOpen)(JAVA_HOME "lib/modules/bootmodules.jimage", &error); 59 * if (image == NULL) { 60 * tty->print_cr("JImage failed to open: %d", error); 61 * ... 62 * } 63 * ... 64 */ 65 66extern "C" JImageFile* JIMAGE_Open(const char *name, jint* error); 67 68typedef JImageFile* (*JImageOpen_t)(const char *name, jint* error); 69 70/* 71 * JImageClose - Given the supplied open image file (see JImageOpen), release 72 * memory and resources used by the open file and close the file. If the image 73 * file is shared by other uses, release and close is deferred until the last use 74 * is also closed. 75 * 76 * Ex. 77 * (*JImageClose)(image); 78 */ 79 80extern "C" void JIMAGE_Close(JImageFile* jimage); 81 82typedef void (*JImageClose_t)(JImageFile* jimage); 83 84 85/* 86 * JImagePackageToModule - Given an open image file (see JImageOpen) and the name 87 * of a package, return the name of module where the package resides. If the 88 * package does not exist in the image file, the function returns NULL. 89 * The resulting string does/should not have to be released. All strings are 90 * utf-8, zero byte terminated. 91 * 92 * Ex. 93 * const char* package = (*JImagePackageToModule)(image, "java/lang"); 94 * tty->print_cr(package); 95 * ���> java.base 96 */ 97 98extern "C" const char * JIMAGE_PackageToModule(JImageFile* jimage, const char* package_name); 99 100typedef const char* (*JImagePackageToModule_t)(JImageFile* jimage, const char* package_name); 101 102 103/* 104 * JImageFindResource - Given an open image file (see JImageOpen), a module 105 * name, a version string and the name of a class/resource, return location 106 * information describing the resource and its size. If no resource is found, the 107 * function returns JIMAGE_NOT_FOUND and the value of size is undefined. 108 * The version number should be "9.0" and is not used in locating the resource. 109 * The resulting location does/should not have to be released. 110 * All strings are utf-8, zero byte terminated. 111 * 112 * Ex. 113 * jlong size; 114 * JImageLocationRef location = (*JImageFindResource)(image, "java.base", "9.0", "java/lang/String.class", &size); 115 */ 116extern "C" JImageLocationRef JIMAGE_FindResource(JImageFile* jimage, 117 const char* module_name, const char* version, const char* name, 118 jlong* size); 119 120typedef JImageLocationRef(*JImageFindResource_t)(JImageFile* jimage, 121 const char* module_name, const char* version, const char* name, 122 jlong* size); 123 124 125/* 126 * JImageGetResource - Given an open image file (see JImageOpen), a resource���s 127 * location information (see JImageFindResource), a buffer of appropriate 128 * size and the size, retrieve the bytes associated with the 129 * resource. If the size is less than the resource size then the read is truncated. 130 * If the size is greater than the resource size then the remainder of the buffer 131 * is zero filled. The function will return the actual size of the resource. 132 * 133 * Ex. 134 * jlong size; 135 * JImageLocationRef location = (*JImageFindResource)(image, "java.base", "9.0", "java/lang/String.class", &size); 136 * char* buffer = new char[size]; 137 * (*JImageGetResource)(image, location, buffer, size); 138 */ 139extern "C" jlong JIMAGE_GetResource(JImageFile* jimage, JImageLocationRef location, 140 char* buffer, jlong size); 141 142typedef jlong(*JImageGetResource_t)(JImageFile* jimage, JImageLocationRef location, 143 char* buffer, jlong size); 144 145 146/* 147 * JImageResourceIterator - Given an open image file (see JImageOpen), a visitor 148 * function and a visitor argument, iterator through each of the image's resources. 149 * The visitor function is called with the image file, the module name, the 150 * package name, the base name, the extension and the visitor argument. The return 151 * value of the visitor function should be true, unless an early iteration exit is 152 * required. All strings are utf-8, zero byte terminated.file. 153 * 154 * Ex. 155 * bool ctw_visitor(JImageFile* jimage, const char* module_name, const char* version, const char* package, const char* name, const char* extension, void* arg) { 156 * if (strcmp(extension, ���class���) == 0) { 157 * char path[JIMAGE_MAX_PATH]; 158 * Thread* THREAD = Thread::current(); 159 * jio_snprintf(path, JIMAGE_MAX_PATH - 1, "/%s/%s", package, name); 160 * ClassLoader::compile_the_world_in(path, (Handle)arg, THREAD); 161 * return !HAS_PENDING_EXCEPTION; 162 * } 163 * return true; 164 * } 165 * (*JImageResourceIterator)(image, ctw_visitor, loader); 166 */ 167 168typedef bool (*JImageResourceVisitor_t)(JImageFile* jimage, 169 const char* module_name, const char* version, const char* package, 170 const char* name, const char* extension, void* arg); 171 172extern "C" void JIMAGE_ResourceIterator(JImageFile* jimage, 173 JImageResourceVisitor_t visitor, void *arg); 174 175typedef void (*JImageResourceIterator_t)(JImageFile* jimage, 176 JImageResourceVisitor_t visitor, void* arg); 177