blob: 3e1eb06c3fc54f608847d0121ec2cb9748ed8813 [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
Jesse Wilson14150742011-07-29 19:04:44 -040052 // string is necessary so that FindClass can assigning names to members
53 Class* java_lang_String = AllocClass(java_lang_Class);
54 CHECK(java_lang_String != NULL);
55 java_lang_String->object_size_ = sizeof(String);
56
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070057 // create storage for root classes, save away our work so far
58 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
59 class_roots_->Set(kJavaLangClass, java_lang_Class);
60 class_roots_->Set(kJavaLangObject, java_lang_Object);
61 class_roots_->Set(kObjectArrayClass, object_array_class);
Jesse Wilson14150742011-07-29 19:04:44 -040062 class_roots_->Set(kJavaLangString, java_lang_String);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070063 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -070064
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070065 // setup boot_class_path_ now that we can use AllocObjectArray to
66 // DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -070067 for (size_t i = 0; i != boot_class_path.size(); ++i) {
68 AppendToBootClassPath(boot_class_path[i]);
69 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070070 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -070071
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070072 // run Object and Class to setup their dex_cache_ fields and register them in classes_.
73 // we also override their object_size_ values to accommodate the extra C++ fields.
74 Class* Object_class = FindSystemClass(java_lang_Object->GetDescriptor());
75 CHECK_EQ(java_lang_Object, Object_class);
76 CHECK_LE(java_lang_Object->object_size_, sizeof(Object));
77 java_lang_Object->object_size_ = sizeof(Object);
78 Class* Class_class = FindSystemClass(java_lang_Class->GetDescriptor());
79 CHECK_EQ(java_lang_Class, Class_class);
80 CHECK_LE(java_lang_Class->object_size_, sizeof(Class));
81 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom913af1b2011-07-23 21:41:13 -070082
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070083 // set special sizes for these C++ extended classes (Field, Method, String).
84 // we also remember them in class_roots_ to construct them within ClassLinker
85 Class* java_lang_reflect_Field = FindSystemClass("Ljava/lang/reflect/Field;");
86 CHECK(java_lang_reflect_Field != NULL);
87 CHECK_LE(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField)));
88 java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField));
89 class_roots_->Set(kJavaLangReflectField, java_lang_reflect_Field);
90
91 Class* java_lang_reflect_Method = FindSystemClass("Ljava/lang/reflect/Method;");
92 CHECK(java_lang_reflect_Method != NULL);
93 CHECK_LE(java_lang_reflect_Method->object_size_, sizeof(Method));
94 java_lang_reflect_Method->object_size_ = sizeof(Method);
95 class_roots_->Set(kJavaLangReflectMethod, java_lang_reflect_Method);
96
Jesse Wilsonf7e85a52011-08-01 18:45:58 -070097 Class* String_class = FindSystemClass("Ljava/lang/String;");
98 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070099 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
100 java_lang_String->object_size_ = sizeof(String);
101 class_roots_->Set(kJavaLangString, java_lang_String);
102
103 // Setup a single, global copy of "interfaces" and "iftable" for
104 // reuse across array classes
105 Class* java_lang_Cloneable = AllocClass();
106 CHECK(java_lang_Cloneable != NULL);
107 java_lang_Cloneable->descriptor_ = "Ljava/lang/Cloneable;";
108
109 Class* java_io_Serializable = AllocClass();
110 CHECK(java_io_Serializable != NULL);
111 java_io_Serializable->descriptor_ = "Ljava/io/Serializable;";
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700112
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700113 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700114 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700115 array_interfaces_->Set(0, java_lang_Cloneable);
116 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700117
118 // We assume that Cloneable/Serializable don't have superinterfaces --
119 // normally we'd have to crawl up and explicitly list all of the
120 // supers as well. These interfaces don't have any methods, so we
121 // don't have to worry about the ifviPool either.
122 array_iftable_ = new InterfaceEntry[2];
123 CHECK(array_iftable_ != NULL);
124 memset(array_iftable_, 0, sizeof(InterfaceEntry) * 2);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700125 array_iftable_[0].SetClass(array_interfaces_->Get(0));
126 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700127 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700128
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700129 // run Object[] through FindClass to complete initialization
130 Class* Object_array_class = FindSystemClass("[Ljava/lang/Object;");
131 CHECK_EQ(object_array_class, Object_array_class);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700132
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700133 // Setup the primitive type classes.
134 class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B"));
135 class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C"));
136 class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D"));
137 class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F"));
138 class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I"));
139 class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J"));
140 class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S"));
141 class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
142 class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V"));
143 // now we can use FindSystemClass for anything, including for "[C"
144
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700145 Class* char_array = FindSystemClass("[C");
146 class_roots_->Set(kCharArrayClass, char_array);
147 String::InitClasses(java_lang_String, char_array);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700148 // Now AllocString* can be used
149
150 // ensure all class_roots_ were initialized
151 for (size_t i = 0; i < kClassRootsMax; i++) {
152 CHECK(class_roots_->Get(i) != NULL);
153 }
154
155 // disable the slow paths in FindClass and CreatePrimitiveClass now
156 // that Object, Class, and Object[] are setup
157 init_done_ = true;
158}
159
Brian Carlstromb88e9442011-07-28 15:15:51 -0700160void ClassLinker::VisitRoots(RootVistor* root_visitor, void* arg) {
161 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700162
163 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700164 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700165 }
166
167 // TODO: acquire classes_lock_
168 typedef Table::const_iterator It; // TODO: C++0x auto
169 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700170 root_visitor(it->second, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700171 }
172 // TODO: release classes_lock_
173
Brian Carlstromb88e9442011-07-28 15:15:51 -0700174 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700175}
176
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700177DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700178 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700179}
180
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700181Class* ClassLinker::AllocClass(Class* java_lang_Class) {
182 return down_cast<Class*>(Object::Alloc(java_lang_Class));
183}
184
185Class* ClassLinker::AllocClass() {
186 return AllocClass(class_roots_->Get(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700187}
188
189StaticField* ClassLinker::AllocStaticField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700190 return down_cast<StaticField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700191 sizeof(StaticField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700192}
193
194InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700195 return down_cast<InstanceField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700196 sizeof(InstanceField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700197}
198
199Method* ClassLinker::AllocMethod() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700200 return down_cast<Method*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectMethod),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700201 sizeof(Method)));
Carl Shapiro565f5072011-07-10 13:39:43 -0700202}
203
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700204String* ClassLinker::AllocStringFromModifiedUtf8(int32_t utf16_length,
205 const char* utf8_data_in) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700206 return String::AllocFromModifiedUtf8(class_roots_->Get(kJavaLangString),
207 class_roots_->Get(kCharArrayClass),
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700208 utf16_length,
209 utf8_data_in);
210}
211
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700212Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700213 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700214 const DexFile* dex_file) {
Carl Shapirob5573532011-07-12 18:22:59 -0700215 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700216 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700217 CHECK(!self->IsExceptionPending());
218 // Find the class in the loaded classes table.
219 Class* klass = LookupClass(descriptor, class_loader);
220 if (klass == NULL) {
221 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700222 if (descriptor[0] == '[') {
Brian Carlstromf615a612011-07-23 12:50:34 -0700223 return CreateArrayClass(descriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700224 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700225 ClassPathEntry pair;
Brian Carlstromf615a612011-07-23 12:50:34 -0700226 if (dex_file == NULL) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700227 pair = FindInBootClassPath(descriptor);
228 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700229 pair.first = dex_file;
230 pair.second = dex_file->FindClassDef(descriptor);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700231 }
232 if (pair.second == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700233 LG << "Class " << descriptor << " not found"; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700234 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700235 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700236 const DexFile* dex_file = pair.first;
237 const DexFile::ClassDef* dex_class_def = pair.second;
238 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700239 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700240 if (!init_done_) {
241 // finish up init of hand crafted class_roots_
242 if (descriptor == "Ljava/lang/Object;") {
243 klass = class_roots_->Get(kJavaLangObject);
244 } else if (descriptor == "Ljava/lang/Class;") {
245 klass = class_roots_->Get(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400246 } else if (descriptor == "Ljava/lang/String;") {
247 klass = class_roots_->Get(kJavaLangString);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700248 } else {
249 klass = AllocClass();
250 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700251 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700252 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700253 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700254 klass->dex_cache_ = dex_cache;
Brian Carlstromf615a612011-07-23 12:50:34 -0700255 LoadClass(*dex_file, *dex_class_def, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700256 // Check for a pending exception during load
257 if (self->IsExceptionPending()) {
258 // TODO: free native allocations in klass
259 return NULL;
260 }
261 {
262 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700263 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700264 // Add the newly loaded class to the loaded classes table.
265 bool success = InsertClass(klass);
266 if (!success) {
267 // We may fail to insert if we raced with another thread.
268 klass->clinit_thread_id_ = 0;
269 // TODO: free native allocations in klass
270 klass = LookupClass(descriptor, class_loader);
271 CHECK(klass != NULL);
272 } else {
273 // Link the class.
Brian Carlstromf615a612011-07-23 12:50:34 -0700274 if (!LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700275 // Linking failed.
276 // TODO: CHECK(self->IsExceptionPending());
277 lock.NotifyAll();
278 return NULL;
279 }
280 }
281 }
282 }
283 // Link the class if it has not already been linked.
284 if (!klass->IsLinked() && !klass->IsErroneous()) {
285 ObjectLock lock(klass);
286 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700287 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700288 LG << "Recursive link"; // TODO: ClassCircularityError
289 return NULL;
290 }
291 // Wait for the pending initialization to complete.
292 while (!klass->IsLinked() && !klass->IsErroneous()) {
293 lock.Wait();
294 }
295 }
296 if (klass->IsErroneous()) {
297 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
298 return NULL;
299 }
300 // Return the loaded class. No exceptions should be pending.
301 CHECK(!self->IsExceptionPending());
302 return klass;
303}
304
Brian Carlstromf615a612011-07-23 12:50:34 -0700305void ClassLinker::LoadClass(const DexFile& dex_file,
306 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700307 Class* klass) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700308 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700309 CHECK(klass->dex_cache_ != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700310 const byte* class_data = dex_file.GetClassData(dex_class_def);
311 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700312
Brian Carlstromf615a612011-07-23 12:50:34 -0700313 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700314 CHECK(descriptor != NULL);
315
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700316 klass->klass_ = class_roots_->Get(kJavaLangClass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700317 klass->descriptor_.set(descriptor);
318 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700319 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700320 klass->class_loader_ = NULL; // TODO
321 klass->primitive_type_ = Class::kPrimNot;
322 klass->status_ = Class::kStatusIdx;
323
324 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700325 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700326
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700327 size_t num_static_fields = header.static_fields_size_;
328 size_t num_instance_fields = header.instance_fields_size_;
329 size_t num_direct_methods = header.direct_methods_size_;
330 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700331
Brian Carlstromf615a612011-07-23 12:50:34 -0700332 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700333
334 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700335 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700336
337 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700338 DCHECK(klass->sfields_ == NULL);
339 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700340 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700341 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700342 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700343 DexFile::Field dex_field;
344 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700345 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700346 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700347 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700348 }
349 }
350
351 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700352 DCHECK(klass->ifields_ == NULL);
353 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700354 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700355 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700356 uint32_t last_idx = 0;
357 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700358 DexFile::Field dex_field;
359 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700360 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700361 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700362 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700363 }
364 }
365
366 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700367 DCHECK(klass->direct_methods_ == NULL);
368 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700369 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700370 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700371 uint32_t last_idx = 0;
372 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700373 DexFile::Method dex_method;
374 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700375 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700376 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700377 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700378 // TODO: register maps
379 }
380 }
381
382 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700383 DCHECK(klass->virtual_methods_ == NULL);
384 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700385 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700386 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700387 uint32_t last_idx = 0;
388 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700389 DexFile::Method dex_method;
390 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700391 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700392 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700393 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700394 // TODO: register maps
395 }
396 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700397}
398
Brian Carlstromf615a612011-07-23 12:50:34 -0700399void ClassLinker::LoadInterfaces(const DexFile& dex_file,
400 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700401 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700402 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700403 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700404 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700405 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700406 DCHECK(klass->interfaces_idx_ == NULL);
407 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700408 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700409 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700410 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700411 }
412 }
413}
414
Brian Carlstromf615a612011-07-23 12:50:34 -0700415void ClassLinker::LoadField(const DexFile& dex_file,
416 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700417 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700418 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700419 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700420 dst->klass_ = klass;
Jesse Wilson14150742011-07-29 19:04:44 -0400421 dst->java_name_ = ResolveString(klass, field_id.name_idx_);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700422 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700423 dst->access_flags_ = src.access_flags_;
424}
425
Brian Carlstromf615a612011-07-23 12:50:34 -0700426void ClassLinker::LoadMethod(const DexFile& dex_file,
427 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700428 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700429 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700430 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700431 dst->klass_ = klass;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700432 dst->java_name_ = ResolveString(klass, method_id.name_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700433 dst->proto_idx_ = method_id.proto_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700434 dst->shorty_.set(dex_file.GetShorty(method_id.proto_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700435 dst->access_flags_ = src.access_flags_;
436
437 // TODO: check for finalize method
438
Brian Carlstromf615a612011-07-23 12:50:34 -0700439 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700440 if (code_item != NULL) {
441 dst->num_registers_ = code_item->registers_size_;
442 dst->num_ins_ = code_item->ins_size_;
443 dst->num_outs_ = code_item->outs_size_;
444 dst->insns_ = code_item->insns_;
445 } else {
446 uint16_t num_args = dst->NumArgRegisters();
447 if (!dst->IsStatic()) {
448 ++num_args;
449 }
450 dst->num_registers_ = dst->num_ins_ + num_args;
451 // TODO: native methods
452 }
453}
454
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700455ClassLinker::ClassPathEntry ClassLinker::FindInBootClassPath(const StringPiece& descriptor) {
456 for (size_t i = 0; i != boot_class_path_.size(); ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700457 const DexFile* dex_file = boot_class_path_[i];
Brian Carlstromf615a612011-07-23 12:50:34 -0700458 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
459 if (dex_class_def != NULL) {
460 return ClassPathEntry(dex_file, dex_class_def);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700461 }
462 }
Brian Carlstroma0808032011-07-18 00:39:23 -0700463 return ClassPathEntry(NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700464}
465
Brian Carlstromf615a612011-07-23 12:50:34 -0700466void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700467 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700468 boot_class_path_.push_back(dex_file);
469 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700470}
471
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700472void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700473 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700474 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700475 DexCache* dex_cache = AllocDexCache();
476 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700477 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
478 AllocObjectArray<Class>(dex_file->NumTypeIds()),
479 AllocObjectArray<Method>(dex_file->NumMethodIds()),
480 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700481 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700482}
483
Brian Carlstromf615a612011-07-23 12:50:34 -0700484const DexFile* ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700485 CHECK(dex_cache != NULL);
486 for (size_t i = 0; i != dex_caches_.size(); ++i) {
487 if (dex_caches_[i] == dex_cache) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700488 return dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700489 }
490 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700491 CHECK(false) << "Could not find DexFile";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700492 return NULL;
493}
494
Brian Carlstromf615a612011-07-23 12:50:34 -0700495DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
496 CHECK(dex_file != NULL);
497 for (size_t i = 0; i != dex_files_.size(); ++i) {
498 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700499 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700500 }
501 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700502 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700503 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700504}
505
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700506Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700507 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700508 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700509 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700510 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
511 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700512 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700513 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700514 bool success = InsertClass(klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700515 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700516 return klass;
517}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700518
Brian Carlstrombe977852011-07-19 14:54:54 -0700519// Create an array class (i.e. the class object for the array, not the
520// array itself). "descriptor" looks like "[C" or "[[[[B" or
521// "[Ljava/lang/String;".
522//
523// If "descriptor" refers to an array of primitives, look up the
524// primitive type's internally-generated class object.
525//
526// "loader" is the class loader of the class that's referring to us. It's
527// used to ensure that we're looking for the element type in the right
528// context. It does NOT become the class loader for the array class; that
529// always comes from the base element class.
530//
531// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700532Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
533 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700534 const DexFile* dex_file)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700535{
536 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700537
Brian Carlstrombe977852011-07-19 14:54:54 -0700538 // Identify the underlying element class and the array dimension depth.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700539 Class* component_type_ = NULL;
540 int array_rank;
541 if (descriptor[1] == '[') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700542 // array of arrays; keep descriptor and grab stuff from parent
Brian Carlstromf615a612011-07-23 12:50:34 -0700543 Class* outer = FindClass(descriptor.substr(1), class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700544 if (outer != NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700545 // want the base class, not "outer", in our component_type_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700546 component_type_ = outer->component_type_;
547 array_rank = outer->array_rank_ + 1;
548 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700549 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700550 }
551 } else {
552 array_rank = 1;
553 if (descriptor[1] == 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700554 // array of objects; strip off "[" and look up descriptor.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700555 const StringPiece subDescriptor = descriptor.substr(1);
Brian Carlstromf615a612011-07-23 12:50:34 -0700556 component_type_ = FindClass(subDescriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700557 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700558 // array of a primitive type
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700559 component_type_ = FindPrimitiveClass(descriptor[1]);
560 }
561 }
562
563 if (component_type_ == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700564 // failed
Brian Carlstrom8ecd08c2011-07-27 17:50:51 -0700565 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700566 return NULL;
567 }
568
Brian Carlstrombe977852011-07-19 14:54:54 -0700569 // See if the component type is already loaded. Array classes are
570 // always associated with the class loader of their underlying
571 // element type -- an array of Strings goes with the loader for
572 // java/lang/String -- so we need to look for it there. (The
573 // caller should have checked for the existence of the class
574 // before calling here, but they did so with *their* class loader,
575 // not the component type's loader.)
576 //
577 // If we find it, the caller adds "loader" to the class' initiating
578 // loader list, which should prevent us from going through this again.
579 //
580 // This call is unnecessary if "loader" and "component_type_->class_loader_"
581 // are the same, because our caller (FindClass) just did the
582 // lookup. (Even if we get this wrong we still have correct behavior,
583 // because we effectively do this lookup again when we add the new
584 // class to the hash table --- necessary because of possible races with
585 // other threads.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700586 if (class_loader != component_type_->class_loader_) {
587 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
588 if (new_class != NULL) {
589 return new_class;
590 }
591 }
592
Brian Carlstrombe977852011-07-19 14:54:54 -0700593 // Fill out the fields in the Class.
594 //
595 // It is possible to execute some methods against arrays, because
596 // all arrays are subclasses of java_lang_Object_, so we need to set
597 // up a vtable. We can just point at the one in java_lang_Object_.
598 //
599 // Array classes are simple enough that we don't need to do a full
600 // link step.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700601
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700602 Class* new_class = NULL;
603 if (!init_done_ && descriptor == "[Ljava/lang/Object;") {
604 new_class = class_roots_->Get(kObjectArrayClass);
605 CHECK(new_class);
606 }
607 if (new_class == NULL) {
608 new_class = AllocClass();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700609 if (new_class == NULL) {
610 return NULL;
611 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700612 }
613 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
614 descriptor.size());
615 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
616 new_class->descriptor_alloc_->size());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700617 Class* java_lang_Object = class_roots_->Get(kJavaLangObject);
618 new_class->super_class_ = java_lang_Object;
619 new_class->vtable_ = java_lang_Object->vtable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700620 new_class->primitive_type_ = Class::kPrimNot;
621 new_class->component_type_ = component_type_;
622 new_class->class_loader_ = component_type_->class_loader_;
623 new_class->array_rank_ = array_rank;
624 new_class->status_ = Class::kStatusInitialized;
Brian Carlstrombe977852011-07-19 14:54:54 -0700625 // don't need to set new_class->object_size_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700626
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700627
Brian Carlstrombe977852011-07-19 14:54:54 -0700628 // All arrays have java/lang/Cloneable and java/io/Serializable as
629 // interfaces. We need to set that up here, so that stuff like
630 // "instanceof" works right.
631 //
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700632 // Note: The GC could run during the call to FindSystemClass,
Brian Carlstrombe977852011-07-19 14:54:54 -0700633 // so we need to make sure the class object is GC-valid while we're in
634 // there. Do this by clearing the interface list so the GC will just
635 // think that the entries are null.
Brian Carlstrombe977852011-07-19 14:54:54 -0700636
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700637
638 // Use the single, global copies of "interfaces" and "iftable"
639 // (remember not to free them for arrays).
640 DCHECK(array_interfaces_ != NULL);
641 new_class->interfaces_ = array_interfaces_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700642 new_class->iftable_count_ = 2;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700643 DCHECK(array_iftable_ != NULL);
644 new_class->iftable_ = array_iftable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700645
Brian Carlstrombe977852011-07-19 14:54:54 -0700646 // Inherit access flags from the component type. Arrays can't be
647 // used as a superclass or interface, so we want to add "final"
648 // and remove "interface".
649 //
650 // Don't inherit any non-standard flags (e.g., kAccFinal)
651 // from component_type_. We assume that the array class does not
652 // override finalize().
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700653 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
654 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
655
656 if (InsertClass(new_class)) {
657 return new_class;
658 }
Brian Carlstrombe977852011-07-19 14:54:54 -0700659 // Another thread must have loaded the class after we
660 // started but before we finished. Abandon what we've
661 // done.
662 //
663 // (Yes, this happens.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700664
Brian Carlstrombe977852011-07-19 14:54:54 -0700665 // Grab the winning class.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700666 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
667 DCHECK(other_class != NULL);
668 return other_class;
669}
670
671Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700672 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700673 case 'B':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700674 DCHECK(class_roots_->Get(kPrimitiveByte) != NULL);
675 return class_roots_->Get(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700676 case 'C':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700677 DCHECK(class_roots_->Get(kPrimitiveChar) != NULL);
678 return class_roots_->Get(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700679 case 'D':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700680 DCHECK(class_roots_->Get(kPrimitiveDouble) != NULL);
681 return class_roots_->Get(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700682 case 'F':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700683 DCHECK(class_roots_->Get(kPrimitiveFloat) != NULL);
684 return class_roots_->Get(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700685 case 'I':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700686 DCHECK(class_roots_->Get(kPrimitiveInt) != NULL);
687 return class_roots_->Get(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700688 case 'J':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700689 DCHECK(class_roots_->Get(kPrimitiveLong) != NULL);
690 return class_roots_->Get(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700691 case 'S':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700692 DCHECK(class_roots_->Get(kPrimitiveShort) != NULL);
693 return class_roots_->Get(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700694 case 'Z':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700695 DCHECK(class_roots_->Get(kPrimitiveBoolean) != NULL);
696 return class_roots_->Get(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700697 case 'V':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700698 DCHECK(class_roots_->Get(kPrimitiveVoid) != NULL);
699 return class_roots_->Get(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700700 case 'L':
701 case '[':
702 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700703 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700704 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700705 };
706 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700707}
708
709bool ClassLinker::InsertClass(Class* klass) {
710 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700711 const StringPiece& key = klass->GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700712 bool success = classes_.insert(std::make_pair(key, klass)).second;
713 // TODO: release classes_lock_
714 return success;
715}
716
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700717Class* ClassLinker::LookupClass(const StringPiece& descriptor, Object* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700718 // TODO: acquire classes_lock_
719 Table::iterator it = classes_.find(descriptor);
720 // TODO: release classes_lock_
721 if (it == classes_.end()) {
722 return NULL;
723 } else {
724 return (*it).second;
725 }
726}
727
728bool ClassLinker::InitializeClass(Class* klass) {
729 CHECK(klass->GetStatus() == Class::kStatusResolved ||
730 klass->GetStatus() == Class::kStatusError);
731
Carl Shapirob5573532011-07-12 18:22:59 -0700732 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700733
734 {
735 ObjectLock lock(klass);
736
737 if (klass->GetStatus() < Class::kStatusVerified) {
738 if (klass->IsErroneous()) {
739 LG << "re-initializing failed class"; // TODO: throw
740 return false;
741 }
742
743 CHECK(klass->GetStatus() == Class::kStatusResolved);
744
745 klass->status_ = Class::kStatusVerifying;
746 if (!DexVerify::VerifyClass(klass)) {
747 LG << "Verification failed"; // TODO: ThrowVerifyError
748 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700749 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
750 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700751 klass->SetStatus(Class::kStatusError);
752 return false;
753 }
754
755 klass->SetStatus(Class::kStatusVerified);
756 }
757
758 if (klass->status_ == Class::kStatusInitialized) {
759 return true;
760 }
761
762 while (klass->status_ == Class::kStatusInitializing) {
763 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700764 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700765 LG << "recursive <clinit>";
766 return true;
767 }
768
769 CHECK(!self->IsExceptionPending());
770
771 lock.Wait(); // TODO: check for interruption
772
773 // When we wake up, repeat the test for init-in-progress. If
774 // there's an exception pending (only possible if
775 // "interruptShouldThrow" was set), bail out.
776 if (self->IsExceptionPending()) {
777 CHECK(false);
778 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
779 klass->SetStatus(Class::kStatusError);
780 return false;
781 }
782 if (klass->GetStatus() == Class::kStatusInitializing) {
783 continue;
784 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700785 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700786 klass->GetStatus() == Class::kStatusError);
787 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700788 // The caller wants an exception, but it was thrown in a
789 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700790 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
791 return false;
792 }
793 return true; // otherwise, initialized
794 }
795
796 // see if we failed previously
797 if (klass->IsErroneous()) {
798 // might be wise to unlock before throwing; depends on which class
799 // it is that we have locked
800
801 // TODO: throwEarlierClassFailure(klass);
802 return false;
803 }
804
805 if (!ValidateSuperClassDescriptors(klass)) {
806 klass->SetStatus(Class::kStatusError);
807 return false;
808 }
809
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700810 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700811
Carl Shapirob5573532011-07-12 18:22:59 -0700812 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700813 klass->status_ = Class::kStatusInitializing;
814 }
815
816 if (!InitializeSuperClass(klass)) {
817 return false;
818 }
819
820 InitializeStaticFields(klass);
821
822 Method* clinit = klass->FindDirectMethodLocally("<clinit>", "()V");
823 if (clinit != NULL) {
824 } else {
825 // JValue unused;
826 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700827 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700828 }
829
830 {
831 ObjectLock lock(klass);
832
833 if (self->IsExceptionPending()) {
834 klass->SetStatus(Class::kStatusError);
835 } else {
836 klass->SetStatus(Class::kStatusInitialized);
837 }
838 lock.NotifyAll();
839 }
840
841 return true;
842}
843
844bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
845 if (klass->IsInterface()) {
846 return true;
847 }
848 // begin with the methods local to the superclass
849 if (klass->HasSuperClass() &&
850 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
851 const Class* super = klass->GetSuperClass();
852 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
853 const Method* method = klass->GetVirtualMethod(i);
854 if (method != super->GetVirtualMethod(i) &&
855 !HasSameMethodDescriptorClasses(method, super, klass)) {
856 LG << "Classes resolve differently in superclass";
857 return false;
858 }
859 }
860 }
861 for (size_t i = 0; i < klass->iftable_count_; ++i) {
862 const InterfaceEntry* iftable = &klass->iftable_[i];
863 Class* interface = iftable->GetClass();
864 if (klass->GetClassLoader() != interface->GetClassLoader()) {
865 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
866 uint32_t vtable_index = iftable->method_index_array_[j];
867 const Method* method = klass->GetVirtualMethod(vtable_index);
868 if (!HasSameMethodDescriptorClasses(method, interface,
869 method->GetClass())) {
870 LG << "Classes resolve differently in interface"; // TODO: LinkageError
871 return false;
872 }
873 }
874 }
875 }
876 return true;
877}
878
879bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700880 const Class* klass1,
881 const Class* klass2) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700882 const DexFile* dex_file = FindDexFile(method->GetClass()->GetDexCache());
883 const DexFile::ProtoId& proto_id = dex_file->GetProtoId(method->proto_idx_);
884 DexFile::ParameterIterator *it;
885 for (it = dex_file->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700886 const char* descriptor = it->GetDescriptor();
887 if (descriptor == NULL) {
888 break;
889 }
890 if (descriptor[0] == 'L' || descriptor[0] == '[') {
891 // Found a non-primitive type.
892 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
893 return false;
894 }
895 }
896 }
897 // Check the return type
Brian Carlstromf615a612011-07-23 12:50:34 -0700898 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700899 if (descriptor[0] == 'L' || descriptor[0] == '[') {
900 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
901 return false;
902 }
903 }
904 return true;
905}
906
907// Returns true if classes referenced by the descriptor are the
908// same classes in klass1 as they are in klass2.
909bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700910 const Class* klass1,
911 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700912 CHECK(descriptor != NULL);
913 CHECK(klass1 != NULL);
914 CHECK(klass2 != NULL);
915#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700916 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700917 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700918 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700919 // TODO: found2 == NULL
920 // TODO: lookup found1 in initiating loader list
921 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700922 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700923 if (found1 == found2) {
924 return true;
925 } else {
926 return false;
927 }
928 }
929#endif
930 return true;
931}
932
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700933bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700934 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
935 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
936 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
937 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700938
939 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstromf615a612011-07-23 12:50:34 -0700940 const DexFile::TypeList* type_list1 = dex1->GetProtoParameters(proto1);
941 const DexFile::TypeList* type_list2 = dex2->GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700942 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
943 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
944 if (arity1 != arity2) {
945 return false;
946 }
947
948 for (size_t i = 0; i < arity1; ++i) {
949 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
950 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700951 const char* type1 = dex1->dexStringByTypeIdx(type_idx1);
952 const char* type2 = dex2->dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700953 if (strcmp(type1, type2) != 0) {
954 return false;
955 }
956 }
957
958 return true;
959}
960
961bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700962 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
963 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
964 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
965 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
966 const char* type1 = dex1->dexStringByTypeIdx(proto1.return_type_idx_);
967 const char* type2 = dex2->dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700968 return (strcmp(type1, type2) == 0);
969}
970
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700971bool ClassLinker::InitializeSuperClass(Class* klass) {
972 CHECK(klass != NULL);
973 // TODO: assert klass lock is acquired
974 if (!klass->IsInterface() && klass->HasSuperClass()) {
975 Class* super_class = klass->GetSuperClass();
976 if (super_class->GetStatus() != Class::kStatusInitialized) {
977 CHECK(!super_class->IsInterface());
978 klass->MonitorExit();
979 bool super_initialized = InitializeClass(super_class);
980 klass->MonitorEnter();
981 // TODO: check for a pending exception
982 if (!super_initialized) {
983 klass->SetStatus(Class::kStatusError);
984 klass->NotifyAll();
985 return false;
986 }
987 }
988 }
989 return true;
990}
991
992void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700993 size_t num_static_fields = klass->NumStaticFields();
994 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700995 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700996 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700997 DexCache* dex_cache = klass->GetDexCache();
998 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700999 return;
1000 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001001 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstromf615a612011-07-23 12:50:34 -07001002 const DexFile* dex_file = FindDexFile(dex_cache);
1003 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
1004 CHECK(dex_class_def != NULL);
1005 const byte* addr = dex_file->GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001006 size_t array_size = DecodeUnsignedLeb128(&addr);
1007 for (size_t i = 0; i < array_size; ++i) {
1008 StaticField* field = klass->GetStaticField(i);
1009 JValue value;
Brian Carlstromf615a612011-07-23 12:50:34 -07001010 DexFile::ValueType type = dex_file->ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001011 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001012 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001013 field->SetByte(value.b);
1014 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001015 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001016 field->SetShort(value.s);
1017 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001018 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001019 field->SetChar(value.c);
1020 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001021 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001022 field->SetInt(value.i);
1023 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001024 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001025 field->SetLong(value.j);
1026 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001027 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001028 field->SetFloat(value.f);
1029 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001030 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001031 field->SetDouble(value.d);
1032 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001033 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001034 uint32_t string_idx = value.i;
1035 String* resolved = ResolveString(klass, string_idx);
1036 field->SetObject(resolved);
1037 break;
1038 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001039 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001040 field->SetBoolean(value.z);
1041 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001042 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001043 field->SetObject(value.l);
1044 break;
1045 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001046 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001047 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001048 }
1049}
1050
Brian Carlstromf615a612011-07-23 12:50:34 -07001051bool ClassLinker::LinkClass(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001052 CHECK(klass->status_ == Class::kStatusIdx ||
1053 klass->status_ == Class::kStatusLoaded);
1054 if (klass->status_ == Class::kStatusIdx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001055 if (!LinkInterfaces(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001056 return false;
1057 }
1058 }
1059 if (!LinkSuperClass(klass)) {
1060 return false;
1061 }
1062 if (!LinkMethods(klass)) {
1063 return false;
1064 }
1065 if (!LinkInstanceFields(klass)) {
1066 return false;
1067 }
1068 CreateReferenceOffsets(klass);
1069 CHECK_EQ(klass->status_, Class::kStatusLoaded);
1070 klass->status_ = Class::kStatusResolved;
1071 return true;
1072}
1073
Brian Carlstromf615a612011-07-23 12:50:34 -07001074bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001075 // Mark the class as loaded.
1076 klass->status_ = Class::kStatusLoaded;
Brian Carlstromf615a612011-07-23 12:50:34 -07001077 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1078 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001079 if (super_class == NULL) {
1080 LG << "Failed to resolve superclass";
1081 return false;
1082 }
1083 klass->super_class_ = super_class; // TODO: write barrier
1084 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001085 if (klass->NumInterfaces() > 0) {
1086 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1087 uint32_t idx = klass->interfaces_idx_[i];
1088 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1089 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001090 LG << "Failed to resolve interface";
1091 return false;
1092 }
1093 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001094 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001095 LG << "Inaccessible interface";
1096 return false;
1097 }
1098 }
1099 }
1100 return true;
1101}
1102
1103bool ClassLinker::LinkSuperClass(Class* klass) {
1104 CHECK(!klass->IsPrimitive());
1105 const Class* super = klass->GetSuperClass();
1106 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1107 if (super != NULL) {
1108 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1109 return false;
1110 }
1111 // TODO: clear finalize attribute
1112 return true;
1113 }
1114 if (super == NULL) {
1115 LG << "No superclass defined"; // TODO: LinkageError
1116 return false;
1117 }
1118 // Verify
1119 if (super->IsFinal()) {
1120 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
1121 return false;
1122 }
1123 if (super->IsInterface()) {
1124 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
1125 return false;
1126 }
1127 if (!klass->CanAccess(super)) {
1128 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
1129 return false;
1130 }
1131 return true;
1132}
1133
1134// Populate the class vtable and itable.
1135bool ClassLinker::LinkMethods(Class* klass) {
1136 if (klass->IsInterface()) {
1137 // No vtable.
1138 size_t count = klass->NumVirtualMethods();
1139 if (!IsUint(16, count)) {
1140 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1141 return false;
1142 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001143 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001144 klass->GetVirtualMethod(i)->method_index_ = i;
1145 }
1146 } else {
1147 // Link virtual method tables
1148 LinkVirtualMethods(klass);
1149
1150 // Link interface method tables
1151 LinkInterfaceMethods(klass);
1152
1153 // Insert stubs.
1154 LinkAbstractMethods(klass);
1155 }
1156 return true;
1157}
1158
1159bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001160 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001161 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001162 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1163 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001164 // TODO: do not assign to the vtable field until it is fully constructed.
1165 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001166 // See if any of our virtual methods override the superclass.
1167 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1168 Method* local_method = klass->GetVirtualMethod(i);
1169 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001170 for (; j < actual_count; ++j) {
1171 Method* super_method = klass->vtable_->Get(j);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001172 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001173 // Verify
1174 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001175 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001176 return false;
1177 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001178 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001179 local_method->method_index_ = j;
1180 break;
1181 }
1182 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001183 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001184 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001185 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001186 local_method->method_index_ = actual_count;
1187 actual_count += 1;
1188 }
1189 }
1190 if (!IsUint(16, actual_count)) {
1191 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1192 return false;
1193 }
1194 CHECK_LE(actual_count, max_count);
1195 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001196 // TODO: do not assign to the vtable field until it is fully constructed.
1197 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001198 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001199 } else {
1200 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001201 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1202 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1203 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001204 LG << "Too many methods"; // TODO: VirtualMachineError
1205 return false;
1206 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001207 // TODO: do not assign to the vtable field until it is fully constructed.
1208 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1209 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001210 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001211 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1212 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001213 }
1214 return true;
1215}
1216
1217bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1218 int pool_offset = 0;
1219 int pool_size = 0;
1220 int miranda_count = 0;
1221 int miranda_alloc = 0;
1222 size_t super_ifcount;
1223 if (klass->HasSuperClass()) {
1224 super_ifcount = klass->GetSuperClass()->iftable_count_;
1225 } else {
1226 super_ifcount = 0;
1227 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001228 size_t ifcount = super_ifcount;
1229 ifcount += klass->NumInterfaces();
1230 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1231 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001232 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001233 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001234 DCHECK(klass->iftable_count_ == 0);
1235 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001236 return true;
1237 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001238 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1239 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001240 if (super_ifcount != 0) {
1241 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1242 sizeof(InterfaceEntry) * super_ifcount);
1243 }
1244 // Flatten the interface inheritance hierarchy.
1245 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001246 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1247 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001248 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001249 if (!interf->IsInterface()) {
1250 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1251 return false;
1252 }
1253 klass->iftable_[idx++].SetClass(interf);
1254 for (size_t j = 0; j < interf->iftable_count_; j++) {
1255 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1256 }
1257 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001258 CHECK_EQ(idx, ifcount);
1259 klass->iftable_count_ = ifcount;
1260 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001261 return true;
1262 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001263 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001264 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1265 }
1266 if (pool_size == 0) {
1267 return true;
1268 }
1269 klass->ifvi_pool_count_ = pool_size;
1270 klass->ifvi_pool_ = new uint32_t[pool_size];
1271 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001272 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001273 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1274 Class* interface = klass->iftable_[i].GetClass();
1275 pool_offset += interface->NumVirtualMethods(); // end here
1276 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1277 Method* interface_method = interface->GetVirtualMethod(j);
1278 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001279 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
1280 if (HasSameNameAndPrototype(interface_method, klass->vtable_->Get(k))) {
1281 if (!klass->vtable_->Get(k)->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001282 LG << "Implementation not public";
1283 return false;
1284 }
1285 klass->iftable_[i].method_index_array_[j] = k;
1286 break;
1287 }
1288 }
1289 if (k < 0) {
1290 if (miranda_count == miranda_alloc) {
1291 miranda_alloc += 8;
1292 if (miranda_list.empty()) {
1293 miranda_list.resize(miranda_alloc);
1294 } else {
1295 miranda_list.resize(miranda_alloc);
1296 }
1297 }
1298 int mir;
1299 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001300 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001301 break;
1302 }
1303 }
1304 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001305 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001306 if (mir == miranda_count) {
1307 miranda_list[miranda_count++] = interface_method;
1308 }
1309 }
1310 }
1311 }
1312 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001313 int old_method_count = klass->NumVirtualMethods();
1314 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001315 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001316
1317 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001318 int old_vtable_count = klass->vtable_->GetLength();
1319 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001320 // TODO: do not assign to the vtable field until it is fully constructed.
1321 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001322
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001323 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001324 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001325 memcpy(meth, miranda_list[i], sizeof(Method));
1326 meth->klass_ = klass;
1327 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001328 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1329 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001330 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001331 }
1332 }
1333 return true;
1334}
1335
1336void ClassLinker::LinkAbstractMethods(Class* klass) {
1337 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1338 Method* method = klass->GetVirtualMethod(i);
1339 if (method->IsAbstract()) {
1340 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1341 }
1342 }
1343}
1344
1345bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001346 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001347 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001348 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001349 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001350 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 }
1352 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001353 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001354 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001355 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001356 InstanceField* pField = klass->GetInstanceField(i);
1357 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001359 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1360 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001361 char rc = refField->GetType();
1362 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001363 klass->SetInstanceField(i, refField);
1364 klass->SetInstanceField(j, pField);
1365 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001366 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001367 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001368 break;
1369 }
1370 }
1371 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001372 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001373 }
1374 if (c != '[' && c != 'L') {
1375 break;
1376 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001377 pField->SetOffset(field_offset);
1378 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001379 }
1380
1381 // Now we want to pack all of the double-wide fields together. If
1382 // we're not aligned, though, we want to shuffle one 32-bit field
1383 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001384 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001385 InstanceField* pField = klass->GetInstanceField(i);
1386 char c = pField->GetType();
1387
1388 if (c != 'J' && c != 'D') {
1389 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001390 DCHECK(c != '[');
1391 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001392 pField->SetOffset(field_offset);
1393 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001394 i++;
1395 } else {
1396 // Next field is 64-bit, so search for a 32-bit field we can
1397 // swap into it.
1398 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001399 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1400 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001401 char rc = singleField->GetType();
1402 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001403 klass->SetInstanceField(i, singleField);
1404 klass->SetInstanceField(j, pField);
1405 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001406 pField->SetOffset(field_offset);
1407 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001408 found = true;
1409 i++;
1410 break;
1411 }
1412 }
1413 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001414 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001415 }
1416 }
1417 }
1418
1419 // Alignment is good, shuffle any double-wide fields forward, and
1420 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001421 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001422 for ( ; i < klass->NumInstanceFields(); i++) {
1423 InstanceField* pField = klass->GetInstanceField(i);
1424 char c = pField->GetType();
1425 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001426 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1427 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001428 char rc = doubleField->GetType();
1429 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001430 klass->SetInstanceField(i, doubleField);
1431 klass->SetInstanceField(j, pField);
1432 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001433 c = rc;
1434 break;
1435 }
1436 }
1437 } else {
1438 // This is a double-wide field, leave it be.
1439 }
1440
Brian Carlstroma0808032011-07-18 00:39:23 -07001441 pField->SetOffset(field_offset);
1442 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001443 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001444 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001445 }
1446
1447#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001448 // Make sure that all reference fields appear before
1449 // non-reference fields, and all double-wide fields are aligned.
1450 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001451 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001452 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001453 char c = pField->GetType();
1454
1455 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001456 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001457 }
1458
1459 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001460 if (!seen_non_ref) {
1461 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001462 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001463 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001464 } else {
1465 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001466 }
1467 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001468 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001469 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001470 }
1471#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001472 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001473 return true;
1474}
1475
1476// Set the bitmap of reference offsets, refOffsets, from the ifields
1477// list.
1478void ClassLinker::CreateReferenceOffsets(Class* klass) {
1479 uint32_t reference_offsets = 0;
1480 if (klass->HasSuperClass()) {
1481 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1482 }
1483 // If our superclass overflowed, we don't stand a chance.
1484 if (reference_offsets != CLASS_WALK_SUPER) {
1485 // All of the fields that contain object references are guaranteed
1486 // to be at the beginning of the ifields list.
1487 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1488 // Note that, per the comment on struct InstField, f->byteOffset
1489 // is the offset from the beginning of obj, not the offset into
1490 // obj->instanceData.
1491 const InstanceField* field = klass->GetInstanceField(i);
1492 size_t byte_offset = field->GetOffset();
1493 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001494 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001495 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1496 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001497 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001498 reference_offsets |= new_bit;
1499 } else {
1500 reference_offsets = CLASS_WALK_SUPER;
1501 break;
1502 }
1503 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001504 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001505 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001506}
1507
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001508Class* ClassLinker::ResolveClass(const Class* referrer,
1509 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001510 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001511 DexCache* dex_cache = referrer->GetDexCache();
1512 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001513 if (resolved != NULL) {
1514 return resolved;
1515 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001516 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001517 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001518 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001519 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001520 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001521 }
1522 if (resolved != NULL) {
1523 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001524 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001525 if (check->GetClassLoader() != NULL) {
1526 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1527 return NULL;
1528 }
1529 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001530 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001531 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001532 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001533 }
1534 return resolved;
1535}
1536
1537Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1538 /*MethodType*/ int method_type) {
1539 CHECK(false);
1540 return NULL;
1541}
1542
Carl Shapiro69759ea2011-07-21 18:13:35 -07001543String* ClassLinker::ResolveString(const Class* referring,
1544 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001545 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1546 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001547 int32_t utf16_length = dex_file->GetStringLength(string_id);
1548 const char* utf8_data = dex_file->GetStringData(string_id);
1549 String* new_string = AllocStringFromModifiedUtf8(utf16_length, utf8_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001550 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001551 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001552 return new_string;
1553}
1554
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001555} // namespace art