]> gcc.gnu.org Git - gcc.git/blame - libjava/prims.cc
* gcc.c: Remove ancient comment about a bug in Sun cc.
[gcc.git] / libjava / prims.cc
CommitLineData
ee9dd372
TT
1// prims.cc - Code for core of runtime environment.
2
e1b871ec 3/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation
ee9dd372
TT
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
11#include <config.h>
b8fe3c1e 12#include <platform.h>
878885b4 13
ee9dd372
TT
14#include <stdlib.h>
15#include <stdarg.h>
16#include <stdio.h>
17#include <string.h>
9b0cb287 18#include <signal.h>
ee9dd372 19
283a159f
AH
20#ifdef HAVE_UNISTD_H
21#include <unistd.h>
22#endif
23
27e934d8 24#include <gcj/cni.h>
ee9dd372 25#include <jvm.h>
a4e44caa 26#include <java-signal.h>
27e934d8 27#include <java-threads.h>
6187fd28 28#include <java-interp.h>
ee9dd372 29
54c2f04b
AG
30#ifdef ENABLE_JVMPI
31#include <jvmpi.h>
c93d7fae 32#include <java/lang/ThreadGroup.h>
54c2f04b
AG
33#endif
34
86acf60c 35#ifdef INTERPRETER
078b3073
KS
36#include <jvmti.h>
37#include "jvmti-int.h"
86acf60c 38#endif
078b3073 39
ffccc6be
AG
40#ifndef DISABLE_GETENV_PROPERTIES
41#include <ctype.h>
42#include <java-props.h>
43#define PROCESS_GCJ_PROPERTIES process_gcj_properties()
44#else
45#define PROCESS_GCJ_PROPERTIES
46#endif // DISABLE_GETENV_PROPERTIES
47
ee9dd372 48#include <java/lang/Class.h>
6aaeb975 49#include <java/lang/ClassLoader.h>
ee9dd372
TT
50#include <java/lang/Runtime.h>
51#include <java/lang/String.h>
52#include <java/lang/Thread.h>
53#include <java/lang/ThreadGroup.h>
ee9dd372 54#include <java/lang/ArrayIndexOutOfBoundsException.h>
a4e44caa 55#include <java/lang/ArithmeticException.h>
ee9dd372 56#include <java/lang/ClassFormatError.h>
c1a9321f 57#include <java/lang/ClassNotFoundException.h>
68940f3f 58#include <java/lang/InternalError.h>
ee9dd372 59#include <java/lang/NegativeArraySizeException.h>
c1a9321f 60#include <java/lang/NoClassDefFoundError.h>
ee9dd372
TT
61#include <java/lang/NullPointerException.h>
62#include <java/lang/OutOfMemoryError.h>
ee9dd372 63#include <java/lang/System.h>
aa893047 64#include <java/lang/VMClassLoader.h>
ee9dd372
TT
65#include <java/lang/reflect/Modifier.h>
66#include <java/io/PrintStream.h>
c93d7fae 67#include <java/lang/UnsatisfiedLinkError.h>
eeedeb5a 68#include <java/lang/VirtualMachineError.h>
ef874386 69#include <gnu/gcj/runtime/ExtensionClassLoader.h>
eeedeb5a 70#include <gnu/gcj/runtime/FinalizerThread.h>
36739040 71#include <execution.h>
86acf60c
DD
72
73#ifdef INTERPRETER
b8caf93e
KS
74#include <gnu/classpath/jdwp/Jdwp.h>
75#include <gnu/classpath/jdwp/VMVirtualMachine.h>
86acf60c
DD
76#endif // INTERPRETER
77
74fa958e 78#include <gnu/java/lang/MainThread.h>
ee9dd372 79
9096b279
TT
80#ifdef USE_LTDL
81#include <ltdl.h>
82#endif
a4e44caa 83
36739040
TT
84// Execution engine for compiled code.
85_Jv_CompiledEngine _Jv_soleCompiledEngine;
86
fdafd461
AH
87// Execution engine for code compiled with -findirect-classes
88_Jv_IndirectCompiledEngine _Jv_soleIndirectCompiledEngine;
89
ee9dd372
TT
90// We allocate a single OutOfMemoryError exception which we keep
91// around for use if we run out of memory.
92static java::lang::OutOfMemoryError *no_memory;
93
bf1b388d
RL
94// Number of bytes in largest array object we create. This could be
95// increased to the largest size_t value, so long as the appropriate
96// functions are changed to take a size_t argument instead of jint.
9d5b51ff 97#define MAX_OBJECT_SIZE (((size_t)1<<31) - 1)
ee9dd372 98
b8c3c4f0 99// Properties set at compile time.
bc71e4a2
TF
100const char **_Jv_Compiler_Properties = NULL;
101int _Jv_Properties_Count = 0;
b8c3c4f0 102
ffccc6be
AG
103#ifndef DISABLE_GETENV_PROPERTIES
104// Property key/value pairs.
105property_pair *_Jv_Environment_Properties;
106#endif
283a159f 107
0acff4bc
BM
108// Stash the argv pointer to benefit native libraries that need it.
109const char **_Jv_argv;
110int _Jv_argc;
111
b8caf93e
KS
112// Debugging options
113static bool remoteDebug = false;
86acf60c 114#ifdef INTERPRETER
9d5b51ff
AH
115static char defaultJdwpOptions[] = "";
116static char *jdwpOptions = defaultJdwpOptions;
b8caf93e 117
3fe9aa9e
KG
118// Typedefs for JVMTI agent functions.
119typedef jint jvmti_agent_onload_func (JavaVM *vm, char *options,
120 void *reserved);
121typedef jint jvmti_agent_onunload_func (JavaVM *vm);
122
123// JVMTI agent function pointers.
124static jvmti_agent_onload_func *jvmti_agentonload = NULL;
125static jvmti_agent_onunload_func *jvmti_agentonunload = NULL;
126static char *jvmti_agent_opts;
86acf60c 127#endif // INTERPRETER
3fe9aa9e 128
c4519773
ME
129// Argument support.
130int
131_Jv_GetNbArgs (void)
132{
133 // _Jv_argc is 0 if not explicitly initialized.
134 return _Jv_argc;
135}
136
137const char *
138_Jv_GetSafeArg (int index)
139{
140 if (index >=0 && index < _Jv_GetNbArgs ())
141 return _Jv_argv[index];
142 else
143 return "";
144}
145
146void
147_Jv_SetArgs (int argc, const char **argv)
148{
149 _Jv_argc = argc;
150 _Jv_argv = argv;
151}
152
54c2f04b
AG
153#ifdef ENABLE_JVMPI
154// Pointer to JVMPI notification functions.
155void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
156void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
157void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
158#endif
ee9dd372
TT
159\f
160
c5fe8107 161#if defined (HANDLE_SEGV) || defined(HANDLE_FPE)
2510de8d
AH
162/* Unblock a signal. Unless we do this, the signal may only be sent
163 once. */
164static void
c5fe8107 165unblock_signal (int signum __attribute__ ((__unused__)))
2510de8d
AH
166{
167#ifdef _POSIX_VERSION
168 sigset_t sigs;
169
170 sigemptyset (&sigs);
171 sigaddset (&sigs, signum);
172 sigprocmask (SIG_UNBLOCK, &sigs, NULL);
173#endif
174}
c5fe8107 175#endif
2510de8d 176
01f78a02 177#ifdef HANDLE_SEGV
01f78a02
AH
178SIGNAL_HANDLER (catch_segv)
179{
2510de8d 180 unblock_signal (SIGSEGV);
6c80c45e 181 MAKE_THROW_FRAME (nullp);
18744d9b
BM
182 java::lang::NullPointerException *nullp
183 = new java::lang::NullPointerException;
bec3e66d 184 throw nullp;
01f78a02
AH
185}
186#endif
187
d342a2e1 188#ifdef HANDLE_FPE
01f78a02
AH
189SIGNAL_HANDLER (catch_fpe)
190{
2510de8d 191 unblock_signal (SIGFPE);
554b61bb
AH
192#ifdef HANDLE_DIVIDE_OVERFLOW
193 HANDLE_DIVIDE_OVERFLOW;
194#else
6c80c45e 195 MAKE_THROW_FRAME (arithexception);
554b61bb 196#endif
18744d9b
BM
197 java::lang::ArithmeticException *arithexception
198 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
bec3e66d 199 throw arithexception;
01f78a02
AH
200}
201#endif
202
01f78a02 203
ee9dd372 204jboolean
9dfc2ec2 205_Jv_equalUtf8Consts (const Utf8Const* a, const Utf8Const *b)
ee9dd372 206{
6c80c45e 207 int len;
9dfc2ec2 208 const _Jv_ushort *aptr, *bptr;
ee9dd372
TT
209 if (a == b)
210 return true;
211 if (a->hash != b->hash)
212 return false;
213 len = a->length;
214 if (b->length != len)
215 return false;
9dfc2ec2
AH
216 aptr = (const _Jv_ushort *)a->data;
217 bptr = (const _Jv_ushort *)b->data;
ee9dd372
TT
218 len = (len + 1) >> 1;
219 while (--len >= 0)
220 if (*aptr++ != *bptr++)
221 return false;
222 return true;
223}
224
225/* True iff A is equal to STR.
554b61bb 226 HASH is STR->hashCode().
ee9dd372
TT
227*/
228
229jboolean
230_Jv_equal (Utf8Const* a, jstring str, jint hash)
231{
232 if (a->hash != (_Jv_ushort) hash)
233 return false;
234 jint len = str->length();
235 jint i = 0;
236 jchar *sptr = _Jv_GetStringChars (str);
6c80c45e
TT
237 unsigned char* ptr = (unsigned char*) a->data;
238 unsigned char* limit = ptr + a->length;
ee9dd372 239 for (;; i++, sptr++)
0f918fea
TT
240 {
241 int ch = UTF8_GET (ptr, limit);
242 if (i == len)
243 return ch < 0;
244 if (ch != *sptr)
245 return false;
246 }
247 return true;
248}
249
250/* Like _Jv_equal, but stop after N characters. */
251jboolean
252_Jv_equaln (Utf8Const *a, jstring str, jint n)
253{
254 jint len = str->length();
255 jint i = 0;
256 jchar *sptr = _Jv_GetStringChars (str);
6c80c45e
TT
257 unsigned char* ptr = (unsigned char*) a->data;
258 unsigned char* limit = ptr + a->length;
0f918fea 259 for (; n-- > 0; i++, sptr++)
ee9dd372
TT
260 {
261 int ch = UTF8_GET (ptr, limit);
262 if (i == len)
263 return ch < 0;
264 if (ch != *sptr)
265 return false;
266 }
267 return true;
268}
269
c1a9321f
RS
270// Determines whether the given Utf8Const object contains
271// a type which is primitive or some derived form of it, eg.
272// an array or multi-dimensional array variant.
273jboolean
274_Jv_isPrimitiveOrDerived(const Utf8Const *a)
275{
276 unsigned char *aptr = (unsigned char *) a->data;
277 unsigned char *alimit = aptr + a->length;
278 int ac = UTF8_GET(aptr, alimit);
279
280 // Skips any leading array marks.
281 while (ac == '[')
282 ac = UTF8_GET(aptr, alimit);
283
284 // There should not be another character. This implies that
285 // the type name is only one character long.
286 if (UTF8_GET(aptr, alimit) == -1)
287 switch ( ac )
288 {
289 case 'Z':
290 case 'B':
291 case 'C':
292 case 'S':
293 case 'I':
294 case 'J':
295 case 'F':
296 case 'D':
297 return true;
298 default:
299 break;
300 }
301
302 return false;
303}
304
305// Find out whether two _Jv_Utf8Const candidates contain the same
306// classname.
307// The method is written to handle the different formats of classnames.
308// Eg. "Ljava/lang/Class;", "Ljava.lang.Class;", "java/lang/Class" and
309// "java.lang.Class" will be seen as equal.
310// Warning: This function is not smart enough to declare "Z" and "boolean"
311// and similar cases as equal (and is not meant to be used this way)!
312jboolean
313_Jv_equalUtf8Classnames (const Utf8Const *a, const Utf8Const *b)
314{
315 // If the class name's length differs by two characters
316 // it is possible that we have candidates which are given
317 // in the two different formats ("Lp1/p2/cn;" vs. "p1/p2/cn")
318 switch (a->length - b->length)
319 {
320 case -2:
321 case 0:
322 case 2:
323 break;
324 default:
325 return false;
326 }
327
328 unsigned char *aptr = (unsigned char *) a->data;
329 unsigned char *alimit = aptr + a->length;
330 unsigned char *bptr = (unsigned char *) b->data;
331 unsigned char *blimit = bptr + b->length;
332
333 if (alimit[-1] == ';')
334 alimit--;
335
336 if (blimit[-1] == ';')
337 blimit--;
338
339 int ac = UTF8_GET(aptr, alimit);
340 int bc = UTF8_GET(bptr, blimit);
341
342 // Checks whether both strings have the same amount of leading [ characters.
343 while (ac == '[')
344 {
345 if (bc == '[')
346 {
347 ac = UTF8_GET(aptr, alimit);
348 bc = UTF8_GET(bptr, blimit);
349 continue;
350 }
351
352 return false;
353 }
354
355 // Skips leading L character.
356 if (ac == 'L')
357 ac = UTF8_GET(aptr, alimit);
358
359 if (bc == 'L')
360 bc = UTF8_GET(bptr, blimit);
361
362 // Compares the remaining characters.
363 while (ac != -1 && bc != -1)
364 {
365 // Replaces package separating dots with slashes.
366 if (ac == '.')
367 ac = '/';
368
369 if (bc == '.')
370 bc = '/';
371
372 // Now classnames differ if there is at least one non-matching
373 // character.
374 if (ac != bc)
375 return false;
376
377 ac = UTF8_GET(aptr, alimit);
378 bc = UTF8_GET(bptr, blimit);
379 }
380
381 return (ac == bc);
382}
383
ee9dd372
TT
384/* Count the number of Unicode chars encoded in a given Ut8 string. */
385int
2cf94e58 386_Jv_strLengthUtf8(const char* str, int len)
ee9dd372 387{
6c80c45e
TT
388 unsigned char* ptr;
389 unsigned char* limit;
ee9dd372
TT
390 int str_length;
391
392 ptr = (unsigned char*) str;
393 limit = ptr + len;
394 str_length = 0;
3610e0d5
TT
395 for (; ptr < limit; str_length++)
396 {
397 if (UTF8_GET (ptr, limit) < 0)
398 return (-1);
ee9dd372 399 }
ee9dd372
TT
400 return (str_length);
401}
402
403/* Calculate a hash value for a string encoded in Utf8 format.
404 * This returns the same hash value as specified or java.lang.String.hashCode.
405 */
b4d49f49 406jint
2cf94e58 407_Jv_hashUtf8String (const char* str, int len)
ee9dd372 408{
6c80c45e
TT
409 unsigned char* ptr = (unsigned char*) str;
410 unsigned char* limit = ptr + len;
ee9dd372
TT
411 jint hash = 0;
412
413 for (; ptr < limit;)
414 {
415 int ch = UTF8_GET (ptr, limit);
416 /* Updated specification from
417 http://www.javasoft.com/docs/books/jls/clarify.html. */
418 hash = (31 * hash) + ch;
419 }
420 return hash;
421}
422
b4d49f49 423void
2cf94e58 424_Jv_Utf8Const::init(const char *s, int len)
b4d49f49
PB
425{
426 ::memcpy (data, s, len);
427 data[len] = 0;
428 length = len;
429 hash = _Jv_hashUtf8String (s, len) & 0xFFFF;
430}
431
ee9dd372 432_Jv_Utf8Const *
2cf94e58 433_Jv_makeUtf8Const (const char* s, int len)
ee9dd372
TT
434{
435 if (len < 0)
436 len = strlen (s);
b4d49f49
PB
437 Utf8Const* m
438 = (Utf8Const*) _Jv_AllocBytes (_Jv_Utf8Const::space_needed(s, len));
439 m->init(s, len);
440 return m;
ee9dd372
TT
441}
442
58eb6e7c
AG
443_Jv_Utf8Const *
444_Jv_makeUtf8Const (jstring string)
445{
446 jint hash = string->hashCode ();
447 jint len = _Jv_GetStringUTFLength (string);
448
449 Utf8Const* m = (Utf8Const*)
3610e0d5 450 _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
58eb6e7c
AG
451
452 m->hash = hash;
453 m->length = len;
454
455 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
456 m->data[len] = 0;
457
458 return m;
459}
460
ee9dd372
TT
461\f
462
463#ifdef DEBUG
464void
465_Jv_Abort (const char *function, const char *file, int line,
466 const char *message)
467#else
468void
469_Jv_Abort (const char *, const char *, int, const char *message)
470#endif
471{
472#ifdef DEBUG
473 fprintf (stderr,
474 "libgcj failure: %s\n in function %s, file %s, line %d\n",
475 message, function, file, line);
476#else
afb2dec5 477 fprintf (stderr, "libgcj failure: %s\n", message);
ee9dd372 478#endif
5df33252 479 fflush (stderr);
ee9dd372
TT
480 abort ();
481}
482
483static void
484fail_on_finalization (jobject)
485{
486 JvFail ("object was finalized");
487}
488
489void
490_Jv_GCWatch (jobject obj)
491{
492 _Jv_RegisterFinalizer (obj, fail_on_finalization);
493}
494
495void
496_Jv_ThrowBadArrayIndex(jint bad_index)
497{
b3208f56
RH
498 throw new java::lang::ArrayIndexOutOfBoundsException
499 (java::lang::String::valueOf (bad_index));
ee9dd372
TT
500}
501
40f4ae32
TT
502void
503_Jv_ThrowNullPointerException ()
504{
b3208f56 505 throw new java::lang::NullPointerException;
40f4ae32
TT
506}
507
f3dc41fe
AH
508// Resolve an entry in the constant pool and return the target
509// address.
510void *
511_Jv_ResolvePoolEntry (jclass this_class, jint index)
512{
513 _Jv_Constants *pool = &this_class->constants;
514
515 if ((pool->tags[index] & JV_CONSTANT_ResolvedFlag) != 0)
516 return pool->data[index].field->u.addr;
517
518 JvSynchronize sync (this_class);
519 return (_Jv_Linker::resolve_pool_entry (this_class, index))
520 .field->u.addr;
521}
522
523
3610e0d5
TT
524// Explicitly throw a no memory exception.
525// The collector calls this when it encounters an out-of-memory condition.
526void _Jv_ThrowNoMemory()
ee9dd372 527{
938f3777 528 throw no_memory;
ee9dd372
TT
529}
530
54c2f04b 531#ifdef ENABLE_JVMPI
8ba14152
BM
532# define JVMPI_NOTIFY_ALLOC(klass,size,obj) \
533 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false)) \
534 jvmpi_notify_alloc(klass,size,obj);
938f3777
HB
535static void
536jvmpi_notify_alloc(jclass klass, jint size, jobject obj)
537{
538 // Service JVMPI allocation request.
8ba14152
BM
539 JVMPI_Event event;
540
541 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
542 event.env_id = NULL;
543 event.u.obj_alloc.arena_id = 0;
544 event.u.obj_alloc.class_id = (jobjectID) klass;
545 event.u.obj_alloc.is_array = 0;
546 event.u.obj_alloc.size = size;
547 event.u.obj_alloc.obj_id = (jobjectID) obj;
548
549 // FIXME: This doesn't look right for the Boehm GC. A GC may
550 // already be in progress. _Jv_DisableGC () doesn't wait for it.
551 // More importantly, I don't see the need for disabling GC, since we
552 // blatantly have a pointer to obj on our stack, ensuring that the
553 // object can't be collected. Even for a nonconservative collector,
554 // it appears to me that this must be true, since we are about to
555 // return obj. Isn't this whole approach way too intrusive for
556 // a useful profiling interface? - HB
557 _Jv_DisableGC ();
558 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
559 _Jv_EnableGC ();
938f3777
HB
560}
561#else /* !ENABLE_JVMPI */
8ba14152 562# define JVMPI_NOTIFY_ALLOC(klass,size,obj) /* do nothing */
54c2f04b
AG
563#endif
564
245c3c04 565// Allocate a new object of class KLASS.
938f3777
HB
566// First a version that assumes that we have no finalizer, and that
567// the class is already initialized.
568// If we know that JVMPI is disabled, this can be replaced by a direct call
569// to the allocator for the appropriate GC.
570jobject
245c3c04 571_Jv_AllocObjectNoInitNoFinalizer (jclass klass)
938f3777 572{
245c3c04 573 jint size = klass->size ();
938f3777 574 jobject obj = (jobject) _Jv_AllocObj (size, klass);
8ba14152 575 JVMPI_NOTIFY_ALLOC (klass, size, obj);
938f3777
HB
576 return obj;
577}
578
579// And now a version that initializes if necessary.
580jobject
245c3c04 581_Jv_AllocObjectNoFinalizer (jclass klass)
938f3777 582{
c1a9321f
RS
583 if (_Jv_IsPhantomClass(klass) )
584 throw new java::lang::NoClassDefFoundError(klass->getName());
585
938f3777 586 _Jv_InitClass (klass);
245c3c04 587 jint size = klass->size ();
938f3777 588 jobject obj = (jobject) _Jv_AllocObj (size, klass);
8ba14152 589 JVMPI_NOTIFY_ALLOC (klass, size, obj);
938f3777
HB
590 return obj;
591}
592
593// And now the general version that registers a finalizer if necessary.
594jobject
245c3c04 595_Jv_AllocObject (jclass klass)
938f3777 596{
245c3c04
BM
597 jobject obj = _Jv_AllocObjectNoFinalizer (klass);
598
938f3777
HB
599 // We assume that the compiler only generates calls to this routine
600 // if there really is an interesting finalizer.
601 // Unfortunately, we still have to the dynamic test, since there may
602 // be cni calls to this routine.
76508852 603 // Note that on IA64 get_finalizer() returns the starting address of the
938f3777
HB
604 // function, not a function pointer. Thus this still works.
605 if (klass->vtable->get_finalizer ()
606 != java::lang::Object::class$.vtable->get_finalizer ())
607 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
ee9dd372
TT
608 return obj;
609}
610
245c3c04
BM
611// Allocate a String, including variable length storage.
612jstring
613_Jv_AllocString(jsize len)
614{
615 using namespace java::lang;
616
617 jsize sz = sizeof(java::lang::String) + len * sizeof(jchar);
618
619 // We assert that for strings allocated this way, the data field
620 // will always point to the object itself. Thus there is no reason
621 // for the garbage collector to scan any of it.
622 // Furthermore, we're about to overwrite the string data, so
623 // initialization of the object is not an issue.
624
625 // String needs no initialization, and there is no finalizer, so
626 // we can go directly to the collector's allocator interface.
627 jstring obj = (jstring) _Jv_AllocPtrFreeObj(sz, &String::class$);
628
629 obj->data = obj;
630 obj->boffset = sizeof(java::lang::String);
631 obj->count = len;
632 obj->cachedHashCode = 0;
245c3c04 633
8ba14152 634 JVMPI_NOTIFY_ALLOC (&String::class$, sz, obj);
245c3c04
BM
635
636 return obj;
637}
638
3610e0d5
TT
639// A version of the above that assumes the object contains no pointers,
640// and requires no finalization. This can't happen if we need pointers
641// to locks.
642#ifdef JV_HASH_SYNCHRONIZATION
643jobject
245c3c04 644_Jv_AllocPtrFreeObject (jclass klass)
3610e0d5
TT
645{
646 _Jv_InitClass (klass);
245c3c04 647 jint size = klass->size ();
3610e0d5
TT
648
649 jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
650
8ba14152 651 JVMPI_NOTIFY_ALLOC (klass, size, obj);
3610e0d5
TT
652
653 return obj;
654}
655#endif /* JV_HASH_SYNCHRONIZATION */
656
657
ee9dd372
TT
658// Allocate a new array of Java objects. Each object is of type
659// `elementClass'. `init' is used to initialize each slot in the
660// array.
661jobjectArray
662_Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
663{
c1a9321f
RS
664 // Creating an array of an unresolved type is impossible. So we throw
665 // the NoClassDefFoundError.
666 if ( _Jv_IsPhantomClass(elementClass) )
667 throw new java::lang::NoClassDefFoundError(elementClass->getName());
668
9d4c2148 669 if (__builtin_expect (count < 0, false))
b3208f56 670 throw new java::lang::NegativeArraySizeException;
ee9dd372 671
93d45562
TT
672 JvAssert (! elementClass->isPrimitive ());
673
5bb11b2e 674 // Ensure that elements pointer is properly aligned.
93d45562 675 jobjectArray obj = NULL;
5bb11b2e 676 size_t size = (size_t) elements (obj);
bf1b388d
RL
677 // Check for overflow.
678 if (__builtin_expect ((size_t) count >
679 (MAX_OBJECT_SIZE - 1 - size) / sizeof (jobject), false))
680 throw no_memory;
681
93d45562 682 size += count * sizeof (jobject);
58eb6e7c 683
421f9e60
AH
684 jclass klass = _Jv_GetArrayClass (elementClass,
685 elementClass->getClassLoaderInternal());
58eb6e7c 686
bf3b8e42 687 obj = (jobjectArray) _Jv_AllocArray (size, klass);
2721806e
TT
688 // Cast away const.
689 jsize *lp = const_cast<jsize *> (&obj->length);
690 *lp = count;
ee9dd372
TT
691 // We know the allocator returns zeroed memory. So don't bother
692 // zeroing it again.
693 if (init)
694 {
5bb11b2e 695 jobject *ptr = elements(obj);
ee9dd372
TT
696 while (--count >= 0)
697 *ptr++ = init;
698 }
ee9dd372
TT
699 return obj;
700}
701
702// Allocate a new array of primitives. ELTYPE is the type of the
703// element, COUNT is the size of the array.
704jobject
705_Jv_NewPrimArray (jclass eltype, jint count)
706{
707 int elsize = eltype->size();
9d4c2148 708 if (__builtin_expect (count < 0, false))
b3208f56 709 throw new java::lang::NegativeArraySizeException;
ee9dd372 710
93d45562
TT
711 JvAssert (eltype->isPrimitive ());
712 jobject dummy = NULL;
713 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
714
ee9dd372 715 // Check for overflow.
1143c0a0 716 if (__builtin_expect ((size_t) count >
bf1b388d 717 (MAX_OBJECT_SIZE - size) / elsize, false))
b3208f56 718 throw no_memory;
ee9dd372 719
5bb11b2e 720 jclass klass = _Jv_GetArrayClass (eltype, 0);
bf3b8e42 721
3610e0d5
TT
722# ifdef JV_HASH_SYNCHRONIZATION
723 // Since the vtable is always statically allocated,
724 // these are completely pointerfree! Make sure the GC doesn't touch them.
725 __JArray *arr =
726 (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
727 memset((char *)arr + size, 0, elsize * count);
728# else
bf3b8e42 729 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
3610e0d5
TT
730 // Note that we assume we are given zeroed memory by the allocator.
731# endif
2721806e
TT
732 // Cast away const.
733 jsize *lp = const_cast<jsize *> (&arr->length);
734 *lp = count;
ee9dd372 735
ee9dd372
TT
736 return arr;
737}
738
ee9dd372
TT
739jobject
740_Jv_NewArray (jint type, jint size)
741{
742 switch (type)
743 {
744 case 4: return JvNewBooleanArray (size);
745 case 5: return JvNewCharArray (size);
746 case 6: return JvNewFloatArray (size);
747 case 7: return JvNewDoubleArray (size);
748 case 8: return JvNewByteArray (size);
749 case 9: return JvNewShortArray (size);
750 case 10: return JvNewIntArray (size);
751 case 11: return JvNewLongArray (size);
752 }
68940f3f
TT
753 throw new java::lang::InternalError
754 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
ee9dd372
TT
755}
756
62a04081
TT
757// Allocate a possibly multi-dimensional array but don't check that
758// any array length is <0.
759static jobject
760_Jv_NewMultiArrayUnchecked (jclass type, jint dimensions, jint *sizes)
ee9dd372
TT
761{
762 JvAssert (type->isArray());
763 jclass element_type = type->getComponentType();
764 jobject result;
765 if (element_type->isPrimitive())
766 result = _Jv_NewPrimArray (element_type, sizes[0]);
767 else
768 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
769
770 if (dimensions > 1)
771 {
772 JvAssert (! element_type->isPrimitive());
773 JvAssert (element_type->isArray());
774 jobject *contents = elements ((jobjectArray) result);
775 for (int i = 0; i < sizes[0]; ++i)
62a04081
TT
776 contents[i] = _Jv_NewMultiArrayUnchecked (element_type, dimensions - 1,
777 sizes + 1);
ee9dd372
TT
778 }
779
780 return result;
781}
782
62a04081
TT
783jobject
784_Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
785{
786 for (int i = 0; i < dimensions; ++i)
787 if (sizes[i] < 0)
788 throw new java::lang::NegativeArraySizeException;
789
790 return _Jv_NewMultiArrayUnchecked (type, dimensions, sizes);
791}
792
ee9dd372
TT
793jobject
794_Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
795{
b2ed63b4
AH
796 // Creating an array of an unresolved type is impossible. So we throw
797 // the NoClassDefFoundError.
798 if (_Jv_IsPhantomClass(array_type))
799 throw new java::lang::NoClassDefFoundError(array_type->getName());
800
ee9dd372
TT
801 va_list args;
802 jint sizes[dimensions];
803 va_start (args, dimensions);
804 for (int i = 0; i < dimensions; ++i)
805 {
806 jint size = va_arg (args, jint);
62a04081
TT
807 if (size < 0)
808 throw new java::lang::NegativeArraySizeException;
ee9dd372
TT
809 sizes[i] = size;
810 }
811 va_end (args);
812
62a04081 813 return _Jv_NewMultiArrayUnchecked (array_type, dimensions, sizes);
ee9dd372
TT
814}
815
816\f
817
45597167 818// Ensure 8-byte alignment, for hash synchronization.
107abb2f 819#define DECLARE_PRIM_TYPE(NAME) \
45597167 820 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
107abb2f 821
dfe5a36e
MM
822DECLARE_PRIM_TYPE(byte)
823DECLARE_PRIM_TYPE(short)
824DECLARE_PRIM_TYPE(int)
825DECLARE_PRIM_TYPE(long)
826DECLARE_PRIM_TYPE(boolean)
827DECLARE_PRIM_TYPE(char)
828DECLARE_PRIM_TYPE(float)
829DECLARE_PRIM_TYPE(double)
830DECLARE_PRIM_TYPE(void)
107abb2f
BM
831
832void
2cf94e58 833_Jv_InitPrimClass (jclass cl, const char *cname, char sig, int len)
107abb2f
BM
834{
835 using namespace java::lang::reflect;
836
68940f3f
TT
837 // We must set the vtable for the class; the Java constructor
838 // doesn't do this.
839 (*(_Jv_VTable **) cl) = java::lang::Class::class$.vtable;
840
841 // Initialize the fields we care about. We do this in the same
842 // order they are declared in Class.h.
107abb2f
BM
843 cl->name = _Jv_makeUtf8Const ((char *) cname, -1);
844 cl->accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
845 cl->method_count = sig;
846 cl->size_in_bytes = len;
847 cl->vtable = JV_PRIMITIVE_VTABLE;
848 cl->state = JV_STATE_DONE;
849 cl->depth = -1;
107abb2f 850}
ee9dd372
TT
851
852jclass
8b6e7690
TT
853_Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader,
854 char **endp)
ee9dd372 855{
8b6e7690
TT
856 // First count arrays.
857 int array_count = 0;
858 while (*sig == '[')
859 {
860 ++sig;
861 ++array_count;
862 }
863
864 jclass result = NULL;
ee9dd372
TT
865 switch (*sig)
866 {
867 case 'B':
8b6e7690
TT
868 result = JvPrimClass (byte);
869 break;
ee9dd372 870 case 'S':
8b6e7690
TT
871 result = JvPrimClass (short);
872 break;
ee9dd372 873 case 'I':
8b6e7690
TT
874 result = JvPrimClass (int);
875 break;
ee9dd372 876 case 'J':
8b6e7690
TT
877 result = JvPrimClass (long);
878 break;
ee9dd372 879 case 'Z':
8b6e7690
TT
880 result = JvPrimClass (boolean);
881 break;
ee9dd372 882 case 'C':
8b6e7690
TT
883 result = JvPrimClass (char);
884 break;
ee9dd372 885 case 'F':
8b6e7690
TT
886 result = JvPrimClass (float);
887 break;
ee9dd372 888 case 'D':
8b6e7690
TT
889 result = JvPrimClass (double);
890 break;
ee9dd372 891 case 'V':
8b6e7690
TT
892 result = JvPrimClass (void);
893 break;
ee9dd372
TT
894 case 'L':
895 {
8b6e7690
TT
896 char *save = ++sig;
897 while (*sig && *sig != ';')
898 ++sig;
899 // Do nothing if signature appears to be malformed.
900 if (*sig == ';')
901 {
902 _Jv_Utf8Const *name = _Jv_makeUtf8Const (save, sig - save);
903 result = _Jv_FindClass (name, loader);
904 }
905 break;
b64295c4 906 }
8b6e7690
TT
907 default:
908 // Do nothing -- bad signature.
909 break;
ee9dd372 910 }
b64295c4 911
8b6e7690
TT
912 if (endp)
913 {
914 // Not really the "end", but the last valid character that we
915 // looked at.
916 *endp = sig;
917 }
918
919 if (! result)
920 return NULL;
921
922 // Find arrays.
923 while (array_count-- > 0)
924 result = _Jv_GetArrayClass (result, loader);
925 return result;
ee9dd372
TT
926}
927
c1a9321f
RS
928
929jclass
930_Jv_FindClassFromSignatureNoException (char *sig, java::lang::ClassLoader *loader,
931 char **endp)
932{
933 jclass klass;
934
935 try
936 {
937 klass = _Jv_FindClassFromSignature(sig, loader, endp);
938 }
939 catch (java::lang::NoClassDefFoundError *ncdfe)
940 {
941 return NULL;
942 }
943 catch (java::lang::ClassNotFoundException *cnfe)
944 {
945 return NULL;
946 }
947
948 return klass;
949}
ee9dd372
TT
950
951JArray<jstring> *
952JvConvertArgv (int argc, const char **argv)
953{
954 if (argc < 0)
955 argc = 0;
36739040 956 jobjectArray ar = JvNewObjectArray(argc, &java::lang::String::class$, NULL);
1ca1c91b
TT
957 jobject *ptr = elements(ar);
958 jbyteArray bytes = NULL;
ee9dd372
TT
959 for (int i = 0; i < argc; i++)
960 {
961 const char *arg = argv[i];
1ca1c91b
TT
962 int len = strlen (arg);
963 if (bytes == NULL || bytes->length < len)
964 bytes = JvNewByteArray (len);
965 jbyte *bytePtr = elements (bytes);
966 // We assume jbyte == char.
967 memcpy (bytePtr, arg, len);
968
969 // Now convert using the default encoding.
970 *ptr++ = new java::lang::String (bytes, 0, len);
ee9dd372
TT
971 }
972 return (JArray<jstring>*) ar;
973}
974
975// FIXME: These variables are static so that they will be
976// automatically scanned by the Boehm collector. This is needed
977// because with qthreads the collector won't scan the initial stack --
978// it will only scan the qthreads stacks.
979
980// Command line arguments.
2dc55bc9 981static JArray<jstring> *arg_vec;
ee9dd372 982
ee9dd372
TT
983// The primary thread.
984static java::lang::Thread *main_thread;
985
ffccc6be
AG
986#ifndef DISABLE_GETENV_PROPERTIES
987
988static char *
989next_property_key (char *s, size_t *length)
990{
991 size_t l = 0;
992
993 JvAssert (s);
994
995 // Skip over whitespace
996 while (isspace (*s))
997 s++;
998
999 // If we've reached the end, return NULL. Also return NULL if for
1000 // some reason we've come across a malformed property string.
1001 if (*s == 0
1002 || *s == ':'
1003 || *s == '=')
1004 return NULL;
1005
1006 // Determine the length of the property key.
1007 while (s[l] != 0
1008 && ! isspace (s[l])
1009 && s[l] != ':'
1010 && s[l] != '=')
1011 {
1012 if (s[l] == '\\'
1013 && s[l+1] != 0)
1014 l++;
1015 l++;
1016 }
1017
1018 *length = l;
1019
1020 return s;
1021}
1022
1023static char *
1024next_property_value (char *s, size_t *length)
1025{
1026 size_t l = 0;
1027
1028 JvAssert (s);
1029
1030 while (isspace (*s))
1031 s++;
1032
1033 if (*s == ':'
1034 || *s == '=')
1035 s++;
1036
1037 while (isspace (*s))
1038 s++;
1039
ffccc6be
AG
1040 // Determine the length of the property value.
1041 while (s[l] != 0
1042 && ! isspace (s[l])
1043 && s[l] != ':'
1044 && s[l] != '=')
1045 {
1046 if (s[l] == '\\'
1047 && s[l+1] != 0)
1048 l += 2;
1049 else
1050 l++;
1051 }
1052
1053 *length = l;
1054
1055 return s;
1056}
1057
1058static void
1059process_gcj_properties ()
1060{
1061 char *props = getenv("GCJ_PROPERTIES");
ffccc6be
AG
1062
1063 if (NULL == props)
1064 return;
1065
38fd6679
AS
1066 // Later on we will write \0s into this string. It is simplest to
1067 // just duplicate it here.
1068 props = strdup (props);
1069
1070 char *p = props;
1071 size_t length;
1072 size_t property_count = 0;
1073
ffccc6be
AG
1074 // Whip through props quickly in order to count the number of
1075 // property values.
1076 while (p && (p = next_property_key (p, &length)))
1077 {
1078 // Skip to the end of the key
1079 p += length;
1080
1081 p = next_property_value (p, &length);
1082 if (p)
1083 p += length;
1084
1085 property_count++;
1086 }
1087
1088 // Allocate an array of property value/key pairs.
1089 _Jv_Environment_Properties =
1090 (property_pair *) malloc (sizeof(property_pair)
1091 * (property_count + 1));
1092
1093 // Go through the properties again, initializing _Jv_Properties
1094 // along the way.
1095 p = props;
1096 property_count = 0;
1097 while (p && (p = next_property_key (p, &length)))
1098 {
1099 _Jv_Environment_Properties[property_count].key = p;
1100 _Jv_Environment_Properties[property_count].key_length = length;
1101
1102 // Skip to the end of the key
1103 p += length;
1104
1105 p = next_property_value (p, &length);
1106
1107 _Jv_Environment_Properties[property_count].value = p;
1108 _Jv_Environment_Properties[property_count].value_length = length;
1109
1110 if (p)
1111 p += length;
1112
1113 property_count++;
1114 }
1115 memset ((void *) &_Jv_Environment_Properties[property_count],
1116 0, sizeof (property_pair));
ffccc6be 1117
08bc8777
PB
1118 // Null terminate the strings.
1119 for (property_pair *prop = &_Jv_Environment_Properties[0];
1120 prop->key != NULL;
1121 prop++)
1122 {
1123 prop->key[prop->key_length] = 0;
1124 prop->value[prop->value_length] = 0;
1125 }
ffccc6be
AG
1126}
1127#endif // DISABLE_GETENV_PROPERTIES
1128
107abb2f
BM
1129namespace gcj
1130{
1131 _Jv_Utf8Const *void_signature;
1132 _Jv_Utf8Const *clinit_name;
1133 _Jv_Utf8Const *init_name;
1134 _Jv_Utf8Const *finit_name;
afb2dec5
BM
1135
1136 bool runtimeInitialized = false;
f5ea88a3
BM
1137
1138 // When true, print debugging information about class loading.
1139 bool verbose_class_flag;
1140
1141 // When true, enable the bytecode verifier and BC-ABI type verification.
1142 bool verifyClasses = true;
11922361
BM
1143
1144 // Thread stack size specified by the -Xss runtime argument.
1145 size_t stack_size = 0;
97b8365c
TT
1146
1147 // Start time of the VM
1148 jlong startTime = 0;
1149
1150 // Arguments passed to the VM
1151 JArray<jstring>* vmArgs;
1152
1153 // Currently loaded classes
1154 jint loadedClasses = 0;
1155
1156 // Unloaded classes
1157 jlong unloadedClasses = 0;
107abb2f
BM
1158}
1159
b6121641
TF
1160// We accept all non-standard options accepted by Sun's java command,
1161// for compatibility with existing application launch scripts.
1162static jint
1163parse_x_arg (char* option_string)
1164{
1165 if (strlen (option_string) <= 0)
1166 return -1;
1167
1168 if (! strcmp (option_string, "int"))
1169 {
1170 // FIXME: this should cause the vm to never load shared objects
1171 }
1172 else if (! strcmp (option_string, "mixed"))
1173 {
1174 // FIXME: allow interpreted and native code
1175 }
1176 else if (! strcmp (option_string, "batch"))
1177 {
1178 // FIXME: disable background JIT'ing
1179 }
1180 else if (! strcmp (option_string, "debug"))
1181 {
b8caf93e
KS
1182 remoteDebug = true;
1183 }
86acf60c 1184#ifdef INTERPRETER
b8caf93e
KS
1185 else if (! strncmp (option_string, "runjdwp:", 8))
1186 {
1187 if (strlen (option_string) > 8)
1188 jdwpOptions = &option_string[8];
1189 else
1190 {
1191 fprintf (stderr,
1192 "libgcj: argument required for JDWP options");
1193 return -1;
1194 }
b6121641 1195 }
86acf60c 1196#endif // INTERPRETER
b6121641
TF
1197 else if (! strncmp (option_string, "bootclasspath:", 14))
1198 {
1199 // FIXME: add a parse_bootclasspath_arg function
1200 }
1201 else if (! strncmp (option_string, "bootclasspath/a:", 16))
1202 {
1203 }
1204 else if (! strncmp (option_string, "bootclasspath/p:", 16))
1205 {
1206 }
1207 else if (! strcmp (option_string, "check:jni"))
1208 {
1209 // FIXME: enable strict JNI checking
1210 }
1211 else if (! strcmp (option_string, "future"))
1212 {
1213 // FIXME: enable strict class file format checks
1214 }
1215 else if (! strcmp (option_string, "noclassgc"))
1216 {
1217 // FIXME: disable garbage collection for classes
1218 }
1219 else if (! strcmp (option_string, "incgc"))
1220 {
1221 // FIXME: incremental garbage collection
1222 }
1223 else if (! strncmp (option_string, "loggc:", 6))
1224 {
1225 if (option_string[6] == '\0')
1226 {
1227 fprintf (stderr,
1228 "libgcj: filename argument expected for loggc option\n");
1229 return -1;
1230 }
1231 // FIXME: set gc logging filename
1232 }
1233 else if (! strncmp (option_string, "ms", 2))
1234 {
1235 // FIXME: ignore this option until PR 20699 is fixed.
1236 // _Jv_SetInitialHeapSize (option_string + 2);
1237 }
1238 else if (! strncmp (option_string, "mx", 2))
1239 _Jv_SetMaximumHeapSize (option_string + 2);
1240 else if (! strcmp (option_string, "prof"))
1241 {
1242 // FIXME: enable profiling of program running in vm
1243 }
1244 else if (! strncmp (option_string, "runhprof:", 9))
1245 {
1246 // FIXME: enable specific type of vm profiling. add a
1247 // parse_runhprof_arg function
1248 }
1249 else if (! strcmp (option_string, "rs"))
1250 {
1251 // FIXME: reduced system signal usage. disable thread dumps,
1252 // only terminate in response to user-initiated calls,
1253 // e.g. System.exit()
1254 }
1255 else if (! strncmp (option_string, "ss", 2))
1256 {
11922361 1257 _Jv_SetStackSize (option_string + 2);
b6121641
TF
1258 }
1259 else if (! strcmp (option_string, "X:+UseAltSigs"))
1260 {
1261 // FIXME: use signals other than SIGUSR1 and SIGUSR2
1262 }
1263 else if (! strcmp (option_string, "share:off"))
1264 {
1265 // FIXME: don't share class data
1266 }
1267 else if (! strcmp (option_string, "share:auto"))
1268 {
1269 // FIXME: share class data where possible
1270 }
1271 else if (! strcmp (option_string, "share:on"))
1272 {
1273 // FIXME: fail if impossible to share class data
1274 }
6e7b20ac
TT
1275 else
1276 {
1277 // Unrecognized.
1278 return -1;
1279 }
b6121641
TF
1280 return 0;
1281}
1282
bc71e4a2
TF
1283static jint
1284parse_verbose_args (char* option_string,
b6121641 1285 bool ignore_unrecognized)
bc71e4a2 1286{
23a8ae8d 1287 size_t len = sizeof ("-verbose") - 1;
bc71e4a2
TF
1288
1289 if (strlen (option_string) < len)
1290 return -1;
1291
1292 if (option_string[len] == ':'
1293 && option_string[len + 1] != '\0')
1294 {
1295 char* verbose_args = option_string + len + 1;
bc71e4a2
TF
1296
1297 do
1298 {
1299 if (! strncmp (verbose_args,
23a8ae8d
TF
1300 "gc", sizeof ("gc") - 1))
1301 {
1302 if (verbose_args[sizeof ("gc") - 1] == '\0'
1303 || verbose_args[sizeof ("gc") - 1] == ',')
1304 {
1305 // FIXME: we should add functions to boehm-gc that
1306 // toggle GC_print_stats, GC_PRINT_ADDRESS_MAP and
1307 // GC_print_back_height.
1308 verbose_args += sizeof ("gc") - 1;
1309 }
1310 else
1311 {
1312 verbose_arg_err:
1313 fprintf (stderr, "libgcj: unknown verbose option: %s\n",
1314 option_string);
1315 return -1;
1316 }
1317 }
bc71e4a2
TF
1318 else if (! strncmp (verbose_args,
1319 "class",
23a8ae8d
TF
1320 sizeof ("class") - 1))
1321 {
1322 if (verbose_args[sizeof ("class") - 1] == '\0'
1323 || verbose_args[sizeof ("class") - 1] == ',')
1324 {
1325 gcj::verbose_class_flag = true;
1326 verbose_args += sizeof ("class") - 1;
1327 }
1328 else
1329 goto verbose_arg_err;
1330 }
bc71e4a2 1331 else if (! strncmp (verbose_args, "jni",
23a8ae8d
TF
1332 sizeof ("jni") - 1))
1333 {
1334 if (verbose_args[sizeof ("jni") - 1] == '\0'
1335 || verbose_args[sizeof ("jni") - 1] == ',')
1336 {
1337 // FIXME: enable JNI messages.
1338 verbose_args += sizeof ("jni") - 1;
1339 }
1340 else
1341 goto verbose_arg_err;
1342 }
bc71e4a2
TF
1343 else if (ignore_unrecognized
1344 && verbose_args[0] == 'X')
1345 {
1346 // ignore unrecognized non-standard verbose option
23a8ae8d
TF
1347 while (verbose_args[0] != '\0'
1348 && verbose_args[0] != ',')
1349 verbose_args++;
bc71e4a2 1350 }
23a8ae8d
TF
1351 else if (verbose_args[0] == ',')
1352 {
1353 verbose_args++;
1354 }
1355 else
1356 goto verbose_arg_err;
1357
1358 if (verbose_args[0] == ',')
1359 verbose_args++;
bc71e4a2 1360 }
23a8ae8d 1361 while (verbose_args[0] != '\0');
bc71e4a2
TF
1362 }
1363 else if (option_string[len] == 'g'
1364 && option_string[len + 1] == 'c'
1365 && option_string[len + 2] == '\0')
1366 {
1367 // FIXME: we should add functions to boehm-gc that
1368 // toggle GC_print_stats, GC_PRINT_ADDRESS_MAP and
1369 // GC_print_back_height.
1370 return 0;
1371 }
1372 else if (option_string[len] == '\0')
1373 {
1374 gcj::verbose_class_flag = true;
1375 return 0;
1376 }
1377 else
1378 {
1379 // unrecognized option beginning with -verbose
1380 return -1;
1381 }
1382 return 0;
1383}
1384
86acf60c 1385#ifdef INTERPRETER
3fe9aa9e
KG
1386// This function loads the agent functions for JVMTI from the library indicated
1387// by name. It returns a negative value on failure, the value of which
1388// indicates where ltdl failed, it also prints an error message.
1389static jint
1390load_jvmti_agent (const char *name)
1391{
1392#ifdef USE_LTDL
1393 if (lt_dlinit ())
1394 {
1395 fprintf (stderr,
1396 "libgcj: Error in ltdl init while loading agent library.\n");
1397 return -1;
1398 }
1399
1400 lt_dlhandle lib = lt_dlopenext (name);
1401 if (!lib)
1402 {
1403 fprintf (stderr,
1404 "libgcj: Error opening agent library.\n");
1405 return -2;
1406 }
1407
1408 if (lib)
1409 {
1410 jvmti_agentonload
1411 = (jvmti_agent_onload_func *) lt_dlsym (lib, "Agent_OnLoad");
1412
1413 if (!jvmti_agentonload)
1414 {
1415 fprintf (stderr,
70275c38 1416 "libgcj: Error finding agent function in library %s.\n",
3fe9aa9e
KG
1417 name);
1418 lt_dlclose (lib);
1419 lib = NULL;
1420 return -4;
1421 }
1422 else
1423 {
1424 jvmti_agentonunload
1425 = (jvmti_agent_onunload_func *) lt_dlsym (lib, "Agent_OnUnload");
1426
1427 return 0;
1428 }
1429 }
1430 else
1431 {
1432 fprintf (stderr, "libgcj: Library %s not found in library path.\n", name);
1433 return -3;
1434 }
1435
1436#endif /* USE_LTDL */
1437
1438 // If LTDL cannot be used, return an error code indicating this.
1439 return -99;
1440}
86acf60c 1441#endif // INTERPRETER
3fe9aa9e 1442
bc71e4a2
TF
1443static jint
1444parse_init_args (JvVMInitArgs* vm_args)
1445{
1446 // if _Jv_Compiler_Properties is non-NULL then it needs to be
1447 // re-allocated dynamically.
1448 if (_Jv_Compiler_Properties)
1449 {
1450 const char** props = _Jv_Compiler_Properties;
1451 _Jv_Compiler_Properties = NULL;
1452
1453 for (int i = 0; props[i]; i++)
1454 {
1455 _Jv_Compiler_Properties = (const char**) _Jv_Realloc
1456 (_Jv_Compiler_Properties,
1457 (_Jv_Properties_Count + 1) * sizeof (const char*));
1458 _Jv_Compiler_Properties[_Jv_Properties_Count++] = props[i];
1459 }
1460 }
1461
1462 if (vm_args == NULL)
1463 return 0;
1464
1465 for (int i = 0; i < vm_args->nOptions; ++i)
1466 {
1467 char* option_string = vm_args->options[i].optionString;
3fe9aa9e 1468
bc71e4a2
TF
1469 if (! strcmp (option_string, "vfprintf")
1470 || ! strcmp (option_string, "exit")
1471 || ! strcmp (option_string, "abort"))
1472 {
1473 // FIXME: we are required to recognize these, but for
1474 // now we don't handle them in any way.
1475 continue;
1476 }
1477 else if (! strncmp (option_string,
1478 "-verbose", sizeof ("-verbose") - 1))
1479 {
1480 jint result = parse_verbose_args (option_string,
b6121641 1481 vm_args->ignoreUnrecognized);
bc71e4a2
TF
1482 if (result < 0)
1483 return result;
1484 }
1485 else if (! strncmp (option_string, "-D", 2))
1486 {
1487 _Jv_Compiler_Properties = (const char**) _Jv_Realloc
1488 (_Jv_Compiler_Properties,
1489 (_Jv_Properties_Count + 1) * sizeof (char*));
1490
1491 _Jv_Compiler_Properties[_Jv_Properties_Count++] =
1492 strdup (option_string + 2);
1493
1494 continue;
1495 }
86acf60c 1496#ifdef INTERPRETER
3fe9aa9e
KG
1497 else if (! strncmp (option_string, "-agentlib", sizeof ("-agentlib") - 1))
1498 {
1499 char *strPtr;
1500
1501 if (strlen(option_string) > (sizeof ("-agentlib:") - 1))
1502 strPtr = &option_string[sizeof ("-agentlib:") - 1];
1503 else
1504 {
1505 fprintf (stderr,
1506 "libgcj: Malformed agentlib argument %s: expected lib name\n",
1507 option_string);
1508 return -1;
1509 }
1510
1511 // These are optional arguments to pass to the agent library.
1512 jvmti_agent_opts = strchr (strPtr, '=');
1513
1514 if (! strncmp (strPtr, "jdwp", 4))
1515 {
1516 // We want to run JDWP here so set the correct variables.
1517 remoteDebug = true;
1518 jdwpOptions = ++jvmti_agent_opts;
1519 }
1520 else
1521 {
1522 jint nameLength;
1523
1524 if (jvmti_agent_opts == NULL)
1525 nameLength = strlen (strPtr);
1526 else
1527 {
1528 nameLength = jvmti_agent_opts - strPtr;
1529 jvmti_agent_opts++;
1530 }
1531
1532 char lib_name[nameLength + 3 + 1];
1533 strcpy (lib_name, "lib");
1534 strncat (lib_name, strPtr, nameLength);
1535
1536 jint result = load_jvmti_agent (lib_name);
1537
1538 if (result < 0)
1539 {
1540 return -1;
1541 }
e1b871ec
KS
1542
1543 // Mark JVMTI active
1544 JVMTI::enabled = true;
3fe9aa9e
KG
1545 }
1546
1547 continue;
1548 }
1549 else if (! strncmp (option_string, "-agentpath:",
1550 sizeof ("-agentpath:") - 1))
1551 {
1552 char *strPtr;
1553
1554 if (strlen(option_string) > 10)
1555 strPtr = &option_string[10];
1556 else
1557 {
1558 fprintf (stderr,
1559 "libgcj: Malformed agentlib argument %s: expected lib path\n",
1560 option_string);
1561 return -1;
1562 }
1563
1564 // These are optional arguments to pass to the agent library.
1565 jvmti_agent_opts = strchr (strPtr, '=');
1566
1567 jint nameLength;
1568
1569 if (jvmti_agent_opts == NULL)
1570 nameLength = strlen (strPtr);
1571 else
1572 {
1573 nameLength = jvmti_agent_opts - strPtr;
1574 jvmti_agent_opts++;
1575 }
1576
1577 char lib_name[nameLength + 3 + 1];
1578 strcpy (lib_name, "lib");
1579 strncat (lib_name, strPtr, nameLength);
1580 jint result = load_jvmti_agent (strPtr);
1581
1582 if (result < 0)
1583 {
1584 return -1;
1585 }
1586
e1b871ec
KS
1587 // Mark JVMTI active
1588 JVMTI::enabled = true;
3fe9aa9e
KG
1589 continue;
1590 }
86acf60c 1591#endif // INTERPRETER
6e7b20ac 1592 else
b6121641 1593 {
6e7b20ac 1594 int r = -1;
b6121641 1595 if (option_string[0] == '_')
6e7b20ac
TT
1596 r = parse_x_arg (option_string + 1);
1597 else if (! strncmp (option_string, "-X", 2))
1598 r = parse_x_arg (option_string + 2);
1599
1600 if (r == -1 && ! vm_args->ignoreUnrecognized)
b6121641 1601 {
b6121641
TF
1602 fprintf (stderr, "libgcj: unknown option: %s\n", option_string);
1603 return -1;
1604 }
bc71e4a2
TF
1605 }
1606 }
1607 return 0;
1608}
1609
c93d7fae 1610jint
bc71e4a2 1611_Jv_CreateJavaVM (JvVMInitArgs* vm_args)
9b0cb287 1612{
107abb2f 1613 using namespace gcj;
bc71e4a2 1614
afb2dec5 1615 if (runtimeInitialized)
107abb2f
BM
1616 return -1;
1617
97b8365c
TT
1618 runtimeInitialized = true;
1619 startTime = _Jv_platform_gettimeofday();
1620
bc71e4a2
TF
1621 jint result = parse_init_args (vm_args);
1622 if (result < 0)
1623 return -1;
1624
ffccc6be
AG
1625 PROCESS_GCJ_PROPERTIES;
1626
7e9534bc
DD
1627 /* Threads must be initialized before the GC, so that it inherits the
1628 signal mask. */
107abb2f
BM
1629 _Jv_InitThreads ();
1630 _Jv_InitGC ();
1631 _Jv_InitializeSyncMutex ();
6187fd28
BM
1632
1633#ifdef INTERPRETER
1634 _Jv_InitInterpreter ();
1635#endif
107abb2f 1636
bd760894
RM
1637#ifdef HANDLE_SEGV
1638 INIT_SEGV;
1639#endif
1640
1641#ifdef HANDLE_FPE
1642 INIT_FPE;
1643#endif
1644
107abb2f
BM
1645 /* Initialize Utf8 constants declared in jvm.h. */
1646 void_signature = _Jv_makeUtf8Const ("()V", 3);
1647 clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
1648 init_name = _Jv_makeUtf8Const ("<init>", 6);
1649 finit_name = _Jv_makeUtf8Const ("finit$", 6);
1650
1651 /* Initialize built-in classes to represent primitive TYPEs. */
ffe4e037
BM
1652 _Jv_InitPrimClass (&_Jv_byteClass, "byte", 'B', 1);
1653 _Jv_InitPrimClass (&_Jv_shortClass, "short", 'S', 2);
1654 _Jv_InitPrimClass (&_Jv_intClass, "int", 'I', 4);
1655 _Jv_InitPrimClass (&_Jv_longClass, "long", 'J', 8);
1656 _Jv_InitPrimClass (&_Jv_booleanClass, "boolean", 'Z', 1);
1657 _Jv_InitPrimClass (&_Jv_charClass, "char", 'C', 2);
1658 _Jv_InitPrimClass (&_Jv_floatClass, "float", 'F', 4);
1659 _Jv_InitPrimClass (&_Jv_doubleClass, "double", 'D', 8);
1660 _Jv_InitPrimClass (&_Jv_voidClass, "void", 'V', 0);
107abb2f 1661
092b50e2
NB
1662 // We have to initialize this fairly early, to avoid circular class
1663 // initialization. In particular we want to start the
1664 // initialization of ClassLoader before we start the initialization
1665 // of VMClassLoader.
1666 _Jv_InitClass (&java::lang::ClassLoader::class$);
bd760894 1667
aa893047 1668 // Set up the system class loader and the bootstrap class loader.
ef874386
TT
1669 gnu::gcj::runtime::ExtensionClassLoader::initialize();
1670 java::lang::VMClassLoader::initialize(JvNewStringLatin1(TOOLEXECLIBDIR));
092b50e2 1671
3fd80100
TT
1672 _Jv_RegisterBootstrapPackages();
1673
c93d7fae 1674 no_memory = new java::lang::OutOfMemoryError;
092b50e2 1675
c93d7fae
PB
1676#ifdef USE_LTDL
1677 LTDL_SET_PRELOADED_SYMBOLS ();
1678#endif
1679
73272ce6 1680 _Jv_platform_initialize ();
c93d7fae
PB
1681
1682 _Jv_JNI_Init ();
86acf60c
DD
1683
1684#ifdef INTERPRETER
e6789bef 1685 _Jv_JVMTI_Init ();
86acf60c 1686#endif
c93d7fae 1687
eeedeb5a
TT
1688 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
1689
1690 // Start the GC finalizer thread. A VirtualMachineError can be
31a7b755 1691 // thrown by the runtime if, say, threads aren't available.
eeedeb5a
TT
1692 try
1693 {
1694 using namespace gnu::gcj::runtime;
1695 FinalizerThread *ft = new FinalizerThread ();
1696 ft->start ();
1697 }
1698 catch (java::lang::VirtualMachineError *ignore)
1699 {
1700 }
1701
8d0941a9
GB
1702 runtimeInitialized = true;
1703
c93d7fae
PB
1704 return 0;
1705}
1706
c93d7fae 1707void
b6121641
TF
1708_Jv_RunMain (JvVMInitArgs *vm_args, jclass klass, const char *name, int argc,
1709 const char **argv, bool is_jar)
c93d7fae 1710{
f82bb1be 1711#ifndef DISABLE_MAIN_ARGS
c4519773 1712 _Jv_SetArgs (argc, argv);
f82bb1be 1713#endif
0acff4bc 1714
2dc55bc9
BM
1715 java::lang::Runtime *runtime = NULL;
1716
2dc55bc9
BM
1717 try
1718 {
b6121641 1719 if (_Jv_CreateJavaVM (vm_args) < 0)
bc71e4a2
TF
1720 {
1721 fprintf (stderr, "libgcj: couldn't create virtual machine\n");
1722 exit (1);
1723 }
97b8365c
TT
1724
1725 if (vm_args == NULL)
1726 gcj::vmArgs = JvConvertArgv(0, NULL);
1727 else
1728 {
1729 const char* vmArgs[vm_args->nOptions];
1730 const char** vmPtr = vmArgs;
1731 struct _Jv_VMOption* optionPtr = vm_args->options;
1732 for (int i = 0; i < vm_args->nOptions; ++i)
1733 *vmPtr++ = (*optionPtr++).optionString;
1734 gcj::vmArgs = JvConvertArgv(vm_args->nOptions, vmArgs);
1735 }
04476544 1736
2dc55bc9
BM
1737 // Get the Runtime here. We want to initialize it before searching
1738 // for `main'; that way it will be set up if `main' is a JNI method.
1739 runtime = java::lang::Runtime::getRuntime ();
6728a61d 1740
b5ee195f
AG
1741#ifdef DISABLE_MAIN_ARGS
1742 arg_vec = JvConvertArgv (0, 0);
1743#else
2dc55bc9 1744 arg_vec = JvConvertArgv (argc - 1, argv + 1);
b5ee195f 1745#endif
1ca1c91b 1746
74fa958e 1747 using namespace gnu::java::lang;
2dc55bc9 1748 if (klass)
74fa958e 1749 main_thread = new MainThread (klass, arg_vec);
2dc55bc9 1750 else
7d6d782d 1751 main_thread = new MainThread (JvNewStringUTF (name),
74fa958e 1752 arg_vec, is_jar);
b8caf93e 1753 _Jv_AttachCurrentThread (main_thread);
86acf60c
DD
1754
1755#ifdef INTERPRETER
3fe9aa9e
KG
1756 // Start JVMTI if an agent function has been found.
1757 if (jvmti_agentonload)
1758 (*jvmti_agentonload) (_Jv_GetJavaVM (), jvmti_agent_opts, NULL);
b8caf93e
KS
1759
1760 // Start JDWP
1761 if (remoteDebug)
1762 {
1763 using namespace gnu::classpath::jdwp;
1764 VMVirtualMachine::initialize ();
1765 Jdwp *jdwp = new Jdwp ();
3fe9aa9e 1766 jdwp->setDaemon (true);
b8caf93e
KS
1767 jdwp->configure (JvNewStringLatin1 (jdwpOptions));
1768 jdwp->start ();
1769
1770 // Wait for JDWP to initialize and start
1771 jdwp->join ();
1772 }
078b3073
KS
1773 // Send VMInit
1774 if (JVMTI_REQUESTED_EVENT (VMInit))
1775 _Jv_JVMTI_PostEvent (JVMTI_EVENT_VM_INIT, main_thread);
86acf60c 1776#endif // INTERPRETER
1a558147 1777 }
2dc55bc9 1778 catch (java::lang::Throwable *t)
1a558147 1779 {
2dc55bc9
BM
1780 java::lang::System::err->println (JvNewStringLatin1
1781 ("Exception during runtime initialization"));
1782 t->printStackTrace();
aa893047 1783 if (runtime)
119afc37 1784 java::lang::Runtime::exitNoChecksAccessor (1);
aa893047
TT
1785 // In case the runtime creation failed.
1786 ::exit (1);
1a558147 1787 }
0f94c029 1788
2dc55bc9 1789 _Jv_ThreadRun (main_thread);
2dc55bc9 1790
86acf60c 1791#ifdef INTERPRETER
078b3073
KS
1792 // Send VMDeath
1793 if (JVMTI_REQUESTED_EVENT (VMDeath))
b8caf93e 1794 {
078b3073
KS
1795 java::lang::Thread *thread = java::lang::Thread::currentThread ();
1796 JNIEnv *jni_env = _Jv_GetCurrentJNIEnv ();
1797 _Jv_JVMTI_PostEvent (JVMTI_EVENT_VM_DEATH, thread, jni_env);
b8caf93e 1798 }
86acf60c
DD
1799
1800 // Run JVMTI AgentOnUnload if it exists and an agent is loaded.
3fe9aa9e
KG
1801 if (jvmti_agentonunload)
1802 (*jvmti_agentonunload) (_Jv_GetJavaVM ());
86acf60c 1803#endif // INTERPRETER
b8caf93e 1804
1058a848
TT
1805 // If we got here then something went wrong, as MainThread is not
1806 // supposed to terminate.
1807 ::exit (1);
2dc55bc9 1808}
0f94c029 1809
b6121641
TF
1810void
1811_Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
1812 bool is_jar)
1813{
1814 _Jv_RunMain (NULL, klass, name, argc, argv, is_jar);
1815}
1816
2dc55bc9
BM
1817void
1818JvRunMain (jclass klass, int argc, const char **argv)
1819{
1820 _Jv_RunMain (klass, NULL, argc, argv, false);
ee9dd372
TT
1821}
1822
97b8365c
TT
1823void
1824JvRunMainName (const char *name, int argc, const char **argv)
1825{
1826 _Jv_RunMain (NULL, name, argc, argv, false);
1827}
1828
b8c3c4f0
TT
1829\f
1830
1831// Parse a string and return a heap size.
1832static size_t
11922361 1833parse_memory_size (const char *spec)
b8c3c4f0
TT
1834{
1835 char *end;
1836 unsigned long val = strtoul (spec, &end, 10);
3948f9d0
JS
1837 if (*end == 'k' || *end == 'K')
1838 val *= 1024;
1839 else if (*end == 'm' || *end == 'M')
1840 val *= 1048576;
b8c3c4f0
TT
1841 return (size_t) val;
1842}
1843
1844// Set the initial heap size. This might be ignored by the GC layer.
1845// This must be called before _Jv_RunMain.
1846void
1847_Jv_SetInitialHeapSize (const char *arg)
1848{
11922361 1849 size_t size = parse_memory_size (arg);
b8c3c4f0
TT
1850 _Jv_GCSetInitialHeapSize (size);
1851}
1852
1853// Set the maximum heap size. This might be ignored by the GC layer.
1854// This must be called before _Jv_RunMain.
1855void
1856_Jv_SetMaximumHeapSize (const char *arg)
1857{
11922361 1858 size_t size = parse_memory_size (arg);
b8c3c4f0
TT
1859 _Jv_GCSetMaximumHeapSize (size);
1860}
58eb6e7c 1861
11922361
BM
1862void
1863_Jv_SetStackSize (const char *arg)
1864{
1865 size_t size = parse_memory_size (arg);
1866 gcj::stack_size = size;
1867}
ee9dd372 1868
7e648cf9 1869void *
ddf0fc6c 1870_Jv_Malloc (jsize size)
7e648cf9 1871{
9d4c2148 1872 if (__builtin_expect (size == 0, false))
7e648cf9 1873 size = 1;
ddf0fc6c 1874 void *ptr = malloc ((size_t) size);
9d4c2148 1875 if (__builtin_expect (ptr == NULL, false))
b3208f56 1876 throw no_memory;
ddf0fc6c 1877 return ptr;
7e648cf9
TT
1878}
1879
ee9dd372 1880void *
ddf0fc6c 1881_Jv_Realloc (void *ptr, jsize size)
ee9dd372 1882{
9d4c2148 1883 if (__builtin_expect (size == 0, false))
ee9dd372 1884 size = 1;
ddf0fc6c 1885 ptr = realloc (ptr, (size_t) size);
9d4c2148 1886 if (__builtin_expect (ptr == NULL, false))
b3208f56 1887 throw no_memory;
ee9dd372
TT
1888 return ptr;
1889}
1890
ddf0fc6c
BM
1891void *
1892_Jv_MallocUnchecked (jsize size)
1893{
9d4c2148 1894 if (__builtin_expect (size == 0, false))
ddf0fc6c
BM
1895 size = 1;
1896 return malloc ((size_t) size);
1897}
1898
ee9dd372
TT
1899void
1900_Jv_Free (void* ptr)
1901{
1902 return free (ptr);
1903}
d342a2e1
AH
1904
1905\f
1906
1907// In theory, these routines can be #ifdef'd away on machines which
1908// support divide overflow signals. However, we never know if some
1909// code might have been compiled with "-fuse-divide-subroutine", so we
1910// always include them in libgcj.
1911
1912jint
1913_Jv_divI (jint dividend, jint divisor)
1914{
9d4c2148 1915 if (__builtin_expect (divisor == 0, false))
bec3e66d
AH
1916 {
1917 java::lang::ArithmeticException *arithexception
1918 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1919 throw arithexception;
1920 }
d342a2e1 1921
58eb6e7c 1922 if (dividend == (jint) 0x80000000L && divisor == -1)
d342a2e1
AH
1923 return dividend;
1924
1925 return dividend / divisor;
1926}
1927
1928jint
1929_Jv_remI (jint dividend, jint divisor)
1930{
9d4c2148 1931 if (__builtin_expect (divisor == 0, false))
bec3e66d
AH
1932 {
1933 java::lang::ArithmeticException *arithexception
1934 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1935 throw arithexception;
1936 }
d342a2e1 1937
58eb6e7c 1938 if (dividend == (jint) 0x80000000L && divisor == -1)
d342a2e1 1939 return 0;
bec3e66d 1940
d342a2e1
AH
1941 return dividend % divisor;
1942}
1943
1944jlong
1945_Jv_divJ (jlong dividend, jlong divisor)
1946{
9d4c2148 1947 if (__builtin_expect (divisor == 0, false))
bec3e66d
AH
1948 {
1949 java::lang::ArithmeticException *arithexception
1950 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1951 throw arithexception;
1952 }
1953
58eb6e7c 1954 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
d342a2e1
AH
1955 return dividend;
1956
1957 return dividend / divisor;
1958}
1959
1960jlong
1961_Jv_remJ (jlong dividend, jlong divisor)
1962{
9d4c2148 1963 if (__builtin_expect (divisor == 0, false))
bec3e66d
AH
1964 {
1965 java::lang::ArithmeticException *arithexception
1966 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1967 throw arithexception;
1968 }
1969
58eb6e7c 1970 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
d342a2e1
AH
1971 return 0;
1972
1973 return dividend % divisor;
1974}
ffd94572
TT
1975
1976\f
1977
1978// Return true if SELF_KLASS can access a field or method in
1979// OTHER_KLASS. The field or method's access flags are specified in
1980// FLAGS.
1981jboolean
1982_Jv_CheckAccess (jclass self_klass, jclass other_klass, jint flags)
1983{
1984 using namespace java::lang::reflect;
1985 return ((self_klass == other_klass)
1986 || ((flags & Modifier::PUBLIC) != 0)
1987 || (((flags & Modifier::PROTECTED) != 0)
852993e3 1988 && _Jv_IsAssignableFromSlow (self_klass, other_klass))
ffd94572
TT
1989 || (((flags & Modifier::PRIVATE) == 0)
1990 && _Jv_ClassNameSamePackage (self_klass->name,
1991 other_klass->name)));
1992}
8c0633b7
TF
1993
1994// Prepend GCJ_VERSIONED_LIBDIR to a module search path stored in a C
1995// char array, if the path is not already prefixed by
1996// GCJ_VERSIONED_LIBDIR. Return a newly JvMalloc'd char buffer. The
1997// result should be freed using JvFree.
1998char*
1999_Jv_PrependVersionedLibdir (char* libpath)
2000{
2001 char* retval = 0;
2002
2003 if (libpath && libpath[0] != '\0')
2004 {
2005 if (! strncmp (libpath,
2006 GCJ_VERSIONED_LIBDIR,
2007 sizeof (GCJ_VERSIONED_LIBDIR) - 1))
2008 {
2009 // LD_LIBRARY_PATH is already prefixed with
2010 // GCJ_VERSIONED_LIBDIR.
2011 retval = (char*) _Jv_Malloc (strlen (libpath) + 1);
2012 strcpy (retval, libpath);
2013 }
2014 else
2015 {
2016 // LD_LIBRARY_PATH is not prefixed with
2017 // GCJ_VERSIONED_LIBDIR.
ab6b9a13
TT
2018 char path_sep[2];
2019 path_sep[0] = (char) _Jv_platform_path_separator;
2020 path_sep[1] = '\0';
2021 jsize total = ((sizeof (GCJ_VERSIONED_LIBDIR) - 1)
2022 + 1 /* path separator */ + strlen (libpath) + 1);
8c0633b7
TF
2023 retval = (char*) _Jv_Malloc (total);
2024 strcpy (retval, GCJ_VERSIONED_LIBDIR);
ab6b9a13 2025 strcat (retval, path_sep);
8c0633b7
TF
2026 strcat (retval, libpath);
2027 }
2028 }
2029 else
2030 {
2031 // LD_LIBRARY_PATH was not specified or is empty.
2032 retval = (char*) _Jv_Malloc (sizeof (GCJ_VERSIONED_LIBDIR));
2033 strcpy (retval, GCJ_VERSIONED_LIBDIR);
2034 }
2035
2036 return retval;
2037}
This page took 1.029593 seconds and 5 git commands to generate.