forked from gcc-mirror/gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.cc
2105 lines (1836 loc) · 62.4 KB
/
link.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// link.cc - Code for linking and resolving classes and pool entries.
/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
Free Software Foundation
This file is part of libgcj.
This software is copyrighted work licensed under the terms of the
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */
/* Author: Kresten Krab Thorup <krab@gnu.org> */
#include <config.h>
#include <platform.h>
#include <stdio.h>
#ifdef USE_LIBFFI
#include <ffi.h>
#endif
#include <java-interp.h>
// Set GC_DEBUG before including gc.h!
#ifdef LIBGCJ_GC_DEBUG
# define GC_DEBUG
#endif
#include <gc.h>
#include <jvm.h>
#include <gcj/cni.h>
#include <string.h>
#include <limits.h>
#include <java-cpool.h>
#include <execution.h>
#ifdef INTERPRETER
#include <jvmti.h>
#include "jvmti-int.h"
#endif
#include <java/lang/Class.h>
#include <java/lang/String.h>
#include <java/lang/StringBuffer.h>
#include <java/lang/Thread.h>
#include <java/lang/InternalError.h>
#include <java/lang/VirtualMachineError.h>
#include <java/lang/VerifyError.h>
#include <java/lang/NoSuchFieldError.h>
#include <java/lang/NoSuchMethodError.h>
#include <java/lang/ClassFormatError.h>
#include <java/lang/IllegalAccessError.h>
#include <java/lang/InternalError.h>
#include <java/lang/AbstractMethodError.h>
#include <java/lang/NoClassDefFoundError.h>
#include <java/lang/IncompatibleClassChangeError.h>
#include <java/lang/VerifyError.h>
#include <java/lang/VMClassLoader.h>
#include <java/lang/reflect/Modifier.h>
#include <java/security/CodeSource.h>
using namespace gcj;
template<typename T>
struct aligner
{
char c;
T field;
};
#define ALIGNOF(TYPE) (offsetof (aligner<TYPE>, field))
// This returns the alignment of a type as it would appear in a
// structure. This can be different from the alignment of the type
// itself. For instance on x86 double is 8-aligned but struct{double}
// is 4-aligned.
int
_Jv_Linker::get_alignment_from_class (jclass klass)
{
if (klass == JvPrimClass (byte))
return ALIGNOF (jbyte);
else if (klass == JvPrimClass (short))
return ALIGNOF (jshort);
else if (klass == JvPrimClass (int))
return ALIGNOF (jint);
else if (klass == JvPrimClass (long))
return ALIGNOF (jlong);
else if (klass == JvPrimClass (boolean))
return ALIGNOF (jboolean);
else if (klass == JvPrimClass (char))
return ALIGNOF (jchar);
else if (klass == JvPrimClass (float))
return ALIGNOF (jfloat);
else if (klass == JvPrimClass (double))
return ALIGNOF (jdouble);
else
return ALIGNOF (jobject);
}
void
_Jv_Linker::resolve_field (_Jv_Field *field, java::lang::ClassLoader *loader)
{
if (! field->isResolved ())
{
_Jv_Utf8Const *sig = (_Jv_Utf8Const *) field->type;
jclass type = _Jv_FindClassFromSignature (sig->chars(), loader);
if (type == NULL)
throw new java::lang::NoClassDefFoundError(field->name->toString());
field->type = type;
field->flags &= ~_Jv_FIELD_UNRESOLVED_FLAG;
}
}
// A helper for find_field that knows how to recursively search
// superclasses and interfaces.
_Jv_Field *
_Jv_Linker::find_field_helper (jclass search, _Jv_Utf8Const *name,
_Jv_Utf8Const *type_name, jclass type,
jclass *declarer)
{
while (search)
{
// From 5.4.3.2. First search class itself.
for (int i = 0; i < search->field_count; ++i)
{
_Jv_Field *field = &search->fields[i];
if (! _Jv_equalUtf8Consts (field->name, name))
continue;
// Checks for the odd situation where we were able to retrieve the
// field's class from signature but the resolution of the field itself
// failed which means a different class was resolved.
if (type != NULL)
{
try
{
resolve_field (field, search->loader);
}
catch (java::lang::Throwable *exc)
{
java::lang::LinkageError *le = new java::lang::LinkageError
(JvNewStringLatin1
("field type mismatch with different loaders"));
le->initCause(exc);
throw le;
}
}
// Note that we compare type names and not types. This is
// bizarre, but we do it because we want to find a field
// (and terminate the search) if it has the correct
// descriptor -- but then later reject it if the class
// loader check results in different classes. We can't just
// pass in the descriptor and check that way, because when
// the field is already resolved there is no easy way to
// find its descriptor again.
if ((field->isResolved ()
? _Jv_equalUtf8Classnames (type_name, field->type->name)
: _Jv_equalUtf8Classnames (type_name,
(_Jv_Utf8Const *) field->type)))
{
*declarer = search;
return field;
}
}
// Next search direct interfaces.
for (int i = 0; i < search->interface_count; ++i)
{
_Jv_Field *result = find_field_helper (search->interfaces[i], name,
type_name, type, declarer);
if (result)
return result;
}
// Now search superclass.
search = search->superclass;
}
return NULL;
}
bool
_Jv_Linker::has_field_p (jclass search, _Jv_Utf8Const *field_name)
{
for (int i = 0; i < search->field_count; ++i)
{
_Jv_Field *field = &search->fields[i];
if (_Jv_equalUtf8Consts (field->name, field_name))
return true;
}
return false;
}
// Find a field.
// KLASS is the class that is requesting the field.
// OWNER is the class in which the field should be found.
// FIELD_TYPE_NAME is the type descriptor for the field.
// Fill FOUND_CLASS with the address of the class in which the field
// is actually declared.
// This function does the class loader type checks, and
// also access checks. Returns the field, or throws an
// exception on error.
_Jv_Field *
_Jv_Linker::find_field (jclass klass, jclass owner,
jclass *found_class,
_Jv_Utf8Const *field_name,
_Jv_Utf8Const *field_type_name)
{
// FIXME: this allocates a _Jv_Utf8Const each time. We should make
// it cheaper.
// Note: This call will resolve the primitive type names ("Z", "B", ...) to
// their Java counterparts ("boolean", "byte", ...) if accessed via
// field_type->name later. Using these variants of the type name is in turn
// important for the find_field_helper function. However if the class
// resolution failed then we can only use the already given type name.
jclass field_type
= _Jv_FindClassFromSignatureNoException (field_type_name->chars(),
klass->loader);
_Jv_Field *the_field
= find_field_helper (owner, field_name,
(field_type
? field_type->name :
field_type_name ),
field_type, found_class);
if (the_field == 0)
{
java::lang::StringBuffer *sb = new java::lang::StringBuffer();
sb->append(JvNewStringLatin1("field "));
sb->append(owner->getName());
sb->append(JvNewStringLatin1("."));
sb->append(_Jv_NewStringUTF(field_name->chars()));
sb->append(JvNewStringLatin1(" was not found."));
throw new java::lang::NoSuchFieldError (sb->toString());
}
// Accept it when the field's class could not be resolved.
if (field_type == NULL)
// Silently ignore that we were not able to retrieve the type to make it
// possible to run code which does not access this field.
return the_field;
if (_Jv_CheckAccess (klass, *found_class, the_field->flags))
{
// Note that the field returned by find_field_helper is always
// resolved. However, we still use the constraint mechanism
// because this may affect other lookups.
_Jv_CheckOrCreateLoadingConstraint (field_type, (*found_class)->loader);
}
else
{
java::lang::StringBuffer *sb
= new java::lang::StringBuffer ();
sb->append(klass->getName());
sb->append(JvNewStringLatin1(": "));
sb->append((*found_class)->getName());
sb->append(JvNewStringLatin1("."));
sb->append(_Jv_NewStringUtf8Const (field_name));
throw new java::lang::IllegalAccessError(sb->toString());
}
return the_field;
}
// Check loading constraints for method.
void
_Jv_Linker::check_loading_constraints (_Jv_Method *method, jclass self_class,
jclass other_class)
{
JArray<jclass> *klass_args;
jclass klass_return;
_Jv_GetTypesFromSignature (method, self_class, &klass_args, &klass_return);
jclass *klass_arg = elements (klass_args);
java::lang::ClassLoader *found_loader = other_class->loader;
_Jv_CheckOrCreateLoadingConstraint (klass_return, found_loader);
for (int i = 0; i < klass_args->length; i++)
_Jv_CheckOrCreateLoadingConstraint (*(klass_arg++), found_loader);
}
_Jv_Method *
_Jv_Linker::resolve_method_entry (jclass klass, jclass &found_class,
int class_index, int name_and_type_index,
bool init, bool is_iface)
{
_Jv_Constants *pool = &klass->constants;
jclass owner = resolve_pool_entry (klass, class_index).clazz;
if (init && owner != klass)
_Jv_InitClass (owner);
_Jv_ushort name_index, type_index;
_Jv_loadIndexes (&pool->data[name_and_type_index],
name_index,
type_index);
_Jv_Utf8Const *method_name = pool->data[name_index].utf8;
_Jv_Utf8Const *method_signature = pool->data[type_index].utf8;
_Jv_Method *the_method = 0;
found_class = 0;
// We're going to cache a pointer to the _Jv_Method object
// when we find it. So, to ensure this doesn't get moved from
// beneath us, we first put all the needed Miranda methods
// into the target class.
wait_for_state (klass, JV_STATE_LOADED);
// First search the class itself.
the_method = search_method_in_class (owner, klass,
method_name, method_signature);
if (the_method != 0)
{
found_class = owner;
goto end_of_method_search;
}
// If we are resolving an interface method, search the
// interface's superinterfaces (A superinterface is not an
// interface's superclass - a superinterface is implemented by
// the interface).
if (is_iface)
{
_Jv_ifaces ifaces;
ifaces.count = 0;
ifaces.len = 4;
ifaces.list = (jclass *) _Jv_Malloc (ifaces.len
* sizeof (jclass *));
get_interfaces (owner, &ifaces);
for (int i = 0; i < ifaces.count; i++)
{
jclass cls = ifaces.list[i];
the_method = search_method_in_class (cls, klass, method_name,
method_signature);
if (the_method != 0)
{
found_class = cls;
break;
}
}
_Jv_Free (ifaces.list);
if (the_method != 0)
goto end_of_method_search;
}
// Finally, search superclasses.
the_method = (search_method_in_superclasses
(owner->getSuperclass (), klass, method_name,
method_signature, &found_class));
end_of_method_search:
if (the_method == 0)
{
java::lang::StringBuffer *sb = new java::lang::StringBuffer();
sb->append(JvNewStringLatin1("method "));
sb->append(owner->getName());
sb->append(JvNewStringLatin1("."));
sb->append(_Jv_NewStringUTF(method_name->chars()));
sb->append(JvNewStringLatin1(" with signature "));
sb->append(_Jv_NewStringUTF(method_signature->chars()));
sb->append(JvNewStringLatin1(" was not found."));
throw new java::lang::NoSuchMethodError (sb->toString());
}
// if (found_class->loader != klass->loader), then we must actually
// check that the types of arguments correspond. JVMS 5.4.3.3.
if (found_class->loader != klass->loader)
check_loading_constraints (the_method, klass, found_class);
return the_method;
}
_Jv_Mutex_t _Jv_Linker::resolve_mutex;
void
_Jv_Linker::init (void)
{
_Jv_MutexInit (&_Jv_Linker::resolve_mutex);
}
// Locking in resolve_pool_entry is somewhat subtle. Constant
// resolution is idempotent, so it doesn't matter if two threads
// resolve the same entry. However, it is important that we always
// write the resolved flag and the data together, atomically. It is
// also important that we read them atomically.
_Jv_word
_Jv_Linker::resolve_pool_entry (jclass klass, int index, bool lazy)
{
using namespace java::lang::reflect;
if (GC_base (klass) && klass->constants.data
&& ! GC_base (klass->constants.data))
// If a class is heap-allocated but the constant pool is not this
// is a "new ABI" class, i.e. one where the initial constant pool
// is in the read-only data section of an object file. Copy the
// initial constant pool from there to a new heap-allocated pool.
{
jsize count = klass->constants.size;
if (count)
{
_Jv_word* constants
= (_Jv_word*) _Jv_AllocRawObj (count * sizeof (_Jv_word));
memcpy ((void*)constants,
(void*)klass->constants.data,
count * sizeof (_Jv_word));
klass->constants.data = constants;
}
}
_Jv_Constants *pool = &klass->constants;
jbyte tags;
_Jv_word data;
tags = read_cpool_entry (&data, pool, index);
if ((tags & JV_CONSTANT_ResolvedFlag) != 0)
return data;
switch (tags & ~JV_CONSTANT_LazyFlag)
{
case JV_CONSTANT_Class:
{
_Jv_Utf8Const *name = data.utf8;
jclass found;
if (name->first() == '[')
found = _Jv_FindClassFromSignatureNoException (name->chars(),
klass->loader);
else
found = _Jv_FindClassNoException (name, klass->loader);
// If the class could not be loaded a phantom class is created. Any
// function that deals with such a class but cannot do something useful
// with it should just throw a NoClassDefFoundError with the class'
// name.
if (! found)
{
if (lazy)
{
found = _Jv_NewClass(name, NULL, NULL);
found->state = JV_STATE_PHANTOM;
tags |= JV_CONSTANT_ResolvedFlag;
data.clazz = found;
break;
}
else
throw new java::lang::NoClassDefFoundError (name->toString());
}
// Check accessibility, but first strip array types as
// _Jv_ClassNameSamePackage can't handle arrays.
jclass check;
for (check = found;
check && check->isArray();
check = check->getComponentType())
;
if ((found->accflags & Modifier::PUBLIC) == Modifier::PUBLIC
|| (_Jv_ClassNameSamePackage (check->name,
klass->name)))
{
data.clazz = found;
tags |= JV_CONSTANT_ResolvedFlag;
}
else
{
java::lang::StringBuffer *sb = new java::lang::StringBuffer ();
sb->append(klass->getName());
sb->append(JvNewStringLatin1(" can't access class "));
sb->append(found->getName());
throw new java::lang::IllegalAccessError(sb->toString());
}
}
break;
case JV_CONSTANT_String:
{
jstring str;
str = _Jv_NewStringUtf8Const (data.utf8);
data.o = str;
tags |= JV_CONSTANT_ResolvedFlag;
}
break;
case JV_CONSTANT_Fieldref:
{
_Jv_ushort class_index, name_and_type_index;
_Jv_loadIndexes (&data,
class_index,
name_and_type_index);
jclass owner = (resolve_pool_entry (klass, class_index, true)).clazz;
// If a phantom class was resolved our field reference is
// unusable because of the missing class.
if (owner->state == JV_STATE_PHANTOM)
throw new java::lang::NoClassDefFoundError(owner->getName());
// We don't initialize 'owner', but we do make sure that its
// fields exist.
wait_for_state (owner, JV_STATE_PREPARED);
_Jv_ushort name_index, type_index;
_Jv_loadIndexes (&pool->data[name_and_type_index],
name_index,
type_index);
_Jv_Utf8Const *field_name = pool->data[name_index].utf8;
_Jv_Utf8Const *field_type_name = pool->data[type_index].utf8;
jclass found_class = 0;
_Jv_Field *the_field = find_field (klass, owner,
&found_class,
field_name,
field_type_name);
// Initialize the field's declaring class, not its qualifying
// class.
_Jv_InitClass (found_class);
data.field = the_field;
tags |= JV_CONSTANT_ResolvedFlag;
}
break;
case JV_CONSTANT_Methodref:
case JV_CONSTANT_InterfaceMethodref:
{
_Jv_ushort class_index, name_and_type_index;
_Jv_loadIndexes (&data,
class_index,
name_and_type_index);
_Jv_Method *the_method;
jclass found_class;
the_method = resolve_method_entry (klass, found_class,
class_index, name_and_type_index,
true,
tags == JV_CONSTANT_InterfaceMethodref);
data.rmethod
= klass->engine->resolve_method(the_method,
found_class,
((the_method->accflags
& Modifier::STATIC) != 0));
tags |= JV_CONSTANT_ResolvedFlag;
}
break;
}
write_cpool_entry (data, tags, pool, index);
return data;
}
// This function is used to lazily locate superclasses and
// superinterfaces. This must be called with the class lock held.
void
_Jv_Linker::resolve_class_ref (jclass klass, jclass *classref)
{
jclass ret = *classref;
// If superclass looks like a constant pool entry, resolve it now.
if (ret && (uaddr) ret < (uaddr) klass->constants.size)
{
if (klass->state < JV_STATE_LINKED)
{
_Jv_Utf8Const *name = klass->constants.data[(uaddr) *classref].utf8;
ret = _Jv_FindClass (name, klass->loader);
if (! ret)
{
throw new java::lang::NoClassDefFoundError (name->toString());
}
}
else
ret = klass->constants.data[(uaddr) classref].clazz;
*classref = ret;
}
}
// Find a method declared in the cls that is referenced from klass and
// perform access checks if CHECK_PERMS is true.
_Jv_Method *
_Jv_Linker::search_method_in_class (jclass cls, jclass klass,
_Jv_Utf8Const *method_name,
_Jv_Utf8Const *method_signature,
bool check_perms)
{
using namespace java::lang::reflect;
for (int i = 0; i < cls->method_count; i++)
{
_Jv_Method *method = &cls->methods[i];
if ( (!_Jv_equalUtf8Consts (method->name,
method_name))
|| (!_Jv_equalUtf8Consts (method->signature,
method_signature)))
continue;
if (!check_perms || _Jv_CheckAccess (klass, cls, method->accflags))
return method;
else
{
java::lang::StringBuffer *sb = new java::lang::StringBuffer();
sb->append(klass->getName());
sb->append(JvNewStringLatin1(": "));
sb->append(cls->getName());
sb->append(JvNewStringLatin1("."));
sb->append(_Jv_NewStringUTF(method_name->chars()));
sb->append(_Jv_NewStringUTF(method_signature->chars()));
throw new java::lang::IllegalAccessError (sb->toString());
}
}
return 0;
}
// Like search_method_in_class, but work our way up the superclass
// chain.
_Jv_Method *
_Jv_Linker::search_method_in_superclasses (jclass cls, jclass klass,
_Jv_Utf8Const *method_name,
_Jv_Utf8Const *method_signature,
jclass *found_class, bool check_perms)
{
_Jv_Method *the_method = NULL;
for ( ; cls != 0; cls = cls->getSuperclass ())
{
the_method = search_method_in_class (cls, klass, method_name,
method_signature, check_perms);
if (the_method != 0)
{
if (found_class)
*found_class = cls;
break;
}
}
return the_method;
}
#define INITIAL_IOFFSETS_LEN 4
#define INITIAL_IFACES_LEN 4
static _Jv_IDispatchTable null_idt = {SHRT_MAX, 0, {}};
// Generate tables for constant-time assignment testing and interface
// method lookup. This implements the technique described by Per Bothner
// <per@bothner.com> on the java-discuss mailing list on 1999-09-02:
// http://gcc.gnu.org/ml/java/1999-q3/msg00377.html
void
_Jv_Linker::prepare_constant_time_tables (jclass klass)
{
if (klass->isPrimitive () || klass->isInterface ())
return;
// Short-circuit in case we've been called already.
if ((klass->idt != NULL) || klass->depth != 0)
return;
// Calculate the class depth and ancestor table. The depth of a class
// is how many "extends" it is removed from Object. Thus the depth of
// java.lang.Object is 0, but the depth of java.io.FilterOutputStream
// is 2. Depth is defined for all regular and array classes, but not
// interfaces or primitive types.
jclass klass0 = klass;
jboolean has_interfaces = false;
while (klass0 != &java::lang::Object::class$)
{
if (klass0->interface_count)
has_interfaces = true;
klass0 = klass0->superclass;
klass->depth++;
}
// We do class member testing in constant time by using a small table
// of all the ancestor classes within each class. The first element is
// a pointer to the current class, and the rest are pointers to the
// classes ancestors, ordered from the current class down by decreasing
// depth. We do not include java.lang.Object in the table of ancestors,
// since it is redundant. Note that the classes pointed to by
// 'ancestors' will always be reachable by other paths.
klass->ancestors = (jclass *) _Jv_AllocBytes (klass->depth
* sizeof (jclass));
klass0 = klass;
for (int index = 0; index < klass->depth; index++)
{
klass->ancestors[index] = klass0;
klass0 = klass0->superclass;
}
if ((klass->accflags & java::lang::reflect::Modifier::ABSTRACT) != 0)
return;
// Optimization: If class implements no interfaces, use a common
// predefined interface table.
if (!has_interfaces)
{
klass->idt = &null_idt;
return;
}
_Jv_ifaces ifaces;
ifaces.count = 0;
ifaces.len = INITIAL_IFACES_LEN;
ifaces.list = (jclass *) _Jv_Malloc (ifaces.len * sizeof (jclass *));
int itable_size = get_interfaces (klass, &ifaces);
if (ifaces.count > 0)
{
// The classes pointed to by the itable will always be reachable
// via other paths.
int idt_bytes = sizeof (_Jv_IDispatchTable) + (itable_size
* sizeof (void *));
klass->idt = (_Jv_IDispatchTable *) _Jv_AllocBytes (idt_bytes);
klass->idt->itable_length = itable_size;
jshort *itable_offsets =
(jshort *) _Jv_Malloc (ifaces.count * sizeof (jshort));
generate_itable (klass, &ifaces, itable_offsets);
jshort cls_iindex = find_iindex (ifaces.list, itable_offsets,
ifaces.count);
for (int i = 0; i < ifaces.count; i++)
{
ifaces.list[i]->ioffsets[cls_iindex] = itable_offsets[i];
}
klass->idt->iindex = cls_iindex;
_Jv_Free (ifaces.list);
_Jv_Free (itable_offsets);
}
else
{
klass->idt->iindex = SHRT_MAX;
}
}
// Return index of item in list, or -1 if item is not present.
inline jshort
_Jv_Linker::indexof (void *item, void **list, jshort list_len)
{
for (int i=0; i < list_len; i++)
{
if (list[i] == item)
return i;
}
return -1;
}
// Find all unique interfaces directly or indirectly implemented by klass.
// Returns the size of the interface dispatch table (itable) for klass, which
// is the number of unique interfaces plus the total number of methods that
// those interfaces declare. May extend ifaces if required.
jshort
_Jv_Linker::get_interfaces (jclass klass, _Jv_ifaces *ifaces)
{
jshort result = 0;
for (int i = 0; i < klass->interface_count; i++)
{
jclass iface = klass->interfaces[i];
/* Make sure interface is linked. */
wait_for_state(iface, JV_STATE_LINKED);
if (indexof (iface, (void **) ifaces->list, ifaces->count) == -1)
{
if (ifaces->count + 1 >= ifaces->len)
{
/* Resize ifaces list */
ifaces->len = ifaces->len * 2;
ifaces->list
= (jclass *) _Jv_Realloc (ifaces->list,
ifaces->len * sizeof(jclass));
}
ifaces->list[ifaces->count] = iface;
ifaces->count++;
result += get_interfaces (klass->interfaces[i], ifaces);
}
}
if (klass->isInterface())
{
// We want to add 1 plus the number of interface methods here.
// But, we take special care to skip <clinit>.
++result;
for (int i = 0; i < klass->method_count; ++i)
{
if (klass->methods[i].name->first() != '<')
++result;
}
}
else if (klass->superclass)
result += get_interfaces (klass->superclass, ifaces);
return result;
}
// Fill out itable in klass, resolving method declarations in each ifaces.
// itable_offsets is filled out with the position of each iface in itable,
// such that itable[itable_offsets[n]] == ifaces.list[n].
void
_Jv_Linker::generate_itable (jclass klass, _Jv_ifaces *ifaces,
jshort *itable_offsets)
{
void **itable = klass->idt->itable;
jshort itable_pos = 0;
for (int i = 0; i < ifaces->count; i++)
{
jclass iface = ifaces->list[i];
itable_offsets[i] = itable_pos;
itable_pos = append_partial_itable (klass, iface, itable, itable_pos);
/* Create ioffsets table for iface */
if (iface->ioffsets == NULL)
{
// The first element of ioffsets is its length (itself included).
jshort *ioffsets = (jshort *) _Jv_AllocBytes (INITIAL_IOFFSETS_LEN
* sizeof (jshort));
ioffsets[0] = INITIAL_IOFFSETS_LEN;
for (int i = 1; i < INITIAL_IOFFSETS_LEN; i++)
ioffsets[i] = -1;
iface->ioffsets = ioffsets;
}
}
}
// Format method name for use in error messages.
jstring
_Jv_GetMethodString (jclass klass, _Jv_Method *meth,
jclass derived)
{
using namespace java::lang;
StringBuffer *buf = new StringBuffer (klass->name->toString());
buf->append (jchar ('.'));
buf->append (meth->name->toString());
buf->append ((jchar) ' ');
buf->append (meth->signature->toString());
if (derived)
{
buf->append(JvNewStringLatin1(" in "));
buf->append(derived->name->toString());
}
return buf->toString();
}
void
_Jv_ThrowNoSuchMethodError ()
{
throw new java::lang::NoSuchMethodError;
}
#if defined USE_LIBFFI && FFI_CLOSURES && defined(INTERPRETER)
// A function whose invocation is prepared using libffi. It gets called
// whenever a static method of a missing class is invoked. The data argument
// holds a reference to a String denoting the missing class.
// The prepared function call is stored in a class' atable.
void
_Jv_ThrowNoClassDefFoundErrorTrampoline(ffi_cif *,
void *,
void **,
void *data)
{
throw new java::lang::NoClassDefFoundError(
_Jv_NewStringUtf8Const((_Jv_Utf8Const *) data));
}
#else
// A variant of the NoClassDefFoundError throwing method that can
// be used without libffi.
void
_Jv_ThrowNoClassDefFoundError()
{
throw new java::lang::NoClassDefFoundError();
}
#endif
// Throw a NoSuchFieldError. Called by compiler-generated code when
// an otable entry is zero. OTABLE_INDEX is the index in the caller's
// otable that refers to the missing field. This index may be used to
// print diagnostic information about the field.
void
_Jv_ThrowNoSuchFieldError (int /* otable_index */)
{
throw new java::lang::NoSuchFieldError;
}
// This is put in empty vtable slots.
void
_Jv_ThrowAbstractMethodError ()
{
throw new java::lang::AbstractMethodError();
}
// Each superinterface of a class (i.e. each interface that the class
// directly or indirectly implements) has a corresponding "Partial
// Interface Dispatch Table" whose size is (number of methods + 1) words.
// The first word is a pointer to the interface (i.e. the java.lang.Class
// instance for that interface). The remaining words are pointers to the
// actual methods that implement the methods declared in the interface,
// in order of declaration.
//
// Append partial interface dispatch table for "iface" to "itable", at
// position itable_pos.
// Returns the offset at which the next partial ITable should be appended.
jshort
_Jv_Linker::append_partial_itable (jclass klass, jclass iface,
void **itable, jshort pos)
{
using namespace java::lang::reflect;
itable[pos++] = (void *) iface;
_Jv_Method *meth;
for (int j=0; j < iface->method_count; j++)
{
// Skip '<clinit>' here.
if (iface->methods[j].name->first() == '<')
continue;
meth = NULL;
jclass cl;
for (cl = klass; cl; cl = cl->getSuperclass())
{
meth = _Jv_GetMethodLocal (cl, iface->methods[j].name,
iface->methods[j].signature);
if (meth)
break;
}
if (meth)
{
if ((meth->accflags & Modifier::STATIC) != 0)
throw new java::lang::IncompatibleClassChangeError
(_Jv_GetMethodString (klass, meth));
if ((meth->accflags & Modifier::PUBLIC) == 0)
throw new java::lang::IllegalAccessError
(_Jv_GetMethodString (klass, meth));
if ((meth->accflags & Modifier::ABSTRACT) != 0)
itable[pos] = (void *) &_Jv_ThrowAbstractMethodError;
else
itable[pos] = meth->ncode;
if (cl->loader != iface->loader)
check_loading_constraints (meth, cl, iface);
}
else
{
// The method doesn't exist in klass. Binary compatibility rules
// permit this, so we delay the error until runtime using a pointer
// to a method which throws an exception.
itable[pos] = (void *) _Jv_ThrowNoSuchMethodError;
}
pos++;
}
return pos;
}
static _Jv_Mutex_t iindex_mutex;
static bool iindex_mutex_initialized = false;
// We need to find the correct offset in the Class Interface Dispatch
// Table for a given interface. Once we have that, invoking an interface
// method just requires combining the Method's index in the interface
// (known at compile time) to get the correct method. Doing a type test
// (cast or instanceof) is the same problem: Once we have a possible Partial
// Interface Dispatch Table, we just compare the first element to see if it
// matches the desired interface. So how can we find the correct offset?
// Our solution is to keep a vector of candiate offsets in each interface
// (ioffsets), and in each class we have an index (idt->iindex) used to
// select the correct offset from ioffsets.
//
// Calculate and return iindex for a new class.
// ifaces is a vector of num interfaces that the class implements.
// offsets[j] is the offset in the interface dispatch table for the
// interface corresponding to ifaces[j].
// May extend the interface ioffsets if required.
jshort
_Jv_Linker::find_iindex (jclass *ifaces, jshort *offsets, jshort num)
{
int i;
int j;