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