blob: 46562ca6dc1aafc7df5995d538ca2b5b072ed326 [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];
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700153 array_iftable_[0].SetClass(array_interfaces_->Get(0));
154 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700155 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700156
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700157 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700158 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
159 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700160
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700161 // Setup the primitive type classes.
162 class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B"));
163 class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C"));
164 class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D"));
165 class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F"));
166 class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I"));
167 class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J"));
168 class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S"));
169 class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
170 class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V"));
171 // now we can use FindSystemClass for anything, including for "[C"
172
Jesse Wilson8989d992011-08-02 13:39:42 -0700173 // run char[] through FindClass to complete initialization
174 Class* found_char_array_class = FindSystemClass("[C");
175 CHECK_EQ(char_array_class, found_char_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700176
177 // ensure all class_roots_ were initialized
178 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700179 CHECK(GetClassRoot(static_cast<ClassRoot>(i)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700180 }
181
182 // disable the slow paths in FindClass and CreatePrimitiveClass now
183 // that Object, Class, and Object[] are setup
184 init_done_ = true;
185}
186
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700187void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700188 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700189
190 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700191 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700192 }
193
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700194 {
195 MutexLock mu(classes_lock_);
196 typedef Table::const_iterator It; // TODO: C++0x auto
197 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
198 root_visitor(it->second, arg);
199 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700200 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700201
202 intern_table_.VisitRoots(root_visitor, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700203
Brian Carlstromb88e9442011-07-28 15:15:51 -0700204 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700205}
206
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700207DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700208 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700209}
210
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700211Class* ClassLinker::AllocClass(Class* java_lang_Class) {
212 return down_cast<Class*>(Object::Alloc(java_lang_Class));
213}
214
215Class* ClassLinker::AllocClass() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700216 return AllocClass(GetClassRoot(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700217}
218
219StaticField* ClassLinker::AllocStaticField() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700220 return down_cast<StaticField*>(Object::Alloc(GetClassRoot(kJavaLangReflectField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700221}
222
223InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700224 return down_cast<InstanceField*>(Object::Alloc(GetClassRoot(kJavaLangReflectField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700225}
226
227Method* ClassLinker::AllocMethod() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700228 return down_cast<Method*>(Object::Alloc(GetClassRoot(kJavaLangReflectMethod)));
229}
230
231// TODO remove once we can use java.lang.Class.getSystemClassLoader
232PathClassLoader* ClassLinker::AllocPathClassLoader(std::vector<const DexFile*> dex_files) {
233 PathClassLoader* cl = down_cast<PathClassLoader*>(Object::Alloc(GetClassRoot(kDalvikSystemPathClassLoader)));
234 cl->SetClassPath(dex_files);
235 return cl;
Carl Shapiro565f5072011-07-10 13:39:43 -0700236}
237
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700238Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700239 ClassLoader* class_loader) {
240 // TODO remove this contrived parent class loader check when we have a real ClassLoader.
241 if (class_loader != NULL) {
242 Class* klass = FindClass(descriptor, NULL);
243 if (klass != NULL) {
244 return klass;
245 }
246 }
247
Carl Shapirob5573532011-07-12 18:22:59 -0700248 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700249 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700250 CHECK(!self->IsExceptionPending());
251 // Find the class in the loaded classes table.
252 Class* klass = LookupClass(descriptor, class_loader);
253 if (klass == NULL) {
254 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700255 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700256 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700257 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700258 DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_);
259 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700260 if (pair.second == NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700261 LG << "Class " << descriptor << " not found in class loader " << class_loader; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700262 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700263 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700264 const DexFile& dex_file = *pair.first;
265 const DexFile::ClassDef& dex_class_def = *pair.second;
266 DexCache* dex_cache = FindDexCache(pair.first);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700267 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700268 if (!init_done_) {
269 // finish up init of hand crafted class_roots_
270 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700271 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700272 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700273 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400274 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700275 klass = GetClassRoot(kJavaLangString);
276 } else if (descriptor == "Ljava/lang/reflect/Field;") {
277 klass = GetClassRoot(kJavaLangReflectField);
278 } else if (descriptor == "Ljava/lang/reflect/Method;") {
279 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700280 } else {
281 klass = AllocClass();
282 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700283 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700284 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700285 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700286 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700287 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700288 // Check for a pending exception during load
289 if (self->IsExceptionPending()) {
290 // TODO: free native allocations in klass
291 return NULL;
292 }
293 {
294 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700295 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700296 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700297 bool success = InsertClass(klass); // TODO just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700298 if (!success) {
299 // We may fail to insert if we raced with another thread.
300 klass->clinit_thread_id_ = 0;
301 // TODO: free native allocations in klass
302 klass = LookupClass(descriptor, class_loader);
303 CHECK(klass != NULL);
304 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700305 // Finish loading (if necessary) by finding parents
306 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
307 // Loading failed.
308 // TODO: CHECK(self->IsExceptionPending());
309 lock.NotifyAll();
310 return NULL;
311 }
312 CHECK(klass->IsLoaded());
313 // Link the class (if necessary)
314 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700315 // Linking failed.
316 // TODO: CHECK(self->IsExceptionPending());
317 lock.NotifyAll();
318 return NULL;
319 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700320 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700321 }
322 }
323 }
324 // Link the class if it has not already been linked.
325 if (!klass->IsLinked() && !klass->IsErroneous()) {
326 ObjectLock lock(klass);
327 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700328 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700329 LG << "Recursive link"; // TODO: ClassCircularityError
330 return NULL;
331 }
332 // Wait for the pending initialization to complete.
333 while (!klass->IsLinked() && !klass->IsErroneous()) {
334 lock.Wait();
335 }
336 }
337 if (klass->IsErroneous()) {
338 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
339 return NULL;
340 }
341 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700342 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700343 CHECK(!self->IsExceptionPending());
344 return klass;
345}
346
Brian Carlstromf615a612011-07-23 12:50:34 -0700347void ClassLinker::LoadClass(const DexFile& dex_file,
348 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700349 Class* klass,
350 ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700351 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700352 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700353 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700354 const byte* class_data = dex_file.GetClassData(dex_class_def);
355 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700356
Brian Carlstromf615a612011-07-23 12:50:34 -0700357 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700358 CHECK(descriptor != NULL);
359
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700360 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700361 klass->descriptor_.set(descriptor);
362 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700363 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700364 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700365 klass->primitive_type_ = Class::kPrimNot;
366 klass->status_ = Class::kStatusIdx;
367
368 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700369 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700370
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700371 size_t num_static_fields = header.static_fields_size_;
372 size_t num_instance_fields = header.instance_fields_size_;
373 size_t num_direct_methods = header.direct_methods_size_;
374 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700375
Brian Carlstromf615a612011-07-23 12:50:34 -0700376 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700377
378 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700379 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700380
381 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700382 DCHECK(klass->sfields_ == NULL);
383 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700384 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700385 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700386 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700387 DexFile::Field dex_field;
388 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700389 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700390 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700391 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700392 }
393 }
394
395 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700396 DCHECK(klass->ifields_ == NULL);
397 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700398 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700399 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700400 uint32_t last_idx = 0;
401 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700402 DexFile::Field dex_field;
403 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700404 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700405 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700406 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700407 }
408 }
409
410 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700411 DCHECK(klass->direct_methods_ == NULL);
412 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700413 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700414 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700415 uint32_t last_idx = 0;
416 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700417 DexFile::Method dex_method;
418 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700419 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700420 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700421 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700422 // TODO: register maps
423 }
424 }
425
426 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700427 DCHECK(klass->virtual_methods_ == NULL);
428 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700429 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700430 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700431 uint32_t last_idx = 0;
432 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700433 DexFile::Method dex_method;
434 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700435 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700436 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700437 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700438 // TODO: register maps
439 }
440 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700441}
442
Brian Carlstromf615a612011-07-23 12:50:34 -0700443void ClassLinker::LoadInterfaces(const DexFile& dex_file,
444 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700445 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700446 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700447 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700448 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700449 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700450 DCHECK(klass->interfaces_idx_ == NULL);
451 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700452 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700453 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700454 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700455 }
456 }
457}
458
Brian Carlstromf615a612011-07-23 12:50:34 -0700459void ClassLinker::LoadField(const DexFile& dex_file,
460 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700461 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700462 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700463 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700464 dst->klass_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700465 dst->name_ = ResolveString(klass, field_id.name_idx_, dex_file);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700466 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700467 dst->access_flags_ = src.access_flags_;
468}
469
Brian Carlstromf615a612011-07-23 12:50:34 -0700470void ClassLinker::LoadMethod(const DexFile& dex_file,
471 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700472 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700473 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700474 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700475 dst->klass_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700476 dst->name_ = ResolveString(klass, method_id.name_idx_, dex_file);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700477 {
478 int32_t utf16_length;
479 scoped_ptr<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
480 &utf16_length));
481 dst->descriptor_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
482 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700483 dst->proto_idx_ = method_id.proto_idx_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700484 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700485 dst->access_flags_ = src.access_flags_;
486
487 // TODO: check for finalize method
488
Brian Carlstromf615a612011-07-23 12:50:34 -0700489 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700490 if (code_item != NULL) {
491 dst->num_registers_ = code_item->registers_size_;
492 dst->num_ins_ = code_item->ins_size_;
493 dst->num_outs_ = code_item->outs_size_;
494 dst->insns_ = code_item->insns_;
495 } else {
496 uint16_t num_args = dst->NumArgRegisters();
497 if (!dst->IsStatic()) {
498 ++num_args;
499 }
500 dst->num_registers_ = dst->num_ins_ + num_args;
501 // TODO: native methods
502 }
503}
504
Brian Carlstromf615a612011-07-23 12:50:34 -0700505void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700506 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700507 boot_class_path_.push_back(dex_file);
508 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700509}
510
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700511void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700512 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700513 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700514 DexCache* dex_cache = AllocDexCache();
515 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700516 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
517 AllocObjectArray<Class>(dex_file->NumTypeIds()),
518 AllocObjectArray<Method>(dex_file->NumMethodIds()),
519 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700520 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700521}
522
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700523const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700524 for (size_t i = 0; i != dex_caches_.size(); ++i) {
525 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700526 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700527 }
528 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700529 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700530 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700531}
532
Brian Carlstromf615a612011-07-23 12:50:34 -0700533DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700534 for (size_t i = 0; i != dex_files_.size(); ++i) {
535 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700536 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700537 }
538 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700539 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700540 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700541}
542
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700543Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700544 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700545 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700546 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700547 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
548 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700549 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700550 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700551 bool success = InsertClass(klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700552 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700553 return klass;
554}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700555
Brian Carlstrombe977852011-07-19 14:54:54 -0700556// Create an array class (i.e. the class object for the array, not the
557// array itself). "descriptor" looks like "[C" or "[[[[B" or
558// "[Ljava/lang/String;".
559//
560// If "descriptor" refers to an array of primitives, look up the
561// primitive type's internally-generated class object.
562//
563// "loader" is the class loader of the class that's referring to us. It's
564// used to ensure that we're looking for the element type in the right
565// context. It does NOT become the class loader for the array class; that
566// always comes from the base element class.
567//
568// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700569Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700570 ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700571{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700572 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700573
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700574 // Identify the underlying element class and the array dimension depth.
575 Class* component_type_ = NULL;
576 int array_rank;
577 if (descriptor[1] == '[') {
578 // array of arrays; keep descriptor and grab stuff from parent
579 Class* outer = FindClass(descriptor.substr(1), class_loader);
580 if (outer != NULL) {
581 // want the base class, not "outer", in our component_type_
582 component_type_ = outer->component_type_;
583 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700584 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700585 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700586 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700587 } else {
588 array_rank = 1;
589 if (descriptor[1] == 'L') {
590 // array of objects; strip off "[" and look up descriptor.
591 const StringPiece subDescriptor = descriptor.substr(1);
592 component_type_ = FindClass(subDescriptor, class_loader);
593 } else {
594 // array of a primitive type
595 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700596 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700597 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700598
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700599 if (component_type_ == NULL) {
600 // failed
601 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
602 return NULL;
603 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700604
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700605 // See if the component type is already loaded. Array classes are
606 // always associated with the class loader of their underlying
607 // element type -- an array of Strings goes with the loader for
608 // java/lang/String -- so we need to look for it there. (The
609 // caller should have checked for the existence of the class
610 // before calling here, but they did so with *their* class loader,
611 // not the component type's loader.)
612 //
613 // If we find it, the caller adds "loader" to the class' initiating
614 // loader list, which should prevent us from going through this again.
615 //
616 // This call is unnecessary if "loader" and "component_type_->class_loader_"
617 // are the same, because our caller (FindClass) just did the
618 // lookup. (Even if we get this wrong we still have correct behavior,
619 // because we effectively do this lookup again when we add the new
620 // class to the hash table --- necessary because of possible races with
621 // other threads.)
622 if (class_loader != component_type_->class_loader_) {
623 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
624 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700625 return new_class;
626 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700627 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700628
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700629 // Fill out the fields in the Class.
630 //
631 // It is possible to execute some methods against arrays, because
632 // all arrays are subclasses of java_lang_Object_, so we need to set
633 // up a vtable. We can just point at the one in java_lang_Object_.
634 //
635 // Array classes are simple enough that we don't need to do a full
636 // link step.
637
638 Class* new_class = NULL;
639 if (!init_done_) {
640 if (descriptor == "[Ljava/lang/Object;") {
641 new_class = GetClassRoot(kObjectArrayClass);
642 } else if (descriptor == "[C") {
643 new_class = GetClassRoot(kCharArrayClass);
644 }
645 }
646 if (new_class == NULL) {
647 new_class = AllocClass();
648 if (new_class == NULL) {
649 return NULL;
650 }
651 }
652 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
653 descriptor.size());
654 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
655 new_class->descriptor_alloc_->size());
656 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
657 new_class->super_class_ = java_lang_Object;
658 new_class->vtable_ = java_lang_Object->vtable_;
659 new_class->primitive_type_ = Class::kPrimNot;
660 new_class->component_type_ = component_type_;
661 new_class->class_loader_ = component_type_->class_loader_;
662 new_class->array_rank_ = array_rank;
663 new_class->status_ = Class::kStatusInitialized;
664 // don't need to set new_class->object_size_
665
666
667 // All arrays have java/lang/Cloneable and java/io/Serializable as
668 // interfaces. We need to set that up here, so that stuff like
669 // "instanceof" works right.
670 //
671 // Note: The GC could run during the call to FindSystemClass,
672 // so we need to make sure the class object is GC-valid while we're in
673 // there. Do this by clearing the interface list so the GC will just
674 // think that the entries are null.
675
676
677 // Use the single, global copies of "interfaces" and "iftable"
678 // (remember not to free them for arrays).
679 DCHECK(array_interfaces_ != NULL);
680 new_class->interfaces_ = array_interfaces_;
681 new_class->iftable_count_ = 2;
682 DCHECK(array_iftable_ != NULL);
683 new_class->iftable_ = array_iftable_;
684
685 // Inherit access flags from the component type. Arrays can't be
686 // used as a superclass or interface, so we want to add "final"
687 // and remove "interface".
688 //
689 // Don't inherit any non-standard flags (e.g., kAccFinal)
690 // from component_type_. We assume that the array class does not
691 // override finalize().
692 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
693 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
694
695 if (InsertClass(new_class)) {
696 return new_class;
697 }
698 // Another thread must have loaded the class after we
699 // started but before we finished. Abandon what we've
700 // done.
701 //
702 // (Yes, this happens.)
703
704 // Grab the winning class.
705 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
706 DCHECK(other_class != NULL);
707 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700708}
709
710Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700711 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700712 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700713 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700714 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700715 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700716 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700717 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700718 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700719 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700720 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700721 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700722 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700723 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700724 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700725 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700726 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700727 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700728 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700729 return GetClassRoot(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700730 case 'L':
731 case '[':
732 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700733 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700734 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro744ad052011-08-06 15:53:36 -0700735 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700736 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700737}
738
739bool ClassLinker::InsertClass(Class* klass) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700740 MutexLock mu(classes_lock_);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700741 const StringPiece& key = klass->GetDescriptor();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700742 Table::iterator it = classes_.insert(std::make_pair(key, klass));
743 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700744}
745
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700746Class* ClassLinker::LookupClass(const StringPiece& descriptor, ClassLoader* class_loader) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700747 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700748 typedef Table::const_iterator It; // TODO: C++0x auto
749 for (It it = classes_.find(descriptor), end = classes_.end(); it != end; ++it) {
750 Class* klass = it->second;
751 if (klass->descriptor_ == descriptor && klass->class_loader_ == class_loader) {
752 return klass;
753 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700754 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700755 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700756}
757
758bool ClassLinker::InitializeClass(Class* klass) {
759 CHECK(klass->GetStatus() == Class::kStatusResolved ||
760 klass->GetStatus() == Class::kStatusError);
761
Carl Shapirob5573532011-07-12 18:22:59 -0700762 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700763
764 {
765 ObjectLock lock(klass);
766
767 if (klass->GetStatus() < Class::kStatusVerified) {
768 if (klass->IsErroneous()) {
769 LG << "re-initializing failed class"; // TODO: throw
770 return false;
771 }
772
773 CHECK(klass->GetStatus() == Class::kStatusResolved);
774
775 klass->status_ = Class::kStatusVerifying;
776 if (!DexVerify::VerifyClass(klass)) {
777 LG << "Verification failed"; // TODO: ThrowVerifyError
778 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700779 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
780 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700781 klass->SetStatus(Class::kStatusError);
782 return false;
783 }
784
785 klass->SetStatus(Class::kStatusVerified);
786 }
787
788 if (klass->status_ == Class::kStatusInitialized) {
789 return true;
790 }
791
792 while (klass->status_ == Class::kStatusInitializing) {
793 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700794 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700795 LG << "recursive <clinit>";
796 return true;
797 }
798
799 CHECK(!self->IsExceptionPending());
800
801 lock.Wait(); // TODO: check for interruption
802
803 // When we wake up, repeat the test for init-in-progress. If
804 // there's an exception pending (only possible if
805 // "interruptShouldThrow" was set), bail out.
806 if (self->IsExceptionPending()) {
807 CHECK(false);
808 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
809 klass->SetStatus(Class::kStatusError);
810 return false;
811 }
812 if (klass->GetStatus() == Class::kStatusInitializing) {
813 continue;
814 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700815 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700816 klass->GetStatus() == Class::kStatusError);
817 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700818 // The caller wants an exception, but it was thrown in a
819 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700820 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
821 return false;
822 }
823 return true; // otherwise, initialized
824 }
825
826 // see if we failed previously
827 if (klass->IsErroneous()) {
828 // might be wise to unlock before throwing; depends on which class
829 // it is that we have locked
830
831 // TODO: throwEarlierClassFailure(klass);
832 return false;
833 }
834
835 if (!ValidateSuperClassDescriptors(klass)) {
836 klass->SetStatus(Class::kStatusError);
837 return false;
838 }
839
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700840 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700841
Carl Shapirob5573532011-07-12 18:22:59 -0700842 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700843 klass->status_ = Class::kStatusInitializing;
844 }
845
846 if (!InitializeSuperClass(klass)) {
847 return false;
848 }
849
850 InitializeStaticFields(klass);
851
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700852 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700853 if (clinit != NULL) {
854 } else {
855 // JValue unused;
856 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700857 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700858 }
859
860 {
861 ObjectLock lock(klass);
862
863 if (self->IsExceptionPending()) {
864 klass->SetStatus(Class::kStatusError);
865 } else {
866 klass->SetStatus(Class::kStatusInitialized);
867 }
868 lock.NotifyAll();
869 }
870
871 return true;
872}
873
874bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
875 if (klass->IsInterface()) {
876 return true;
877 }
878 // begin with the methods local to the superclass
879 if (klass->HasSuperClass() &&
880 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
881 const Class* super = klass->GetSuperClass();
882 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
883 const Method* method = klass->GetVirtualMethod(i);
884 if (method != super->GetVirtualMethod(i) &&
885 !HasSameMethodDescriptorClasses(method, super, klass)) {
886 LG << "Classes resolve differently in superclass";
887 return false;
888 }
889 }
890 }
891 for (size_t i = 0; i < klass->iftable_count_; ++i) {
892 const InterfaceEntry* iftable = &klass->iftable_[i];
893 Class* interface = iftable->GetClass();
894 if (klass->GetClassLoader() != interface->GetClassLoader()) {
895 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
896 uint32_t vtable_index = iftable->method_index_array_[j];
897 const Method* method = klass->GetVirtualMethod(vtable_index);
898 if (!HasSameMethodDescriptorClasses(method, interface,
899 method->GetClass())) {
900 LG << "Classes resolve differently in interface"; // TODO: LinkageError
901 return false;
902 }
903 }
904 }
905 }
906 return true;
907}
908
909bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700910 const Class* klass1,
911 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700912 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
913 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700914 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700915 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700916 const char* descriptor = it->GetDescriptor();
917 if (descriptor == NULL) {
918 break;
919 }
920 if (descriptor[0] == 'L' || descriptor[0] == '[') {
921 // Found a non-primitive type.
922 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
923 return false;
924 }
925 }
926 }
927 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700928 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700929 if (descriptor[0] == 'L' || descriptor[0] == '[') {
930 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
931 return false;
932 }
933 }
934 return true;
935}
936
937// Returns true if classes referenced by the descriptor are the
938// same classes in klass1 as they are in klass2.
939bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700940 const Class* klass1,
941 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700942 CHECK(descriptor != NULL);
943 CHECK(klass1 != NULL);
944 CHECK(klass2 != NULL);
945#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700946 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700947 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700948 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700949 // TODO: found2 == NULL
950 // TODO: lookup found1 in initiating loader list
951 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700952 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700953 if (found1 == found2) {
954 return true;
955 } else {
956 return false;
957 }
958 }
959#endif
960 return true;
961}
962
963bool ClassLinker::InitializeSuperClass(Class* klass) {
964 CHECK(klass != NULL);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700965 MutexLock mu(classes_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700966 if (!klass->IsInterface() && klass->HasSuperClass()) {
967 Class* super_class = klass->GetSuperClass();
968 if (super_class->GetStatus() != Class::kStatusInitialized) {
969 CHECK(!super_class->IsInterface());
970 klass->MonitorExit();
971 bool super_initialized = InitializeClass(super_class);
972 klass->MonitorEnter();
973 // TODO: check for a pending exception
974 if (!super_initialized) {
975 klass->SetStatus(Class::kStatusError);
976 klass->NotifyAll();
977 return false;
978 }
979 }
980 }
981 return true;
982}
983
984void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700985 size_t num_static_fields = klass->NumStaticFields();
986 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700987 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700988 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700989 DexCache* dex_cache = klass->GetDexCache();
990 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700991 return;
992 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700993 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700994 const DexFile& dex_file = FindDexFile(dex_cache);
995 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -0700996 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700997 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700998 size_t array_size = DecodeUnsignedLeb128(&addr);
999 for (size_t i = 0; i < array_size; ++i) {
1000 StaticField* field = klass->GetStaticField(i);
1001 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001002 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001003 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001004 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001005 field->SetByte(value.b);
1006 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001007 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001008 field->SetShort(value.s);
1009 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001010 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001011 field->SetChar(value.c);
1012 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001013 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001014 field->SetInt(value.i);
1015 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001016 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001017 field->SetLong(value.j);
1018 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001019 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001020 field->SetFloat(value.f);
1021 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001022 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001023 field->SetDouble(value.d);
1024 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001025 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001026 uint32_t string_idx = value.i;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001027 String* resolved = ResolveString(klass, string_idx, dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001028 field->SetObject(resolved);
1029 break;
1030 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001031 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001032 field->SetBoolean(value.z);
1033 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001034 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001035 field->SetObject(value.l);
1036 break;
1037 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001038 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001039 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001040 }
1041}
1042
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001043bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1044 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001045 if (!LinkSuperClass(klass)) {
1046 return false;
1047 }
1048 if (!LinkMethods(klass)) {
1049 return false;
1050 }
1051 if (!LinkInstanceFields(klass)) {
1052 return false;
1053 }
1054 CreateReferenceOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001055 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001056 klass->status_ = Class::kStatusResolved;
1057 return true;
1058}
1059
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001060bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1061 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001062 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1063 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001064 if (super_class == NULL) {
1065 LG << "Failed to resolve superclass";
1066 return false;
1067 }
1068 klass->super_class_ = super_class; // TODO: write barrier
1069 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001070 if (klass->NumInterfaces() > 0) {
1071 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1072 uint32_t idx = klass->interfaces_idx_[i];
1073 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1074 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001075 LG << "Failed to resolve interface";
1076 return false;
1077 }
1078 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001079 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001080 LG << "Inaccessible interface";
1081 return false;
1082 }
1083 }
1084 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001085 // Mark the class as loaded.
1086 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087 return true;
1088}
1089
1090bool ClassLinker::LinkSuperClass(Class* klass) {
1091 CHECK(!klass->IsPrimitive());
1092 const Class* super = klass->GetSuperClass();
1093 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1094 if (super != NULL) {
1095 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1096 return false;
1097 }
1098 // TODO: clear finalize attribute
1099 return true;
1100 }
1101 if (super == NULL) {
1102 LG << "No superclass defined"; // TODO: LinkageError
1103 return false;
1104 }
1105 // Verify
1106 if (super->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001107 LG << "Superclass " << super->descriptor_ << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001108 return false;
1109 }
1110 if (super->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001111 LG << "Superclass " << super->descriptor_ << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001112 return false;
1113 }
1114 if (!klass->CanAccess(super)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001115 LG << "Superclass " << super->descriptor_ << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001116 return false;
1117 }
1118 return true;
1119}
1120
1121// Populate the class vtable and itable.
1122bool ClassLinker::LinkMethods(Class* klass) {
1123 if (klass->IsInterface()) {
1124 // No vtable.
1125 size_t count = klass->NumVirtualMethods();
1126 if (!IsUint(16, count)) {
1127 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1128 return false;
1129 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001130 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001131 klass->GetVirtualMethod(i)->method_index_ = i;
1132 }
1133 } else {
1134 // Link virtual method tables
1135 LinkVirtualMethods(klass);
1136
1137 // Link interface method tables
1138 LinkInterfaceMethods(klass);
1139
1140 // Insert stubs.
1141 LinkAbstractMethods(klass);
1142 }
1143 return true;
1144}
1145
1146bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001147 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001148 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001149 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1150 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001151 // TODO: do not assign to the vtable field until it is fully constructed.
1152 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001153 // See if any of our virtual methods override the superclass.
1154 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1155 Method* local_method = klass->GetVirtualMethod(i);
1156 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001157 for (; j < actual_count; ++j) {
1158 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001159 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001160 // Verify
1161 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001162 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001163 return false;
1164 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001165 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001166 local_method->method_index_ = j;
1167 break;
1168 }
1169 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001170 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001171 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001172 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001173 local_method->method_index_ = actual_count;
1174 actual_count += 1;
1175 }
1176 }
1177 if (!IsUint(16, actual_count)) {
1178 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1179 return false;
1180 }
1181 CHECK_LE(actual_count, max_count);
1182 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001183 // TODO: do not assign to the vtable field until it is fully constructed.
1184 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001185 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001186 } else {
1187 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001188 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1189 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1190 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001191 LG << "Too many methods"; // TODO: VirtualMachineError
1192 return false;
1193 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001194 // TODO: do not assign to the vtable field until it is fully constructed.
1195 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1196 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001197 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001198 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1199 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001200 }
1201 return true;
1202}
1203
1204bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1205 int pool_offset = 0;
1206 int pool_size = 0;
1207 int miranda_count = 0;
1208 int miranda_alloc = 0;
1209 size_t super_ifcount;
1210 if (klass->HasSuperClass()) {
1211 super_ifcount = klass->GetSuperClass()->iftable_count_;
1212 } else {
1213 super_ifcount = 0;
1214 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001215 size_t ifcount = super_ifcount;
1216 ifcount += klass->NumInterfaces();
1217 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1218 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001219 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001220 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001221 DCHECK(klass->iftable_count_ == 0);
1222 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001223 return true;
1224 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001225 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1226 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001227 if (super_ifcount != 0) {
1228 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1229 sizeof(InterfaceEntry) * super_ifcount);
1230 }
1231 // Flatten the interface inheritance hierarchy.
1232 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001233 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1234 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001235 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001236 if (!interf->IsInterface()) {
1237 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1238 return false;
1239 }
1240 klass->iftable_[idx++].SetClass(interf);
1241 for (size_t j = 0; j < interf->iftable_count_; j++) {
1242 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1243 }
1244 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001245 CHECK_EQ(idx, ifcount);
1246 klass->iftable_count_ = ifcount;
1247 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001248 return true;
1249 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001250 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001251 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1252 }
1253 if (pool_size == 0) {
1254 return true;
1255 }
1256 klass->ifvi_pool_count_ = pool_size;
1257 klass->ifvi_pool_ = new uint32_t[pool_size];
1258 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001259 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001260 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1261 Class* interface = klass->iftable_[i].GetClass();
1262 pool_offset += interface->NumVirtualMethods(); // end here
1263 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1264 Method* interface_method = interface->GetVirtualMethod(j);
1265 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001266 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001267 Method* vtable_method = klass->vtable_->Get(k);
1268 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1269 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001270 LG << "Implementation not public";
1271 return false;
1272 }
1273 klass->iftable_[i].method_index_array_[j] = k;
1274 break;
1275 }
1276 }
1277 if (k < 0) {
1278 if (miranda_count == miranda_alloc) {
1279 miranda_alloc += 8;
1280 if (miranda_list.empty()) {
1281 miranda_list.resize(miranda_alloc);
1282 } else {
1283 miranda_list.resize(miranda_alloc);
1284 }
1285 }
1286 int mir;
1287 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001288 Method* miranda_method = miranda_list[mir];
1289 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001290 break;
1291 }
1292 }
1293 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001294 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001295 if (mir == miranda_count) {
1296 miranda_list[miranda_count++] = interface_method;
1297 }
1298 }
1299 }
1300 }
1301 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001302 int old_method_count = klass->NumVirtualMethods();
1303 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001304 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001305
1306 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001307 int old_vtable_count = klass->vtable_->GetLength();
1308 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001309 // TODO: do not assign to the vtable field until it is fully constructed.
1310 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001311
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001312 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001313 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001314 memcpy(meth, miranda_list[i], sizeof(Method));
1315 meth->klass_ = klass;
1316 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001317 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1318 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001319 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001320 }
1321 }
1322 return true;
1323}
1324
1325void ClassLinker::LinkAbstractMethods(Class* klass) {
1326 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1327 Method* method = klass->GetVirtualMethod(i);
1328 if (method->IsAbstract()) {
1329 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1330 }
1331 }
1332}
1333
1334bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001335 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001336 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001337 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001338 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001339 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001340 }
1341 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001342 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001343 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001344 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001345 InstanceField* pField = klass->GetInstanceField(i);
1346 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001347 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001348 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1349 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001350 char rc = refField->GetType();
1351 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001352 klass->SetInstanceField(i, refField);
1353 klass->SetInstanceField(j, pField);
1354 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001355 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001356 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001357 break;
1358 }
1359 }
1360 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001361 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001362 }
1363 if (c != '[' && c != 'L') {
1364 break;
1365 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001366 pField->SetOffset(field_offset);
1367 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001368 }
1369
1370 // Now we want to pack all of the double-wide fields together. If
1371 // we're not aligned, though, we want to shuffle one 32-bit field
1372 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001373 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001374 InstanceField* pField = klass->GetInstanceField(i);
1375 char c = pField->GetType();
1376
1377 if (c != 'J' && c != 'D') {
1378 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001379 DCHECK(c != '[');
1380 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001381 pField->SetOffset(field_offset);
1382 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001383 i++;
1384 } else {
1385 // Next field is 64-bit, so search for a 32-bit field we can
1386 // swap into it.
1387 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001388 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1389 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001390 char rc = singleField->GetType();
1391 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001392 klass->SetInstanceField(i, singleField);
1393 klass->SetInstanceField(j, pField);
1394 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001395 pField->SetOffset(field_offset);
1396 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001397 found = true;
1398 i++;
1399 break;
1400 }
1401 }
1402 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001403 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001404 }
1405 }
1406 }
1407
1408 // Alignment is good, shuffle any double-wide fields forward, and
1409 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001410 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001411 for ( ; i < klass->NumInstanceFields(); i++) {
1412 InstanceField* pField = klass->GetInstanceField(i);
1413 char c = pField->GetType();
1414 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001415 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1416 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001417 char rc = doubleField->GetType();
1418 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001419 klass->SetInstanceField(i, doubleField);
1420 klass->SetInstanceField(j, pField);
1421 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001422 c = rc;
1423 break;
1424 }
1425 }
1426 } else {
1427 // This is a double-wide field, leave it be.
1428 }
1429
Brian Carlstroma0808032011-07-18 00:39:23 -07001430 pField->SetOffset(field_offset);
1431 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001432 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001433 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001434 }
1435
1436#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001437 // Make sure that all reference fields appear before
1438 // non-reference fields, and all double-wide fields are aligned.
1439 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001440 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001441 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001442 char c = pField->GetType();
1443
1444 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001445 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001446 }
1447
1448 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001449 if (!seen_non_ref) {
1450 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001451 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001452 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001453 } else {
1454 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001455 }
1456 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001457 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001458 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459 }
1460#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001461 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001462 return true;
1463}
1464
1465// Set the bitmap of reference offsets, refOffsets, from the ifields
1466// list.
1467void ClassLinker::CreateReferenceOffsets(Class* klass) {
1468 uint32_t reference_offsets = 0;
1469 if (klass->HasSuperClass()) {
1470 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1471 }
1472 // If our superclass overflowed, we don't stand a chance.
1473 if (reference_offsets != CLASS_WALK_SUPER) {
1474 // All of the fields that contain object references are guaranteed
1475 // to be at the beginning of the ifields list.
1476 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1477 // Note that, per the comment on struct InstField, f->byteOffset
1478 // is the offset from the beginning of obj, not the offset into
1479 // obj->instanceData.
1480 const InstanceField* field = klass->GetInstanceField(i);
1481 size_t byte_offset = field->GetOffset();
1482 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001483 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001484 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1485 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001486 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001487 reference_offsets |= new_bit;
1488 } else {
1489 reference_offsets = CLASS_WALK_SUPER;
1490 break;
1491 }
1492 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001493 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001494 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001495}
1496
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001497Class* ClassLinker::ResolveClass(const Class* referrer,
1498 uint32_t class_idx,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001499 const DexFile& dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001500 DexCache* dex_cache = referrer->GetDexCache();
1501 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001502 if (resolved != NULL) {
1503 return resolved;
1504 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001505 const char* descriptor = dex_file.dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001506 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001507 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001508 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001509 resolved = FindClass(descriptor, referrer->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001510 }
1511 if (resolved != NULL) {
1512 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001513 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001514 if (check->GetClassLoader() != NULL) {
1515 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1516 return NULL;
1517 }
1518 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001519 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001520 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001521 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001522 }
1523 return resolved;
1524}
1525
1526Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1527 /*MethodType*/ int method_type) {
1528 CHECK(false);
1529 return NULL;
1530}
1531
Carl Shapiro69759ea2011-07-21 18:13:35 -07001532String* ClassLinker::ResolveString(const Class* referring,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001533 uint32_t string_idx,
1534 const DexFile& dex_file) {
1535 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1536 int32_t utf16_length = dex_file.GetStringLength(string_id);
1537 const char* utf8_data = dex_file.GetStringData(string_id);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001538 String* string = intern_table_.Intern(utf16_length, utf8_data);
1539 referring->GetDexCache()->SetResolvedString(string_idx, string);
1540 return string;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001541}
1542
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001543} // namespace art