allow all arm targets to use -mstructure-size-boundary=XX
[official-gcc.git] / libjava / defineclass.cc
blobd096bfa159c5e58c199edd0471d4d7df4930b9dd
1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 1999 Cygnus Solutions
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
9 details. */
11 /*
12 Author: Kresten Krab Thorup <krab@gnu.org>
14 Written using the online versions of Java Language Specification (1st
15 ed.) and The Java Virtual Machine Specification (2nd ed.).
17 Future work may include reading (and handling) attributes which are
18 currently being ignored ("InnerClasses", "LineNumber", etc...).
21 #include <config.h>
23 #include <java-interp.h>
25 #ifdef INTERPRETER
27 #include <java-cpool.h>
28 #include <gcj/cni.h>
30 #include <java/lang/Class.h>
31 #include <java/lang/Float.h>
32 #include <java/lang/Double.h>
33 #include <java/lang/Character.h>
34 #include <java/lang/LinkageError.h>
35 #include <java/lang/InternalError.h>
36 #include <java/lang/ClassFormatError.h>
37 #include <java/lang/NoClassDefFoundError.h>
38 #include <java/lang/ClassCircularityError.h>
39 #include <java/lang/ClassNotFoundException.h>
40 #include <java/lang/IncompatibleClassChangeError.h>
41 #include <java/lang/reflect/Modifier.h>
43 #define ClassClass _CL_Q34java4lang5Class
44 extern java::lang::Class ClassClass;
45 #define StringClass _CL_Q34java4lang6String
46 extern java::lang::Class StringClass;
47 #define ClassObject _CL_Q34java4lang6Object
48 extern java::lang::Class ClassObject;
50 // we don't verify method names that match these.
51 static _Jv_Utf8Const *clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
52 static _Jv_Utf8Const *init_name = _Jv_makeUtf8Const ("<init>", 6);
55 // these go in some seperate functions, to avoid having _Jv_InitClass
56 // inserted all over the place.
57 static void throw_internal_error (char *msg)
58 __attribute__ ((__noreturn__));
59 static void throw_no_class_def_found_error (jstring msg)
60 __attribute__ ((__noreturn__));
61 static void throw_no_class_def_found_error (char *msg)
62 __attribute__ ((__noreturn__));
63 static void throw_class_format_error (jstring msg)
64 __attribute__ ((__noreturn__));
65 static void throw_class_format_error (char *msg)
66 __attribute__ ((__noreturn__));
67 static void throw_incompatible_class_change_error (jstring msg)
68 __attribute__ ((__noreturn__));
69 static void throw_class_circularity_error (jstring msg)
70 __attribute__ ((__noreturn__));
72 static jdouble long_bits_to_double (jlong);
73 static jfloat int_bits_to_float (jint);
75 /**
76 * We define class reading using a class. It is practical, since then
77 * the entire class-reader can be a friend of class Class (it needs to
78 * write all it's different structures); but also because this makes it
79 * easy to make class definition reentrant, and thus two threads can be
80 * defining classes at the same time. This class (_Jv_ClassReader) is
81 * never exposed outside this file, so we don't have to worry about
82 * public or private members here.
85 struct _Jv_ClassReader {
87 // do verification? Currently, there is no option to disable this.
88 // This flag just controls the verificaiton done by the class loader;
89 // i.e., checking the integrity of the constant pool; and it is
90 // allways on. You always want this as far as I can see, but it also
91 // controls weither identifiers and type descriptors/signatures are
92 // verified as legal. This could be somewhat more expensive since it
93 // will call Characher.isJavaIdentifier{Start,Part} for each character
94 // in any identifier (field name or method name) it comes by. Thus,
95 // it might be useful to turn off this verification for classes that
96 // come from a trusted source. However, for GCJ, trusted classes are
97 // most likely to be linked in.
99 bool verify;
101 // input data.
102 unsigned char *bytes;
103 int len;
105 // current input position
106 int pos;
108 // the constant pool data
109 int pool_count;
110 unsigned char *tags;
111 unsigned int *offsets;
113 // the class to define (see java-interp.h)
114 _Jv_InterpClass *def;
116 /* check that the given number of input bytes are available */
117 inline void check (int num)
119 if (pos + num > len)
120 throw_class_format_error ("Premature end of data");
123 /* skip a given number of bytes in input */
124 inline void skip (int num)
126 check (num);
127 pos += num;
130 /* read an unsignend 1-byte unit */
131 inline static jint get1u (unsigned char* bytes)
133 return bytes[0];
136 /* read an unsigned 1-byte unit */
137 inline jint read1u ()
139 skip (1);
140 return get1u (bytes+pos-1);
143 /* read an unsigned 2-byte unit */
144 inline static jint get2u (unsigned char *bytes)
146 return (((jint)bytes[0]) << 8) | ((jint)bytes[1]);
149 /* read an unsigned 2-byte unit */
150 inline jint read2u ()
152 skip (2);
153 return get2u (bytes+pos-2);
156 /* read a 4-byte unit */
157 static jint get4 (unsigned char *bytes)
159 return (((jint)bytes[0]) << 24)
160 | (((jint)bytes[1]) << 16)
161 | (((jint)bytes[2]) << 8)
162 | (((jint)bytes[3]) << 0);
165 /* read a 4-byte unit, (we don't do that quite so often) */
166 inline jint read4 ()
168 skip (4);
169 return get4 (bytes+pos-4);
172 /* read a 8-byte unit */
173 static jlong get8 (unsigned char* bytes)
175 return (((jlong)bytes[0]) << 56)
176 | (((jlong)bytes[1]) << 48)
177 | (((jlong)bytes[2]) << 40)
178 | (((jlong)bytes[3]) << 32)
179 | (((jlong)bytes[4]) << 24)
180 | (((jlong)bytes[5]) << 16)
181 | (((jlong)bytes[6]) << 8)
182 | (((jlong)bytes[7]) << 0);
185 /* read a 8-byte unit */
186 inline jlong read8 ()
188 skip (8);
189 return get8 (bytes+pos-8);
192 inline void check_tag (int index, char expected_tag)
194 if (index < 0
195 || index > pool_count
196 || tags[index] != expected_tag)
197 throw_class_format_error ("erroneous constant pool tag");
200 _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length)
202 if (klass == 0 || length < 0 || offset+length > data->length)
203 throw_internal_error ("arguments to _Jv_DefineClass");
205 verify = true;
206 bytes = (unsigned char*) (elements (data)+offset);
207 len = length;
208 pos = 0;
209 def = (_Jv_InterpClass*) klass;
212 /** and here goes the parser members defined out-of-line */
213 void parse ();
214 void read_constpool ();
215 void prepare_pool_entry (int index, unsigned char tag);
216 void read_fields ();
217 void read_methods ();
218 void read_one_class_attribute ();
219 void read_one_method_attribute (int method);
220 void read_one_code_attribute (int method);
221 void read_one_field_attribute (int field);
223 /** check an utf8 entry, without creating a Utf8Const object */
224 bool is_attribute_name (int index, char *name);
226 /** here goes the class-loader members defined out-of-line */
227 void handleConstantPool ();
228 void handleClassBegin (int, int, int);
229 void handleInterfacesBegin (int);
230 void handleInterface (int, int);
231 void handleFieldsBegin (int);
232 void handleField (int, int, int, int);
233 void handleFieldsEnd ();
234 void handleConstantValueAttribute (int,int);
235 void handleMethodsBegin (int);
236 void handleMethod (int, int, int, int);
237 void handleMethodsEnd ();
238 void handleCodeAttribute (int, int, int, int, int, int);
239 void handleExceptionTableEntry (int, int, int, int, int, int);
241 void checkExtends (jclass sub, jclass super);
242 void checkImplements (jclass sub, jclass super);
245 * FIXME: we should keep a hash table of utf8-strings, since many will
246 * be the same. It's a little tricky, however, because the hash table
247 * needs to interact gracefully with the garbage collector. Much
248 * memory is to be saved by this, however! perhaps the improvement
249 * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
250 * computes the hash value anyway.
254 /* This is used for the isJavaIdentifierStart & isJavaIdentifierPart
255 methods, so we avoid doing _Jv_InitClass all the time */
257 static const java::lang::Character *character = 0;
258 static void prepare_character ();
260 void
261 _Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length)
263 if (character == 0)
264 prepare_character ();
266 _Jv_ClassReader reader (klass, data, offset, length);
267 reader.parse();
269 /* that's it! */
272 /** put it after _Jv_DefineClass, so it doesn't get inlined */
273 static void prepare_character ()
275 character = new java::lang::Character ('!');
279 /** This section defines the parsing/scanning of the class data */
281 void
282 _Jv_ClassReader::parse ()
284 int magic = read4 ();
286 /* FIXME: Decide which range of version numbers to allow */
288 /* int minor_version = */ read2u ();
289 /* int major_verson = */ read2u ();
291 if (magic != (int) 0xCAFEBABE)
292 throw_class_format_error ("bad magic number");
294 pool_count = read2u ();
296 read_constpool ();
298 int access_flags = read2u ();
299 int this_class = read2u ();
300 int super_class = read2u ();
302 check_tag (this_class, JV_CONSTANT_Class);
303 if (super_class != 0)
304 check_tag (super_class, JV_CONSTANT_Class);
306 handleClassBegin (access_flags, this_class, super_class);
308 int interfaces_count = read2u ();
310 handleInterfacesBegin (interfaces_count);
312 for (int i = 0; i < interfaces_count; i++)
314 int iface = read2u ();
315 check_tag (iface, JV_CONSTANT_Class);
316 handleInterface (i, iface);
319 read_fields ();
320 read_methods ();
322 int attributes_count = read2u ();
324 for (int i = 0; i < attributes_count; i++)
326 read_one_class_attribute ();
329 if (pos != len)
330 throw_class_format_error ("unused data before end of file");
332 // tell everyone we're done.
333 def->state = JV_STATE_LOADED;
334 def->notifyAll ();
338 void _Jv_ClassReader::read_constpool ()
340 tags = (unsigned char*) _Jv_AllocBytesChecked (pool_count);
341 offsets = (unsigned int *) _Jv_AllocBytesChecked (sizeof (int)
342 * pool_count) ;
344 /** first, we scan the constant pool, collecting tags and offsets */
345 tags[0] = JV_CONSTANT_Undefined;
346 offsets[0] = pos;
347 for (int c = 1; c < pool_count; c++)
349 tags[c] = read1u ();
350 offsets[c] = pos;
352 switch (tags[c])
354 case JV_CONSTANT_String:
355 case JV_CONSTANT_Class:
356 skip (2);
357 break;
359 case JV_CONSTANT_Fieldref:
360 case JV_CONSTANT_Methodref:
361 case JV_CONSTANT_InterfaceMethodref:
362 case JV_CONSTANT_NameAndType:
363 case JV_CONSTANT_Integer:
364 case JV_CONSTANT_Float:
365 skip (4);
366 break;
368 case JV_CONSTANT_Double:
369 case JV_CONSTANT_Long:
370 skip (8);
371 tags[++c] = JV_CONSTANT_Undefined;
372 break;
374 case JV_CONSTANT_Utf8:
376 int len = read2u ();
377 skip (len);
379 break;
381 case JV_CONSTANT_Unicode:
382 throw_class_format_error ("unicode not supported");
383 break;
385 default:
386 throw_class_format_error ("erroneous constant pool tag");
390 handleConstantPool ();
394 void _Jv_ClassReader::read_fields ()
396 int fields_count = read2u ();
397 handleFieldsBegin (fields_count);
399 for (int i = 0; i < fields_count; i++)
401 int access_flags = read2u ();
402 int name_index = read2u ();
403 int descriptor_index = read2u ();
404 int attributes_count = read2u ();
406 check_tag (name_index, JV_CONSTANT_Utf8);
407 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
409 check_tag (descriptor_index, JV_CONSTANT_Utf8);
410 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
412 handleField (i, access_flags, name_index, descriptor_index);
414 for (int j = 0; j < attributes_count; j++)
416 read_one_field_attribute (i);
420 handleFieldsEnd ();
423 bool
424 _Jv_ClassReader::is_attribute_name (int index, char *name)
426 check_tag (index, JV_CONSTANT_Utf8);
427 int len = get2u (bytes+offsets[index]);
428 if (len != (int) strlen (name))
429 return false;
430 else
431 return !memcmp (bytes+offsets[index]+2, name, len);
434 void _Jv_ClassReader::read_one_field_attribute (int field_index)
436 int name = read2u ();
437 int length = read4 ();
439 if (is_attribute_name (name, "ConstantValue"))
441 int cv = read2u ();
443 if (cv < pool_count
444 && cv > 0
445 && (tags[cv] == JV_CONSTANT_Integer
446 || tags[cv] == JV_CONSTANT_Float
447 || tags[cv] == JV_CONSTANT_Long
448 || tags[cv] == JV_CONSTANT_Double
449 || tags[cv] == JV_CONSTANT_String))
451 handleConstantValueAttribute (field_index, cv);
453 else
455 throw_class_format_error ("erroneous ConstantValue attribute");
458 if (length != 2)
459 throw_class_format_error ("erroneous ConstantValue attribute");
462 else
464 skip (length);
468 void _Jv_ClassReader::read_methods ()
470 int methods_count = read2u ();
472 handleMethodsBegin (methods_count);
474 for (int i = 0; i < methods_count; i++)
476 int access_flags = read2u ();
477 int name_index = read2u ();
478 int descriptor_index = read2u ();
479 int attributes_count = read2u ();
481 check_tag (name_index, JV_CONSTANT_Utf8);
482 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
484 check_tag (name_index, JV_CONSTANT_Utf8);
485 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
487 handleMethod (i, access_flags, name_index,
488 descriptor_index);
490 for (int j = 0; j < attributes_count; j++)
492 read_one_method_attribute (i);
496 handleMethodsEnd ();
499 void _Jv_ClassReader::read_one_method_attribute (int method_index)
501 int name = read2u ();
502 int length = read4 ();
504 if (is_attribute_name (name, "Exceptions"))
506 /* we ignore this for now */
507 skip (length);
510 else if (is_attribute_name (name, "Code"))
512 int start_off = pos;
513 int max_stack = read2u ();
514 int max_locals = read2u ();
515 int code_length = read4 ();
517 int code_start = pos;
518 skip (code_length);
519 int exception_table_length = read2u ();
521 handleCodeAttribute (method_index,
522 max_stack, max_locals,
523 code_start, code_length,
524 exception_table_length);
527 for (int i = 0; i < exception_table_length; i++)
529 int start_pc = read2u ();
530 int end_pc = read2u ();
531 int handler_pc = read2u ();
532 int catch_type = read2u ();
534 if (start_pc > end_pc
535 || start_pc < 0
536 || end_pc >= code_length
537 || handler_pc >= code_length)
538 throw_class_format_error ("erroneous exception handler info");
540 if (! (tags[catch_type] == JV_CONSTANT_Class
541 || tags[catch_type] == 0))
543 throw_class_format_error ("erroneous exception handler info");
546 handleExceptionTableEntry (method_index,
548 start_pc,
549 end_pc,
550 handler_pc,
551 catch_type);
555 int attributes_count = read2u ();
557 for (int i = 0; i < attributes_count; i++)
559 read_one_code_attribute (method_index);
562 if ((pos - start_off) != length)
563 throw_class_format_error ("code attribute too short");
566 else
568 /* ignore unknown attributes */
569 skip (length);
573 void _Jv_ClassReader::read_one_code_attribute (int /*method*/)
575 /* ignore for now, ... later we may want to pick up
576 line number information, for debugging purposes;
577 in fact, the whole debugger issue is open! */
579 /* int name = */ read2u ();
580 int length = read4 ();
581 skip (length);
585 void _Jv_ClassReader::read_one_class_attribute ()
587 /* we also ignore the class attributes, ...
588 some day we'll add inner-classes support. */
590 /* int name = */ read2u ();
591 int length = read4 ();
592 skip (length);
598 /* this section defines the semantic actions of the parser */
600 void _Jv_ClassReader::handleConstantPool ()
602 /** now, we actually define the class' constant pool */
604 // the pool is scanned explicitly by the collector
605 jbyte *pool_tags = (jbyte*) _Jv_AllocBytesChecked (pool_count);
606 _Jv_word *pool_data
607 = (_Jv_word*) _Jv_AllocBytesChecked (pool_count * sizeof (_Jv_word));
609 def->constants.tags = pool_tags;
610 def->constants.data = pool_data;
611 def->constants.size = pool_count;
613 // Here we make a pass to collect the strings! We do this, because
614 // internally in the GCJ runtime, classes are encoded with .'s not /'s.
615 // Therefore, we first collect the strings, and then translate the rest
616 // of the utf8-entries (thus not representing strings) from /-notation
617 // to .-notation.
618 for (int i = 1; i < pool_count; i++)
620 if (tags[i] == JV_CONSTANT_String)
622 unsigned char* str_data = bytes + offsets [i];
623 int utf_index = get2u (str_data);
624 check_tag (utf_index, JV_CONSTANT_Utf8);
625 unsigned char *utf_data = bytes + offsets[utf_index];
626 int len = get2u (utf_data);
627 pool_data[i].utf8 = _Jv_makeUtf8Const ((char*)(utf_data+2), len);
628 pool_tags[i] = JV_CONSTANT_String;
630 else
632 pool_tags[i] = JV_CONSTANT_Undefined;
636 // and now, we scan everything else but strings & utf8-entries. This
637 // leaves out those utf8-entries which are not used; which will be left
638 // with a tag of JV_CONSTANT_Undefined in the class definition.
639 for (int index = 1; index < pool_count; index++)
641 switch (tags[index])
643 case JV_CONSTANT_Undefined:
644 case JV_CONSTANT_String:
645 case JV_CONSTANT_Utf8:
646 continue;
648 default:
649 prepare_pool_entry (index, tags[index]);
655 /* this is a recursive procedure, which will prepare pool entries as needed.
656 Which is how we avoid initializing those entries which go unused. */
657 void
658 _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag)
660 /* these two, pool_data and pool_tags, point into the class
661 structure we are currently defining */
663 unsigned char *pool_tags = (unsigned char*) def->constants.tags;
664 _Jv_word *pool_data = def->constants.data;
666 /* this entry was already prepared */
667 if (pool_tags[index] == this_tag)
668 return;
670 /* this_data points to the constant-pool information for the current
671 constant-pool entry */
673 unsigned char *this_data = bytes + offsets[index];
675 switch (this_tag)
677 case JV_CONSTANT_Utf8:
679 // If we came here, it is because some other tag needs this
680 // utf8-entry for type information! Thus, we translate /'s to .'s in
681 // order to accomondate gcj's internal representation.
683 int len = get2u (this_data);
684 char *buffer = (char*) alloca (len);
685 char *s = ((char*) this_data)+2;
687 /* FIXME: avoid using a buffer here */
688 for (int i = 0; i < len; i++)
690 if (s[i] == '/')
691 buffer[i] = '.';
692 else
693 buffer[i] = (char) s[i];
696 pool_data[index].utf8 = _Jv_makeUtf8Const (buffer, len);
697 pool_tags[index] = JV_CONSTANT_Utf8;
699 break;
701 case JV_CONSTANT_Class:
703 int utf_index = get2u (this_data);
704 check_tag (utf_index, JV_CONSTANT_Utf8);
705 prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
707 if (verify)
708 _Jv_VerifyClassName (pool_data[utf_index].utf8);
710 pool_data[index].utf8 = pool_data[utf_index].utf8;
711 pool_tags[index] = JV_CONSTANT_Class;
713 break;
715 case JV_CONSTANT_String:
716 // already handled before...
717 break;
719 case JV_CONSTANT_Fieldref:
720 case JV_CONSTANT_Methodref:
721 case JV_CONSTANT_InterfaceMethodref:
723 int class_index = get2u (this_data);
724 int nat_index = get2u (this_data+2);
726 check_tag (class_index, JV_CONSTANT_Class);
727 prepare_pool_entry (class_index, JV_CONSTANT_Class);
729 check_tag (nat_index, JV_CONSTANT_NameAndType);
730 prepare_pool_entry (nat_index, JV_CONSTANT_NameAndType);
732 // here, verify the signature and identifier name
733 if (verify)
735 _Jv_ushort name_index, type_index;
736 _Jv_loadIndexes (&pool_data[nat_index],
737 name_index, type_index);
739 if (this_tag == JV_CONSTANT_Fieldref)
740 _Jv_VerifyFieldSignature (pool_data[type_index].utf8);
741 else
742 _Jv_VerifyMethodSignature (pool_data[type_index].utf8);
744 _Jv_Utf8Const* name = pool_data[name_index].utf8;
746 if (this_tag != JV_CONSTANT_Fieldref
747 && ( _Jv_equalUtf8Consts (name, clinit_name)
748 || _Jv_equalUtf8Consts (name, init_name)))
749 /* ignore */;
750 else
751 _Jv_VerifyIdentifier (pool_data[name_index].utf8);
754 _Jv_storeIndexes (&pool_data[index], class_index, nat_index);
755 pool_tags[index] = this_tag;
757 break;
759 case JV_CONSTANT_NameAndType:
761 _Jv_ushort name_index = get2u (this_data);
762 _Jv_ushort type_index = get2u (this_data+2);
764 check_tag (name_index, JV_CONSTANT_Utf8);
765 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
767 check_tag (type_index, JV_CONSTANT_Utf8);
768 prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
770 _Jv_storeIndexes (&pool_data[index], name_index, type_index);
771 pool_tags[index] = JV_CONSTANT_NameAndType;
773 break;
775 case JV_CONSTANT_Float:
777 jfloat f = int_bits_to_float ((jint) get4 (this_data));
778 _Jv_storeFloat (&pool_data[index], f);
779 pool_tags[index] = JV_CONSTANT_Float;
781 break;
783 case JV_CONSTANT_Integer:
785 int i = get4 (this_data);
786 _Jv_storeInt (&pool_data[index], i);
787 pool_tags[index] = JV_CONSTANT_Integer;
789 break;
791 case JV_CONSTANT_Double:
793 jdouble d = long_bits_to_double ((jlong) get8 (this_data));
794 _Jv_storeDouble (&pool_data[index], d);
795 pool_tags[index] = JV_CONSTANT_Double;
797 break;
799 case JV_CONSTANT_Long:
801 jlong i = get8 (this_data);
802 _Jv_storeLong (&pool_data[index], i);
803 pool_tags[index] = JV_CONSTANT_Long;
805 break;
807 default:
808 throw_class_format_error ("erroneous constant pool tag");
813 void
814 _Jv_ClassReader::handleClassBegin
815 (int access_flags, int this_class, int super_class)
817 using namespace java::lang::reflect;
819 unsigned char *pool_tags = (unsigned char*) def->constants.tags;
820 _Jv_word *pool_data = def->constants.data;
822 check_tag (this_class, JV_CONSTANT_Class);
823 _Jv_Utf8Const *loadedName = pool_data[this_class].utf8;
825 // was ClassLoader.defineClass called with an expected class name?
826 if (def->name == 0)
828 jclass orig = _Jv_FindClassInCache (loadedName, def->loader);
830 if (orig == 0)
832 def->name = loadedName;
834 else
836 jstring msg = JvNewStringUTF ("anonymous "
837 "class data denotes "
838 "existing class ");
839 msg = msg->concat (orig->getName ());
841 throw_no_class_def_found_error (msg);
845 // assert that the loaded class has the expected name, 5.3.5
846 else if (! _Jv_equalUtf8Consts (loadedName, def->name))
848 jstring msg = JvNewStringUTF ("loaded class ");
849 msg = msg->concat (def->getName ());
850 msg = msg->concat (_Jv_NewStringUTF (" was in fact named "));
851 jstring klass_name = _Jv_NewStringUTF (loadedName->data);
852 msg = msg->concat (klass_name);
854 throw_no_class_def_found_error (msg);
857 def->accflags = access_flags;
858 pool_data[this_class].clazz = def;
859 pool_tags[this_class] = JV_CONSTANT_ResolvedClass;
861 if (super_class == 0)
863 // interfaces have java.lang.Object as super.
864 if (access_flags & Modifier::INTERFACE)
866 def->superclass = (jclass)&ClassObject;
869 // FIXME: Consider this carefully!
870 else if (!_Jv_equalUtf8Consts (def->name, ClassObject.name))
872 throw_no_class_def_found_error ("loading java.lang.Object");
876 // In the pre-loading state, it can be looked up in the
877 // cache only by this thread! This allows the super-class
878 // to include references to this class.
880 def->state = JV_STATE_PRELOADING;
881 _Jv_RegisterClass (def);
883 if (super_class != 0)
885 // load the super class
886 check_tag (super_class, JV_CONSTANT_Class);
887 _Jv_Utf8Const* super_name = pool_data[super_class].utf8;
889 // load the super class using our defining loader
890 jclass the_super = _Jv_FindClass (super_name,
891 def->loader);
893 // This will establish that we are allowed to be a subclass,
894 // and check for class circularity error
895 checkExtends (def, the_super);
897 def->superclass = the_super;
898 pool_data[super_class].clazz = the_super;
899 pool_tags[super_class] = JV_CONSTANT_ResolvedClass;
902 // now we've come past the circularity problem, we can
903 // now say that we're loading...
905 def->state = JV_STATE_LOADING;
906 def->notifyAll ();
909 ///// implements the checks described in sect. 5.3.5.3
910 void
911 _Jv_ClassReader::checkExtends (jclass sub, jclass super)
913 using namespace java::lang::reflect;
915 // having an interface or a final class as a superclass is no good
916 if ((super->accflags & (Modifier::INTERFACE | Modifier::FINAL)) != 0)
918 throw_incompatible_class_change_error (sub->getName ());
921 // if the super class is not public, we need to check some more
922 if ((super->accflags & Modifier::PUBLIC) == 0)
924 // With package scope, the classes must have the same
925 // class loader.
926 if ( sub->loader != super->loader
927 || !_Jv_ClassNameSamePackage (sub->name, super->name))
929 throw_incompatible_class_change_error (sub->getName ());
933 for (; super != 0; super = super->superclass)
935 if (super == sub)
936 throw_class_circularity_error (sub->getName ());
942 void _Jv_ClassReader::handleInterfacesBegin (int count)
944 def->interfaces = (jclass*) _Jv_AllocBytesChecked (count*sizeof (jclass));
945 def->interface_count = count;
948 void _Jv_ClassReader::handleInterface (int if_number, int offset)
950 _Jv_word * pool_data = def->constants.data;
951 unsigned char * pool_tags = (unsigned char*) def->constants.tags;
953 jclass the_interface;
955 if (pool_tags[offset] == JV_CONSTANT_Class)
957 _Jv_Utf8Const* name = pool_data[offset].utf8;
958 the_interface = _Jv_FindClass (name, def->loader);
960 else if (pool_tags[offset] == JV_CONSTANT_ResolvedClass)
962 the_interface = pool_data[offset].clazz;
964 else
966 throw_no_class_def_found_error ("erroneous constant pool tag");
969 // checks the validity of the_interface, and that we are in fact
970 // allowed to implement that interface.
971 checkImplements (def, the_interface);
973 pool_data[offset].clazz = the_interface;
974 pool_tags[offset] = JV_CONSTANT_ResolvedClass;
976 def->interfaces[if_number] = the_interface;
979 void
980 _Jv_ClassReader::checkImplements (jclass sub, jclass super)
982 using namespace java::lang::reflect;
984 // well, it *must* be an interface
985 if ((super->accflags & Modifier::INTERFACE) == 0)
987 throw_incompatible_class_change_error (sub->getName ());
990 // if it has package scope, it must also be defined by the
991 // same loader.
992 if ((super->accflags & Modifier::PUBLIC) == 0)
994 if ( sub->loader != super->loader
995 || !_Jv_ClassNameSamePackage (sub->name, super->name))
997 throw_incompatible_class_change_error (sub->getName ());
1001 // FIXME: add interface circularity check here
1002 if (sub == super)
1004 throw_class_circularity_error (sub->getName ());
1008 void _Jv_ClassReader::handleFieldsBegin (int count)
1010 def->fields = (_Jv_Field*)
1011 _Jv_AllocBytesChecked (count * sizeof (_Jv_Field));
1012 def->field_count = count;
1013 def->field_initializers = (_Jv_ushort*)
1014 _Jv_AllocBytesChecked (count * sizeof (_Jv_ushort));
1015 for (int i = 0; i < count; i++)
1016 def->field_initializers[i] = (_Jv_ushort) 0;
1019 void _Jv_ClassReader::handleField (int field_no,
1020 int flags,
1021 int name,
1022 int desc)
1024 using namespace java::lang::reflect;
1026 _Jv_word *pool_data = def->constants.data;
1028 _Jv_Field *field = &def->fields[field_no];
1029 _Jv_Utf8Const *field_name = pool_data[name].utf8;
1031 #ifndef COMPACT_FIELDS
1032 field->name = field_name;
1033 #else
1034 field->nameIndex = name;
1035 #endif
1037 if (verify)
1038 _Jv_VerifyIdentifier (field_name);
1040 // ignore flags we don't know about.
1041 field->flags = flags & Modifier::ALL_FLAGS;
1043 if (verify)
1045 if (field->flags & (Modifier::SYNCHRONIZED
1046 | Modifier::NATIVE
1047 | Modifier::INTERFACE
1048 | Modifier::ABSTRACT))
1049 throw_class_format_error ("erroneous field access flags");
1051 if (1 < ( ((field->flags & Modifier::PUBLIC) ? 1 : 0)
1052 +((field->flags & Modifier::PRIVATE) ? 1 : 0)
1053 +((field->flags & Modifier::PROTECTED) ? 1 : 0)))
1054 throw_class_format_error ("erroneous field access flags");
1057 _Jv_Utf8Const* sig = pool_data[desc].utf8;
1059 if (verify)
1060 _Jv_VerifyFieldSignature (sig);
1062 // field->type is really a jclass, but while it is still
1063 // unresolved we keep an _Jv_Utf8Const* instead.
1064 field->type = (jclass) sig;
1065 field->flags |= _Jv_FIELD_UNRESOLVED_FLAG;
1066 field->u.boffset = 0;
1070 void _Jv_ClassReader::handleConstantValueAttribute (int field_index,
1071 int value)
1073 using namespace java::lang::reflect;
1075 _Jv_Field *field = &def->fields[field_index];
1077 if ((field->flags & (Modifier::STATIC
1078 | Modifier::FINAL
1079 | Modifier::PRIVATE)) == 0)
1081 // Ignore, as per vmspec #4.7.2
1082 return;
1085 // do not allow multiple constant fields!
1086 if (field->flags & _Jv_FIELD_CONSTANT_VALUE)
1087 throw_class_format_error ("field has multiple ConstantValue attributes");
1089 field->flags |= _Jv_FIELD_CONSTANT_VALUE;
1090 def->field_initializers[field_index] = value;
1092 /* type check the initializer */
1094 if (value <= 0 || value >= pool_count)
1095 throw_class_format_error ("erroneous ConstantValue attribute");
1097 /* FIXME: do the rest */
1100 void _Jv_ClassReader::handleFieldsEnd ()
1102 using namespace java::lang::reflect;
1104 // We need to reorganize the fields so that the static ones are first,
1105 // to conform to GCJ class layout.
1107 int low = 0;
1108 int high = def->field_count-1;
1109 _Jv_Field *fields = def->fields;
1110 _Jv_ushort *inits = def->field_initializers;
1112 // this is kind of a raw version of quicksort.
1113 while (low < high)
1115 // go forward on low, while it's a static
1116 while (low < high && (fields[low].flags & Modifier::STATIC) != 0)
1117 low++;
1119 // go backwards on high, while it's a non-static
1120 while (low < high && (fields[high].flags & Modifier::STATIC) == 0)
1121 high--;
1123 if (low==high)
1124 break;
1126 _Jv_Field tmp = fields[low];
1127 _Jv_ushort itmp = inits[low];
1129 fields[low] = fields[high];
1130 inits[low] = inits[high];
1132 fields[high] = tmp;
1133 inits[high] = itmp;
1135 high -= 1;
1136 low += 1;
1139 if ((fields[low].flags & Modifier::STATIC) != 0)
1140 low += 1;
1142 def->static_field_count = low;
1147 void _Jv_ClassReader::handleMethodsBegin (int count)
1149 def->methods = (_Jv_Method*)
1150 _Jv_AllocBytesChecked (sizeof (_Jv_Method)*count);
1152 def->interpreted_methods = (_Jv_InterpMethod**)
1153 _Jv_AllocBytesChecked (sizeof (_Jv_InterpMethod*) * count);
1155 for (int i = 0; i < count; i++)
1156 def->interpreted_methods[i] = 0;
1158 def->method_count = count;
1162 void _Jv_ClassReader::handleMethod
1163 (int mth_index, int accflags, int name, int desc)
1165 using namespace java::lang::reflect;
1167 _Jv_word *pool_data = def->constants.data;
1168 _Jv_Method *method = &def->methods[mth_index];
1170 check_tag (name, JV_CONSTANT_Utf8);
1171 prepare_pool_entry (name, JV_CONSTANT_Utf8);
1172 method->name = pool_data[name].utf8;
1174 check_tag (desc, JV_CONSTANT_Utf8);
1175 prepare_pool_entry (desc, JV_CONSTANT_Utf8);
1176 method->signature = pool_data[desc].utf8;
1178 // ignore unknown flags
1179 method->accflags = accflags & Modifier::ALL_FLAGS;
1181 // intialize...
1182 method->ncode = 0;
1184 if (verify)
1186 if (_Jv_equalUtf8Consts (method->name, clinit_name)
1187 || _Jv_equalUtf8Consts (method->name, init_name))
1188 /* ignore */;
1189 else
1190 _Jv_VerifyIdentifier (method->name);
1192 _Jv_VerifyMethodSignature (method->signature);
1194 if (method->accflags & (Modifier::VOLATILE
1195 | Modifier::TRANSIENT
1196 | Modifier::INTERFACE))
1197 throw_class_format_error ("erroneous method access flags");
1199 if (1 < ( ((method->accflags & Modifier::PUBLIC) ? 1 : 0)
1200 +((method->accflags & Modifier::PRIVATE) ? 1 : 0)
1201 +((method->accflags & Modifier::PROTECTED) ? 1 : 0)))
1202 throw_class_format_error ("erroneous method access flags");
1206 void _Jv_ClassReader::handleCodeAttribute
1207 (int method_index, int max_stack, int max_locals,
1208 int code_start, int code_length, int exc_table_length)
1210 int size = _Jv_InterpMethod::size (exc_table_length, code_length);
1211 _Jv_InterpMethod *method =
1212 (_Jv_InterpMethod*) (_Jv_AllocBytesChecked (size));
1214 method->max_stack = max_stack;
1215 method->max_locals = max_locals;
1216 method->code_length = code_length;
1217 method->exc_count = exc_table_length;
1218 method->defining_class = def;
1219 method->self = &def->methods[method_index];
1221 // grab the byte code!
1222 memcpy ((void*) method->bytecode (),
1223 (void*) (bytes+code_start),
1224 code_length);
1226 def->interpreted_methods[method_index] = method;
1228 /* that's all we do for now */
1231 void _Jv_ClassReader::handleExceptionTableEntry
1232 (int method_index, int exc_index,
1233 int start_pc, int end_pc, int handler_pc, int catch_type)
1235 _Jv_InterpMethod *method = def->interpreted_methods[method_index];
1236 _Jv_InterpException *exc = method->exceptions ();
1238 exc[exc_index].start_pc = start_pc;
1239 exc[exc_index].end_pc = end_pc;
1240 exc[exc_index].handler_pc = handler_pc;
1241 exc[exc_index].handler_type = catch_type;
1244 void _Jv_ClassReader::handleMethodsEnd ()
1246 using namespace java::lang::reflect;
1248 for (int i = 0; i < def->method_count; i++)
1250 _Jv_Method *method = &def->methods[i];
1251 if (method->accflags & (Modifier::NATIVE | Modifier::ABSTRACT))
1253 if (def->interpreted_methods[i] != 0)
1254 throw_class_format_error ("code provided "
1255 "for abstract or native method");
1257 else
1259 if (def->interpreted_methods[i] == 0)
1260 throw_class_format_error ("abstract or native method "
1261 "with no code");
1268 /** This section takes care of verifying integrity of identifiers,
1269 signatures, field ddescriptors, and class names */
1271 #define UTF8_PEEK(PTR, LIMIT) \
1272 ({ unsigned char* xxkeep = (PTR); \
1273 int xxch = UTF8_GET(PTR,LIMIT); \
1274 PTR = xxkeep; xxch; })
1276 /* verify one element of a type descriptor or signature */
1277 static unsigned char*
1278 _Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok)
1280 if (ptr >= limit)
1281 return 0;
1283 int ch = UTF8_GET (ptr, limit);
1285 switch (ch)
1287 case 'V':
1288 if (! void_ok) return 0;
1290 case 'S': case 'B': case 'I': case 'J':
1291 case 'Z': case 'C': case 'F': case 'D':
1292 break;
1294 case 'L':
1296 unsigned char *start = ptr, *end;
1297 do {
1298 if (ptr > limit)
1299 return 0;
1301 end = ptr;
1303 if ((ch = UTF8_GET (ptr, limit)) == -1)
1304 return 0;
1306 } while (ch != ';');
1307 _Jv_VerifyClassName (start, (unsigned short) (end-start));
1309 break;
1311 case '[':
1312 return _Jv_VerifyOne (ptr, limit, false);
1313 break;
1315 default:
1316 return 0;
1319 return ptr;
1324 /** verification and loading procedures **/
1326 void
1327 _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
1329 unsigned char* ptr = (unsigned char*) sig->data;
1330 unsigned char* limit = ptr + sig->length;
1332 ptr = _Jv_VerifyOne (ptr, limit, false);
1334 if (ptr != limit)
1335 throw_class_format_error ("erroneous type descriptor");
1338 void
1339 _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig)
1341 unsigned char* ptr = (unsigned char*) sig->data;
1342 unsigned char* limit = ptr + sig->length;
1344 if (ptr == limit)
1345 throw_class_format_error ("erroneous type descriptor");
1347 if (UTF8_GET(ptr,limit) != '(')
1348 throw_class_format_error ("erroneous type descriptor");
1350 while (ptr && UTF8_PEEK (ptr, limit) != ')')
1351 ptr = _Jv_VerifyOne (ptr, limit, false);
1353 if (UTF8_GET (ptr, limit) != ')')
1354 throw_class_format_error ("erroneous type descriptor");
1356 // get the return type
1357 ptr = _Jv_VerifyOne (ptr, limit, true);
1359 if (ptr != limit)
1360 throw_class_format_error ("erroneous type descriptor");
1362 return;
1366 /* we try to avoid calling the Character methods all the time,
1367 in fact, they will only be called for non-standard things */
1369 static __inline__ int
1370 is_identifier_start (int c)
1372 unsigned int ch = (unsigned)c;
1374 if ((ch - 0x41U) < 29U) /* A ... Z */
1375 return 1;
1376 if ((ch - 0x61U) < 29U) /* a ... z */
1377 return 1;
1378 if (ch == 0x5FU) /* _ */
1379 return 1;
1381 return character->isJavaIdentifierStart ((jchar) ch);
1384 static __inline__ int
1385 is_identifier_part (int c)
1387 unsigned int ch = (unsigned)c;
1389 if ((ch - 0x41U) < 29U) /* A ... Z */
1390 return 1;
1391 if ((ch - 0x61U) < 29U) /* a ... z */
1392 return 1;
1393 if ((ch - 0x30) < 10U) /* 0 .. 9 */
1394 return 1;
1395 if (ch == 0x5FU || ch == 0x24U) /* _ $ */
1396 return 1;
1398 return character->isJavaIdentifierStart ((jchar) ch);
1401 void
1402 _Jv_VerifyIdentifier (_Jv_Utf8Const* name)
1404 unsigned char *ptr = (unsigned char*) name->data;
1405 unsigned char *limit = ptr + name->length;
1406 int ch;
1408 if ((ch = UTF8_GET (ptr, limit))==-1
1409 || ! is_identifier_start (ch))
1410 throw_class_format_error ("erroneous identifier");
1412 while (ptr != limit)
1414 if ((ch = UTF8_GET (ptr, limit))==-1
1415 || ! is_identifier_part (ch))
1416 throw_class_format_error ("erroneous identifier");
1421 void
1422 _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length)
1424 unsigned char *limit = ptr+length;
1425 int ch;
1427 if ('[' == UTF8_PEEK (ptr, limit))
1429 if (! _Jv_VerifyOne (++ptr, limit, false))
1430 throw_class_format_error ("erroneous class name");
1431 else
1432 return;
1435 next_level:
1436 do {
1437 if ((ch = UTF8_GET (ptr, limit))==-1)
1438 throw_class_format_error ("erroneous class name");
1439 if (! is_identifier_start (ch))
1440 throw_class_format_error ("erroneous class name");
1441 do {
1442 if (ptr == limit)
1443 return;
1444 else if ((ch = UTF8_GET (ptr, limit))==-1)
1445 throw_class_format_error ("erroneous class name");
1446 else if (ch == '.')
1447 goto next_level;
1448 else if (! is_identifier_part (ch))
1449 throw_class_format_error ("erroneous class name");
1450 } while (true);
1451 } while (true);
1455 void
1456 _Jv_VerifyClassName (_Jv_Utf8Const *name)
1458 _Jv_VerifyClassName ((unsigned char*)&name->data[0],
1459 (_Jv_ushort) name->length);
1463 /** returns true, if name1 and name2 represents classes in the same
1464 package. */
1466 bool
1467 _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2)
1469 unsigned char* ptr1 = (unsigned char*) name1->data;
1470 unsigned char* limit1 = ptr1 + name1->length;
1472 unsigned char* last1 = ptr1;
1474 // scan name1, and find the last occurrence of '.'
1475 while (ptr1 < limit1) {
1476 int ch1 = UTF8_GET (ptr1, limit1);
1478 if (ch1 == '.')
1479 last1 = ptr1;
1481 else if (ch1 == -1)
1482 return false;
1485 // now the length of name1's package name is len
1486 int len = last1 - (unsigned char*) name1->data;
1488 // if this is longer than name2, then we're off
1489 if (len > name2->length)
1490 return false;
1492 // then compare the first len bytes for equality
1493 if (memcmp ((void*) name1->data, (void*) name2->data, len) == 0)
1495 // check that there are no .'s after position len in name2
1497 unsigned char* ptr2 = (unsigned char*) name2->data + len;
1498 unsigned char* limit2 =
1499 (unsigned char*) name2->data + name2->length;
1501 while (ptr2 < limit2)
1503 int ch2 = UTF8_GET (ptr2, limit2);
1504 if (ch2 == -1 || ch2 == '.')
1505 return false;
1507 return true;
1509 return false;
1514 /** Here we define the exceptions that can be thrown */
1516 static void
1517 throw_no_class_def_found_error (jstring msg)
1519 if (msg == 0)
1520 JvThrow (new java::lang::NoClassDefFoundError);
1521 else
1522 JvThrow (new java::lang::NoClassDefFoundError (msg));
1525 static void
1526 throw_no_class_def_found_error (char *msg)
1528 throw_no_class_def_found_error (JvNewStringLatin1 (msg));
1531 static void
1532 throw_class_format_error (jstring msg)
1534 if (msg == 0)
1535 JvThrow (new java::lang::ClassFormatError);
1536 else
1537 JvThrow (new java::lang::ClassFormatError (msg));
1540 static void
1541 throw_class_format_error (char *msg)
1543 throw_class_format_error (JvNewStringLatin1 (msg));
1546 static void
1547 throw_internal_error (char *msg)
1549 JvThrow
1550 (new java::lang::InternalError (JvNewStringLatin1 (msg)));
1553 static jfloat int_bits_to_float (jint value)
1555 return java::lang::Float::intBitsToFloat (value);
1558 static jdouble long_bits_to_double (jlong value)
1560 return java::lang::Double::longBitsToDouble (value);
1563 static void throw_incompatible_class_change_error (jstring msg)
1565 JvThrow (new java::lang::IncompatibleClassChangeError (msg));
1568 static void throw_class_circularity_error (jstring msg)
1570 JvThrow (new java::lang::ClassCircularityError (msg));
1573 #endif /* INTERPRETER */