| 1 | // natRuntime.cc - Implementation of native side of Runtime class.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | #include <config.h>
|
|---|
| 12 |
|
|---|
| 13 | #include <stdlib.h>
|
|---|
| 14 |
|
|---|
| 15 | #include <gcj/cni.h>
|
|---|
| 16 | #include <jvm.h>
|
|---|
| 17 | #include <java/lang/Runtime.h>
|
|---|
| 18 | #include <java/lang/UnknownError.h>
|
|---|
| 19 | #include <java/lang/UnsatisfiedLinkError.h>
|
|---|
| 20 | #include <gnu/gcj/runtime/FileDeleter.h>
|
|---|
| 21 | #include <gnu/gcj/runtime/FinalizerThread.h>
|
|---|
| 22 |
|
|---|
| 23 | #include <jni.h>
|
|---|
| 24 |
|
|---|
| 25 | #ifdef USE_LTDL
|
|---|
| 26 | #include <ltdl.h>
|
|---|
| 27 |
|
|---|
| 28 | /* FIXME: we don't always need this. The next libtool will let us use
|
|---|
| 29 | AC_LTDL_PREOPEN to see if we do. */
|
|---|
| 30 | extern const lt_dlsymlist lt_preloaded_symbols[1] = { { 0, 0 } };
|
|---|
| 31 |
|
|---|
| 32 | // We keep track of all the libraries loaded by this application. For
|
|---|
| 33 | // now we use them to look up symbols for JNI. `libraries_size' holds
|
|---|
| 34 | // the total size of the buffer. `libraries_count' is the number of
|
|---|
| 35 | // items which are in use.
|
|---|
| 36 | static int libraries_size;
|
|---|
| 37 | static int libraries_count;
|
|---|
| 38 | static lt_dlhandle *libraries;
|
|---|
| 39 |
|
|---|
| 40 | static void
|
|---|
| 41 | add_library (lt_dlhandle lib)
|
|---|
| 42 | {
|
|---|
| 43 | if (libraries_count == libraries_size)
|
|---|
| 44 | {
|
|---|
| 45 | int ns = libraries_size * 2;
|
|---|
| 46 | if (ns == 0)
|
|---|
| 47 | ns = 10;
|
|---|
| 48 | lt_dlhandle *n = (lt_dlhandle *) _Jv_Malloc (ns * sizeof (lt_dlhandle));
|
|---|
| 49 | if (libraries)
|
|---|
| 50 | {
|
|---|
| 51 | memcpy (n, libraries, libraries_size * sizeof (lt_dlhandle));
|
|---|
| 52 | _Jv_Free (libraries);
|
|---|
| 53 | }
|
|---|
| 54 | libraries = n;
|
|---|
| 55 | libraries_size = ns;
|
|---|
| 56 | for (int i = libraries_count; i < libraries_size; ++i)
|
|---|
| 57 | libraries[i] = NULL;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | libraries[libraries_count++] = lib;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void *
|
|---|
| 64 | _Jv_FindSymbolInExecutable (const char *symname)
|
|---|
| 65 | {
|
|---|
| 66 | for (int i = 0; i < libraries_count; ++i)
|
|---|
| 67 | {
|
|---|
| 68 | void *r = lt_dlsym (libraries[i], symname);
|
|---|
| 69 | if (r)
|
|---|
| 70 | return r;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | return NULL;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | #else
|
|---|
| 77 |
|
|---|
| 78 | void *
|
|---|
| 79 | _Jv_FindSymbolInExecutable (const char *symname)
|
|---|
| 80 | {
|
|---|
| 81 | return NULL;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | #endif /* USE_LTDL */
|
|---|
| 85 |
|
|---|
| 86 | void
|
|---|
| 87 | java::lang::Runtime::exit (jint status)
|
|---|
| 88 | {
|
|---|
| 89 | checkExit (status);
|
|---|
| 90 | _exit (status);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void
|
|---|
| 94 | java::lang::Runtime::_exit (jint status)
|
|---|
| 95 | {
|
|---|
| 96 | // Make status right for Unix. This is perhaps strange.
|
|---|
| 97 | if (status < 0 || status > 255)
|
|---|
| 98 | status = 255;
|
|---|
| 99 |
|
|---|
| 100 | if (finalize_on_exit)
|
|---|
| 101 | _Jv_RunAllFinalizers ();
|
|---|
| 102 |
|
|---|
| 103 | // Delete all files registered with File.deleteOnExit()
|
|---|
| 104 | gnu::gcj::runtime::FileDeleter::deleteOnExitNow ();
|
|---|
| 105 |
|
|---|
| 106 | ::exit (status);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | jlong
|
|---|
| 110 | java::lang::Runtime::freeMemory (void)
|
|---|
| 111 | {
|
|---|
| 112 | return _Jv_GCFreeMemory ();
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | void
|
|---|
| 116 | java::lang::Runtime::gc (void)
|
|---|
| 117 | {
|
|---|
| 118 | _Jv_RunGC ();
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | void
|
|---|
| 122 | java::lang::Runtime::_load (jstring path, jboolean do_search)
|
|---|
| 123 | {
|
|---|
| 124 | JvSynchronize sync (this);
|
|---|
| 125 | checkLink (path);
|
|---|
| 126 | using namespace java::lang;
|
|---|
| 127 | #ifdef USE_LTDL
|
|---|
| 128 | jint len = _Jv_GetStringUTFLength (path);
|
|---|
| 129 | char buf[len + 1 + 3];
|
|---|
| 130 | int offset = 0;
|
|---|
| 131 | #ifndef WIN32
|
|---|
| 132 | // On Unix boxes, prefix library name with `lib', for loadLibrary.
|
|---|
| 133 | if (do_search)
|
|---|
| 134 | {
|
|---|
| 135 | strcpy (buf, "lib");
|
|---|
| 136 | offset = 3;
|
|---|
| 137 | }
|
|---|
| 138 | #endif
|
|---|
| 139 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), &buf[offset]);
|
|---|
| 140 | buf[offset + total] = '\0';
|
|---|
| 141 | lt_dlhandle h;
|
|---|
| 142 | // FIXME: make sure path is absolute.
|
|---|
| 143 | {
|
|---|
| 144 | // Synchronize on java.lang.Class. This is to protect the class chain from
|
|---|
| 145 | // concurrent modification by class registration calls which may be run
|
|---|
| 146 | // during the dlopen().
|
|---|
| 147 | JvSynchronize sync (&java::lang::Class::class$);
|
|---|
| 148 | h = do_search ? lt_dlopenext (buf) : lt_dlopen (buf);
|
|---|
| 149 | }
|
|---|
| 150 | if (h == NULL)
|
|---|
| 151 | {
|
|---|
| 152 | const char *msg = lt_dlerror ();
|
|---|
| 153 | jstring str = path->concat (JvNewStringLatin1 (": "));
|
|---|
| 154 | str = str->concat (JvNewStringLatin1 (msg));
|
|---|
| 155 | throw new UnsatisfiedLinkError (str);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | add_library (h);
|
|---|
| 159 |
|
|---|
| 160 | void *onload = lt_dlsym (h, "JNI_OnLoad");
|
|---|
| 161 | if (onload != NULL)
|
|---|
| 162 | {
|
|---|
| 163 | JavaVM *vm = _Jv_GetJavaVM ();
|
|---|
| 164 | if (vm == NULL)
|
|---|
| 165 | {
|
|---|
| 166 | // FIXME: what?
|
|---|
| 167 | return;
|
|---|
| 168 | }
|
|---|
| 169 | jint vers = ((jint (*) (JavaVM *, void *)) onload) (vm, NULL);
|
|---|
| 170 | if (vers != JNI_VERSION_1_1 && vers != JNI_VERSION_1_2)
|
|---|
| 171 | {
|
|---|
| 172 | // FIXME: unload the library.
|
|---|
| 173 | throw new UnsatisfiedLinkError (JvNewStringLatin1 ("unrecognized version from JNI_OnLoad"));
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 | #else
|
|---|
| 177 | throw new UnknownError
|
|---|
| 178 | (JvNewStringLatin1 (do_search
|
|---|
| 179 | ? "Runtime.loadLibrary not implemented"
|
|---|
| 180 | : "Runtime.load not implemented"));
|
|---|
| 181 | #endif /* USE_LTDL */
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | jboolean
|
|---|
| 185 | java::lang::Runtime::loadLibraryInternal (jstring lib)
|
|---|
| 186 | {
|
|---|
| 187 | JvSynchronize sync (this);
|
|---|
| 188 | using namespace java::lang;
|
|---|
| 189 | #ifdef USE_LTDL
|
|---|
| 190 | jint len = _Jv_GetStringUTFLength (lib);
|
|---|
| 191 | char buf[len + 1];
|
|---|
| 192 | jsize total = JvGetStringUTFRegion (lib, 0, lib->length(), buf);
|
|---|
| 193 | buf[total] = '\0';
|
|---|
| 194 | // FIXME: make sure path is absolute.
|
|---|
| 195 | lt_dlhandle h = lt_dlopenext (buf);
|
|---|
| 196 | if (h != NULL)
|
|---|
| 197 | add_library (h);
|
|---|
| 198 | return h != NULL;
|
|---|
| 199 | #else
|
|---|
| 200 | return false;
|
|---|
| 201 | #endif /* USE_LTDL */
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | void
|
|---|
| 205 | java::lang::Runtime::init (void)
|
|---|
| 206 | {
|
|---|
| 207 | finalize_on_exit = false;
|
|---|
| 208 | #ifdef USE_LTDL
|
|---|
| 209 | lt_dlinit ();
|
|---|
| 210 | lt_dlhandle self = lt_dlopen (NULL);
|
|---|
| 211 | if (self != NULL)
|
|---|
| 212 | add_library (self);
|
|---|
| 213 | #endif
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | void
|
|---|
| 217 | java::lang::Runtime::runFinalization (void)
|
|---|
| 218 | {
|
|---|
| 219 | gnu::gcj::runtime::FinalizerThread::finalizerReady ();
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | jlong
|
|---|
| 223 | java::lang::Runtime::totalMemory (void)
|
|---|
| 224 | {
|
|---|
| 225 | return _Jv_GCTotalMemory ();
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | void
|
|---|
| 229 | java::lang::Runtime::traceInstructions (jboolean)
|
|---|
| 230 | {
|
|---|
| 231 | // Do nothing.
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | void
|
|---|
| 235 | java::lang::Runtime::traceMethodCalls (jboolean)
|
|---|
| 236 | {
|
|---|
| 237 | // Do nothing.
|
|---|
| 238 | }
|
|---|