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 typedef unsigned int uaddr
__attribute__ ((mode (pointer
)));
56 #define ALIGNOF(TYPE) (offsetof (aligner<TYPE>, field))
58 // This returns the alignment of a type as it would appear in a
59 // structure. This can be different from the alignment of the type
60 // itself. For instance on x86 double is 8-aligned but struct{double}
63 _Jv_Linker::get_alignment_from_class (jclass klass
)
65 if (klass
== JvPrimClass (byte
))
66 return ALIGNOF (jbyte
);
67 else if (klass
== JvPrimClass (short))
68 return ALIGNOF (jshort
);
69 else if (klass
== JvPrimClass (int))
70 return ALIGNOF (jint
);
71 else if (klass
== JvPrimClass (long))
72 return ALIGNOF (jlong
);
73 else if (klass
== JvPrimClass (boolean
))
74 return ALIGNOF (jboolean
);
75 else if (klass
== JvPrimClass (char))
76 return ALIGNOF (jchar
);
77 else if (klass
== JvPrimClass (float))
78 return ALIGNOF (jfloat
);
79 else if (klass
== JvPrimClass (double))
80 return ALIGNOF (jdouble
);
82 return ALIGNOF (jobject
);
86 _Jv_Linker::resolve_field (_Jv_Field
*field
, java::lang::ClassLoader
*loader
)
88 if (! field
->isResolved ())
90 _Jv_Utf8Const
*sig
= (_Jv_Utf8Const
*) field
->type
;
91 jclass type
= _Jv_FindClassFromSignature (sig
->chars(), loader
);
93 throw new java::lang::NoClassDefFoundError(field
->name
->toString());
95 field
->flags
&= ~_Jv_FIELD_UNRESOLVED_FLAG
;
99 // A helper for find_field that knows how to recursively search
100 // superclasses and interfaces.
102 _Jv_Linker::find_field_helper (jclass search
, _Jv_Utf8Const
*name
,
103 _Jv_Utf8Const
*type_name
,
108 // From 5.4.3.2. First search class itself.
109 for (int i
= 0; i
< search
->field_count
; ++i
)
111 _Jv_Field
*field
= &search
->fields
[i
];
112 if (! _Jv_equalUtf8Consts (field
->name
, name
))
115 if (! field
->isResolved ())
116 resolve_field (field
, search
->loader
);
118 // Note that we compare type names and not types. This is
119 // bizarre, but we do it because we want to find a field
120 // (and terminate the search) if it has the correct
121 // descriptor -- but then later reject it if the class
122 // loader check results in different classes. We can't just
123 // pass in the descriptor and check that way, because when
124 // the field is already resolved there is no easy way to
125 // find its descriptor again.
126 if (_Jv_equalUtf8Consts (type_name
, field
->type
->name
))
133 // Next search direct interfaces.
134 for (int i
= 0; i
< search
->interface_count
; ++i
)
136 _Jv_Field
*result
= find_field_helper (search
->interfaces
[i
], name
,
137 type_name
, declarer
);
142 // Now search superclass.
143 search
= search
->superclass
;
150 _Jv_Linker::has_field_p (jclass search
, _Jv_Utf8Const
*field_name
)
152 for (int i
= 0; i
< search
->field_count
; ++i
)
154 _Jv_Field
*field
= &search
->fields
[i
];
155 if (_Jv_equalUtf8Consts (field
->name
, field_name
))
162 // KLASS is the class that is requesting the field.
163 // OWNER is the class in which the field should be found.
164 // FIELD_TYPE_NAME is the type descriptor for the field.
165 // Fill FOUND_CLASS with the address of the class in which the field
166 // is actually declared.
167 // This function does the class loader type checks, and
168 // also access checks. Returns the field, or throws an
169 // exception on error.
171 _Jv_Linker::find_field (jclass klass
, jclass owner
,
173 _Jv_Utf8Const
*field_name
,
174 _Jv_Utf8Const
*field_type_name
)
176 // FIXME: this allocates a _Jv_Utf8Const each time. We should make
178 jclass field_type
= _Jv_FindClassFromSignature (field_type_name
->chars(),
180 if (field_type
== NULL
)
181 throw new java::lang::NoClassDefFoundError(field_name
->toString());
183 _Jv_Field
*the_field
= find_field_helper (owner
, field_name
,
184 field_type
->name
, found_class
);
188 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer();
189 sb
->append(JvNewStringLatin1("field "));
190 sb
->append(owner
->getName());
191 sb
->append(JvNewStringLatin1("."));
192 sb
->append(_Jv_NewStringUTF(field_name
->chars()));
193 sb
->append(JvNewStringLatin1(" was not found."));
194 throw new java::lang::NoSuchFieldError (sb
->toString());
197 if (_Jv_CheckAccess (klass
, *found_class
, the_field
->flags
))
199 // Note that the field returned by find_field_helper is always
200 // resolved. There's no point checking class loaders here,
201 // since we already did the work to look up all the types.
202 // FIXME: being lazy here would be nice.
203 if (the_field
->type
!= field_type
)
204 throw new java::lang::LinkageError
206 ("field type mismatch with different loaders"));
210 java::lang::StringBuffer
*sb
211 = new java::lang::StringBuffer ();
212 sb
->append(klass
->getName());
213 sb
->append(JvNewStringLatin1(": "));
214 sb
->append((*found_class
)->getName());
215 sb
->append(JvNewStringLatin1("."));
216 sb
->append(_Jv_NewStringUtf8Const (field_name
));
217 throw new java::lang::IllegalAccessError(sb
->toString());
224 _Jv_Linker::resolve_pool_entry (jclass klass
, int index
)
226 using namespace java::lang::reflect
;
228 _Jv_Constants
*pool
= &klass
->constants
;
230 if ((pool
->tags
[index
] & JV_CONSTANT_ResolvedFlag
) != 0)
231 return pool
->data
[index
];
233 switch (pool
->tags
[index
])
235 case JV_CONSTANT_Class
:
237 _Jv_Utf8Const
*name
= pool
->data
[index
].utf8
;
240 if (name
->first() == '[')
241 found
= _Jv_FindClassFromSignature (name
->chars(),
244 found
= _Jv_FindClass (name
, klass
->loader
);
247 throw new java::lang::NoClassDefFoundError (name
->toString());
249 // Check accessibility, but first strip array types as
250 // _Jv_ClassNameSamePackage can't handle arrays.
253 check
&& check
->isArray();
254 check
= check
->getComponentType())
256 if ((found
->accflags
& Modifier::PUBLIC
) == Modifier::PUBLIC
257 || (_Jv_ClassNameSamePackage (check
->name
,
260 pool
->data
[index
].clazz
= found
;
261 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
265 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer ();
266 sb
->append(klass
->getName());
267 sb
->append(JvNewStringLatin1(" can't access class "));
268 sb
->append(found
->getName());
269 throw new java::lang::IllegalAccessError(sb
->toString());
274 case JV_CONSTANT_String
:
277 str
= _Jv_NewStringUtf8Const (pool
->data
[index
].utf8
);
278 pool
->data
[index
].o
= str
;
279 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
283 case JV_CONSTANT_Fieldref
:
285 _Jv_ushort class_index
, name_and_type_index
;
286 _Jv_loadIndexes (&pool
->data
[index
],
288 name_and_type_index
);
289 jclass owner
= (resolve_pool_entry (klass
, class_index
)).clazz
;
292 _Jv_InitClass (owner
);
294 _Jv_ushort name_index
, type_index
;
295 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
299 _Jv_Utf8Const
*field_name
= pool
->data
[name_index
].utf8
;
300 _Jv_Utf8Const
*field_type_name
= pool
->data
[type_index
].utf8
;
302 jclass found_class
= 0;
303 _Jv_Field
*the_field
= find_field (klass
, owner
,
307 if (owner
!= found_class
)
308 _Jv_InitClass (found_class
);
309 pool
->data
[index
].field
= the_field
;
310 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
314 case JV_CONSTANT_Methodref
:
315 case JV_CONSTANT_InterfaceMethodref
:
317 _Jv_ushort class_index
, name_and_type_index
;
318 _Jv_loadIndexes (&pool
->data
[index
],
320 name_and_type_index
);
321 jclass owner
= (resolve_pool_entry (klass
, class_index
)).clazz
;
324 _Jv_InitClass (owner
);
326 _Jv_ushort name_index
, type_index
;
327 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
331 _Jv_Utf8Const
*method_name
= pool
->data
[name_index
].utf8
;
332 _Jv_Utf8Const
*method_signature
= pool
->data
[type_index
].utf8
;
334 _Jv_Method
*the_method
= 0;
335 jclass found_class
= 0;
337 // We're going to cache a pointer to the _Jv_Method object
338 // when we find it. So, to ensure this doesn't get moved from
339 // beneath us, we first put all the needed Miranda methods
340 // into the target class.
341 wait_for_state (klass
, JV_STATE_LOADED
);
343 // First search the class itself.
344 the_method
= search_method_in_class (owner
, klass
,
345 method_name
, method_signature
);
350 goto end_of_method_search
;
353 // If we are resolving an interface method, search the
354 // interface's superinterfaces (A superinterface is not an
355 // interface's superclass - a superinterface is implemented by
357 if (pool
->tags
[index
] == JV_CONSTANT_InterfaceMethodref
)
362 ifaces
.list
= (jclass
*) _Jv_Malloc (ifaces
.len
363 * sizeof (jclass
*));
365 get_interfaces (owner
, &ifaces
);
367 for (int i
= 0; i
< ifaces
.count
; i
++)
369 jclass cls
= ifaces
.list
[i
];
370 the_method
= search_method_in_class (cls
, klass
, method_name
,
379 _Jv_Free (ifaces
.list
);
382 goto end_of_method_search
;
385 // Finally, search superclasses.
386 for (jclass cls
= owner
->getSuperclass (); cls
!= 0;
387 cls
= cls
->getSuperclass ())
389 the_method
= search_method_in_class (cls
, klass
, method_name
,
398 end_of_method_search
:
400 // FIXME: if (cls->loader != klass->loader), then we
401 // must actually check that the types of arguments
402 // correspond. That is, for each argument type, and
403 // the return type, doing _Jv_FindClassFromSignature
404 // with either loader should produce the same result,
405 // i.e., exactly the same jclass object. JVMS 5.4.3.3
409 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer();
410 sb
->append(JvNewStringLatin1("method "));
411 sb
->append(owner
->getName());
412 sb
->append(JvNewStringLatin1("."));
413 sb
->append(_Jv_NewStringUTF(method_name
->chars()));
414 sb
->append(JvNewStringLatin1(" with signature "));
415 sb
->append(_Jv_NewStringUTF(method_signature
->chars()));
416 sb
->append(JvNewStringLatin1(" was not found."));
417 throw new java::lang::NoSuchMethodError (sb
->toString());
420 int vtable_index
= -1;
421 if (pool
->tags
[index
] != JV_CONSTANT_InterfaceMethodref
)
422 vtable_index
= (jshort
)the_method
->index
;
424 pool
->data
[index
].rmethod
425 = klass
->engine
->resolve_method(the_method
,
427 ((the_method
->accflags
428 & Modifier::STATIC
) != 0),
430 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
434 return pool
->data
[index
];
437 // This function is used to lazily locate superclasses and
438 // superinterfaces. This must be called with the class lock held.
440 _Jv_Linker::resolve_class_ref (jclass klass
, jclass
*classref
)
442 jclass ret
= *classref
;
444 // If superclass looks like a constant pool entry, resolve it now.
445 if (ret
&& (uaddr
) ret
< (uaddr
) klass
->constants
.size
)
447 if (klass
->state
< JV_STATE_LINKED
)
449 _Jv_Utf8Const
*name
= klass
->constants
.data
[(uaddr
) *classref
].utf8
;
450 ret
= _Jv_FindClass (name
, klass
->loader
);
453 throw new java::lang::NoClassDefFoundError (name
->toString());
457 ret
= klass
->constants
.data
[(uaddr
) classref
].clazz
;
462 // Find a method declared in the cls that is referenced from klass and
463 // perform access checks.
465 _Jv_Linker::search_method_in_class (jclass cls
, jclass klass
,
466 _Jv_Utf8Const
*method_name
,
467 _Jv_Utf8Const
*method_signature
)
469 using namespace java::lang::reflect
;
471 for (int i
= 0; i
< cls
->method_count
; i
++)
473 _Jv_Method
*method
= &cls
->methods
[i
];
474 if ( (!_Jv_equalUtf8Consts (method
->name
,
476 || (!_Jv_equalUtf8Consts (method
->signature
,
480 if (_Jv_CheckAccess (klass
, cls
, method
->accflags
))
484 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer();
485 sb
->append(klass
->getName());
486 sb
->append(JvNewStringLatin1(": "));
487 sb
->append(cls
->getName());
488 sb
->append(JvNewStringLatin1("."));
489 sb
->append(_Jv_NewStringUTF(method_name
->chars()));
490 sb
->append(_Jv_NewStringUTF(method_signature
->chars()));
491 throw new java::lang::IllegalAccessError (sb
->toString());
498 #define INITIAL_IOFFSETS_LEN 4
499 #define INITIAL_IFACES_LEN 4
501 static _Jv_IDispatchTable null_idt
= { {SHRT_MAX
, 0, NULL
} };
503 // Generate tables for constant-time assignment testing and interface
504 // method lookup. This implements the technique described by Per Bothner
505 // <per@bothner.com> on the java-discuss mailing list on 1999-09-02:
506 // http://gcc.gnu.org/ml/java/1999-q3/msg00377.html
508 _Jv_Linker::prepare_constant_time_tables (jclass klass
)
510 if (klass
->isPrimitive () || klass
->isInterface ())
513 // Short-circuit in case we've been called already.
514 if ((klass
->idt
!= NULL
) || klass
->depth
!= 0)
517 // Calculate the class depth and ancestor table. The depth of a class
518 // is how many "extends" it is removed from Object. Thus the depth of
519 // java.lang.Object is 0, but the depth of java.io.FilterOutputStream
520 // is 2. Depth is defined for all regular and array classes, but not
521 // interfaces or primitive types.
523 jclass klass0
= klass
;
524 jboolean has_interfaces
= 0;
525 while (klass0
!= &java::lang::Object::class$
)
527 has_interfaces
+= klass0
->interface_count
;
528 klass0
= klass0
->superclass
;
532 // We do class member testing in constant time by using a small table
533 // of all the ancestor classes within each class. The first element is
534 // a pointer to the current class, and the rest are pointers to the
535 // classes ancestors, ordered from the current class down by decreasing
536 // depth. We do not include java.lang.Object in the table of ancestors,
537 // since it is redundant.
539 // FIXME: _Jv_AllocBytes
540 klass
->ancestors
= (jclass
*) _Jv_Malloc (klass
->depth
543 for (int index
= 0; index
< klass
->depth
; index
++)
545 klass
->ancestors
[index
] = klass0
;
546 klass0
= klass0
->superclass
;
549 if ((klass
->accflags
& java::lang::reflect::Modifier::ABSTRACT
) != 0)
552 // Optimization: If class implements no interfaces, use a common
553 // predefined interface table.
556 klass
->idt
= &null_idt
;
560 // FIXME: _Jv_AllocBytes
562 (_Jv_IDispatchTable
*) _Jv_Malloc (sizeof (_Jv_IDispatchTable
));
566 ifaces
.len
= INITIAL_IFACES_LEN
;
567 ifaces
.list
= (jclass
*) _Jv_Malloc (ifaces
.len
* sizeof (jclass
*));
569 int itable_size
= get_interfaces (klass
, &ifaces
);
571 if (ifaces
.count
> 0)
573 klass
->idt
->cls
.itable
=
574 // FIXME: _Jv_AllocBytes
575 (void **) _Jv_Malloc (itable_size
* sizeof (void *));
576 klass
->idt
->cls
.itable_length
= itable_size
;
578 jshort
*itable_offsets
=
579 (jshort
*) _Jv_Malloc (ifaces
.count
* sizeof (jshort
));
581 generate_itable (klass
, &ifaces
, itable_offsets
);
583 jshort cls_iindex
= find_iindex (ifaces
.list
, itable_offsets
,
586 for (int i
= 0; i
< ifaces
.count
; i
++)
588 ifaces
.list
[i
]->idt
->iface
.ioffsets
[cls_iindex
] =
592 klass
->idt
->cls
.iindex
= cls_iindex
;
594 _Jv_Free (ifaces
.list
);
595 _Jv_Free (itable_offsets
);
599 klass
->idt
->cls
.iindex
= SHRT_MAX
;
603 // Return index of item in list, or -1 if item is not present.
605 _Jv_Linker::indexof (void *item
, void **list
, jshort list_len
)
607 for (int i
=0; i
< list_len
; i
++)
615 // Find all unique interfaces directly or indirectly implemented by klass.
616 // Returns the size of the interface dispatch table (itable) for klass, which
617 // is the number of unique interfaces plus the total number of methods that
618 // those interfaces declare. May extend ifaces if required.
620 _Jv_Linker::get_interfaces (jclass klass
, _Jv_ifaces
*ifaces
)
624 for (int i
= 0; i
< klass
->interface_count
; i
++)
626 jclass iface
= klass
->interfaces
[i
];
628 /* Make sure interface is linked. */
629 wait_for_state(iface
, JV_STATE_LINKED
);
631 if (indexof (iface
, (void **) ifaces
->list
, ifaces
->count
) == -1)
633 if (ifaces
->count
+ 1 >= ifaces
->len
)
635 /* Resize ifaces list */
636 ifaces
->len
= ifaces
->len
* 2;
638 = (jclass
*) _Jv_Realloc (ifaces
->list
,
639 ifaces
->len
* sizeof(jclass
));
641 ifaces
->list
[ifaces
->count
] = iface
;
644 result
+= get_interfaces (klass
->interfaces
[i
], ifaces
);
648 if (klass
->isInterface())
649 result
+= klass
->method_count
+ 1;
650 else if (klass
->superclass
)
651 result
+= get_interfaces (klass
->superclass
, ifaces
);
655 // Fill out itable in klass, resolving method declarations in each ifaces.
656 // itable_offsets is filled out with the position of each iface in itable,
657 // such that itable[itable_offsets[n]] == ifaces.list[n].
659 _Jv_Linker::generate_itable (jclass klass
, _Jv_ifaces
*ifaces
,
660 jshort
*itable_offsets
)
662 void **itable
= klass
->idt
->cls
.itable
;
663 jshort itable_pos
= 0;
665 for (int i
= 0; i
< ifaces
->count
; i
++)
667 jclass iface
= ifaces
->list
[i
];
668 itable_offsets
[i
] = itable_pos
;
669 itable_pos
= append_partial_itable (klass
, iface
, itable
, itable_pos
);
671 /* Create interface dispatch table for iface */
672 if (iface
->idt
== NULL
)
674 // FIXME: _Jv_AllocBytes
676 = (_Jv_IDispatchTable
*) _Jv_Malloc (sizeof (_Jv_IDispatchTable
));
678 // The first element of ioffsets is its length (itself included).
679 // FIXME: _Jv_AllocBytes
680 jshort
*ioffsets
= (jshort
*) _Jv_Malloc (INITIAL_IOFFSETS_LEN
682 ioffsets
[0] = INITIAL_IOFFSETS_LEN
;
683 for (int i
= 1; i
< INITIAL_IOFFSETS_LEN
; i
++)
686 iface
->idt
->iface
.ioffsets
= ioffsets
;
691 // Format method name for use in error messages.
693 _Jv_GetMethodString (jclass klass
, _Jv_Method
*meth
,
696 using namespace java::lang
;
697 StringBuffer
*buf
= new StringBuffer (klass
->name
->toString());
698 buf
->append (jchar ('.'));
699 buf
->append (meth
->name
->toString());
700 buf
->append ((jchar
) ' ');
701 buf
->append (meth
->signature
->toString());
704 buf
->append(JvNewStringLatin1(" in "));
705 buf
->append(derived
->name
->toString());
707 return buf
->toString();
711 _Jv_ThrowNoSuchMethodError ()
713 throw new java::lang::NoSuchMethodError
;
716 // Throw a NoSuchFieldError. Called by compiler-generated code when
717 // an otable entry is zero. OTABLE_INDEX is the index in the caller's
718 // otable that refers to the missing field. This index may be used to
719 // print diagnostic information about the field.
721 _Jv_ThrowNoSuchFieldError (int /* otable_index */)
723 throw new java::lang::NoSuchFieldError
;
727 // This is put in empty vtable slots.
729 _Jv_ThrowAbstractMethodError ()
731 throw new java::lang::AbstractMethodError();
734 // Each superinterface of a class (i.e. each interface that the class
735 // directly or indirectly implements) has a corresponding "Partial
736 // Interface Dispatch Table" whose size is (number of methods + 1) words.
737 // The first word is a pointer to the interface (i.e. the java.lang.Class
738 // instance for that interface). The remaining words are pointers to the
739 // actual methods that implement the methods declared in the interface,
740 // in order of declaration.
742 // Append partial interface dispatch table for "iface" to "itable", at
743 // position itable_pos.
744 // Returns the offset at which the next partial ITable should be appended.
746 _Jv_Linker::append_partial_itable (jclass klass
, jclass iface
,
747 void **itable
, jshort pos
)
749 using namespace java::lang::reflect
;
751 itable
[pos
++] = (void *) iface
;
754 for (int j
=0; j
< iface
->method_count
; j
++)
757 for (jclass cl
= klass
; cl
; cl
= cl
->getSuperclass())
759 meth
= _Jv_GetMethodLocal (cl
, iface
->methods
[j
].name
,
760 iface
->methods
[j
].signature
);
766 if (meth
&& (meth
->name
->first() == '<'))
768 // leave a placeholder in the itable for hidden init methods.
773 if ((meth
->accflags
& Modifier::STATIC
) != 0)
774 throw new java::lang::IncompatibleClassChangeError
775 (_Jv_GetMethodString (klass
, meth
));
776 if ((meth
->accflags
& Modifier::PUBLIC
) == 0)
777 throw new java::lang::IllegalAccessError
778 (_Jv_GetMethodString (klass
, meth
));
780 if ((meth
->accflags
& Modifier::ABSTRACT
) != 0)
781 itable
[pos
] = (void *) &_Jv_ThrowAbstractMethodError
;
783 itable
[pos
] = meth
->ncode
;
787 // The method doesn't exist in klass. Binary compatibility rules
788 // permit this, so we delay the error until runtime using a pointer
789 // to a method which throws an exception.
790 itable
[pos
] = (void *) _Jv_ThrowNoSuchMethodError
;
798 static _Jv_Mutex_t iindex_mutex
;
799 static bool iindex_mutex_initialized
= false;
801 // We need to find the correct offset in the Class Interface Dispatch
802 // Table for a given interface. Once we have that, invoking an interface
803 // method just requires combining the Method's index in the interface
804 // (known at compile time) to get the correct method. Doing a type test
805 // (cast or instanceof) is the same problem: Once we have a possible Partial
806 // Interface Dispatch Table, we just compare the first element to see if it
807 // matches the desired interface. So how can we find the correct offset?
808 // Our solution is to keep a vector of candiate offsets in each interface
809 // (idt->iface.ioffsets), and in each class we have an index
810 // (idt->cls.iindex) used to select the correct offset from ioffsets.
812 // Calculate and return iindex for a new class.
813 // ifaces is a vector of num interfaces that the class implements.
814 // offsets[j] is the offset in the interface dispatch table for the
815 // interface corresponding to ifaces[j].
816 // May extend the interface ioffsets if required.
818 _Jv_Linker::find_iindex (jclass
*ifaces
, jshort
*offsets
, jshort num
)
823 // Acquire a global lock to prevent itable corruption in case of multiple
824 // classes that implement an intersecting set of interfaces being linked
825 // simultaneously. We can assume that the mutex will be initialized
827 if (! iindex_mutex_initialized
)
829 _Jv_MutexInit (&iindex_mutex
);
830 iindex_mutex_initialized
= true;
833 _Jv_MutexLock (&iindex_mutex
);
835 for (i
=1;; i
++) /* each potential position in ioffsets */
837 for (j
=0;; j
++) /* each iface */
841 if (i
>= ifaces
[j
]->idt
->iface
.ioffsets
[0])
843 int ioffset
= ifaces
[j
]->idt
->iface
.ioffsets
[i
];
844 /* We can potentially share this position with another class. */
845 if (ioffset
>= 0 && ioffset
!= offsets
[j
])
846 break; /* Nope. Try next i. */
850 for (j
= 0; j
< num
; j
++)
852 int len
= ifaces
[j
]->idt
->iface
.ioffsets
[0];
856 int newlen
= 2 * len
;
859 jshort
*old_ioffsets
= ifaces
[j
]->idt
->iface
.ioffsets
;
860 // FIXME: _Jv_AllocBytes
861 jshort
*new_ioffsets
= (jshort
*) _Jv_Malloc (newlen
863 memcpy (&new_ioffsets
[1], &old_ioffsets
[1],
864 (len
- 1) * sizeof (jshort
));
865 new_ioffsets
[0] = newlen
;
868 new_ioffsets
[len
++] = -1;
870 ifaces
[j
]->idt
->iface
.ioffsets
= new_ioffsets
;
872 ifaces
[j
]->idt
->iface
.ioffsets
[i
] = offsets
[j
];
875 _Jv_MutexUnlock (&iindex_mutex
);
881 // Functions for indirect dispatch (symbolic virtual binding) support.
883 // There are three tables, atable otable and itable. atable is an
884 // array of addresses, and otable is an array of offsets, and these
885 // are used for static and virtual members respectively. itable is an
886 // array of pairs {address, index} where each address is a pointer to
889 // {a,o,i}table_syms is an array of _Jv_MethodSymbols. Each such
890 // symbol is a tuple of {classname, member name, signature}.
892 // Set this to true to enable debugging of indirect dispatch tables/linking.
893 static bool debug_link
= false;
895 // link_symbol_table() scans these two arrays and fills in the
896 // corresponding atable and otable with the addresses of static
897 // members and the offsets of virtual members.
899 // The offset (in bytes) for each resolved method or field is placed
900 // at the corresponding position in the virtual method offset table
903 // The same otable and atable may be shared by many classes.
905 // This must be called while holding the class lock.
908 _Jv_Linker::link_symbol_table (jclass klass
)
911 _Jv_MethodSymbol sym
;
912 if (klass
->otable
== NULL
913 || klass
->otable
->state
!= 0)
916 klass
->otable
->state
= 1;
919 fprintf (stderr
, "Fixing up otable in %s:\n", klass
->name
->chars());
921 (sym
= klass
->otable_syms
[index
]).class_name
!= NULL
;
924 jclass target_class
= _Jv_FindClass (sym
.class_name
, klass
->loader
);
925 _Jv_Method
*meth
= NULL
;
927 _Jv_Utf8Const
*signature
= sym
.signature
;
929 if (target_class
== NULL
)
930 throw new java::lang::NoClassDefFoundError
931 (_Jv_NewStringUTF (sym
.class_name
->chars()));
933 // We're looking for a field or a method, and we can tell
934 // which is needed by looking at the signature.
935 if (signature
->first() == '(' && signature
->len() >= 2)
937 // Looks like someone is trying to invoke an interface method
938 if (target_class
->isInterface())
940 using namespace java::lang
;
941 StringBuffer
*sb
= new StringBuffer();
942 sb
->append(JvNewStringLatin1("found interface "));
943 sb
->append(target_class
->getName());
944 sb
->append(JvNewStringLatin1(" when searching for a class"));
945 throw new VerifyError(sb
->toString());
948 // If the target class does not have a vtable_method_count yet,
949 // then we can't tell the offsets for its methods, so we must lay
951 wait_for_state(target_class
, JV_STATE_PREPARED
);
953 meth
= _Jv_LookupDeclaredMethod(target_class
, sym
.name
,
956 // Every class has a throwNoSuchMethodErrorIndex method that
957 // it inherits from java.lang.Object. Find its vtable
959 static int throwNoSuchMethodErrorIndex
;
960 if (throwNoSuchMethodErrorIndex
== 0)
963 = _Jv_makeUtf8Const ("throwNoSuchMethodError",
964 strlen ("throwNoSuchMethodError"));
966 = _Jv_LookupDeclaredMethod (&java::lang::Object::class$
,
967 name
, gcj::void_signature
);
968 throwNoSuchMethodErrorIndex
969 = _Jv_VTable::idx_to_offset (meth
->index
);
972 // If we don't find a nonstatic method, insert the
973 // vtable index of Object.throwNoSuchMethodError().
974 // This defers the missing method error until an attempt
975 // is made to execute it.
980 offset
= _Jv_VTable::idx_to_offset (meth
->index
);
982 offset
= throwNoSuchMethodErrorIndex
;
985 JvFail ("Bad method index");
986 JvAssert (meth
->index
< target_class
->vtable_method_count
);
988 klass
->otable
->offsets
[index
] = offset
;
992 fprintf (stderr
, " offsets[%d] = %d (class %s@%p : %s(%s))\n",
994 (int)klass
->otable
->offsets
[index
],
995 (const char*)target_class
->name
->chars(),
997 (const char*)sym
.name
->chars(),
998 (const char*)signature
->chars());
1004 wait_for_state(target_class
, JV_STATE_PREPARED
);
1006 _Jv_Field
*the_field
= NULL
;
1009 the_field
= find_field (klass
, target_class
, &found_class
,
1010 sym
.name
, sym
.signature
);
1011 if ((the_field
->flags
& java::lang::reflect::Modifier::STATIC
))
1012 throw new java::lang::IncompatibleClassChangeError
;
1014 klass
->otable
->offsets
[index
] = the_field
->u
.boffset
;
1016 catch (java::lang::NoSuchFieldError
*err
)
1018 klass
->otable
->offsets
[index
] = 0;
1024 if (klass
->atable
== NULL
|| klass
->atable
->state
!= 0)
1027 klass
->atable
->state
= 1;
1030 (sym
= klass
->atable_syms
[index
]).class_name
!= NULL
;
1033 jclass target_class
= _Jv_FindClass (sym
.class_name
, klass
->loader
);
1034 _Jv_Method
*meth
= NULL
;
1035 _Jv_Utf8Const
*signature
= sym
.signature
;
1037 // ??? Setting this pointer to null will at least get us a
1038 // NullPointerException
1039 klass
->atable
->addresses
[index
] = NULL
;
1041 if (target_class
== NULL
)
1042 throw new java::lang::NoClassDefFoundError
1043 (_Jv_NewStringUTF (sym
.class_name
->chars()));
1045 // We're looking for a static field or a static method, and we
1046 // can tell which is needed by looking at the signature.
1047 if (signature
->first() == '(' && signature
->len() >= 2)
1049 // If the target class does not have a vtable_method_count yet,
1050 // then we can't tell the offsets for its methods, so we must lay
1052 wait_for_state (target_class
, JV_STATE_PREPARED
);
1054 // Interface methods cannot have bodies.
1055 if (target_class
->isInterface())
1057 using namespace java::lang
;
1058 StringBuffer
*sb
= new StringBuffer();
1059 sb
->append(JvNewStringLatin1("class "));
1060 sb
->append(target_class
->getName());
1061 sb
->append(JvNewStringLatin1(" is an interface: "
1063 throw new VerifyError(sb
->toString());
1066 meth
= _Jv_LookupDeclaredMethod(target_class
, sym
.name
,
1071 if (meth
->ncode
) // Maybe abstract?
1073 klass
->atable
->addresses
[index
] = meth
->ncode
;
1075 fprintf (stderr
, " addresses[%d] = %p (class %s@%p : %s(%s))\n",
1077 &klass
->atable
->addresses
[index
],
1078 (const char*)target_class
->name
->chars(),
1080 (const char*)sym
.name
->chars(),
1081 (const char*)signature
->chars());
1085 klass
->atable
->addresses
[index
]
1086 = (void *)_Jv_ThrowNoSuchMethodError
;
1093 wait_for_state(target_class
, JV_STATE_PREPARED
);
1095 _Jv_Field
*the_field
= find_field (klass
, target_class
, &found_class
,
1096 sym
.name
, sym
.signature
);
1097 if ((the_field
->flags
& java::lang::reflect::Modifier::STATIC
))
1098 klass
->atable
->addresses
[index
] = the_field
->u
.addr
;
1100 throw new java::lang::IncompatibleClassChangeError
;
1105 if (klass
->itable
== NULL
1106 || klass
->itable
->state
!= 0)
1109 klass
->itable
->state
= 1;
1112 (sym
= klass
->itable_syms
[index
]).class_name
!= NULL
;
1115 jclass target_class
= _Jv_FindClass (sym
.class_name
, klass
->loader
);
1116 _Jv_Utf8Const
*signature
= sym
.signature
;
1121 wait_for_state(target_class
, JV_STATE_LOADED
);
1122 bool found
= _Jv_getInterfaceMethod (target_class
, cls
, i
,
1123 sym
.name
, sym
.signature
);
1127 klass
->itable
->addresses
[index
* 2] = cls
;
1128 klass
->itable
->addresses
[index
* 2 + 1] = (void *)(unsigned long) i
;
1131 fprintf (stderr
, " interfaces[%d] = %p (interface %s@%p : %s(%s))\n",
1133 klass
->itable
->addresses
[index
* 2],
1134 (const char*)cls
->name
->chars(),
1136 (const char*)sym
.name
->chars(),
1137 (const char*)signature
->chars());
1138 fprintf (stderr
, " [%d] = offset %d\n",
1140 (int)(unsigned long)klass
->itable
->addresses
[index
* 2 + 1]);
1145 throw new java::lang::IncompatibleClassChangeError
;
1150 // For each catch_record in the list of caught classes, fill in the
1153 _Jv_Linker::link_exception_table (jclass self
)
1155 struct _Jv_CatchClass
*catch_record
= self
->catch_classes
;
1156 if (!catch_record
|| catch_record
->classname
)
1159 while (catch_record
->classname
)
1164 = _Jv_FindClass (catch_record
->classname
,
1165 self
->getClassLoaderInternal ());
1166 *catch_record
->address
= target_class
;
1168 catch (::java::lang::Throwable
*t
)
1170 // FIXME: We need to do something better here.
1171 *catch_record
->address
= 0;
1175 self
->catch_classes
->classname
= (_Jv_Utf8Const
*)-1;
1178 // Set itable method indexes for members of interface IFACE.
1180 _Jv_Linker::layout_interface_methods (jclass iface
)
1182 if (! iface
->isInterface())
1185 // itable indexes start at 1.
1186 // FIXME: Static initalizers currently get a NULL placeholder entry in the
1187 // itable so they are also assigned an index here.
1188 for (int i
= 0; i
< iface
->method_count
; i
++)
1189 iface
->methods
[i
].index
= i
+ 1;
1192 // Prepare virtual method declarations in KLASS, and any superclasses
1193 // as required, by determining their vtable index, setting
1194 // method->index, and finally setting the class's vtable_method_count.
1195 // Must be called with the lock for KLASS held.
1197 _Jv_Linker::layout_vtable_methods (jclass klass
)
1199 if (klass
->vtable
!= NULL
|| klass
->isInterface()
1200 || klass
->vtable_method_count
!= -1)
1203 jclass superclass
= klass
->getSuperclass();
1205 if (superclass
!= NULL
&& superclass
->vtable_method_count
== -1)
1207 JvSynchronize
sync (superclass
);
1208 layout_vtable_methods (superclass
);
1211 int index
= (superclass
== NULL
? 0 : superclass
->vtable_method_count
);
1213 for (int i
= 0; i
< klass
->method_count
; ++i
)
1215 _Jv_Method
*meth
= &klass
->methods
[i
];
1216 _Jv_Method
*super_meth
= NULL
;
1218 if (! _Jv_isVirtualMethod (meth
))
1221 if (superclass
!= NULL
)
1224 super_meth
= _Jv_LookupDeclaredMethod (superclass
, meth
->name
,
1225 meth
->signature
, &declarer
);
1226 // See if this method actually overrides the other method
1230 if (! _Jv_isVirtualMethod (super_meth
)
1231 || ! _Jv_CheckAccess (klass
, declarer
,
1232 super_meth
->accflags
))
1234 else if ((super_meth
->accflags
1235 & java::lang::reflect::Modifier::FINAL
) != 0)
1237 using namespace java::lang
;
1238 StringBuffer
*sb
= new StringBuffer();
1239 sb
->append(JvNewStringLatin1("method "));
1240 sb
->append(_Jv_GetMethodString(klass
, meth
));
1241 sb
->append(JvNewStringLatin1(" overrides final method "));
1242 sb
->append(_Jv_GetMethodString(declarer
, super_meth
));
1243 throw new VerifyError(sb
->toString());
1249 meth
->index
= super_meth
->index
;
1251 meth
->index
= index
++;
1254 klass
->vtable_method_count
= index
;
1257 // Set entries in VTABLE for virtual methods declared in KLASS.
1259 _Jv_Linker::set_vtable_entries (jclass klass
, _Jv_VTable
*vtable
)
1261 for (int i
= klass
->method_count
- 1; i
>= 0; i
--)
1263 using namespace java::lang::reflect
;
1265 _Jv_Method
*meth
= &klass
->methods
[i
];
1266 if (meth
->index
== (_Jv_ushort
) -1)
1268 if ((meth
->accflags
& Modifier::ABSTRACT
))
1269 // FIXME: it might be nice to have a libffi trampoline here,
1270 // so we could pass in the method name and other information.
1271 vtable
->set_method(meth
->index
,
1272 (void *) &_Jv_ThrowAbstractMethodError
);
1274 vtable
->set_method(meth
->index
, meth
->ncode
);
1278 // Allocate and lay out the virtual method table for KLASS. This will
1279 // also cause vtables to be generated for any non-abstract
1280 // superclasses, and virtual method layout to occur for any abstract
1281 // superclasses. Must be called with monitor lock for KLASS held.
1283 _Jv_Linker::make_vtable (jclass klass
)
1285 using namespace java::lang::reflect
;
1287 // If the vtable exists, or for interface classes, do nothing. All
1288 // other classes, including abstract classes, need a vtable.
1289 if (klass
->vtable
!= NULL
|| klass
->isInterface())
1292 // Ensure all the `ncode' entries are set.
1293 klass
->engine
->create_ncode(klass
);
1295 // Class must be laid out before we can create a vtable.
1296 if (klass
->vtable_method_count
== -1)
1297 layout_vtable_methods (klass
);
1299 // Allocate the new vtable.
1300 _Jv_VTable
*vtable
= _Jv_VTable::new_vtable (klass
->vtable_method_count
);
1301 klass
->vtable
= vtable
;
1303 // Copy the vtable of the closest superclass.
1304 jclass superclass
= klass
->superclass
;
1306 JvSynchronize
sync (superclass
);
1307 make_vtable (superclass
);
1309 for (int i
= 0; i
< superclass
->vtable_method_count
; ++i
)
1310 vtable
->set_method (i
, superclass
->vtable
->get_method (i
));
1312 // Set the class pointer and GC descriptor.
1313 vtable
->clas
= klass
;
1314 vtable
->gc_descr
= _Jv_BuildGCDescr (klass
);
1316 // For each virtual declared in klass, set new vtable entry or
1317 // override an old one.
1318 set_vtable_entries (klass
, vtable
);
1320 // Note that we don't check for abstract methods here. We used to,
1321 // but there is a JVMS clarification that indicates that a check
1322 // here would be too eager. And, a simple test case confirms this.
1325 // Lay out the class, allocating space for static fields and computing
1326 // offsets of instance fields. The class lock must be held by the
1329 _Jv_Linker::ensure_fields_laid_out (jclass klass
)
1331 if (klass
->size_in_bytes
!= -1)
1334 // Compute the alignment for this type by searching through the
1335 // superclasses and finding the maximum required alignment. We
1336 // could consider caching this in the Class.
1337 int max_align
= __alignof__ (java::lang::Object
);
1338 jclass super
= klass
->getSuperclass();
1339 while (super
!= NULL
)
1341 // Ensure that our super has its super installed before
1343 wait_for_state(super
, JV_STATE_LOADING
);
1344 ensure_fields_laid_out(super
);
1345 int num
= JvNumInstanceFields (super
);
1346 _Jv_Field
*field
= JvGetFirstInstanceField (super
);
1349 int field_align
= get_alignment_from_class (field
->type
);
1350 if (field_align
> max_align
)
1351 max_align
= field_align
;
1355 super
= super
->getSuperclass();
1359 int static_size
= 0;
1361 // Although java.lang.Object is never interpreted, an interface can
1362 // have a null superclass. Note that we have to lay out an
1363 // interface because it might have static fields.
1364 if (klass
->superclass
)
1365 instance_size
= klass
->superclass
->size();
1367 instance_size
= java::lang::Object::class$
.size();
1369 for (int i
= 0; i
< klass
->field_count
; i
++)
1374 _Jv_Field
*field
= &klass
->fields
[i
];
1376 if (! field
->isRef ())
1378 // It is safe to resolve the field here, since it's a
1379 // primitive class, which does not cause loading to happen.
1380 resolve_field (field
, klass
->loader
);
1382 field_size
= field
->type
->size ();
1383 field_align
= get_alignment_from_class (field
->type
);
1387 field_size
= sizeof (jobject
);
1388 field_align
= __alignof__ (jobject
);
1391 field
->bsize
= field_size
;
1393 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
))
1395 if (field
->u
.addr
== NULL
)
1397 // This computes an offset into a region we'll allocate
1398 // shortly, and then add this offset to the start
1400 static_size
= ROUND (static_size
, field_align
);
1401 field
->u
.boffset
= static_size
;
1402 static_size
+= field_size
;
1407 instance_size
= ROUND (instance_size
, field_align
);
1408 field
->u
.boffset
= instance_size
;
1409 instance_size
+= field_size
;
1410 if (field_align
> max_align
)
1411 max_align
= field_align
;
1415 if (static_size
!= 0)
1416 klass
->engine
->allocate_static_fields (klass
, static_size
);
1418 // Set the instance size for the class. Note that first we round it
1419 // to the alignment required for this object; this keeps us in sync
1420 // with our current ABI.
1421 instance_size
= ROUND (instance_size
, max_align
);
1422 klass
->size_in_bytes
= instance_size
;
1425 // This takes the class to state JV_STATE_LINKED. The class lock must
1426 // be held when calling this.
1428 _Jv_Linker::ensure_class_linked (jclass klass
)
1430 if (klass
->state
>= JV_STATE_LINKED
)
1433 int state
= klass
->state
;
1436 // Short-circuit, so that mutually dependent classes are ok.
1437 klass
->state
= JV_STATE_LINKED
;
1439 _Jv_Constants
*pool
= &klass
->constants
;
1441 // Compiled classes require that their class constants be
1442 // resolved here. However, interpreted classes need their
1443 // constants to be resolved lazily. If we resolve an
1444 // interpreted class' constants eagerly, we can end up with
1445 // spurious IllegalAccessErrors when the constant pool contains
1446 // a reference to a class we can't access. This can validly
1447 // occur in an obscure case involving the InnerClasses
1449 if (! _Jv_IsInterpretedClass (klass
))
1451 // Resolve class constants first, since other constant pool
1452 // entries may rely on these.
1453 for (int index
= 1; index
< pool
->size
; ++index
)
1455 if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1456 resolve_pool_entry (klass
, index
);
1460 #if 0 // Should be redundant now
1461 // If superclass looks like a constant pool entry,
1463 if ((uaddr
) klass
->superclass
< (uaddr
) pool
->size
)
1464 klass
->superclass
= pool
->data
[(uaddr
) klass
->superclass
].clazz
;
1466 // Likewise for interfaces.
1467 for (int i
= 0; i
< klass
->interface_count
; i
++)
1469 if ((uaddr
) klass
->interfaces
[i
] < (uaddr
) pool
->size
)
1470 klass
->interfaces
[i
]
1471 = pool
->data
[(uaddr
) klass
->interfaces
[i
]].clazz
;
1475 // Resolve the remaining constant pool entries.
1476 for (int index
= 1; index
< pool
->size
; ++index
)
1478 if (pool
->tags
[index
] == JV_CONSTANT_String
)
1482 str
= _Jv_NewStringUtf8Const (pool
->data
[index
].utf8
);
1483 pool
->data
[index
].o
= str
;
1484 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
1488 if (klass
->engine
->need_resolve_string_fields())
1490 jfieldID f
= JvGetFirstStaticField (klass
);
1491 for (int n
= JvNumStaticFields (klass
); n
> 0; --n
)
1493 int mod
= f
->getModifiers ();
1494 // If we have a static String field with a non-null initial
1495 // value, we know it points to a Utf8Const.
1496 resolve_field(f
, klass
->loader
);
1497 if (f
->getClass () == &java::lang::String::class$
1498 && (mod
& java::lang::reflect::Modifier::STATIC
) != 0)
1500 jstring
*strp
= (jstring
*) f
->u
.addr
;
1502 *strp
= _Jv_NewStringUtf8Const ((_Jv_Utf8Const
*) *strp
);
1504 f
= f
->getNextField ();
1508 klass
->notifyAll ();
1510 _Jv_PushClass (klass
);
1512 catch (java::lang::Throwable
*t
)
1514 klass
->state
= state
;
1519 // This ensures that symbolic superclass and superinterface references
1520 // are resolved for the indicated class. This must be called with the
1523 _Jv_Linker::ensure_supers_installed (jclass klass
)
1525 resolve_class_ref (klass
, &klass
->superclass
);
1526 // An interface won't have a superclass.
1527 if (klass
->superclass
)
1528 wait_for_state (klass
->superclass
, JV_STATE_LOADING
);
1530 for (int i
= 0; i
< klass
->interface_count
; ++i
)
1532 resolve_class_ref (klass
, &klass
->interfaces
[i
]);
1533 wait_for_state (klass
->interfaces
[i
], JV_STATE_LOADING
);
1537 // This adds missing `Miranda methods' to a class.
1539 _Jv_Linker::add_miranda_methods (jclass base
, jclass iface_class
)
1541 // Note that at this point, all our supers, and the supers of all
1542 // our superclasses and superinterfaces, will have been installed.
1544 for (int i
= 0; i
< iface_class
->interface_count
; ++i
)
1546 jclass interface
= iface_class
->interfaces
[i
];
1548 for (int j
= 0; j
< interface
->method_count
; ++j
)
1550 _Jv_Method
*meth
= &interface
->methods
[j
];
1551 // Don't bother with <clinit>.
1552 if (meth
->name
->first() == '<')
1554 _Jv_Method
*new_meth
= _Jv_LookupDeclaredMethod (base
, meth
->name
,
1558 // We assume that such methods are very unlikely, so we
1559 // just reallocate the method array each time one is
1560 // found. This greatly simplifies the searching --
1561 // otherwise we have to make sure that each such method
1562 // found is really unique among all superinterfaces.
1563 int new_count
= base
->method_count
+ 1;
1565 = (_Jv_Method
*) _Jv_AllocBytes (sizeof (_Jv_Method
)
1567 memcpy (new_m
, base
->methods
,
1568 sizeof (_Jv_Method
) * base
->method_count
);
1571 new_m
[base
->method_count
] = *meth
;
1572 new_m
[base
->method_count
].index
= (_Jv_ushort
) -1;
1573 new_m
[base
->method_count
].accflags
1574 |= java::lang::reflect::Modifier::INVISIBLE
;
1576 base
->methods
= new_m
;
1577 base
->method_count
= new_count
;
1581 wait_for_state (interface
, JV_STATE_LOADED
);
1582 add_miranda_methods (base
, interface
);
1586 // This ensures that the class' method table is "complete". This must
1587 // be called with the class lock held.
1589 _Jv_Linker::ensure_method_table_complete (jclass klass
)
1591 if (klass
->vtable
!= NULL
)
1594 // We need our superclass to have its own Miranda methods installed.
1595 if (! klass
->isInterface())
1596 wait_for_state (klass
->getSuperclass (), JV_STATE_LOADED
);
1598 // A class might have so-called "Miranda methods". This is a method
1599 // that is declared in an interface and not re-declared in an
1600 // abstract class. Some compilers don't emit declarations for such
1601 // methods in the class; this will give us problems since we expect
1602 // a declaration for any method requiring a vtable entry. We handle
1603 // this here by searching for such methods and constructing new
1604 // internal declarations for them. Note that we do this
1605 // unconditionally, and not just for abstract classes, to correctly
1606 // account for cases where a class is modified to be concrete and
1607 // still incorrectly inherits an abstract method.
1608 int pre_count
= klass
->method_count
;
1609 add_miranda_methods (klass
, klass
);
1611 // Let the execution engine know that we've added methods.
1612 if (klass
->method_count
!= pre_count
)
1613 klass
->engine
->post_miranda_hook(klass
);
1616 // Verify a class. Must be called with class lock held.
1618 _Jv_Linker::verify_class (jclass klass
)
1620 klass
->engine
->verify(klass
);
1623 // Check the assertions contained in the type assertion table for KLASS.
1624 // This is the equivilent of bytecode verification for native, BC-ABI code.
1626 _Jv_Linker::verify_type_assertions (jclass klass
)
1629 fprintf (stderr
, "Evaluating type assertions for %s:\n",
1630 klass
->name
->chars());
1632 if (klass
->assertion_table
== NULL
)
1635 for (int i
= 0;; i
++)
1637 int assertion_code
= klass
->assertion_table
[i
].assertion_code
;
1638 _Jv_Utf8Const
*op1
= klass
->assertion_table
[i
].op1
;
1639 _Jv_Utf8Const
*op2
= klass
->assertion_table
[i
].op2
;
1641 if (assertion_code
== JV_ASSERT_END_OF_TABLE
)
1643 else if (assertion_code
== JV_ASSERT_TYPES_COMPATIBLE
)
1647 fprintf (stderr
, " code=%i, operand A=%s B=%s\n",
1648 assertion_code
, op1
->chars(), op2
->chars());
1651 // The operands are class signatures. op1 is the source,
1652 // op2 is the target.
1653 jclass cl1
= _Jv_FindClassFromSignature (op1
->chars(),
1654 klass
->getClassLoaderInternal());
1655 jclass cl2
= _Jv_FindClassFromSignature (op2
->chars(),
1656 klass
->getClassLoaderInternal());
1658 // If the class doesn't exist, ignore the assertion. An exception
1659 // will be thrown later if an attempt is made to actually
1660 // instantiate the class.
1661 if (cl1
== NULL
|| cl2
== NULL
)
1664 if (! _Jv_IsAssignableFromSlow (cl1
, cl2
))
1666 jstring s
= JvNewStringUTF ("Incompatible types: In class ");
1667 s
= s
->concat (klass
->getName());
1668 s
= s
->concat (JvNewStringUTF (": "));
1669 s
= s
->concat (cl1
->getName());
1670 s
= s
->concat (JvNewStringUTF (" is not assignable to "));
1671 s
= s
->concat (cl2
->getName());
1672 throw new java::lang::VerifyError (s
);
1675 else if (assertion_code
== JV_ASSERT_IS_INSTANTIABLE
)
1677 // TODO: Implement this.
1679 // Unknown assertion codes are ignored, for forwards-compatibility.
1684 _Jv_Linker::print_class_loaded (jclass klass
)
1686 char *codesource
= NULL
;
1687 if (klass
->protectionDomain
!= NULL
)
1689 java::security::CodeSource
*cs
1690 = klass
->protectionDomain
->getCodeSource();
1693 jstring css
= cs
->toString();
1694 int len
= JvGetStringUTFLength(css
);
1695 codesource
= (char *) _Jv_AllocBytes(len
+ 1);
1696 JvGetStringUTFRegion(css
, 0, css
->length(), codesource
);
1697 codesource
[len
] = '\0';
1700 if (codesource
== NULL
)
1701 codesource
= "<no code source>";
1704 if (_Jv_IsInterpretedClass (klass
))
1706 else if (_Jv_IsBinaryCompatibilityABI (klass
))
1707 abi
= "BC-compiled";
1709 abi
= "pre-compiled";
1711 fprintf (stderr
, "[Loaded (%s) %s from %s]\n", abi
, klass
->name
->chars(),
1715 // FIXME: mention invariants and stuff.
1717 _Jv_Linker::wait_for_state (jclass klass
, int state
)
1719 if (klass
->state
>= state
)
1722 JvSynchronize
sync (klass
);
1724 // This is similar to the strategy for class initialization. If we
1725 // already hold the lock, just leave.
1726 java::lang::Thread
*self
= java::lang::Thread::currentThread();
1727 while (klass
->state
<= state
1729 && klass
->thread
!= self
)
1732 java::lang::Thread
*save
= klass
->thread
;
1733 klass
->thread
= self
;
1735 // Print some debugging info if requested. Interpreted classes are
1736 // handled in defineclass, so we only need to handle the two
1737 // pre-compiled cases here.
1738 if (gcj::verbose_class_flag
1739 && (klass
->state
== JV_STATE_COMPILED
1740 || klass
->state
== JV_STATE_PRELOADING
)
1741 && ! _Jv_IsInterpretedClass (klass
))
1742 print_class_loaded (klass
);
1746 if (state
>= JV_STATE_LOADING
&& klass
->state
< JV_STATE_LOADING
)
1748 ensure_supers_installed (klass
);
1749 klass
->set_state(JV_STATE_LOADING
);
1752 if (state
>= JV_STATE_LOADED
&& klass
->state
< JV_STATE_LOADED
)
1754 ensure_method_table_complete (klass
);
1755 klass
->set_state(JV_STATE_LOADED
);
1758 if (state
>= JV_STATE_PREPARED
&& klass
->state
< JV_STATE_PREPARED
)
1760 ensure_fields_laid_out (klass
);
1761 make_vtable (klass
);
1762 layout_interface_methods (klass
);
1763 prepare_constant_time_tables (klass
);
1764 klass
->set_state(JV_STATE_PREPARED
);
1767 if (state
>= JV_STATE_LINKED
&& klass
->state
< JV_STATE_LINKED
)
1769 if (gcj::verifyClasses
)
1770 verify_class (klass
);
1772 ensure_class_linked (klass
);
1773 link_exception_table (klass
);
1774 link_symbol_table (klass
);
1775 klass
->set_state(JV_STATE_LINKED
);
1778 catch (java::lang::Throwable
*exc
)
1780 klass
->thread
= save
;
1781 klass
->set_state(JV_STATE_ERROR
);
1785 klass
->thread
= save
;
1787 if (klass
->state
== JV_STATE_ERROR
)
1788 throw new java::lang::LinkageError
;