* Removed all Id tags.
[cacao.git] / src / vmcore / loader.c
1 /* src/vmcore/loader.c - class loader functions
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33
34 #include "vm/types.h"
35
36 #include "mm/memory.h"
37
38 #include "native/llni.h"
39
40 #include "threads/lock-common.h"
41
42 #include "toolbox/logging.h"
43
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/global.h"
47 #include "vm/primitive.h"
48 #include "vm/stringlocal.h"
49 #include "vm/vm.h"
50
51 #include "vm/jit_interface.h"
52
53 #if defined(ENABLE_JAVASE)
54 # include "vmcore/annotation.h"
55 # include "vmcore/stackmap.h"
56 #endif
57
58 #include "vmcore/classcache.h"
59 #include "vmcore/field.h"
60 #include "vmcore/linker.h"
61 #include "vmcore/loader.h"
62 #include "vmcore/method.h"
63 #include "vmcore/options.h"
64 #include "vmcore/rt-timing.h"
65
66 #if defined(ENABLE_STATISTICS)
67 # include "vmcore/statistics.h"
68 #endif
69
70 #include "vmcore/suck.h"
71
72 #if defined(ENABLE_ZLIB)
73 # include "vmcore/zip.h"
74 #endif
75
76 #if defined(ENABLE_JVMTI)
77 # include "native/jvmti/cacaodbg.h"
78 #endif
79
80
81 /* loader_init *****************************************************************
82
83    Initializes all lists and loads all classes required for the system
84    or the compiler.
85
86 *******************************************************************************/
87  
88 bool loader_init(void)
89 {
90 #if defined(ENABLE_THREADS)
91         list_classpath_entry *lce;
92
93         /* Initialize the monitor pointer for zip/jar file locking. */
94
95         for (lce = list_first(list_classpath_entries); lce != NULL;
96                  lce = list_next(list_classpath_entries, lce))
97                 if (lce->type == CLASSPATH_ARCHIVE)
98                         LOCK_INIT_OBJECT_LOCK(lce);
99 #endif
100
101         /* load some important classes */
102
103         if (!(class_java_lang_Object = load_class_bootstrap(utf_java_lang_Object)))
104                 return false;
105
106         if (!(class_java_lang_String = load_class_bootstrap(utf_java_lang_String)))
107                 return false;
108
109 #if defined(ENABLE_JAVASE)
110         if (!(class_java_lang_Cloneable =
111                   load_class_bootstrap(utf_java_lang_Cloneable)))
112                 return false;
113
114         if (!(class_java_io_Serializable =
115                   load_class_bootstrap(utf_java_io_Serializable)))
116                 return false;
117 #endif
118
119         /* load classes for wrapping primitive types */
120
121 #if defined(ENABLE_JAVASE)
122         if (!(class_java_lang_Void = load_class_bootstrap(utf_java_lang_Void)))
123                 return false;
124 #endif
125
126         if (!(class_java_lang_Boolean =
127                   load_class_bootstrap(utf_java_lang_Boolean)))
128                 return false;
129
130         if (!(class_java_lang_Byte = load_class_bootstrap(utf_java_lang_Byte)))
131                 return false;
132
133         if (!(class_java_lang_Character =
134                   load_class_bootstrap(utf_java_lang_Character)))
135                 return false;
136
137         if (!(class_java_lang_Short = load_class_bootstrap(utf_java_lang_Short)))
138                 return false;
139
140         if (!(class_java_lang_Integer =
141                   load_class_bootstrap(utf_java_lang_Integer)))
142                 return false;
143
144         if (!(class_java_lang_Long = load_class_bootstrap(utf_java_lang_Long)))
145                 return false;
146
147         if (!(class_java_lang_Float = load_class_bootstrap(utf_java_lang_Float)))
148                 return false;
149
150         if (!(class_java_lang_Double = load_class_bootstrap(utf_java_lang_Double)))
151                 return false;
152
153
154         /* load some other important classes */
155
156         if (!(class_java_lang_Class = load_class_bootstrap(utf_java_lang_Class)))
157                 return false;
158
159 #if defined(ENABLE_JAVASE)
160         if (!(class_java_lang_ClassLoader =
161                   load_class_bootstrap(utf_java_lang_ClassLoader)))
162                 return false;
163
164         if (!(class_java_lang_SecurityManager =
165                   load_class_bootstrap(utf_java_lang_SecurityManager)))
166                 return false;
167 #endif
168
169         if (!(class_java_lang_System = load_class_bootstrap(utf_java_lang_System)))
170                 return false;
171
172         if (!(class_java_lang_Thread =
173                   load_class_bootstrap(utf_new_char("java/lang/Thread"))))
174                 return false;
175
176 #if defined(ENABLE_JAVASE)
177         if (!(class_java_lang_ThreadGroup =
178                   load_class_bootstrap(utf_java_lang_ThreadGroup)))
179                 return false;
180 #endif
181
182 #if defined(WITH_CLASSPATH_GNU)
183         if (!(class_java_lang_VMSystem =
184                   load_class_bootstrap(utf_new_char("java/lang/VMSystem"))))
185
186                 return false;
187
188         if (!(class_java_lang_VMThread =
189                   load_class_bootstrap(utf_new_char("java/lang/VMThread"))))
190                 return false;
191 #endif
192
193
194         /* some classes which may be used more often */
195
196 #if defined(ENABLE_JAVASE)
197         if (!(class_java_lang_StackTraceElement =
198                   load_class_bootstrap(utf_java_lang_StackTraceElement)))
199                 return false;
200
201         if (!(class_java_lang_reflect_Constructor =
202                   load_class_bootstrap(utf_java_lang_reflect_Constructor)))
203                 return false;
204
205         if (!(class_java_lang_reflect_Field =
206                   load_class_bootstrap(utf_java_lang_reflect_Field)))
207                 return false;
208
209         if (!(class_java_lang_reflect_Method =
210                   load_class_bootstrap(utf_java_lang_reflect_Method)))
211                 return false;
212
213         if (!(class_java_security_PrivilegedAction =
214                   load_class_bootstrap(utf_new_char("java/security/PrivilegedAction"))))
215                 return false;
216
217         if (!(class_java_util_Vector = load_class_bootstrap(utf_java_util_Vector)))
218                 return false;
219
220 # if defined(WITH_CLASSPATH_SUN)
221         if (!(class_sun_reflect_MagicAccessorImpl =
222                   load_class_bootstrap(utf_new_char("sun/reflect/MagicAccessorImpl"))))
223                 return false;
224 # endif
225
226         if (!(arrayclass_java_lang_Object =
227                   load_class_bootstrap(utf_new_char("[Ljava/lang/Object;"))))
228                 return false;
229
230 #if defined(ENABLE_ANNOTATIONS)
231         /* needed by annotation support */
232         if (!(class_sun_reflect_ConstantPool = 
233                   load_class_bootstrap(utf_sun_reflect_ConstantPool)))
234                 return false;
235
236 #if defined(WITH_CLASSPATH_GNU)
237         /* needed by GNU Classpaths annotation support */
238         if (!(class_sun_reflect_annotation_AnnotationParser = 
239                   load_class_bootstrap(utf_sun_reflect_annotation_AnnotationParser)))
240                 return false;
241 #endif
242 #endif
243 #endif
244
245
246         return true;
247 }
248
249
250 /* loader_load_all_classes *****************************************************
251
252    Loads all classes specified in the BOOTCLASSPATH.
253
254 *******************************************************************************/
255
256 void loader_load_all_classes(void)
257 {
258         list_classpath_entry    *lce;
259 #if defined(ENABLE_ZLIB)
260         hashtable               *ht;
261         s4                       slot;
262         hashtable_zipfile_entry *htzfe;
263         utf                     *u;
264 #endif
265
266         for (lce = list_first(list_classpath_entries); lce != NULL;
267                  lce = list_next(list_classpath_entries, lce)) {
268 #if defined(ENABLE_ZLIB)
269                 if (lce->type == CLASSPATH_ARCHIVE) {
270                         /* get the classes hashtable */
271
272                         ht = lce->htclasses;
273
274                         for (slot = 0; slot < ht->size; slot++) {
275                                 htzfe = (hashtable_zipfile_entry *) ht->ptr[slot];
276
277                                 for (; htzfe; htzfe = htzfe->hashlink) {
278                                         u = htzfe->filename;
279
280                                         /* skip all entries in META-INF and .properties,
281                        .png files */
282
283                                         if (!strncmp(u->text, "META-INF", strlen("META-INF")) ||
284                                                 strstr(u->text, ".properties") ||
285                                                 strstr(u->text, ".png"))
286                                                 continue;
287
288                                         /* load class from bootstrap classloader */
289
290                                         if (!load_class_bootstrap(u)) {
291                                                 fprintf(stderr, "Error loading: ");
292                                                 utf_fprint_printable_ascii_classname(stderr, u);
293                                                 fprintf(stderr, "\n");
294
295 #if !defined(NDEBUG)
296                                                 /* print out exception and cause */
297
298                                                 exceptions_print_current_exception();
299 #endif
300                                         }
301                                 }
302                         }
303
304                 } else {
305 #endif
306 #if defined(ENABLE_ZLIB)
307                 }
308 #endif
309         }
310 }
311
312
313 /* loader_skip_attribute_body **************************************************
314
315    Skips an attribute the attribute_name_index has already been read.
316         
317    attribute_info {
318        u2 attribute_name_index;
319        u4 attribute_length;
320        u1 info[attribute_length];
321    }
322
323 *******************************************************************************/
324
325 bool loader_skip_attribute_body(classbuffer *cb)
326 {
327         u4 attribute_length;
328
329         if (!suck_check_classbuffer_size(cb, 4))
330                 return false;
331
332         attribute_length = suck_u4(cb);
333
334         if (!suck_check_classbuffer_size(cb, attribute_length))
335                 return false;
336
337         suck_skip_nbytes(cb, attribute_length);
338
339         return true;
340 }
341
342
343 /* load_constantpool ***********************************************************
344
345    Loads the constantpool of a class, the entries are transformed into
346    a simpler format by resolving references (a detailed overview of
347    the compact structures can be found in global.h).
348
349 *******************************************************************************/
350
351 static bool load_constantpool(classbuffer *cb, descriptor_pool *descpool)
352 {
353
354         /* The following structures are used to save information which cannot be 
355            processed during the first pass. After the complete constantpool has 
356            been traversed the references can be resolved. 
357            (only in specific order)                                                */
358         
359         /* CONSTANT_Class entries */
360         typedef struct forward_class {
361                 struct forward_class *next;
362                 u2 thisindex;
363                 u2 name_index;
364         } forward_class;
365
366         /* CONSTANT_String */
367         typedef struct forward_string {
368                 struct forward_string *next;
369                 u2 thisindex;
370                 u2 string_index;
371         } forward_string;
372
373         /* CONSTANT_NameAndType */
374         typedef struct forward_nameandtype {
375                 struct forward_nameandtype *next;
376                 u2 thisindex;
377                 u2 name_index;
378                 u2 sig_index;
379         } forward_nameandtype;
380
381         /* CONSTANT_Fieldref, CONSTANT_Methodref or CONSTANT_InterfaceMethodref */
382         typedef struct forward_fieldmethint {
383                 struct forward_fieldmethint *next;
384                 u2 thisindex;
385                 u1 tag;
386                 u2 class_index;
387                 u2 nameandtype_index;
388         } forward_fieldmethint;
389
390
391         classinfo *c;
392         u4 idx;
393
394         forward_class *forward_classes = NULL;
395         forward_string *forward_strings = NULL;
396         forward_nameandtype *forward_nameandtypes = NULL;
397         forward_fieldmethint *forward_fieldmethints = NULL;
398
399         forward_class *nfc;
400         forward_string *nfs;
401         forward_nameandtype *nfn;
402         forward_fieldmethint *nff;
403
404         u4 cpcount;
405         u1 *cptags;
406         voidptr *cpinfos;
407
408         c = cb->class;
409
410         /* number of entries in the constant_pool table plus one */
411         if (!suck_check_classbuffer_size(cb, 2))
412                 return false;
413
414         cpcount = c->cpcount = suck_u2(cb);
415
416         /* allocate memory */
417         cptags  = c->cptags  = MNEW(u1, cpcount);
418         cpinfos = c->cpinfos = MNEW(voidptr, cpcount);
419
420         if (cpcount < 1) {
421                 exceptions_throw_classformaterror(c, "Illegal constant pool size");
422                 return false;
423         }
424         
425 #if defined(ENABLE_STATISTICS)
426         if (opt_stat)
427                 count_const_pool_len += (sizeof(u1) + sizeof(voidptr)) * cpcount;
428 #endif
429         
430         /* initialize constantpool */
431         for (idx = 0; idx < cpcount; idx++) {
432                 cptags[idx] = CONSTANT_UNUSED;
433                 cpinfos[idx] = NULL;
434         }
435
436                         
437         /******* first pass *******/
438         /* entries which cannot be resolved now are written into 
439            temporary structures and traversed again later        */
440                    
441         idx = 1;
442         while (idx < cpcount) {
443                 u4 t;
444
445                 /* get constant type */
446                 if (!suck_check_classbuffer_size(cb, 1))
447                         return false;
448
449                 t = suck_u1(cb);
450
451                 switch (t) {
452                 case CONSTANT_Class:
453                         nfc = DNEW(forward_class);
454
455                         nfc->next = forward_classes;
456                         forward_classes = nfc;
457
458                         nfc->thisindex = idx;
459                         /* reference to CONSTANT_NameAndType */
460                         if (!suck_check_classbuffer_size(cb, 2))
461                                 return false;
462
463                         nfc->name_index = suck_u2(cb);
464
465                         idx++;
466                         break;
467                         
468                 case CONSTANT_String:
469                         nfs = DNEW(forward_string);
470                                 
471                         nfs->next = forward_strings;
472                         forward_strings = nfs;
473                                 
474                         nfs->thisindex = idx;
475
476                         /* reference to CONSTANT_Utf8_info with string characters */
477                         if (!suck_check_classbuffer_size(cb, 2))
478                                 return false;
479
480                         nfs->string_index = suck_u2(cb);
481                                 
482                         idx++;
483                         break;
484
485                 case CONSTANT_NameAndType:
486                         nfn = DNEW(forward_nameandtype);
487                                 
488                         nfn->next = forward_nameandtypes;
489                         forward_nameandtypes = nfn;
490                                 
491                         nfn->thisindex = idx;
492
493                         if (!suck_check_classbuffer_size(cb, 2 + 2))
494                                 return false;
495
496                         /* reference to CONSTANT_Utf8_info containing simple name */
497                         nfn->name_index = suck_u2(cb);
498
499                         /* reference to CONSTANT_Utf8_info containing field or method
500                            descriptor */
501                         nfn->sig_index = suck_u2(cb);
502                                 
503                         idx++;
504                         break;
505
506                 case CONSTANT_Fieldref:
507                 case CONSTANT_Methodref:
508                 case CONSTANT_InterfaceMethodref:
509                         nff = DNEW(forward_fieldmethint);
510                         
511                         nff->next = forward_fieldmethints;
512                         forward_fieldmethints = nff;
513
514                         nff->thisindex = idx;
515                         /* constant type */
516                         nff->tag = t;
517
518                         if (!suck_check_classbuffer_size(cb, 2 + 2))
519                                 return false;
520
521                         /* class or interface type that contains the declaration of the
522                            field or method */
523                         nff->class_index = suck_u2(cb);
524
525                         /* name and descriptor of the field or method */
526                         nff->nameandtype_index = suck_u2(cb);
527
528                         idx++;
529                         break;
530                                 
531                 case CONSTANT_Integer: {
532                         constant_integer *ci = NEW(constant_integer);
533
534 #if defined(ENABLE_STATISTICS)
535                         if (opt_stat)
536                                 count_const_pool_len += sizeof(constant_integer);
537 #endif
538
539                         if (!suck_check_classbuffer_size(cb, 4))
540                                 return false;
541
542                         ci->value = suck_s4(cb);
543                         cptags[idx] = CONSTANT_Integer;
544                         cpinfos[idx] = ci;
545
546                         idx++;
547                         break;
548                 }
549                                 
550                 case CONSTANT_Float: {
551                         constant_float *cf = NEW(constant_float);
552
553 #if defined(ENABLE_STATISTICS)
554                         if (opt_stat)
555                                 count_const_pool_len += sizeof(constant_float);
556 #endif
557
558                         if (!suck_check_classbuffer_size(cb, 4))
559                                 return false;
560
561                         cf->value = suck_float(cb);
562                         cptags[idx] = CONSTANT_Float;
563                         cpinfos[idx] = cf;
564
565                         idx++;
566                         break;
567                 }
568                                 
569                 case CONSTANT_Long: {
570                         constant_long *cl = NEW(constant_long);
571                                         
572 #if defined(ENABLE_STATISTICS)
573                         if (opt_stat)
574                                 count_const_pool_len += sizeof(constant_long);
575 #endif
576
577                         if (!suck_check_classbuffer_size(cb, 8))
578                                 return false;
579
580                         cl->value = suck_s8(cb);
581                         cptags[idx] = CONSTANT_Long;
582                         cpinfos[idx] = cl;
583                         idx += 2;
584                         if (idx > cpcount) {
585                                 exceptions_throw_classformaterror(c, "Invalid constant pool entry");
586                                 return false;
587                         }
588                         break;
589                 }
590                         
591                 case CONSTANT_Double: {
592                         constant_double *cd = NEW(constant_double);
593                                 
594 #if defined(ENABLE_STATISTICS)
595                         if (opt_stat)
596                                 count_const_pool_len += sizeof(constant_double);
597 #endif
598
599                         if (!suck_check_classbuffer_size(cb, 8))
600                                 return false;
601
602                         cd->value = suck_double(cb);
603                         cptags[idx] = CONSTANT_Double;
604                         cpinfos[idx] = cd;
605                         idx += 2;
606                         if (idx > cpcount) {
607                                 exceptions_throw_classformaterror(c, "Invalid constant pool entry");
608                                 return false;
609                         }
610                         break;
611                 }
612                                 
613                 case CONSTANT_Utf8: { 
614                         u4 length;
615
616                         /* number of bytes in the bytes array (not string-length) */
617                         if (!suck_check_classbuffer_size(cb, 2))
618                                 return false;
619
620                         length = suck_u2(cb);
621                         cptags[idx] = CONSTANT_Utf8;
622
623                         /* validate the string */
624                         if (!suck_check_classbuffer_size(cb, length))
625                                 return false;
626
627 #ifdef ENABLE_VERIFIER
628                         if (opt_verify &&
629                                 !is_valid_utf((char *) cb->pos, (char *) (cb->pos + length))) 
630                         {
631                                 exceptions_throw_classformaterror(c, "Invalid UTF-8 string");
632                                 return false;
633                         }
634 #endif /* ENABLE_VERIFIER */
635                         /* insert utf-string into the utf-symboltable */
636                         cpinfos[idx] = utf_new((char *) cb->pos, length);
637
638                         /* skip bytes of the string (buffer size check above) */
639                         suck_skip_nbytes(cb, length);
640                         idx++;
641                         break;
642                 }
643                                                                                 
644                 default:
645                         exceptions_throw_classformaterror(c, "Illegal constant pool type");
646                         return false;
647                 }  /* end switch */
648         } /* end while */
649
650
651         /* resolve entries in temporary structures */
652
653         while (forward_classes) {
654                 utf *name =
655                         class_getconstant(c, forward_classes->name_index, CONSTANT_Utf8);
656                 if (!name)
657                         return false;
658
659 #ifdef ENABLE_VERIFIER
660                 if (opt_verify && !is_valid_name_utf(name)) {
661                         exceptions_throw_classformaterror(c, "Class reference with invalid name");
662                         return false;
663                 }
664 #endif /* ENABLE_VERIFIER */
665
666                 /* add all class references to the descriptor_pool */
667
668                 if (!descriptor_pool_add_class(descpool, name))
669                         return false;
670
671                 cptags[forward_classes->thisindex] = CONSTANT_Class;
672
673                 /* the classref is created later */
674                 cpinfos[forward_classes->thisindex] = name;
675
676                 nfc = forward_classes;
677                 forward_classes = forward_classes->next;
678         }
679
680         while (forward_strings) {
681                 utf *text =
682                         class_getconstant(c, forward_strings->string_index, CONSTANT_Utf8);
683                 if (!text)
684                         return false;
685
686                 /* resolve utf-string */
687                 cptags[forward_strings->thisindex] = CONSTANT_String;
688                 cpinfos[forward_strings->thisindex] = text;
689                 
690                 nfs = forward_strings;
691                 forward_strings = forward_strings->next;
692         }
693
694         while (forward_nameandtypes) {
695                 constant_nameandtype *cn = NEW(constant_nameandtype);   
696
697 #if defined(ENABLE_STATISTICS)
698                 if (opt_stat)
699                         count_const_pool_len += sizeof(constant_nameandtype);
700 #endif
701
702                 /* resolve simple name and descriptor */
703                 cn->name = class_getconstant(c,
704                                                                          forward_nameandtypes->name_index,
705                                                                          CONSTANT_Utf8);
706                 if (!cn->name)
707                         return false;
708
709                 cn->descriptor = class_getconstant(c,
710                                                                                    forward_nameandtypes->sig_index,
711                                                                                    CONSTANT_Utf8);
712                 if (!cn->descriptor)
713                         return false;
714
715 #ifdef ENABLE_VERIFIER
716                 if (opt_verify) {
717                         /* check name */
718                         if (!is_valid_name_utf(cn->name)) {
719                                 exceptions_throw_classformaterror(c,
720                                                                                                   "Illegal Field name \"%s\"",
721                                                                                                   cn->name->text);
722
723                                 return false;
724                         }
725
726                         /* disallow referencing <clinit> among others */
727                         if (cn->name->text[0] == '<' && cn->name != utf_init) {
728                                 exceptions_throw_classformaterror(c, "Illegal reference to special method");
729                                 return false;
730                         }
731                 }
732 #endif /* ENABLE_VERIFIER */
733
734                 cptags[forward_nameandtypes->thisindex] = CONSTANT_NameAndType;
735                 cpinfos[forward_nameandtypes->thisindex] = cn;
736
737                 nfn = forward_nameandtypes;
738                 forward_nameandtypes = forward_nameandtypes->next;
739         }
740
741         while (forward_fieldmethints) {
742                 constant_nameandtype *nat;
743                 constant_FMIref *fmi = NEW(constant_FMIref);
744
745 #if defined(ENABLE_STATISTICS)
746                 if (opt_stat)
747                         count_const_pool_len += sizeof(constant_FMIref);
748 #endif
749                 /* resolve simple name and descriptor */
750
751                 nat = class_getconstant(c,
752                                                                 forward_fieldmethints->nameandtype_index,
753                                                                 CONSTANT_NameAndType);
754                 if (!nat)
755                         return false;
756
757                 /* add all descriptors in {Field,Method}ref to the descriptor_pool */
758
759                 if (!descriptor_pool_add(descpool, nat->descriptor, NULL))
760                         return false;
761
762                 /* the classref is created later */
763
764                 fmi->p.index = forward_fieldmethints->class_index;
765                 fmi->name = nat->name;
766                 fmi->descriptor = nat->descriptor;
767
768                 cptags[forward_fieldmethints->thisindex] = forward_fieldmethints->tag;
769                 cpinfos[forward_fieldmethints->thisindex] = fmi;
770         
771                 nff = forward_fieldmethints;
772                 forward_fieldmethints = forward_fieldmethints->next;
773         }
774
775         /* everything was ok */
776
777         return true;
778 }
779
780
781 /* loader_load_attribute_signature *********************************************
782
783    Signature_attribute {
784        u2 attribute_name_index;
785            u4 atrribute_length;
786            u2 signature_index;
787    }
788
789 *******************************************************************************/
790
791 #if defined(ENABLE_JAVASE)
792 bool loader_load_attribute_signature(classbuffer *cb, utf **signature)
793 {
794         classinfo *c;
795         u4         attribute_length;
796         u2         signature_index;
797
798         /* get classinfo */
799
800         c = cb->class;
801
802         /* check remaining bytecode */
803
804         if (!suck_check_classbuffer_size(cb, 4 + 2))
805                 return false;
806
807         /* check attribute length */
808
809         attribute_length = suck_u4(cb);
810
811         if (attribute_length != 2) {
812                 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
813                 return false;
814         }
815
816         if (*signature != NULL) {
817                 exceptions_throw_classformaterror(c, "Multiple Signature attributes");
818                 return false;
819         }
820
821         /* get signature */
822
823         signature_index = suck_u2(cb);
824
825         if (!(*signature = class_getconstant(c, signature_index, CONSTANT_Utf8)))
826                 return false;
827
828         return true;
829 }
830 #endif /* defined(ENABLE_JAVASE) */
831
832
833 /* load_class_from_sysloader ***************************************************
834
835    Load the class with the given name using the system class loader
836
837    IN:
838        name.............the classname
839
840    RETURN VALUE:
841        the loaded class, or
842            NULL if an exception has been thrown
843
844 *******************************************************************************/
845
846 classinfo *load_class_from_sysloader(utf *name)
847 {
848         methodinfo  *m;
849         classloader *cl;
850         classinfo   *c;
851
852         assert(class_java_lang_Object);
853         assert(class_java_lang_ClassLoader);
854         assert(class_java_lang_ClassLoader->state & CLASS_LINKED);
855         
856         m = class_resolveclassmethod(class_java_lang_ClassLoader,
857                                                                  utf_getSystemClassLoader,
858                                                                  utf_void__java_lang_ClassLoader,
859                                                                  class_java_lang_Object,
860                                                                  false);
861
862         if (!m)
863                 return false;
864
865         cl = vm_call_method(m, NULL);
866
867         if (!cl)
868                 return false;
869
870         c = load_class_from_classloader(name, cl);
871
872         return c;
873 }
874
875
876 /* load_class_from_classloader *************************************************
877
878    Load the class with the given name using the given user-defined class loader.
879
880    IN:
881        name.............the classname
882            cl...............user-defined class loader
883            
884    RETURN VALUE:
885        the loaded class, or
886            NULL if an exception has been thrown
887
888 *******************************************************************************/
889
890 classinfo *load_class_from_classloader(utf *name, classloader *cl)
891 {
892         java_handle_t *o;
893         classinfo     *c;
894         classinfo     *tmpc;
895         java_handle_t *string;
896 #if defined(ENABLE_RT_TIMING)
897         struct timespec time_start, time_lookup, time_prepare, time_java, 
898                                         time_cache;
899 #endif
900
901         RT_TIMING_GET_TIME(time_start);
902
903         assert(name);
904
905         /* lookup if this class has already been loaded */
906
907         c = classcache_lookup(cl, name);
908
909         RT_TIMING_GET_TIME(time_lookup);
910         RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_CL_LOOKUP);
911
912         if (c != NULL)
913                 return c;
914
915         /* if other class loader than bootstrap, call it */
916
917         if (cl != NULL) {
918                 methodinfo *lc;
919                 char       *text;
920                 s4          namelen;
921
922                 text = name->text;
923                 namelen = name->blength;
924
925                 /* handle array classes */
926                 if (text[0] == '[') {
927                         classinfo *comp;
928                         utf       *u;
929
930                         switch (text[1]) {
931                         case 'L':
932                                 /* check for cases like `[L;' or `[L[I;' or `[Ljava.lang.Object' */
933                                 if (namelen < 4 || text[2] == '[' || text[namelen - 1] != ';') {
934                                         exceptions_throw_classnotfoundexception(name);
935                                         return false;
936                                 }
937
938                                 u = utf_new(text + 2, namelen - 3);
939
940                                 if (!(comp = load_class_from_classloader(u, cl)))
941                                         return false;
942
943                                 /* create the array class */
944
945                                 c = class_array_of(comp, false);
946
947                                 tmpc = classcache_store(cl, c, true);
948
949                                 if (tmpc == NULL) {
950                                         /* exception, free the loaded class */
951                                         c->state &= ~CLASS_LOADING;
952                                         class_free(c);
953                                 }
954
955                                 return tmpc;
956
957                         case '[':
958                                 /* load the component class */
959
960                                 u = utf_new(text + 1, namelen - 1);
961
962                                 if (!(comp = load_class_from_classloader(u, cl)))
963                                         return false;
964
965                                 /* create the array class */
966
967                                 c = class_array_of(comp, false);
968
969                                 tmpc = classcache_store(cl, c, true);
970
971                                 if (tmpc == NULL) {
972                                         /* exception, free the loaded class */
973                                         c->state &= ~CLASS_LOADING;
974                                         class_free(c);
975                                 }
976
977                                 return tmpc;
978
979                         default:
980                                 /* primitive array classes are loaded by the bootstrap loader */
981
982                                 c = load_class_bootstrap(name);
983
984                                 return c;
985                         }
986                 }
987                 
988                 assert(class_java_lang_Object);
989
990                 lc = class_resolveclassmethod(cl->vftbl->class,
991                                                                           utf_loadClass,
992                                                                           utf_java_lang_String__java_lang_Class,
993                                                                           class_java_lang_Object,
994                                                                           true);
995
996                 if (!lc)
997                         return false; /* exception */
998
999                 /* move return value into `o' and cast it afterwards to a classinfo* */
1000
1001                 string = javastring_new_slash_to_dot(name);
1002
1003                 RT_TIMING_GET_TIME(time_prepare);
1004
1005                 o = vm_call_method(lc, cl, string);
1006
1007                 RT_TIMING_GET_TIME(time_java);
1008
1009                 c = LLNI_classinfo_unwrap(o);
1010
1011                 if (c != NULL) {
1012                         /* Store this class in the loaded class cache. If another
1013                            class with the same (initloader,name) pair has been
1014                            stored earlier it will be returned by classcache_store
1015                            In this case classcache_store may not free the class
1016                            because it has already been exposed to Java code which
1017                            may have kept references to that class. */
1018
1019                     tmpc = classcache_store(cl, c, false);
1020
1021                         if (tmpc == NULL) {
1022                                 /* exception, free the loaded class */
1023                                 c->state &= ~CLASS_LOADING;
1024                                 class_free(c);
1025                         }
1026
1027                         c = tmpc;
1028                 }
1029
1030                 RT_TIMING_GET_TIME(time_cache);
1031
1032                 RT_TIMING_TIME_DIFF(time_lookup , time_prepare, RT_TIMING_LOAD_CL_PREPARE);
1033                 RT_TIMING_TIME_DIFF(time_prepare, time_java   , RT_TIMING_LOAD_CL_JAVA);
1034                 RT_TIMING_TIME_DIFF(time_java   , time_cache  , RT_TIMING_LOAD_CL_CACHE);
1035
1036                 /* SUN compatible -verbose:class output */
1037
1038                 if (opt_verboseclass && (c != NULL) && (c->classloader == cl)) {
1039                         printf("[Loaded ");
1040                         utf_display_printable_ascii_classname(name);
1041                         printf("]\n");
1042                 }
1043
1044 #if defined(ENABLE_JVMTI)
1045                 /* fire Class Load JVMTI event */
1046                 if (jvmti) jvmti_ClassLoadPrepare(false, c);
1047 #endif
1048
1049
1050                 return c;
1051         } 
1052
1053         c = load_class_bootstrap(name);
1054
1055         return c;
1056 }
1057
1058
1059 /* load_class_bootstrap ********************************************************
1060         
1061    Load the class with the given name using the bootstrap class loader.
1062
1063    IN:
1064        name.............the classname
1065
1066    RETURN VALUE:
1067        loaded classinfo, or
1068            NULL if an exception has been thrown
1069
1070    SYNCHRONIZATION:
1071        load_class_bootstrap is synchronized. It can be treated as an
1072            atomic operation.
1073
1074 *******************************************************************************/
1075
1076 classinfo *load_class_bootstrap(utf *name)
1077 {
1078         classbuffer *cb;
1079         classinfo   *c;
1080         classinfo   *r;
1081 #if defined(ENABLE_RT_TIMING)
1082         struct timespec time_start, time_lookup, time_array, time_suck, 
1083                                         time_load, time_cache;
1084 #endif
1085
1086         RT_TIMING_GET_TIME(time_start);
1087
1088         /* for debugging */
1089
1090         assert(name);
1091
1092         /* lookup if this class has already been loaded */
1093
1094         r = classcache_lookup(NULL, name);
1095
1096         if (r != NULL) {
1097                 RT_TIMING_GET_TIME(time_lookup);
1098                 RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_BOOT_LOOKUP);
1099                 
1100                 return r;
1101         }
1102
1103         RT_TIMING_GET_TIME(time_lookup);
1104         RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_BOOT_LOOKUP);
1105                 
1106         /* create the classinfo */
1107
1108         c = class_create_classinfo(name);
1109
1110         /* handle array classes */
1111
1112         if (name->text[0] == '[') {
1113                 c = load_newly_created_array(c, NULL);
1114
1115                 if (c == NULL)
1116                         return NULL;
1117
1118                 assert(c->state & CLASS_LOADED);
1119
1120                 RT_TIMING_GET_TIME(time_array);
1121                 RT_TIMING_TIME_DIFF(time_start,time_array,RT_TIMING_LOAD_BOOT_ARRAY);
1122                 
1123                 return c;
1124         }
1125
1126 #if defined(ENABLE_STATISTICS)
1127         /* measure time */
1128
1129         if (opt_getcompilingtime)
1130                 compilingtime_stop();
1131
1132         if (opt_getloadingtime)
1133                 loadingtime_start();
1134 #endif
1135
1136         /* load classdata, throw exception on error */
1137
1138         cb = suck_start(c);
1139
1140         if (cb == NULL) {
1141                 /* this normally means, the classpath was not set properly */
1142
1143                 if (name == utf_java_lang_Object)
1144                         vm_abort("java/lang/NoClassDefFoundError: java/lang/Object");
1145
1146                 exceptions_throw_classnotfoundexception(name);
1147
1148                 return NULL;
1149         }
1150
1151         RT_TIMING_GET_TIME(time_suck);
1152         
1153         /* load the class from the buffer */
1154
1155         r = load_class_from_classbuffer(cb);
1156
1157         RT_TIMING_GET_TIME(time_load);
1158         
1159         if (!r) {
1160                 /* the class could not be loaded, free the classinfo struct */
1161
1162                 class_free(c);
1163
1164         } else {
1165                 /* Store this class in the loaded class cache this step also
1166                 checks the loading constraints. If the class has been loaded
1167                 before, the earlier loaded class is returned. */
1168
1169                 classinfo *res = classcache_store(NULL, c, true);
1170
1171                 if (!res) {
1172                         /* exception */
1173                         class_free(c);
1174                 }
1175
1176                 r = res;
1177         }
1178
1179         RT_TIMING_GET_TIME(time_cache);
1180         
1181         /* SUN compatible -verbose:class output */
1182
1183         if (opt_verboseclass && r) {
1184                 printf("[Loaded ");
1185                 utf_display_printable_ascii_classname(name);
1186                 printf(" from %s]\n", cb->path);
1187         }
1188
1189         /* free memory */
1190
1191         suck_stop(cb);
1192
1193 #if defined(ENABLE_STATISTICS)
1194         /* measure time */
1195
1196         if (opt_getloadingtime)
1197                 loadingtime_stop();
1198
1199         if (opt_getcompilingtime)
1200                 compilingtime_start();
1201 #endif
1202
1203         RT_TIMING_TIME_DIFF(time_lookup, time_suck , RT_TIMING_LOAD_BOOT_SUCK);
1204         RT_TIMING_TIME_DIFF(time_suck  , time_load , RT_TIMING_LOAD_BOOT_LOAD);
1205         RT_TIMING_TIME_DIFF(time_load  , time_cache, RT_TIMING_LOAD_BOOT_CACHE);
1206         RT_TIMING_TIME_DIFF(time_lookup, time_cache, RT_TIMING_LOAD_BOOT_TOTAL);
1207
1208         return r;
1209 }
1210
1211
1212 /* load_class_from_classbuffer *************************************************
1213         
1214    Loads everything interesting about a class from the class file. The
1215    'classinfo' structure must have been allocated previously.
1216
1217    The super class and the interfaces implemented by this class need
1218    not be loaded. The link is set later by the function 'class_link'.
1219
1220    SYNCHRONIZATION:
1221        This function is NOT synchronized!
1222    
1223 *******************************************************************************/
1224
1225 classinfo *load_class_from_classbuffer(classbuffer *cb)
1226 {
1227         classinfo *c;
1228         utf *name;
1229         utf *supername;
1230         u4 i,j;
1231         u4 ma, mi;
1232         s4 dumpsize;
1233         descriptor_pool *descpool;
1234 #if defined(ENABLE_STATISTICS)
1235         u4 classrefsize;
1236         u4 descsize;
1237 #endif
1238 #if defined(ENABLE_RT_TIMING)
1239         struct timespec time_start, time_checks, time_ndpool, time_cpool,
1240                                         time_setup, time_fields, time_methods, time_classrefs,
1241                                         time_descs,     time_setrefs, time_parsefds, time_parsemds,
1242                                         time_parsecpool, time_verify, time_attrs;
1243 #endif
1244
1245         RT_TIMING_GET_TIME(time_start);
1246
1247         /* get the classbuffer's class */
1248
1249         c = cb->class;
1250
1251         /* the class is already loaded */
1252
1253         if (c->state & CLASS_LOADED)
1254                 return c;
1255
1256 #if defined(ENABLE_STATISTICS)
1257         if (opt_stat)
1258                 count_class_loads++;
1259 #endif
1260
1261 #if !defined(NDEBUG)
1262         /* output for debugging purposes */
1263
1264         if (loadverbose)
1265                 log_message_class("Loading class: ", c);
1266 #endif
1267
1268         /* mark start of dump memory area */
1269
1270         dumpsize = dump_size();
1271
1272         /* class is currently loading */
1273
1274         c->state |= CLASS_LOADING;
1275
1276         if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
1277                 goto return_exception;
1278
1279         /* check signature */
1280
1281         if (suck_u4(cb) != MAGIC) {
1282                 exceptions_throw_classformaterror(c, "Bad magic number");
1283
1284                 goto return_exception;
1285         }
1286
1287         /* check version */
1288
1289         mi = suck_u2(cb);
1290         ma = suck_u2(cb);
1291
1292         if (!(ma < MAJOR_VERSION || (ma == MAJOR_VERSION && mi <= MINOR_VERSION))) {
1293                 exceptions_throw_unsupportedclassversionerror(c, ma, mi);
1294                 goto return_exception;
1295         }
1296
1297         RT_TIMING_GET_TIME(time_checks);
1298
1299         /* create a new descriptor pool */
1300
1301         descpool = descriptor_pool_new(c);
1302
1303         RT_TIMING_GET_TIME(time_ndpool);
1304
1305         /* load the constant pool */
1306
1307         if (!load_constantpool(cb, descpool))
1308                 goto return_exception;
1309
1310         RT_TIMING_GET_TIME(time_cpool);
1311
1312         /* ACC flags */
1313
1314         if (!suck_check_classbuffer_size(cb, 2))
1315                 goto return_exception;
1316
1317         /* We OR the flags here, as we set already some flags in
1318            class_create_classinfo. */
1319
1320         c->flags |= suck_u2(cb);
1321
1322         /* check ACC flags consistency */
1323
1324         if (c->flags & ACC_INTERFACE) {
1325                 if (!(c->flags & ACC_ABSTRACT)) {
1326                         /* We work around this because interfaces in JDK 1.1 are
1327                          * not declared abstract. */
1328
1329                         c->flags |= ACC_ABSTRACT;
1330                 }
1331
1332                 if (c->flags & ACC_FINAL) {
1333                         exceptions_throw_classformaterror(c,
1334                                                                                           "Illegal class modifiers: 0x%X",
1335                                                                                           c->flags);
1336                         goto return_exception;
1337                 }
1338
1339                 if (c->flags & ACC_SUPER) {
1340                         c->flags &= ~ACC_SUPER; /* kjc seems to set this on interfaces */
1341                 }
1342         }
1343
1344         if ((c->flags & (ACC_ABSTRACT | ACC_FINAL)) == (ACC_ABSTRACT | ACC_FINAL)) {
1345                 exceptions_throw_classformaterror(c,
1346                                                                                   "Illegal class modifiers: 0x%X",
1347                                                                                   c->flags);
1348                 goto return_exception;
1349         }
1350
1351         if (!suck_check_classbuffer_size(cb, 2 + 2))
1352                 goto return_exception;
1353
1354         /* this class */
1355
1356         i = suck_u2(cb);
1357
1358         if (!(name = (utf *) class_getconstant(c, i, CONSTANT_Class)))
1359                 goto return_exception;
1360
1361         if (c->name == utf_not_named_yet) {
1362                 /* we finally have a name for this class */
1363                 c->name = name;
1364                 class_set_packagename(c);
1365         }
1366         else if (name != c->name) {
1367                 exceptions_throw_noclassdeffounderror_wrong_name(c, name);
1368                 goto return_exception;
1369         }
1370
1371         /* retrieve superclass */
1372
1373         c->super.any = NULL;
1374
1375         if ((i = suck_u2(cb))) {
1376                 if (!(supername = (utf *) class_getconstant(c, i, CONSTANT_Class)))
1377                         goto return_exception;
1378
1379                 /* java.lang.Object may not have a super class. */
1380
1381                 if (c->name == utf_java_lang_Object) {
1382                         exceptions_throw_classformaterror(NULL, "java.lang.Object with superclass");
1383                         goto return_exception;
1384                 }
1385
1386                 /* Interfaces must have java.lang.Object as super class. */
1387
1388                 if ((c->flags & ACC_INTERFACE) && (supername != utf_java_lang_Object)) {
1389                         exceptions_throw_classformaterror(c, "Interfaces must have java.lang.Object as superclass");
1390                         goto return_exception;
1391                 }
1392
1393         } else {
1394                 supername = NULL;
1395
1396                 /* This is only allowed for java.lang.Object. */
1397
1398                 if (c->name != utf_java_lang_Object) {
1399                         exceptions_throw_classformaterror(c, "Bad superclass index");
1400                         goto return_exception;
1401                 }
1402         }
1403
1404         /* retrieve interfaces */
1405
1406         if (!suck_check_classbuffer_size(cb, 2))
1407                 goto return_exception;
1408
1409         c->interfacescount = suck_u2(cb);
1410
1411         if (!suck_check_classbuffer_size(cb, 2 * c->interfacescount))
1412                 goto return_exception;
1413
1414         c->interfaces = MNEW(classref_or_classinfo, c->interfacescount);
1415         for (i = 0; i < c->interfacescount; i++) {
1416                 /* the classrefs are created later */
1417                 if (!(c->interfaces[i].any = (utf *) class_getconstant(c, suck_u2(cb), CONSTANT_Class)))
1418                         goto return_exception;
1419         }
1420
1421         RT_TIMING_GET_TIME(time_setup);
1422
1423         /* load fields */
1424
1425         if (!suck_check_classbuffer_size(cb, 2))
1426                 goto return_exception;
1427
1428         c->fieldscount = suck_u2(cb);
1429         c->fields      = MNEW(fieldinfo, c->fieldscount);
1430
1431         MZERO(c->fields, fieldinfo, c->fieldscount);
1432
1433         for (i = 0; i < c->fieldscount; i++) {
1434                 if (!field_load(cb, &(c->fields[i]), descpool))
1435                         goto return_exception;
1436         }
1437
1438         RT_TIMING_GET_TIME(time_fields);
1439
1440         /* load methods */
1441
1442         if (!suck_check_classbuffer_size(cb, 2))
1443                 goto return_exception;
1444
1445         c->methodscount = suck_u2(cb);
1446         c->methods      = MNEW(methodinfo, c->methodscount);
1447
1448         MZERO(c->methods, methodinfo, c->methodscount);
1449         
1450         for (i = 0; i < c->methodscount; i++) {
1451                 if (!method_load(cb, &(c->methods[i]), descpool))
1452                         goto return_exception;
1453         }
1454
1455         RT_TIMING_GET_TIME(time_methods);
1456
1457         /* create the class reference table */
1458
1459         c->classrefs =
1460                 descriptor_pool_create_classrefs(descpool, &(c->classrefcount));
1461
1462         RT_TIMING_GET_TIME(time_classrefs);
1463
1464         /* allocate space for the parsed descriptors */
1465
1466         descriptor_pool_alloc_parsed_descriptors(descpool);
1467         c->parseddescs =
1468                 descriptor_pool_get_parsed_descriptors(descpool, &(c->parseddescsize));
1469
1470 #if defined(ENABLE_STATISTICS)
1471         if (opt_stat) {
1472                 descriptor_pool_get_sizes(descpool, &classrefsize, &descsize);
1473                 count_classref_len += classrefsize;
1474                 count_parsed_desc_len += descsize;
1475         }
1476 #endif
1477
1478         RT_TIMING_GET_TIME(time_descs);
1479
1480         /* put the classrefs in the constant pool */
1481         for (i = 0; i < c->cpcount; i++) {
1482                 if (c->cptags[i] == CONSTANT_Class) {
1483                         utf *name = (utf *) c->cpinfos[i];
1484                         c->cpinfos[i] = descriptor_pool_lookup_classref(descpool, name);
1485                 }
1486         }
1487
1488         /* set the super class reference */
1489
1490         if (supername) {
1491                 c->super.ref = descriptor_pool_lookup_classref(descpool, supername);
1492                 if (!c->super.ref)
1493                         goto return_exception;
1494         }
1495
1496         /* set the super interfaces references */
1497
1498         for (i = 0; i < c->interfacescount; i++) {
1499                 c->interfaces[i].ref =
1500                         descriptor_pool_lookup_classref(descpool,
1501                                                                                         (utf *) c->interfaces[i].any);
1502                 if (!c->interfaces[i].ref)
1503                         goto return_exception;
1504         }
1505
1506         RT_TIMING_GET_TIME(time_setrefs);
1507
1508         /* parse field descriptors */
1509
1510         for (i = 0; i < c->fieldscount; i++) {
1511                 c->fields[i].parseddesc =
1512                         descriptor_pool_parse_field_descriptor(descpool,
1513                                                                                                    c->fields[i].descriptor);
1514                 if (!c->fields[i].parseddesc)
1515                         goto return_exception;
1516         }
1517
1518         RT_TIMING_GET_TIME(time_parsefds);
1519
1520         /* parse method descriptors */
1521
1522         for (i = 0; i < c->methodscount; i++) {
1523                 methodinfo *m = &c->methods[i];
1524                 m->parseddesc =
1525                         descriptor_pool_parse_method_descriptor(descpool, m->descriptor,
1526                                                                                                         m->flags, class_get_self_classref(m->class));
1527                 if (!m->parseddesc)
1528                         goto return_exception;
1529
1530                 for (j = 0; j < m->rawexceptiontablelength; j++) {
1531                         if (!m->rawexceptiontable[j].catchtype.any)
1532                                 continue;
1533                         if ((m->rawexceptiontable[j].catchtype.ref =
1534                                  descriptor_pool_lookup_classref(descpool,
1535                                                 (utf *) m->rawexceptiontable[j].catchtype.any)) == NULL)
1536                                 goto return_exception;
1537                 }
1538
1539                 for (j = 0; j < m->thrownexceptionscount; j++) {
1540                         if (!m->thrownexceptions[j].any)
1541                                 continue;
1542                         if ((m->thrownexceptions[j].ref = descriptor_pool_lookup_classref(descpool,
1543                                                 (utf *) m->thrownexceptions[j].any)) == NULL)
1544                                 goto return_exception;
1545                 }
1546         }
1547
1548         RT_TIMING_GET_TIME(time_parsemds);
1549
1550         /* parse the loaded descriptors */
1551
1552         for (i = 0; i < c->cpcount; i++) {
1553                 constant_FMIref *fmi;
1554                 s4               index;
1555
1556                 switch (c->cptags[i]) {
1557                 case CONSTANT_Fieldref:
1558                         fmi = (constant_FMIref *) c->cpinfos[i];
1559                         fmi->parseddesc.fd =
1560                                 descriptor_pool_parse_field_descriptor(descpool,
1561                                                                                                            fmi->descriptor);
1562                         if (!fmi->parseddesc.fd)
1563                                 goto return_exception;
1564                         index = fmi->p.index;
1565                         fmi->p.classref =
1566                                 (constant_classref *) class_getconstant(c, index,
1567                                                                                                                 CONSTANT_Class);
1568                         if (!fmi->p.classref)
1569                                 goto return_exception;
1570                         break;
1571                 case CONSTANT_Methodref:
1572                 case CONSTANT_InterfaceMethodref:
1573                         fmi = (constant_FMIref *) c->cpinfos[i];
1574                         index = fmi->p.index;
1575                         fmi->p.classref =
1576                                 (constant_classref *) class_getconstant(c, index,
1577                                                                                                                 CONSTANT_Class);
1578                         if (!fmi->p.classref)
1579                                 goto return_exception;
1580                         fmi->parseddesc.md =
1581                                 descriptor_pool_parse_method_descriptor(descpool,
1582                                                                                                                 fmi->descriptor,
1583                                                                                                                 ACC_UNDEF,
1584                                                                                                                 fmi->p.classref);
1585                         if (!fmi->parseddesc.md)
1586                                 goto return_exception;
1587                         break;
1588                 }
1589         }
1590
1591         RT_TIMING_GET_TIME(time_parsecpool);
1592
1593 #ifdef ENABLE_VERIFIER
1594         /* Check if all fields and methods can be uniquely
1595          * identified by (name,descriptor). */
1596
1597         if (opt_verify) {
1598                 /* We use a hash table here to avoid making the
1599                  * average case quadratic in # of methods, fields.
1600                  */
1601                 static int shift = 0;
1602                 u2 *hashtab;
1603                 u2 *next; /* for chaining colliding hash entries */
1604                 size_t len;
1605                 size_t hashlen;
1606                 u2 index;
1607                 u2 old;
1608
1609                 /* Allocate hashtable */
1610                 len = c->methodscount;
1611                 if (len < c->fieldscount) len = c->fieldscount;
1612                 hashlen = 5 * len;
1613                 hashtab = MNEW(u2,(hashlen + len));
1614                 next = hashtab + hashlen;
1615
1616                 /* Determine bitshift (to get good hash values) */
1617                 if (!shift) {
1618                         len = sizeof(utf);
1619                         while (len) {
1620                                 len >>= 1;
1621                                 shift++;
1622                         }
1623                 }
1624
1625                 /* Check fields */
1626                 memset(hashtab, 0, sizeof(u2) * (hashlen + len));
1627
1628                 for (i = 0; i < c->fieldscount; ++i) {
1629                         fieldinfo *fi = c->fields + i;
1630
1631                         /* It's ok if we lose bits here */
1632                         index = ((((size_t) fi->name) +
1633                                           ((size_t) fi->descriptor)) >> shift) % hashlen;
1634
1635                         if ((old = hashtab[index])) {
1636                                 old--;
1637                                 next[i] = old;
1638                                 do {
1639                                         if (c->fields[old].name == fi->name &&
1640                                                 c->fields[old].descriptor == fi->descriptor) {
1641                                                 exceptions_throw_classformaterror(c, "Repetitive field name/signature");
1642                                                 goto return_exception;
1643                                         }
1644                                 } while ((old = next[old]));
1645                         }
1646                         hashtab[index] = i + 1;
1647                 }
1648
1649                 /* Check methods */
1650                 memset(hashtab, 0, sizeof(u2) * (hashlen + hashlen/5));
1651
1652                 for (i = 0; i < c->methodscount; ++i) {
1653                         methodinfo *mi = c->methods + i;
1654
1655                         /* It's ok if we lose bits here */
1656                         index = ((((size_t) mi->name) +
1657                                           ((size_t) mi->descriptor)) >> shift) % hashlen;
1658
1659                         /*{ JOWENN
1660                                 int dbg;
1661                                 for (dbg=0;dbg<hashlen+hashlen/5;++dbg){
1662                                         printf("Hash[%d]:%d\n",dbg,hashtab[dbg]);
1663                                 }
1664                         }*/
1665
1666                         if ((old = hashtab[index])) {
1667                                 old--;
1668                                 next[i] = old;
1669                                 do {
1670                                         if (c->methods[old].name == mi->name &&
1671                                                 c->methods[old].descriptor == mi->descriptor) {
1672                                                 exceptions_throw_classformaterror(c, "Repetitive method name/signature");
1673                                                 goto return_exception;
1674                                         }
1675                                 } while ((old = next[old]));
1676                         }
1677                         hashtab[index] = i + 1;
1678                 }
1679
1680                 MFREE(hashtab, u2, (hashlen + len));
1681         }
1682 #endif /* ENABLE_VERIFIER */
1683
1684         RT_TIMING_GET_TIME(time_verify);
1685
1686 #if defined(ENABLE_STATISTICS)
1687         if (opt_stat) {
1688                 size_classinfo  += sizeof(classinfo*) * c->interfacescount;
1689                 size_fieldinfo  += sizeof(fieldinfo)  * c->fieldscount;
1690                 size_methodinfo += sizeof(methodinfo) * c->methodscount;
1691         }
1692 #endif
1693
1694         /* load attribute structures */
1695
1696         if (!class_load_attributes(cb))
1697                 goto return_exception;
1698
1699         /* Pre Java 1.5 version don't check this. This implementation is like
1700            Java 1.5 do it: for class file version 45.3 we don't check it, older
1701            versions are checked.
1702          */
1703
1704         if (((ma == 45) && (mi > 3)) || (ma > 45)) {
1705                 /* check if all data has been read */
1706                 s4 classdata_left = ((cb->data + cb->size) - cb->pos);
1707
1708                 if (classdata_left > 0) {
1709                         exceptions_throw_classformaterror(c, "Extra bytes at the end of class file");
1710                         goto return_exception;
1711                 }
1712         }
1713
1714         RT_TIMING_GET_TIME(time_attrs);
1715
1716         /* release dump area */
1717
1718         dump_release(dumpsize);
1719
1720         /* revert loading state and class is loaded */
1721
1722         c->state = (c->state & ~CLASS_LOADING) | CLASS_LOADED;
1723
1724 #if defined(ENABLE_JVMTI)
1725         /* fire Class Prepare JVMTI event */
1726
1727         if (jvmti)
1728                 jvmti_ClassLoadPrepare(true, c);
1729 #endif
1730
1731 #if !defined(NDEBUG)
1732         if (loadverbose)
1733                 log_message_class("Loading done class: ", c);
1734 #endif
1735
1736         RT_TIMING_TIME_DIFF(time_start     , time_checks    , RT_TIMING_LOAD_CHECKS);
1737         RT_TIMING_TIME_DIFF(time_checks    , time_ndpool    , RT_TIMING_LOAD_NDPOOL);
1738         RT_TIMING_TIME_DIFF(time_ndpool    , time_cpool     , RT_TIMING_LOAD_CPOOL);
1739         RT_TIMING_TIME_DIFF(time_cpool     , time_setup     , RT_TIMING_LOAD_SETUP);
1740         RT_TIMING_TIME_DIFF(time_setup     , time_fields    , RT_TIMING_LOAD_FIELDS);
1741         RT_TIMING_TIME_DIFF(time_fields    , time_methods   , RT_TIMING_LOAD_METHODS);
1742         RT_TIMING_TIME_DIFF(time_methods   , time_classrefs , RT_TIMING_LOAD_CLASSREFS);
1743         RT_TIMING_TIME_DIFF(time_classrefs , time_descs     , RT_TIMING_LOAD_DESCS);
1744         RT_TIMING_TIME_DIFF(time_descs     , time_setrefs   , RT_TIMING_LOAD_SETREFS);
1745         RT_TIMING_TIME_DIFF(time_setrefs   , time_parsefds  , RT_TIMING_LOAD_PARSEFDS);
1746         RT_TIMING_TIME_DIFF(time_parsefds  , time_parsemds  , RT_TIMING_LOAD_PARSEMDS);
1747         RT_TIMING_TIME_DIFF(time_parsemds  , time_parsecpool, RT_TIMING_LOAD_PARSECP);
1748         RT_TIMING_TIME_DIFF(time_parsecpool, time_verify    , RT_TIMING_LOAD_VERIFY);
1749         RT_TIMING_TIME_DIFF(time_verify    , time_attrs     , RT_TIMING_LOAD_ATTRS);
1750         RT_TIMING_TIME_DIFF(time_start     , time_attrs     , RT_TIMING_LOAD_TOTAL);
1751
1752         return c;
1753
1754 return_exception:
1755         /* release dump area */
1756
1757         dump_release(dumpsize);
1758
1759         /* an exception has been thrown */
1760
1761         return NULL;
1762 }
1763
1764
1765 /* load_newly_created_array ****************************************************
1766
1767    Load a newly created array class.
1768
1769         RETURN VALUE:
1770             c....................the array class C has been loaded
1771                 other classinfo......the array class was found in the class cache, 
1772                                      C has been freed
1773             NULL.................an exception has been thrown
1774
1775         Note:
1776                 This is an internal function. Do not use it unless you know exactly
1777                 what you are doing!
1778
1779                 Use one of the load_class_... functions for general array class loading.
1780
1781 *******************************************************************************/
1782
1783 classinfo *load_newly_created_array(classinfo *c, classloader *loader)
1784 {
1785         classinfo         *comp = NULL;
1786         methodinfo        *clone;
1787         methoddesc        *clonedesc;
1788         constant_classref *classrefs;
1789         char              *text;
1790         s4                 namelen;
1791         utf               *u;
1792
1793         text    = c->name->text;
1794         namelen = c->name->blength;
1795
1796         /* Check array class name */
1797
1798         if ((namelen < 2) || (text[0] != '[')) {
1799                 exceptions_throw_classnotfoundexception(c->name);
1800                 return NULL;
1801         }
1802
1803         /* Check the element type */
1804
1805         switch (text[1]) {
1806         case '[':
1807                 /* c is an array of arrays. We have to create the component class. */
1808
1809                 u = utf_new(text + 1, namelen - 1);
1810
1811                 comp = load_class_from_classloader(u, loader);
1812
1813                 if (comp == NULL)
1814                         return NULL;
1815
1816                 assert(comp->state & CLASS_LOADED);
1817
1818                 /* the array's flags are that of the component class */
1819                 c->flags = (comp->flags & ~ACC_INTERFACE) | ACC_FINAL | ACC_ABSTRACT;
1820                 c->classloader = comp->classloader;
1821                 break;
1822
1823         case 'L':
1824                 /* c is an array of objects. */
1825
1826                 /* check for cases like `[L;' or `[L[I;' or `[Ljava.lang.Object' */
1827                 if ((namelen < 4) || (text[2] == '[') || (text[namelen - 1] != ';')) {
1828                         exceptions_throw_classnotfoundexception(c->name);
1829                         return NULL;
1830                 }
1831
1832                 u = utf_new(text + 2, namelen - 3);
1833
1834                 if (!(comp = load_class_from_classloader(u, loader)))
1835                         return NULL;
1836
1837                 assert(comp->state & CLASS_LOADED);
1838
1839                 /* the array's flags are that of the component class */
1840                 c->flags = (comp->flags & ~ACC_INTERFACE) | ACC_FINAL | ACC_ABSTRACT;
1841                 c->classloader = comp->classloader;
1842                 break;
1843
1844         default:
1845                 /* c is an array of a primitive type */
1846
1847                 /* check for cases like `[II' and whether the character is a
1848                    valid primitive type */
1849
1850                 if ((namelen > 2) || (primitive_class_get_by_char(text[1]) == NULL)) {
1851                         exceptions_throw_classnotfoundexception(c->name);
1852                         return NULL;
1853                 }
1854
1855                 /* the accessibility of the array class is public (VM Spec 5.3.3) */
1856                 c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT;
1857                 c->classloader = NULL;
1858         }
1859
1860         assert(class_java_lang_Object);
1861 #if defined(ENABLE_JAVASE)
1862         assert(class_java_lang_Cloneable);
1863         assert(class_java_io_Serializable);
1864 #endif
1865
1866         /* setup the array class */
1867
1868         c->super.cls = class_java_lang_Object;
1869
1870 #if defined(ENABLE_JAVASE)
1871
1872         c->interfacescount   = 2;
1873     c->interfaces        = MNEW(classref_or_classinfo, 2);
1874         c->interfaces[0].cls = class_java_lang_Cloneable;
1875         c->interfaces[1].cls = class_java_io_Serializable;
1876
1877 #elif defined(ENABLE_JAVAME_CLDC1_1)
1878
1879         c->interfacescount   = 0;
1880         c->interfaces        = NULL;
1881
1882 #else
1883 # error unknow Java configuration
1884 #endif
1885
1886         c->methodscount = 1;
1887         c->methods = MNEW(methodinfo, c->methodscount);
1888         MZERO(c->methods, methodinfo, c->methodscount);
1889
1890         classrefs = MNEW(constant_classref, 2);
1891         CLASSREF_INIT(classrefs[0], c, c->name);
1892         CLASSREF_INIT(classrefs[1], c, utf_java_lang_Object);
1893
1894         /* create descriptor for clone method */
1895         /* we need one paramslot which is reserved for the 'this' parameter */
1896         clonedesc = NEW(methoddesc);
1897         clonedesc->returntype.type = TYPE_ADR;
1898         clonedesc->returntype.classref = classrefs + 1;
1899         clonedesc->returntype.arraydim = 0;
1900         /* initialize params to "empty", add real params below in
1901            descriptor_params_from_paramtypes */
1902         clonedesc->paramcount = 0;
1903         clonedesc->paramslots = 0;
1904         clonedesc->paramtypes[0].classref = classrefs + 0;
1905         clonedesc->params = NULL;
1906
1907         /* create methodinfo */
1908
1909         clone = c->methods;
1910         MSET(clone, 0, methodinfo, 1);
1911
1912 #if defined(ENABLE_THREADS)
1913         lock_init_object_lock(&clone->header);
1914 #endif
1915
1916         /* ATTENTION: if you delete the ACC_NATIVE below, set
1917            clone->maxlocals=1 (interpreter related) */
1918
1919         clone->flags      = ACC_PUBLIC | ACC_NATIVE;
1920         clone->name       = utf_clone;
1921         clone->descriptor = utf_void__java_lang_Object;
1922         clone->parseddesc = clonedesc;
1923         clone->class      = c;
1924
1925         /* parse the descriptor to get the register allocation */
1926
1927         if (!descriptor_params_from_paramtypes(clonedesc, clone->flags))
1928                 return false;
1929
1930         clone->code = codegen_generate_stub_native(clone, BUILTIN_clone);
1931
1932         /* XXX: field: length? */
1933
1934         /* array classes are not loaded from class files */
1935
1936         c->state          |= CLASS_LOADED;
1937         c->parseddescs    = (u1 *) clonedesc;
1938         c->parseddescsize = sizeof(methodinfo);
1939         c->classrefs      = classrefs;
1940         c->classrefcount  = 1;
1941
1942         /* insert class into the loaded class cache */
1943         /* XXX free classinfo if NULL returned? */
1944
1945         return classcache_store(loader, c, true);
1946 }
1947
1948
1949 /* loader_close ****************************************************************
1950
1951    Frees all resources.
1952         
1953 *******************************************************************************/
1954
1955 void loader_close(void)
1956 {
1957         /* empty */
1958 }
1959
1960
1961 /*
1962  * These are local overrides for various environment variables in Emacs.
1963  * Please do not remove this and leave it at the end of the file, where
1964  * Emacs will automagically detect them.
1965  * ---------------------------------------------------------------------
1966  * Local variables:
1967  * mode: c
1968  * indent-tabs-mode: t
1969  * c-basic-offset: 4
1970  * tab-width: 4
1971  * End:
1972  * vim:noexpandtab:sw=4:ts=4:
1973  */