1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
13 #ifdef USE_WIN32_SIGNALLING
15 #endif /* USE_WIN32_SIGNALLING */
18 #undef __INSIDE_CYGWIN__
20 #endif /* USE_WINSOCK */
34 #include <java-signal.h>
35 #include <java-threads.h>
39 #include <java/lang/ThreadGroup.h>
42 #ifndef DISABLE_GETENV_PROPERTIES
44 #include <java-props.h>
45 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
47 #define PROCESS_GCJ_PROPERTIES
48 #endif // DISABLE_GETENV_PROPERTIES
50 #include <java/lang/Class.h>
51 #include <java/lang/ClassLoader.h>
52 #include <java/lang/Runtime.h>
53 #include <java/lang/String.h>
54 #include <java/lang/Thread.h>
55 #include <java/lang/ThreadGroup.h>
56 #include <gnu/gcj/runtime/FirstThread.h>
57 #include <java/lang/ArrayIndexOutOfBoundsException.h>
58 #include <java/lang/ArithmeticException.h>
59 #include <java/lang/ClassFormatError.h>
60 #include <java/lang/NegativeArraySizeException.h>
61 #include <java/lang/NullPointerException.h>
62 #include <java/lang/OutOfMemoryError.h>
63 #include <java/lang/System.h>
64 #include <java/lang/reflect/Modifier.h>
65 #include <java/io/PrintStream.h>
66 #include <java/lang/UnsatisfiedLinkError.h>
72 // We allocate a single OutOfMemoryError exception which we keep
73 // around for use if we run out of memory.
74 static java::lang::OutOfMemoryError *no_memory;
76 // Largest representable size_t.
77 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
79 static const char *no_properties[] = { NULL };
81 // Properties set at compile time.
82 const char **_Jv_Compiler_Properties = no_properties;
84 // The JAR file to add to the beginning of java.class.path.
85 const char *_Jv_Jar_Class_Path;
87 #ifndef DISABLE_GETENV_PROPERTIES
88 // Property key/value pairs.
89 property_pair *_Jv_Environment_Properties;
92 // The name of this executable.
93 static char * _Jv_execName;
95 // Stash the argv pointer to benefit native libraries that need it.
96 const char **_Jv_argv;
99 typedef void main_func (jobject);
102 // Pointer to JVMPI notification functions.
103 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
104 void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
105 void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
109 extern "C" void _Jv_ThrowSignal (jthrowable) __attribute ((noreturn));
111 // Just like _Jv_Throw, but fill in the stack trace first. Although
112 // this is declared extern in order that its name not be mangled, it
113 // is not intended to be used outside this file.
115 _Jv_ThrowSignal (jthrowable throwable)
117 throwable->fillInStackTrace ();
122 static java::lang::NullPointerException *nullp;
124 SIGNAL_HANDLER (catch_segv)
126 MAKE_THROW_FRAME (nullp);
127 _Jv_ThrowSignal (nullp);
131 static java::lang::ArithmeticException *arithexception;
134 SIGNAL_HANDLER (catch_fpe)
136 #ifdef HANDLE_DIVIDE_OVERFLOW
137 HANDLE_DIVIDE_OVERFLOW;
139 MAKE_THROW_FRAME (arithexception);
141 _Jv_ThrowSignal (arithexception);
148 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
151 _Jv_ushort *aptr, *bptr;
154 if (a->hash != b->hash)
157 if (b->length != len)
159 aptr = (_Jv_ushort *)a->data;
160 bptr = (_Jv_ushort *)b->data;
161 len = (len + 1) >> 1;
163 if (*aptr++ != *bptr++)
168 /* True iff A is equal to STR.
169 HASH is STR->hashCode().
173 _Jv_equal (Utf8Const* a, jstring str, jint hash)
175 if (a->hash != (_Jv_ushort) hash)
177 jint len = str->length();
179 jchar *sptr = _Jv_GetStringChars (str);
180 unsigned char* ptr = (unsigned char*) a->data;
181 unsigned char* limit = ptr + a->length;
184 int ch = UTF8_GET (ptr, limit);
193 /* Like _Jv_equal, but stop after N characters. */
195 _Jv_equaln (Utf8Const *a, jstring str, jint n)
197 jint len = str->length();
199 jchar *sptr = _Jv_GetStringChars (str);
200 unsigned char* ptr = (unsigned char*) a->data;
201 unsigned char* limit = ptr + a->length;
202 for (; n-- > 0; i++, sptr++)
204 int ch = UTF8_GET (ptr, limit);
213 /* Count the number of Unicode chars encoded in a given Ut8 string. */
215 _Jv_strLengthUtf8(char* str, int len)
218 unsigned char* limit;
221 ptr = (unsigned char*) str;
224 for (; ptr < limit; str_length++)
226 if (UTF8_GET (ptr, limit) < 0)
232 /* Calculate a hash value for a string encoded in Utf8 format.
233 * This returns the same hash value as specified or java.lang.String.hashCode.
236 hashUtf8String (char* str, int len)
238 unsigned char* ptr = (unsigned char*) str;
239 unsigned char* limit = ptr + len;
244 int ch = UTF8_GET (ptr, limit);
245 /* Updated specification from
246 http://www.javasoft.com/docs/books/jls/clarify.html. */
247 hash = (31 * hash) + ch;
253 _Jv_makeUtf8Const (char* s, int len)
257 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
260 memcpy (m->data, s, len);
263 m->hash = hashUtf8String (s, len) & 0xFFFF;
268 _Jv_makeUtf8Const (jstring string)
270 jint hash = string->hashCode ();
271 jint len = _Jv_GetStringUTFLength (string);
273 Utf8Const* m = (Utf8Const*)
274 _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
279 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
289 _Jv_Abort (const char *function, const char *file, int line,
293 _Jv_Abort (const char *, const char *, int, const char *message)
298 "libgcj failure: %s\n in function %s, file %s, line %d\n",
299 message, function, file, line);
301 java::io::PrintStream *err = java::lang::System::err;
302 err->print(JvNewStringLatin1 ("libgcj failure: "));
303 err->println(JvNewStringLatin1 (message));
310 fail_on_finalization (jobject)
312 JvFail ("object was finalized");
316 _Jv_GCWatch (jobject obj)
318 _Jv_RegisterFinalizer (obj, fail_on_finalization);
322 _Jv_ThrowBadArrayIndex(jint bad_index)
324 throw new java::lang::ArrayIndexOutOfBoundsException
325 (java::lang::String::valueOf (bad_index));
329 _Jv_ThrowNullPointerException ()
331 throw new java::lang::NullPointerException;
334 // Explicitly throw a no memory exception.
335 // The collector calls this when it encounters an out-of-memory condition.
336 void _Jv_ThrowNoMemory()
338 _Jv_Throw (no_memory);
341 // Allocate a new object of class KLASS. SIZE is the size of the object
342 // to allocate. You might think this is redundant, but it isn't; some
343 // classes, such as String, aren't of fixed size.
345 _Jv_AllocObject (jclass klass, jint size)
347 _Jv_InitClass (klass);
349 jobject obj = (jobject) _Jv_AllocObj (size, klass);
351 // If this class has inherited finalize from Object, then don't
352 // bother registering a finalizer. We know that finalize() is the
353 // very first method after the dummy entry. If this turns out to be
354 // unreliable, a more robust implementation can be written. Such an
355 // implementation would look for Object.finalize in Object's method
356 // table at startup, and then use that information to find the
357 // appropriate index in the method vector.
358 if (klass->vtable->get_finalizer()
359 != java::lang::Object::class$.vtable->get_finalizer())
360 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
363 // Service JVMPI request.
365 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
369 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
371 event.u.obj_alloc.arena_id = 0;
372 event.u.obj_alloc.class_id = (jobjectID) klass;
373 event.u.obj_alloc.is_array = 0;
374 event.u.obj_alloc.size = size;
375 event.u.obj_alloc.obj_id = (jobjectID) obj;
377 // FIXME: This doesn't look right for the Boehm GC. A GC may
378 // already be in progress. _Jv_DisableGC () doesn't wait for it.
379 // More importantly, I don't see the need for disabling GC, since we
380 // blatantly have a pointer to obj on our stack, ensuring that the
381 // object can't be collected. Even for a nonconservative collector,
382 // it appears to me that this must be true, since we are about to
383 // return obj. Isn't this whole approach way too intrusive for
384 // a useful profiling interface? - HB
386 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
394 // A version of the above that assumes the object contains no pointers,
395 // and requires no finalization. This can't happen if we need pointers
397 #ifdef JV_HASH_SYNCHRONIZATION
399 _Jv_AllocPtrFreeObject (jclass klass, jint size)
401 _Jv_InitClass (klass);
403 jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
406 // Service JVMPI request.
408 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
412 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
414 event.u.obj_alloc.arena_id = 0;
415 event.u.obj_alloc.class_id = (jobjectID) klass;
416 event.u.obj_alloc.is_array = 0;
417 event.u.obj_alloc.size = size;
418 event.u.obj_alloc.obj_id = (jobjectID) obj;
421 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
428 #endif /* JV_HASH_SYNCHRONIZATION */
431 // Allocate a new array of Java objects. Each object is of type
432 // `elementClass'. `init' is used to initialize each slot in the
435 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
437 if (__builtin_expect (count < 0, false))
438 throw new java::lang::NegativeArraySizeException;
440 JvAssert (! elementClass->isPrimitive ());
442 // Ensure that elements pointer is properly aligned.
443 jobjectArray obj = NULL;
444 size_t size = (size_t) elements (obj);
445 size += count * sizeof (jobject);
447 // FIXME: second argument should be "current loader"
448 jclass klass = _Jv_GetArrayClass (elementClass, 0);
450 obj = (jobjectArray) _Jv_AllocArray (size, klass);
452 jsize *lp = const_cast<jsize *> (&obj->length);
454 // We know the allocator returns zeroed memory. So don't bother
458 jobject *ptr = elements(obj);
465 // Allocate a new array of primitives. ELTYPE is the type of the
466 // element, COUNT is the size of the array.
468 _Jv_NewPrimArray (jclass eltype, jint count)
470 int elsize = eltype->size();
471 if (__builtin_expect (count < 0, false))
472 throw new java::lang::NegativeArraySizeException;
474 JvAssert (eltype->isPrimitive ());
475 jobject dummy = NULL;
476 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
478 // Check for overflow.
479 if (__builtin_expect ((size_t) count >
480 (SIZE_T_MAX - size) / elsize, false))
483 jclass klass = _Jv_GetArrayClass (eltype, 0);
485 # ifdef JV_HASH_SYNCHRONIZATION
486 // Since the vtable is always statically allocated,
487 // these are completely pointerfree! Make sure the GC doesn't touch them.
489 (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
490 memset((char *)arr + size, 0, elsize * count);
492 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
493 // Note that we assume we are given zeroed memory by the allocator.
496 jsize *lp = const_cast<jsize *> (&arr->length);
503 _Jv_NewArray (jint type, jint size)
507 case 4: return JvNewBooleanArray (size);
508 case 5: return JvNewCharArray (size);
509 case 6: return JvNewFloatArray (size);
510 case 7: return JvNewDoubleArray (size);
511 case 8: return JvNewByteArray (size);
512 case 9: return JvNewShortArray (size);
513 case 10: return JvNewIntArray (size);
514 case 11: return JvNewLongArray (size);
516 JvFail ("newarray - bad type code");
517 return NULL; // Placate compiler.
521 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
523 JvAssert (type->isArray());
524 jclass element_type = type->getComponentType();
526 if (element_type->isPrimitive())
527 result = _Jv_NewPrimArray (element_type, sizes[0]);
529 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
533 JvAssert (! element_type->isPrimitive());
534 JvAssert (element_type->isArray());
535 jobject *contents = elements ((jobjectArray) result);
536 for (int i = 0; i < sizes[0]; ++i)
537 contents[i] = _Jv_NewMultiArray (element_type, dimensions - 1,
545 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
548 jint sizes[dimensions];
549 va_start (args, dimensions);
550 for (int i = 0; i < dimensions; ++i)
552 jint size = va_arg (args, jint);
557 return _Jv_NewMultiArray (array_type, dimensions, sizes);
562 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
563 _Jv_ArrayVTable _Jv_##NAME##VTable; \
564 java::lang::Class _Jv_##NAME##Class ((jobject) #NAME, \
565 (jbyte) SIG, (jint) LEN, \
566 (jobject) &_Jv_##NAME##VTable);
568 DECLARE_PRIM_TYPE(byte, 'B', 1);
569 DECLARE_PRIM_TYPE(short, 'S', 2);
570 DECLARE_PRIM_TYPE(int, 'I', 4);
571 DECLARE_PRIM_TYPE(long, 'J', 8);
572 DECLARE_PRIM_TYPE(boolean, 'Z', 1);
573 DECLARE_PRIM_TYPE(char, 'C', 2);
574 DECLARE_PRIM_TYPE(float, 'F', 4);
575 DECLARE_PRIM_TYPE(double, 'D', 8);
576 DECLARE_PRIM_TYPE(void, 'V', 0);
579 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
584 return JvPrimClass (byte);
586 return JvPrimClass (short);
588 return JvPrimClass (int);
590 return JvPrimClass (long);
592 return JvPrimClass (boolean);
594 return JvPrimClass (char);
596 return JvPrimClass (float);
598 return JvPrimClass (double);
600 return JvPrimClass (void);
604 for (i = 1; sig[i] && sig[i] != ';'; ++i)
606 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
607 return _Jv_FindClass (name, loader);
612 jclass klass = _Jv_FindClassFromSignature (&sig[1], loader);
615 return _Jv_GetArrayClass (klass, loader);
619 return NULL; // Placate compiler.
625 JvConvertArgv (int argc, const char **argv)
629 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
630 jobject* ptr = elements(ar);
631 for (int i = 0; i < argc; i++)
633 const char *arg = argv[i];
634 // FIXME - should probably use JvNewStringUTF.
635 *ptr++ = JvNewStringLatin1(arg, strlen(arg));
637 return (JArray<jstring>*) ar;
640 // FIXME: These variables are static so that they will be
641 // automatically scanned by the Boehm collector. This is needed
642 // because with qthreads the collector won't scan the initial stack --
643 // it will only scan the qthreads stacks.
645 // Command line arguments.
646 static jobject arg_vec;
648 // The primary thread.
649 static java::lang::Thread *main_thread;
652 _Jv_ThisExecutable (void)
658 _Jv_ThisExecutable (const char *name)
662 _Jv_execName = (char *) _Jv_Malloc (strlen (name) + 1);
663 strcpy (_Jv_execName, name);
667 #ifdef USE_WIN32_SIGNALLING
669 extern "C" int* win32_get_restart_frame (void *);
672 win32_exception_handler (LPEXCEPTION_POINTERS e)
675 if (e->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
676 setjmp_buf = win32_get_restart_frame (nullp);
677 else if (e->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO)
678 setjmp_buf = win32_get_restart_frame (arithexception);
680 return EXCEPTION_CONTINUE_SEARCH;
682 e->ContextRecord->Ebp = setjmp_buf[0];
683 // FIXME: Why does i386-signal.h increment the PC here, do we need to do it?
684 e->ContextRecord->Eip = setjmp_buf[1];
685 // FIXME: Is this the stack pointer? Do we need it?
686 e->ContextRecord->Esp = setjmp_buf[2];
688 return EXCEPTION_CONTINUE_EXECUTION;
693 /* This will be different from _JNI_OnLoad if the user has preloaded a JNI
694 library, or linked one into the executable. */
697 /* Some systems, like Tru64 UNIX, don't support weak definitions, so use
698 an empty dummy function to check if the user provided his own. */
699 #pragma weak JNI_OnLoad = _JNI_OnLoad
700 extern jint JNI_OnLoad (JavaVM *, void *) __attribute__((weak));
702 jint _JNI_OnLoad (JavaVM *vm, void *)
709 #ifndef DISABLE_GETENV_PROPERTIES
712 next_property_key (char *s, size_t *length)
718 // Skip over whitespace
722 // If we've reached the end, return NULL. Also return NULL if for
723 // some reason we've come across a malformed property string.
729 // Determine the length of the property key.
747 next_property_value (char *s, size_t *length)
763 // If we've reached the end, return NULL.
767 // Determine the length of the property value.
786 process_gcj_properties ()
788 char *props = getenv("GCJ_PROPERTIES");
791 size_t property_count = 0;
796 // Whip through props quickly in order to count the number of
798 while (p && (p = next_property_key (p, &length)))
800 // Skip to the end of the key
803 p = next_property_value (p, &length);
810 // Allocate an array of property value/key pairs.
811 _Jv_Environment_Properties =
812 (property_pair *) malloc (sizeof(property_pair)
813 * (property_count + 1));
815 // Go through the properties again, initializing _Jv_Properties
819 while (p && (p = next_property_key (p, &length)))
821 _Jv_Environment_Properties[property_count].key = p;
822 _Jv_Environment_Properties[property_count].key_length = length;
824 // Skip to the end of the key
827 p = next_property_value (p, &length);
829 _Jv_Environment_Properties[property_count].value = p;
830 _Jv_Environment_Properties[property_count].value_length = length;
837 memset ((void *) &_Jv_Environment_Properties[property_count],
838 0, sizeof (property_pair));
842 // Null terminate the strings.
843 while (_Jv_Environment_Properties[i].key)
845 _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
846 _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
850 #endif // DISABLE_GETENV_PROPERTIES
853 _Jv_CreateJavaVM (void* /*vm_args*/)
855 PROCESS_GCJ_PROPERTIES;
857 // Turn stack trace generation off while creating exception objects.
858 _Jv_InitClass (&java::lang::Throwable::class$);
859 java::lang::Throwable::trace_enabled = 0;
865 arithexception = new java::lang::ArithmeticException
866 (JvNewStringLatin1 ("/ by zero"));
869 no_memory = new java::lang::OutOfMemoryError;
871 java::lang::Throwable::trace_enabled = 1;
874 LTDL_SET_PRELOADED_SYMBOLS ();
878 // Initialise winsock for networking
880 if (WSAStartup (MAKEWORD (1, 1), &data))
881 MessageBox (NULL, "Error initialising winsock library.", "Error", MB_OK | MB_ICONEXCLAMATION);
882 #endif /* USE_WINSOCK */
884 #ifdef USE_WIN32_SIGNALLING
885 // Install exception handler
886 SetUnhandledExceptionFilter (win32_exception_handler);
887 #elif defined(HAVE_SIGACTION)
888 // We only want this on POSIX systems.
889 struct sigaction act;
890 act.sa_handler = SIG_IGN;
891 sigemptyset (&act.sa_mask);
893 sigaction (SIGPIPE, &act, NULL);
895 signal (SIGPIPE, SIG_IGN);
900 /* Some systems let you preload shared libraries before running a
901 program. Under Linux, this is done by setting the LD_PRELOAD
902 environment variable. We take advatage of this here to allow for
903 dynamically loading a JNI library into a fully linked executable. */
905 if (JNI_OnLoad != _JNI_OnLoad)
907 JavaVM *vm = _Jv_GetJavaVM ();
913 jint vers = JNI_OnLoad (vm, NULL);
914 if (vers != JNI_VERSION_1_1 && vers != JNI_VERSION_1_2)
916 // FIXME: unload the library.
917 _Jv_Throw (new java::lang::UnsatisfiedLinkError (JvNewStringLatin1 ("unrecognized version from preloaded JNI_OnLoad")));
924 runFirst (::java::lang::Class *klass, ::java::lang::Object *args)
926 Utf8Const* main_signature = _Jv_makeUtf8Const ("([Ljava.lang.String;)V", 22);
927 Utf8Const* main_name = _Jv_makeUtf8Const ("main", 4);
929 _Jv_Method *meth = _Jv_GetMethodLocal (klass, main_name, main_signature);
931 // Some checks from Java Spec section 12.1.4.
932 const char *msg = NULL;
934 msg = "no suitable method `main' in class";
935 else if (! java::lang::reflect::Modifier::isStatic(meth->accflags))
936 msg = "`main' must be static";
937 else if (! java::lang::reflect::Modifier::isPublic(meth->accflags))
938 msg = "`main' must be public";
941 fprintf (stderr, "%s\n", msg);
946 if (_Jv_JVMPI_Notify_THREAD_START)
950 jstring thread_name = getName ();
951 jstring group_name = NULL, parent_name = NULL;
952 java::lang::ThreadGroup *group = getThreadGroup ();
956 group_name = group->getName ();
957 group = group->getParent ();
960 parent_name = group->getName ();
963 int thread_len = thread_name ? JvGetStringUTFLength (thread_name) : 0;
964 int group_len = group_name ? JvGetStringUTFLength (group_name) : 0;
965 int parent_len = parent_name ? JvGetStringUTFLength (parent_name) : 0;
967 char thread_chars[thread_len + 1];
968 char group_chars[group_len + 1];
969 char parent_chars[parent_len + 1];
972 JvGetStringUTFRegion (thread_name, 0,
973 thread_name->length(), thread_chars);
975 JvGetStringUTFRegion (group_name, 0,
976 group_name->length(), group_chars);
978 JvGetStringUTFRegion (parent_name, 0,
979 parent_name->length(), parent_chars);
981 thread_chars[thread_len] = '\0';
982 group_chars[group_len] = '\0';
983 parent_chars[parent_len] = '\0';
985 event.event_type = JVMPI_EVENT_THREAD_START;
987 event.u.thread_start.thread_name = thread_chars;
988 event.u.thread_start.group_name = group_chars;
989 event.u.thread_start.parent_name = parent_chars;
990 event.u.thread_start.thread_id = (jobjectID) this;
991 event.u.thread_start.thread_env_id = _Jv_GetCurrentJNIEnv ();
994 (*_Jv_JVMPI_Notify_THREAD_START) (&event);
999 main_func *real_main = (main_func *) meth->ncode;
1000 (*real_main) (args);
1004 JvRunMain (jclass klass, int argc, const char **argv)
1009 _Jv_CreateJavaVM (NULL);
1010 #ifdef HAVE_PROC_SELF_EXE
1012 sprintf (exec_name, "/proc/%d/exe", getpid ());
1013 _Jv_ThisExecutable (exec_name);
1015 _Jv_ThisExecutable (argv[0]);
1018 // Get the Runtime here. We want to initialize it before searching
1019 // for `main'; that way it will be set up if `main' is a JNI method.
1020 java::lang::Runtime *rtime = java::lang::Runtime::getRuntime ();
1022 main_thread = _Jv_AttachCurrentThread (JvNewStringLatin1 ("main"), NULL);
1023 arg_vec = JvConvertArgv (argc - 1, argv + 1);
1024 runFirst (klass, arg_vec);
1027 int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
1029 rtime->_exit (status);
1033 _Jv_RunMain (const char *name, int argc, const char **argv, bool is_jar)
1037 _Jv_CreateJavaVM (NULL);
1039 #ifdef HAVE_PROC_SELF_EXE
1041 sprintf (exec_name, "/proc/%d/exe", getpid ());
1042 _Jv_ThisExecutable (exec_name);
1045 // Get the Runtime here. We want to initialize it before searching
1046 // for `main'; that way it will be set up if `main' is a JNI method.
1047 java::lang::Runtime *rtime = java::lang::Runtime::getRuntime ();
1049 main_thread = _Jv_AttachCurrentThread (JvNewStringLatin1 ("main"), NULL);
1053 // name specifies a jar file. We must now extract the
1054 // Main-Class attribute from the jar's manifest file.
1055 // This is done by gnu.gcj.runtime.FirstThread.getMain.
1056 _Jv_Jar_Class_Path = strdup (name);
1057 jstring jar_name = JvNewStringLatin1 (name);
1058 // FirstThread.getMain extracts the main class name.
1059 class_name = gnu::gcj::runtime::FirstThread::getMain (jar_name);
1061 // We need a new ClassLoader because the classpath must be the
1062 // jar file only. The easiest way to do this is to lose our
1063 // reference to the previous classloader.
1064 java::lang::ClassLoader::system = NULL;
1067 class_name = JvNewStringLatin1 (name);
1069 arg_vec = JvConvertArgv (argc - 1, argv + 1);
1073 runFirst(java::lang::Class::forName (class_name), arg_vec);
1077 int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
1079 rtime->exit (status);
1084 // Parse a string and return a heap size.
1086 parse_heap_size (const char *spec)
1089 unsigned long val = strtoul (spec, &end, 10);
1090 if (*end == 'k' || *end == 'K')
1092 else if (*end == 'm' || *end == 'M')
1094 return (size_t) val;
1097 // Set the initial heap size. This might be ignored by the GC layer.
1098 // This must be called before _Jv_RunMain.
1100 _Jv_SetInitialHeapSize (const char *arg)
1102 size_t size = parse_heap_size (arg);
1103 _Jv_GCSetInitialHeapSize (size);
1106 // Set the maximum heap size. This might be ignored by the GC layer.
1107 // This must be called before _Jv_RunMain.
1109 _Jv_SetMaximumHeapSize (const char *arg)
1111 size_t size = parse_heap_size (arg);
1112 _Jv_GCSetMaximumHeapSize (size);
1118 _Jv_Malloc (jsize size)
1120 if (__builtin_expect (size == 0, false))
1122 void *ptr = malloc ((size_t) size);
1123 if (__builtin_expect (ptr == NULL, false))
1129 _Jv_Realloc (void *ptr, jsize size)
1131 if (__builtin_expect (size == 0, false))
1133 ptr = realloc (ptr, (size_t) size);
1134 if (__builtin_expect (ptr == NULL, false))
1140 _Jv_MallocUnchecked (jsize size)
1142 if (__builtin_expect (size == 0, false))
1144 return malloc ((size_t) size);
1148 _Jv_Free (void* ptr)
1155 // In theory, these routines can be #ifdef'd away on machines which
1156 // support divide overflow signals. However, we never know if some
1157 // code might have been compiled with "-fuse-divide-subroutine", so we
1158 // always include them in libgcj.
1161 _Jv_divI (jint dividend, jint divisor)
1163 if (__builtin_expect (divisor == 0, false))
1164 _Jv_ThrowSignal (arithexception);
1166 if (dividend == (jint) 0x80000000L && divisor == -1)
1169 return dividend / divisor;
1173 _Jv_remI (jint dividend, jint divisor)
1175 if (__builtin_expect (divisor == 0, false))
1176 _Jv_ThrowSignal (arithexception);
1178 if (dividend == (jint) 0x80000000L && divisor == -1)
1181 return dividend % divisor;
1185 _Jv_divJ (jlong dividend, jlong divisor)
1187 if (__builtin_expect (divisor == 0, false))
1188 _Jv_ThrowSignal (arithexception);
1190 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1193 return dividend / divisor;
1197 _Jv_remJ (jlong dividend, jlong divisor)
1199 if (__builtin_expect (divisor == 0, false))
1200 _Jv_ThrowSignal (arithexception);
1202 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1205 return dividend % divisor;