forked from gcc-mirror/gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefineclass.cc
1730 lines (1409 loc) · 45.1 KB
/
defineclass.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
// defineclass.cc - defining a class from .class format.
/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 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 <[email protected]>
Written using the online versions of Java Language Specification (1st
ed.) and The Java Virtual Machine Specification (2nd ed.).
Future work may include reading (and handling) attributes which are
currently being ignored ("InnerClasses", "LineNumber", etc...).
*/
#include <config.h>
#include <java-interp.h>
#include <stdlib.h>
#include <stdio.h>
#include <java-cpool.h>
#include <gcj/cni.h>
#include <execution.h>
#include <java/lang/Class.h>
#include <java/lang/Float.h>
#include <java/lang/Double.h>
#include <java/lang/Character.h>
#include <java/lang/LinkageError.h>
#include <java/lang/InternalError.h>
#include <java/lang/ClassFormatError.h>
#include <java/lang/NoClassDefFoundError.h>
#include <java/lang/ClassCircularityError.h>
#include <java/lang/IncompatibleClassChangeError.h>
#include <java/lang/reflect/Modifier.h>
#include <java/security/ProtectionDomain.h>
using namespace gcj;
#ifdef INTERPRETER
// these go in some separate functions, to avoid having _Jv_InitClass
// inserted all over the place.
static void throw_internal_error (const char *msg)
__attribute__ ((__noreturn__));
static void throw_no_class_def_found_error (jstring msg)
__attribute__ ((__noreturn__));
static void throw_no_class_def_found_error (const char *msg)
__attribute__ ((__noreturn__));
static void throw_class_format_error (jstring msg)
__attribute__ ((__noreturn__));
static void throw_incompatible_class_change_error (jstring msg)
__attribute__ ((__noreturn__));
static void throw_class_circularity_error (jstring msg)
__attribute__ ((__noreturn__));
/**
* We define class reading using a class. It is practical, since then
* the entire class-reader can be a friend of class Class (it needs to
* write all it's different structures); but also because this makes it
* easy to make class definition reentrant, and thus two threads can be
* defining classes at the same time. This class (_Jv_ClassReader) is
* never exposed outside this file, so we don't have to worry about
* public or private members here.
*/
struct _Jv_ClassReader
{
// do verification? Currently, there is no option to disable this.
// This flag just controls the verificaiton done by the class loader;
// i.e., checking the integrity of the constant pool; and it is
// allways on. You always want this as far as I can see, but it also
// controls weither identifiers and type descriptors/signatures are
// verified as legal. This could be somewhat more expensive since it
// will call Character.isJavaIdentifier{Start,Part} for each character
// in any identifier (field name or method name) it comes by. Thus,
// it might be useful to turn off this verification for classes that
// come from a trusted source. However, for GCJ, trusted classes are
// most likely to be linked in.
bool verify;
// input data.
unsigned char *bytes;
int len;
// current input position
int pos;
// the constant pool data
int pool_count;
unsigned char *tags;
unsigned int *offsets;
// the class to define (see java-interp.h)
jclass def;
// the classes associated interpreter data.
_Jv_InterpClass *def_interp;
// The name we found.
_Jv_Utf8Const **found_name;
// True if this is a 1.5 class file.
bool is_15;
/* check that the given number of input bytes are available */
inline void check (int num)
{
if (pos + num > len)
throw_class_format_error ("Premature end of data");
}
/* skip a given number of bytes in input */
inline void skip (int num)
{
check (num);
pos += num;
}
/* read an unsignend 1-byte unit */
inline static jint get1u (unsigned char* bytes)
{
return bytes[0];
}
/* read an unsigned 1-byte unit */
inline jint read1u ()
{
skip (1);
return get1u (bytes+pos-1);
}
/* read an unsigned 2-byte unit */
inline static jint get2u (unsigned char *bytes)
{
return (((jint)bytes[0]) << 8) | ((jint)bytes[1]);
}
/* read an unsigned 2-byte unit */
inline jint read2u ()
{
skip (2);
return get2u (bytes+pos-2);
}
/* read a 4-byte unit */
static jint get4 (unsigned char *bytes)
{
return (((jint)bytes[0]) << 24)
| (((jint)bytes[1]) << 16)
| (((jint)bytes[2]) << 8)
| (((jint)bytes[3]) << 0);
}
/* read a 4-byte unit, (we don't do that quite so often) */
inline jint read4 ()
{
skip (4);
return get4 (bytes+pos-4);
}
/* read a 8-byte unit */
static jlong get8 (unsigned char* bytes)
{
return (((jlong)bytes[0]) << 56)
| (((jlong)bytes[1]) << 48)
| (((jlong)bytes[2]) << 40)
| (((jlong)bytes[3]) << 32)
| (((jlong)bytes[4]) << 24)
| (((jlong)bytes[5]) << 16)
| (((jlong)bytes[6]) << 8)
| (((jlong)bytes[7]) << 0);
}
/* read a 8-byte unit */
inline jlong read8 ()
{
skip (8);
return get8 (bytes+pos-8);
}
inline void check_tag (int index, char expected_tag)
{
if (index < 0
|| index > pool_count
|| tags[index] != expected_tag)
throw_class_format_error ("erroneous constant pool tag");
}
inline void verify_identifier (_Jv_Utf8Const* name)
{
if (! _Jv_VerifyIdentifier (name))
throw_class_format_error ("erroneous identifier");
}
inline void verify_classname (unsigned char* ptr, _Jv_ushort length)
{
if (! _Jv_VerifyClassName (ptr, length))
throw_class_format_error ("erroneous class name");
}
inline void verify_classname (_Jv_Utf8Const *name)
{
if (! _Jv_VerifyClassName (name))
throw_class_format_error ("erroneous class name");
}
inline void verify_field_signature (_Jv_Utf8Const *sig)
{
if (! _Jv_VerifyFieldSignature (sig))
throw_class_format_error ("erroneous type descriptor");
}
inline void verify_method_signature (_Jv_Utf8Const *sig)
{
if (! _Jv_VerifyMethodSignature (sig))
throw_class_format_error ("erroneous type descriptor");
}
_Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length,
java::security::ProtectionDomain *pd,
_Jv_Utf8Const **name_result)
{
if (klass == 0 || length < 0 || offset+length > data->length)
throw_internal_error ("arguments to _Jv_DefineClass");
verify = true;
bytes = (unsigned char*) (elements (data)+offset);
len = length;
pos = 0;
is_15 = false;
def = klass;
found_name = name_result;
def->size_in_bytes = -1;
def->vtable_method_count = -1;
def->engine = &_Jv_soleInterpreterEngine;
def->protectionDomain = pd;
}
/** and here goes the parser members defined out-of-line */
void parse ();
void read_constpool ();
void prepare_pool_entry (int index, unsigned char tag);
void read_fields ();
void read_methods ();
void read_one_class_attribute ();
void read_one_method_attribute (int method);
void read_one_code_attribute (int method);
void read_one_field_attribute (int field);
void throw_class_format_error (const char *msg);
/** check an utf8 entry, without creating a Utf8Const object */
bool is_attribute_name (int index, const char *name);
/** here goes the class-loader members defined out-of-line */
void handleConstantPool ();
void handleClassBegin (int, int, int);
void handleInterfacesBegin (int);
void handleInterface (int, int);
void handleFieldsBegin (int);
void handleField (int, int, int, int);
void handleFieldsEnd ();
void handleConstantValueAttribute (int,int);
void handleMethodsBegin (int);
void handleMethod (int, int, int, int);
void handleMethodsEnd ();
void handleCodeAttribute (int, int, int, int, int, int);
void handleExceptionTableEntry (int, int, int, int, int, int);
void checkExtends (jclass sub, jclass super);
void checkImplements (jclass sub, jclass super);
/*
* FIXME: we should keep a hash table of utf8-strings, since many will
* be the same. It's a little tricky, however, because the hash table
* needs to interact gracefully with the garbage collector. Much
* memory is to be saved by this, however! perhaps the improvement
* could be implemented in prims.cc (_Jv_makeUtf8Const), since it
* computes the hash value anyway.
*/
};
// Note that *NAME_RESULT will only be set if the class is registered
// with the class loader. This is how the caller can know whether
// unregistration is require.
void
_Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length,
java::security::ProtectionDomain *pd,
_Jv_Utf8Const **name_result)
{
_Jv_ClassReader reader (klass, data, offset, length, pd, name_result);
reader.parse();
/* that's it! */
}
/** This section defines the parsing/scanning of the class data */
// Major and minor version numbers for various releases.
#define MAJOR_1_1 45
#define MINOR_1_1 3
#define MAJOR_1_2 46
#define MINOR_1_2 0
#define MAJOR_1_3 47
#define MINOR_1_3 0
#define MAJOR_1_4 48
#define MINOR_1_4 0
#define MAJOR_1_5 49
#define MINOR_1_5 0
void
_Jv_ClassReader::parse ()
{
int magic = read4 ();
if (magic != (int) 0xCAFEBABE)
throw_class_format_error ("bad magic number");
int minor_version = read2u ();
int major_version = read2u ();
if (major_version < MAJOR_1_1 || major_version > MAJOR_1_5
|| (major_version == MAJOR_1_5 && minor_version > MINOR_1_5))
throw_class_format_error ("unrecognized class file version");
is_15 = (major_version == MAJOR_1_5);
pool_count = read2u ();
read_constpool ();
int access_flags = read2u ();
int this_class = read2u ();
int super_class = read2u ();
check_tag (this_class, JV_CONSTANT_Class);
if (super_class != 0)
check_tag (super_class, JV_CONSTANT_Class);
handleClassBegin (access_flags, this_class, super_class);
// Allocate our aux_info here, after the name is set, to fulfill our
// contract with the collector interface.
def->aux_info = (void *) _Jv_AllocRawObj (sizeof (_Jv_InterpClass));
def_interp = (_Jv_InterpClass *) def->aux_info;
int interfaces_count = read2u ();
handleInterfacesBegin (interfaces_count);
for (int i = 0; i < interfaces_count; i++)
{
int iface = read2u ();
check_tag (iface, JV_CONSTANT_Class);
handleInterface (i, iface);
}
read_fields ();
read_methods ();
int attributes_count = read2u ();
for (int i = 0; i < attributes_count; i++)
{
read_one_class_attribute ();
}
if (pos != len)
throw_class_format_error ("unused data before end of file");
// Tell everyone we're done.
def->state = JV_STATE_READ;
if (gcj::verbose_class_flag)
_Jv_Linker::print_class_loaded (def);
def->notifyAll ();
}
void _Jv_ClassReader::read_constpool ()
{
tags = (unsigned char*) _Jv_AllocBytes (pool_count);
offsets = (unsigned int *) _Jv_AllocBytes (sizeof (int) * pool_count) ;
/** first, we scan the constant pool, collecting tags and offsets */
tags[0] = JV_CONSTANT_Undefined;
offsets[0] = pos;
for (int c = 1; c < pool_count; c++)
{
tags[c] = read1u ();
offsets[c] = pos;
switch (tags[c])
{
case JV_CONSTANT_String:
case JV_CONSTANT_Class:
skip (2);
break;
case JV_CONSTANT_Fieldref:
case JV_CONSTANT_Methodref:
case JV_CONSTANT_InterfaceMethodref:
case JV_CONSTANT_NameAndType:
case JV_CONSTANT_Integer:
case JV_CONSTANT_Float:
skip (4);
break;
case JV_CONSTANT_Double:
case JV_CONSTANT_Long:
skip (8);
tags[++c] = JV_CONSTANT_Undefined;
break;
case JV_CONSTANT_Utf8:
{
int len = read2u ();
skip (len);
}
break;
case JV_CONSTANT_Unicode:
throw_class_format_error ("unicode not supported");
break;
default:
throw_class_format_error ("erroneous constant pool tag");
}
}
handleConstantPool ();
}
void _Jv_ClassReader::read_fields ()
{
int fields_count = read2u ();
handleFieldsBegin (fields_count);
for (int i = 0; i < fields_count; i++)
{
int access_flags = read2u ();
int name_index = read2u ();
int descriptor_index = read2u ();
int attributes_count = read2u ();
check_tag (name_index, JV_CONSTANT_Utf8);
prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
check_tag (descriptor_index, JV_CONSTANT_Utf8);
prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
handleField (i, access_flags, name_index, descriptor_index);
for (int j = 0; j < attributes_count; j++)
{
read_one_field_attribute (i);
}
}
handleFieldsEnd ();
}
bool
_Jv_ClassReader::is_attribute_name (int index, const char *name)
{
check_tag (index, JV_CONSTANT_Utf8);
int len = get2u (bytes+offsets[index]);
if (len != (int) strlen (name))
return false;
else
return !memcmp (bytes+offsets[index]+2, name, len);
}
void _Jv_ClassReader::read_one_field_attribute (int field_index)
{
int name = read2u ();
int length = read4 ();
if (is_attribute_name (name, "ConstantValue"))
{
int cv = read2u ();
if (cv < pool_count
&& cv > 0
&& (tags[cv] == JV_CONSTANT_Integer
|| tags[cv] == JV_CONSTANT_Float
|| tags[cv] == JV_CONSTANT_Long
|| tags[cv] == JV_CONSTANT_Double
|| tags[cv] == JV_CONSTANT_String))
{
handleConstantValueAttribute (field_index, cv);
}
else
{
throw_class_format_error ("erroneous ConstantValue attribute");
}
if (length != 2)
throw_class_format_error ("erroneous ConstantValue attribute");
}
else
{
skip (length);
}
}
void _Jv_ClassReader::read_methods ()
{
int methods_count = read2u ();
handleMethodsBegin (methods_count);
for (int i = 0; i < methods_count; i++)
{
int access_flags = read2u ();
int name_index = read2u ();
int descriptor_index = read2u ();
int attributes_count = read2u ();
check_tag (name_index, JV_CONSTANT_Utf8);
prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
check_tag (descriptor_index, JV_CONSTANT_Utf8);
prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
handleMethod (i, access_flags, name_index,
descriptor_index);
for (int j = 0; j < attributes_count; j++)
{
read_one_method_attribute (i);
}
}
handleMethodsEnd ();
}
void _Jv_ClassReader::read_one_method_attribute (int method_index)
{
int name = read2u ();
int length = read4 ();
if (is_attribute_name (name, "Exceptions"))
{
_Jv_Method *method = reinterpret_cast<_Jv_Method *>
(&def->methods[method_index]);
if (method->throws != NULL)
throw_class_format_error ("only one Exceptions attribute allowed per method");
int num_exceptions = read2u ();
_Jv_Utf8Const **exceptions =
(_Jv_Utf8Const **) _Jv_AllocBytes ((num_exceptions + 1)
* sizeof (_Jv_Utf8Const *));
int out = 0;
_Jv_word *pool_data = def->constants.data;
for (int i = 0; i < num_exceptions; ++i)
{
int ndx = read2u ();
// JLS 2nd Ed. 4.7.5 requires that the tag not be 0.
if (ndx != 0)
{
check_tag (ndx, JV_CONSTANT_Class);
exceptions[out++] = pool_data[ndx].utf8;
}
}
exceptions[out] = NULL;
method->throws = exceptions;
}
else if (is_attribute_name (name, "Code"))
{
int start_off = pos;
int max_stack = read2u ();
int max_locals = read2u ();
int code_length = read4 ();
int code_start = pos;
skip (code_length);
int exception_table_length = read2u ();
handleCodeAttribute (method_index,
max_stack, max_locals,
code_start, code_length,
exception_table_length);
for (int i = 0; i < exception_table_length; i++)
{
int start_pc = read2u ();
int end_pc = read2u ();
int handler_pc = read2u ();
int catch_type = read2u ();
if (start_pc > end_pc
|| start_pc < 0
// END_PC can be equal to CODE_LENGTH.
// See JVM Spec 4.7.4.
|| end_pc > code_length
|| handler_pc >= code_length)
throw_class_format_error ("erroneous exception handler info");
if (! (tags[catch_type] == JV_CONSTANT_Class
|| tags[catch_type] == 0))
{
throw_class_format_error ("erroneous exception handler info");
}
handleExceptionTableEntry (method_index,
i,
start_pc,
end_pc,
handler_pc,
catch_type);
}
int attributes_count = read2u ();
for (int i = 0; i < attributes_count; i++)
{
read_one_code_attribute (method_index);
}
if ((pos - start_off) != length)
throw_class_format_error ("code attribute too short");
}
else
{
/* ignore unknown attributes */
skip (length);
}
}
void _Jv_ClassReader::read_one_code_attribute (int method_index)
{
int name = read2u ();
int length = read4 ();
if (is_attribute_name (name, "LineNumberTable"))
{
_Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
(def_interp->interpreted_methods[method_index]);
if (method->line_table != NULL)
throw_class_format_error ("Method already has LineNumberTable");
int table_len = read2u ();
_Jv_LineTableEntry* table
= (_Jv_LineTableEntry *) _Jv_AllocBytes (table_len
* sizeof (_Jv_LineTableEntry));
for (int i = 0; i < table_len; i++)
{
table[i].bytecode_pc = read2u ();
table[i].line = read2u ();
}
method->line_table_len = table_len;
method->line_table = table;
}
else
{
/* ignore unknown code attributes */
skip (length);
}
}
void _Jv_ClassReader::read_one_class_attribute ()
{
int name = read2u ();
int length = read4 ();
if (is_attribute_name (name, "SourceFile"))
{
int source_index = read2u ();
check_tag (source_index, JV_CONSTANT_Utf8);
prepare_pool_entry (source_index, JV_CONSTANT_Utf8);
def_interp->source_file_name = _Jv_NewStringUtf8Const
(def->constants.data[source_index].utf8);
}
else
{
/* Currently, we ignore most class attributes.
FIXME: Add inner-classes attributes support. */
skip (length);
}
}
/* this section defines the semantic actions of the parser */
void _Jv_ClassReader::handleConstantPool ()
{
/** now, we actually define the class' constant pool */
jbyte *pool_tags = (jbyte*) _Jv_AllocBytes (pool_count);
_Jv_word *pool_data
= (_Jv_word*) _Jv_AllocRawObj (pool_count * sizeof (_Jv_word));
def->constants.tags = pool_tags;
def->constants.data = pool_data;
def->constants.size = pool_count;
// Here we make a pass to collect the strings! We do this, because
// internally in the GCJ runtime, classes are encoded with .'s not /'s.
// Therefore, we first collect the strings, and then translate the rest
// of the utf8-entries (thus not representing strings) from /-notation
// to .-notation.
for (int i = 1; i < pool_count; i++)
{
if (tags[i] == JV_CONSTANT_String)
{
unsigned char* str_data = bytes + offsets [i];
int utf_index = get2u (str_data);
check_tag (utf_index, JV_CONSTANT_Utf8);
unsigned char *utf_data = bytes + offsets[utf_index];
int len = get2u (utf_data);
pool_data[i].utf8 = _Jv_makeUtf8Const ((char*)(utf_data+2), len);
pool_tags[i] = JV_CONSTANT_String;
}
else
{
pool_tags[i] = JV_CONSTANT_Undefined;
}
}
// and now, we scan everything else but strings & utf8-entries. This
// leaves out those utf8-entries which are not used; which will be left
// with a tag of JV_CONSTANT_Undefined in the class definition.
for (int index = 1; index < pool_count; index++)
{
switch (tags[index])
{
case JV_CONSTANT_Undefined:
case JV_CONSTANT_String:
case JV_CONSTANT_Utf8:
continue;
default:
prepare_pool_entry (index, tags[index]);
}
}
}
/* this is a recursive procedure, which will prepare pool entries as needed.
Which is how we avoid initializing those entries which go unused. */
void
_Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag)
{
/* these two, pool_data and pool_tags, point into the class
structure we are currently defining */
unsigned char *pool_tags = (unsigned char*) def->constants.tags;
_Jv_word *pool_data = def->constants.data;
/* this entry was already prepared */
if (pool_tags[index] == this_tag)
return;
/* this_data points to the constant-pool information for the current
constant-pool entry */
unsigned char *this_data = bytes + offsets[index];
switch (this_tag)
{
case JV_CONSTANT_Utf8:
{
// If we came here, it is because some other tag needs this
// utf8-entry for type information! Thus, we translate /'s to .'s in
// order to accomondate gcj's internal representation.
int len = get2u (this_data);
char *buffer = (char*) __builtin_alloca (len);
char *s = ((char*) this_data)+2;
/* FIXME: avoid using a buffer here */
for (int i = 0; i < len; i++)
{
if (s[i] == '/')
buffer[i] = '.';
else
buffer[i] = (char) s[i];
}
pool_data[index].utf8 = _Jv_makeUtf8Const (buffer, len);
pool_tags[index] = JV_CONSTANT_Utf8;
}
break;
case JV_CONSTANT_Class:
{
int utf_index = get2u (this_data);
check_tag (utf_index, JV_CONSTANT_Utf8);
prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
if (verify)
verify_classname (pool_data[utf_index].utf8);
pool_data[index].utf8 = pool_data[utf_index].utf8;
pool_tags[index] = JV_CONSTANT_Class;
}
break;
case JV_CONSTANT_String:
// already handled before...
break;
case JV_CONSTANT_Fieldref:
case JV_CONSTANT_Methodref:
case JV_CONSTANT_InterfaceMethodref:
{
int class_index = get2u (this_data);
int nat_index = get2u (this_data+2);
check_tag (class_index, JV_CONSTANT_Class);
prepare_pool_entry (class_index, JV_CONSTANT_Class);
check_tag (nat_index, JV_CONSTANT_NameAndType);
prepare_pool_entry (nat_index, JV_CONSTANT_NameAndType);
// here, verify the signature and identifier name
if (verify)
{
_Jv_ushort name_index, type_index;
_Jv_loadIndexes (&pool_data[nat_index],
name_index, type_index);
if (this_tag == JV_CONSTANT_Fieldref)
verify_field_signature (pool_data[type_index].utf8);
else
verify_method_signature (pool_data[type_index].utf8);
_Jv_Utf8Const* name = pool_data[name_index].utf8;
if (this_tag != JV_CONSTANT_Fieldref
&& ( _Jv_equalUtf8Consts (name, clinit_name)
|| _Jv_equalUtf8Consts (name, init_name)))
/* ignore */;
else
verify_identifier (pool_data[name_index].utf8);
}
_Jv_storeIndexes (&pool_data[index], class_index, nat_index);
pool_tags[index] = this_tag;
}
break;
case JV_CONSTANT_NameAndType:
{
_Jv_ushort name_index = get2u (this_data);
_Jv_ushort type_index = get2u (this_data+2);
check_tag (name_index, JV_CONSTANT_Utf8);
prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
check_tag (type_index, JV_CONSTANT_Utf8);
prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
_Jv_storeIndexes (&pool_data[index], name_index, type_index);
pool_tags[index] = JV_CONSTANT_NameAndType;
}
break;
case JV_CONSTANT_Float:
{
jfloat f = java::lang::Float::intBitsToFloat ((jint) get4 (this_data));
_Jv_storeFloat (&pool_data[index], f);
pool_tags[index] = JV_CONSTANT_Float;
}
break;
case JV_CONSTANT_Integer:
{
int i = get4 (this_data);
_Jv_storeInt (&pool_data[index], i);
pool_tags[index] = JV_CONSTANT_Integer;
}
break;
case JV_CONSTANT_Double:
{
jdouble d
= java::lang::Double::longBitsToDouble ((jlong) get8 (this_data));
_Jv_storeDouble (&pool_data[index], d);
pool_tags[index] = JV_CONSTANT_Double;
}
break;
case JV_CONSTANT_Long:
{
jlong i = get8 (this_data);
_Jv_storeLong (&pool_data[index], i);
pool_tags[index] = JV_CONSTANT_Long;
}
break;
default:
throw_class_format_error ("erroneous constant pool tag");
}
}
void
_Jv_ClassReader::handleClassBegin (int access_flags, int this_class, int super_class)
{
using namespace java::lang::reflect;
unsigned char *pool_tags = (unsigned char*) def->constants.tags;
_Jv_word *pool_data = def->constants.data;
check_tag (this_class, JV_CONSTANT_Class);
_Jv_Utf8Const *loadedName = pool_data[this_class].utf8;
// was ClassLoader.defineClass called with an expected class name?
if (def->name == 0)
{
jclass orig = def->loader->findLoadedClass(loadedName->toString());
if (orig == 0)
{
def->name = loadedName;
}
else
{
jstring msg = JvNewStringUTF ("anonymous "
"class data denotes "
"existing class ");
msg = msg->concat (orig->getName ());
throw_no_class_def_found_error (msg);
}
}
// assert that the loaded class has the expected name, 5.3.5
else if (! _Jv_equalUtf8Consts (loadedName, def->name))
{
jstring msg = JvNewStringUTF ("loaded class ");
msg = msg->concat (def->getName ());
msg = msg->concat (_Jv_NewStringUTF (" was in fact named "));
jstring klass_name = loadedName->toString();
msg = msg->concat (klass_name);
throw_no_class_def_found_error (msg);
}
def->accflags = access_flags | java::lang::reflect::Modifier::INTERPRETED;
pool_data[this_class].clazz = def;
pool_tags[this_class] = JV_CONSTANT_ResolvedClass;
if (super_class == 0)
{
// Note that this is ok if we are defining java.lang.Object.
// But there is no way to have this class be interpreted.
throw_class_format_error ("no superclass reference");
}
def->state = JV_STATE_PRELOADING;
// Register this class with its defining loader as well (despite the
// name of the function we're calling), so that super class lookups
// work properly. If there is an error, our caller will unregister
// this class from the class loader. Also, we don't need to hold a
// lock here, as our caller has acquired it.
_Jv_RegisterInitiatingLoader (def, def->loader);
// Note that we found a name so that unregistration can happen if
// needed.
*found_name = def->name;
if (super_class != 0)
{
// Load the superclass.
check_tag (super_class, JV_CONSTANT_Class);
_Jv_Utf8Const* super_name = pool_data[super_class].utf8;
// Load the superclass using our defining loader.
jclass the_super = _Jv_FindClass (super_name, def->loader);
// This will establish that we are allowed to be a subclass,
// and check for class circularity error.
checkExtends (def, the_super);
// Note: for an interface we will find Object as the
// superclass. We still check it above to ensure class file
// validity, but we simply assign `null' to the actual field in
// this case.
def->superclass = (((access_flags & Modifier::INTERFACE))
? NULL : the_super);
pool_data[super_class].clazz = the_super;
pool_tags[super_class] = JV_CONSTANT_ResolvedClass;