1 // link.cc - Code for linking and resolving classes and pool entries.
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 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> */
16 #include <java-interp.h>
22 #include <java-cpool.h>
23 #include <execution.h>
24 #include <java/lang/Class.h>
25 #include <java/lang/String.h>
26 #include <java/lang/StringBuffer.h>
27 #include <java/lang/Thread.h>
28 #include <java/lang/InternalError.h>
29 #include <java/lang/VirtualMachineError.h>
30 #include <java/lang/VerifyError.h>
31 #include <java/lang/NoSuchFieldError.h>
32 #include <java/lang/NoSuchMethodError.h>
33 #include <java/lang/ClassFormatError.h>
34 #include <java/lang/IllegalAccessError.h>
35 #include <java/lang/AbstractMethodError.h>
36 #include <java/lang/NoClassDefFoundError.h>
37 #include <java/lang/IncompatibleClassChangeError.h>
38 #include <java/lang/VerifyError.h>
39 #include <java/lang/VMClassLoader.h>
40 #include <java/lang/reflect/Modifier.h>
41 #include <java/security/CodeSource.h>
45 // When true, print debugging information about class loading.
46 bool gcj::verbose_class_flag
;
48 typedef unsigned int uaddr
__attribute__ ((mode (pointer
)));
57 #define ALIGNOF(TYPE) (offsetof (aligner<TYPE>, field))
59 // This returns the alignment of a type as it would appear in a
60 // structure. This can be different from the alignment of the type
61 // itself. For instance on x86 double is 8-aligned but struct{double}
64 _Jv_Linker::get_alignment_from_class (jclass klass
)
66 if (klass
== JvPrimClass (byte
))
67 return ALIGNOF (jbyte
);
68 else if (klass
== JvPrimClass (short))
69 return ALIGNOF (jshort
);
70 else if (klass
== JvPrimClass (int))
71 return ALIGNOF (jint
);
72 else if (klass
== JvPrimClass (long))
73 return ALIGNOF (jlong
);
74 else if (klass
== JvPrimClass (boolean
))
75 return ALIGNOF (jboolean
);
76 else if (klass
== JvPrimClass (char))
77 return ALIGNOF (jchar
);
78 else if (klass
== JvPrimClass (float))
79 return ALIGNOF (jfloat
);
80 else if (klass
== JvPrimClass (double))
81 return ALIGNOF (jdouble
);
83 return ALIGNOF (jobject
);
87 _Jv_Linker::resolve_field (_Jv_Field
*field
, java::lang::ClassLoader
*loader
)
89 if (! field
->isResolved ())
91 _Jv_Utf8Const
*sig
= (_Jv_Utf8Const
*)field
->type
;
92 field
->type
= _Jv_FindClassFromSignature (sig
->chars(), loader
);
93 field
->flags
&= ~_Jv_FIELD_UNRESOLVED_FLAG
;
98 _Jv_Linker::resolve_pool_entry (jclass klass
, int index
)
100 using namespace java::lang::reflect
;
102 _Jv_Constants
*pool
= &klass
->constants
;
104 if ((pool
->tags
[index
] & JV_CONSTANT_ResolvedFlag
) != 0)
105 return pool
->data
[index
];
107 switch (pool
->tags
[index
])
109 case JV_CONSTANT_Class
:
111 _Jv_Utf8Const
*name
= pool
->data
[index
].utf8
;
114 if (name
->first() == '[')
115 found
= _Jv_FindClassFromSignature (name
->chars(),
118 found
= _Jv_FindClass (name
, klass
->loader
);
121 throw new java::lang::NoClassDefFoundError (name
->toString());
123 // Check accessibility, but first strip array types as
124 // _Jv_ClassNameSamePackage can't handle arrays.
127 check
&& check
->isArray();
128 check
= check
->getComponentType())
130 if ((found
->accflags
& Modifier::PUBLIC
) == Modifier::PUBLIC
131 || (_Jv_ClassNameSamePackage (check
->name
,
134 pool
->data
[index
].clazz
= found
;
135 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
139 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer ();
140 sb
->append(klass
->getName());
141 sb
->append(JvNewStringLatin1(" can't access class "));
142 sb
->append(found
->getName());
143 throw new java::lang::IllegalAccessError(sb
->toString());
148 case JV_CONSTANT_String
:
151 str
= _Jv_NewStringUtf8Const (pool
->data
[index
].utf8
);
152 pool
->data
[index
].o
= str
;
153 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
157 case JV_CONSTANT_Fieldref
:
159 _Jv_ushort class_index
, name_and_type_index
;
160 _Jv_loadIndexes (&pool
->data
[index
],
162 name_and_type_index
);
163 jclass owner
= (resolve_pool_entry (klass
, class_index
)).clazz
;
166 _Jv_InitClass (owner
);
168 _Jv_ushort name_index
, type_index
;
169 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
173 _Jv_Utf8Const
*field_name
= pool
->data
[name_index
].utf8
;
174 _Jv_Utf8Const
*field_type_name
= pool
->data
[type_index
].utf8
;
176 // FIXME: The implementation of this function
177 // (_Jv_FindClassFromSignature) will generate an instance of
178 // _Jv_Utf8Const for each call if the field type is a class name
179 // (Lxx.yy.Z;). This may be too expensive to do for each and
180 // every fieldref being resolved. For now, we fix the problem by
181 // only doing it when we have a loader different from the class
182 // declaring the field.
184 jclass field_type
= 0;
186 if (owner
->loader
!= klass
->loader
)
187 field_type
= _Jv_FindClassFromSignature (field_type_name
->chars(),
190 _Jv_Field
* the_field
= 0;
192 for (jclass cls
= owner
; cls
!= 0; cls
= cls
->getSuperclass ())
194 for (int i
= 0; i
< cls
->field_count
; i
++)
196 _Jv_Field
*field
= &cls
->fields
[i
];
197 if (! _Jv_equalUtf8Consts (field
->name
, field_name
))
200 if (_Jv_CheckAccess (klass
, cls
, field
->flags
))
202 // Resolve the field using the class' own loader if
205 if (!field
->isResolved ())
206 resolve_field (field
, cls
->loader
);
208 if (field_type
!= 0 && field
->type
!= field_type
)
209 throw new java::lang::LinkageError
211 ("field type mismatch with different loaders"));
214 goto end_of_field_search
;
218 java::lang::StringBuffer
*sb
219 = new java::lang::StringBuffer ();
220 sb
->append(klass
->getName());
221 sb
->append(JvNewStringLatin1(": "));
222 sb
->append(cls
->getName());
223 sb
->append(JvNewStringLatin1("."));
224 sb
->append(_Jv_NewStringUtf8Const (field_name
));
225 throw new java::lang::IllegalAccessError(sb
->toString());
233 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer();
234 sb
->append(JvNewStringLatin1("field "));
235 sb
->append(owner
->getName());
236 sb
->append(JvNewStringLatin1("."));
237 sb
->append(_Jv_NewStringUTF(field_name
->chars()));
238 sb
->append(JvNewStringLatin1(" was not found."));
240 new java::lang::IncompatibleClassChangeError (sb
->toString());
243 pool
->data
[index
].field
= the_field
;
244 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
248 case JV_CONSTANT_Methodref
:
249 case JV_CONSTANT_InterfaceMethodref
:
251 _Jv_ushort class_index
, name_and_type_index
;
252 _Jv_loadIndexes (&pool
->data
[index
],
254 name_and_type_index
);
255 jclass owner
= (resolve_pool_entry (klass
, class_index
)).clazz
;
258 _Jv_InitClass (owner
);
260 _Jv_ushort name_index
, type_index
;
261 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
265 _Jv_Utf8Const
*method_name
= pool
->data
[name_index
].utf8
;
266 _Jv_Utf8Const
*method_signature
= pool
->data
[type_index
].utf8
;
268 _Jv_Method
*the_method
= 0;
269 jclass found_class
= 0;
271 // We're going to cache a pointer to the _Jv_Method object
272 // when we find it. So, to ensure this doesn't get moved from
273 // beneath us, we first put all the needed Miranda methods
274 // into the target class.
275 wait_for_state (klass
, JV_STATE_LOADED
);
277 // First search the class itself.
278 the_method
= search_method_in_class (owner
, klass
,
279 method_name
, method_signature
);
284 goto end_of_method_search
;
287 // If we are resolving an interface method, search the
288 // interface's superinterfaces (A superinterface is not an
289 // interface's superclass - a superinterface is implemented by
291 if (pool
->tags
[index
] == JV_CONSTANT_InterfaceMethodref
)
296 ifaces
.list
= (jclass
*) _Jv_Malloc (ifaces
.len
297 * sizeof (jclass
*));
299 get_interfaces (owner
, &ifaces
);
301 for (int i
= 0; i
< ifaces
.count
; i
++)
303 jclass cls
= ifaces
.list
[i
];
304 the_method
= search_method_in_class (cls
, klass
, method_name
,
313 _Jv_Free (ifaces
.list
);
316 goto end_of_method_search
;
319 // Finally, search superclasses.
320 for (jclass cls
= owner
->getSuperclass (); cls
!= 0;
321 cls
= cls
->getSuperclass ())
323 the_method
= search_method_in_class (cls
, klass
, method_name
,
332 end_of_method_search
:
334 // FIXME: if (cls->loader != klass->loader), then we
335 // must actually check that the types of arguments
336 // correspond. That is, for each argument type, and
337 // the return type, doing _Jv_FindClassFromSignature
338 // with either loader should produce the same result,
339 // i.e., exactly the same jclass object. JVMS 5.4.3.3
343 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer();
344 sb
->append(JvNewStringLatin1("method "));
345 sb
->append(owner
->getName());
346 sb
->append(JvNewStringLatin1("."));
347 sb
->append(_Jv_NewStringUTF(method_name
->chars()));
348 sb
->append(JvNewStringLatin1(" with signature "));
349 sb
->append(_Jv_NewStringUTF(method_signature
->chars()));
350 sb
->append(JvNewStringLatin1(" was not found."));
351 throw new java::lang::NoSuchMethodError (sb
->toString());
354 int vtable_index
= -1;
355 if (pool
->tags
[index
] != JV_CONSTANT_InterfaceMethodref
)
356 vtable_index
= (jshort
)the_method
->index
;
358 pool
->data
[index
].rmethod
359 = klass
->engine
->resolve_method(the_method
,
361 ((the_method
->accflags
362 & Modifier::STATIC
) != 0),
364 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
368 return pool
->data
[index
];
371 // This function is used to lazily locate superclasses and
372 // superinterfaces. This must be called with the class lock held.
374 _Jv_Linker::resolve_class_ref (jclass klass
, jclass
*classref
)
376 jclass ret
= *classref
;
378 // If superclass looks like a constant pool entry, resolve it now.
379 if (ret
&& (uaddr
) ret
< (uaddr
) klass
->constants
.size
)
381 if (klass
->state
< JV_STATE_LINKED
)
383 _Jv_Utf8Const
*name
= klass
->constants
.data
[(uaddr
) *classref
].utf8
;
384 ret
= _Jv_FindClass (name
, klass
->loader
);
387 throw new java::lang::NoClassDefFoundError (name
->toString());
391 ret
= klass
->constants
.data
[(uaddr
) classref
].clazz
;
396 // Find a method declared in the cls that is referenced from klass and
397 // perform access checks.
399 _Jv_Linker::search_method_in_class (jclass cls
, jclass klass
,
400 _Jv_Utf8Const
*method_name
,
401 _Jv_Utf8Const
*method_signature
)
403 using namespace java::lang::reflect
;
405 for (int i
= 0; i
< cls
->method_count
; i
++)
407 _Jv_Method
*method
= &cls
->methods
[i
];
408 if ( (!_Jv_equalUtf8Consts (method
->name
,
410 || (!_Jv_equalUtf8Consts (method
->signature
,
414 if (_Jv_CheckAccess (klass
, cls
, method
->accflags
))
418 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer();
419 sb
->append(klass
->getName());
420 sb
->append(JvNewStringLatin1(": "));
421 sb
->append(cls
->getName());
422 sb
->append(JvNewStringLatin1("."));
423 sb
->append(_Jv_NewStringUTF(method_name
->chars()));
424 sb
->append(_Jv_NewStringUTF(method_signature
->chars()));
425 throw new java::lang::IllegalAccessError (sb
->toString());
432 #define INITIAL_IOFFSETS_LEN 4
433 #define INITIAL_IFACES_LEN 4
435 static _Jv_IDispatchTable null_idt
= { {SHRT_MAX
, 0, NULL
} };
437 // Generate tables for constant-time assignment testing and interface
438 // method lookup. This implements the technique described by Per Bothner
439 // <per@bothner.com> on the java-discuss mailing list on 1999-09-02:
440 // http://gcc.gnu.org/ml/java/1999-q3/msg00377.html
442 _Jv_Linker::prepare_constant_time_tables (jclass klass
)
444 if (klass
->isPrimitive () || klass
->isInterface ())
447 // Short-circuit in case we've been called already.
448 if ((klass
->idt
!= NULL
) || klass
->depth
!= 0)
451 // Calculate the class depth and ancestor table. The depth of a class
452 // is how many "extends" it is removed from Object. Thus the depth of
453 // java.lang.Object is 0, but the depth of java.io.FilterOutputStream
454 // is 2. Depth is defined for all regular and array classes, but not
455 // interfaces or primitive types.
457 jclass klass0
= klass
;
458 jboolean has_interfaces
= 0;
459 while (klass0
!= &java::lang::Object::class$
)
461 has_interfaces
+= klass0
->interface_count
;
462 klass0
= klass0
->superclass
;
466 // We do class member testing in constant time by using a small table
467 // of all the ancestor classes within each class. The first element is
468 // a pointer to the current class, and the rest are pointers to the
469 // classes ancestors, ordered from the current class down by decreasing
470 // depth. We do not include java.lang.Object in the table of ancestors,
471 // since it is redundant.
473 // FIXME: _Jv_AllocBytes
474 klass
->ancestors
= (jclass
*) _Jv_Malloc (klass
->depth
477 for (int index
= 0; index
< klass
->depth
; index
++)
479 klass
->ancestors
[index
] = klass0
;
480 klass0
= klass0
->superclass
;
483 if ((klass
->accflags
& java::lang::reflect::Modifier::ABSTRACT
) != 0)
486 // Optimization: If class implements no interfaces, use a common
487 // predefined interface table.
490 klass
->idt
= &null_idt
;
494 // FIXME: _Jv_AllocBytes
496 (_Jv_IDispatchTable
*) _Jv_Malloc (sizeof (_Jv_IDispatchTable
));
500 ifaces
.len
= INITIAL_IFACES_LEN
;
501 ifaces
.list
= (jclass
*) _Jv_Malloc (ifaces
.len
* sizeof (jclass
*));
503 int itable_size
= get_interfaces (klass
, &ifaces
);
505 if (ifaces
.count
> 0)
507 klass
->idt
->cls
.itable
=
508 // FIXME: _Jv_AllocBytes
509 (void **) _Jv_Malloc (itable_size
* sizeof (void *));
510 klass
->idt
->cls
.itable_length
= itable_size
;
512 jshort
*itable_offsets
=
513 (jshort
*) _Jv_Malloc (ifaces
.count
* sizeof (jshort
));
515 generate_itable (klass
, &ifaces
, itable_offsets
);
517 jshort cls_iindex
= find_iindex (ifaces
.list
, itable_offsets
,
520 for (int i
= 0; i
< ifaces
.count
; i
++)
522 ifaces
.list
[i
]->idt
->iface
.ioffsets
[cls_iindex
] =
526 klass
->idt
->cls
.iindex
= cls_iindex
;
528 _Jv_Free (ifaces
.list
);
529 _Jv_Free (itable_offsets
);
533 klass
->idt
->cls
.iindex
= SHRT_MAX
;
537 // Return index of item in list, or -1 if item is not present.
539 _Jv_Linker::indexof (void *item
, void **list
, jshort list_len
)
541 for (int i
=0; i
< list_len
; i
++)
549 // Find all unique interfaces directly or indirectly implemented by klass.
550 // Returns the size of the interface dispatch table (itable) for klass, which
551 // is the number of unique interfaces plus the total number of methods that
552 // those interfaces declare. May extend ifaces if required.
554 _Jv_Linker::get_interfaces (jclass klass
, _Jv_ifaces
*ifaces
)
558 for (int i
= 0; i
< klass
->interface_count
; i
++)
560 jclass iface
= klass
->interfaces
[i
];
562 /* Make sure interface is linked. */
563 wait_for_state(iface
, JV_STATE_LINKED
);
565 if (indexof (iface
, (void **) ifaces
->list
, ifaces
->count
) == -1)
567 if (ifaces
->count
+ 1 >= ifaces
->len
)
569 /* Resize ifaces list */
570 ifaces
->len
= ifaces
->len
* 2;
572 = (jclass
*) _Jv_Realloc (ifaces
->list
,
573 ifaces
->len
* sizeof(jclass
));
575 ifaces
->list
[ifaces
->count
] = iface
;
578 result
+= get_interfaces (klass
->interfaces
[i
], ifaces
);
582 if (klass
->isInterface())
583 result
+= klass
->method_count
+ 1;
584 else if (klass
->superclass
)
585 result
+= get_interfaces (klass
->superclass
, ifaces
);
589 // Fill out itable in klass, resolving method declarations in each ifaces.
590 // itable_offsets is filled out with the position of each iface in itable,
591 // such that itable[itable_offsets[n]] == ifaces.list[n].
593 _Jv_Linker::generate_itable (jclass klass
, _Jv_ifaces
*ifaces
,
594 jshort
*itable_offsets
)
596 void **itable
= klass
->idt
->cls
.itable
;
597 jshort itable_pos
= 0;
599 for (int i
= 0; i
< ifaces
->count
; i
++)
601 jclass iface
= ifaces
->list
[i
];
602 itable_offsets
[i
] = itable_pos
;
603 itable_pos
= append_partial_itable (klass
, iface
, itable
, itable_pos
);
605 /* Create interface dispatch table for iface */
606 if (iface
->idt
== NULL
)
608 // FIXME: _Jv_AllocBytes
610 = (_Jv_IDispatchTable
*) _Jv_Malloc (sizeof (_Jv_IDispatchTable
));
612 // The first element of ioffsets is its length (itself included).
613 // FIXME: _Jv_AllocBytes
614 jshort
*ioffsets
= (jshort
*) _Jv_Malloc (INITIAL_IOFFSETS_LEN
616 ioffsets
[0] = INITIAL_IOFFSETS_LEN
;
617 for (int i
= 1; i
< INITIAL_IOFFSETS_LEN
; i
++)
620 iface
->idt
->iface
.ioffsets
= ioffsets
;
625 // Format method name for use in error messages.
627 _Jv_GetMethodString (jclass klass
, _Jv_Utf8Const
*name
)
629 jstring r
= klass
->name
->toString();
630 r
= r
->concat (JvNewStringUTF ("."));
631 r
= r
->concat (name
->toString());
636 _Jv_ThrowNoSuchMethodError ()
638 throw new java::lang::NoSuchMethodError
;
641 // Each superinterface of a class (i.e. each interface that the class
642 // directly or indirectly implements) has a corresponding "Partial
643 // Interface Dispatch Table" whose size is (number of methods + 1) words.
644 // The first word is a pointer to the interface (i.e. the java.lang.Class
645 // instance for that interface). The remaining words are pointers to the
646 // actual methods that implement the methods declared in the interface,
647 // in order of declaration.
649 // Append partial interface dispatch table for "iface" to "itable", at
650 // position itable_pos.
651 // Returns the offset at which the next partial ITable should be appended.
653 _Jv_Linker::append_partial_itable (jclass klass
, jclass iface
,
654 void **itable
, jshort pos
)
656 using namespace java::lang::reflect
;
658 itable
[pos
++] = (void *) iface
;
661 for (int j
=0; j
< iface
->method_count
; j
++)
664 for (jclass cl
= klass
; cl
; cl
= cl
->getSuperclass())
666 meth
= _Jv_GetMethodLocal (cl
, iface
->methods
[j
].name
,
667 iface
->methods
[j
].signature
);
673 if (meth
&& (meth
->name
->first() == '<'))
675 // leave a placeholder in the itable for hidden init methods.
680 if ((meth
->accflags
& Modifier::STATIC
) != 0)
681 throw new java::lang::IncompatibleClassChangeError
682 (_Jv_GetMethodString (klass
, meth
->name
));
683 if ((meth
->accflags
& Modifier::ABSTRACT
) != 0)
684 throw new java::lang::AbstractMethodError
685 (_Jv_GetMethodString (klass
, meth
->name
));
686 if ((meth
->accflags
& Modifier::PUBLIC
) == 0)
687 throw new java::lang::IllegalAccessError
688 (_Jv_GetMethodString (klass
, meth
->name
));
690 itable
[pos
] = meth
->ncode
;
694 // The method doesn't exist in klass. Binary compatibility rules
695 // permit this, so we delay the error until runtime using a pointer
696 // to a method which throws an exception.
697 itable
[pos
] = (void *) _Jv_ThrowNoSuchMethodError
;
705 static _Jv_Mutex_t iindex_mutex
;
706 static bool iindex_mutex_initialized
= false;
708 // We need to find the correct offset in the Class Interface Dispatch
709 // Table for a given interface. Once we have that, invoking an interface
710 // method just requires combining the Method's index in the interface
711 // (known at compile time) to get the correct method. Doing a type test
712 // (cast or instanceof) is the same problem: Once we have a possible Partial
713 // Interface Dispatch Table, we just compare the first element to see if it
714 // matches the desired interface. So how can we find the correct offset?
715 // Our solution is to keep a vector of candiate offsets in each interface
716 // (idt->iface.ioffsets), and in each class we have an index
717 // (idt->cls.iindex) used to select the correct offset from ioffsets.
719 // Calculate and return iindex for a new class.
720 // ifaces is a vector of num interfaces that the class implements.
721 // offsets[j] is the offset in the interface dispatch table for the
722 // interface corresponding to ifaces[j].
723 // May extend the interface ioffsets if required.
725 _Jv_Linker::find_iindex (jclass
*ifaces
, jshort
*offsets
, jshort num
)
730 // Acquire a global lock to prevent itable corruption in case of multiple
731 // classes that implement an intersecting set of interfaces being linked
732 // simultaneously. We can assume that the mutex will be initialized
734 if (! iindex_mutex_initialized
)
736 _Jv_MutexInit (&iindex_mutex
);
737 iindex_mutex_initialized
= true;
740 _Jv_MutexLock (&iindex_mutex
);
742 for (i
=1;; i
++) /* each potential position in ioffsets */
744 for (j
=0;; j
++) /* each iface */
748 if (i
>= ifaces
[j
]->idt
->iface
.ioffsets
[0])
750 int ioffset
= ifaces
[j
]->idt
->iface
.ioffsets
[i
];
751 /* We can potentially share this position with another class. */
752 if (ioffset
>= 0 && ioffset
!= offsets
[j
])
753 break; /* Nope. Try next i. */
757 for (j
= 0; j
< num
; j
++)
759 int len
= ifaces
[j
]->idt
->iface
.ioffsets
[0];
763 int newlen
= 2 * len
;
766 jshort
*old_ioffsets
= ifaces
[j
]->idt
->iface
.ioffsets
;
767 // FIXME: _Jv_AllocBytes
768 jshort
*new_ioffsets
= (jshort
*) _Jv_Malloc (newlen
770 memcpy (&new_ioffsets
[1], &old_ioffsets
[1],
771 (len
- 1) * sizeof (jshort
));
772 new_ioffsets
[0] = newlen
;
775 new_ioffsets
[len
++] = -1;
777 ifaces
[j
]->idt
->iface
.ioffsets
= new_ioffsets
;
779 ifaces
[j
]->idt
->iface
.ioffsets
[i
] = offsets
[j
];
782 _Jv_MutexUnlock (&iindex_mutex
);
788 // Functions for indirect dispatch (symbolic virtual binding) support.
790 // There are three tables, atable otable and itable. atable is an
791 // array of addresses, and otable is an array of offsets, and these
792 // are used for static and virtual members respectively. itable is an
793 // array of pairs {address, index} where each address is a pointer to
796 // {a,o,i}table_syms is an array of _Jv_MethodSymbols. Each such
797 // symbol is a tuple of {classname, member name, signature}.
799 // Set this to true to enable debugging of indirect dispatch tables/linking.
800 static bool debug_link
= false;
802 // link_symbol_table() scans these two arrays and fills in the
803 // corresponding atable and otable with the addresses of static
804 // members and the offsets of virtual members.
806 // The offset (in bytes) for each resolved method or field is placed
807 // at the corresponding position in the virtual method offset table
810 // The same otable and atable may be shared by many classes.
812 // This must be called while holding the class lock.
815 _Jv_Linker::link_symbol_table (jclass klass
)
818 _Jv_MethodSymbol sym
;
819 if (klass
->otable
== NULL
820 || klass
->otable
->state
!= 0)
823 klass
->otable
->state
= 1;
826 fprintf (stderr
, "Fixing up otable in %s:\n", klass
->name
->chars());
828 (sym
= klass
->otable_syms
[index
]).class_name
!= NULL
;
831 jclass target_class
= _Jv_FindClass (sym
.class_name
, klass
->loader
);
832 _Jv_Method
*meth
= NULL
;
834 _Jv_Utf8Const
*signature
= sym
.signature
;
837 static char *bounce
= (char *)_Jv_ThrowNoSuchMethodError
;
838 ptrdiff_t offset
= (char *)(klass
->vtable
) - bounce
;
839 klass
->otable
->offsets
[index
] = offset
;
842 if (target_class
== NULL
)
843 throw new java::lang::NoClassDefFoundError
844 (_Jv_NewStringUTF (sym
.class_name
->chars()));
846 // We're looking for a field or a method, and we can tell
847 // which is needed by looking at the signature.
848 if (signature
->first() == '(' && signature
->len() >= 2)
850 // Looks like someone is trying to invoke an interface method
851 if (target_class
->isInterface())
853 using namespace java::lang
;
854 StringBuffer
*sb
= new StringBuffer();
855 sb
->append(JvNewStringLatin1("found interface "));
856 sb
->append(target_class
->getName());
857 sb
->append(JvNewStringLatin1(" when searching for a class"));
858 throw new VerifyError(sb
->toString());
861 // If the target class does not have a vtable_method_count yet,
862 // then we can't tell the offsets for its methods, so we must lay
864 wait_for_state(target_class
, JV_STATE_PREPARED
);
866 meth
= _Jv_LookupDeclaredMethod(target_class
, sym
.name
,
871 int offset
= _Jv_VTable::idx_to_offset (meth
->index
);
873 JvFail ("Bad method index");
874 JvAssert (meth
->index
< target_class
->vtable_method_count
);
875 klass
->otable
->offsets
[index
] = offset
;
878 fprintf (stderr
, " offsets[%d] = %d (class %s@%p : %s(%s))\n",
880 (int)klass
->otable
->offsets
[index
],
881 (const char*)target_class
->name
->chars(),
883 (const char*)sym
.name
->chars(),
884 (const char*)signature
->chars());
890 _Jv_Field
*the_field
= NULL
;
892 wait_for_state(target_class
, JV_STATE_PREPARED
);
893 for (jclass cls
= target_class
; cls
!= 0; cls
= cls
->getSuperclass ())
895 for (int i
= 0; i
< cls
->field_count
; i
++)
897 _Jv_Field
*field
= &cls
->fields
[i
];
898 if (! _Jv_equalUtf8Consts (field
->name
, sym
.name
))
901 // FIXME: What access checks should we perform here?
902 // if (_Jv_CheckAccess (klass, cls, field->flags))
905 if (!field
->isResolved ())
906 resolve_field (field
, cls
->loader
);
908 // if (field_type != 0 && field->type != field_type)
909 // throw new java::lang::LinkageError
910 // (JvNewStringLatin1
911 // ("field type mismatch with different loaders"));
915 fprintf (stderr
, " offsets[%d] = %d (class %s@%p : %s)\n",
917 (int)field
->u
.boffset
,
918 (const char*)cls
->name
->chars(),
920 (const char*)field
->name
->chars());
921 goto end_of_field_search
;
925 if (the_field
!= NULL
)
927 if ((the_field
->flags
& java::lang::reflect::Modifier::STATIC
))
928 throw new java::lang::IncompatibleClassChangeError
;
930 klass
->otable
->offsets
[index
] = the_field
->u
.boffset
;
934 throw new java::lang::NoSuchFieldError
935 (_Jv_NewStringUtf8Const (sym
.name
));
941 if (klass
->atable
== NULL
|| klass
->atable
->state
!= 0)
944 klass
->atable
->state
= 1;
947 (sym
= klass
->atable_syms
[index
]).class_name
!= NULL
;
950 jclass target_class
= _Jv_FindClass (sym
.class_name
, klass
->loader
);
951 _Jv_Method
*meth
= NULL
;
952 _Jv_Utf8Const
*signature
= sym
.signature
;
954 // ??? Setting this pointer to null will at least get us a
955 // NullPointerException
956 klass
->atable
->addresses
[index
] = NULL
;
958 if (target_class
== NULL
)
959 throw new java::lang::NoClassDefFoundError
960 (_Jv_NewStringUTF (sym
.class_name
->chars()));
962 // We're looking for a static field or a static method, and we
963 // can tell which is needed by looking at the signature.
964 if (signature
->first() == '(' && signature
->len() >= 2)
966 // If the target class does not have a vtable_method_count yet,
967 // then we can't tell the offsets for its methods, so we must lay
969 wait_for_state (target_class
, JV_STATE_PREPARED
);
971 // Interface methods cannot have bodies.
972 if (target_class
->isInterface())
974 using namespace java::lang
;
975 StringBuffer
*sb
= new StringBuffer();
976 sb
->append(JvNewStringLatin1("class "));
977 sb
->append(target_class
->getName());
978 sb
->append(JvNewStringLatin1(" is an interface: "
980 throw new VerifyError(sb
->toString());
983 meth
= _Jv_LookupDeclaredMethod(target_class
, sym
.name
,
988 if (meth
->ncode
) // Maybe abstract?
990 klass
->atable
->addresses
[index
] = meth
->ncode
;
992 fprintf (stderr
, " addresses[%d] = %p (class %s@%p : %s(%s))\n",
994 &klass
->atable
->addresses
[index
],
995 (const char*)target_class
->name
->chars(),
997 (const char*)sym
.name
->chars(),
998 (const char*)signature
->chars());
1002 klass
->atable
->addresses
[index
]
1003 = (void *)_Jv_ThrowNoSuchMethodError
;
1010 _Jv_Field
*the_field
= NULL
;
1012 wait_for_state(target_class
, JV_STATE_PREPARED
);
1013 for (jclass cls
= target_class
; cls
!= 0; cls
= cls
->getSuperclass ())
1015 for (int i
= 0; i
< cls
->field_count
; i
++)
1017 _Jv_Field
*field
= &cls
->fields
[i
];
1018 if (! _Jv_equalUtf8Consts (field
->name
, sym
.name
))
1021 // FIXME: What access checks should we perform here?
1022 // if (_Jv_CheckAccess (klass, cls, field->flags))
1025 if (!field
->isResolved ())
1026 resolve_field (field
, cls
->loader
);
1028 // if (field_type != 0 && field->type != field_type)
1029 // throw new java::lang::LinkageError
1030 // (JvNewStringLatin1
1031 // ("field type mismatch with different loaders"));
1034 goto end_of_static_field_search
;
1037 end_of_static_field_search
:
1038 if (the_field
!= NULL
)
1040 if ((the_field
->flags
& java::lang::reflect::Modifier::STATIC
))
1041 klass
->atable
->addresses
[index
] = the_field
->u
.addr
;
1043 throw new java::lang::IncompatibleClassChangeError
;
1047 throw new java::lang::NoSuchFieldError
1048 (_Jv_NewStringUtf8Const (sym
.name
));
1054 if (klass
->itable
== NULL
1055 || klass
->itable
->state
!= 0)
1058 klass
->itable
->state
= 1;
1061 (sym
= klass
->itable_syms
[index
]).class_name
!= NULL
;
1064 jclass target_class
= _Jv_FindClass (sym
.class_name
, klass
->loader
);
1065 _Jv_Utf8Const
*signature
= sym
.signature
;
1070 wait_for_state(target_class
, JV_STATE_LOADED
);
1071 bool found
= _Jv_getInterfaceMethod (target_class
, cls
, i
,
1072 sym
.name
, sym
.signature
);
1076 klass
->itable
->addresses
[index
* 2] = cls
;
1077 klass
->itable
->addresses
[index
* 2 + 1] = (void *)(unsigned long) i
;
1080 fprintf (stderr
, " interfaces[%d] = %p (interface %s@%p : %s(%s))\n",
1082 klass
->itable
->addresses
[index
* 2],
1083 (const char*)cls
->name
->chars(),
1085 (const char*)sym
.name
->chars(),
1086 (const char*)signature
->chars());
1087 fprintf (stderr
, " [%d] = offset %d\n",
1089 (int)(unsigned long)klass
->itable
->addresses
[index
* 2 + 1]);
1094 throw new java::lang::IncompatibleClassChangeError
;
1099 // For each catch_record in the list of caught classes, fill in the
1102 _Jv_Linker::link_exception_table (jclass self
)
1104 struct _Jv_CatchClass
*catch_record
= self
->catch_classes
;
1105 if (!catch_record
|| catch_record
->classname
)
1108 while (catch_record
->classname
)
1113 = _Jv_FindClass (catch_record
->classname
,
1114 self
->getClassLoaderInternal ());
1115 *catch_record
->address
= target_class
;
1117 catch (::java::lang::Throwable
*t
)
1119 // FIXME: We need to do something better here.
1120 *catch_record
->address
= 0;
1124 self
->catch_classes
->classname
= (_Jv_Utf8Const
*)-1;
1127 // This is put in empty vtable slots.
1129 _Jv_abstractMethodError (void)
1131 throw new java::lang::AbstractMethodError();
1134 // Set itable method indexes for members of interface IFACE.
1136 _Jv_Linker::layout_interface_methods (jclass iface
)
1138 if (! iface
->isInterface())
1141 // itable indexes start at 1.
1142 // FIXME: Static initalizers currently get a NULL placeholder entry in the
1143 // itable so they are also assigned an index here.
1144 for (int i
= 0; i
< iface
->method_count
; i
++)
1145 iface
->methods
[i
].index
= i
+ 1;
1148 // Prepare virtual method declarations in KLASS, and any superclasses
1149 // as required, by determining their vtable index, setting
1150 // method->index, and finally setting the class's vtable_method_count.
1151 // Must be called with the lock for KLASS held.
1153 _Jv_Linker::layout_vtable_methods (jclass klass
)
1155 if (klass
->vtable
!= NULL
|| klass
->isInterface()
1156 || klass
->vtable_method_count
!= -1)
1159 jclass superclass
= klass
->getSuperclass();
1161 if (superclass
!= NULL
&& superclass
->vtable_method_count
== -1)
1163 JvSynchronize
sync (superclass
);
1164 layout_vtable_methods (superclass
);
1167 int index
= (superclass
== NULL
? 0 : superclass
->vtable_method_count
);
1169 for (int i
= 0; i
< klass
->method_count
; ++i
)
1171 _Jv_Method
*meth
= &klass
->methods
[i
];
1172 _Jv_Method
*super_meth
= NULL
;
1174 if (! _Jv_isVirtualMethod (meth
))
1177 if (superclass
!= NULL
)
1180 super_meth
= _Jv_LookupDeclaredMethod (superclass
, meth
->name
,
1181 meth
->signature
, &declarer
);
1182 // See if this method actually overrides the other method
1186 if (! _Jv_isVirtualMethod (super_meth
)
1187 || ! _Jv_CheckAccess (klass
, declarer
,
1188 super_meth
->accflags
))
1190 else if ((super_meth
->accflags
1191 & java::lang::reflect::Modifier::FINAL
) != 0)
1193 using namespace java::lang
;
1194 StringBuffer
*sb
= new StringBuffer();
1195 sb
->append(JvNewStringLatin1("method "));
1196 sb
->append(_Jv_GetMethodString(klass
, meth
->name
));
1197 sb
->append(JvNewStringLatin1(" overrides final method "));
1198 sb
->append(_Jv_GetMethodString(declarer
, super_meth
->name
));
1199 throw new VerifyError(sb
->toString());
1205 meth
->index
= super_meth
->index
;
1207 meth
->index
= index
++;
1210 klass
->vtable_method_count
= index
;
1213 // Set entries in VTABLE for virtual methods declared in KLASS.
1215 _Jv_Linker::set_vtable_entries (jclass klass
, _Jv_VTable
*vtable
)
1217 for (int i
= klass
->method_count
- 1; i
>= 0; i
--)
1219 using namespace java::lang::reflect
;
1221 _Jv_Method
*meth
= &klass
->methods
[i
];
1222 if (meth
->index
== (_Jv_ushort
) -1)
1224 if ((meth
->accflags
& Modifier::ABSTRACT
))
1225 vtable
->set_method(meth
->index
, (void *) &_Jv_abstractMethodError
);
1227 vtable
->set_method(meth
->index
, meth
->ncode
);
1231 // Allocate and lay out the virtual method table for KLASS. This will
1232 // also cause vtables to be generated for any non-abstract
1233 // superclasses, and virtual method layout to occur for any abstract
1234 // superclasses. Must be called with monitor lock for KLASS held.
1236 _Jv_Linker::make_vtable (jclass klass
)
1238 using namespace java::lang::reflect
;
1240 // If the vtable exists, or for interface classes, do nothing. All
1241 // other classes, including abstract classes, need a vtable.
1242 if (klass
->vtable
!= NULL
|| klass
->isInterface())
1245 // Ensure all the `ncode' entries are set.
1246 klass
->engine
->create_ncode(klass
);
1248 // Class must be laid out before we can create a vtable.
1249 if (klass
->vtable_method_count
== -1)
1250 layout_vtable_methods (klass
);
1252 // Allocate the new vtable.
1253 _Jv_VTable
*vtable
= _Jv_VTable::new_vtable (klass
->vtable_method_count
);
1254 klass
->vtable
= vtable
;
1256 // Copy the vtable of the closest superclass.
1257 jclass superclass
= klass
->superclass
;
1259 JvSynchronize
sync (superclass
);
1260 make_vtable (superclass
);
1262 for (int i
= 0; i
< superclass
->vtable_method_count
; ++i
)
1263 vtable
->set_method (i
, superclass
->vtable
->get_method (i
));
1265 // Set the class pointer and GC descriptor.
1266 vtable
->clas
= klass
;
1267 vtable
->gc_descr
= _Jv_BuildGCDescr (klass
);
1269 // For each virtual declared in klass, set new vtable entry or
1270 // override an old one.
1271 set_vtable_entries (klass
, vtable
);
1273 // It is an error to have an abstract method in a concrete class.
1274 if (! (klass
->accflags
& Modifier::ABSTRACT
))
1276 for (int i
= 0; i
< klass
->vtable_method_count
; ++i
)
1277 if (vtable
->get_method(i
) == (void *) &_Jv_abstractMethodError
)
1279 using namespace java::lang
;
1280 while (klass
!= NULL
)
1282 for (int j
= 0; j
< klass
->method_count
; ++j
)
1284 if (klass
->methods
[j
].index
== i
)
1286 StringBuffer
*buf
= new StringBuffer ();
1287 buf
->append (_Jv_NewStringUtf8Const (klass
->methods
[j
].name
));
1288 buf
->append ((jchar
) ' ');
1289 buf
->append (_Jv_NewStringUtf8Const (klass
->methods
[j
].signature
));
1290 throw new AbstractMethodError (buf
->toString ());
1293 klass
= klass
->getSuperclass ();
1295 // Couldn't find the name, which is weird.
1296 // But we still must throw the error.
1297 throw new AbstractMethodError ();
1302 // Lay out the class, allocating space for static fields and computing
1303 // offsets of instance fields. The class lock must be held by the
1306 _Jv_Linker::ensure_fields_laid_out (jclass klass
)
1308 if (klass
->size_in_bytes
!= -1)
1311 // Compute the alignment for this type by searching through the
1312 // superclasses and finding the maximum required alignment. We
1313 // could consider caching this in the Class.
1314 int max_align
= __alignof__ (java::lang::Object
);
1315 jclass super
= klass
->getSuperclass();
1316 while (super
!= NULL
)
1318 // Ensure that our super has its super installed before
1320 wait_for_state(super
, JV_STATE_LOADING
);
1321 ensure_fields_laid_out(super
);
1322 int num
= JvNumInstanceFields (super
);
1323 _Jv_Field
*field
= JvGetFirstInstanceField (super
);
1326 int field_align
= get_alignment_from_class (field
->type
);
1327 if (field_align
> max_align
)
1328 max_align
= field_align
;
1332 super
= super
->getSuperclass();
1336 int static_size
= 0;
1338 // Although java.lang.Object is never interpreted, an interface can
1339 // have a null superclass. Note that we have to lay out an
1340 // interface because it might have static fields.
1341 if (klass
->superclass
)
1342 instance_size
= klass
->superclass
->size();
1344 instance_size
= java::lang::Object::class$
.size();
1346 for (int i
= 0; i
< klass
->field_count
; i
++)
1351 _Jv_Field
*field
= &klass
->fields
[i
];
1353 if (! field
->isRef ())
1355 // It is safe to resolve the field here, since it's a
1356 // primitive class, which does not cause loading to happen.
1357 resolve_field (field
, klass
->loader
);
1359 field_size
= field
->type
->size ();
1360 field_align
= get_alignment_from_class (field
->type
);
1364 field_size
= sizeof (jobject
);
1365 field_align
= __alignof__ (jobject
);
1368 field
->bsize
= field_size
;
1370 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
))
1372 if (field
->u
.addr
== NULL
)
1374 // This computes an offset into a region we'll allocate
1375 // shortly, and then add this offset to the start
1377 static_size
= ROUND (static_size
, field_align
);
1378 field
->u
.boffset
= static_size
;
1379 static_size
+= field_size
;
1384 instance_size
= ROUND (instance_size
, field_align
);
1385 field
->u
.boffset
= instance_size
;
1386 instance_size
+= field_size
;
1387 if (field_align
> max_align
)
1388 max_align
= field_align
;
1392 if (static_size
!= 0)
1393 klass
->engine
->allocate_static_fields (klass
, static_size
);
1395 // Set the instance size for the class. Note that first we round it
1396 // to the alignment required for this object; this keeps us in sync
1397 // with our current ABI.
1398 instance_size
= ROUND (instance_size
, max_align
);
1399 klass
->size_in_bytes
= instance_size
;
1402 // This takes the class to state JV_STATE_LINKED. The class lock must
1403 // be held when calling this.
1405 _Jv_Linker::ensure_class_linked (jclass klass
)
1407 if (klass
->state
>= JV_STATE_LINKED
)
1410 int state
= klass
->state
;
1413 // Short-circuit, so that mutually dependent classes are ok.
1414 klass
->state
= JV_STATE_LINKED
;
1416 _Jv_Constants
*pool
= &klass
->constants
;
1418 // Compiled classes require that their class constants be
1419 // resolved here. However, interpreted classes need their
1420 // constants to be resolved lazily. If we resolve an
1421 // interpreted class' constants eagerly, we can end up with
1422 // spurious IllegalAccessErrors when the constant pool contains
1423 // a reference to a class we can't access. This can validly
1424 // occur in an obscure case involving the InnerClasses
1427 if (! _Jv_IsInterpretedClass (klass
))
1430 // Resolve class constants first, since other constant pool
1431 // entries may rely on these.
1432 for (int index
= 1; index
< pool
->size
; ++index
)
1434 if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1435 resolve_pool_entry (klass
, index
);
1439 #if 0 // Should be redundant now
1440 // If superclass looks like a constant pool entry,
1442 if ((uaddr
) klass
->superclass
< (uaddr
) pool
->size
)
1443 klass
->superclass
= pool
->data
[(uaddr
) klass
->superclass
].clazz
;
1445 // Likewise for interfaces.
1446 for (int i
= 0; i
< klass
->interface_count
; i
++)
1448 if ((uaddr
) klass
->interfaces
[i
] < (uaddr
) pool
->size
)
1449 klass
->interfaces
[i
]
1450 = pool
->data
[(uaddr
) klass
->interfaces
[i
]].clazz
;
1454 // Resolve the remaining constant pool entries.
1455 for (int index
= 1; index
< pool
->size
; ++index
)
1457 if (pool
->tags
[index
] == JV_CONSTANT_String
)
1461 str
= _Jv_NewStringUtf8Const (pool
->data
[index
].utf8
);
1462 pool
->data
[index
].o
= str
;
1463 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
1467 if (klass
->engine
->need_resolve_string_fields())
1469 jfieldID f
= JvGetFirstStaticField (klass
);
1470 for (int n
= JvNumStaticFields (klass
); n
> 0; --n
)
1472 int mod
= f
->getModifiers ();
1473 // If we have a static String field with a non-null initial
1474 // value, we know it points to a Utf8Const.
1475 resolve_field(f
, klass
->loader
);
1476 if (f
->getClass () == &java::lang::String::class$
1477 && (mod
& java::lang::reflect::Modifier::STATIC
) != 0)
1479 jstring
*strp
= (jstring
*) f
->u
.addr
;
1481 *strp
= _Jv_NewStringUtf8Const ((_Jv_Utf8Const
*) *strp
);
1483 f
= f
->getNextField ();
1487 klass
->notifyAll ();
1489 _Jv_PushClass (klass
);
1491 catch (java::lang::Throwable
*t
)
1493 klass
->state
= state
;
1498 // This ensures that symbolic superclass and superinterface references
1499 // are resolved for the indicated class. This must be called with the
1502 _Jv_Linker::ensure_supers_installed (jclass klass
)
1504 resolve_class_ref (klass
, &klass
->superclass
);
1505 // An interface won't have a superclass.
1506 if (klass
->superclass
)
1507 wait_for_state (klass
->superclass
, JV_STATE_LOADING
);
1509 for (int i
= 0; i
< klass
->interface_count
; ++i
)
1511 resolve_class_ref (klass
, &klass
->interfaces
[i
]);
1512 wait_for_state (klass
->interfaces
[i
], JV_STATE_LOADING
);
1516 // This adds missing `Miranda methods' to a class.
1518 _Jv_Linker::add_miranda_methods (jclass base
, jclass iface_class
)
1520 // Note that at this point, all our supers, and the supers of all
1521 // our superclasses and superinterfaces, will have been installed.
1523 for (int i
= 0; i
< iface_class
->interface_count
; ++i
)
1525 jclass interface
= iface_class
->interfaces
[i
];
1527 for (int j
= 0; j
< interface
->method_count
; ++j
)
1529 _Jv_Method
*meth
= &interface
->methods
[j
];
1530 // Don't bother with <clinit>.
1531 if (meth
->name
->first() == '<')
1533 _Jv_Method
*new_meth
= _Jv_LookupDeclaredMethod (base
, meth
->name
,
1537 // We assume that such methods are very unlikely, so we
1538 // just reallocate the method array each time one is
1539 // found. This greatly simplifies the searching --
1540 // otherwise we have to make sure that each such method
1541 // found is really unique among all superinterfaces.
1542 int new_count
= base
->method_count
+ 1;
1544 = (_Jv_Method
*) _Jv_AllocBytes (sizeof (_Jv_Method
)
1546 memcpy (new_m
, base
->methods
,
1547 sizeof (_Jv_Method
) * base
->method_count
);
1550 new_m
[base
->method_count
] = *meth
;
1551 new_m
[base
->method_count
].index
= (_Jv_ushort
) -1;
1552 new_m
[base
->method_count
].accflags
1553 |= java::lang::reflect::Modifier::INVISIBLE
;
1555 base
->methods
= new_m
;
1556 base
->method_count
= new_count
;
1560 wait_for_state (interface
, JV_STATE_LOADED
);
1561 add_miranda_methods (base
, interface
);
1565 // This ensures that the class' method table is "complete". This must
1566 // be called with the class lock held.
1568 _Jv_Linker::ensure_method_table_complete (jclass klass
)
1570 if (klass
->vtable
!= NULL
|| klass
->isInterface())
1573 // We need our superclass to have its own Miranda methods installed.
1574 wait_for_state (klass
->getSuperclass (), JV_STATE_LOADED
);
1576 // A class might have so-called "Miranda methods". This is a method
1577 // that is declared in an interface and not re-declared in an
1578 // abstract class. Some compilers don't emit declarations for such
1579 // methods in the class; this will give us problems since we expect
1580 // a declaration for any method requiring a vtable entry. We handle
1581 // this here by searching for such methods and constructing new
1582 // internal declarations for them. Note that we do this
1583 // unconditionally, and not just for abstract classes, to correctly
1584 // account for cases where a class is modified to be concrete and
1585 // still incorrectly inherits an abstract method.
1586 int pre_count
= klass
->method_count
;
1587 add_miranda_methods (klass
, klass
);
1589 // Let the execution engine know that we've added methods.
1590 if (klass
->method_count
!= pre_count
)
1591 klass
->engine
->post_miranda_hook(klass
);
1594 // Verify a class. Must be called with class lock held.
1596 _Jv_Linker::verify_class (jclass klass
)
1598 klass
->engine
->verify(klass
);
1601 // Check the assertions contained in the type assertion table for KLASS.
1602 // This is the equivilent of bytecode verification for native, BC-ABI code.
1604 _Jv_Linker::verify_type_assertions (jclass klass
)
1607 fprintf (stderr
, "Evaluating type assertions for %s:\n",
1608 klass
->name
->chars());
1610 if (klass
->assertion_table
== NULL
)
1613 for (int i
= 0;; i
++)
1615 int assertion_code
= klass
->assertion_table
[i
].assertion_code
;
1616 _Jv_Utf8Const
*op1
= klass
->assertion_table
[i
].op1
;
1617 _Jv_Utf8Const
*op2
= klass
->assertion_table
[i
].op2
;
1619 if (assertion_code
== JV_ASSERT_END_OF_TABLE
)
1621 else if (assertion_code
== JV_ASSERT_TYPES_COMPATIBLE
)
1625 fprintf (stderr
, " code=%i, operand A=%s B=%s\n",
1626 assertion_code
, op1
->chars(), op2
->chars());
1629 // The operands are class signatures. op1 is the source,
1630 // op2 is the target.
1631 jclass cl1
= _Jv_FindClassFromSignature (op1
->chars(),
1632 klass
->getClassLoaderInternal());
1633 jclass cl2
= _Jv_FindClassFromSignature (op2
->chars(),
1634 klass
->getClassLoaderInternal());
1636 // If the class doesn't exist, ignore the assertion. An exception
1637 // will be thrown later if an attempt is made to actually
1638 // instantiate the class.
1639 if (cl1
== NULL
|| cl2
== NULL
)
1642 if (! _Jv_IsAssignableFromSlow (cl2
, cl1
))
1644 jstring s
= JvNewStringUTF ("Incompatible types: In class ");
1645 s
= s
->concat (klass
->getName());
1646 s
= s
->concat (JvNewStringUTF (": "));
1647 s
= s
->concat (cl1
->getName());
1648 s
= s
->concat (JvNewStringUTF (" is not assignable to "));
1649 s
= s
->concat (cl2
->getName());
1650 throw new java::lang::VerifyError (s
);
1653 else if (assertion_code
== JV_ASSERT_IS_INSTANTIABLE
)
1655 // TODO: Implement this.
1657 // Unknown assertion codes are ignored, for forwards-compatibility.
1662 _Jv_Linker::print_class_loaded (jclass klass
)
1664 char *codesource
= NULL
;
1665 if (klass
->protectionDomain
!= NULL
)
1667 java::security::CodeSource
*cs
1668 = klass
->protectionDomain
->getCodeSource();
1671 jstring css
= cs
->toString();
1672 int len
= JvGetStringUTFLength(css
);
1673 codesource
= (char *) _Jv_AllocBytes(len
+ 1);
1674 JvGetStringUTFRegion(css
, 0, css
->length(), codesource
);
1675 codesource
[len
] = '\0';
1678 if (codesource
== NULL
)
1679 codesource
= "<no code source>";
1681 // We use a somewhat bogus test for the ABI here.
1684 if (_Jv_IsInterpretedClass (klass
))
1689 else if (klass
->state
== JV_STATE_PRELOADING
)
1690 abi
= "BC-compiled";
1692 abi
= "pre-compiled";
1694 fprintf (stderr
, "[Loaded (%s) %s from %s]\n", abi
, klass
->name
->chars(),
1698 // FIXME: mention invariants and stuff.
1700 _Jv_Linker::wait_for_state (jclass klass
, int state
)
1702 if (klass
->state
>= state
)
1705 JvSynchronize
sync (klass
);
1707 // This is similar to the strategy for class initialization. If we
1708 // already hold the lock, just leave.
1709 java::lang::Thread
*self
= java::lang::Thread::currentThread();
1710 while (klass
->state
<= state
1712 && klass
->thread
!= self
)
1715 java::lang::Thread
*save
= klass
->thread
;
1716 klass
->thread
= self
;
1718 // Print some debugging info if requested. Interpreted classes are
1719 // handled in defineclass, so we only need to handle the two
1720 // pre-compiled cases here.
1721 if (gcj::verbose_class_flag
1722 && (klass
->state
== JV_STATE_COMPILED
1723 || klass
->state
== JV_STATE_PRELOADING
)
1725 && ! _Jv_IsInterpretedClass (klass
)
1728 print_class_loaded (klass
);
1732 if (state
>= JV_STATE_LOADING
&& klass
->state
< JV_STATE_LOADING
)
1734 ensure_supers_installed (klass
);
1735 klass
->set_state(JV_STATE_LOADING
);
1738 if (state
>= JV_STATE_LOADED
&& klass
->state
< JV_STATE_LOADED
)
1740 ensure_method_table_complete (klass
);
1741 klass
->set_state(JV_STATE_LOADED
);
1744 if (state
>= JV_STATE_PREPARED
&& klass
->state
< JV_STATE_PREPARED
)
1746 ensure_fields_laid_out (klass
);
1747 make_vtable (klass
);
1748 layout_interface_methods (klass
);
1749 prepare_constant_time_tables (klass
);
1750 klass
->set_state(JV_STATE_PREPARED
);
1753 if (state
>= JV_STATE_LINKED
&& klass
->state
< JV_STATE_LINKED
)
1755 verify_class (klass
);
1757 ensure_class_linked (klass
);
1758 link_exception_table (klass
);
1759 link_symbol_table (klass
);
1760 klass
->set_state(JV_STATE_LINKED
);
1763 catch (java::lang::Throwable
*exc
)
1765 klass
->thread
= save
;
1766 klass
->set_state(JV_STATE_ERROR
);
1770 klass
->thread
= save
;
1772 if (klass
->state
== JV_STATE_ERROR
)
1773 throw new java::lang::LinkageError
;