blob: 593b3e0cb0677776c9f1a871f05f4bf7ecaacb77 [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 Wilson14150742011-07-29 19:04:44 -040097 FindSystemClass("Ljava/lang/String;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070098 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
99 java_lang_String->object_size_ = sizeof(String);
100 class_roots_->Set(kJavaLangString, java_lang_String);
101
102 // Setup a single, global copy of "interfaces" and "iftable" for
103 // reuse across array classes
104 Class* java_lang_Cloneable = AllocClass();
105 CHECK(java_lang_Cloneable != NULL);
106 java_lang_Cloneable->descriptor_ = "Ljava/lang/Cloneable;";
107
108 Class* java_io_Serializable = AllocClass();
109 CHECK(java_io_Serializable != NULL);
110 java_io_Serializable->descriptor_ = "Ljava/io/Serializable;";
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700111
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700112 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700113 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700114 array_interfaces_->Set(0, java_lang_Cloneable);
115 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700116
117 // We assume that Cloneable/Serializable don't have superinterfaces --
118 // normally we'd have to crawl up and explicitly list all of the
119 // supers as well. These interfaces don't have any methods, so we
120 // don't have to worry about the ifviPool either.
121 array_iftable_ = new InterfaceEntry[2];
122 CHECK(array_iftable_ != NULL);
123 memset(array_iftable_, 0, sizeof(InterfaceEntry) * 2);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700124 array_iftable_[0].SetClass(array_interfaces_->Get(0));
125 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700126 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700127
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700128 // run Object[] through FindClass to complete initialization
129 Class* Object_array_class = FindSystemClass("[Ljava/lang/Object;");
130 CHECK_EQ(object_array_class, Object_array_class);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700131
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700132 // Setup the primitive type classes.
133 class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B"));
134 class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C"));
135 class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D"));
136 class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F"));
137 class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I"));
138 class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J"));
139 class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S"));
140 class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
141 class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V"));
142 // now we can use FindSystemClass for anything, including for "[C"
143
144 class_roots_->Set(kCharArrayClass, FindSystemClass("[C"));
145 // Now AllocString* can be used
146
147 // ensure all class_roots_ were initialized
148 for (size_t i = 0; i < kClassRootsMax; i++) {
149 CHECK(class_roots_->Get(i) != NULL);
150 }
151
152 // disable the slow paths in FindClass and CreatePrimitiveClass now
153 // that Object, Class, and Object[] are setup
154 init_done_ = true;
155}
156
Brian Carlstromb88e9442011-07-28 15:15:51 -0700157void ClassLinker::VisitRoots(RootVistor* root_visitor, void* arg) {
158 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700159
160 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700161 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700162 }
163
164 // TODO: acquire classes_lock_
165 typedef Table::const_iterator It; // TODO: C++0x auto
166 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700167 root_visitor(it->second, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700168 }
169 // TODO: release classes_lock_
170
Brian Carlstromb88e9442011-07-28 15:15:51 -0700171 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700172}
173
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700174DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700175 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700176}
177
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700178Class* ClassLinker::AllocClass(Class* java_lang_Class) {
179 return down_cast<Class*>(Object::Alloc(java_lang_Class));
180}
181
182Class* ClassLinker::AllocClass() {
183 return AllocClass(class_roots_->Get(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700184}
185
186StaticField* ClassLinker::AllocStaticField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700187 return down_cast<StaticField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700188 sizeof(StaticField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700189}
190
191InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700192 return down_cast<InstanceField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700193 sizeof(InstanceField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700194}
195
196Method* ClassLinker::AllocMethod() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700197 return down_cast<Method*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectMethod),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700198 sizeof(Method)));
Carl Shapiro565f5072011-07-10 13:39:43 -0700199}
200
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700201String* ClassLinker::AllocStringFromModifiedUtf8(int32_t utf16_length,
202 const char* utf8_data_in) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700203 return String::AllocFromModifiedUtf8(class_roots_->Get(kJavaLangString),
204 class_roots_->Get(kCharArrayClass),
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700205 utf16_length,
206 utf8_data_in);
207}
208
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700209Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700210 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700211 const DexFile* dex_file) {
Carl Shapirob5573532011-07-12 18:22:59 -0700212 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700213 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700214 CHECK(!self->IsExceptionPending());
215 // Find the class in the loaded classes table.
216 Class* klass = LookupClass(descriptor, class_loader);
217 if (klass == NULL) {
218 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700219 if (descriptor[0] == '[') {
Brian Carlstromf615a612011-07-23 12:50:34 -0700220 return CreateArrayClass(descriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700221 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700222 ClassPathEntry pair;
Brian Carlstromf615a612011-07-23 12:50:34 -0700223 if (dex_file == NULL) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700224 pair = FindInBootClassPath(descriptor);
225 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700226 pair.first = dex_file;
227 pair.second = dex_file->FindClassDef(descriptor);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700228 }
229 if (pair.second == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700230 LG << "Class " << descriptor << " not found"; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700231 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700233 const DexFile* dex_file = pair.first;
234 const DexFile::ClassDef* dex_class_def = pair.second;
235 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700237 if (!init_done_) {
238 // finish up init of hand crafted class_roots_
239 if (descriptor == "Ljava/lang/Object;") {
240 klass = class_roots_->Get(kJavaLangObject);
241 } else if (descriptor == "Ljava/lang/Class;") {
242 klass = class_roots_->Get(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400243 } else if (descriptor == "Ljava/lang/String;") {
244 klass = class_roots_->Get(kJavaLangString);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700245 } else {
246 klass = AllocClass();
247 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700248 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700249 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700250 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700251 klass->dex_cache_ = dex_cache;
Brian Carlstromf615a612011-07-23 12:50:34 -0700252 LoadClass(*dex_file, *dex_class_def, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700253 // Check for a pending exception during load
254 if (self->IsExceptionPending()) {
255 // TODO: free native allocations in klass
256 return NULL;
257 }
258 {
259 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700260 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700261 // Add the newly loaded class to the loaded classes table.
262 bool success = InsertClass(klass);
263 if (!success) {
264 // We may fail to insert if we raced with another thread.
265 klass->clinit_thread_id_ = 0;
266 // TODO: free native allocations in klass
267 klass = LookupClass(descriptor, class_loader);
268 CHECK(klass != NULL);
269 } else {
270 // Link the class.
Brian Carlstromf615a612011-07-23 12:50:34 -0700271 if (!LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700272 // Linking failed.
273 // TODO: CHECK(self->IsExceptionPending());
274 lock.NotifyAll();
275 return NULL;
276 }
277 }
278 }
279 }
280 // Link the class if it has not already been linked.
281 if (!klass->IsLinked() && !klass->IsErroneous()) {
282 ObjectLock lock(klass);
283 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700284 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700285 LG << "Recursive link"; // TODO: ClassCircularityError
286 return NULL;
287 }
288 // Wait for the pending initialization to complete.
289 while (!klass->IsLinked() && !klass->IsErroneous()) {
290 lock.Wait();
291 }
292 }
293 if (klass->IsErroneous()) {
294 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
295 return NULL;
296 }
297 // Return the loaded class. No exceptions should be pending.
298 CHECK(!self->IsExceptionPending());
299 return klass;
300}
301
Brian Carlstromf615a612011-07-23 12:50:34 -0700302void ClassLinker::LoadClass(const DexFile& dex_file,
303 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700304 Class* klass) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700305 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700306 CHECK(klass->dex_cache_ != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700307 const byte* class_data = dex_file.GetClassData(dex_class_def);
308 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700309
Brian Carlstromf615a612011-07-23 12:50:34 -0700310 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700311 CHECK(descriptor != NULL);
312
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700313 klass->klass_ = class_roots_->Get(kJavaLangClass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700314 klass->descriptor_.set(descriptor);
315 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700316 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700317 klass->class_loader_ = NULL; // TODO
318 klass->primitive_type_ = Class::kPrimNot;
319 klass->status_ = Class::kStatusIdx;
320
321 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700322 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700323
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700324 size_t num_static_fields = header.static_fields_size_;
325 size_t num_instance_fields = header.instance_fields_size_;
326 size_t num_direct_methods = header.direct_methods_size_;
327 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700328
Brian Carlstromf615a612011-07-23 12:50:34 -0700329 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700330
331 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700332 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700333
334 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700335 DCHECK(klass->sfields_ == NULL);
336 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700337 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700338 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700339 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700340 DexFile::Field dex_field;
341 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700342 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700343 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700344 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700345 }
346 }
347
348 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700349 DCHECK(klass->ifields_ == NULL);
350 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700351 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700352 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700353 uint32_t last_idx = 0;
354 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700355 DexFile::Field dex_field;
356 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700357 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700358 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700359 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700360 }
361 }
362
363 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700364 DCHECK(klass->direct_methods_ == NULL);
365 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700366 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700367 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700368 uint32_t last_idx = 0;
369 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700370 DexFile::Method dex_method;
371 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700372 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700373 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700374 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700375 // TODO: register maps
376 }
377 }
378
379 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700380 DCHECK(klass->virtual_methods_ == NULL);
381 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700382 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700383 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700384 uint32_t last_idx = 0;
385 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700386 DexFile::Method dex_method;
387 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700388 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700389 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700390 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700391 // TODO: register maps
392 }
393 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700394}
395
Brian Carlstromf615a612011-07-23 12:50:34 -0700396void ClassLinker::LoadInterfaces(const DexFile& dex_file,
397 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700398 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700399 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700400 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700401 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700402 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700403 DCHECK(klass->interfaces_idx_ == NULL);
404 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700405 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700406 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700407 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700408 }
409 }
410}
411
Brian Carlstromf615a612011-07-23 12:50:34 -0700412void ClassLinker::LoadField(const DexFile& dex_file,
413 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700414 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700415 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700416 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700417 dst->klass_ = klass;
Jesse Wilson14150742011-07-29 19:04:44 -0400418 dst->java_name_ = ResolveString(klass, field_id.name_idx_);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700419 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700420 dst->access_flags_ = src.access_flags_;
421}
422
Brian Carlstromf615a612011-07-23 12:50:34 -0700423void ClassLinker::LoadMethod(const DexFile& dex_file,
424 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700425 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700426 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700427 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700428 dst->klass_ = klass;
Brian Carlstromf615a612011-07-23 12:50:34 -0700429 dst->name_.set(dex_file.dexStringById(method_id.name_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700430 dst->proto_idx_ = method_id.proto_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700431 dst->shorty_.set(dex_file.GetShorty(method_id.proto_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700432 dst->access_flags_ = src.access_flags_;
433
434 // TODO: check for finalize method
435
Brian Carlstromf615a612011-07-23 12:50:34 -0700436 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700437 if (code_item != NULL) {
438 dst->num_registers_ = code_item->registers_size_;
439 dst->num_ins_ = code_item->ins_size_;
440 dst->num_outs_ = code_item->outs_size_;
441 dst->insns_ = code_item->insns_;
442 } else {
443 uint16_t num_args = dst->NumArgRegisters();
444 if (!dst->IsStatic()) {
445 ++num_args;
446 }
447 dst->num_registers_ = dst->num_ins_ + num_args;
448 // TODO: native methods
449 }
450}
451
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700452ClassLinker::ClassPathEntry ClassLinker::FindInBootClassPath(const StringPiece& descriptor) {
453 for (size_t i = 0; i != boot_class_path_.size(); ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700454 const DexFile* dex_file = boot_class_path_[i];
Brian Carlstromf615a612011-07-23 12:50:34 -0700455 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
456 if (dex_class_def != NULL) {
457 return ClassPathEntry(dex_file, dex_class_def);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700458 }
459 }
Brian Carlstroma0808032011-07-18 00:39:23 -0700460 return ClassPathEntry(NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700461}
462
Brian Carlstromf615a612011-07-23 12:50:34 -0700463void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700464 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700465 boot_class_path_.push_back(dex_file);
466 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700467}
468
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700469void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700470 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700471 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700472 DexCache* dex_cache = AllocDexCache();
473 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700474 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
475 AllocObjectArray<Class>(dex_file->NumTypeIds()),
476 AllocObjectArray<Method>(dex_file->NumMethodIds()),
477 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700478 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700479}
480
Brian Carlstromf615a612011-07-23 12:50:34 -0700481const DexFile* ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700482 CHECK(dex_cache != NULL);
483 for (size_t i = 0; i != dex_caches_.size(); ++i) {
484 if (dex_caches_[i] == dex_cache) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700485 return dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700486 }
487 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700488 CHECK(false) << "Could not find DexFile";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700489 return NULL;
490}
491
Brian Carlstromf615a612011-07-23 12:50:34 -0700492DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
493 CHECK(dex_file != NULL);
494 for (size_t i = 0; i != dex_files_.size(); ++i) {
495 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700496 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700497 }
498 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700499 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700500 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700501}
502
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700503Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700504 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700505 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700506 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700507 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
508 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700509 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700510 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700511 bool success = InsertClass(klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700512 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700513 return klass;
514}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700515
Brian Carlstrombe977852011-07-19 14:54:54 -0700516// Create an array class (i.e. the class object for the array, not the
517// array itself). "descriptor" looks like "[C" or "[[[[B" or
518// "[Ljava/lang/String;".
519//
520// If "descriptor" refers to an array of primitives, look up the
521// primitive type's internally-generated class object.
522//
523// "loader" is the class loader of the class that's referring to us. It's
524// used to ensure that we're looking for the element type in the right
525// context. It does NOT become the class loader for the array class; that
526// always comes from the base element class.
527//
528// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700529Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
530 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700531 const DexFile* dex_file)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700532{
533 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700534
Brian Carlstrombe977852011-07-19 14:54:54 -0700535 // Identify the underlying element class and the array dimension depth.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700536 Class* component_type_ = NULL;
537 int array_rank;
538 if (descriptor[1] == '[') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700539 // array of arrays; keep descriptor and grab stuff from parent
Brian Carlstromf615a612011-07-23 12:50:34 -0700540 Class* outer = FindClass(descriptor.substr(1), class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700541 if (outer != NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700542 // want the base class, not "outer", in our component_type_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700543 component_type_ = outer->component_type_;
544 array_rank = outer->array_rank_ + 1;
545 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700546 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700547 }
548 } else {
549 array_rank = 1;
550 if (descriptor[1] == 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700551 // array of objects; strip off "[" and look up descriptor.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700552 const StringPiece subDescriptor = descriptor.substr(1);
Brian Carlstromf615a612011-07-23 12:50:34 -0700553 component_type_ = FindClass(subDescriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700554 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700555 // array of a primitive type
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700556 component_type_ = FindPrimitiveClass(descriptor[1]);
557 }
558 }
559
560 if (component_type_ == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700561 // failed
Brian Carlstrom8ecd08c2011-07-27 17:50:51 -0700562 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700563 return NULL;
564 }
565
Brian Carlstrombe977852011-07-19 14:54:54 -0700566 // See if the component type is already loaded. Array classes are
567 // always associated with the class loader of their underlying
568 // element type -- an array of Strings goes with the loader for
569 // java/lang/String -- so we need to look for it there. (The
570 // caller should have checked for the existence of the class
571 // before calling here, but they did so with *their* class loader,
572 // not the component type's loader.)
573 //
574 // If we find it, the caller adds "loader" to the class' initiating
575 // loader list, which should prevent us from going through this again.
576 //
577 // This call is unnecessary if "loader" and "component_type_->class_loader_"
578 // are the same, because our caller (FindClass) just did the
579 // lookup. (Even if we get this wrong we still have correct behavior,
580 // because we effectively do this lookup again when we add the new
581 // class to the hash table --- necessary because of possible races with
582 // other threads.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700583 if (class_loader != component_type_->class_loader_) {
584 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
585 if (new_class != NULL) {
586 return new_class;
587 }
588 }
589
Brian Carlstrombe977852011-07-19 14:54:54 -0700590 // Fill out the fields in the Class.
591 //
592 // It is possible to execute some methods against arrays, because
593 // all arrays are subclasses of java_lang_Object_, so we need to set
594 // up a vtable. We can just point at the one in java_lang_Object_.
595 //
596 // Array classes are simple enough that we don't need to do a full
597 // link step.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700598
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700599 Class* new_class = NULL;
600 if (!init_done_ && descriptor == "[Ljava/lang/Object;") {
601 new_class = class_roots_->Get(kObjectArrayClass);
602 CHECK(new_class);
603 }
604 if (new_class == NULL) {
605 new_class = AllocClass();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700606 if (new_class == NULL) {
607 return NULL;
608 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700609 }
610 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
611 descriptor.size());
612 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
613 new_class->descriptor_alloc_->size());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700614 Class* java_lang_Object = class_roots_->Get(kJavaLangObject);
615 new_class->super_class_ = java_lang_Object;
616 new_class->vtable_ = java_lang_Object->vtable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700617 new_class->primitive_type_ = Class::kPrimNot;
618 new_class->component_type_ = component_type_;
619 new_class->class_loader_ = component_type_->class_loader_;
620 new_class->array_rank_ = array_rank;
621 new_class->status_ = Class::kStatusInitialized;
Brian Carlstrombe977852011-07-19 14:54:54 -0700622 // don't need to set new_class->object_size_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700623
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700624
Brian Carlstrombe977852011-07-19 14:54:54 -0700625 // All arrays have java/lang/Cloneable and java/io/Serializable as
626 // interfaces. We need to set that up here, so that stuff like
627 // "instanceof" works right.
628 //
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700629 // Note: The GC could run during the call to FindSystemClass,
Brian Carlstrombe977852011-07-19 14:54:54 -0700630 // so we need to make sure the class object is GC-valid while we're in
631 // there. Do this by clearing the interface list so the GC will just
632 // think that the entries are null.
Brian Carlstrombe977852011-07-19 14:54:54 -0700633
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700634
635 // Use the single, global copies of "interfaces" and "iftable"
636 // (remember not to free them for arrays).
637 DCHECK(array_interfaces_ != NULL);
638 new_class->interfaces_ = array_interfaces_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700639 new_class->iftable_count_ = 2;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700640 DCHECK(array_iftable_ != NULL);
641 new_class->iftable_ = array_iftable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700642
Brian Carlstrombe977852011-07-19 14:54:54 -0700643 // Inherit access flags from the component type. Arrays can't be
644 // used as a superclass or interface, so we want to add "final"
645 // and remove "interface".
646 //
647 // Don't inherit any non-standard flags (e.g., kAccFinal)
648 // from component_type_. We assume that the array class does not
649 // override finalize().
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700650 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
651 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
652
653 if (InsertClass(new_class)) {
654 return new_class;
655 }
Brian Carlstrombe977852011-07-19 14:54:54 -0700656 // Another thread must have loaded the class after we
657 // started but before we finished. Abandon what we've
658 // done.
659 //
660 // (Yes, this happens.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700661
Brian Carlstrombe977852011-07-19 14:54:54 -0700662 // Grab the winning class.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700663 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
664 DCHECK(other_class != NULL);
665 return other_class;
666}
667
668Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700669 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700670 case 'B':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700671 DCHECK(class_roots_->Get(kPrimitiveByte) != NULL);
672 return class_roots_->Get(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700673 case 'C':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700674 DCHECK(class_roots_->Get(kPrimitiveChar) != NULL);
675 return class_roots_->Get(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700676 case 'D':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700677 DCHECK(class_roots_->Get(kPrimitiveDouble) != NULL);
678 return class_roots_->Get(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700679 case 'F':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700680 DCHECK(class_roots_->Get(kPrimitiveFloat) != NULL);
681 return class_roots_->Get(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700682 case 'I':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700683 DCHECK(class_roots_->Get(kPrimitiveInt) != NULL);
684 return class_roots_->Get(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700685 case 'J':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700686 DCHECK(class_roots_->Get(kPrimitiveLong) != NULL);
687 return class_roots_->Get(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700688 case 'S':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700689 DCHECK(class_roots_->Get(kPrimitiveShort) != NULL);
690 return class_roots_->Get(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700691 case 'Z':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700692 DCHECK(class_roots_->Get(kPrimitiveBoolean) != NULL);
693 return class_roots_->Get(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700694 case 'V':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700695 DCHECK(class_roots_->Get(kPrimitiveVoid) != NULL);
696 return class_roots_->Get(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700697 case 'L':
698 case '[':
699 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700700 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700701 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700702 };
703 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700704}
705
706bool ClassLinker::InsertClass(Class* klass) {
707 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700708 const StringPiece& key = klass->GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700709 bool success = classes_.insert(std::make_pair(key, klass)).second;
710 // TODO: release classes_lock_
711 return success;
712}
713
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700714Class* ClassLinker::LookupClass(const StringPiece& descriptor, Object* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700715 // TODO: acquire classes_lock_
716 Table::iterator it = classes_.find(descriptor);
717 // TODO: release classes_lock_
718 if (it == classes_.end()) {
719 return NULL;
720 } else {
721 return (*it).second;
722 }
723}
724
725bool ClassLinker::InitializeClass(Class* klass) {
726 CHECK(klass->GetStatus() == Class::kStatusResolved ||
727 klass->GetStatus() == Class::kStatusError);
728
Carl Shapirob5573532011-07-12 18:22:59 -0700729 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700730
731 {
732 ObjectLock lock(klass);
733
734 if (klass->GetStatus() < Class::kStatusVerified) {
735 if (klass->IsErroneous()) {
736 LG << "re-initializing failed class"; // TODO: throw
737 return false;
738 }
739
740 CHECK(klass->GetStatus() == Class::kStatusResolved);
741
742 klass->status_ = Class::kStatusVerifying;
743 if (!DexVerify::VerifyClass(klass)) {
744 LG << "Verification failed"; // TODO: ThrowVerifyError
745 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700746 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
747 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700748 klass->SetStatus(Class::kStatusError);
749 return false;
750 }
751
752 klass->SetStatus(Class::kStatusVerified);
753 }
754
755 if (klass->status_ == Class::kStatusInitialized) {
756 return true;
757 }
758
759 while (klass->status_ == Class::kStatusInitializing) {
760 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700761 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700762 LG << "recursive <clinit>";
763 return true;
764 }
765
766 CHECK(!self->IsExceptionPending());
767
768 lock.Wait(); // TODO: check for interruption
769
770 // When we wake up, repeat the test for init-in-progress. If
771 // there's an exception pending (only possible if
772 // "interruptShouldThrow" was set), bail out.
773 if (self->IsExceptionPending()) {
774 CHECK(false);
775 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
776 klass->SetStatus(Class::kStatusError);
777 return false;
778 }
779 if (klass->GetStatus() == Class::kStatusInitializing) {
780 continue;
781 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700782 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700783 klass->GetStatus() == Class::kStatusError);
784 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700785 // The caller wants an exception, but it was thrown in a
786 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700787 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
788 return false;
789 }
790 return true; // otherwise, initialized
791 }
792
793 // see if we failed previously
794 if (klass->IsErroneous()) {
795 // might be wise to unlock before throwing; depends on which class
796 // it is that we have locked
797
798 // TODO: throwEarlierClassFailure(klass);
799 return false;
800 }
801
802 if (!ValidateSuperClassDescriptors(klass)) {
803 klass->SetStatus(Class::kStatusError);
804 return false;
805 }
806
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700807 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700808
Carl Shapirob5573532011-07-12 18:22:59 -0700809 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700810 klass->status_ = Class::kStatusInitializing;
811 }
812
813 if (!InitializeSuperClass(klass)) {
814 return false;
815 }
816
817 InitializeStaticFields(klass);
818
819 Method* clinit = klass->FindDirectMethodLocally("<clinit>", "()V");
820 if (clinit != NULL) {
821 } else {
822 // JValue unused;
823 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700824 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700825 }
826
827 {
828 ObjectLock lock(klass);
829
830 if (self->IsExceptionPending()) {
831 klass->SetStatus(Class::kStatusError);
832 } else {
833 klass->SetStatus(Class::kStatusInitialized);
834 }
835 lock.NotifyAll();
836 }
837
838 return true;
839}
840
841bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
842 if (klass->IsInterface()) {
843 return true;
844 }
845 // begin with the methods local to the superclass
846 if (klass->HasSuperClass() &&
847 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
848 const Class* super = klass->GetSuperClass();
849 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
850 const Method* method = klass->GetVirtualMethod(i);
851 if (method != super->GetVirtualMethod(i) &&
852 !HasSameMethodDescriptorClasses(method, super, klass)) {
853 LG << "Classes resolve differently in superclass";
854 return false;
855 }
856 }
857 }
858 for (size_t i = 0; i < klass->iftable_count_; ++i) {
859 const InterfaceEntry* iftable = &klass->iftable_[i];
860 Class* interface = iftable->GetClass();
861 if (klass->GetClassLoader() != interface->GetClassLoader()) {
862 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
863 uint32_t vtable_index = iftable->method_index_array_[j];
864 const Method* method = klass->GetVirtualMethod(vtable_index);
865 if (!HasSameMethodDescriptorClasses(method, interface,
866 method->GetClass())) {
867 LG << "Classes resolve differently in interface"; // TODO: LinkageError
868 return false;
869 }
870 }
871 }
872 }
873 return true;
874}
875
876bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700877 const Class* klass1,
878 const Class* klass2) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700879 const DexFile* dex_file = FindDexFile(method->GetClass()->GetDexCache());
880 const DexFile::ProtoId& proto_id = dex_file->GetProtoId(method->proto_idx_);
881 DexFile::ParameterIterator *it;
882 for (it = dex_file->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700883 const char* descriptor = it->GetDescriptor();
884 if (descriptor == NULL) {
885 break;
886 }
887 if (descriptor[0] == 'L' || descriptor[0] == '[') {
888 // Found a non-primitive type.
889 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
890 return false;
891 }
892 }
893 }
894 // Check the return type
Brian Carlstromf615a612011-07-23 12:50:34 -0700895 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700896 if (descriptor[0] == 'L' || descriptor[0] == '[') {
897 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
898 return false;
899 }
900 }
901 return true;
902}
903
904// Returns true if classes referenced by the descriptor are the
905// same classes in klass1 as they are in klass2.
906bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700907 const Class* klass1,
908 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700909 CHECK(descriptor != NULL);
910 CHECK(klass1 != NULL);
911 CHECK(klass2 != NULL);
912#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700913 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700914 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700915 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700916 // TODO: found2 == NULL
917 // TODO: lookup found1 in initiating loader list
918 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700919 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700920 if (found1 == found2) {
921 return true;
922 } else {
923 return false;
924 }
925 }
926#endif
927 return true;
928}
929
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700930bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700931 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
932 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
933 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
934 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700935
936 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstromf615a612011-07-23 12:50:34 -0700937 const DexFile::TypeList* type_list1 = dex1->GetProtoParameters(proto1);
938 const DexFile::TypeList* type_list2 = dex2->GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700939 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
940 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
941 if (arity1 != arity2) {
942 return false;
943 }
944
945 for (size_t i = 0; i < arity1; ++i) {
946 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
947 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700948 const char* type1 = dex1->dexStringByTypeIdx(type_idx1);
949 const char* type2 = dex2->dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700950 if (strcmp(type1, type2) != 0) {
951 return false;
952 }
953 }
954
955 return true;
956}
957
958bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700959 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
960 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
961 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
962 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
963 const char* type1 = dex1->dexStringByTypeIdx(proto1.return_type_idx_);
964 const char* type2 = dex2->dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700965 return (strcmp(type1, type2) == 0);
966}
967
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700968bool ClassLinker::InitializeSuperClass(Class* klass) {
969 CHECK(klass != NULL);
970 // TODO: assert klass lock is acquired
971 if (!klass->IsInterface() && klass->HasSuperClass()) {
972 Class* super_class = klass->GetSuperClass();
973 if (super_class->GetStatus() != Class::kStatusInitialized) {
974 CHECK(!super_class->IsInterface());
975 klass->MonitorExit();
976 bool super_initialized = InitializeClass(super_class);
977 klass->MonitorEnter();
978 // TODO: check for a pending exception
979 if (!super_initialized) {
980 klass->SetStatus(Class::kStatusError);
981 klass->NotifyAll();
982 return false;
983 }
984 }
985 }
986 return true;
987}
988
989void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700990 size_t num_static_fields = klass->NumStaticFields();
991 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700992 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700993 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700994 DexCache* dex_cache = klass->GetDexCache();
995 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700996 return;
997 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700998 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstromf615a612011-07-23 12:50:34 -0700999 const DexFile* dex_file = FindDexFile(dex_cache);
1000 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
1001 CHECK(dex_class_def != NULL);
1002 const byte* addr = dex_file->GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001003 size_t array_size = DecodeUnsignedLeb128(&addr);
1004 for (size_t i = 0; i < array_size; ++i) {
1005 StaticField* field = klass->GetStaticField(i);
1006 JValue value;
Brian Carlstromf615a612011-07-23 12:50:34 -07001007 DexFile::ValueType type = dex_file->ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001008 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001009 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001010 field->SetByte(value.b);
1011 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001012 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001013 field->SetShort(value.s);
1014 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001015 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001016 field->SetChar(value.c);
1017 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001018 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001019 field->SetInt(value.i);
1020 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001021 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001022 field->SetLong(value.j);
1023 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001024 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001025 field->SetFloat(value.f);
1026 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001027 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001028 field->SetDouble(value.d);
1029 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001030 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001031 uint32_t string_idx = value.i;
1032 String* resolved = ResolveString(klass, string_idx);
1033 field->SetObject(resolved);
1034 break;
1035 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001036 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001037 field->SetBoolean(value.z);
1038 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001039 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001040 field->SetObject(value.l);
1041 break;
1042 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001043 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001044 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001045 }
1046}
1047
Brian Carlstromf615a612011-07-23 12:50:34 -07001048bool ClassLinker::LinkClass(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001049 CHECK(klass->status_ == Class::kStatusIdx ||
1050 klass->status_ == Class::kStatusLoaded);
1051 if (klass->status_ == Class::kStatusIdx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001052 if (!LinkInterfaces(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001053 return false;
1054 }
1055 }
1056 if (!LinkSuperClass(klass)) {
1057 return false;
1058 }
1059 if (!LinkMethods(klass)) {
1060 return false;
1061 }
1062 if (!LinkInstanceFields(klass)) {
1063 return false;
1064 }
1065 CreateReferenceOffsets(klass);
1066 CHECK_EQ(klass->status_, Class::kStatusLoaded);
1067 klass->status_ = Class::kStatusResolved;
1068 return true;
1069}
1070
Brian Carlstromf615a612011-07-23 12:50:34 -07001071bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001072 // Mark the class as loaded.
1073 klass->status_ = Class::kStatusLoaded;
Brian Carlstromf615a612011-07-23 12:50:34 -07001074 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1075 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001076 if (super_class == NULL) {
1077 LG << "Failed to resolve superclass";
1078 return false;
1079 }
1080 klass->super_class_ = super_class; // TODO: write barrier
1081 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001082 if (klass->NumInterfaces() > 0) {
1083 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1084 uint32_t idx = klass->interfaces_idx_[i];
1085 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1086 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087 LG << "Failed to resolve interface";
1088 return false;
1089 }
1090 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001091 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001092 LG << "Inaccessible interface";
1093 return false;
1094 }
1095 }
1096 }
1097 return true;
1098}
1099
1100bool ClassLinker::LinkSuperClass(Class* klass) {
1101 CHECK(!klass->IsPrimitive());
1102 const Class* super = klass->GetSuperClass();
1103 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1104 if (super != NULL) {
1105 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1106 return false;
1107 }
1108 // TODO: clear finalize attribute
1109 return true;
1110 }
1111 if (super == NULL) {
1112 LG << "No superclass defined"; // TODO: LinkageError
1113 return false;
1114 }
1115 // Verify
1116 if (super->IsFinal()) {
1117 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
1118 return false;
1119 }
1120 if (super->IsInterface()) {
1121 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
1122 return false;
1123 }
1124 if (!klass->CanAccess(super)) {
1125 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
1126 return false;
1127 }
1128 return true;
1129}
1130
1131// Populate the class vtable and itable.
1132bool ClassLinker::LinkMethods(Class* klass) {
1133 if (klass->IsInterface()) {
1134 // No vtable.
1135 size_t count = klass->NumVirtualMethods();
1136 if (!IsUint(16, count)) {
1137 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1138 return false;
1139 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001140 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001141 klass->GetVirtualMethod(i)->method_index_ = i;
1142 }
1143 } else {
1144 // Link virtual method tables
1145 LinkVirtualMethods(klass);
1146
1147 // Link interface method tables
1148 LinkInterfaceMethods(klass);
1149
1150 // Insert stubs.
1151 LinkAbstractMethods(klass);
1152 }
1153 return true;
1154}
1155
1156bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001157 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001158 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001159 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1160 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001161 // TODO: do not assign to the vtable field until it is fully constructed.
1162 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001163 // See if any of our virtual methods override the superclass.
1164 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1165 Method* local_method = klass->GetVirtualMethod(i);
1166 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001167 for (; j < actual_count; ++j) {
1168 Method* super_method = klass->vtable_->Get(j);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001169 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001170 // Verify
1171 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001172 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001173 return false;
1174 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001175 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001176 local_method->method_index_ = j;
1177 break;
1178 }
1179 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001180 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001181 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001182 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001183 local_method->method_index_ = actual_count;
1184 actual_count += 1;
1185 }
1186 }
1187 if (!IsUint(16, actual_count)) {
1188 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1189 return false;
1190 }
1191 CHECK_LE(actual_count, max_count);
1192 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001193 // TODO: do not assign to the vtable field until it is fully constructed.
1194 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001195 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001196 } else {
1197 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001198 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1199 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1200 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201 LG << "Too many methods"; // TODO: VirtualMachineError
1202 return false;
1203 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001204 // TODO: do not assign to the vtable field until it is fully constructed.
1205 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1206 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001207 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001208 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1209 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001210 }
1211 return true;
1212}
1213
1214bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1215 int pool_offset = 0;
1216 int pool_size = 0;
1217 int miranda_count = 0;
1218 int miranda_alloc = 0;
1219 size_t super_ifcount;
1220 if (klass->HasSuperClass()) {
1221 super_ifcount = klass->GetSuperClass()->iftable_count_;
1222 } else {
1223 super_ifcount = 0;
1224 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001225 size_t ifcount = super_ifcount;
1226 ifcount += klass->NumInterfaces();
1227 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1228 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001229 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001230 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001231 DCHECK(klass->iftable_count_ == 0);
1232 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001233 return true;
1234 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001235 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1236 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001237 if (super_ifcount != 0) {
1238 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1239 sizeof(InterfaceEntry) * super_ifcount);
1240 }
1241 // Flatten the interface inheritance hierarchy.
1242 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001243 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1244 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001245 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001246 if (!interf->IsInterface()) {
1247 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1248 return false;
1249 }
1250 klass->iftable_[idx++].SetClass(interf);
1251 for (size_t j = 0; j < interf->iftable_count_; j++) {
1252 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1253 }
1254 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001255 CHECK_EQ(idx, ifcount);
1256 klass->iftable_count_ = ifcount;
1257 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001258 return true;
1259 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001260 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001261 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1262 }
1263 if (pool_size == 0) {
1264 return true;
1265 }
1266 klass->ifvi_pool_count_ = pool_size;
1267 klass->ifvi_pool_ = new uint32_t[pool_size];
1268 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001269 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001270 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1271 Class* interface = klass->iftable_[i].GetClass();
1272 pool_offset += interface->NumVirtualMethods(); // end here
1273 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1274 Method* interface_method = interface->GetVirtualMethod(j);
1275 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001276 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
1277 if (HasSameNameAndPrototype(interface_method, klass->vtable_->Get(k))) {
1278 if (!klass->vtable_->Get(k)->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001279 LG << "Implementation not public";
1280 return false;
1281 }
1282 klass->iftable_[i].method_index_array_[j] = k;
1283 break;
1284 }
1285 }
1286 if (k < 0) {
1287 if (miranda_count == miranda_alloc) {
1288 miranda_alloc += 8;
1289 if (miranda_list.empty()) {
1290 miranda_list.resize(miranda_alloc);
1291 } else {
1292 miranda_list.resize(miranda_alloc);
1293 }
1294 }
1295 int mir;
1296 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001297 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001298 break;
1299 }
1300 }
1301 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001302 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001303 if (mir == miranda_count) {
1304 miranda_list[miranda_count++] = interface_method;
1305 }
1306 }
1307 }
1308 }
1309 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001310 int old_method_count = klass->NumVirtualMethods();
1311 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001312 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001313
1314 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001315 int old_vtable_count = klass->vtable_->GetLength();
1316 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001317 // TODO: do not assign to the vtable field until it is fully constructed.
1318 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001319
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001320 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001321 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001322 memcpy(meth, miranda_list[i], sizeof(Method));
1323 meth->klass_ = klass;
1324 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001325 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1326 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001327 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001328 }
1329 }
1330 return true;
1331}
1332
1333void ClassLinker::LinkAbstractMethods(Class* klass) {
1334 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1335 Method* method = klass->GetVirtualMethod(i);
1336 if (method->IsAbstract()) {
1337 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1338 }
1339 }
1340}
1341
1342bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001343 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001344 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001345 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001346 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001347 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001348 }
1349 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001350 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001352 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001353 InstanceField* pField = klass->GetInstanceField(i);
1354 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001355 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001356 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1357 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 char rc = refField->GetType();
1359 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001360 klass->SetInstanceField(i, refField);
1361 klass->SetInstanceField(j, pField);
1362 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001363 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001364 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001365 break;
1366 }
1367 }
1368 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001369 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001370 }
1371 if (c != '[' && c != 'L') {
1372 break;
1373 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001374 pField->SetOffset(field_offset);
1375 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001376 }
1377
1378 // Now we want to pack all of the double-wide fields together. If
1379 // we're not aligned, though, we want to shuffle one 32-bit field
1380 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001381 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001382 InstanceField* pField = klass->GetInstanceField(i);
1383 char c = pField->GetType();
1384
1385 if (c != 'J' && c != 'D') {
1386 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001387 DCHECK(c != '[');
1388 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001389 pField->SetOffset(field_offset);
1390 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001391 i++;
1392 } else {
1393 // Next field is 64-bit, so search for a 32-bit field we can
1394 // swap into it.
1395 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001396 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1397 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001398 char rc = singleField->GetType();
1399 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001400 klass->SetInstanceField(i, singleField);
1401 klass->SetInstanceField(j, pField);
1402 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001403 pField->SetOffset(field_offset);
1404 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001405 found = true;
1406 i++;
1407 break;
1408 }
1409 }
1410 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001411 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001412 }
1413 }
1414 }
1415
1416 // Alignment is good, shuffle any double-wide fields forward, and
1417 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001418 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001419 for ( ; i < klass->NumInstanceFields(); i++) {
1420 InstanceField* pField = klass->GetInstanceField(i);
1421 char c = pField->GetType();
1422 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001423 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1424 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001425 char rc = doubleField->GetType();
1426 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001427 klass->SetInstanceField(i, doubleField);
1428 klass->SetInstanceField(j, pField);
1429 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001430 c = rc;
1431 break;
1432 }
1433 }
1434 } else {
1435 // This is a double-wide field, leave it be.
1436 }
1437
Brian Carlstroma0808032011-07-18 00:39:23 -07001438 pField->SetOffset(field_offset);
1439 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001440 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001441 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001442 }
1443
1444#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001445 // Make sure that all reference fields appear before
1446 // non-reference fields, and all double-wide fields are aligned.
1447 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001448 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001449 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001450 char c = pField->GetType();
1451
1452 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001453 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001454 }
1455
1456 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001457 if (!seen_non_ref) {
1458 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001459 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001460 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001461 } else {
1462 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001463 }
1464 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001465 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001466 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001467 }
1468#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001469 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001470 return true;
1471}
1472
1473// Set the bitmap of reference offsets, refOffsets, from the ifields
1474// list.
1475void ClassLinker::CreateReferenceOffsets(Class* klass) {
1476 uint32_t reference_offsets = 0;
1477 if (klass->HasSuperClass()) {
1478 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1479 }
1480 // If our superclass overflowed, we don't stand a chance.
1481 if (reference_offsets != CLASS_WALK_SUPER) {
1482 // All of the fields that contain object references are guaranteed
1483 // to be at the beginning of the ifields list.
1484 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1485 // Note that, per the comment on struct InstField, f->byteOffset
1486 // is the offset from the beginning of obj, not the offset into
1487 // obj->instanceData.
1488 const InstanceField* field = klass->GetInstanceField(i);
1489 size_t byte_offset = field->GetOffset();
1490 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001491 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001492 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1493 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001494 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001495 reference_offsets |= new_bit;
1496 } else {
1497 reference_offsets = CLASS_WALK_SUPER;
1498 break;
1499 }
1500 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001501 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001502 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001503}
1504
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001505Class* ClassLinker::ResolveClass(const Class* referrer,
1506 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001507 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001508 DexCache* dex_cache = referrer->GetDexCache();
1509 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001510 if (resolved != NULL) {
1511 return resolved;
1512 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001513 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001514 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001515 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001516 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001517 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001518 }
1519 if (resolved != NULL) {
1520 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001521 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001522 if (check->GetClassLoader() != NULL) {
1523 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1524 return NULL;
1525 }
1526 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001527 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001528 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001529 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001530 }
1531 return resolved;
1532}
1533
1534Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1535 /*MethodType*/ int method_type) {
1536 CHECK(false);
1537 return NULL;
1538}
1539
Carl Shapiro69759ea2011-07-21 18:13:35 -07001540String* ClassLinker::ResolveString(const Class* referring,
1541 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001542 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1543 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001544 int32_t utf16_length = dex_file->GetStringLength(string_id);
1545 const char* utf8_data = dex_file->GetStringData(string_id);
1546 String* new_string = AllocStringFromModifiedUtf8(utf16_length, utf8_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001547 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001548 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001549 return new_string;
1550}
1551
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001552} // namespace art