blob: e1dcbf89781db443023463ca7059eccdb8505ce7 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: cshapiro@google.com (Carl Shapiro)
3
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07004#include "class_linker.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005
6#include <vector>
7#include <utility>
8
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "casts.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070010#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "dex_verifier.h"
12#include "heap.h"
13#include "logging.h"
14#include "monitor.h"
15#include "object.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070016#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "scoped_ptr.h"
18#include "thread.h"
19#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
22
Carl Shapiro2ed144c2011-07-26 16:52:08 -070023ClassLinker* ClassLinker::Create(const std::vector<DexFile*>& boot_class_path) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070024 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025 class_linker->Init(boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070026 // TODO: check for failure during initialization
27 return class_linker.release();
28}
29
Carl Shapiro2ed144c2011-07-26 16:52:08 -070030void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070031 init_done_ = false;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070033 // java_lang_Class comes first, its needed for AllocClass
34 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(Class)));
35 CHECK(java_lang_Class != NULL);
36 java_lang_Class->descriptor_ = "Ljava/lang/Class;";
37 java_lang_Class->object_size_ = sizeof(Class);
38 java_lang_Class->klass_ = java_lang_Class;
39 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070040
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070041 // java_lang_Object comes next so that object_array_class can be created
42 Class* java_lang_Object = AllocClass(java_lang_Class);
43 CHECK(java_lang_Object != NULL);
44 java_lang_Object->descriptor_ = "Ljava/lang/Object;";
45 // backfill Object as the super class of Class
46 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstroma0808032011-07-18 00:39:23 -070047
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070048 // object_array_class is for root_classes to provide the storage for these classes
49 Class* object_array_class = AllocClass(java_lang_Class);
50 CHECK(object_array_class != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -070051
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070052 // String and char[] are necessary so that FindClass can assign names to members
Jesse Wilson14150742011-07-29 19:04:44 -040053 Class* java_lang_String = AllocClass(java_lang_Class);
54 CHECK(java_lang_String != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070055 java_lang_String->descriptor_ = "Ljava/lang/String;";
56 CHECK_LT(java_lang_String->object_size_, sizeof(String));
Jesse Wilson14150742011-07-29 19:04:44 -040057 java_lang_String->object_size_ = sizeof(String);
Jesse Wilson8989d992011-08-02 13:39:42 -070058 Class* char_array_class = AllocClass(java_lang_Class);
59 CHECK(char_array_class != NULL);
Jesse Wilson14150742011-07-29 19:04:44 -040060
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070061 // Field and Method are necessary so that FindClass can link members
62 Class* java_lang_reflect_Field = AllocClass(java_lang_Class);
63 CHECK(java_lang_reflect_Field != NULL);
64 java_lang_reflect_Field->descriptor_ = "Ljava/lang/reflect/Field;";
65 CHECK_LT(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField)));
66 java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField));
67 Class* java_lang_reflect_Method = AllocClass(java_lang_Class);
68 java_lang_reflect_Method->descriptor_ = "Ljava/lang/reflect/Method;";
69 CHECK(java_lang_reflect_Method != NULL);
70 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
71 java_lang_reflect_Method->object_size_ = sizeof(Method);
72
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070073 // create storage for root classes, save away our work so far
74 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
75 class_roots_->Set(kJavaLangClass, java_lang_Class);
76 class_roots_->Set(kJavaLangObject, java_lang_Object);
77 class_roots_->Set(kObjectArrayClass, object_array_class);
Jesse Wilson14150742011-07-29 19:04:44 -040078 class_roots_->Set(kJavaLangString, java_lang_String);
Jesse Wilson8989d992011-08-02 13:39:42 -070079 class_roots_->Set(kCharArrayClass, char_array_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070080 class_roots_->Set(kJavaLangReflectField, java_lang_reflect_Field);
81 class_roots_->Set(kJavaLangReflectMethod, java_lang_reflect_Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070082 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -070083
Jesse Wilson8989d992011-08-02 13:39:42 -070084 String::InitClasses(java_lang_String, char_array_class);
85 // Now AllocString* can be used
86
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070087 // setup boot_class_path_ now that we can use AllocObjectArray to
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070088 // create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -070089 for (size_t i = 0; i != boot_class_path.size(); ++i) {
90 AppendToBootClassPath(boot_class_path[i]);
91 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070092 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -070093
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070094 // run Class, Field, and Method through FindSystemClass.
95 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070096 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070097 Class* Class_class = FindSystemClass(java_lang_Class->GetDescriptor());
98 CHECK_EQ(java_lang_Class, Class_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070099 CHECK_LT(java_lang_Class->object_size_, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700100 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700101 Class* Field_class = FindSystemClass(java_lang_reflect_Field->GetDescriptor());
102 CHECK_EQ(java_lang_reflect_Field, Field_class);
103 CHECK_LT(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700104 java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700105 Class* Method_class = FindSystemClass(java_lang_reflect_Method->GetDescriptor());
106 CHECK_EQ(java_lang_reflect_Method, Method_class);
107 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700108 java_lang_reflect_Method->object_size_ = sizeof(Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700109
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700110 // Object and String just need more minimal setup, since they do not have extra C++ fields.
111 Class* Object_class = FindSystemClass(java_lang_Object->GetDescriptor());
112 CHECK_EQ(java_lang_Object, Object_class);
113 CHECK_EQ(java_lang_Object->object_size_, sizeof(Object));
114 Class* String_class = FindSystemClass(java_lang_String->GetDescriptor());
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700115 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700116 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700117
118 // Setup the ClassLoaders, adjusting the object_size_ as necessary
119 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
120 CHECK(java_lang_ClassLoader != NULL);
121 CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader));
122 java_lang_ClassLoader->object_size_ = sizeof(ClassLoader);
123 class_roots_->Set(kJavaLangClassLoader, java_lang_ClassLoader);
124 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
125 CHECK(dalvik_system_BaseDexClassLoader != NULL);
126 CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader));
127 class_roots_->Set(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
128 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
129 CHECK(dalvik_system_PathClassLoader != NULL);
130 CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader));
131 class_roots_->Set(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700132
133 // Setup a single, global copy of "interfaces" and "iftable" for
134 // reuse across array classes
135 Class* java_lang_Cloneable = AllocClass();
136 CHECK(java_lang_Cloneable != NULL);
137 java_lang_Cloneable->descriptor_ = "Ljava/lang/Cloneable;";
138
139 Class* java_io_Serializable = AllocClass();
140 CHECK(java_io_Serializable != NULL);
141 java_io_Serializable->descriptor_ = "Ljava/io/Serializable;";
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700142
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700143 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700144 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700145 array_interfaces_->Set(0, java_lang_Cloneable);
146 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700147
148 // We assume that Cloneable/Serializable don't have superinterfaces --
149 // normally we'd have to crawl up and explicitly list all of the
150 // supers as well. These interfaces don't have any methods, so we
151 // don't have to worry about the ifviPool either.
152 array_iftable_ = new InterfaceEntry[2];
153 CHECK(array_iftable_ != NULL);
154 memset(array_iftable_, 0, sizeof(InterfaceEntry) * 2);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700155 array_iftable_[0].SetClass(array_interfaces_->Get(0));
156 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700157 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700158
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700159 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700160 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
161 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700162
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700163 // Setup the primitive type classes.
164 class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B"));
165 class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C"));
166 class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D"));
167 class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F"));
168 class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I"));
169 class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J"));
170 class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S"));
171 class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
172 class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V"));
173 // now we can use FindSystemClass for anything, including for "[C"
174
Jesse Wilson8989d992011-08-02 13:39:42 -0700175 // run char[] through FindClass to complete initialization
176 Class* found_char_array_class = FindSystemClass("[C");
177 CHECK_EQ(char_array_class, found_char_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700178
179 // ensure all class_roots_ were initialized
180 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700181 CHECK(GetClassRoot(static_cast<ClassRoot>(i)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700182 }
183
184 // disable the slow paths in FindClass and CreatePrimitiveClass now
185 // that Object, Class, and Object[] are setup
186 init_done_ = true;
187}
188
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700189void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700190 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700191
192 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700193 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700194 }
195
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700196 {
197 MutexLock mu(classes_lock_);
198 typedef Table::const_iterator It; // TODO: C++0x auto
199 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
200 root_visitor(it->second, arg);
201 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700202 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700203
204 intern_table_.VisitRoots(root_visitor, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700205
Brian Carlstromb88e9442011-07-28 15:15:51 -0700206 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700207}
208
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700209DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700210 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700211}
212
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700213Class* ClassLinker::AllocClass(Class* java_lang_Class) {
214 return down_cast<Class*>(Object::Alloc(java_lang_Class));
215}
216
217Class* ClassLinker::AllocClass() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700218 return AllocClass(GetClassRoot(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700219}
220
221StaticField* ClassLinker::AllocStaticField() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700222 return down_cast<StaticField*>(Object::Alloc(GetClassRoot(kJavaLangReflectField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700223}
224
225InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700226 return down_cast<InstanceField*>(Object::Alloc(GetClassRoot(kJavaLangReflectField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700227}
228
229Method* ClassLinker::AllocMethod() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700230 return down_cast<Method*>(Object::Alloc(GetClassRoot(kJavaLangReflectMethod)));
231}
232
233// TODO remove once we can use java.lang.Class.getSystemClassLoader
234PathClassLoader* ClassLinker::AllocPathClassLoader(std::vector<const DexFile*> dex_files) {
235 PathClassLoader* cl = down_cast<PathClassLoader*>(Object::Alloc(GetClassRoot(kDalvikSystemPathClassLoader)));
236 cl->SetClassPath(dex_files);
237 return cl;
Carl Shapiro565f5072011-07-10 13:39:43 -0700238}
239
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700240Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700241 ClassLoader* class_loader) {
242 // TODO remove this contrived parent class loader check when we have a real ClassLoader.
243 if (class_loader != NULL) {
244 Class* klass = FindClass(descriptor, NULL);
245 if (klass != NULL) {
246 return klass;
247 }
248 }
249
Carl Shapirob5573532011-07-12 18:22:59 -0700250 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700251 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700252 CHECK(!self->IsExceptionPending());
253 // Find the class in the loaded classes table.
254 Class* klass = LookupClass(descriptor, class_loader);
255 if (klass == NULL) {
256 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700257 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700258 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700259 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700260 DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_);
261 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700262 if (pair.second == NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700263 LG << "Class " << descriptor << " not found in class loader " << class_loader; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700264 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700265 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700266 const DexFile& dex_file = *pair.first;
267 const DexFile::ClassDef& dex_class_def = *pair.second;
268 DexCache* dex_cache = FindDexCache(pair.first);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700269 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700270 if (!init_done_) {
271 // finish up init of hand crafted class_roots_
272 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700273 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700274 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700275 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400276 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700277 klass = GetClassRoot(kJavaLangString);
278 } else if (descriptor == "Ljava/lang/reflect/Field;") {
279 klass = GetClassRoot(kJavaLangReflectField);
280 } else if (descriptor == "Ljava/lang/reflect/Method;") {
281 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700282 } else {
283 klass = AllocClass();
284 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700285 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700286 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700287 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700288 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700289 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700290 // Check for a pending exception during load
291 if (self->IsExceptionPending()) {
292 // TODO: free native allocations in klass
293 return NULL;
294 }
295 {
296 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700297 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700298 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700299 bool success = InsertClass(klass); // TODO just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700300 if (!success) {
301 // We may fail to insert if we raced with another thread.
302 klass->clinit_thread_id_ = 0;
303 // TODO: free native allocations in klass
304 klass = LookupClass(descriptor, class_loader);
305 CHECK(klass != NULL);
306 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700307 // Finish loading (if necessary) by finding parents
308 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
309 // Loading failed.
310 // TODO: CHECK(self->IsExceptionPending());
311 lock.NotifyAll();
312 return NULL;
313 }
314 CHECK(klass->IsLoaded());
315 // Link the class (if necessary)
316 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700317 // Linking failed.
318 // TODO: CHECK(self->IsExceptionPending());
319 lock.NotifyAll();
320 return NULL;
321 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700322 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700323 }
324 }
325 }
326 // Link the class if it has not already been linked.
327 if (!klass->IsLinked() && !klass->IsErroneous()) {
328 ObjectLock lock(klass);
329 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700330 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700331 LG << "Recursive link"; // TODO: ClassCircularityError
332 return NULL;
333 }
334 // Wait for the pending initialization to complete.
335 while (!klass->IsLinked() && !klass->IsErroneous()) {
336 lock.Wait();
337 }
338 }
339 if (klass->IsErroneous()) {
340 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
341 return NULL;
342 }
343 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700344 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700345 CHECK(!self->IsExceptionPending());
346 return klass;
347}
348
Brian Carlstromf615a612011-07-23 12:50:34 -0700349void ClassLinker::LoadClass(const DexFile& dex_file,
350 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700351 Class* klass,
352 ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700353 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700354 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700355 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700356 const byte* class_data = dex_file.GetClassData(dex_class_def);
357 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700358
Brian Carlstromf615a612011-07-23 12:50:34 -0700359 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700360 CHECK(descriptor != NULL);
361
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700362 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700363 klass->descriptor_.set(descriptor);
364 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700365 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700366 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700367 klass->primitive_type_ = Class::kPrimNot;
368 klass->status_ = Class::kStatusIdx;
369
370 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700371 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700372
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700373 size_t num_static_fields = header.static_fields_size_;
374 size_t num_instance_fields = header.instance_fields_size_;
375 size_t num_direct_methods = header.direct_methods_size_;
376 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700377
Brian Carlstromf615a612011-07-23 12:50:34 -0700378 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700379
380 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700381 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700382
383 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700384 DCHECK(klass->sfields_ == NULL);
385 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700386 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700387 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700388 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700389 DexFile::Field dex_field;
390 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700391 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700392 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700393 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700394 }
395 }
396
397 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700398 DCHECK(klass->ifields_ == NULL);
399 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700400 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700401 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700402 uint32_t last_idx = 0;
403 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700404 DexFile::Field dex_field;
405 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700406 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700407 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700408 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700409 }
410 }
411
412 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700413 DCHECK(klass->direct_methods_ == NULL);
414 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700415 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700416 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700417 uint32_t last_idx = 0;
418 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700419 DexFile::Method dex_method;
420 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700421 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700422 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700423 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700424 // TODO: register maps
425 }
426 }
427
428 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700429 DCHECK(klass->virtual_methods_ == NULL);
430 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700431 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700432 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700433 uint32_t last_idx = 0;
434 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700435 DexFile::Method dex_method;
436 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700437 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700438 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700439 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700440 // TODO: register maps
441 }
442 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700443}
444
Brian Carlstromf615a612011-07-23 12:50:34 -0700445void ClassLinker::LoadInterfaces(const DexFile& dex_file,
446 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700447 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700448 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700449 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700450 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700451 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700452 DCHECK(klass->interfaces_idx_ == NULL);
453 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700454 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700455 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700456 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700457 }
458 }
459}
460
Brian Carlstromf615a612011-07-23 12:50:34 -0700461void ClassLinker::LoadField(const DexFile& dex_file,
462 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700463 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700464 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700465 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700466 dst->klass_ = klass;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700467 dst->java_name_ = ResolveString(klass, field_id.name_idx_, dex_file);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700468 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700469 dst->access_flags_ = src.access_flags_;
470}
471
Brian Carlstromf615a612011-07-23 12:50:34 -0700472void ClassLinker::LoadMethod(const DexFile& dex_file,
473 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700474 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700475 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700476 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700477 dst->klass_ = klass;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700478 dst->java_name_ = ResolveString(klass, method_id.name_idx_, dex_file);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700479 {
480 int32_t utf16_length;
481 scoped_ptr<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
482 &utf16_length));
483 dst->descriptor_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
484 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700485 dst->proto_idx_ = method_id.proto_idx_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700486 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700487 dst->access_flags_ = src.access_flags_;
488
489 // TODO: check for finalize method
490
Brian Carlstromf615a612011-07-23 12:50:34 -0700491 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700492 if (code_item != NULL) {
493 dst->num_registers_ = code_item->registers_size_;
494 dst->num_ins_ = code_item->ins_size_;
495 dst->num_outs_ = code_item->outs_size_;
496 dst->insns_ = code_item->insns_;
497 } else {
498 uint16_t num_args = dst->NumArgRegisters();
499 if (!dst->IsStatic()) {
500 ++num_args;
501 }
502 dst->num_registers_ = dst->num_ins_ + num_args;
503 // TODO: native methods
504 }
505}
506
Brian Carlstromf615a612011-07-23 12:50:34 -0700507void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700508 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700509 boot_class_path_.push_back(dex_file);
510 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700511}
512
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700513void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700514 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700515 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700516 DexCache* dex_cache = AllocDexCache();
517 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700518 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
519 AllocObjectArray<Class>(dex_file->NumTypeIds()),
520 AllocObjectArray<Method>(dex_file->NumMethodIds()),
521 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700522 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700523}
524
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700525const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700526 for (size_t i = 0; i != dex_caches_.size(); ++i) {
527 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700528 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700529 }
530 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700531 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700532 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700533}
534
Brian Carlstromf615a612011-07-23 12:50:34 -0700535DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700536 for (size_t i = 0; i != dex_files_.size(); ++i) {
537 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700538 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700539 }
540 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700541 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700542 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700543}
544
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700545Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700546 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700547 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700548 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700549 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
550 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700551 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700552 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700553 bool success = InsertClass(klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700554 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700555 return klass;
556}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700557
Brian Carlstrombe977852011-07-19 14:54:54 -0700558// Create an array class (i.e. the class object for the array, not the
559// array itself). "descriptor" looks like "[C" or "[[[[B" or
560// "[Ljava/lang/String;".
561//
562// If "descriptor" refers to an array of primitives, look up the
563// primitive type's internally-generated class object.
564//
565// "loader" is the class loader of the class that's referring to us. It's
566// used to ensure that we're looking for the element type in the right
567// context. It does NOT become the class loader for the array class; that
568// always comes from the base element class.
569//
570// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700571Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700572 ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700573{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700574 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700575
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700576 // Identify the underlying element class and the array dimension depth.
577 Class* component_type_ = NULL;
578 int array_rank;
579 if (descriptor[1] == '[') {
580 // array of arrays; keep descriptor and grab stuff from parent
581 Class* outer = FindClass(descriptor.substr(1), class_loader);
582 if (outer != NULL) {
583 // want the base class, not "outer", in our component_type_
584 component_type_ = outer->component_type_;
585 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700586 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700587 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700588 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700589 } else {
590 array_rank = 1;
591 if (descriptor[1] == 'L') {
592 // array of objects; strip off "[" and look up descriptor.
593 const StringPiece subDescriptor = descriptor.substr(1);
594 component_type_ = FindClass(subDescriptor, class_loader);
595 } else {
596 // array of a primitive type
597 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700598 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700599 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700600
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700601 if (component_type_ == NULL) {
602 // failed
603 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
604 return NULL;
605 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700606
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700607 // See if the component type is already loaded. Array classes are
608 // always associated with the class loader of their underlying
609 // element type -- an array of Strings goes with the loader for
610 // java/lang/String -- so we need to look for it there. (The
611 // caller should have checked for the existence of the class
612 // before calling here, but they did so with *their* class loader,
613 // not the component type's loader.)
614 //
615 // If we find it, the caller adds "loader" to the class' initiating
616 // loader list, which should prevent us from going through this again.
617 //
618 // This call is unnecessary if "loader" and "component_type_->class_loader_"
619 // are the same, because our caller (FindClass) just did the
620 // lookup. (Even if we get this wrong we still have correct behavior,
621 // because we effectively do this lookup again when we add the new
622 // class to the hash table --- necessary because of possible races with
623 // other threads.)
624 if (class_loader != component_type_->class_loader_) {
625 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
626 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700627 return new_class;
628 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700629 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700630
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700631 // Fill out the fields in the Class.
632 //
633 // It is possible to execute some methods against arrays, because
634 // all arrays are subclasses of java_lang_Object_, so we need to set
635 // up a vtable. We can just point at the one in java_lang_Object_.
636 //
637 // Array classes are simple enough that we don't need to do a full
638 // link step.
639
640 Class* new_class = NULL;
641 if (!init_done_) {
642 if (descriptor == "[Ljava/lang/Object;") {
643 new_class = GetClassRoot(kObjectArrayClass);
644 } else if (descriptor == "[C") {
645 new_class = GetClassRoot(kCharArrayClass);
646 }
647 }
648 if (new_class == NULL) {
649 new_class = AllocClass();
650 if (new_class == NULL) {
651 return NULL;
652 }
653 }
654 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
655 descriptor.size());
656 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
657 new_class->descriptor_alloc_->size());
658 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
659 new_class->super_class_ = java_lang_Object;
660 new_class->vtable_ = java_lang_Object->vtable_;
661 new_class->primitive_type_ = Class::kPrimNot;
662 new_class->component_type_ = component_type_;
663 new_class->class_loader_ = component_type_->class_loader_;
664 new_class->array_rank_ = array_rank;
665 new_class->status_ = Class::kStatusInitialized;
666 // don't need to set new_class->object_size_
667
668
669 // All arrays have java/lang/Cloneable and java/io/Serializable as
670 // interfaces. We need to set that up here, so that stuff like
671 // "instanceof" works right.
672 //
673 // Note: The GC could run during the call to FindSystemClass,
674 // so we need to make sure the class object is GC-valid while we're in
675 // there. Do this by clearing the interface list so the GC will just
676 // think that the entries are null.
677
678
679 // Use the single, global copies of "interfaces" and "iftable"
680 // (remember not to free them for arrays).
681 DCHECK(array_interfaces_ != NULL);
682 new_class->interfaces_ = array_interfaces_;
683 new_class->iftable_count_ = 2;
684 DCHECK(array_iftable_ != NULL);
685 new_class->iftable_ = array_iftable_;
686
687 // Inherit access flags from the component type. Arrays can't be
688 // used as a superclass or interface, so we want to add "final"
689 // and remove "interface".
690 //
691 // Don't inherit any non-standard flags (e.g., kAccFinal)
692 // from component_type_. We assume that the array class does not
693 // override finalize().
694 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
695 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
696
697 if (InsertClass(new_class)) {
698 return new_class;
699 }
700 // Another thread must have loaded the class after we
701 // started but before we finished. Abandon what we've
702 // done.
703 //
704 // (Yes, this happens.)
705
706 // Grab the winning class.
707 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
708 DCHECK(other_class != NULL);
709 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700710}
711
712Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700713 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700714 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700715 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700716 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700717 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700718 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700719 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700720 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700721 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700722 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700723 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700724 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700725 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700726 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700727 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700728 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700729 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700730 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700731 return GetClassRoot(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700732 case 'L':
733 case '[':
734 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700735 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700736 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700737 };
738 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700739}
740
741bool ClassLinker::InsertClass(Class* klass) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700742 MutexLock mu(classes_lock_);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700743 const StringPiece& key = klass->GetDescriptor();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700744 Table::iterator it = classes_.insert(std::make_pair(key, klass));
745 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700746}
747
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700748Class* ClassLinker::LookupClass(const StringPiece& descriptor, ClassLoader* class_loader) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700749 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700750 typedef Table::const_iterator It; // TODO: C++0x auto
751 for (It it = classes_.find(descriptor), end = classes_.end(); it != end; ++it) {
752 Class* klass = it->second;
753 if (klass->descriptor_ == descriptor && klass->class_loader_ == class_loader) {
754 return klass;
755 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700756 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700757 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700758}
759
760bool ClassLinker::InitializeClass(Class* klass) {
761 CHECK(klass->GetStatus() == Class::kStatusResolved ||
762 klass->GetStatus() == Class::kStatusError);
763
Carl Shapirob5573532011-07-12 18:22:59 -0700764 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700765
766 {
767 ObjectLock lock(klass);
768
769 if (klass->GetStatus() < Class::kStatusVerified) {
770 if (klass->IsErroneous()) {
771 LG << "re-initializing failed class"; // TODO: throw
772 return false;
773 }
774
775 CHECK(klass->GetStatus() == Class::kStatusResolved);
776
777 klass->status_ = Class::kStatusVerifying;
778 if (!DexVerify::VerifyClass(klass)) {
779 LG << "Verification failed"; // TODO: ThrowVerifyError
780 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700781 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
782 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700783 klass->SetStatus(Class::kStatusError);
784 return false;
785 }
786
787 klass->SetStatus(Class::kStatusVerified);
788 }
789
790 if (klass->status_ == Class::kStatusInitialized) {
791 return true;
792 }
793
794 while (klass->status_ == Class::kStatusInitializing) {
795 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700796 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700797 LG << "recursive <clinit>";
798 return true;
799 }
800
801 CHECK(!self->IsExceptionPending());
802
803 lock.Wait(); // TODO: check for interruption
804
805 // When we wake up, repeat the test for init-in-progress. If
806 // there's an exception pending (only possible if
807 // "interruptShouldThrow" was set), bail out.
808 if (self->IsExceptionPending()) {
809 CHECK(false);
810 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
811 klass->SetStatus(Class::kStatusError);
812 return false;
813 }
814 if (klass->GetStatus() == Class::kStatusInitializing) {
815 continue;
816 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700817 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700818 klass->GetStatus() == Class::kStatusError);
819 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700820 // The caller wants an exception, but it was thrown in a
821 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700822 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
823 return false;
824 }
825 return true; // otherwise, initialized
826 }
827
828 // see if we failed previously
829 if (klass->IsErroneous()) {
830 // might be wise to unlock before throwing; depends on which class
831 // it is that we have locked
832
833 // TODO: throwEarlierClassFailure(klass);
834 return false;
835 }
836
837 if (!ValidateSuperClassDescriptors(klass)) {
838 klass->SetStatus(Class::kStatusError);
839 return false;
840 }
841
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700842 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700843
Carl Shapirob5573532011-07-12 18:22:59 -0700844 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700845 klass->status_ = Class::kStatusInitializing;
846 }
847
848 if (!InitializeSuperClass(klass)) {
849 return false;
850 }
851
852 InitializeStaticFields(klass);
853
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700854 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700855 if (clinit != NULL) {
856 } else {
857 // JValue unused;
858 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700859 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700860 }
861
862 {
863 ObjectLock lock(klass);
864
865 if (self->IsExceptionPending()) {
866 klass->SetStatus(Class::kStatusError);
867 } else {
868 klass->SetStatus(Class::kStatusInitialized);
869 }
870 lock.NotifyAll();
871 }
872
873 return true;
874}
875
876bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
877 if (klass->IsInterface()) {
878 return true;
879 }
880 // begin with the methods local to the superclass
881 if (klass->HasSuperClass() &&
882 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
883 const Class* super = klass->GetSuperClass();
884 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
885 const Method* method = klass->GetVirtualMethod(i);
886 if (method != super->GetVirtualMethod(i) &&
887 !HasSameMethodDescriptorClasses(method, super, klass)) {
888 LG << "Classes resolve differently in superclass";
889 return false;
890 }
891 }
892 }
893 for (size_t i = 0; i < klass->iftable_count_; ++i) {
894 const InterfaceEntry* iftable = &klass->iftable_[i];
895 Class* interface = iftable->GetClass();
896 if (klass->GetClassLoader() != interface->GetClassLoader()) {
897 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
898 uint32_t vtable_index = iftable->method_index_array_[j];
899 const Method* method = klass->GetVirtualMethod(vtable_index);
900 if (!HasSameMethodDescriptorClasses(method, interface,
901 method->GetClass())) {
902 LG << "Classes resolve differently in interface"; // TODO: LinkageError
903 return false;
904 }
905 }
906 }
907 }
908 return true;
909}
910
911bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700912 const Class* klass1,
913 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700914 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
915 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700916 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700917 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700918 const char* descriptor = it->GetDescriptor();
919 if (descriptor == NULL) {
920 break;
921 }
922 if (descriptor[0] == 'L' || descriptor[0] == '[') {
923 // Found a non-primitive type.
924 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
925 return false;
926 }
927 }
928 }
929 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700930 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700931 if (descriptor[0] == 'L' || descriptor[0] == '[') {
932 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
933 return false;
934 }
935 }
936 return true;
937}
938
939// Returns true if classes referenced by the descriptor are the
940// same classes in klass1 as they are in klass2.
941bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700942 const Class* klass1,
943 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700944 CHECK(descriptor != NULL);
945 CHECK(klass1 != NULL);
946 CHECK(klass2 != NULL);
947#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700948 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700949 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700950 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700951 // TODO: found2 == NULL
952 // TODO: lookup found1 in initiating loader list
953 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700954 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700955 if (found1 == found2) {
956 return true;
957 } else {
958 return false;
959 }
960 }
961#endif
962 return true;
963}
964
965bool ClassLinker::InitializeSuperClass(Class* klass) {
966 CHECK(klass != NULL);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700967 MutexLock mu(classes_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700968 if (!klass->IsInterface() && klass->HasSuperClass()) {
969 Class* super_class = klass->GetSuperClass();
970 if (super_class->GetStatus() != Class::kStatusInitialized) {
971 CHECK(!super_class->IsInterface());
972 klass->MonitorExit();
973 bool super_initialized = InitializeClass(super_class);
974 klass->MonitorEnter();
975 // TODO: check for a pending exception
976 if (!super_initialized) {
977 klass->SetStatus(Class::kStatusError);
978 klass->NotifyAll();
979 return false;
980 }
981 }
982 }
983 return true;
984}
985
986void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700987 size_t num_static_fields = klass->NumStaticFields();
988 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700989 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700990 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700991 DexCache* dex_cache = klass->GetDexCache();
992 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700993 return;
994 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700995 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700996 const DexFile& dex_file = FindDexFile(dex_cache);
997 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -0700998 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700999 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001000 size_t array_size = DecodeUnsignedLeb128(&addr);
1001 for (size_t i = 0; i < array_size; ++i) {
1002 StaticField* field = klass->GetStaticField(i);
1003 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001004 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001005 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001006 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001007 field->SetByte(value.b);
1008 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001009 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001010 field->SetShort(value.s);
1011 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001012 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001013 field->SetChar(value.c);
1014 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001015 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001016 field->SetInt(value.i);
1017 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001018 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001019 field->SetLong(value.j);
1020 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001021 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001022 field->SetFloat(value.f);
1023 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001024 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001025 field->SetDouble(value.d);
1026 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001027 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001028 uint32_t string_idx = value.i;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001029 String* resolved = ResolveString(klass, string_idx, dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001030 field->SetObject(resolved);
1031 break;
1032 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001033 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001034 field->SetBoolean(value.z);
1035 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001036 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001037 field->SetObject(value.l);
1038 break;
1039 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001040 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001041 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001042 }
1043}
1044
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001045bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1046 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001047 if (!LinkSuperClass(klass)) {
1048 return false;
1049 }
1050 if (!LinkMethods(klass)) {
1051 return false;
1052 }
1053 if (!LinkInstanceFields(klass)) {
1054 return false;
1055 }
1056 CreateReferenceOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001057 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001058 klass->status_ = Class::kStatusResolved;
1059 return true;
1060}
1061
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001062bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1063 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001064 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1065 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001066 if (super_class == NULL) {
1067 LG << "Failed to resolve superclass";
1068 return false;
1069 }
1070 klass->super_class_ = super_class; // TODO: write barrier
1071 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001072 if (klass->NumInterfaces() > 0) {
1073 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1074 uint32_t idx = klass->interfaces_idx_[i];
1075 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1076 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001077 LG << "Failed to resolve interface";
1078 return false;
1079 }
1080 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001081 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001082 LG << "Inaccessible interface";
1083 return false;
1084 }
1085 }
1086 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001087 // Mark the class as loaded.
1088 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001089 return true;
1090}
1091
1092bool ClassLinker::LinkSuperClass(Class* klass) {
1093 CHECK(!klass->IsPrimitive());
1094 const Class* super = klass->GetSuperClass();
1095 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1096 if (super != NULL) {
1097 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1098 return false;
1099 }
1100 // TODO: clear finalize attribute
1101 return true;
1102 }
1103 if (super == NULL) {
1104 LG << "No superclass defined"; // TODO: LinkageError
1105 return false;
1106 }
1107 // Verify
1108 if (super->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001109 LG << "Superclass " << super->descriptor_ << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001110 return false;
1111 }
1112 if (super->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001113 LG << "Superclass " << super->descriptor_ << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001114 return false;
1115 }
1116 if (!klass->CanAccess(super)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001117 LG << "Superclass " << super->descriptor_ << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001118 return false;
1119 }
1120 return true;
1121}
1122
1123// Populate the class vtable and itable.
1124bool ClassLinker::LinkMethods(Class* klass) {
1125 if (klass->IsInterface()) {
1126 // No vtable.
1127 size_t count = klass->NumVirtualMethods();
1128 if (!IsUint(16, count)) {
1129 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1130 return false;
1131 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001132 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001133 klass->GetVirtualMethod(i)->method_index_ = i;
1134 }
1135 } else {
1136 // Link virtual method tables
1137 LinkVirtualMethods(klass);
1138
1139 // Link interface method tables
1140 LinkInterfaceMethods(klass);
1141
1142 // Insert stubs.
1143 LinkAbstractMethods(klass);
1144 }
1145 return true;
1146}
1147
1148bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001149 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001150 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001151 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1152 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001153 // TODO: do not assign to the vtable field until it is fully constructed.
1154 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001155 // See if any of our virtual methods override the superclass.
1156 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1157 Method* local_method = klass->GetVirtualMethod(i);
1158 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001159 for (; j < actual_count; ++j) {
1160 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001161 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001162 // Verify
1163 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001164 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001165 return false;
1166 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001167 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001168 local_method->method_index_ = j;
1169 break;
1170 }
1171 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001172 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001173 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001174 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001175 local_method->method_index_ = actual_count;
1176 actual_count += 1;
1177 }
1178 }
1179 if (!IsUint(16, actual_count)) {
1180 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1181 return false;
1182 }
1183 CHECK_LE(actual_count, max_count);
1184 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001185 // TODO: do not assign to the vtable field until it is fully constructed.
1186 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001187 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001188 } else {
1189 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001190 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1191 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1192 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001193 LG << "Too many methods"; // TODO: VirtualMachineError
1194 return false;
1195 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001196 // TODO: do not assign to the vtable field until it is fully constructed.
1197 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1198 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001199 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001200 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1201 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001202 }
1203 return true;
1204}
1205
1206bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1207 int pool_offset = 0;
1208 int pool_size = 0;
1209 int miranda_count = 0;
1210 int miranda_alloc = 0;
1211 size_t super_ifcount;
1212 if (klass->HasSuperClass()) {
1213 super_ifcount = klass->GetSuperClass()->iftable_count_;
1214 } else {
1215 super_ifcount = 0;
1216 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001217 size_t ifcount = super_ifcount;
1218 ifcount += klass->NumInterfaces();
1219 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1220 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001221 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001222 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001223 DCHECK(klass->iftable_count_ == 0);
1224 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001225 return true;
1226 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001227 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1228 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001229 if (super_ifcount != 0) {
1230 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1231 sizeof(InterfaceEntry) * super_ifcount);
1232 }
1233 // Flatten the interface inheritance hierarchy.
1234 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001235 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1236 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001237 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001238 if (!interf->IsInterface()) {
1239 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1240 return false;
1241 }
1242 klass->iftable_[idx++].SetClass(interf);
1243 for (size_t j = 0; j < interf->iftable_count_; j++) {
1244 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1245 }
1246 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001247 CHECK_EQ(idx, ifcount);
1248 klass->iftable_count_ = ifcount;
1249 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001250 return true;
1251 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001252 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001253 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1254 }
1255 if (pool_size == 0) {
1256 return true;
1257 }
1258 klass->ifvi_pool_count_ = pool_size;
1259 klass->ifvi_pool_ = new uint32_t[pool_size];
1260 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001261 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001262 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1263 Class* interface = klass->iftable_[i].GetClass();
1264 pool_offset += interface->NumVirtualMethods(); // end here
1265 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1266 Method* interface_method = interface->GetVirtualMethod(j);
1267 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001268 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001269 Method* vtable_method = klass->vtable_->Get(k);
1270 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1271 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001272 LG << "Implementation not public";
1273 return false;
1274 }
1275 klass->iftable_[i].method_index_array_[j] = k;
1276 break;
1277 }
1278 }
1279 if (k < 0) {
1280 if (miranda_count == miranda_alloc) {
1281 miranda_alloc += 8;
1282 if (miranda_list.empty()) {
1283 miranda_list.resize(miranda_alloc);
1284 } else {
1285 miranda_list.resize(miranda_alloc);
1286 }
1287 }
1288 int mir;
1289 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001290 Method* miranda_method = miranda_list[mir];
1291 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001292 break;
1293 }
1294 }
1295 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001296 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001297 if (mir == miranda_count) {
1298 miranda_list[miranda_count++] = interface_method;
1299 }
1300 }
1301 }
1302 }
1303 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001304 int old_method_count = klass->NumVirtualMethods();
1305 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001306 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001307
1308 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001309 int old_vtable_count = klass->vtable_->GetLength();
1310 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001311 // TODO: do not assign to the vtable field until it is fully constructed.
1312 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001313
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001314 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001315 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001316 memcpy(meth, miranda_list[i], sizeof(Method));
1317 meth->klass_ = klass;
1318 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001319 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1320 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001321 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001322 }
1323 }
1324 return true;
1325}
1326
1327void ClassLinker::LinkAbstractMethods(Class* klass) {
1328 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1329 Method* method = klass->GetVirtualMethod(i);
1330 if (method->IsAbstract()) {
1331 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1332 }
1333 }
1334}
1335
1336bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001337 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001338 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001339 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001340 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001341 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001342 }
1343 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001344 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001345 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001346 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001347 InstanceField* pField = klass->GetInstanceField(i);
1348 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001349 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001350 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1351 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001352 char rc = refField->GetType();
1353 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001354 klass->SetInstanceField(i, refField);
1355 klass->SetInstanceField(j, pField);
1356 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001357 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001358 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001359 break;
1360 }
1361 }
1362 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001363 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001364 }
1365 if (c != '[' && c != 'L') {
1366 break;
1367 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001368 pField->SetOffset(field_offset);
1369 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001370 }
1371
1372 // Now we want to pack all of the double-wide fields together. If
1373 // we're not aligned, though, we want to shuffle one 32-bit field
1374 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001375 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001376 InstanceField* pField = klass->GetInstanceField(i);
1377 char c = pField->GetType();
1378
1379 if (c != 'J' && c != 'D') {
1380 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001381 DCHECK(c != '[');
1382 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001383 pField->SetOffset(field_offset);
1384 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001385 i++;
1386 } else {
1387 // Next field is 64-bit, so search for a 32-bit field we can
1388 // swap into it.
1389 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001390 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1391 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001392 char rc = singleField->GetType();
1393 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001394 klass->SetInstanceField(i, singleField);
1395 klass->SetInstanceField(j, pField);
1396 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001397 pField->SetOffset(field_offset);
1398 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001399 found = true;
1400 i++;
1401 break;
1402 }
1403 }
1404 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001405 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001406 }
1407 }
1408 }
1409
1410 // Alignment is good, shuffle any double-wide fields forward, and
1411 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001412 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001413 for ( ; i < klass->NumInstanceFields(); i++) {
1414 InstanceField* pField = klass->GetInstanceField(i);
1415 char c = pField->GetType();
1416 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001417 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1418 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001419 char rc = doubleField->GetType();
1420 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001421 klass->SetInstanceField(i, doubleField);
1422 klass->SetInstanceField(j, pField);
1423 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001424 c = rc;
1425 break;
1426 }
1427 }
1428 } else {
1429 // This is a double-wide field, leave it be.
1430 }
1431
Brian Carlstroma0808032011-07-18 00:39:23 -07001432 pField->SetOffset(field_offset);
1433 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001434 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001435 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001436 }
1437
1438#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001439 // Make sure that all reference fields appear before
1440 // non-reference fields, and all double-wide fields are aligned.
1441 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001442 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001443 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001444 char c = pField->GetType();
1445
1446 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001447 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001448 }
1449
1450 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001451 if (!seen_non_ref) {
1452 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001453 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001454 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001455 } else {
1456 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001457 }
1458 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001459 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001460 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001461 }
1462#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001463 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001464 return true;
1465}
1466
1467// Set the bitmap of reference offsets, refOffsets, from the ifields
1468// list.
1469void ClassLinker::CreateReferenceOffsets(Class* klass) {
1470 uint32_t reference_offsets = 0;
1471 if (klass->HasSuperClass()) {
1472 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1473 }
1474 // If our superclass overflowed, we don't stand a chance.
1475 if (reference_offsets != CLASS_WALK_SUPER) {
1476 // All of the fields that contain object references are guaranteed
1477 // to be at the beginning of the ifields list.
1478 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1479 // Note that, per the comment on struct InstField, f->byteOffset
1480 // is the offset from the beginning of obj, not the offset into
1481 // obj->instanceData.
1482 const InstanceField* field = klass->GetInstanceField(i);
1483 size_t byte_offset = field->GetOffset();
1484 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001485 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001486 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1487 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001488 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001489 reference_offsets |= new_bit;
1490 } else {
1491 reference_offsets = CLASS_WALK_SUPER;
1492 break;
1493 }
1494 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001495 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001496 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001497}
1498
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001499Class* ClassLinker::ResolveClass(const Class* referrer,
1500 uint32_t class_idx,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001501 const DexFile& dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001502 DexCache* dex_cache = referrer->GetDexCache();
1503 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001504 if (resolved != NULL) {
1505 return resolved;
1506 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001507 const char* descriptor = dex_file.dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001508 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001509 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001510 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001511 resolved = FindClass(descriptor, referrer->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001512 }
1513 if (resolved != NULL) {
1514 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001515 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001516 if (check->GetClassLoader() != NULL) {
1517 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1518 return NULL;
1519 }
1520 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001521 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001522 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001523 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001524 }
1525 return resolved;
1526}
1527
1528Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1529 /*MethodType*/ int method_type) {
1530 CHECK(false);
1531 return NULL;
1532}
1533
Carl Shapiro69759ea2011-07-21 18:13:35 -07001534String* ClassLinker::ResolveString(const Class* referring,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001535 uint32_t string_idx,
1536 const DexFile& dex_file) {
1537 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1538 int32_t utf16_length = dex_file.GetStringLength(string_id);
1539 const char* utf8_data = dex_file.GetStringData(string_id);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001540 String* string = intern_table_.Intern(utf16_length, utf8_data);
1541 referring->GetDexCache()->SetResolvedString(string_idx, string);
1542 return string;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001543}
1544
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001545} // namespace art