6 A \textit{Java Virtual Machine} (JVM) dynamically loads, links and
7 initializes classes and interfaces when they are needed. Loading a
8 class or interface means locating the binary representation---the
9 class files---and creating a class of interface structure from that
10 binary representation. Linking takes a loaded class or interface and
11 transfers it into the runtime state of the \textit{Java Virtual
12 Machine} so that it can be executed. Initialization of a class or
13 interface means executing the static class of interface initializer
16 The following sections describe the process of loading, linking and
17 initalizing a class or interface in the CACAO \textit{Java Virtual
18 Machine} in greater detail. Further the used data structures and
19 techniques used in CACAO and the interaction with the GNU classpath
23 \section{System class loader}
25 The class loader of a \textit{Java Virtual Machine} (JVM) is
26 responsible for loading all type of classes and interfaces into the
27 runtime system of the JVM. Every JVM has a \textit{system class
28 loader} which is implemented in \texttt{java.lang.ClassLoader} and
29 this class interacts via native function calls with the JVM itself.
31 The \textit{GNU classpath} implements the system class loader in
32 \texttt{gnu.java.lang.SystemClassLoader} which extends
33 \texttt{java.lang.ClassLoader} and interacts with the JVM. The
34 \textit{bootstrap class loader} is implemented in
35 \texttt{java.lang.ClassLoader} plus the JVM depended class
36 \texttt{java.lang.VMClassLoader}. \texttt{java.lang.VMClassLoader} is
37 the main class how the bootstrap class loader of the GNU classpath
38 interacts with the JVM. The main functions of this class is
41 static final native Class loadClass(String name, boolean resolve)
42 throws ClassNotFoundException;
45 This is a native function implemented in the CACAO JVM, which is
46 located in \texttt{nat/VMClassLoader.c} and calls the internal loader
47 functions of CACAO. If the \texttt{name} argument is \texttt{NULL}, a
48 new \texttt{java.lang.NullPointerException} is created and the
49 function returns \texttt{NULL}.
51 If the \texttt{name} is non-NULL a new UTF8 string of the class' name
52 is created in the internal \textit{symbol table} via
55 utf *javastring_toutf(java_lang_String *string, bool isclassname);
58 This function converts a \texttt{java.lang.String} string into the
59 internal used UTF8 string representation. \texttt{isclassname} tells
60 the function to convert any \texttt{.} (dots) found in the class name
61 into \texttt{/} (slashes), so the class loader can find the specified
64 Then a new \texttt{classinfo} structure is created via the
67 classinfo *class_new(utf *classname);
70 function call. This function creates a unique representation of this
71 class, identified by its name, in the JVM's internal \textit{class
72 hashtable}. The newly created \texttt{classinfo} structure is
73 initialized with correct values, like \texttt{loaded = false;},
74 \texttt{linked = false;} and \texttt{initialized = false;}. This
75 guarantees a definite state of a new class.
77 The next step is to actually load the class requested. Thus the main
81 classinfo *class_load(classinfo *c);
84 is called, which is a wrapper function to the real loader function
87 classinfo *class_load_intern(classbuffer *cb);
90 This wrapper function is required to ensure some requirements:
93 \item enter a monitor on the \texttt{classinfo} structure, so that
94 only one thread can load the same class at the same time
96 \item initialize the \texttt{classbuffer} structure with the actual
99 \item remove the \texttt{classinfo} structure from the internal table
100 if we got an exception during loading
102 \item free any allocated memory and leave the monitor
105 The \texttt{class\_load\_intern} functions preforms the actual loading
106 of the binary representation of the class or interface. During loading
107 some verifier checks are performed which can throw a
108 \texttt{java.lang.ClassFormatError} or
109 \texttt{java.lang.NoClassDefFoundError}. Some of these
110 \texttt{java.lang.ClassFormatError} checks are
113 \item \textit{Truncated class file} --- unexpected end of class file
116 \item \textit{Bad magic number} --- class file does not contain the magic bytes
119 \item \textit{Unsupported major.minor version} --- the bytecode
120 version of the given class file is not supported by the JVM
123 After some loaded bytes, the class' constant pool is loaded via
126 static bool class_loadcpool(classbuffer *cb, classinfo *c);
129 from the \texttt{constant\_pool} table in the binary representation of
130 the class of interface. The constant pool needs to be parsed in two
131 passes. In the first pass the information loaded is saved in temporary
132 structures, which are further processed in the second pass, when the
133 complete constant pool has been traversed. Only when the whole
134 constant pool entries have been loaded, any constant pool entry can be
135 completely resolved, but this resolving can only be done in a specific
139 \item \texttt{CONSTANT\_Class}
141 \item \texttt{CONSTANT\_String}
143 \item \texttt{CONSTANT\_NameAndType}
145 \item \texttt{CONSTANT\_Fieldref}, \texttt{CONSTANT\_Methodref} and
146 \texttt{CONSTANT\_InterfaceMethodref} --- these are combined into one
150 The remaining constant pool types \texttt{CONSTANT\_Integer},
151 \texttt{CONSTANT\_Float}, \texttt{CONSTANT\_Long},
152 \texttt{CONSTANT\_Double} and \texttt{CONSTANT\_Utf8} can be resolved
153 in the first pass and need no further processing.
155 These are the temporary structures used to \textit{forward} the data
156 from the first pass into the second:
159 /* CONSTANT_Class entries */
160 typedef struct forward_class {
161 struct forward_class *next;
166 /* CONSTANT_String */
167 typedef struct forward_string {
168 struct forward_string *next;
173 /* CONSTANT_NameAndType */
174 typedef struct forward_nameandtype {
175 struct forward_nameandtype *next;
179 } forward_nameandtype;
181 /* CONSTANT_Fieldref, CONSTANT_Methodref or CONSTANT_InterfaceMethodref */
182 typedef struct forward_fieldmethint {
183 struct forward_fieldmethint *next;
187 u2 nameandtype_index;
188 } forward_fieldmethint;
191 The \texttt{classinfo} structure has two pointers to arrays which
192 contain the class' constant pool infos, namely: \texttt{cptags} and
193 \texttt{cpinfos}. \texttt{cptags} contains the type of the constant
194 pool entry. \texttt{cpinfos} contains a pointer to the constant pool
195 entry itself. In the second pass the references are resolved and the
196 runtime structures are created. In further detail this includes for
199 \item \texttt{CONSTANT\_Class}: get the UTF8 name string of the
200 class, store type \texttt{CONSTANT\_Class} in \texttt{cptags}, create
201 a class in the class hashtable with the UTF8 name and store the
202 pointer to the new class in \texttt{cpinfos}
204 \item \texttt{CONSTANT\_String}: get the UTF8 string of the
205 referenced string, store type \texttt{CONSTANT\_String} in
206 \texttt{cptags} and store the UTF8 string pointer into
209 \item \texttt{CONSTANT\_NameAndType}: create a
210 \texttt{constant\_nameandtype} structure, get the UTF8 name and
211 description string of the field or method and store them into the
212 \texttt{constant\_nameandtype} structure, store type
213 \texttt{CONSTANT\_NameAndType} into \texttt{cptags} and store a
214 pointer to the \texttt{constant\_nameandtype} structure into
217 \item \texttt{CONSTANT\_Fieldref}, \texttt{CONSTANT\_Methodref} and
218 \texttt{CONSTANT\_InterfaceMethodref}: create a
219 \texttt{constant\_FMIref} structure, get the referenced
220 \texttt{constant\_nameandtype} structure which contains the name and
221 descriptor resolved in a previous step and store the name and
222 descriptor into the \texttt{constant\_FMIref} structure, get the
223 pointer of the referenced class, which was created in a previous
224 step, and store the pointer of the class into the
225 \texttt{constant\_FMIref} structure, store the type of the current
226 constant pool entry in \texttt{cptags} and store a pointer to
227 \texttt{constant\_FMIref} in \texttt{cpinfos}
230 After we have loaded the complete constant pool and after loading the
231 class flags, we can resolve the class and super class of the currently
232 loaded class or interface.
235 \section{Data structures}
237 \section{Dynamic class loader}
239 \section{Eager - lazy class loading}
243 \section{Initialization}