]> git.ipfire.org Git - thirdparty/gcc.git/blame - libjava/java/lang/natClassLoader.cc
Index: gcc/gcc/ChangeLog
[thirdparty/gcc.git] / libjava / java / lang / natClassLoader.cc
CommitLineData
58eb6e7c
AG
1// natClassLoader.cc - Implementation of java.lang.ClassLoader native methods.
2
b54a2715 3/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation
58eb6e7c
AG
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/* Author: Kresten Krab Thorup <krab@gnu.org> */
12
13#include <config.h>
14
15#include <stdlib.h>
0039c16d 16#include <stdio.h>
58eb6e7c
AG
17#include <string.h>
18
27e934d8 19#include <gcj/cni.h>
58eb6e7c 20#include <jvm.h>
27e934d8
TT
21
22#include <java-threads.h>
23#include <java-interp.h>
24
58eb6e7c
AG
25#include <java/lang/Character.h>
26#include <java/lang/Thread.h>
27#include <java/lang/ClassLoader.h>
eb4534a6 28#include <gnu/gcj/runtime/VMClassLoader.h>
58eb6e7c 29#include <java/lang/InternalError.h>
eb4534a6 30#include <java/lang/IllegalAccessError.h>
58eb6e7c 31#include <java/lang/LinkageError.h>
58eb6e7c
AG
32#include <java/lang/NoClassDefFoundError.h>
33#include <java/lang/ClassNotFoundException.h>
34#include <java/lang/ClassCircularityError.h>
35#include <java/lang/IncompatibleClassChangeError.h>
c946ec44 36#include <java/lang/VirtualMachineError.h>
839f8204 37#include <java/lang/VMClassLoader.h>
58eb6e7c 38#include <java/lang/reflect/Modifier.h>
7af85558 39#include <java/lang/Runtime.h>
cb7d9f67 40#include <java/lang/StringBuffer.h>
1d336a09
TT
41#include <java/io/Serializable.h>
42#include <java/lang/Cloneable.h>
43
58eb6e7c
AG
44void
45_Jv_WaitForState (jclass klass, int state)
46{
47 if (klass->state >= state)
48 return;
49
50 _Jv_MonitorEnter (klass) ;
51
963ddbd5
PB
52 if (klass->state == JV_STATE_COMPILED)
53 {
54 klass->state = JV_STATE_LOADED;
55 if (gcj::verbose_class_flag)
b4d49f49 56 fprintf (stderr, "[Loaded (pre-compiled) %s]\n", klass->name->chars());
963ddbd5 57 }
58eb6e7c
AG
58 if (state == JV_STATE_LINKED)
59 {
773c6f00
TT
60 // Must call _Jv_PrepareCompiledClass while holding the class
61 // mutex.
ae724017
JS
62#ifdef INTERPRETER
63 if (_Jv_IsInterpretedClass (klass))
64 _Jv_PrepareClass (klass);
65#endif
eb4534a6 66 _Jv_PrepareCompiledClass (klass);
773c6f00 67 _Jv_MonitorExit (klass);
58eb6e7c
AG
68 return;
69 }
70
71 java::lang::Thread *self = java::lang::Thread::currentThread();
72
73 // this is similar to the strategy for class initialization.
74 // if we already hold the lock, just leave.
75 while (klass->state <= state
76 && klass->thread
77 && klass->thread != self)
78 klass->wait ();
79
80 _Jv_MonitorExit (klass);
81
82 if (klass->state == JV_STATE_ERROR)
b3208f56 83 throw new java::lang::LinkageError;
58eb6e7c
AG
84}
85
4017ae6e
JS
86typedef unsigned int uaddr __attribute__ ((mode (pointer)));
87
eb4534a6 88/** This function does class-preparation for compiled classes.
f7b4fb11 89 NOTE: It contains replicated functionality from
eb4534a6 90 _Jv_ResolvePoolEntry, and this is intentional, since that function
f7b4fb11 91 lives in resolve.cc which is entirely conditionally compiled.
eb4534a6 92 */
58eb6e7c 93void
5e5c1371 94_Jv_PrepareCompiledClass (jclass klass)
58eb6e7c 95{
963ddbd5
PB
96 jint state = klass->state;
97 if (state >= JV_STATE_LINKED)
58eb6e7c
AG
98 return;
99
773c6f00 100 // Short-circuit, so that mutually dependent classes are ok.
58eb6e7c
AG
101 klass->state = JV_STATE_LINKED;
102
103 _Jv_Constants *pool = &klass->constants;
4017ae6e
JS
104
105 // Resolve class constants first, since other constant pool
106 // entries may rely on these.
eb4534a6 107 for (int index = 1; index < pool->size; ++index)
58eb6e7c 108 {
eb4534a6
KKT
109 if (pool->tags[index] == JV_CONSTANT_Class)
110 {
111 _Jv_Utf8Const *name = pool->data[index].utf8;
112
113 jclass found;
b4d49f49
PB
114 if (name->first() == '[')
115 found = _Jv_FindClassFromSignature (name->chars(),
eb4534a6
KKT
116 klass->loader);
117 else
118 found = _Jv_FindClass (name, klass->loader);
119
120 if (! found)
121 {
b4d49f49 122 jstring str = name->toString();
169f75f3 123 throw new java::lang::NoClassDefFoundError (str);
eb4534a6
KKT
124 }
125
f7b4fb11
KKT
126 pool->data[index].clazz = found;
127 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
eb4534a6 128 }
4017ae6e
JS
129 }
130
131 // If superclass looks like a constant pool entry,
132 // resolve it now.
133 if ((uaddr) klass->superclass < pool->size)
b3a45023 134 klass->superclass = pool->data[(uaddr) klass->superclass].clazz;
4017ae6e
JS
135
136 // Likewise for interfaces.
137 for (int i = 0; i < klass->interface_count; i++)
138 if ((uaddr) klass->interfaces[i] < pool->size)
b3a45023 139 klass->interfaces[i] = pool->data[(uaddr) klass->interfaces[i]].clazz;
4017ae6e
JS
140
141 // Resolve the remaining constant pool entries.
142 for (int index = 1; index < pool->size; ++index)
143 {
144 if (pool->tags[index] == JV_CONSTANT_String)
58eb6e7c
AG
145 {
146 jstring str;
d3cc3f10 147
eb4534a6
KKT
148 str = _Jv_NewStringUtf8Const (pool->data[index].utf8);
149 pool->data[index].o = str;
150 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
58eb6e7c
AG
151 }
152 }
153
5e5c1371
TT
154#ifdef INTERPRETER
155 // FIXME: although the comment up top says that this function is
156 // only called for compiled classes, it is actually called for every
157 // class.
158 if (! _Jv_IsInterpretedClass (klass))
4ea9cbf1 159 {
5e5c1371
TT
160#endif /* INTERPRETER */
161 jfieldID f = JvGetFirstStaticField (klass);
162 for (int n = JvNumStaticFields (klass); n > 0; --n)
4ea9cbf1 163 {
5e5c1371 164 int mod = f->getModifiers ();
d47eb5d3
TT
165 // If we have a static String field with a non-null initial
166 // value, we know it points to a Utf8Const.
d55d97f1 167 _Jv_ResolveField(f, klass->loader);
d74bba04 168 if (f->getClass () == &java::lang::String::class$
d47eb5d3 169 && java::lang::reflect::Modifier::isStatic (mod))
5e5c1371
TT
170 {
171 jstring *strp = (jstring *) f->u.addr;
172 if (*strp)
173 *strp = _Jv_NewStringUtf8Const ((_Jv_Utf8Const *) *strp);
174 }
175 f = f->getNextField ();
4ea9cbf1 176 }
5e5c1371 177#ifdef INTERPRETER
4ea9cbf1 178 }
5e5c1371 179#endif /* INTERPRETER */
4ea9cbf1 180
17abdabc
BM
181 if (klass->isInterface ())
182 _Jv_LayoutInterfaceMethods (klass);
183
963ddbd5
PB
184 if (state == JV_STATE_COMPILED && gcj::verbose_class_flag)
185 fprintf (stderr, "[Loaded (pre-compiled) %s]\n",
b4d49f49 186 klass->name->chars());
963ddbd5 187
58eb6e7c 188 klass->notifyAll ();
421f9e60
AH
189
190 _Jv_PushClass (klass);
58eb6e7c
AG
191}
192
193
194//
195// A single class can have many "initiating" class loaders,
196// and a single "defining" class loader. The Defining
197// class loader is what is returned from Class.getClassLoader()
198// and is used when loading dependent classes during resolution.
199// The set of initiating class loaders are used to ensure
200// safety of linking, and is maintained in the hash table
201// "initiated_classes". A defining classloader is by definition also
b9f42bb0 202// initiating, so we only store classes in this table if they have more
58eb6e7c
AG
203// than one class loader associated.
204//
205
206
207// Size of local hash table.
208#define HASH_LEN 1013
209
210// Hash function for Utf8Consts.
b4d49f49 211#define HASH_UTF(Utf) ((Utf)->hash16() % HASH_LEN)
58eb6e7c 212
a1aba4f9
TT
213struct _Jv_LoaderInfo
214{
215 _Jv_LoaderInfo *next;
216 java::lang::Class *klass;
217 java::lang::ClassLoader *loader;
58eb6e7c
AG
218};
219
facc279f
TT
220static _Jv_LoaderInfo *initiated_classes[HASH_LEN];
221static jclass loaded_classes[HASH_LEN];
222
223// This is the root of a linked list of classes
224
225\f
58eb6e7c
AG
226
227jclass
228_Jv_FindClassInCache (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
229{
a1aba4f9 230 JvSynchronize sync (&java::lang::Class::class$);
58eb6e7c
AG
231 jint hash = HASH_UTF (name);
232
a1aba4f9
TT
233 if (loader && loader == java::lang::ClassLoader::getSystemClassLoader())
234 loader = NULL;
235
58eb6e7c
AG
236 // first, if LOADER is a defining loader, then it is also initiating
237 jclass klass;
238 for (klass = loaded_classes[hash]; klass; klass = klass->next)
239 {
240 if (loader == klass->loader && _Jv_equalUtf8Consts (name, klass->name))
241 break;
242 }
243
244 // otherwise, it may be that the class in question was defined
245 // by some other loader, but that the loading was initiated by
246 // the loader in question.
247 if (!klass)
248 {
249 _Jv_LoaderInfo *info;
250 for (info = initiated_classes[hash]; info; info = info->next)
251 {
252 if (loader == info->loader
253 && _Jv_equalUtf8Consts (name, info->klass->name))
254 {
255 klass = info->klass;
256 break;
257 }
258 }
259 }
260
58eb6e7c
AG
261 return klass;
262}
263
264void
265_Jv_UnregisterClass (jclass the_class)
266{
a1aba4f9 267 JvSynchronize sync (&java::lang::Class::class$);
58eb6e7c
AG
268 jint hash = HASH_UTF(the_class->name);
269
270 jclass *klass = &(loaded_classes[hash]);
271 for ( ; *klass; klass = &((*klass)->next))
272 {
273 if (*klass == the_class)
274 {
275 *klass = (*klass)->next;
276 break;
277 }
278 }
279
280 _Jv_LoaderInfo **info = &(initiated_classes[hash]);
715bdd81 281 for ( ; ; info = &((*info)->next))
58eb6e7c 282 {
715bdd81 283 while (*info && (*info)->klass == the_class)
58eb6e7c 284 {
a1aba4f9 285 _Jv_LoaderInfo *old = *info;
58eb6e7c 286 *info = (*info)->next;
a1aba4f9 287 _Jv_Free (old);
58eb6e7c 288 }
715bdd81
AH
289
290 if (*info == NULL)
291 break;
58eb6e7c 292 }
58eb6e7c
AG
293}
294
295void
296_Jv_RegisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
297{
a1aba4f9
TT
298 if (loader && loader == java::lang::ClassLoader::getSystemClassLoader())
299 loader = NULL;
300
301 // This information can't be visible to the GC.
302 _Jv_LoaderInfo *info
303 = (_Jv_LoaderInfo *) _Jv_Malloc (sizeof(_Jv_LoaderInfo));
58eb6e7c
AG
304 jint hash = HASH_UTF(klass->name);
305
a1aba4f9 306 JvSynchronize sync (&java::lang::Class::class$);
58eb6e7c
AG
307 info->loader = loader;
308 info->klass = klass;
309 info->next = initiated_classes[hash];
310 initiated_classes[hash] = info;
58eb6e7c
AG
311}
312
313// This function is called many times during startup, before main() is
107abb2f
BM
314// run. At that point in time we know for certain we are running
315// single-threaded, so we don't need to lock when adding classes to the
316// class chain. At all other times, the caller should synchronize on
317// Class::class$.
58eb6e7c 318void
f1a66265 319_Jv_RegisterClasses (const jclass *classes)
58eb6e7c 320{
58eb6e7c
AG
321 for (; *classes; ++classes)
322 {
323 jclass klass = *classes;
dee45a7f
PB
324
325 (*_Jv_RegisterClassHook) (klass);
58eb6e7c
AG
326
327 // registering a compiled class causes
328 // it to be immediately "prepared".
6e87747b 329 if (klass->state == JV_STATE_NOTHING)
58eb6e7c
AG
330 klass->state = JV_STATE_COMPILED;
331 }
332}
333
f1a66265
GK
334// This is a version of _Jv_RegisterClasses that takes a count.
335void
336_Jv_RegisterClasses_Counted (const jclass * classes, size_t count)
337{
338 size_t i;
339 for (i = 0; i < count; i++)
340 {
341 jclass klass = classes[i];
342
343 (*_Jv_RegisterClassHook) (klass);
344
345 // registering a compiled class causes
346 // it to be immediately "prepared".
347 if (klass->state == JV_STATE_NOTHING)
348 klass->state = JV_STATE_COMPILED;
349 }
350}
351
dee45a7f
PB
352void
353_Jv_RegisterClassHookDefault (jclass klass)
354{
355 jint hash = HASH_UTF (klass->name);
c946ec44
CM
356
357 jclass check_class = loaded_classes[hash];
358
359 // If the class is already registered, don't re-register it.
360 while (check_class != NULL)
361 {
362 if (check_class == klass)
363 {
364 // If you get this, it means you have the same class in two
365 // different libraries.
d12a1873
TT
366#define TEXT "Duplicate class registration: "
367 // We size-limit MESSAGE so that you can't trash the stack.
11598139 368 char message[200];
d12a1873 369 strcpy (message, TEXT);
b4d49f49 370 strncpy (message + sizeof (TEXT) - 1, klass->name->chars(),
d12a1873
TT
371 sizeof (message) - sizeof (TEXT));
372 message[sizeof (message) - 1] = '\0';
afb2dec5
BM
373 if (! gcj::runtimeInitialized)
374 JvFail (message);
375 else
376 {
377 java::lang::String *str = JvNewStringLatin1 (message);
afb2dec5
BM
378 throw new java::lang::VirtualMachineError (str);
379 }
c946ec44
CM
380 }
381
382 check_class = check_class->next;
383 }
384
dee45a7f
PB
385 klass->next = loaded_classes[hash];
386 loaded_classes[hash] = klass;
387}
388
389// A pointer to a function that actually registers a class.
390// Normally _Jv_RegisterClassHookDefault, but could be some other function
391// that registers the class in e.g. a ClassLoader-local table.
392// Should synchronize on Class:class$ while setting/restore this variable.
393
394void (*_Jv_RegisterClassHook) (jclass cl) = _Jv_RegisterClassHookDefault;
395
58eb6e7c
AG
396void
397_Jv_RegisterClass (jclass klass)
398{
399 jclass classes[2];
400 classes[0] = klass;
401 classes[1] = NULL;
402 _Jv_RegisterClasses (classes);
403}
404
58eb6e7c 405jclass
7af85558 406_Jv_FindClass (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
58eb6e7c
AG
407{
408 jclass klass = _Jv_FindClassInCache (name, loader);
409
58eb6e7c
AG
410 if (! klass)
411 {
b4d49f49 412 jstring sname = name->toString();
58eb6e7c 413
a1aba4f9
TT
414 java::lang::ClassLoader *sys
415 = java::lang::ClassLoader::getSystemClassLoader ();
416
58eb6e7c
AG
417 if (loader)
418 {
419 // Load using a user-defined loader, jvmspec 5.3.2
420 klass = loader->loadClass(sname, false);
421
357100a3
TT
422 // If "loader" delegated the loadClass operation to another
423 // loader, explicitly register that it is also an initiating
424 // loader of the given class.
a1aba4f9
TT
425 java::lang::ClassLoader *delegate = (loader == sys
426 ? NULL
427 : loader);
428 if (klass && klass->getClassLoaderInternal () != delegate)
357100a3 429 _Jv_RegisterInitiatingLoader (klass, loader);
58eb6e7c
AG
430 }
431 else
432 {
357100a3 433 // Load using the bootstrap loader jvmspec 5.3.1.
b8c3c4f0 434 klass = sys->loadClass (sname, false);
58eb6e7c 435
b8c3c4f0 436 // Register that we're an initiating loader.
58eb6e7c 437 if (klass)
b8c3c4f0 438 _Jv_RegisterInitiatingLoader (klass, 0);
58eb6e7c
AG
439 }
440 }
441 else
442 {
443 // we need classes to be in the hash while
444 // we're loading, so that they can refer to themselves.
445 _Jv_WaitForState (klass, JV_STATE_LOADED);
446 }
58eb6e7c
AG
447
448 return klass;
449}
450
68940f3f
TT
451jclass
452_Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
453 java::lang::ClassLoader *loader)
454{
f5310108 455 jclass ret = (jclass) _Jv_AllocObject (&java::lang::Class::class$);
68940f3f
TT
456 ret->name = name;
457 ret->superclass = superclass;
458 ret->loader = loader;
58eb6e7c
AG
459
460 _Jv_RegisterClass (ret);
461
462 return ret;
463}
464
5bb11b2e
BM
465static _Jv_IDispatchTable *array_idt = NULL;
466static jshort array_depth = 0;
467static jclass *array_ancestors = NULL;
468
469// Create a class representing an array of ELEMENT and store a pointer to it
470// in element->arrayclass. LOADER is the ClassLoader which _initiated_ the
471// instantiation of this array. ARRAY_VTABLE is the vtable to use for the new
472// array class. This parameter is optional.
473void
474_Jv_NewArrayClass (jclass element, java::lang::ClassLoader *loader,
475 _Jv_VTable *array_vtable)
58eb6e7c 476{
5bb11b2e
BM
477 JvSynchronize sync (element);
478
58eb6e7c
AG
479 _Jv_Utf8Const *array_name;
480 int len;
5bb11b2e
BM
481
482 if (element->arrayclass)
483 return;
484
58eb6e7c 485 if (element->isPrimitive())
bea31ffb
TT
486 {
487 if (element == JvPrimClass (void))
488 throw new java::lang::ClassNotFoundException ();
489 len = 3;
490 }
58eb6e7c 491 else
b4d49f49 492 len = element->name->len() + 5;
58eb6e7c
AG
493
494 {
495 char signature[len];
496 int index = 0;
497 signature[index++] = '[';
5bb11b2e 498 // Compute name of array class.
58eb6e7c
AG
499 if (element->isPrimitive())
500 {
501 signature[index++] = (char) element->method_count;
502 }
503 else
504 {
b4d49f49
PB
505 size_t length = element->name->len();
506 const char *const name = element->name->chars();
58eb6e7c
AG
507 if (name[0] != '[')
508 signature[index++] = 'L';
509 memcpy (&signature[index], name, length);
510 index += length;
511 if (name[0] != '[')
512 signature[index++] = ';';
513 }
514 array_name = _Jv_makeUtf8Const (signature, index);
515 }
516
5bb11b2e 517 // Create new array class.
d74bba04 518 jclass array_class = _Jv_NewClass (array_name, &java::lang::Object::class$,
5bb11b2e
BM
519 element->loader);
520
521 // Note that `vtable_method_count' doesn't include the initial
522 // gc_descr slot.
d74bba04
TT
523 JvAssert (java::lang::Object::class$.vtable_method_count
524 == NUM_OBJECT_METHODS);
525 int dm_count = java::lang::Object::class$.vtable_method_count;
5bb11b2e 526
f5ddf154 527 // Create a new vtable by copying Object's vtable.
5bb11b2e
BM
528 _Jv_VTable *vtable;
529 if (array_vtable)
530 vtable = array_vtable;
531 else
f5ddf154 532 vtable = _Jv_VTable::new_vtable (dm_count);
5bb11b2e 533 vtable->clas = array_class;
d74bba04 534 vtable->gc_descr = java::lang::Object::class$.vtable->gc_descr;
f5ddf154 535 for (int i = 0; i < dm_count; ++i)
d74bba04 536 vtable->set_method (i, java::lang::Object::class$.vtable->get_method (i));
f5ddf154 537
5bb11b2e 538 array_class->vtable = vtable;
d74bba04
TT
539 array_class->vtable_method_count
540 = java::lang::Object::class$.vtable_method_count;
5bb11b2e
BM
541
542 // Stash the pointer to the element type.
543 array_class->methods = (_Jv_Method *) element;
544
545 // Register our interfaces.
d74bba04
TT
546 static jclass interfaces[] =
547 {
548 &java::lang::Cloneable::class$,
549 &java::io::Serializable::class$
550 };
5bb11b2e
BM
551 array_class->interfaces = interfaces;
552 array_class->interface_count = sizeof interfaces / sizeof interfaces[0];
553
554 // Since all array classes have the same interface dispatch table, we can
18e1f2bd 555 // cache one and reuse it. It is not necessary to synchronize this.
5bb11b2e 556 if (!array_idt)
58eb6e7c 557 {
fb863f62 558 _Jv_PrepareConstantTimeTables (array_class);
5bb11b2e
BM
559 array_idt = array_class->idt;
560 array_depth = array_class->depth;
561 array_ancestors = array_class->ancestors;
562 }
563 else
564 {
565 array_class->idt = array_idt;
566 array_class->depth = array_depth;
567 array_class->ancestors = array_ancestors;
568 }
fb863f62 569
5bb11b2e
BM
570 using namespace java::lang::reflect;
571 {
572 // Array classes are "abstract final"...
573 _Jv_ushort accflags = Modifier::FINAL | Modifier::ABSTRACT;
574 // ... and inherit accessibility from element type, per vmspec 5.3.3.2
575 accflags |= (element->accflags & Modifier::PUBLIC);
576 accflags |= (element->accflags & Modifier::PROTECTED);
577 accflags |= (element->accflags & Modifier::PRIVATE);
578 array_class->accflags = accflags;
579 }
58eb6e7c 580
5bb11b2e
BM
581 // An array class has no visible instance fields. "length" is invisible to
582 // reflection.
58eb6e7c 583
5bb11b2e
BM
584 // say this class is initialized and ready to go!
585 array_class->state = JV_STATE_DONE;
58eb6e7c 586
5bb11b2e
BM
587 // vmspec, section 5.3.3 describes this
588 if (element->loader != loader)
589 _Jv_RegisterInitiatingLoader (array_class, loader);
58eb6e7c 590
5bb11b2e 591 element->arrayclass = array_class;
58eb6e7c 592}
421f9e60
AH
593
594static jclass stack_head;
595
596// These two functions form a stack of classes. When a class is loaded
597// it is pushed onto the stack by the class loader; this is so that
598// StackTrace can quickly determine which classes have been loaded.
599
600jclass
601_Jv_PopClass (void)
602{
603 JvSynchronize sync (&java::lang::Class::class$);
604 if (stack_head)
605 {
606 jclass tmp = stack_head;
607 stack_head = tmp->chain;
608 return tmp;
609 }
610 return NULL;
611}
612
613void
614_Jv_PushClass (jclass k)
615{
616 JvSynchronize sync (&java::lang::Class::class$);
617 jclass tmp = stack_head;
618 stack_head = k;
619 k->chain = tmp;
620}