OSDN Git Service

c66fff84b7f70bad11573dec002322aaf62bc826
[pf3gnuchains/gcc-fork.git] / libjava / defineclass.cc
1 // defineclass.cc - defining a class from .class format.
2
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 /* 
12    Author: Kresten Krab Thorup <krab@gnu.org> 
13
14    Written using the online versions of Java Language Specification (1st
15    ed.) and The Java Virtual Machine Specification (2nd ed.). 
16
17    Future work may include reading (and handling) attributes which are
18    currently being ignored ("InnerClasses", "LineNumber", etc...).  
19 */
20
21 #include <config.h>
22
23 #include <java-interp.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <java-cpool.h>
28 #include <gcj/cni.h>
29 #include <execution.h>
30
31 #include <java/lang/Class.h>
32 #include <java/lang/Float.h>
33 #include <java/lang/Double.h>
34 #include <java/lang/Character.h>
35 #include <java/lang/LinkageError.h>
36 #include <java/lang/InternalError.h>
37 #include <java/lang/ClassFormatError.h>
38 #include <java/lang/NoClassDefFoundError.h>
39 #include <java/lang/ClassCircularityError.h>
40 #include <java/lang/IncompatibleClassChangeError.h>
41 #include <java/lang/reflect/Modifier.h>
42 #include <java/lang/reflect/Field.h>
43 #include <java/lang/reflect/Method.h>
44 #include <java/security/ProtectionDomain.h>
45 #include <java/io/DataOutputStream.h>
46 #include <java/io/ByteArrayOutputStream.h>
47
48 using namespace gcj;
49
50 #ifdef INTERPRETER
51
52 // these go in some separate functions, to avoid having _Jv_InitClass
53 // inserted all over the place.
54 static void throw_internal_error (const char *msg)
55         __attribute__ ((__noreturn__));
56 static void throw_no_class_def_found_error (jstring msg)
57         __attribute__ ((__noreturn__));
58 static void throw_no_class_def_found_error (const char *msg)
59         __attribute__ ((__noreturn__));
60 static void throw_class_format_error (jstring msg)
61         __attribute__ ((__noreturn__));
62 static void throw_incompatible_class_change_error (jstring msg)
63         __attribute__ ((__noreturn__));
64 static void throw_class_circularity_error (jstring msg)
65         __attribute__ ((__noreturn__));
66
67 /**
68  * We define class reading using a class.  It is practical, since then
69  * the entire class-reader can be a friend of class Class (it needs to
70  * write all it's different structures); but also because this makes it
71  * easy to make class definition reentrant, and thus two threads can be
72  * defining classes at the same time.   This class (_Jv_ClassReader) is
73  * never exposed outside this file, so we don't have to worry about
74  * public or private members here.
75  */
76
77 struct _Jv_ClassReader
78 {
79
80   // do verification?  Currently, there is no option to disable this.
81   // This flag just controls the verificaiton done by the class loader;
82   // i.e., checking the integrity of the constant pool; and it is
83   // allways on.  You always want this as far as I can see, but it also
84   // controls weither identifiers and type descriptors/signatures are
85   // verified as legal.  This could be somewhat more expensive since it
86   // will call Character.isJavaIdentifier{Start,Part} for each character
87   // in any identifier (field name or method name) it comes by.  Thus,
88   // it might be useful to turn off this verification for classes that
89   // come from a trusted source.  However, for GCJ, trusted classes are
90   // most likely to be linked in.
91
92   bool verify;
93
94   // original input data.
95   jbyteArray input_data;
96   jint input_offset;
97
98   // input data.
99   unsigned char     *bytes;
100   int                len;
101
102   // current input position
103   int                pos;
104
105   // the constant pool data
106   int pool_count;
107   unsigned char     *tags;
108   unsigned int      *offsets;
109
110   // the class to define (see java-interp.h)
111   jclass           def;
112
113   // the classes associated interpreter data.
114   _Jv_InterpClass  *def_interp;
115
116   // The name we found.
117   _Jv_Utf8Const **found_name;
118
119   // True if this is a 1.5 class file.
120   bool             is_15;
121
122   // Buffer holding extra reflection data.
123   ::java::io::ByteArrayOutputStream *reflection_data;
124   ::java::io::DataOutputStream *data_stream;
125
126
127   /* check that the given number of input bytes are available */
128   inline void check (int num)
129   {
130     if (pos + num > len)
131       throw_class_format_error ("Premature end of data");
132   }
133
134   /* skip a given number of bytes in input */
135   inline void skip (int num)
136   {
137     check (num);
138     pos += num;
139   }
140   
141   /* read an unsigned 1-byte unit */
142   inline static jint get1u (unsigned char* bytes)
143   {
144     return bytes[0];
145   }
146   
147   /* read an unsigned 1-byte unit */
148   inline jint read1u ()
149   {
150     skip (1);
151     return get1u (bytes+pos-1);
152   }
153   
154   /* read an unsigned 2-byte unit */
155   inline static jint get2u (unsigned char *bytes)
156   {
157     return (((jint)bytes[0]) << 8) | ((jint)bytes[1]);
158   }
159   
160   /* read an unsigned 2-byte unit */
161   inline jint read2u ()
162   {
163     skip (2);  
164     return get2u (bytes+pos-2);
165   }
166   
167   /* read a 4-byte unit */
168   static jint get4 (unsigned char *bytes)
169   {
170     return (((jint)bytes[0]) << 24)
171          | (((jint)bytes[1]) << 16)
172          | (((jint)bytes[2]) << 8)
173          | (((jint)bytes[3]) << 0);
174   }
175
176   /* read a 4-byte unit, (we don't do that quite so often) */
177   inline jint read4 ()
178   {
179     skip (4);  
180     return get4 (bytes+pos-4);
181   }
182
183   /* read a 8-byte unit */
184   static jlong get8 (unsigned char* bytes)
185   {
186     return (((jlong)bytes[0]) << 56)
187          | (((jlong)bytes[1]) << 48)
188          | (((jlong)bytes[2]) << 40)
189          | (((jlong)bytes[3]) << 32) 
190          | (((jlong)bytes[4]) << 24)
191          | (((jlong)bytes[5]) << 16)
192          | (((jlong)bytes[6]) << 8)
193          | (((jlong)bytes[7]) << 0);
194   }
195
196   /* read a 8-byte unit */
197   inline jlong read8 ()
198   {
199     skip (8);  
200     return get8 (bytes+pos-8);
201   }
202
203   inline void check_tag (int index, char expected_tag)
204   {
205     if (index < 0
206         || index > pool_count
207         || tags[index] != expected_tag)
208       throw_class_format_error ("erroneous constant pool tag");
209   }
210
211   inline void verify_identifier (_Jv_Utf8Const* name)
212   {
213     if (! _Jv_VerifyIdentifier (name))
214       throw_class_format_error ("erroneous identifier");
215   }
216
217   inline void verify_classname (unsigned char* ptr, _Jv_ushort length)
218   {
219     if (! _Jv_VerifyClassName (ptr, length))
220       throw_class_format_error ("erroneous class name");
221   }
222
223   inline void verify_classname (_Jv_Utf8Const *name)
224   {
225     if (! _Jv_VerifyClassName (name))
226       throw_class_format_error ("erroneous class name");
227   }
228
229   inline void verify_field_signature (_Jv_Utf8Const *sig)
230   {
231     if (! _Jv_VerifyFieldSignature (sig))
232       throw_class_format_error ("erroneous type descriptor");
233   }
234
235   inline void verify_method_signature (_Jv_Utf8Const *sig)
236   {
237     if (! _Jv_VerifyMethodSignature (sig))
238       throw_class_format_error ("erroneous type descriptor");
239   }
240
241   ::java::io::DataOutputStream *get_reflection_stream ()
242   {
243     if (reflection_data == NULL)
244       {
245         reflection_data = new ::java::io::ByteArrayOutputStream();
246         data_stream = new ::java::io::DataOutputStream(reflection_data);
247       }
248     return data_stream;
249   }
250
251   _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length,
252                    java::security::ProtectionDomain *pd,
253                    _Jv_Utf8Const **name_result)
254   {
255     if (klass == 0 || length < 0 || offset+length > data->length)
256       throw_internal_error ("arguments to _Jv_DefineClass");
257
258     verify = true;
259     input_data = data;
260     input_offset = offset;
261     bytes  = (unsigned char*) (elements (data)+offset);
262     len    = length;
263     pos    = 0;
264     is_15  = false;
265
266     def    = klass;
267     found_name = name_result;
268     reflection_data = NULL;
269     data_stream = NULL;
270
271     def->size_in_bytes = -1;
272     def->vtable_method_count = -1;
273     def->engine = &_Jv_soleInterpreterEngine;
274     def->protectionDomain = pd;
275   }
276
277   /** and here goes the parser members defined out-of-line */
278   void parse ();
279   void read_constpool ();
280   void prepare_pool_entry (int index, unsigned char tag,
281                            bool rewrite = true);
282   void read_fields ();
283   void read_methods ();
284   void read_one_class_attribute ();
285   void read_one_method_attribute (int method);
286   void read_one_code_attribute (int method);
287   void read_one_field_attribute (int field, bool *);
288   void throw_class_format_error (const char *msg);
289
290   void handleEnclosingMethod(int);
291   void handleGenericSignature(jv_attr_type, unsigned short, int);
292   void handleAnnotationElement();
293   void handleAnnotation();
294   void handleAnnotations();
295   void handleMemberAnnotations(jv_attr_type, int, int);
296   void handleAnnotationDefault(int, int);
297   void handleParameterAnnotations(int, int);
298   void finish_reflection_data ();
299
300   /** check an utf8 entry, without creating a Utf8Const object */
301   bool is_attribute_name (int index, const char *name);
302   
303   /** return the value of a utf8 entry in the passed array */
304   int pool_Utf8_to_char_arr (int index, char **entry);
305
306   /** here goes the class-loader members defined out-of-line */
307   void handleConstantPool ();
308   void handleClassBegin (int, int, int);
309   void handleInterfacesBegin (int);
310   void handleInterface (int, int);
311   void handleFieldsBegin (int);
312   void handleField (int, int, int, int, int *);
313   void handleConstantValueAttribute (int, int, bool *);
314   void handleMethodsBegin (int);
315   void handleMethod (int, int, int, int);
316   void handleMethodsEnd ();
317   void handleCodeAttribute (int, int, int, int, int, int);
318   void handleExceptionTableEntry (int, int, int, int, int, int);
319
320   void checkExtends (jclass sub, jclass super);
321   void checkImplements (jclass sub, jclass super);
322
323   /*
324    * FIXME: we should keep a hash table of utf8-strings, since many will
325    * be the same.  It's a little tricky, however, because the hash table
326    * needs to interact gracefully with the garbage collector.  Much
327    * memory is to be saved by this, however!  perhaps the improvement
328    * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
329    * computes the hash value anyway.
330    */
331 };
332
333 // Note that *NAME_RESULT will only be set if the class is registered
334 // with the class loader.  This is how the caller can know whether
335 // unregistration is require.
336 void
337 _Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length,
338                  java::security::ProtectionDomain *pd,
339                  _Jv_Utf8Const **name_result)
340 {
341   _Jv_ClassReader reader (klass, data, offset, length, pd, name_result);
342   reader.parse();
343
344   /* that's it! */
345 }
346
347 \f
348 /** This section defines the parsing/scanning of the class data */
349
350 // Major and minor version numbers for various releases.
351 #define MAJOR_1_1 45
352 #define MINOR_1_1  3
353 #define MAJOR_1_2 46
354 #define MINOR_1_2  0
355 #define MAJOR_1_3 47
356 #define MINOR_1_3  0
357 #define MAJOR_1_4 48
358 #define MINOR_1_4  0
359 #define MAJOR_1_5 49
360 #define MINOR_1_5  0
361
362 void
363 _Jv_ClassReader::parse ()
364 {
365   int magic = read4 ();
366   if (magic != (int) 0xCAFEBABE)
367     throw_class_format_error ("bad magic number");
368
369   int minor_version = read2u ();
370   int major_version = read2u ();
371   if (major_version < MAJOR_1_1 || major_version > MAJOR_1_5
372       || (major_version == MAJOR_1_5 && minor_version > MINOR_1_5))
373     throw_class_format_error ("unrecognized class file version");
374   is_15 = (major_version == MAJOR_1_5);
375
376   pool_count = read2u ();
377
378   read_constpool ();
379
380   int access_flags = read2u ();
381   int this_class = read2u ();
382   int super_class = read2u ();
383
384   check_tag (this_class, JV_CONSTANT_Class);
385   if (super_class != 0) 
386     check_tag (super_class, JV_CONSTANT_Class);
387
388   handleClassBegin (access_flags, this_class, super_class);
389
390   // Allocate our aux_info here, after the name is set, to fulfill our
391   // contract with the collector interface.
392   def->aux_info = (void *) _Jv_AllocRawObj (sizeof (_Jv_InterpClass));
393   def_interp = (_Jv_InterpClass *) def->aux_info;
394
395   int interfaces_count = read2u (); 
396
397   handleInterfacesBegin (interfaces_count);
398
399   for (int i = 0; i < interfaces_count; i++)
400     {
401       int iface = read2u ();
402       check_tag (iface, JV_CONSTANT_Class);
403       handleInterface (i, iface);
404     }
405   
406   read_fields ();
407   read_methods ();
408   
409   int attributes_count = read2u ();
410   
411   for (int i = 0; i < attributes_count; i++)
412     {
413       read_one_class_attribute ();
414     }
415
416   if (pos != len)
417     throw_class_format_error ("unused data before end of file");
418
419   finish_reflection_data ();
420
421   // Tell everyone we're done.
422   def->state = JV_STATE_READ;
423   if (gcj::verbose_class_flag)
424     _Jv_Linker::print_class_loaded (def);
425   ++gcj::loadedClasses;
426   def->notifyAll ();
427 }
428
429 void
430 _Jv_ClassReader::finish_reflection_data ()
431 {
432   if (data_stream == NULL)
433     return;
434   data_stream->writeByte(JV_DONE_ATTR);
435   data_stream->flush();
436   int nbytes = reflection_data->count;
437   unsigned char *new_bytes = (unsigned char *) _Jv_AllocBytes (nbytes);
438   memcpy (new_bytes, elements (reflection_data->buf), nbytes);
439   def->reflection_data = new_bytes;
440 }
441
442 void
443 _Jv_ClassReader::handleEnclosingMethod (int len)
444 {
445   if (len != 4)
446     throw_class_format_error ("invalid EnclosingMethod attribute");
447   // FIXME: only allow one...
448
449   int class_index = read2u();
450   check_tag (class_index, JV_CONSTANT_Class);
451   prepare_pool_entry (class_index, JV_CONSTANT_Class);
452
453   int method_index = read2u();
454   // Zero is ok and means no enclosing method.
455   if (method_index != 0)
456     {
457       check_tag (method_index, JV_CONSTANT_NameAndType);
458       prepare_pool_entry (method_index, JV_CONSTANT_NameAndType);
459     }
460
461   ::java::io::DataOutputStream *stream = get_reflection_stream ();
462   stream->writeByte(JV_CLASS_ATTR);
463   stream->writeInt(5);
464   stream->writeByte(JV_ENCLOSING_METHOD_KIND);
465   stream->writeShort(class_index);
466   stream->writeShort(method_index);
467 }
468
469 void
470 _Jv_ClassReader::handleGenericSignature (jv_attr_type type,
471                                          unsigned short index,
472                                          int len)
473 {
474   if (len != 2)
475     throw_class_format_error ("invalid Signature attribute");
476
477   int cpool_idx = read2u();
478   check_tag (cpool_idx, JV_CONSTANT_Utf8);
479   prepare_pool_entry (cpool_idx, JV_CONSTANT_Utf8, false);
480
481   ::java::io::DataOutputStream *stream = get_reflection_stream ();
482   stream->writeByte(type);
483   int attrlen = 3;
484   if (type != JV_CLASS_ATTR)
485     attrlen += 2;
486   stream->writeInt(attrlen);
487   if (type != JV_CLASS_ATTR)
488     stream->writeShort(index);
489   stream->writeByte(JV_SIGNATURE_KIND);
490   stream->writeShort(cpool_idx);
491 }
492
493 void
494 _Jv_ClassReader::handleAnnotationElement()
495 {
496   int tag = read1u();
497   switch (tag)
498     {
499     case 'B':
500     case 'C':
501     case 'S':
502     case 'Z':
503     case 'I':
504       {
505         int index = read2u();
506         check_tag (index, JV_CONSTANT_Integer);
507         prepare_pool_entry (index, JV_CONSTANT_Integer);
508       }
509       break;
510     case 'D':
511       {
512         int index = read2u();
513         check_tag (index, JV_CONSTANT_Double);
514         prepare_pool_entry (index, JV_CONSTANT_Double);
515       }
516       break;
517     case 'F':
518       {
519         int index = read2u();
520         check_tag (index, JV_CONSTANT_Float);
521         prepare_pool_entry (index, JV_CONSTANT_Float);
522       }
523       break;
524     case 'J':
525       {
526         int index = read2u();
527         check_tag (index, JV_CONSTANT_Long);
528         prepare_pool_entry (index, JV_CONSTANT_Long);
529       }
530       break;
531     case 's':
532       {
533         int index = read2u();
534         // Despite what the JVM spec says, compilers generate a Utf8
535         // constant here, not a String.
536         check_tag (index, JV_CONSTANT_Utf8);
537         prepare_pool_entry (index, JV_CONSTANT_Utf8, false);
538       }
539       break;
540
541     case 'e':
542       {
543         int type_name_index = read2u();
544         int const_name_index = read2u ();
545         check_tag (type_name_index, JV_CONSTANT_Utf8);
546         prepare_pool_entry (type_name_index, JV_CONSTANT_Utf8);
547         check_tag (const_name_index, JV_CONSTANT_Utf8);
548         prepare_pool_entry (const_name_index, JV_CONSTANT_Utf8, false);
549       }
550       break;
551     case 'c':
552       {
553         int index = read2u();
554         check_tag (index, JV_CONSTANT_Utf8);
555         prepare_pool_entry (index, JV_CONSTANT_Utf8);
556       }
557       break;
558     case '@':
559       handleAnnotation();
560       break;
561     case '[':
562       {
563         int n_array_elts = read2u ();
564         for (int i = 0; i < n_array_elts; ++i)
565           handleAnnotationElement();
566       }
567       break;
568     default:
569       throw_class_format_error ("invalid annotation element");
570     }
571 }
572
573 void
574 _Jv_ClassReader::handleAnnotation()
575 {
576   int type_index = read2u();
577   check_tag (type_index, JV_CONSTANT_Utf8);
578   prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
579
580   int npairs = read2u();
581   for (int i = 0; i < npairs; ++i)
582     {
583       int name_index = read2u();
584       check_tag (name_index, JV_CONSTANT_Utf8);
585       prepare_pool_entry (name_index, JV_CONSTANT_Utf8, false);
586       handleAnnotationElement();
587     }
588 }
589
590 void
591 _Jv_ClassReader::handleAnnotations()
592 {
593   int num = read2u();
594   while (num--)
595     handleAnnotation();
596 }
597
598 void
599 _Jv_ClassReader::handleMemberAnnotations(jv_attr_type member_type,
600                                          int member_index,
601                                          int len)
602 {
603   // We're going to copy the bytes in verbatim.  But first we want to
604   // make sure the attribute is well-formed, and we want to prepare
605   // the constant pool.  So, we save our starting point.
606   int orig_pos = pos;
607
608   handleAnnotations();
609   // FIXME: check that we read all LEN bytes?
610
611   ::java::io::DataOutputStream *stream = get_reflection_stream ();
612   stream->writeByte(member_type);
613   int newLen = len + 1;
614   if (member_type != JV_CLASS_ATTR)
615     newLen += 2;
616   stream->writeInt(newLen);
617   stream->writeByte(JV_ANNOTATIONS_KIND);
618   if (member_type != JV_CLASS_ATTR)
619     stream->writeShort(member_index);
620   // Write the data as-is.
621   stream->write(input_data, input_offset + orig_pos, len);
622 }
623
624 void
625 _Jv_ClassReader::handleAnnotationDefault(int member_index, int len)
626 {
627   int orig_pos = pos;
628   handleAnnotationElement();
629
630   ::java::io::DataOutputStream *stream = get_reflection_stream ();
631   stream->writeByte(JV_METHOD_ATTR);
632   stream->writeInt(len + 3);
633   stream->writeByte(JV_ANNOTATION_DEFAULT_KIND);
634   stream->writeShort(member_index);
635   stream->write(input_data, input_offset + orig_pos, len);
636 }
637
638 void
639 _Jv_ClassReader::handleParameterAnnotations(int member_index, int len)
640 {
641   int orig_pos = pos;
642
643   int n_params = read1u();
644   for (int i = 0; i < n_params; ++i)
645     handleAnnotations();
646
647   ::java::io::DataOutputStream *stream = get_reflection_stream ();
648   stream->writeByte(JV_METHOD_ATTR);
649   stream->writeInt(len + 3);
650   stream->writeByte(JV_PARAMETER_ANNOTATIONS_KIND);
651   stream->writeShort(member_index);
652   stream->write(input_data, input_offset + orig_pos, len);
653 }
654
655 void _Jv_ClassReader::read_constpool ()
656 {
657   tags    = (unsigned char*) _Jv_AllocBytes (pool_count);
658   offsets = (unsigned int *) _Jv_AllocBytes (sizeof (int) * pool_count) ;
659
660   /** first, we scan the constant pool, collecting tags and offsets */
661   tags[0]   = JV_CONSTANT_Undefined;
662   offsets[0] = pos;
663   for (int c = 1; c < pool_count; c++)
664     {
665       tags[c]    = read1u ();
666       offsets[c] = pos;
667
668       switch (tags[c])
669         {
670         case JV_CONSTANT_String:
671         case JV_CONSTANT_Class:
672           skip (2);
673           break;
674
675         case JV_CONSTANT_Fieldref:
676         case JV_CONSTANT_Methodref:
677         case JV_CONSTANT_InterfaceMethodref:
678         case JV_CONSTANT_NameAndType:
679         case JV_CONSTANT_Integer:
680         case JV_CONSTANT_Float:
681           skip (4);
682           break;
683
684         case JV_CONSTANT_Double:
685         case JV_CONSTANT_Long:
686           skip (8);
687           tags[++c] = JV_CONSTANT_Undefined;
688           break;
689             
690         case JV_CONSTANT_Utf8:
691           {                 
692             int len = read2u ();
693             skip (len);
694           }
695           break;
696
697         case JV_CONSTANT_Unicode:
698           throw_class_format_error ("unicode not supported");
699           break;
700
701         default:
702           throw_class_format_error ("erroneous constant pool tag");
703         }
704     }
705
706   handleConstantPool ();
707 }
708
709
710 void _Jv_ClassReader::read_fields ()
711 {
712   int fields_count = read2u ();
713   handleFieldsBegin (fields_count);
714
715   // We want to sort the fields so that static fields come first,
716   // followed by instance fields.  We do this before parsing the
717   // fields so that we can have the new indices available when
718   // creating the annotation data structures.
719
720   // Allocate this on the heap in case there are a large number of
721   // fields.
722   int *fieldmap = (int *) _Jv_AllocBytes (fields_count * sizeof (int));
723   int save_pos = pos;
724   int static_count = 0, instance_count = -1;
725   for (int i = 0; i < fields_count; ++i)
726     {
727       using namespace java::lang::reflect;
728
729       int access_flags = read2u ();
730       skip (4);
731       int attributes_count = read2u ();
732
733       if ((access_flags & Modifier::STATIC) != 0) 
734         fieldmap[i] = static_count++;
735       else
736         fieldmap[i] = instance_count--;
737
738       for (int j = 0; j < attributes_count; ++j)
739         {
740           skip (2);
741           int length = read4 ();
742           skip (length);
743         }
744     }
745   pos = save_pos;
746
747   // In the loop above, instance fields are represented by negative
748   // numbers.  Here we rewrite these to be proper offsets.
749   for (int i = 0; i < fields_count; ++i)
750     {
751       if (fieldmap[i] < 0)
752         fieldmap[i] = static_count - 1 - fieldmap[i];
753     }
754   def->static_field_count = static_count;
755
756   for (int i = 0; i < fields_count; i++)
757     {
758       int access_flags     = read2u ();
759       int name_index       = read2u ();
760       int descriptor_index = read2u ();
761       int attributes_count = read2u ();
762
763       check_tag (name_index, JV_CONSTANT_Utf8);
764       prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
765
766       check_tag (descriptor_index, JV_CONSTANT_Utf8);
767       prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
768
769       handleField (i, access_flags, name_index, descriptor_index, fieldmap);
770
771       bool found_value = false;
772       for (int j = 0; j < attributes_count; j++)
773         {
774           read_one_field_attribute (fieldmap[i], &found_value);
775         }
776     }
777 }
778
779 bool
780 _Jv_ClassReader::is_attribute_name (int index, const char *name)
781 {
782   check_tag (index, JV_CONSTANT_Utf8);
783   int len = get2u (bytes+offsets[index]);
784   if (len != (int) strlen (name))
785     return false;
786   else
787     return !memcmp (bytes+offsets[index]+2, name, len);
788 }
789
790 // Get a UTF8 value from the constant pool and turn it into a garbage
791 // collected char array.
792 int _Jv_ClassReader::pool_Utf8_to_char_arr (int index, char** entry)
793 {
794   check_tag (index, JV_CONSTANT_Utf8);
795   int len = get2u (bytes + offsets[index]);
796   *entry = reinterpret_cast<char *> (_Jv_AllocBytes (len + 1));
797   (*entry)[len] = '\0';
798   memcpy (*entry, bytes + offsets[index] + 2, len);
799   return len + 1;
800 }
801
802 void _Jv_ClassReader::read_one_field_attribute (int field_index,
803                                                 bool *found_value)
804 {
805   int name = read2u ();
806   int length = read4 ();
807
808   if (is_attribute_name (name, "ConstantValue"))
809     {
810       int cv = read2u ();
811
812       if (cv < pool_count 
813           && cv > 0
814           && (tags[cv] == JV_CONSTANT_Integer
815               || tags[cv] == JV_CONSTANT_Float
816               || tags[cv] == JV_CONSTANT_Long
817               || tags[cv] == JV_CONSTANT_Double
818               || tags[cv] == JV_CONSTANT_String))
819         {
820           handleConstantValueAttribute (field_index, cv, found_value);
821         }
822       else
823         {
824           throw_class_format_error ("erroneous ConstantValue attribute");
825         }
826
827       if (length != 2) 
828         throw_class_format_error ("erroneous ConstantValue attribute");
829     }
830   else if (is_attribute_name (name, "Signature"))
831     handleGenericSignature(JV_FIELD_ATTR, field_index, length);
832   else if (is_attribute_name (name, "RuntimeVisibleAnnotations"))
833     handleMemberAnnotations(JV_FIELD_ATTR, field_index, length);
834   else
835     skip (length);
836 }
837
838 void _Jv_ClassReader::read_methods ()
839 {
840   int methods_count = read2u ();
841   
842   handleMethodsBegin (methods_count);
843   
844   for (int i = 0; i < methods_count; i++)
845     {
846       int access_flags     = read2u ();
847       int name_index       = read2u ();
848       int descriptor_index = read2u ();
849       int attributes_count = read2u ();
850       
851       check_tag (name_index, JV_CONSTANT_Utf8);
852       prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
853
854       check_tag (descriptor_index, JV_CONSTANT_Utf8);
855       prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
856
857       handleMethod (i, access_flags, name_index,
858                     descriptor_index);
859
860       for (int j = 0; j < attributes_count; j++)
861         {
862           read_one_method_attribute (i);
863         }
864     }
865   
866   handleMethodsEnd ();
867 }
868
869 void _Jv_ClassReader::read_one_method_attribute (int method_index) 
870 {
871   int name = read2u ();
872   int length = read4 ();
873
874   if (is_attribute_name (name, "Exceptions"))
875     {
876       _Jv_Method *method = reinterpret_cast<_Jv_Method *>
877         (&def->methods[method_index]);
878       if (method->throws != NULL)
879         throw_class_format_error ("only one Exceptions attribute allowed per method");
880
881       int num_exceptions = read2u ();
882       _Jv_Utf8Const **exceptions =
883         (_Jv_Utf8Const **) _Jv_AllocBytes ((num_exceptions + 1)
884                                            * sizeof (_Jv_Utf8Const *));
885
886       int out = 0;
887       _Jv_word *pool_data = def->constants.data;
888       for (int i = 0; i < num_exceptions; ++i)
889         {
890           int ndx = read2u ();
891           // JLS 2nd Ed. 4.7.5 requires that the tag not be 0.
892           if (ndx != 0)
893             {
894               check_tag (ndx, JV_CONSTANT_Class);
895               exceptions[out++] = pool_data[ndx].utf8; 
896             }
897         }
898       exceptions[out] = NULL;
899       method->throws = exceptions;
900     }
901
902   else if (is_attribute_name (name, "Code"))
903     {
904       int start_off = pos;
905       int max_stack = read2u ();
906       int max_locals = read2u ();
907       int code_length = read4 ();
908
909       int code_start = pos;
910       skip (code_length);
911       int exception_table_length = read2u ();
912
913       handleCodeAttribute (method_index, 
914                            max_stack, max_locals,
915                            code_start, code_length,
916                            exception_table_length);
917       
918
919       for (int i = 0; i < exception_table_length; i++)
920         {
921           int start_pc   = read2u ();
922           int end_pc     = read2u ();
923           int handler_pc = read2u ();
924           int catch_type = read2u ();
925
926           if (start_pc > end_pc
927               || start_pc < 0
928               // END_PC can be equal to CODE_LENGTH.
929               // See JVM Spec 4.7.4.
930               || end_pc > code_length
931               || handler_pc >= code_length)
932             throw_class_format_error ("erroneous exception handler info");
933
934           if (! (tags[catch_type] == JV_CONSTANT_Class
935                  || tags[catch_type] == 0))
936             {
937               throw_class_format_error ("erroneous exception handler info");
938             }
939
940           handleExceptionTableEntry (method_index,
941                                      i,
942                                      start_pc,
943                                      end_pc,
944                                      handler_pc, 
945                                      catch_type);
946
947         }
948
949       int attributes_count = read2u ();
950
951       for (int i = 0; i < attributes_count; i++)
952         {
953           read_one_code_attribute (method_index);
954         }
955
956       if ((pos - start_off) != length)
957         throw_class_format_error ("code attribute too short");
958     }
959   else if (is_attribute_name (name, "Signature"))
960     handleGenericSignature(JV_METHOD_ATTR, method_index, length);
961   else if (is_attribute_name (name, "RuntimeVisibleAnnotations"))
962     handleMemberAnnotations(JV_METHOD_ATTR, method_index, length);
963   else if (is_attribute_name (name, "RuntimeVisibleParameterAnnotations"))
964     handleParameterAnnotations(method_index, length);
965   else if (is_attribute_name (name, "AnnotationDefault"))
966     handleAnnotationDefault(method_index, length);
967   else
968     {
969       /* ignore unknown attributes */
970       skip (length);
971     }
972 }
973
974 void _Jv_ClassReader::read_one_code_attribute (int method_index) 
975 {
976   int name = read2u ();
977   int length = read4 ();
978   if (is_attribute_name (name, "LineNumberTable"))
979     {
980       _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
981         (def_interp->interpreted_methods[method_index]);
982       if (method->line_table != NULL)
983         throw_class_format_error ("Method already has LineNumberTable");
984
985       int table_len = read2u ();
986       _Jv_LineTableEntry* table
987         = (_Jv_LineTableEntry *) _Jv_AllocBytes (table_len
988                                                  * sizeof (_Jv_LineTableEntry));
989       for (int i = 0; i < table_len; i++)
990        {
991          table[i].bytecode_pc = read2u ();
992          table[i].line = read2u ();
993        }
994       method->line_table_len = table_len;
995       method->line_table = table;
996     }
997   else if (is_attribute_name (name, "LocalVariableTable"))
998     {
999       _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
1000                                (def_interp->interpreted_methods[method_index]);
1001       if (method->local_var_table != NULL)
1002         throw_class_format_error ("Method already has LocalVariableTable");
1003         
1004       int table_len = read2u ();
1005       _Jv_LocalVarTableEntry *table 
1006         = reinterpret_cast<_Jv_LocalVarTableEntry *>
1007             (_Jv_AllocRawObj (table_len * sizeof (_Jv_LocalVarTableEntry)));
1008                                
1009       for (int i = 0; i < table_len; i++)
1010         {
1011           table[i].bytecode_start_pc = read2u ();
1012           table[i].length = read2u ();
1013           int len;
1014           len = pool_Utf8_to_char_arr (read2u (), &table[i].name);
1015           len = pool_Utf8_to_char_arr (read2u (), &table[i].descriptor);
1016           table[i].slot = read2u ();
1017           
1018           if (table[i].slot > method->max_locals || table[i].slot < 0)
1019             throw_class_format_error ("Malformed Local Variable Table: Invalid Slot");
1020         }
1021             
1022       method->local_var_table_len = table_len;
1023       method->local_var_table = table;
1024     }
1025   else
1026     {
1027       /* ignore unknown code attributes */
1028       skip (length);
1029     }
1030 }
1031
1032 void _Jv_ClassReader::read_one_class_attribute () 
1033 {
1034   int name = read2u ();
1035   int length = read4 ();
1036   if (is_attribute_name (name, "SourceFile"))
1037     {
1038       int source_index = read2u ();
1039       check_tag (source_index, JV_CONSTANT_Utf8);
1040       prepare_pool_entry (source_index, JV_CONSTANT_Utf8, false);
1041       def_interp->source_file_name = _Jv_NewStringUtf8Const
1042         (def->constants.data[source_index].utf8);
1043     }
1044   else if (is_attribute_name (name, "Signature"))
1045     handleGenericSignature(JV_CLASS_ATTR, 0, length);
1046   else if (is_attribute_name (name, "EnclosingMethod"))
1047     handleEnclosingMethod(length);
1048   else if (is_attribute_name (name, "RuntimeVisibleAnnotations"))
1049     handleMemberAnnotations(JV_CLASS_ATTR, 0, length);
1050   else if (is_attribute_name (name, "InnerClasses"))
1051     {
1052       ::java::io::DataOutputStream *stream = get_reflection_stream ();
1053       stream->writeByte(JV_CLASS_ATTR);
1054       stream->writeInt(length + 1);
1055       stream->writeByte(JV_INNER_CLASSES_KIND);
1056       stream->write(input_data, input_offset + pos, length);
1057       skip (length);
1058     }
1059   else
1060     {
1061       /* Currently, we ignore most class attributes. */
1062      skip (length);
1063     }
1064 }
1065
1066
1067
1068 \f
1069 /* this section defines the semantic actions of the parser */
1070
1071 void _Jv_ClassReader::handleConstantPool ()
1072 {
1073   /** now, we actually define the class' constant pool */
1074
1075   jbyte *pool_tags = (jbyte*) _Jv_AllocBytes (pool_count);
1076   _Jv_word *pool_data
1077     = (_Jv_word*) _Jv_AllocRawObj (pool_count * sizeof (_Jv_word));
1078
1079   def->constants.tags = pool_tags;
1080   def->constants.data = pool_data;
1081   def->constants.size = pool_count;
1082
1083   // Here we make a pass to collect the strings!   We do this, because
1084   // internally in the GCJ runtime, classes are encoded with .'s not /'s. 
1085   // Therefore, we first collect the strings, and then translate the rest
1086   // of the utf8-entries (thus not representing strings) from /-notation
1087   // to .-notation.
1088   for (int i = 1; i < pool_count; i++)
1089     {
1090       if (tags[i] == JV_CONSTANT_String)
1091         {
1092           unsigned char* str_data = bytes + offsets [i];
1093           int utf_index = get2u (str_data);
1094           check_tag (utf_index, JV_CONSTANT_Utf8);
1095           unsigned char *utf_data = bytes + offsets[utf_index];
1096           int len = get2u (utf_data);
1097           pool_data[i].utf8 = _Jv_makeUtf8Const ((char*)(utf_data+2), len);
1098           pool_tags[i] = JV_CONSTANT_String;
1099         }
1100       else
1101         {
1102           pool_tags[i] = JV_CONSTANT_Undefined;
1103         }
1104     }
1105
1106   // and now, we scan everything else but strings & utf8-entries.  This
1107   // leaves out those utf8-entries which are not used; which will be left
1108   // with a tag of JV_CONSTANT_Undefined in the class definition.
1109   for (int index = 1; index < pool_count; index++)
1110     {
1111       switch (tags[index])
1112         {
1113         case JV_CONSTANT_Undefined:
1114         case JV_CONSTANT_String:
1115         case JV_CONSTANT_Utf8:
1116           continue;
1117           
1118         default:
1119           prepare_pool_entry (index, tags[index]);
1120         }
1121     }  
1122   
1123 }
1124
1125 /* this is a recursive procedure, which will prepare pool entries as needed.
1126    Which is how we avoid initializing those entries which go unused. 
1127    
1128    REWRITE is true iff this pool entry is the Utf8 representation of a
1129    class name or a signature.
1130 */
1131
1132 void
1133 _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag,
1134                                      bool rewrite)
1135 {
1136   /* these two, pool_data and pool_tags, point into the class
1137      structure we are currently defining */
1138
1139   unsigned char *pool_tags = (unsigned char*) def->constants.tags;
1140   _Jv_word      *pool_data = def->constants.data;
1141
1142   /* this entry was already prepared */
1143   if (pool_tags[index] == this_tag)
1144     return;
1145
1146   /* this_data points to the constant-pool information for the current
1147      constant-pool entry */
1148
1149   unsigned char *this_data = bytes + offsets[index];
1150
1151   switch (this_tag)
1152     {
1153     case JV_CONSTANT_Utf8: 
1154       {
1155         int len = get2u (this_data);
1156         char *s = ((char*) this_data)+2;
1157         pool_tags[index] = JV_CONSTANT_Utf8;
1158
1159         if (! rewrite)
1160           {
1161             pool_data[index].utf8 = _Jv_makeUtf8Const (s, len);
1162             break;
1163           }
1164
1165         // If REWRITE is set, it is because some other tag needs this
1166         // utf8-entry for type information: it is a class or a
1167         // signature.  Thus, we translate /'s to .'s in order to
1168         // accomondate gcj's internal representation.
1169         char *buffer = (char*) __builtin_alloca (len);
1170         for (int i = 0; i < len; i++)
1171           {
1172             if (s[i] == '/')
1173               buffer[i] = '.';
1174             else
1175               buffer[i] = s[i];
1176           }
1177         pool_data[index].utf8 = _Jv_makeUtf8Const (buffer, len);
1178       }
1179       break;
1180             
1181     case JV_CONSTANT_Class:      
1182       {
1183         int utf_index = get2u (this_data);
1184         check_tag (utf_index, JV_CONSTANT_Utf8);
1185         prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
1186
1187         if (verify)
1188           verify_classname (pool_data[utf_index].utf8);
1189                 
1190         pool_data[index].utf8 = pool_data[utf_index].utf8;
1191         pool_tags[index] = JV_CONSTANT_Class;
1192       }
1193       break;
1194             
1195     case JV_CONSTANT_String:
1196       // already handled before... 
1197       break;
1198             
1199     case JV_CONSTANT_Fieldref:
1200     case JV_CONSTANT_Methodref:
1201     case JV_CONSTANT_InterfaceMethodref:
1202       {
1203         int class_index = get2u (this_data);
1204         int nat_index = get2u (this_data+2);
1205
1206         check_tag (class_index, JV_CONSTANT_Class);
1207         prepare_pool_entry (class_index, JV_CONSTANT_Class);        
1208
1209         check_tag (nat_index, JV_CONSTANT_NameAndType);
1210         prepare_pool_entry (nat_index, JV_CONSTANT_NameAndType);
1211
1212         // here, verify the signature and identifier name
1213         if (verify)
1214         {
1215           _Jv_ushort name_index, type_index;
1216           _Jv_loadIndexes (&pool_data[nat_index],
1217                            name_index, type_index);
1218
1219           if (this_tag == JV_CONSTANT_Fieldref)
1220             verify_field_signature (pool_data[type_index].utf8);
1221           else
1222             verify_method_signature (pool_data[type_index].utf8);
1223
1224           _Jv_Utf8Const* name = pool_data[name_index].utf8;
1225
1226           if (this_tag != JV_CONSTANT_Fieldref
1227               && (   _Jv_equalUtf8Consts (name, clinit_name)
1228                   || _Jv_equalUtf8Consts (name, init_name)))
1229             /* ignore */;
1230           else
1231             verify_identifier (pool_data[name_index].utf8);
1232         }
1233             
1234         _Jv_storeIndexes (&pool_data[index], class_index, nat_index);
1235         pool_tags[index] = this_tag;
1236       }
1237       break;
1238             
1239     case JV_CONSTANT_NameAndType:
1240       {
1241         _Jv_ushort name_index = get2u (this_data);
1242         _Jv_ushort type_index = get2u (this_data+2);
1243
1244         check_tag (name_index, JV_CONSTANT_Utf8);
1245         prepare_pool_entry (name_index, JV_CONSTANT_Utf8, false);
1246         check_tag (type_index, JV_CONSTANT_Utf8);
1247         prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
1248
1249         _Jv_storeIndexes (&pool_data[index], name_index, type_index);
1250         pool_tags[index] = JV_CONSTANT_NameAndType;
1251       }
1252       break;
1253             
1254     case JV_CONSTANT_Float:
1255       {
1256         jfloat f = java::lang::Float::intBitsToFloat ((jint) get4 (this_data));
1257         _Jv_storeFloat (&pool_data[index], f);
1258         pool_tags[index] = JV_CONSTANT_Float;
1259       }
1260       break;
1261             
1262     case JV_CONSTANT_Integer:
1263       {
1264         int i = get4 (this_data);
1265         _Jv_storeInt (&pool_data[index], i);
1266         pool_tags[index] = JV_CONSTANT_Integer;
1267       }
1268       break;
1269             
1270     case JV_CONSTANT_Double:
1271       {
1272         jdouble d
1273           = java::lang::Double::longBitsToDouble ((jlong) get8 (this_data));
1274         _Jv_storeDouble (&pool_data[index], d);
1275         pool_tags[index] = JV_CONSTANT_Double;
1276       }
1277       break;
1278             
1279     case JV_CONSTANT_Long:
1280       {
1281         jlong i = get8 (this_data);
1282         _Jv_storeLong (&pool_data[index], i);
1283         pool_tags[index] = JV_CONSTANT_Long;
1284       }
1285       break;
1286             
1287     default:
1288       throw_class_format_error ("erroneous constant pool tag");
1289     }
1290 }
1291
1292
1293 void
1294 _Jv_ClassReader::handleClassBegin (int access_flags, int this_class, int super_class)
1295 {
1296   using namespace java::lang::reflect;
1297
1298   unsigned char *pool_tags = (unsigned char*) def->constants.tags;
1299   _Jv_word      *pool_data = def->constants.data;
1300
1301   check_tag (this_class, JV_CONSTANT_Class);
1302   _Jv_Utf8Const *loadedName = pool_data[this_class].utf8;
1303
1304   // was ClassLoader.defineClass called with an expected class name?
1305   if (def->name == 0)
1306     {
1307       jclass orig = def->loader->findLoadedClass(loadedName->toString());
1308
1309       if (orig == 0)
1310         {
1311           def->name = loadedName;
1312         }
1313       else
1314         {
1315           jstring msg = JvNewStringUTF ("anonymous "
1316                                         "class data denotes "
1317                                         "existing class ");
1318           msg = msg->concat (orig->getName ());
1319
1320           throw_no_class_def_found_error (msg);
1321         }
1322     }
1323
1324   // assert that the loaded class has the expected name, 5.3.5
1325   else if (! _Jv_equalUtf8Consts (loadedName, def->name))
1326     {
1327       jstring msg = JvNewStringUTF ("loaded class ");
1328       msg = msg->concat (def->getName ());
1329       msg = msg->concat (_Jv_NewStringUTF (" was in fact named "));
1330       jstring klass_name = loadedName->toString();
1331       msg = msg->concat (klass_name);
1332
1333       throw_no_class_def_found_error (msg);
1334     }
1335
1336   def->accflags = access_flags | java::lang::reflect::Modifier::INTERPRETED;
1337   pool_data[this_class].clazz = def;
1338   pool_tags[this_class] = JV_CONSTANT_ResolvedClass;
1339
1340   if (super_class == 0)
1341     {
1342       // Note that this is ok if we are defining java.lang.Object.
1343       // But there is no way to have this class be interpreted.
1344       throw_class_format_error ("no superclass reference");
1345     }
1346
1347   def->state = JV_STATE_PRELOADING;
1348
1349   // Register this class with its defining loader as well (despite the
1350   // name of the function we're calling), so that super class lookups
1351   // work properly.  If there is an error, our caller will unregister
1352   // this class from the class loader.  Also, we don't need to hold a
1353   // lock here, as our caller has acquired it.
1354   _Jv_RegisterInitiatingLoader (def, def->loader);
1355
1356   // Note that we found a name so that unregistration can happen if
1357   // needed.
1358   *found_name = def->name;
1359
1360   if (super_class != 0)
1361     {
1362       // Load the superclass.
1363       check_tag (super_class, JV_CONSTANT_Class);
1364       _Jv_Utf8Const* super_name = pool_data[super_class].utf8; 
1365
1366       // Load the superclass using our defining loader.
1367       jclass the_super = _Jv_FindClass (super_name, def->loader);
1368
1369       // This will establish that we are allowed to be a subclass,
1370       // and check for class circularity error.
1371       checkExtends (def, the_super);
1372
1373       // Note: for an interface we will find Object as the
1374       // superclass.  We still check it above to ensure class file
1375       // validity, but we simply assign `null' to the actual field in
1376       // this case.
1377       def->superclass = (((access_flags & Modifier::INTERFACE))
1378                          ? NULL : the_super);
1379       pool_data[super_class].clazz = the_super;
1380       pool_tags[super_class] = JV_CONSTANT_ResolvedClass;
1381     }
1382
1383   // Now we've come past the circularity problem, we can 
1384   // now say that we're loading.
1385
1386   def->state = JV_STATE_LOADING;
1387   def->notifyAll ();
1388 }
1389
1390 ///// Implements the checks described in sect. 5.3.5.3
1391 void
1392 _Jv_ClassReader::checkExtends (jclass sub, jclass super)
1393 {
1394   using namespace java::lang::reflect;
1395
1396   _Jv_Linker::wait_for_state (super, JV_STATE_LOADING);
1397
1398   // Having an interface or a final class as a superclass is no good.
1399   if ((super->accflags & (Modifier::INTERFACE | Modifier::FINAL)) != 0)
1400     {
1401       throw_incompatible_class_change_error (sub->getName ());
1402     }
1403
1404   // If the super class is not public, we need to check some more.
1405   if ((super->accflags & Modifier::PUBLIC) == 0)
1406     {
1407       // With package scope, the classes must have the same class
1408       // loader.
1409       if (   sub->loader != super->loader
1410           || !_Jv_ClassNameSamePackage (sub->name, super->name))
1411         {
1412           throw_incompatible_class_change_error (sub->getName ());
1413         }
1414     } 
1415
1416   for (; super != 0; super = super->getSuperclass ())
1417     {
1418       if (super == sub)
1419         throw_class_circularity_error (sub->getName ());
1420     }
1421 }
1422
1423
1424
1425 void _Jv_ClassReader::handleInterfacesBegin (int count)
1426 {
1427   def->interfaces = (jclass*) _Jv_AllocRawObj (count*sizeof (jclass));
1428   def->interface_count = count;
1429 }
1430
1431 void _Jv_ClassReader::handleInterface (int if_number, int offset)
1432 {
1433   _Jv_word       * pool_data = def->constants.data;
1434   unsigned char  * pool_tags = (unsigned char*) def->constants.tags;
1435
1436   jclass the_interface;
1437
1438   if (pool_tags[offset] == JV_CONSTANT_Class)
1439     {
1440       _Jv_Utf8Const* name = pool_data[offset].utf8;
1441       the_interface =  _Jv_FindClass (name, def->loader);
1442     }
1443   else if (pool_tags[offset] == JV_CONSTANT_ResolvedClass)
1444     {
1445       the_interface = pool_data[offset].clazz;
1446     }
1447   else
1448     {
1449       throw_no_class_def_found_error ("erroneous constant pool tag");
1450     }
1451
1452   // checks the validity of the_interface, and that we are in fact
1453   // allowed to implement that interface.
1454   checkImplements (def, the_interface);
1455   
1456   pool_data[offset].clazz = the_interface;
1457   pool_tags[offset] = JV_CONSTANT_ResolvedClass;
1458   
1459   def->interfaces[if_number] = the_interface;
1460 }
1461
1462 void
1463 _Jv_ClassReader::checkImplements (jclass sub, jclass super)
1464 {
1465   using namespace java::lang::reflect;
1466
1467   // well, it *must* be an interface
1468   if ((super->accflags & Modifier::INTERFACE) == 0)
1469     {
1470       throw_incompatible_class_change_error (sub->getName ());
1471     }
1472
1473   // if it has package scope, it must also be defined by the 
1474   // same loader.
1475   if ((super->accflags & Modifier::PUBLIC) == 0)
1476     {
1477       if (    sub->loader != super->loader
1478           || !_Jv_ClassNameSamePackage (sub->name, super->name))
1479         {
1480           throw_incompatible_class_change_error (sub->getName ());
1481         }
1482     } 
1483
1484   // FIXME: add interface circularity check here
1485   if (sub == super)
1486     {
1487       throw_class_circularity_error (sub->getName ());
1488     }           
1489 }
1490
1491 void _Jv_ClassReader::handleFieldsBegin (int count)
1492 {
1493   def->fields = (_Jv_Field*) _Jv_AllocRawObj (count * sizeof (_Jv_Field));
1494   def->field_count = count;
1495   def_interp->field_initializers
1496     = (_Jv_ushort*) _Jv_AllocRawObj (count * sizeof (_Jv_ushort));
1497   for (int i = 0; i < count; i++)
1498     def_interp->field_initializers[i] = (_Jv_ushort) 0;
1499 }
1500
1501 void _Jv_ClassReader::handleField (int field_no,
1502                                    int flags,
1503                                    int name,
1504                                    int desc,
1505                                    int *fieldmap)
1506 {
1507   using namespace java::lang::reflect;
1508
1509   _Jv_word *pool_data = def->constants.data;
1510
1511   _Jv_Field *field = &def->fields[fieldmap[field_no]];
1512   _Jv_Utf8Const *field_name = pool_data[name].utf8;
1513
1514   field->name      = field_name;
1515
1516   // Ignore flags we don't know about.  
1517   field->flags = flags & (Field::FIELD_MODIFIERS
1518                           | Modifier::SYNTHETIC
1519                           | Modifier::ENUM);
1520
1521   _Jv_Utf8Const* sig = pool_data[desc].utf8;
1522
1523   if (verify)
1524     {
1525       verify_identifier (field_name);
1526
1527       for (int i = 0; i < field_no; ++i)
1528         {
1529           if (_Jv_equalUtf8Consts (field_name, def->fields[fieldmap[i]].name)
1530               && _Jv_equalUtf8Consts (sig,
1531                                       // We know the other fields are
1532                                       // unresolved.
1533                                       (_Jv_Utf8Const *) def->fields[i].type))
1534             throw_class_format_error ("duplicate field name");
1535         }
1536
1537       // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed.
1538       if (1 < ( ((field->flags & Modifier::PUBLIC) ? 1 : 0)
1539                 +((field->flags & Modifier::PRIVATE) ? 1 : 0)
1540                 +((field->flags & Modifier::PROTECTED) ? 1 : 0)))
1541         throw_class_format_error ("erroneous field access flags");
1542
1543       // FIXME: JVM spec S4.5: Verify ACC_FINAL and ACC_VOLATILE are not 
1544       // both set. Verify modifiers for interface fields.
1545       
1546     }
1547
1548   if (verify)
1549     verify_field_signature (sig);
1550
1551   // field->type is really a jclass, but while it is still
1552   // unresolved we keep an _Jv_Utf8Const* instead.
1553   field->type       = (jclass) sig;
1554   field->flags     |= _Jv_FIELD_UNRESOLVED_FLAG;
1555   field->u.boffset  = 0;
1556 }
1557
1558
1559 void _Jv_ClassReader::handleConstantValueAttribute (int field_index, 
1560                                                     int value,
1561                                                     bool *found_value)
1562 {
1563   using namespace java::lang::reflect;
1564
1565   _Jv_Field *field = &def->fields[field_index];
1566
1567   if ((field->flags & (Modifier::STATIC
1568                        | Modifier::FINAL
1569                        | Modifier::PRIVATE)) == 0)
1570     {
1571       // Ignore, as per vmspec #4.7.2
1572       return;
1573     }
1574
1575   // do not allow multiple constant fields!
1576   if (*found_value)
1577     throw_class_format_error ("field has multiple ConstantValue attributes");
1578
1579   *found_value = true;
1580   def_interp->field_initializers[field_index] = value;
1581
1582   /* type check the initializer */
1583   
1584   if (value <= 0 || value >= pool_count)
1585     throw_class_format_error ("erroneous ConstantValue attribute");
1586
1587   /* FIXME: do the rest */
1588 }
1589
1590 void
1591 _Jv_ClassReader::handleMethodsBegin (int count)
1592 {
1593   def->methods = (_Jv_Method *) _Jv_AllocRawObj (sizeof (_Jv_Method) * count);
1594
1595   def_interp->interpreted_methods
1596     = (_Jv_MethodBase **) _Jv_AllocRawObj (sizeof (_Jv_MethodBase *)
1597                                            * count);
1598
1599   for (int i = 0; i < count; i++)
1600     {
1601       def_interp->interpreted_methods[i] = 0;
1602       def->methods[i].index = (_Jv_ushort) -1;
1603     }
1604
1605   def->method_count = count;
1606 }
1607
1608
1609 void _Jv_ClassReader::handleMethod 
1610     (int mth_index, int accflags, int name, int desc)
1611
1612   using namespace java::lang::reflect;
1613
1614   _Jv_word *pool_data = def->constants.data;
1615   _Jv_Method *method = &def->methods[mth_index];
1616
1617   check_tag (name, JV_CONSTANT_Utf8);
1618   prepare_pool_entry (name, JV_CONSTANT_Utf8, false);
1619   method->name = pool_data[name].utf8;
1620
1621   check_tag (desc, JV_CONSTANT_Utf8);
1622   prepare_pool_entry (desc, JV_CONSTANT_Utf8);
1623   method->signature = pool_data[desc].utf8;
1624
1625   // ignore unknown flags
1626   method->accflags = accflags & (Method::METHOD_MODIFIERS
1627                                  | Modifier::BRIDGE
1628                                  | Modifier::SYNTHETIC
1629                                  | Modifier::VARARGS);
1630
1631   // Initialize...
1632   method->ncode = 0;
1633   method->throws = NULL;
1634   
1635   if (verify)
1636     {
1637       if (_Jv_equalUtf8Consts (method->name, clinit_name)
1638           || _Jv_equalUtf8Consts (method->name, init_name))
1639         /* ignore */;
1640       else
1641         verify_identifier (method->name);
1642
1643       verify_method_signature (method->signature);
1644
1645       for (int i = 0; i < mth_index; ++i)
1646         {
1647           if (_Jv_equalUtf8Consts (method->name, def->methods[i].name)
1648               && _Jv_equalUtf8Consts (method->signature,
1649                                       def->methods[i].signature))
1650             throw_class_format_error ("duplicate method");
1651         }
1652
1653       // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed.
1654       if (1 < ( ((method->accflags & Modifier::PUBLIC) ? 1 : 0)
1655                 +((method->accflags & Modifier::PRIVATE) ? 1 : 0)
1656                 +((method->accflags & Modifier::PROTECTED) ? 1 : 0)))
1657         throw_class_format_error ("erroneous method access flags");
1658
1659       // FIXME: JVM spec S4.6: if ABSTRACT modifier is set, verify other 
1660       // flags are not set. Verify flags for interface methods.  Verify
1661       // modifiers for initializers. 
1662     }
1663 }
1664
1665 void _Jv_ClassReader::handleCodeAttribute
1666   (int method_index, int max_stack, int max_locals, 
1667    int code_start, int code_length, int exc_table_length)
1668 {
1669   int size = _Jv_InterpMethod::size (exc_table_length, code_length);
1670   _Jv_InterpMethod *method = 
1671     (_Jv_InterpMethod*) (_Jv_AllocRawObj (size));
1672
1673   method->max_stack      = max_stack;
1674   method->max_locals     = max_locals;
1675   method->code_length    = code_length;
1676   method->exc_count      = exc_table_length;
1677   method->is_15          = is_15;
1678   method->defining_class = def;
1679   method->self           = &def->methods[method_index];
1680   method->prepared       = NULL;
1681   method->line_table_len = 0;
1682   method->line_table     = NULL;
1683
1684
1685   // grab the byte code!
1686   memcpy ((void*) method->bytecode (),
1687           (void*) (bytes+code_start),
1688           code_length);
1689
1690   def_interp->interpreted_methods[method_index] = method;
1691
1692   if ((method->self->accflags & java::lang::reflect::Modifier::STATIC))
1693     {
1694       // Precompute the ncode field for a static method.  This lets us
1695       // call a static method of an interpreted class from precompiled
1696       // code without first resolving the class (that will happen
1697       // during class initialization instead).
1698       method->self->ncode = method->ncode ();
1699     }
1700 }
1701
1702 void _Jv_ClassReader::handleExceptionTableEntry
1703   (int method_index, int exc_index, 
1704    int start_pc, int end_pc, int handler_pc, int catch_type)
1705 {
1706   _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
1707     (def_interp->interpreted_methods[method_index]);
1708   _Jv_InterpException *exc = method->exceptions ();
1709
1710   exc[exc_index].start_pc.i     = start_pc;
1711   exc[exc_index].end_pc.i       = end_pc;
1712   exc[exc_index].handler_pc.i   = handler_pc;
1713   exc[exc_index].handler_type.i = catch_type;
1714 }
1715
1716 void _Jv_ClassReader::handleMethodsEnd ()
1717 {
1718   using namespace java::lang::reflect;
1719
1720   for (int i = 0; i < def->method_count; i++)
1721     {
1722       _Jv_Method *method = &def->methods[i];
1723       if ((method->accflags & Modifier::NATIVE) != 0)
1724         {
1725           if (def_interp->interpreted_methods[i] != 0)
1726             throw_class_format_error ("code provided for native method");
1727           else
1728             {
1729               _Jv_JNIMethod *m = (_Jv_JNIMethod *)
1730                 _Jv_AllocRawObj (sizeof (_Jv_JNIMethod));
1731               m->defining_class = def;
1732               m->self = method;
1733               m->function = NULL;
1734               def_interp->interpreted_methods[i] = m;
1735
1736               if ((method->accflags & Modifier::STATIC))
1737                 {
1738                   // Precompute the ncode field for a static method.
1739                   // This lets us call a static method of an
1740                   // interpreted class from precompiled code without
1741                   // first resolving the class (that will happen
1742                   // during class initialization instead).
1743                   method->ncode = m->ncode ();
1744                 }
1745             }
1746         }
1747       else if ((method->accflags & Modifier::ABSTRACT) != 0)
1748         {
1749           if (def_interp->interpreted_methods[i] != 0)
1750             throw_class_format_error ("code provided for abstract method");
1751           method->ncode = (void *) &_Jv_ThrowAbstractMethodError;
1752         }
1753       else
1754         {
1755           if (def_interp->interpreted_methods[i] == 0)
1756             throw_class_format_error ("method with no code");
1757         }
1758     }
1759 }
1760
1761 void _Jv_ClassReader::throw_class_format_error (const char *msg)
1762 {
1763   jstring str;
1764   if (def->name != NULL)
1765     {
1766       jsize mlen = strlen (msg);
1767       unsigned char* data = (unsigned char*) def->name->chars();
1768       int ulen = def->name->len();
1769       unsigned char* limit = data + ulen;
1770       jsize nlen = _Jv_strLengthUtf8 ((char *) data, ulen);
1771       jsize len = nlen + mlen + 3;
1772       str = JvAllocString(len);
1773       jchar *chrs = JvGetStringChars(str);
1774       while (data < limit)
1775         *chrs++ = UTF8_GET(data, limit);
1776       *chrs++ = ' ';
1777       *chrs++ = '(';
1778       for (;;)
1779         {
1780           char c = *msg++;
1781           if (c == 0)
1782             break;
1783           *chrs++ = c & 0xFFFF;
1784         }
1785       *chrs++ = ')';
1786     }
1787   else
1788     str = JvNewStringLatin1 (msg);
1789   ::throw_class_format_error (str);
1790 }
1791 \f
1792 /** Here we define the exceptions that can be thrown */
1793
1794 static void
1795 throw_no_class_def_found_error (jstring msg)
1796 {
1797   throw (msg
1798          ? new java::lang::NoClassDefFoundError (msg)
1799          : new java::lang::NoClassDefFoundError);
1800 }
1801
1802 static void
1803 throw_no_class_def_found_error (const char *msg)
1804 {
1805   throw_no_class_def_found_error (JvNewStringLatin1 (msg));
1806 }
1807
1808 static void
1809 throw_class_format_error (jstring msg)
1810 {
1811   throw (msg
1812          ? new java::lang::ClassFormatError (msg)
1813          : new java::lang::ClassFormatError);
1814 }
1815
1816 static void
1817 throw_internal_error (const char *msg)
1818 {
1819   throw new java::lang::InternalError (JvNewStringLatin1 (msg));
1820 }
1821
1822 static void
1823 throw_incompatible_class_change_error (jstring msg)
1824 {
1825   throw new java::lang::IncompatibleClassChangeError (msg);
1826 }
1827
1828 static void
1829 throw_class_circularity_error (jstring msg)
1830 {
1831   throw new java::lang::ClassCircularityError (msg);
1832 }
1833
1834 #endif /* INTERPRETER */
1835
1836 \f
1837
1838 /** This section takes care of verifying integrity of identifiers,
1839     signatures, field ddescriptors, and class names */
1840
1841 #define UTF8_PEEK(PTR, LIMIT) \
1842   ({ unsigned char* xxkeep = (PTR); \
1843      int xxch = UTF8_GET(PTR,LIMIT); \
1844      PTR = xxkeep; xxch; })
1845
1846 /* Verify one element of a type descriptor or signature.  */
1847 static unsigned char*
1848 _Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok)
1849 {
1850   if (ptr >= limit)
1851     return 0;
1852
1853   int ch = UTF8_GET (ptr, limit);
1854
1855   switch (ch)
1856     {
1857     case 'V':
1858       if (! void_ok)
1859         return 0;
1860
1861     case 'S': case 'B': case 'I': case 'J':
1862     case 'Z': case 'C': case 'F': case 'D': 
1863       break;
1864
1865     case 'L':
1866       {
1867         unsigned char *start = ptr, *end;
1868         do
1869           {
1870             if (ptr > limit)
1871               return 0;
1872
1873             end = ptr;
1874
1875             if ((ch = UTF8_GET (ptr, limit)) == -1)
1876               return 0;
1877
1878           }
1879         while (ch != ';');
1880         if (! _Jv_VerifyClassName (start, (unsigned short) (end-start)))
1881           return 0;
1882       }
1883       break;
1884
1885     case '[':
1886       return _Jv_VerifyOne (ptr, limit, false);
1887       break;
1888
1889     default:
1890       return 0;
1891     }
1892
1893   return ptr;
1894 }
1895
1896 /* Verification and loading procedures.  */
1897 bool
1898 _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
1899 {
1900   unsigned char* ptr = (unsigned char*) sig->chars();
1901   unsigned char* limit = ptr + sig->len();
1902
1903   ptr = _Jv_VerifyOne (ptr, limit, false);
1904
1905   return ptr == limit;
1906 }
1907
1908 bool
1909 _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig)
1910 {
1911   unsigned char* ptr = (unsigned char*) sig->chars();
1912   unsigned char* limit = ptr + sig->len();
1913
1914   if (ptr == limit || UTF8_GET(ptr,limit) != '(')
1915     return false;
1916
1917   while (ptr && UTF8_PEEK (ptr, limit) != ')')
1918     ptr = _Jv_VerifyOne (ptr, limit, false);
1919
1920   if (! ptr || UTF8_GET (ptr, limit) != ')')
1921     return false;
1922
1923   // get the return type
1924   ptr = _Jv_VerifyOne (ptr, limit, true);
1925
1926   return ptr == limit;
1927 }
1928
1929 /* We try to avoid calling the Character methods all the time, in
1930    fact, they will only be called for non-standard things. */
1931 static __inline__ int 
1932 is_identifier_start (int c)
1933 {
1934   unsigned int ch = (unsigned)c;
1935
1936   if ((ch - 0x41U) < 29U)               /* A ... Z */
1937     return 1;
1938   if ((ch - 0x61U) < 29U)               /* a ... z */
1939     return 1;
1940   if (ch == 0x5FU)                      /* _ */
1941     return 1;
1942
1943   return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
1944 }
1945
1946 static __inline__ int 
1947 is_identifier_part (int c)
1948 {
1949   unsigned int ch = (unsigned)c;
1950
1951   if ((ch - 0x41U) < 29U)               /* A ... Z */
1952     return 1;
1953   if ((ch - 0x61U) < 29U)               /* a ... z */
1954     return 1;
1955   if ((ch - 0x30) < 10U)                /* 0 .. 9 */
1956     return 1;
1957   if (ch == 0x5FU || ch == 0x24U)       /* _ $ */
1958     return 1;
1959
1960   return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
1961 }
1962
1963 bool
1964 _Jv_VerifyIdentifier (_Jv_Utf8Const* name)
1965 {
1966   unsigned char *ptr   = (unsigned char*) name->chars();
1967   unsigned char *limit = (unsigned char*) name->limit();
1968   int ch;
1969
1970   if ((ch = UTF8_GET (ptr, limit))==-1
1971       || ! is_identifier_start (ch))
1972     return false;
1973
1974   while (ptr != limit)
1975     {
1976       if ((ch = UTF8_GET (ptr, limit))==-1
1977           || ! is_identifier_part (ch))
1978         return false;
1979     }
1980   return true;
1981 }
1982
1983 bool
1984 _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length)
1985 {
1986   unsigned char *limit = ptr+length;
1987   int ch;
1988
1989   if ('[' == UTF8_PEEK (ptr, limit))
1990     {
1991       unsigned char *end = _Jv_VerifyOne (++ptr, limit, false);
1992       // _Jv_VerifyOne must leave us looking at the terminating nul
1993       // byte.
1994       if (! end || *end)
1995         return false;
1996       else
1997         return true;
1998     }
1999
2000  next_level:
2001   for (;;) {
2002     if ((ch = UTF8_GET (ptr, limit))==-1)
2003       return false;
2004     if (! is_identifier_start (ch))
2005       return false;
2006     for (;;) {
2007       if (ptr == limit)
2008         return true;
2009       else if ((ch = UTF8_GET (ptr, limit))==-1)
2010         return false;
2011       else if (ch == '.')
2012         goto next_level;
2013       else if (! is_identifier_part (ch))
2014         return false;
2015     }
2016   }
2017 }
2018
2019 bool
2020 _Jv_VerifyClassName (_Jv_Utf8Const *name)
2021 {
2022   return _Jv_VerifyClassName ((unsigned char*)name->chars(), name->len());
2023 }
2024
2025 /* Returns true, if NAME1 and NAME2 represent classes in the same
2026    package.  Neither NAME2 nor NAME2 may name an array type.  */
2027 bool
2028 _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2)
2029 {
2030   unsigned char* ptr1 = (unsigned char*) name1->chars();
2031   unsigned char* limit1 = (unsigned char*) name1->limit();
2032
2033   unsigned char* last1 = ptr1;
2034
2035   // scan name1, and find the last occurrence of '.'
2036   while (ptr1 < limit1) {
2037     int ch1 = UTF8_GET (ptr1, limit1);
2038
2039     if (ch1 == '.')
2040       last1 = ptr1;
2041
2042     else if (ch1 == -1)
2043       return false;
2044   }
2045
2046   // Now the length of NAME1's package name is LEN.
2047   int len = last1 - (unsigned char*) name1->chars();
2048
2049   // If this is longer than NAME2, then we're off.
2050   if (len > name2->len())
2051     return false;
2052
2053   // Then compare the first len bytes for equality.
2054   if (memcmp ((void*) name1->chars(), (void*) name2->chars(), len) == 0)
2055     {
2056       // Check that there are no .'s after position LEN in NAME2.
2057
2058       unsigned char* ptr2 = (unsigned char*) name2->chars() + len;
2059       unsigned char* limit2 = (unsigned char*) name2->limit();
2060
2061       while (ptr2 < limit2)
2062         {
2063           int ch2 = UTF8_GET (ptr2, limit2);
2064           if (ch2 == -1 || ch2 == '.')
2065             return false;
2066         }
2067       return true;
2068     }
2069   return false;
2070 }