1 // natClassLoader.cc - Implementation of java.lang.ClassLoader native methods.
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 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
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
21 #include <execution.h>
23 #include <java-threads.h>
24 #include <java-interp.h>
26 #include <java/lang/Character.h>
27 #include <java/lang/Thread.h>
28 #include <java/lang/ClassLoader.h>
29 #include <java/lang/InternalError.h>
30 #include <java/lang/IllegalAccessError.h>
31 #include <java/lang/LinkageError.h>
32 #include <java/lang/NoClassDefFoundError.h>
33 #include <java/lang/ClassNotFoundException.h>
34 #include <java/lang/ClassCircularityError.h>
35 #include <java/lang/IncompatibleClassChangeError.h>
36 #include <java/lang/ClassFormatError.h>
37 #include <java/lang/VirtualMachineError.h>
38 #include <java/lang/VMClassLoader.h>
39 #include <java/lang/reflect/Modifier.h>
40 #include <java/lang/Runtime.h>
41 #include <java/lang/StringBuffer.h>
42 #include <java/io/Serializable.h>
43 #include <java/lang/Cloneable.h>
44 #include <java/util/HashMap.h>
45 #include <gnu/gcj/runtime/BootClassLoader.h>
46 #include <gnu/gcj/runtime/SystemClassLoader.h>
48 // Size of local hash table.
51 // Hash function for Utf8Consts.
52 #define HASH_UTF(Utf) ((Utf)->hash16() % HASH_LEN)
54 // This records classes which will be registered with the system class
55 // loader when it is initialized.
56 static jclass system_class_list;
58 // This is used as the value of system_class_list after we have
59 // initialized the system class loader; it lets us know that we should
60 // no longer pay attention to the system abi flag.
61 #define SYSTEM_LOADER_INITIALIZED ((jclass) -1)
63 static jclass loaded_classes[HASH_LEN];
65 // This is the root of a linked list of classes
66 static jclass stack_head;
68 // While bootstrapping we keep a list of classes we found, so that we
69 // can register their packages. There aren't many of these so we
70 // just keep a small buffer here and abort if we overflow.
71 #define BOOTSTRAP_CLASS_LIST_SIZE 20
72 static jclass bootstrap_class_list[BOOTSTRAP_CLASS_LIST_SIZE];
73 static int bootstrap_index;
79 java::lang::ClassLoader::loadClassFromSig(jstring name)
81 int len = _Jv_GetStringUTFLength (name);
83 _Jv_GetStringUTFRegion (name, 0, name->length(), sig);
84 jclass result = _Jv_FindClassFromSignature(sig, this);
86 throw new ClassNotFoundException(name);
92 // This tries to find a class in our built-in cache. This cache is
93 // used only for classes which are linked in to the executable or
94 // loaded via dlopen().
96 _Jv_FindClassInCache (_Jv_Utf8Const *name)
98 JvSynchronize sync (&java::lang::Class::class$);
99 jint hash = HASH_UTF (name);
102 for (klass = loaded_classes[hash]; klass; klass = klass->next_or_version)
104 if (_Jv_equalUtf8Consts (name, klass->name))
112 _Jv_UnregisterClass (jclass the_class)
114 // This can happen if the class could not be defined properly.
115 if (! the_class->name)
118 JvSynchronize sync (&java::lang::Class::class$);
119 jint hash = HASH_UTF(the_class->name);
121 jclass *klass = &(loaded_classes[hash]);
122 for ( ; *klass; klass = &((*klass)->next_or_version))
124 if (*klass == the_class)
126 *klass = (*klass)->next_or_version;
132 // Register an initiating class loader for a given class.
134 _Jv_RegisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
137 loader = java::lang::VMClassLoader::bootLoader;
140 // Very early in the bootstrap process, the Bootstrap classloader may
142 // FIXME: We could maintain a list of these and come back and register
146 loader->loadedClasses->put(klass->name->toString(), klass);
149 // If we found an error while defining an interpreted class, we must
150 // go back and unregister it.
152 _Jv_UnregisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
155 loader = java::lang::VMClassLoader::bootLoader;
156 loader->loadedClasses->remove(klass->name->toString());
160 // Class registration.
162 // There are two kinds of functions that register classes.
166 // These take the address of a class that is in an object file.
167 // Because these classes are not allocated on the heap, It is also
168 // necessary to register the address of the object for garbage
169 // collection. This is used with the "old" C++ ABI and with
170 // -findirect-dispatch -fno-indirect-classes.
174 // These take an initializer struct, create the class, and return the
175 // address of the newly created class to their caller. These are used
176 // with -findirect-dispatch.
178 // _Jv_RegisterClasses() and _Jv_RegisterClasses_Counted() are
179 // functions of Type 1, and _Jv_NewClassFromInitializer() and
180 // _Jv_RegisterNewClasses() are of Type 2.
183 // Check that the file we're trying to load has been compiled with a
184 // compatible version of gcj. In previous versions of libgcj we
185 // silently failed to register classes of an incompatible ABI version,
186 // but this was totally bogus.
188 _Jv_CheckABIVersion (unsigned long value)
190 // We are compatible with GCJ 4.0.0 BC-ABI classes. This release used a
191 // different format for the version ID string.
192 if (value == OLD_GCJ_40_BC_ABI_VERSION)
195 // The 20 low-end bits are used for the version number.
196 unsigned long version = value & 0xfffff;
198 if (value & FLAG_BINARYCOMPAT_ABI)
200 int abi_rev = version % 100;
201 int abi_ver = version - abi_rev;
202 // We are compatible with abi_rev 0 and 1.
203 if (abi_ver == GCJ_40_BC_ABI_VERSION && abi_rev <= 1)
209 if (version == GCJ_CXX_ABI_VERSION)
213 throw new ::java::lang::ClassFormatError
214 (JvNewStringLatin1 ("Library compiled with later ABI version than"
215 " this version of libgcj supports"));
218 // This function is called many times during startup, before main() is
219 // run. At that point in time we know for certain we are running
220 // single-threaded, so we don't need to lock when adding classes to the
221 // class chain. At all other times, the caller should synchronize on
224 _Jv_RegisterClasses (const jclass *classes)
226 _Jv_RegisterLibForGc (classes);
228 for (; *classes; ++classes)
230 jclass klass = *classes;
232 _Jv_CheckABIVersion ((unsigned long) klass->next_or_version);
233 (*_Jv_RegisterClassHook) (klass);
237 // This is a version of _Jv_RegisterClasses that takes a count.
239 _Jv_RegisterClasses_Counted (const jclass * classes, size_t count)
243 _Jv_RegisterLibForGc (classes);
245 for (i = 0; i < count; i++)
247 jclass klass = classes[i];
249 _Jv_CheckABIVersion ((unsigned long) klass->next_or_version);
250 (*_Jv_RegisterClassHook) (klass);
254 // Create a class on the heap from an initializer struct.
256 _Jv_NewClassFromInitializer (const char *class_initializer)
258 const unsigned long version
260 ((::java::lang::Class *)class_initializer)->next_or_version);
261 _Jv_CheckABIVersion (version);
263 /* We create an instance of java::lang::Class and copy all of its
264 fields except the first word (the vtable pointer) from
265 CLASS_INITIALIZER. This first word is pre-initialized by
266 _Jv_AllocObj, and we don't want to overwrite it. */
269 = (jclass)_Jv_AllocObj (sizeof (::java::lang::Class),
270 &::java::lang::Class::class$);
271 const char *src = class_initializer + sizeof (void*);
272 char *dst = (char*)new_class + sizeof (void*);
273 size_t len = (::java::lang::Class::initializerSize (version)
275 memcpy (dst, src, len);
277 new_class->engine = &_Jv_soleIndirectCompiledEngine;
279 (*_Jv_RegisterClassHook) (new_class);
284 // Called by compiler-generated code at DSO initialization. CLASSES
285 // is an array of pairs: the first item of each pair is a pointer to
286 // the initialized data that is a class initializer in a DSO, and the
287 // second is a pointer to a class reference.
288 // _Jv_NewClassFromInitializer() creates the new class (on the Java
289 // heap) and we write the address of the new class into the address
290 // pointed to by the second word.
292 _Jv_RegisterNewClasses (char **classes)
296 const char *initializer;
298 while ((initializer = *classes++))
300 jclass *class_ptr = (jclass *)*classes++;
301 *class_ptr = _Jv_NewClassFromInitializer (initializer);
306 _Jv_RegisterClassHookDefault (jclass klass)
308 // This is bogus, but there doesn't seem to be a better place to do
311 klass->engine = &_Jv_soleCompiledEngine;
313 if (system_class_list != SYSTEM_LOADER_INITIALIZED)
315 unsigned long abi = (unsigned long) klass->next_or_version;
316 if (! _Jv_ClassForBootstrapLoader (abi))
318 klass->next_or_version = system_class_list;
319 system_class_list = klass;
324 jint hash = HASH_UTF (klass->name);
326 // If the class is already registered, don't re-register it.
327 for (jclass check_class = loaded_classes[hash];
329 check_class = check_class->next_or_version)
331 if (check_class == klass)
333 // If you get this, it means you have the same class in two
334 // different libraries.
335 #define TEXT "Duplicate class registration: "
336 // We size-limit MESSAGE so that you can't trash the stack.
338 strcpy (message, TEXT);
339 strncpy (message + sizeof (TEXT) - 1, klass->name->chars(),
340 sizeof (message) - sizeof (TEXT));
341 message[sizeof (message) - 1] = '\0';
342 if (! gcj::runtimeInitialized)
346 java::lang::String *str = JvNewStringLatin1 (message);
347 throw new java::lang::VirtualMachineError (str);
352 klass->next_or_version = loaded_classes[hash];
353 loaded_classes[hash] = klass;
356 // A pointer to a function that actually registers a class.
357 // Normally _Jv_RegisterClassHookDefault, but could be some other function
358 // that registers the class in e.g. a ClassLoader-local table.
359 // Should synchronize on Class:class$ while setting/restore this variable.
361 void (*_Jv_RegisterClassHook) (jclass cl) = _Jv_RegisterClassHookDefault;
364 _Jv_RegisterClass (jclass klass)
369 _Jv_RegisterClasses (classes);
372 // This is used during initialization to register all compiled-in
373 // classes that are not part of the core with the system class loader.
375 _Jv_CopyClassesToSystemLoader (gnu::gcj::runtime::SystemClassLoader *loader)
377 for (jclass klass = system_class_list;
379 klass = klass->next_or_version)
381 klass->loader = loader;
382 loader->addClass(klass);
384 system_class_list = SYSTEM_LOADER_INITIALIZED;
387 // An internal variant of _Jv_FindClass which simply swallows a
388 // NoClassDefFoundError or a ClassNotFoundException. This gives the
389 // caller a chance to evaluate the situation and behave accordingly.
391 _Jv_FindClassNoException (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
397 klass = _Jv_FindClass(name, loader);
399 catch ( java::lang::NoClassDefFoundError *ncdfe )
403 catch ( java::lang::ClassNotFoundException *cnfe )
412 _Jv_FindClass (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
414 // See if the class was already loaded by this loader. This handles
415 // initiating loader checks, as we register classes with their
416 // initiating loaders.
418 java::lang::ClassLoader *boot = java::lang::VMClassLoader::bootLoader;
419 java::lang::ClassLoader *real = loader;
422 jstring sname = name->toString();
423 // We might still be bootstrapping the VM, in which case there
424 // won't be a bootstrap class loader yet.
425 jclass klass = real ? real->findLoadedClass (sname) : NULL;
431 // Load using a user-defined loader, jvmspec 5.3.2.
432 // Note that we explicitly must call the single-argument form.
433 klass = loader->loadClass(sname);
435 // If "loader" delegated the loadClass operation to another
436 // loader, explicitly register that it is also an initiating
437 // loader of the given class.
438 java::lang::ClassLoader *delegate = (loader == boot
441 if (klass && klass->getClassLoaderInternal () != delegate)
442 _Jv_RegisterInitiatingLoader (klass, loader);
446 // Load using the bootstrap loader jvmspec 5.3.1.
447 klass = java::lang::VMClassLoader::loadClass (sname, false);
449 // Register that we're an initiating loader.
451 _Jv_RegisterInitiatingLoader (klass, 0);
455 // Not even a bootstrap loader, try the built-in cache.
456 klass = _Jv_FindClassInCache (name);
461 for (int i = 0; i < bootstrap_index; ++i)
463 if (bootstrap_class_list[i] == klass)
471 if (bootstrap_index == BOOTSTRAP_CLASS_LIST_SIZE)
473 bootstrap_class_list[bootstrap_index++] = klass;
483 _Jv_RegisterBootstrapPackages ()
485 for (int i = 0; i < bootstrap_index; ++i)
486 java::lang::VMClassLoader::definePackageForNative(bootstrap_class_list[i]->getName());
490 _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
491 java::lang::ClassLoader *loader)
493 jclass ret = (jclass) _Jv_AllocObject (&java::lang::Class::class$);
495 ret->superclass = superclass;
496 ret->loader = loader;
498 _Jv_RegisterInitiatingLoader (ret, loader);
503 static _Jv_IDispatchTable *array_idt = NULL;
504 static jshort array_depth = 0;
505 static jclass *array_ancestors = NULL;
507 static jclass interfaces[] =
509 &java::lang::Cloneable::class$,
510 &java::io::Serializable::class$
513 // Create a class representing an array of ELEMENT and store a pointer to it
514 // in element->arrayclass. LOADER is the ClassLoader which _initiated_ the
515 // instantiation of this array. ARRAY_VTABLE is the vtable to use for the new
516 // array class. This parameter is optional.
518 _Jv_NewArrayClass (jclass element, java::lang::ClassLoader *loader,
519 _Jv_VTable *array_vtable)
521 JvSynchronize sync (element);
523 _Jv_Utf8Const *array_name;
526 if (element->arrayclass)
529 if (element->isPrimitive())
531 if (element == JvPrimClass (void))
532 throw new java::lang::ClassNotFoundException ();
536 len = element->name->len() + 5;
541 signature[index++] = '[';
542 // Compute name of array class.
543 if (element->isPrimitive())
545 signature[index++] = (char) element->method_count;
549 size_t length = element->name->len();
550 const char *const name = element->name->chars();
552 signature[index++] = 'L';
553 memcpy (&signature[index], name, length);
556 signature[index++] = ';';
558 array_name = _Jv_makeUtf8Const (signature, index);
561 // Create new array class.
562 jclass array_class = _Jv_NewClass (array_name, &java::lang::Object::class$,
565 // Note that `vtable_method_count' doesn't include the initial
567 int dm_count = java::lang::Object::class$.vtable_method_count;
569 // Create a new vtable by copying Object's vtable.
572 vtable = array_vtable;
574 vtable = _Jv_VTable::new_vtable (dm_count);
575 vtable->clas = array_class;
576 vtable->gc_descr = java::lang::Object::class$.vtable->gc_descr;
577 for (int i = 0; i < dm_count; ++i)
578 vtable->set_method (i, java::lang::Object::class$.vtable->get_method (i));
580 array_class->vtable = vtable;
581 array_class->vtable_method_count
582 = java::lang::Object::class$.vtable_method_count;
584 // Stash the pointer to the element type.
585 array_class->element_type = element;
587 // Register our interfaces.
588 array_class->interfaces = interfaces;
589 array_class->interface_count = sizeof interfaces / sizeof interfaces[0];
591 // Since all array classes have the same interface dispatch table, we can
592 // cache one and reuse it. It is not necessary to synchronize this.
595 _Jv_Linker::wait_for_state(array_class, JV_STATE_PREPARED);
596 array_idt = array_class->idt;
597 array_depth = array_class->depth;
598 array_ancestors = array_class->ancestors;
602 array_class->idt = array_idt;
603 array_class->depth = array_depth;
604 array_class->ancestors = array_ancestors;
607 using namespace java::lang::reflect;
609 // Array classes are "abstract final" and inherit accessibility
610 // from element type, per vmspec 5.3.3.2
611 _Jv_ushort accflags = (Modifier::FINAL | Modifier::ABSTRACT
613 & (Modifier::PUBLIC | Modifier::PROTECTED
614 | Modifier::PRIVATE)));
615 array_class->accflags = accflags;
618 // An array class has no visible instance fields. "length" is invisible to
621 // Say this class is initialized and ready to go!
622 array_class->state = JV_STATE_DONE;
624 // vmspec, section 5.3.3 describes this
625 if (element->loader != loader)
626 _Jv_RegisterInitiatingLoader (array_class, loader);
628 element->arrayclass = array_class;
631 // These two functions form a stack of classes. When a class is loaded
632 // it is pushed onto the stack by the class loader; this is so that
633 // StackTrace can quickly determine which classes have been loaded.
638 JvSynchronize sync (&java::lang::Class::class$);
641 jclass tmp = stack_head;
642 stack_head = tmp->chain;
649 _Jv_PushClass (jclass k)
651 JvSynchronize sync (&java::lang::Class::class$);
652 jclass tmp = stack_head;