1 // link.cc - Code for linking and resolving classes and pool entries.
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005 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> */
18 #include <java-interp.h>
24 #include <java-cpool.h>
25 #include <execution.h>
26 #include <java/lang/Class.h>
27 #include <java/lang/String.h>
28 #include <java/lang/StringBuffer.h>
29 #include <java/lang/Thread.h>
30 #include <java/lang/InternalError.h>
31 #include <java/lang/VirtualMachineError.h>
32 #include <java/lang/VerifyError.h>
33 #include <java/lang/NoSuchFieldError.h>
34 #include <java/lang/NoSuchMethodError.h>
35 #include <java/lang/ClassFormatError.h>
36 #include <java/lang/IllegalAccessError.h>
37 #include <java/lang/AbstractMethodError.h>
38 #include <java/lang/NoClassDefFoundError.h>
39 #include <java/lang/IncompatibleClassChangeError.h>
40 #include <java/lang/VerifyError.h>
41 #include <java/lang/VMClassLoader.h>
42 #include <java/lang/reflect/Modifier.h>
43 #include <java/security/CodeSource.h>
47 // When true, print debugging information about class loading.
48 bool gcj::verbose_class_flag;
50 typedef unsigned int uaddr __attribute__ ((mode (pointer)));
59 #define ALIGNOF(TYPE) (offsetof (aligner<TYPE>, field))
61 // This returns the alignment of a type as it would appear in a
62 // structure. This can be different from the alignment of the type
63 // itself. For instance on x86 double is 8-aligned but struct{double}
66 _Jv_Linker::get_alignment_from_class (jclass klass)
68 if (klass == JvPrimClass (byte))
69 return ALIGNOF (jbyte);
70 else if (klass == JvPrimClass (short))
71 return ALIGNOF (jshort);
72 else if (klass == JvPrimClass (int))
73 return ALIGNOF (jint);
74 else if (klass == JvPrimClass (long))
75 return ALIGNOF (jlong);
76 else if (klass == JvPrimClass (boolean))
77 return ALIGNOF (jboolean);
78 else if (klass == JvPrimClass (char))
79 return ALIGNOF (jchar);
80 else if (klass == JvPrimClass (float))
81 return ALIGNOF (jfloat);
82 else if (klass == JvPrimClass (double))
83 return ALIGNOF (jdouble);
85 return ALIGNOF (jobject);
89 _Jv_Linker::resolve_field (_Jv_Field *field, java::lang::ClassLoader *loader)
91 if (! field->isResolved ())
93 _Jv_Utf8Const *sig = (_Jv_Utf8Const *) field->type;
94 jclass type = _Jv_FindClassFromSignature (sig->chars(), loader);
96 throw new java::lang::NoClassDefFoundError(field->name->toString());
98 field->flags &= ~_Jv_FIELD_UNRESOLVED_FLAG;
102 // A helper for find_field that knows how to recursively search
103 // superclasses and interfaces.
105 _Jv_Linker::find_field_helper (jclass search, _Jv_Utf8Const *name,
106 _Jv_Utf8Const *type_name,
111 // From 5.4.3.2. First search class itself.
112 for (int i = 0; i < search->field_count; ++i)
114 _Jv_Field *field = &search->fields[i];
115 if (! _Jv_equalUtf8Consts (field->name, name))
118 if (! field->isResolved ())
119 resolve_field (field, search->loader);
121 // Note that we compare type names and not types. This is
122 // bizarre, but we do it because we want to find a field
123 // (and terminate the search) if it has the correct
124 // descriptor -- but then later reject it if the class
125 // loader check results in different classes. We can't just
126 // pass in the descriptor and check that way, because when
127 // the field is already resolved there is no easy way to
128 // find its descriptor again.
129 if (_Jv_equalUtf8Consts (type_name, field->type->name))
136 // Next search direct interfaces.
137 for (int i = 0; i < search->interface_count; ++i)
139 _Jv_Field *result = find_field_helper (search->interfaces[i], name,
140 type_name, declarer);
145 // Now search superclass.
146 search = search->superclass;
153 _Jv_Linker::has_field_p (jclass search, _Jv_Utf8Const *field_name)
155 for (int i = 0; i < search->field_count; ++i)
157 _Jv_Field *field = &search->fields[i];
158 if (_Jv_equalUtf8Consts (field->name, field_name))
165 // KLASS is the class that is requesting the field.
166 // OWNER is the class in which the field should be found.
167 // FIELD_TYPE_NAME is the type descriptor for the field.
168 // Fill FOUND_CLASS with the address of the class in which the field
169 // is actually declared.
170 // This function does the class loader type checks, and
171 // also access checks. Returns the field, or throws an
172 // exception on error.
174 _Jv_Linker::find_field (jclass klass, jclass owner,
176 _Jv_Utf8Const *field_name,
177 _Jv_Utf8Const *field_type_name)
179 // FIXME: this allocates a _Jv_Utf8Const each time. We should make
181 jclass field_type = _Jv_FindClassFromSignature (field_type_name->chars(),
183 if (field_type == NULL)
184 throw new java::lang::NoClassDefFoundError(field_name->toString());
186 _Jv_Field *the_field = find_field_helper (owner, field_name,
187 field_type->name, found_class);
191 java::lang::StringBuffer *sb = new java::lang::StringBuffer();
192 sb->append(JvNewStringLatin1("field "));
193 sb->append(owner->getName());
194 sb->append(JvNewStringLatin1("."));
195 sb->append(_Jv_NewStringUTF(field_name->chars()));
196 sb->append(JvNewStringLatin1(" was not found."));
197 throw new java::lang::NoSuchFieldError (sb->toString());
200 if (_Jv_CheckAccess (klass, *found_class, the_field->flags))
202 // Note that the field returned by find_field_helper is always
203 // resolved. There's no point checking class loaders here,
204 // since we already did the work to look up all the types.
205 // FIXME: being lazy here would be nice.
206 if (the_field->type != field_type)
207 throw new java::lang::LinkageError
209 ("field type mismatch with different loaders"));
213 java::lang::StringBuffer *sb
214 = new java::lang::StringBuffer ();
215 sb->append(klass->getName());
216 sb->append(JvNewStringLatin1(": "));
217 sb->append((*found_class)->getName());
218 sb->append(JvNewStringLatin1("."));
219 sb->append(_Jv_NewStringUtf8Const (field_name));
220 throw new java::lang::IllegalAccessError(sb->toString());
227 _Jv_Linker::resolve_pool_entry (jclass klass, int index)
229 using namespace java::lang::reflect;
231 _Jv_Constants *pool = &klass->constants;
233 if ((pool->tags[index] & JV_CONSTANT_ResolvedFlag) != 0)
234 return pool->data[index];
236 switch (pool->tags[index])
238 case JV_CONSTANT_Class:
240 _Jv_Utf8Const *name = pool->data[index].utf8;
243 if (name->first() == '[')
244 found = _Jv_FindClassFromSignature (name->chars(),
247 found = _Jv_FindClass (name, klass->loader);
250 throw new java::lang::NoClassDefFoundError (name->toString());
252 // Check accessibility, but first strip array types as
253 // _Jv_ClassNameSamePackage can't handle arrays.
256 check && check->isArray();
257 check = check->getComponentType())
259 if ((found->accflags & Modifier::PUBLIC) == Modifier::PUBLIC
260 || (_Jv_ClassNameSamePackage (check->name,
263 pool->data[index].clazz = found;
264 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
268 java::lang::StringBuffer *sb = new java::lang::StringBuffer ();
269 sb->append(klass->getName());
270 sb->append(JvNewStringLatin1(" can't access class "));
271 sb->append(found->getName());
272 throw new java::lang::IllegalAccessError(sb->toString());
277 case JV_CONSTANT_String:
280 str = _Jv_NewStringUtf8Const (pool->data[index].utf8);
281 pool->data[index].o = str;
282 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
286 case JV_CONSTANT_Fieldref:
288 _Jv_ushort class_index, name_and_type_index;
289 _Jv_loadIndexes (&pool->data[index],
291 name_and_type_index);
292 jclass owner = (resolve_pool_entry (klass, class_index)).clazz;
295 _Jv_InitClass (owner);
297 _Jv_ushort name_index, type_index;
298 _Jv_loadIndexes (&pool->data[name_and_type_index],
302 _Jv_Utf8Const *field_name = pool->data[name_index].utf8;
303 _Jv_Utf8Const *field_type_name = pool->data[type_index].utf8;
305 jclass found_class = 0;
306 _Jv_Field *the_field = find_field (klass, owner,
310 if (owner != found_class)
311 _Jv_InitClass (found_class);
312 pool->data[index].field = the_field;
313 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
317 case JV_CONSTANT_Methodref:
318 case JV_CONSTANT_InterfaceMethodref:
320 _Jv_ushort class_index, name_and_type_index;
321 _Jv_loadIndexes (&pool->data[index],
323 name_and_type_index);
324 jclass owner = (resolve_pool_entry (klass, class_index)).clazz;
327 _Jv_InitClass (owner);
329 _Jv_ushort name_index, type_index;
330 _Jv_loadIndexes (&pool->data[name_and_type_index],
334 _Jv_Utf8Const *method_name = pool->data[name_index].utf8;
335 _Jv_Utf8Const *method_signature = pool->data[type_index].utf8;
337 _Jv_Method *the_method = 0;
338 jclass found_class = 0;
340 // We're going to cache a pointer to the _Jv_Method object
341 // when we find it. So, to ensure this doesn't get moved from
342 // beneath us, we first put all the needed Miranda methods
343 // into the target class.
344 wait_for_state (klass, JV_STATE_LOADED);
346 // First search the class itself.
347 the_method = search_method_in_class (owner, klass,
348 method_name, method_signature);
353 goto end_of_method_search;
356 // If we are resolving an interface method, search the
357 // interface's superinterfaces (A superinterface is not an
358 // interface's superclass - a superinterface is implemented by
360 if (pool->tags[index] == JV_CONSTANT_InterfaceMethodref)
365 ifaces.list = (jclass *) _Jv_Malloc (ifaces.len
366 * sizeof (jclass *));
368 get_interfaces (owner, &ifaces);
370 for (int i = 0; i < ifaces.count; i++)
372 jclass cls = ifaces.list[i];
373 the_method = search_method_in_class (cls, klass, method_name,
382 _Jv_Free (ifaces.list);
385 goto end_of_method_search;
388 // Finally, search superclasses.
389 for (jclass cls = owner->getSuperclass (); cls != 0;
390 cls = cls->getSuperclass ())
392 the_method = search_method_in_class (cls, klass, method_name,
401 end_of_method_search:
403 // FIXME: if (cls->loader != klass->loader), then we
404 // must actually check that the types of arguments
405 // correspond. That is, for each argument type, and
406 // the return type, doing _Jv_FindClassFromSignature
407 // with either loader should produce the same result,
408 // i.e., exactly the same jclass object. JVMS 5.4.3.3
412 java::lang::StringBuffer *sb = new java::lang::StringBuffer();
413 sb->append(JvNewStringLatin1("method "));
414 sb->append(owner->getName());
415 sb->append(JvNewStringLatin1("."));
416 sb->append(_Jv_NewStringUTF(method_name->chars()));
417 sb->append(JvNewStringLatin1(" with signature "));
418 sb->append(_Jv_NewStringUTF(method_signature->chars()));
419 sb->append(JvNewStringLatin1(" was not found."));
420 throw new java::lang::NoSuchMethodError (sb->toString());
423 int vtable_index = -1;
424 if (pool->tags[index] != JV_CONSTANT_InterfaceMethodref)
425 vtable_index = (jshort)the_method->index;
427 pool->data[index].rmethod
428 = klass->engine->resolve_method(the_method,
430 ((the_method->accflags
431 & Modifier::STATIC) != 0),
433 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
437 return pool->data[index];
440 // This function is used to lazily locate superclasses and
441 // superinterfaces. This must be called with the class lock held.
443 _Jv_Linker::resolve_class_ref (jclass klass, jclass *classref)
445 jclass ret = *classref;
447 // If superclass looks like a constant pool entry, resolve it now.
448 if (ret && (uaddr) ret < (uaddr) klass->constants.size)
450 if (klass->state < JV_STATE_LINKED)
452 _Jv_Utf8Const *name = klass->constants.data[(uaddr) *classref].utf8;
453 ret = _Jv_FindClass (name, klass->loader);
456 throw new java::lang::NoClassDefFoundError (name->toString());
460 ret = klass->constants.data[(uaddr) classref].clazz;
465 // Find a method declared in the cls that is referenced from klass and
466 // perform access checks.
468 _Jv_Linker::search_method_in_class (jclass cls, jclass klass,
469 _Jv_Utf8Const *method_name,
470 _Jv_Utf8Const *method_signature)
472 using namespace java::lang::reflect;
474 for (int i = 0; i < cls->method_count; i++)
476 _Jv_Method *method = &cls->methods[i];
477 if ( (!_Jv_equalUtf8Consts (method->name,
479 || (!_Jv_equalUtf8Consts (method->signature,
483 if (_Jv_CheckAccess (klass, cls, method->accflags))
487 java::lang::StringBuffer *sb = new java::lang::StringBuffer();
488 sb->append(klass->getName());
489 sb->append(JvNewStringLatin1(": "));
490 sb->append(cls->getName());
491 sb->append(JvNewStringLatin1("."));
492 sb->append(_Jv_NewStringUTF(method_name->chars()));
493 sb->append(_Jv_NewStringUTF(method_signature->chars()));
494 throw new java::lang::IllegalAccessError (sb->toString());
501 #define INITIAL_IOFFSETS_LEN 4
502 #define INITIAL_IFACES_LEN 4
504 static _Jv_IDispatchTable null_idt = { {SHRT_MAX, 0, NULL} };
506 // Generate tables for constant-time assignment testing and interface
507 // method lookup. This implements the technique described by Per Bothner
508 // <per@bothner.com> on the java-discuss mailing list on 1999-09-02:
509 // http://gcc.gnu.org/ml/java/1999-q3/msg00377.html
511 _Jv_Linker::prepare_constant_time_tables (jclass klass)
513 if (klass->isPrimitive () || klass->isInterface ())
516 // Short-circuit in case we've been called already.
517 if ((klass->idt != NULL) || klass->depth != 0)
520 // Calculate the class depth and ancestor table. The depth of a class
521 // is how many "extends" it is removed from Object. Thus the depth of
522 // java.lang.Object is 0, but the depth of java.io.FilterOutputStream
523 // is 2. Depth is defined for all regular and array classes, but not
524 // interfaces or primitive types.
526 jclass klass0 = klass;
527 jboolean has_interfaces = 0;
528 while (klass0 != &java::lang::Object::class$)
530 has_interfaces += klass0->interface_count;
531 klass0 = klass0->superclass;
535 // We do class member testing in constant time by using a small table
536 // of all the ancestor classes within each class. The first element is
537 // a pointer to the current class, and the rest are pointers to the
538 // classes ancestors, ordered from the current class down by decreasing
539 // depth. We do not include java.lang.Object in the table of ancestors,
540 // since it is redundant.
542 // FIXME: _Jv_AllocBytes
543 klass->ancestors = (jclass *) _Jv_Malloc (klass->depth
546 for (int index = 0; index < klass->depth; index++)
548 klass->ancestors[index] = klass0;
549 klass0 = klass0->superclass;
552 if ((klass->accflags & java::lang::reflect::Modifier::ABSTRACT) != 0)
555 // Optimization: If class implements no interfaces, use a common
556 // predefined interface table.
559 klass->idt = &null_idt;
563 // FIXME: _Jv_AllocBytes
565 (_Jv_IDispatchTable *) _Jv_Malloc (sizeof (_Jv_IDispatchTable));
569 ifaces.len = INITIAL_IFACES_LEN;
570 ifaces.list = (jclass *) _Jv_Malloc (ifaces.len * sizeof (jclass *));
572 int itable_size = get_interfaces (klass, &ifaces);
574 if (ifaces.count > 0)
576 klass->idt->cls.itable =
577 // FIXME: _Jv_AllocBytes
578 (void **) _Jv_Malloc (itable_size * sizeof (void *));
579 klass->idt->cls.itable_length = itable_size;
581 jshort *itable_offsets =
582 (jshort *) _Jv_Malloc (ifaces.count * sizeof (jshort));
584 generate_itable (klass, &ifaces, itable_offsets);
586 jshort cls_iindex = find_iindex (ifaces.list, itable_offsets,
589 for (int i = 0; i < ifaces.count; i++)
591 ifaces.list[i]->idt->iface.ioffsets[cls_iindex] =
595 klass->idt->cls.iindex = cls_iindex;
597 _Jv_Free (ifaces.list);
598 _Jv_Free (itable_offsets);
602 klass->idt->cls.iindex = SHRT_MAX;
606 // Return index of item in list, or -1 if item is not present.
608 _Jv_Linker::indexof (void *item, void **list, jshort list_len)
610 for (int i=0; i < list_len; i++)
618 // Find all unique interfaces directly or indirectly implemented by klass.
619 // Returns the size of the interface dispatch table (itable) for klass, which
620 // is the number of unique interfaces plus the total number of methods that
621 // those interfaces declare. May extend ifaces if required.
623 _Jv_Linker::get_interfaces (jclass klass, _Jv_ifaces *ifaces)
627 for (int i = 0; i < klass->interface_count; i++)
629 jclass iface = klass->interfaces[i];
631 /* Make sure interface is linked. */
632 wait_for_state(iface, JV_STATE_LINKED);
634 if (indexof (iface, (void **) ifaces->list, ifaces->count) == -1)
636 if (ifaces->count + 1 >= ifaces->len)
638 /* Resize ifaces list */
639 ifaces->len = ifaces->len * 2;
641 = (jclass *) _Jv_Realloc (ifaces->list,
642 ifaces->len * sizeof(jclass));
644 ifaces->list[ifaces->count] = iface;
647 result += get_interfaces (klass->interfaces[i], ifaces);
651 if (klass->isInterface())
652 result += klass->method_count + 1;
653 else if (klass->superclass)
654 result += get_interfaces (klass->superclass, ifaces);
658 // Fill out itable in klass, resolving method declarations in each ifaces.
659 // itable_offsets is filled out with the position of each iface in itable,
660 // such that itable[itable_offsets[n]] == ifaces.list[n].
662 _Jv_Linker::generate_itable (jclass klass, _Jv_ifaces *ifaces,
663 jshort *itable_offsets)
665 void **itable = klass->idt->cls.itable;
666 jshort itable_pos = 0;
668 for (int i = 0; i < ifaces->count; i++)
670 jclass iface = ifaces->list[i];
671 itable_offsets[i] = itable_pos;
672 itable_pos = append_partial_itable (klass, iface, itable, itable_pos);
674 /* Create interface dispatch table for iface */
675 if (iface->idt == NULL)
677 // FIXME: _Jv_AllocBytes
679 = (_Jv_IDispatchTable *) _Jv_Malloc (sizeof (_Jv_IDispatchTable));
681 // The first element of ioffsets is its length (itself included).
682 // FIXME: _Jv_AllocBytes
683 jshort *ioffsets = (jshort *) _Jv_Malloc (INITIAL_IOFFSETS_LEN
685 ioffsets[0] = INITIAL_IOFFSETS_LEN;
686 for (int i = 1; i < INITIAL_IOFFSETS_LEN; i++)
689 iface->idt->iface.ioffsets = ioffsets;
694 // Format method name for use in error messages.
696 _Jv_GetMethodString (jclass klass, _Jv_Method *meth,
699 using namespace java::lang;
700 StringBuffer *buf = new StringBuffer (klass->name->toString());
701 buf->append (jchar ('.'));
702 buf->append (meth->name->toString());
703 buf->append ((jchar) ' ');
704 buf->append (meth->signature->toString());
707 buf->append(JvNewStringLatin1(" in "));
708 buf->append(derived->name->toString());
710 return buf->toString();
714 _Jv_ThrowNoSuchMethodError ()
716 throw new java::lang::NoSuchMethodError;
719 // This is put in empty vtable slots.
721 _Jv_abstractMethodError (void)
723 throw new java::lang::AbstractMethodError();
726 // Each superinterface of a class (i.e. each interface that the class
727 // directly or indirectly implements) has a corresponding "Partial
728 // Interface Dispatch Table" whose size is (number of methods + 1) words.
729 // The first word is a pointer to the interface (i.e. the java.lang.Class
730 // instance for that interface). The remaining words are pointers to the
731 // actual methods that implement the methods declared in the interface,
732 // in order of declaration.
734 // Append partial interface dispatch table for "iface" to "itable", at
735 // position itable_pos.
736 // Returns the offset at which the next partial ITable should be appended.
738 _Jv_Linker::append_partial_itable (jclass klass, jclass iface,
739 void **itable, jshort pos)
741 using namespace java::lang::reflect;
743 itable[pos++] = (void *) iface;
746 for (int j=0; j < iface->method_count; j++)
749 for (jclass cl = klass; cl; cl = cl->getSuperclass())
751 meth = _Jv_GetMethodLocal (cl, iface->methods[j].name,
752 iface->methods[j].signature);
758 if (meth && (meth->name->first() == '<'))
760 // leave a placeholder in the itable for hidden init methods.
765 if ((meth->accflags & Modifier::STATIC) != 0)
766 throw new java::lang::IncompatibleClassChangeError
767 (_Jv_GetMethodString (klass, meth));
768 if ((meth->accflags & Modifier::PUBLIC) == 0)
769 throw new java::lang::IllegalAccessError
770 (_Jv_GetMethodString (klass, meth));
772 if ((meth->accflags & Modifier::ABSTRACT) != 0)
773 itable[pos] = (void *) &_Jv_abstractMethodError;
775 itable[pos] = meth->ncode;
779 // The method doesn't exist in klass. Binary compatibility rules
780 // permit this, so we delay the error until runtime using a pointer
781 // to a method which throws an exception.
782 itable[pos] = (void *) _Jv_ThrowNoSuchMethodError;
790 static _Jv_Mutex_t iindex_mutex;
791 static bool iindex_mutex_initialized = false;
793 // We need to find the correct offset in the Class Interface Dispatch
794 // Table for a given interface. Once we have that, invoking an interface
795 // method just requires combining the Method's index in the interface
796 // (known at compile time) to get the correct method. Doing a type test
797 // (cast or instanceof) is the same problem: Once we have a possible Partial
798 // Interface Dispatch Table, we just compare the first element to see if it
799 // matches the desired interface. So how can we find the correct offset?
800 // Our solution is to keep a vector of candiate offsets in each interface
801 // (idt->iface.ioffsets), and in each class we have an index
802 // (idt->cls.iindex) used to select the correct offset from ioffsets.
804 // Calculate and return iindex for a new class.
805 // ifaces is a vector of num interfaces that the class implements.
806 // offsets[j] is the offset in the interface dispatch table for the
807 // interface corresponding to ifaces[j].
808 // May extend the interface ioffsets if required.
810 _Jv_Linker::find_iindex (jclass *ifaces, jshort *offsets, jshort num)
815 // Acquire a global lock to prevent itable corruption in case of multiple
816 // classes that implement an intersecting set of interfaces being linked
817 // simultaneously. We can assume that the mutex will be initialized
819 if (! iindex_mutex_initialized)
821 _Jv_MutexInit (&iindex_mutex);
822 iindex_mutex_initialized = true;
825 _Jv_MutexLock (&iindex_mutex);
827 for (i=1;; i++) /* each potential position in ioffsets */
829 for (j=0;; j++) /* each iface */
833 if (i >= ifaces[j]->idt->iface.ioffsets[0])
835 int ioffset = ifaces[j]->idt->iface.ioffsets[i];
836 /* We can potentially share this position with another class. */
837 if (ioffset >= 0 && ioffset != offsets[j])
838 break; /* Nope. Try next i. */
842 for (j = 0; j < num; j++)
844 int len = ifaces[j]->idt->iface.ioffsets[0];
848 int newlen = 2 * len;
851 jshort *old_ioffsets = ifaces[j]->idt->iface.ioffsets;
852 // FIXME: _Jv_AllocBytes
853 jshort *new_ioffsets = (jshort *) _Jv_Malloc (newlen
855 memcpy (&new_ioffsets[1], &old_ioffsets[1],
856 (len - 1) * sizeof (jshort));
857 new_ioffsets[0] = newlen;
860 new_ioffsets[len++] = -1;
862 ifaces[j]->idt->iface.ioffsets = new_ioffsets;
864 ifaces[j]->idt->iface.ioffsets[i] = offsets[j];
867 _Jv_MutexUnlock (&iindex_mutex);
873 // Functions for indirect dispatch (symbolic virtual binding) support.
875 // There are three tables, atable otable and itable. atable is an
876 // array of addresses, and otable is an array of offsets, and these
877 // are used for static and virtual members respectively. itable is an
878 // array of pairs {address, index} where each address is a pointer to
881 // {a,o,i}table_syms is an array of _Jv_MethodSymbols. Each such
882 // symbol is a tuple of {classname, member name, signature}.
884 // Set this to true to enable debugging of indirect dispatch tables/linking.
885 static bool debug_link = false;
887 // link_symbol_table() scans these two arrays and fills in the
888 // corresponding atable and otable with the addresses of static
889 // members and the offsets of virtual members.
891 // The offset (in bytes) for each resolved method or field is placed
892 // at the corresponding position in the virtual method offset table
895 // The same otable and atable may be shared by many classes.
897 // This must be called while holding the class lock.
900 _Jv_Linker::link_symbol_table (jclass klass)
903 _Jv_MethodSymbol sym;
904 if (klass->otable == NULL
905 || klass->otable->state != 0)
908 klass->otable->state = 1;
911 fprintf (stderr, "Fixing up otable in %s:\n", klass->name->chars());
913 (sym = klass->otable_syms[index]).class_name != NULL;
916 jclass target_class = _Jv_FindClass (sym.class_name, klass->loader);
917 _Jv_Method *meth = NULL;
919 _Jv_Utf8Const *signature = sym.signature;
922 static char *bounce = (char *)_Jv_ThrowNoSuchMethodError;
923 ptrdiff_t offset = (char *)(klass->vtable) - bounce;
924 klass->otable->offsets[index] = offset;
927 if (target_class == NULL)
928 throw new java::lang::NoClassDefFoundError
929 (_Jv_NewStringUTF (sym.class_name->chars()));
931 // We're looking for a field or a method, and we can tell
932 // which is needed by looking at the signature.
933 if (signature->first() == '(' && signature->len() >= 2)
935 // Looks like someone is trying to invoke an interface method
936 if (target_class->isInterface())
938 using namespace java::lang;
939 StringBuffer *sb = new StringBuffer();
940 sb->append(JvNewStringLatin1("found interface "));
941 sb->append(target_class->getName());
942 sb->append(JvNewStringLatin1(" when searching for a class"));
943 throw new VerifyError(sb->toString());
946 // If the target class does not have a vtable_method_count yet,
947 // then we can't tell the offsets for its methods, so we must lay
949 wait_for_state(target_class, JV_STATE_PREPARED);
951 meth = _Jv_LookupDeclaredMethod(target_class, sym.name,
956 int offset = _Jv_VTable::idx_to_offset (meth->index);
958 JvFail ("Bad method index");
959 JvAssert (meth->index < target_class->vtable_method_count);
960 klass->otable->offsets[index] = offset;
963 fprintf (stderr, " offsets[%d] = %d (class %s@%p : %s(%s))\n",
965 (int)klass->otable->offsets[index],
966 (const char*)target_class->name->chars(),
968 (const char*)sym.name->chars(),
969 (const char*)signature->chars());
975 wait_for_state(target_class, JV_STATE_PREPARED);
977 _Jv_Field *the_field = find_field (klass, target_class, &found_class,
978 sym.name, sym.signature);
979 if ((the_field->flags & java::lang::reflect::Modifier::STATIC))
980 throw new java::lang::IncompatibleClassChangeError;
982 klass->otable->offsets[index] = the_field->u.boffset;
987 if (klass->atable == NULL || klass->atable->state != 0)
990 klass->atable->state = 1;
993 (sym = klass->atable_syms[index]).class_name != NULL;
996 jclass target_class = _Jv_FindClass (sym.class_name, klass->loader);
997 _Jv_Method *meth = NULL;
998 _Jv_Utf8Const *signature = sym.signature;
1000 // ??? Setting this pointer to null will at least get us a
1001 // NullPointerException
1002 klass->atable->addresses[index] = NULL;
1004 if (target_class == NULL)
1005 throw new java::lang::NoClassDefFoundError
1006 (_Jv_NewStringUTF (sym.class_name->chars()));
1008 // We're looking for a static field or a static method, and we
1009 // can tell which is needed by looking at the signature.
1010 if (signature->first() == '(' && signature->len() >= 2)
1012 // If the target class does not have a vtable_method_count yet,
1013 // then we can't tell the offsets for its methods, so we must lay
1015 wait_for_state (target_class, JV_STATE_PREPARED);
1017 // Interface methods cannot have bodies.
1018 if (target_class->isInterface())
1020 using namespace java::lang;
1021 StringBuffer *sb = new StringBuffer();
1022 sb->append(JvNewStringLatin1("class "));
1023 sb->append(target_class->getName());
1024 sb->append(JvNewStringLatin1(" is an interface: "
1026 throw new VerifyError(sb->toString());
1029 meth = _Jv_LookupDeclaredMethod(target_class, sym.name,
1034 if (meth->ncode) // Maybe abstract?
1036 klass->atable->addresses[index] = meth->ncode;
1038 fprintf (stderr, " addresses[%d] = %p (class %s@%p : %s(%s))\n",
1040 &klass->atable->addresses[index],
1041 (const char*)target_class->name->chars(),
1043 (const char*)sym.name->chars(),
1044 (const char*)signature->chars());
1048 klass->atable->addresses[index]
1049 = (void *)_Jv_ThrowNoSuchMethodError;
1056 wait_for_state(target_class, JV_STATE_PREPARED);
1058 _Jv_Field *the_field = find_field (klass, target_class, &found_class,
1059 sym.name, sym.signature);
1060 if ((the_field->flags & java::lang::reflect::Modifier::STATIC))
1061 klass->atable->addresses[index] = the_field->u.addr;
1063 throw new java::lang::IncompatibleClassChangeError;
1068 if (klass->itable == NULL
1069 || klass->itable->state != 0)
1072 klass->itable->state = 1;
1075 (sym = klass->itable_syms[index]).class_name != NULL;
1078 jclass target_class = _Jv_FindClass (sym.class_name, klass->loader);
1079 _Jv_Utf8Const *signature = sym.signature;
1084 wait_for_state(target_class, JV_STATE_LOADED);
1085 bool found = _Jv_getInterfaceMethod (target_class, cls, i,
1086 sym.name, sym.signature);
1090 klass->itable->addresses[index * 2] = cls;
1091 klass->itable->addresses[index * 2 + 1] = (void *)(unsigned long) i;
1094 fprintf (stderr, " interfaces[%d] = %p (interface %s@%p : %s(%s))\n",
1096 klass->itable->addresses[index * 2],
1097 (const char*)cls->name->chars(),
1099 (const char*)sym.name->chars(),
1100 (const char*)signature->chars());
1101 fprintf (stderr, " [%d] = offset %d\n",
1103 (int)(unsigned long)klass->itable->addresses[index * 2 + 1]);
1108 throw new java::lang::IncompatibleClassChangeError;
1113 // For each catch_record in the list of caught classes, fill in the
1116 _Jv_Linker::link_exception_table (jclass self)
1118 struct _Jv_CatchClass *catch_record = self->catch_classes;
1119 if (!catch_record || catch_record->classname)
1122 while (catch_record->classname)
1127 = _Jv_FindClass (catch_record->classname,
1128 self->getClassLoaderInternal ());
1129 *catch_record->address = target_class;
1131 catch (::java::lang::Throwable *t)
1133 // FIXME: We need to do something better here.
1134 *catch_record->address = 0;
1138 self->catch_classes->classname = (_Jv_Utf8Const *)-1;
1141 // Set itable method indexes for members of interface IFACE.
1143 _Jv_Linker::layout_interface_methods (jclass iface)
1145 if (! iface->isInterface())
1148 // itable indexes start at 1.
1149 // FIXME: Static initalizers currently get a NULL placeholder entry in the
1150 // itable so they are also assigned an index here.
1151 for (int i = 0; i < iface->method_count; i++)
1152 iface->methods[i].index = i + 1;
1155 // Prepare virtual method declarations in KLASS, and any superclasses
1156 // as required, by determining their vtable index, setting
1157 // method->index, and finally setting the class's vtable_method_count.
1158 // Must be called with the lock for KLASS held.
1160 _Jv_Linker::layout_vtable_methods (jclass klass)
1162 if (klass->vtable != NULL || klass->isInterface()
1163 || klass->vtable_method_count != -1)
1166 jclass superclass = klass->getSuperclass();
1168 if (superclass != NULL && superclass->vtable_method_count == -1)
1170 JvSynchronize sync (superclass);
1171 layout_vtable_methods (superclass);
1174 int index = (superclass == NULL ? 0 : superclass->vtable_method_count);
1176 for (int i = 0; i < klass->method_count; ++i)
1178 _Jv_Method *meth = &klass->methods[i];
1179 _Jv_Method *super_meth = NULL;
1181 if (! _Jv_isVirtualMethod (meth))
1184 if (superclass != NULL)
1187 super_meth = _Jv_LookupDeclaredMethod (superclass, meth->name,
1188 meth->signature, &declarer);
1189 // See if this method actually overrides the other method
1193 if (! _Jv_isVirtualMethod (super_meth)
1194 || ! _Jv_CheckAccess (klass, declarer,
1195 super_meth->accflags))
1197 else if ((super_meth->accflags
1198 & java::lang::reflect::Modifier::FINAL) != 0)
1200 using namespace java::lang;
1201 StringBuffer *sb = new StringBuffer();
1202 sb->append(JvNewStringLatin1("method "));
1203 sb->append(_Jv_GetMethodString(klass, meth));
1204 sb->append(JvNewStringLatin1(" overrides final method "));
1205 sb->append(_Jv_GetMethodString(declarer, super_meth));
1206 throw new VerifyError(sb->toString());
1212 meth->index = super_meth->index;
1214 meth->index = index++;
1217 klass->vtable_method_count = index;
1220 // Set entries in VTABLE for virtual methods declared in KLASS.
1222 _Jv_Linker::set_vtable_entries (jclass klass, _Jv_VTable *vtable)
1224 for (int i = klass->method_count - 1; i >= 0; i--)
1226 using namespace java::lang::reflect;
1228 _Jv_Method *meth = &klass->methods[i];
1229 if (meth->index == (_Jv_ushort) -1)
1231 if ((meth->accflags & Modifier::ABSTRACT))
1232 // FIXME: it might be nice to have a libffi trampoline here,
1233 // so we could pass in the method name and other information.
1234 vtable->set_method(meth->index, (void *) &_Jv_abstractMethodError);
1236 vtable->set_method(meth->index, meth->ncode);
1240 // Allocate and lay out the virtual method table for KLASS. This will
1241 // also cause vtables to be generated for any non-abstract
1242 // superclasses, and virtual method layout to occur for any abstract
1243 // superclasses. Must be called with monitor lock for KLASS held.
1245 _Jv_Linker::make_vtable (jclass klass)
1247 using namespace java::lang::reflect;
1249 // If the vtable exists, or for interface classes, do nothing. All
1250 // other classes, including abstract classes, need a vtable.
1251 if (klass->vtable != NULL || klass->isInterface())
1254 // Ensure all the `ncode' entries are set.
1255 klass->engine->create_ncode(klass);
1257 // Class must be laid out before we can create a vtable.
1258 if (klass->vtable_method_count == -1)
1259 layout_vtable_methods (klass);
1261 // Allocate the new vtable.
1262 _Jv_VTable *vtable = _Jv_VTable::new_vtable (klass->vtable_method_count);
1263 klass->vtable = vtable;
1265 // Copy the vtable of the closest superclass.
1266 jclass superclass = klass->superclass;
1268 JvSynchronize sync (superclass);
1269 make_vtable (superclass);
1271 for (int i = 0; i < superclass->vtable_method_count; ++i)
1272 vtable->set_method (i, superclass->vtable->get_method (i));
1274 // Set the class pointer and GC descriptor.
1275 vtable->clas = klass;
1276 vtable->gc_descr = _Jv_BuildGCDescr (klass);
1278 // For each virtual declared in klass, set new vtable entry or
1279 // override an old one.
1280 set_vtable_entries (klass, vtable);
1282 // Note that we don't check for abstract methods here. We used to,
1283 // but there is a JVMS clarification that indicates that a check
1284 // here would be too eager. And, a simple test case confirms this.
1287 // Lay out the class, allocating space for static fields and computing
1288 // offsets of instance fields. The class lock must be held by the
1291 _Jv_Linker::ensure_fields_laid_out (jclass klass)
1293 if (klass->size_in_bytes != -1)
1296 // Compute the alignment for this type by searching through the
1297 // superclasses and finding the maximum required alignment. We
1298 // could consider caching this in the Class.
1299 int max_align = __alignof__ (java::lang::Object);
1300 jclass super = klass->getSuperclass();
1301 while (super != NULL)
1303 // Ensure that our super has its super installed before
1305 wait_for_state(super, JV_STATE_LOADING);
1306 ensure_fields_laid_out(super);
1307 int num = JvNumInstanceFields (super);
1308 _Jv_Field *field = JvGetFirstInstanceField (super);
1311 int field_align = get_alignment_from_class (field->type);
1312 if (field_align > max_align)
1313 max_align = field_align;
1317 super = super->getSuperclass();
1321 int static_size = 0;
1323 // Although java.lang.Object is never interpreted, an interface can
1324 // have a null superclass. Note that we have to lay out an
1325 // interface because it might have static fields.
1326 if (klass->superclass)
1327 instance_size = klass->superclass->size();
1329 instance_size = java::lang::Object::class$.size();
1331 for (int i = 0; i < klass->field_count; i++)
1336 _Jv_Field *field = &klass->fields[i];
1338 if (! field->isRef ())
1340 // It is safe to resolve the field here, since it's a
1341 // primitive class, which does not cause loading to happen.
1342 resolve_field (field, klass->loader);
1344 field_size = field->type->size ();
1345 field_align = get_alignment_from_class (field->type);
1349 field_size = sizeof (jobject);
1350 field_align = __alignof__ (jobject);
1353 field->bsize = field_size;
1355 if ((field->flags & java::lang::reflect::Modifier::STATIC))
1357 if (field->u.addr == NULL)
1359 // This computes an offset into a region we'll allocate
1360 // shortly, and then add this offset to the start
1362 static_size = ROUND (static_size, field_align);
1363 field->u.boffset = static_size;
1364 static_size += field_size;
1369 instance_size = ROUND (instance_size, field_align);
1370 field->u.boffset = instance_size;
1371 instance_size += field_size;
1372 if (field_align > max_align)
1373 max_align = field_align;
1377 if (static_size != 0)
1378 klass->engine->allocate_static_fields (klass, static_size);
1380 // Set the instance size for the class. Note that first we round it
1381 // to the alignment required for this object; this keeps us in sync
1382 // with our current ABI.
1383 instance_size = ROUND (instance_size, max_align);
1384 klass->size_in_bytes = instance_size;
1387 // This takes the class to state JV_STATE_LINKED. The class lock must
1388 // be held when calling this.
1390 _Jv_Linker::ensure_class_linked (jclass klass)
1392 if (klass->state >= JV_STATE_LINKED)
1395 int state = klass->state;
1398 // Short-circuit, so that mutually dependent classes are ok.
1399 klass->state = JV_STATE_LINKED;
1401 _Jv_Constants *pool = &klass->constants;
1403 // Compiled classes require that their class constants be
1404 // resolved here. However, interpreted classes need their
1405 // constants to be resolved lazily. If we resolve an
1406 // interpreted class' constants eagerly, we can end up with
1407 // spurious IllegalAccessErrors when the constant pool contains
1408 // a reference to a class we can't access. This can validly
1409 // occur in an obscure case involving the InnerClasses
1411 if (! _Jv_IsInterpretedClass (klass))
1413 // Resolve class constants first, since other constant pool
1414 // entries may rely on these.
1415 for (int index = 1; index < pool->size; ++index)
1417 if (pool->tags[index] == JV_CONSTANT_Class)
1418 resolve_pool_entry (klass, index);
1422 #if 0 // Should be redundant now
1423 // If superclass looks like a constant pool entry,
1425 if ((uaddr) klass->superclass < (uaddr) pool->size)
1426 klass->superclass = pool->data[(uaddr) klass->superclass].clazz;
1428 // Likewise for interfaces.
1429 for (int i = 0; i < klass->interface_count; i++)
1431 if ((uaddr) klass->interfaces[i] < (uaddr) pool->size)
1432 klass->interfaces[i]
1433 = pool->data[(uaddr) klass->interfaces[i]].clazz;
1437 // Resolve the remaining constant pool entries.
1438 for (int index = 1; index < pool->size; ++index)
1440 if (pool->tags[index] == JV_CONSTANT_String)
1444 str = _Jv_NewStringUtf8Const (pool->data[index].utf8);
1445 pool->data[index].o = str;
1446 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
1450 if (klass->engine->need_resolve_string_fields())
1452 jfieldID f = JvGetFirstStaticField (klass);
1453 for (int n = JvNumStaticFields (klass); n > 0; --n)
1455 int mod = f->getModifiers ();
1456 // If we have a static String field with a non-null initial
1457 // value, we know it points to a Utf8Const.
1458 resolve_field(f, klass->loader);
1459 if (f->getClass () == &java::lang::String::class$
1460 && (mod & java::lang::reflect::Modifier::STATIC) != 0)
1462 jstring *strp = (jstring *) f->u.addr;
1464 *strp = _Jv_NewStringUtf8Const ((_Jv_Utf8Const *) *strp);
1466 f = f->getNextField ();
1470 klass->notifyAll ();
1472 _Jv_PushClass (klass);
1474 catch (java::lang::Throwable *t)
1476 klass->state = state;
1481 // This ensures that symbolic superclass and superinterface references
1482 // are resolved for the indicated class. This must be called with the
1485 _Jv_Linker::ensure_supers_installed (jclass klass)
1487 resolve_class_ref (klass, &klass->superclass);
1488 // An interface won't have a superclass.
1489 if (klass->superclass)
1490 wait_for_state (klass->superclass, JV_STATE_LOADING);
1492 for (int i = 0; i < klass->interface_count; ++i)
1494 resolve_class_ref (klass, &klass->interfaces[i]);
1495 wait_for_state (klass->interfaces[i], JV_STATE_LOADING);
1499 // This adds missing `Miranda methods' to a class.
1501 _Jv_Linker::add_miranda_methods (jclass base, jclass iface_class)
1503 // Note that at this point, all our supers, and the supers of all
1504 // our superclasses and superinterfaces, will have been installed.
1506 for (int i = 0; i < iface_class->interface_count; ++i)
1508 jclass interface = iface_class->interfaces[i];
1510 for (int j = 0; j < interface->method_count; ++j)
1512 _Jv_Method *meth = &interface->methods[j];
1513 // Don't bother with <clinit>.
1514 if (meth->name->first() == '<')
1516 _Jv_Method *new_meth = _Jv_LookupDeclaredMethod (base, meth->name,
1520 // We assume that such methods are very unlikely, so we
1521 // just reallocate the method array each time one is
1522 // found. This greatly simplifies the searching --
1523 // otherwise we have to make sure that each such method
1524 // found is really unique among all superinterfaces.
1525 int new_count = base->method_count + 1;
1527 = (_Jv_Method *) _Jv_AllocBytes (sizeof (_Jv_Method)
1529 memcpy (new_m, base->methods,
1530 sizeof (_Jv_Method) * base->method_count);
1533 new_m[base->method_count] = *meth;
1534 new_m[base->method_count].index = (_Jv_ushort) -1;
1535 new_m[base->method_count].accflags
1536 |= java::lang::reflect::Modifier::INVISIBLE;
1538 base->methods = new_m;
1539 base->method_count = new_count;
1543 wait_for_state (interface, JV_STATE_LOADED);
1544 add_miranda_methods (base, interface);
1548 // This ensures that the class' method table is "complete". This must
1549 // be called with the class lock held.
1551 _Jv_Linker::ensure_method_table_complete (jclass klass)
1553 if (klass->vtable != NULL || klass->isInterface())
1556 // We need our superclass to have its own Miranda methods installed.
1557 wait_for_state (klass->getSuperclass (), JV_STATE_LOADED);
1559 // A class might have so-called "Miranda methods". This is a method
1560 // that is declared in an interface and not re-declared in an
1561 // abstract class. Some compilers don't emit declarations for such
1562 // methods in the class; this will give us problems since we expect
1563 // a declaration for any method requiring a vtable entry. We handle
1564 // this here by searching for such methods and constructing new
1565 // internal declarations for them. Note that we do this
1566 // unconditionally, and not just for abstract classes, to correctly
1567 // account for cases where a class is modified to be concrete and
1568 // still incorrectly inherits an abstract method.
1569 int pre_count = klass->method_count;
1570 add_miranda_methods (klass, klass);
1572 // Let the execution engine know that we've added methods.
1573 if (klass->method_count != pre_count)
1574 klass->engine->post_miranda_hook(klass);
1577 // Verify a class. Must be called with class lock held.
1579 _Jv_Linker::verify_class (jclass klass)
1581 klass->engine->verify(klass);
1584 // Check the assertions contained in the type assertion table for KLASS.
1585 // This is the equivilent of bytecode verification for native, BC-ABI code.
1587 _Jv_Linker::verify_type_assertions (jclass klass)
1590 fprintf (stderr, "Evaluating type assertions for %s:\n",
1591 klass->name->chars());
1593 if (klass->assertion_table == NULL)
1596 for (int i = 0;; i++)
1598 int assertion_code = klass->assertion_table[i].assertion_code;
1599 _Jv_Utf8Const *op1 = klass->assertion_table[i].op1;
1600 _Jv_Utf8Const *op2 = klass->assertion_table[i].op2;
1602 if (assertion_code == JV_ASSERT_END_OF_TABLE)
1604 else if (assertion_code == JV_ASSERT_TYPES_COMPATIBLE)
1608 fprintf (stderr, " code=%i, operand A=%s B=%s\n",
1609 assertion_code, op1->chars(), op2->chars());
1612 // The operands are class signatures. op1 is the source,
1613 // op2 is the target.
1614 jclass cl1 = _Jv_FindClassFromSignature (op1->chars(),
1615 klass->getClassLoaderInternal());
1616 jclass cl2 = _Jv_FindClassFromSignature (op2->chars(),
1617 klass->getClassLoaderInternal());
1619 // If the class doesn't exist, ignore the assertion. An exception
1620 // will be thrown later if an attempt is made to actually
1621 // instantiate the class.
1622 if (cl1 == NULL || cl2 == NULL)
1625 if (! _Jv_IsAssignableFromSlow (cl2, cl1))
1627 jstring s = JvNewStringUTF ("Incompatible types: In class ");
1628 s = s->concat (klass->getName());
1629 s = s->concat (JvNewStringUTF (": "));
1630 s = s->concat (cl1->getName());
1631 s = s->concat (JvNewStringUTF (" is not assignable to "));
1632 s = s->concat (cl2->getName());
1633 throw new java::lang::VerifyError (s);
1636 else if (assertion_code == JV_ASSERT_IS_INSTANTIABLE)
1638 // TODO: Implement this.
1640 // Unknown assertion codes are ignored, for forwards-compatibility.
1645 _Jv_Linker::print_class_loaded (jclass klass)
1647 char *codesource = NULL;
1648 if (klass->protectionDomain != NULL)
1650 java::security::CodeSource *cs
1651 = klass->protectionDomain->getCodeSource();
1654 jstring css = cs->toString();
1655 int len = JvGetStringUTFLength(css);
1656 codesource = (char *) _Jv_AllocBytes(len + 1);
1657 JvGetStringUTFRegion(css, 0, css->length(), codesource);
1658 codesource[len] = '\0';
1661 if (codesource == NULL)
1662 codesource = "<no code source>";
1664 // We use a somewhat bogus test for the ABI here.
1666 if (_Jv_IsInterpretedClass (klass))
1668 else if (klass->state == JV_STATE_PRELOADING)
1669 abi = "BC-compiled";
1671 abi = "pre-compiled";
1673 fprintf (stderr, "[Loaded (%s) %s from %s]\n", abi, klass->name->chars(),
1677 // FIXME: mention invariants and stuff.
1679 _Jv_Linker::wait_for_state (jclass klass, int state)
1681 if (klass->state >= state)
1684 JvSynchronize sync (klass);
1686 // This is similar to the strategy for class initialization. If we
1687 // already hold the lock, just leave.
1688 java::lang::Thread *self = java::lang::Thread::currentThread();
1689 while (klass->state <= state
1691 && klass->thread != self)
1694 java::lang::Thread *save = klass->thread;
1695 klass->thread = self;
1697 // Print some debugging info if requested. Interpreted classes are
1698 // handled in defineclass, so we only need to handle the two
1699 // pre-compiled cases here.
1700 if (gcj::verbose_class_flag
1701 && (klass->state == JV_STATE_COMPILED
1702 || klass->state == JV_STATE_PRELOADING)
1703 && ! _Jv_IsInterpretedClass (klass))
1704 print_class_loaded (klass);
1708 if (state >= JV_STATE_LOADING && klass->state < JV_STATE_LOADING)
1710 ensure_supers_installed (klass);
1711 klass->set_state(JV_STATE_LOADING);
1714 if (state >= JV_STATE_LOADED && klass->state < JV_STATE_LOADED)
1716 ensure_method_table_complete (klass);
1717 klass->set_state(JV_STATE_LOADED);
1720 if (state >= JV_STATE_PREPARED && klass->state < JV_STATE_PREPARED)
1722 ensure_fields_laid_out (klass);
1723 make_vtable (klass);
1724 layout_interface_methods (klass);
1725 prepare_constant_time_tables (klass);
1726 klass->set_state(JV_STATE_PREPARED);
1729 if (state >= JV_STATE_LINKED && klass->state < JV_STATE_LINKED)
1731 verify_class (klass);
1733 ensure_class_linked (klass);
1734 link_exception_table (klass);
1735 link_symbol_table (klass);
1736 klass->set_state(JV_STATE_LINKED);
1739 catch (java::lang::Throwable *exc)
1741 klass->thread = save;
1742 klass->set_state(JV_STATE_ERROR);
1746 klass->thread = save;
1748 if (klass->state == JV_STATE_ERROR)
1749 throw new java::lang::LinkageError;