blob: d3d1a965f1e462ae99b094e956a56005681af8af [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: cshapiro@google.com (Carl Shapiro)
3
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07004#include "class_linker.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005
6#include <vector>
7#include <utility>
8
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "casts.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070010#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "dex_verifier.h"
12#include "heap.h"
13#include "logging.h"
14#include "monitor.h"
15#include "object.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070016#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "scoped_ptr.h"
18#include "thread.h"
19#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
22
Carl Shapiro2ed144c2011-07-26 16:52:08 -070023ClassLinker* ClassLinker::Create(const std::vector<DexFile*>& boot_class_path) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070024 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025 class_linker->Init(boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070026 // TODO: check for failure during initialization
27 return class_linker.release();
28}
29
Carl Shapiro2ed144c2011-07-26 16:52:08 -070030void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070031 init_done_ = false;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070033 // java_lang_Class comes first, its needed for AllocClass
34 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(Class)));
35 CHECK(java_lang_Class != NULL);
36 java_lang_Class->descriptor_ = "Ljava/lang/Class;";
37 java_lang_Class->object_size_ = sizeof(Class);
38 java_lang_Class->klass_ = java_lang_Class;
39 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070040
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070041 // java_lang_Object comes next so that object_array_class can be created
42 Class* java_lang_Object = AllocClass(java_lang_Class);
43 CHECK(java_lang_Object != NULL);
44 java_lang_Object->descriptor_ = "Ljava/lang/Object;";
45 // backfill Object as the super class of Class
46 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstroma0808032011-07-18 00:39:23 -070047
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070048 // object_array_class is for root_classes to provide the storage for these classes
49 Class* object_array_class = AllocClass(java_lang_Class);
50 CHECK(object_array_class != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -070051
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070052 // create storage for root classes, save away our work so far
53 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
54 class_roots_->Set(kJavaLangClass, java_lang_Class);
55 class_roots_->Set(kJavaLangObject, java_lang_Object);
56 class_roots_->Set(kObjectArrayClass, object_array_class);
57 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -070058
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070059 // setup boot_class_path_ now that we can use AllocObjectArray to
60 // DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -070061 for (size_t i = 0; i != boot_class_path.size(); ++i) {
62 AppendToBootClassPath(boot_class_path[i]);
63 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070064 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -070065
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070066 // run Object and Class to setup their dex_cache_ fields and register them in classes_.
67 // we also override their object_size_ values to accommodate the extra C++ fields.
68 Class* Object_class = FindSystemClass(java_lang_Object->GetDescriptor());
69 CHECK_EQ(java_lang_Object, Object_class);
70 CHECK_LE(java_lang_Object->object_size_, sizeof(Object));
71 java_lang_Object->object_size_ = sizeof(Object);
72 Class* Class_class = FindSystemClass(java_lang_Class->GetDescriptor());
73 CHECK_EQ(java_lang_Class, Class_class);
74 CHECK_LE(java_lang_Class->object_size_, sizeof(Class));
75 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom913af1b2011-07-23 21:41:13 -070076
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070077 // set special sizes for these C++ extended classes (Field, Method, String).
78 // we also remember them in class_roots_ to construct them within ClassLinker
79 Class* java_lang_reflect_Field = FindSystemClass("Ljava/lang/reflect/Field;");
80 CHECK(java_lang_reflect_Field != NULL);
81 CHECK_LE(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField)));
82 java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField));
83 class_roots_->Set(kJavaLangReflectField, java_lang_reflect_Field);
84
85 Class* java_lang_reflect_Method = FindSystemClass("Ljava/lang/reflect/Method;");
86 CHECK(java_lang_reflect_Method != NULL);
87 CHECK_LE(java_lang_reflect_Method->object_size_, sizeof(Method));
88 java_lang_reflect_Method->object_size_ = sizeof(Method);
89 class_roots_->Set(kJavaLangReflectMethod, java_lang_reflect_Method);
90
91 Class* java_lang_String = FindSystemClass("Ljava/lang/String;");
92 CHECK(java_lang_String != NULL);
93 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
94 java_lang_String->object_size_ = sizeof(String);
95 class_roots_->Set(kJavaLangString, java_lang_String);
96
97 // Setup a single, global copy of "interfaces" and "iftable" for
98 // reuse across array classes
99 Class* java_lang_Cloneable = AllocClass();
100 CHECK(java_lang_Cloneable != NULL);
101 java_lang_Cloneable->descriptor_ = "Ljava/lang/Cloneable;";
102
103 Class* java_io_Serializable = AllocClass();
104 CHECK(java_io_Serializable != NULL);
105 java_io_Serializable->descriptor_ = "Ljava/io/Serializable;";
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700106
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700107 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700108 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700109 array_interfaces_->Set(0, java_lang_Cloneable);
110 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700111
112 // We assume that Cloneable/Serializable don't have superinterfaces --
113 // normally we'd have to crawl up and explicitly list all of the
114 // supers as well. These interfaces don't have any methods, so we
115 // don't have to worry about the ifviPool either.
116 array_iftable_ = new InterfaceEntry[2];
117 CHECK(array_iftable_ != NULL);
118 memset(array_iftable_, 0, sizeof(InterfaceEntry) * 2);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700119 array_iftable_[0].SetClass(array_interfaces_->Get(0));
120 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700121 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700122
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700123 // run Object[] through FindClass to complete initialization
124 Class* Object_array_class = FindSystemClass("[Ljava/lang/Object;");
125 CHECK_EQ(object_array_class, Object_array_class);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700126
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700127 // Setup the primitive type classes.
128 class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B"));
129 class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C"));
130 class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D"));
131 class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F"));
132 class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I"));
133 class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J"));
134 class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S"));
135 class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
136 class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V"));
137 // now we can use FindSystemClass for anything, including for "[C"
138
139 class_roots_->Set(kCharArrayClass, FindSystemClass("[C"));
140 // Now AllocString* can be used
141
142 // ensure all class_roots_ were initialized
143 for (size_t i = 0; i < kClassRootsMax; i++) {
144 CHECK(class_roots_->Get(i) != NULL);
145 }
146
147 // disable the slow paths in FindClass and CreatePrimitiveClass now
148 // that Object, Class, and Object[] are setup
149 init_done_ = true;
150}
151
Brian Carlstromb88e9442011-07-28 15:15:51 -0700152void ClassLinker::VisitRoots(RootVistor* root_visitor, void* arg) {
153 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700154
155 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700156 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700157 }
158
159 // TODO: acquire classes_lock_
160 typedef Table::const_iterator It; // TODO: C++0x auto
161 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700162 root_visitor(it->second, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700163 }
164 // TODO: release classes_lock_
165
Brian Carlstromb88e9442011-07-28 15:15:51 -0700166 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700167}
168
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700169DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700170 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700171}
172
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700173Class* ClassLinker::AllocClass(Class* java_lang_Class) {
174 return down_cast<Class*>(Object::Alloc(java_lang_Class));
175}
176
177Class* ClassLinker::AllocClass() {
178 return AllocClass(class_roots_->Get(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700179}
180
181StaticField* ClassLinker::AllocStaticField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700182 return down_cast<StaticField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700183 sizeof(StaticField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700184}
185
186InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700187 return down_cast<InstanceField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700188 sizeof(InstanceField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700189}
190
191Method* ClassLinker::AllocMethod() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700192 return down_cast<Method*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectMethod),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700193 sizeof(Method)));
Carl Shapiro565f5072011-07-10 13:39:43 -0700194}
195
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700196String* ClassLinker::AllocStringFromModifiedUtf8(int32_t utf16_length,
197 const char* utf8_data_in) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700198 return String::AllocFromModifiedUtf8(class_roots_->Get(kJavaLangString),
199 class_roots_->Get(kCharArrayClass),
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700200 utf16_length,
201 utf8_data_in);
202}
203
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700204Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700205 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700206 const DexFile* dex_file) {
Carl Shapirob5573532011-07-12 18:22:59 -0700207 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700208 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700209 CHECK(!self->IsExceptionPending());
210 // Find the class in the loaded classes table.
211 Class* klass = LookupClass(descriptor, class_loader);
212 if (klass == NULL) {
213 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700214 if (descriptor[0] == '[') {
Brian Carlstromf615a612011-07-23 12:50:34 -0700215 return CreateArrayClass(descriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700216 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700217 ClassPathEntry pair;
Brian Carlstromf615a612011-07-23 12:50:34 -0700218 if (dex_file == NULL) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700219 pair = FindInBootClassPath(descriptor);
220 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700221 pair.first = dex_file;
222 pair.second = dex_file->FindClassDef(descriptor);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700223 }
224 if (pair.second == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700225 LG << "Class " << descriptor << " not found"; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700226 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700228 const DexFile* dex_file = pair.first;
229 const DexFile::ClassDef* dex_class_def = pair.second;
230 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700232 if (!init_done_) {
233 // finish up init of hand crafted class_roots_
234 if (descriptor == "Ljava/lang/Object;") {
235 klass = class_roots_->Get(kJavaLangObject);
236 } else if (descriptor == "Ljava/lang/Class;") {
237 klass = class_roots_->Get(kJavaLangClass);
238 } else {
239 klass = AllocClass();
240 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700241 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700242 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700243 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700244 klass->dex_cache_ = dex_cache;
Brian Carlstromf615a612011-07-23 12:50:34 -0700245 LoadClass(*dex_file, *dex_class_def, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700246 // Check for a pending exception during load
247 if (self->IsExceptionPending()) {
248 // TODO: free native allocations in klass
249 return NULL;
250 }
251 {
252 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700253 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700254 // Add the newly loaded class to the loaded classes table.
255 bool success = InsertClass(klass);
256 if (!success) {
257 // We may fail to insert if we raced with another thread.
258 klass->clinit_thread_id_ = 0;
259 // TODO: free native allocations in klass
260 klass = LookupClass(descriptor, class_loader);
261 CHECK(klass != NULL);
262 } else {
263 // Link the class.
Brian Carlstromf615a612011-07-23 12:50:34 -0700264 if (!LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700265 // Linking failed.
266 // TODO: CHECK(self->IsExceptionPending());
267 lock.NotifyAll();
268 return NULL;
269 }
270 }
271 }
272 }
273 // Link the class if it has not already been linked.
274 if (!klass->IsLinked() && !klass->IsErroneous()) {
275 ObjectLock lock(klass);
276 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700277 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700278 LG << "Recursive link"; // TODO: ClassCircularityError
279 return NULL;
280 }
281 // Wait for the pending initialization to complete.
282 while (!klass->IsLinked() && !klass->IsErroneous()) {
283 lock.Wait();
284 }
285 }
286 if (klass->IsErroneous()) {
287 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
288 return NULL;
289 }
290 // Return the loaded class. No exceptions should be pending.
291 CHECK(!self->IsExceptionPending());
292 return klass;
293}
294
Brian Carlstromf615a612011-07-23 12:50:34 -0700295void ClassLinker::LoadClass(const DexFile& dex_file,
296 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700297 Class* klass) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700298 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700299 CHECK(klass->dex_cache_ != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700300 const byte* class_data = dex_file.GetClassData(dex_class_def);
301 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700302
Brian Carlstromf615a612011-07-23 12:50:34 -0700303 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700304 CHECK(descriptor != NULL);
305
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700306 klass->klass_ = class_roots_->Get(kJavaLangClass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700307 klass->descriptor_.set(descriptor);
308 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700309 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700310 klass->class_loader_ = NULL; // TODO
311 klass->primitive_type_ = Class::kPrimNot;
312 klass->status_ = Class::kStatusIdx;
313
314 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700315 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700316
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700317 size_t num_static_fields = header.static_fields_size_;
318 size_t num_instance_fields = header.instance_fields_size_;
319 size_t num_direct_methods = header.direct_methods_size_;
320 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700321
Brian Carlstromf615a612011-07-23 12:50:34 -0700322 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700323
324 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700325 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700326
327 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700328 DCHECK(klass->sfields_ == NULL);
329 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700330 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700331 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700332 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700333 DexFile::Field dex_field;
334 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700335 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700336 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700337 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700338 }
339 }
340
341 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700342 DCHECK(klass->ifields_ == NULL);
343 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700344 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700345 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700346 uint32_t last_idx = 0;
347 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700348 DexFile::Field dex_field;
349 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700350 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700351 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700352 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700353 }
354 }
355
356 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700357 DCHECK(klass->direct_methods_ == NULL);
358 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700359 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700360 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700361 uint32_t last_idx = 0;
362 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700363 DexFile::Method dex_method;
364 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700365 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700366 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700367 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700368 // TODO: register maps
369 }
370 }
371
372 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700373 DCHECK(klass->virtual_methods_ == NULL);
374 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700375 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700376 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700377 uint32_t last_idx = 0;
378 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700379 DexFile::Method dex_method;
380 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700381 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700382 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700383 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700384 // TODO: register maps
385 }
386 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700387}
388
Brian Carlstromf615a612011-07-23 12:50:34 -0700389void ClassLinker::LoadInterfaces(const DexFile& dex_file,
390 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700391 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700392 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700393 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700394 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700395 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700396 DCHECK(klass->interfaces_idx_ == NULL);
397 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700398 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700399 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700400 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700401 }
402 }
403}
404
Brian Carlstromf615a612011-07-23 12:50:34 -0700405void ClassLinker::LoadField(const DexFile& dex_file,
406 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700407 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700408 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700409 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700410 dst->klass_ = klass;
Brian Carlstromae3ac012011-07-27 01:30:28 -0700411 dst->name_.set(dex_file.dexStringById(field_id.name_idx_));
412 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700413 dst->access_flags_ = src.access_flags_;
414}
415
Brian Carlstromf615a612011-07-23 12:50:34 -0700416void ClassLinker::LoadMethod(const DexFile& dex_file,
417 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700418 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700419 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700420 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700421 dst->klass_ = klass;
Brian Carlstromf615a612011-07-23 12:50:34 -0700422 dst->name_.set(dex_file.dexStringById(method_id.name_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700423 dst->proto_idx_ = method_id.proto_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700424 dst->shorty_.set(dex_file.GetShorty(method_id.proto_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700425 dst->access_flags_ = src.access_flags_;
426
427 // TODO: check for finalize method
428
Brian Carlstromf615a612011-07-23 12:50:34 -0700429 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700430 if (code_item != NULL) {
431 dst->num_registers_ = code_item->registers_size_;
432 dst->num_ins_ = code_item->ins_size_;
433 dst->num_outs_ = code_item->outs_size_;
434 dst->insns_ = code_item->insns_;
435 } else {
436 uint16_t num_args = dst->NumArgRegisters();
437 if (!dst->IsStatic()) {
438 ++num_args;
439 }
440 dst->num_registers_ = dst->num_ins_ + num_args;
441 // TODO: native methods
442 }
443}
444
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700445ClassLinker::ClassPathEntry ClassLinker::FindInBootClassPath(const StringPiece& descriptor) {
446 for (size_t i = 0; i != boot_class_path_.size(); ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700447 const DexFile* dex_file = boot_class_path_[i];
Brian Carlstromf615a612011-07-23 12:50:34 -0700448 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
449 if (dex_class_def != NULL) {
450 return ClassPathEntry(dex_file, dex_class_def);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700451 }
452 }
Brian Carlstroma0808032011-07-18 00:39:23 -0700453 return ClassPathEntry(NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700454}
455
Brian Carlstromf615a612011-07-23 12:50:34 -0700456void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700457 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700458 boot_class_path_.push_back(dex_file);
459 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700460}
461
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700462void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700463 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700464 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700465 DexCache* dex_cache = AllocDexCache();
466 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700467 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
468 AllocObjectArray<Class>(dex_file->NumTypeIds()),
469 AllocObjectArray<Method>(dex_file->NumMethodIds()),
470 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700471 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700472}
473
Brian Carlstromf615a612011-07-23 12:50:34 -0700474const DexFile* ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700475 CHECK(dex_cache != NULL);
476 for (size_t i = 0; i != dex_caches_.size(); ++i) {
477 if (dex_caches_[i] == dex_cache) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700478 return dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700479 }
480 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700481 CHECK(false) << "Could not find DexFile";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700482 return NULL;
483}
484
Brian Carlstromf615a612011-07-23 12:50:34 -0700485DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
486 CHECK(dex_file != NULL);
487 for (size_t i = 0; i != dex_files_.size(); ++i) {
488 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700489 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700490 }
491 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700492 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700493 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700494}
495
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700496Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700497 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700498 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700499 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700500 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
501 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700502 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700503 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700504 bool success = InsertClass(klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700505 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700506 return klass;
507}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700508
Brian Carlstrombe977852011-07-19 14:54:54 -0700509// Create an array class (i.e. the class object for the array, not the
510// array itself). "descriptor" looks like "[C" or "[[[[B" or
511// "[Ljava/lang/String;".
512//
513// If "descriptor" refers to an array of primitives, look up the
514// primitive type's internally-generated class object.
515//
516// "loader" is the class loader of the class that's referring to us. It's
517// used to ensure that we're looking for the element type in the right
518// context. It does NOT become the class loader for the array class; that
519// always comes from the base element class.
520//
521// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700522Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
523 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700524 const DexFile* dex_file)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700525{
526 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700527
Brian Carlstrombe977852011-07-19 14:54:54 -0700528 // Identify the underlying element class and the array dimension depth.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700529 Class* component_type_ = NULL;
530 int array_rank;
531 if (descriptor[1] == '[') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700532 // array of arrays; keep descriptor and grab stuff from parent
Brian Carlstromf615a612011-07-23 12:50:34 -0700533 Class* outer = FindClass(descriptor.substr(1), class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700534 if (outer != NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700535 // want the base class, not "outer", in our component_type_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700536 component_type_ = outer->component_type_;
537 array_rank = outer->array_rank_ + 1;
538 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700539 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700540 }
541 } else {
542 array_rank = 1;
543 if (descriptor[1] == 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700544 // array of objects; strip off "[" and look up descriptor.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700545 const StringPiece subDescriptor = descriptor.substr(1);
Brian Carlstromf615a612011-07-23 12:50:34 -0700546 component_type_ = FindClass(subDescriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700547 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700548 // array of a primitive type
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700549 component_type_ = FindPrimitiveClass(descriptor[1]);
550 }
551 }
552
553 if (component_type_ == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700554 // failed
Brian Carlstrom8ecd08c2011-07-27 17:50:51 -0700555 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700556 return NULL;
557 }
558
Brian Carlstrombe977852011-07-19 14:54:54 -0700559 // See if the component type is already loaded. Array classes are
560 // always associated with the class loader of their underlying
561 // element type -- an array of Strings goes with the loader for
562 // java/lang/String -- so we need to look for it there. (The
563 // caller should have checked for the existence of the class
564 // before calling here, but they did so with *their* class loader,
565 // not the component type's loader.)
566 //
567 // If we find it, the caller adds "loader" to the class' initiating
568 // loader list, which should prevent us from going through this again.
569 //
570 // This call is unnecessary if "loader" and "component_type_->class_loader_"
571 // are the same, because our caller (FindClass) just did the
572 // lookup. (Even if we get this wrong we still have correct behavior,
573 // because we effectively do this lookup again when we add the new
574 // class to the hash table --- necessary because of possible races with
575 // other threads.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700576 if (class_loader != component_type_->class_loader_) {
577 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
578 if (new_class != NULL) {
579 return new_class;
580 }
581 }
582
Brian Carlstrombe977852011-07-19 14:54:54 -0700583 // Fill out the fields in the Class.
584 //
585 // It is possible to execute some methods against arrays, because
586 // all arrays are subclasses of java_lang_Object_, so we need to set
587 // up a vtable. We can just point at the one in java_lang_Object_.
588 //
589 // Array classes are simple enough that we don't need to do a full
590 // link step.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700591
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700592 Class* new_class = NULL;
593 if (!init_done_ && descriptor == "[Ljava/lang/Object;") {
594 new_class = class_roots_->Get(kObjectArrayClass);
595 CHECK(new_class);
596 }
597 if (new_class == NULL) {
598 new_class = AllocClass();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700599 if (new_class == NULL) {
600 return NULL;
601 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700602 }
603 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
604 descriptor.size());
605 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
606 new_class->descriptor_alloc_->size());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700607 Class* java_lang_Object = class_roots_->Get(kJavaLangObject);
608 new_class->super_class_ = java_lang_Object;
609 new_class->vtable_ = java_lang_Object->vtable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700610 new_class->primitive_type_ = Class::kPrimNot;
611 new_class->component_type_ = component_type_;
612 new_class->class_loader_ = component_type_->class_loader_;
613 new_class->array_rank_ = array_rank;
614 new_class->status_ = Class::kStatusInitialized;
Brian Carlstrombe977852011-07-19 14:54:54 -0700615 // don't need to set new_class->object_size_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700616
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700617
Brian Carlstrombe977852011-07-19 14:54:54 -0700618 // All arrays have java/lang/Cloneable and java/io/Serializable as
619 // interfaces. We need to set that up here, so that stuff like
620 // "instanceof" works right.
621 //
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700622 // Note: The GC could run during the call to FindSystemClass,
Brian Carlstrombe977852011-07-19 14:54:54 -0700623 // so we need to make sure the class object is GC-valid while we're in
624 // there. Do this by clearing the interface list so the GC will just
625 // think that the entries are null.
Brian Carlstrombe977852011-07-19 14:54:54 -0700626
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700627
628 // Use the single, global copies of "interfaces" and "iftable"
629 // (remember not to free them for arrays).
630 DCHECK(array_interfaces_ != NULL);
631 new_class->interfaces_ = array_interfaces_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700632 new_class->iftable_count_ = 2;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700633 DCHECK(array_iftable_ != NULL);
634 new_class->iftable_ = array_iftable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700635
Brian Carlstrombe977852011-07-19 14:54:54 -0700636 // Inherit access flags from the component type. Arrays can't be
637 // used as a superclass or interface, so we want to add "final"
638 // and remove "interface".
639 //
640 // Don't inherit any non-standard flags (e.g., kAccFinal)
641 // from component_type_. We assume that the array class does not
642 // override finalize().
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700643 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
644 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
645
646 if (InsertClass(new_class)) {
647 return new_class;
648 }
Brian Carlstrombe977852011-07-19 14:54:54 -0700649 // Another thread must have loaded the class after we
650 // started but before we finished. Abandon what we've
651 // done.
652 //
653 // (Yes, this happens.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700654
Brian Carlstrombe977852011-07-19 14:54:54 -0700655 // Grab the winning class.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700656 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
657 DCHECK(other_class != NULL);
658 return other_class;
659}
660
661Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700662 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700663 case 'B':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700664 DCHECK(class_roots_->Get(kPrimitiveByte) != NULL);
665 return class_roots_->Get(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700666 case 'C':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700667 DCHECK(class_roots_->Get(kPrimitiveChar) != NULL);
668 return class_roots_->Get(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700669 case 'D':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700670 DCHECK(class_roots_->Get(kPrimitiveDouble) != NULL);
671 return class_roots_->Get(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700672 case 'F':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700673 DCHECK(class_roots_->Get(kPrimitiveFloat) != NULL);
674 return class_roots_->Get(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700675 case 'I':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700676 DCHECK(class_roots_->Get(kPrimitiveInt) != NULL);
677 return class_roots_->Get(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700678 case 'J':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700679 DCHECK(class_roots_->Get(kPrimitiveLong) != NULL);
680 return class_roots_->Get(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700681 case 'S':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700682 DCHECK(class_roots_->Get(kPrimitiveShort) != NULL);
683 return class_roots_->Get(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700684 case 'Z':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700685 DCHECK(class_roots_->Get(kPrimitiveBoolean) != NULL);
686 return class_roots_->Get(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700687 case 'V':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700688 DCHECK(class_roots_->Get(kPrimitiveVoid) != NULL);
689 return class_roots_->Get(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700690 case 'L':
691 case '[':
692 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700693 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700694 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700695 };
696 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700697}
698
699bool ClassLinker::InsertClass(Class* klass) {
700 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700701 const StringPiece& key = klass->GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700702 bool success = classes_.insert(std::make_pair(key, klass)).second;
703 // TODO: release classes_lock_
704 return success;
705}
706
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700707Class* ClassLinker::LookupClass(const StringPiece& descriptor, Object* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700708 // TODO: acquire classes_lock_
709 Table::iterator it = classes_.find(descriptor);
710 // TODO: release classes_lock_
711 if (it == classes_.end()) {
712 return NULL;
713 } else {
714 return (*it).second;
715 }
716}
717
718bool ClassLinker::InitializeClass(Class* klass) {
719 CHECK(klass->GetStatus() == Class::kStatusResolved ||
720 klass->GetStatus() == Class::kStatusError);
721
Carl Shapirob5573532011-07-12 18:22:59 -0700722 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700723
724 {
725 ObjectLock lock(klass);
726
727 if (klass->GetStatus() < Class::kStatusVerified) {
728 if (klass->IsErroneous()) {
729 LG << "re-initializing failed class"; // TODO: throw
730 return false;
731 }
732
733 CHECK(klass->GetStatus() == Class::kStatusResolved);
734
735 klass->status_ = Class::kStatusVerifying;
736 if (!DexVerify::VerifyClass(klass)) {
737 LG << "Verification failed"; // TODO: ThrowVerifyError
738 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700739 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
740 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700741 klass->SetStatus(Class::kStatusError);
742 return false;
743 }
744
745 klass->SetStatus(Class::kStatusVerified);
746 }
747
748 if (klass->status_ == Class::kStatusInitialized) {
749 return true;
750 }
751
752 while (klass->status_ == Class::kStatusInitializing) {
753 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700754 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700755 LG << "recursive <clinit>";
756 return true;
757 }
758
759 CHECK(!self->IsExceptionPending());
760
761 lock.Wait(); // TODO: check for interruption
762
763 // When we wake up, repeat the test for init-in-progress. If
764 // there's an exception pending (only possible if
765 // "interruptShouldThrow" was set), bail out.
766 if (self->IsExceptionPending()) {
767 CHECK(false);
768 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
769 klass->SetStatus(Class::kStatusError);
770 return false;
771 }
772 if (klass->GetStatus() == Class::kStatusInitializing) {
773 continue;
774 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700775 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700776 klass->GetStatus() == Class::kStatusError);
777 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700778 // The caller wants an exception, but it was thrown in a
779 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700780 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
781 return false;
782 }
783 return true; // otherwise, initialized
784 }
785
786 // see if we failed previously
787 if (klass->IsErroneous()) {
788 // might be wise to unlock before throwing; depends on which class
789 // it is that we have locked
790
791 // TODO: throwEarlierClassFailure(klass);
792 return false;
793 }
794
795 if (!ValidateSuperClassDescriptors(klass)) {
796 klass->SetStatus(Class::kStatusError);
797 return false;
798 }
799
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700800 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700801
Carl Shapirob5573532011-07-12 18:22:59 -0700802 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700803 klass->status_ = Class::kStatusInitializing;
804 }
805
806 if (!InitializeSuperClass(klass)) {
807 return false;
808 }
809
810 InitializeStaticFields(klass);
811
812 Method* clinit = klass->FindDirectMethodLocally("<clinit>", "()V");
813 if (clinit != NULL) {
814 } else {
815 // JValue unused;
816 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700817 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700818 }
819
820 {
821 ObjectLock lock(klass);
822
823 if (self->IsExceptionPending()) {
824 klass->SetStatus(Class::kStatusError);
825 } else {
826 klass->SetStatus(Class::kStatusInitialized);
827 }
828 lock.NotifyAll();
829 }
830
831 return true;
832}
833
834bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
835 if (klass->IsInterface()) {
836 return true;
837 }
838 // begin with the methods local to the superclass
839 if (klass->HasSuperClass() &&
840 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
841 const Class* super = klass->GetSuperClass();
842 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
843 const Method* method = klass->GetVirtualMethod(i);
844 if (method != super->GetVirtualMethod(i) &&
845 !HasSameMethodDescriptorClasses(method, super, klass)) {
846 LG << "Classes resolve differently in superclass";
847 return false;
848 }
849 }
850 }
851 for (size_t i = 0; i < klass->iftable_count_; ++i) {
852 const InterfaceEntry* iftable = &klass->iftable_[i];
853 Class* interface = iftable->GetClass();
854 if (klass->GetClassLoader() != interface->GetClassLoader()) {
855 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
856 uint32_t vtable_index = iftable->method_index_array_[j];
857 const Method* method = klass->GetVirtualMethod(vtable_index);
858 if (!HasSameMethodDescriptorClasses(method, interface,
859 method->GetClass())) {
860 LG << "Classes resolve differently in interface"; // TODO: LinkageError
861 return false;
862 }
863 }
864 }
865 }
866 return true;
867}
868
869bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700870 const Class* klass1,
871 const Class* klass2) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700872 const DexFile* dex_file = FindDexFile(method->GetClass()->GetDexCache());
873 const DexFile::ProtoId& proto_id = dex_file->GetProtoId(method->proto_idx_);
874 DexFile::ParameterIterator *it;
875 for (it = dex_file->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700876 const char* descriptor = it->GetDescriptor();
877 if (descriptor == NULL) {
878 break;
879 }
880 if (descriptor[0] == 'L' || descriptor[0] == '[') {
881 // Found a non-primitive type.
882 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
883 return false;
884 }
885 }
886 }
887 // Check the return type
Brian Carlstromf615a612011-07-23 12:50:34 -0700888 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700889 if (descriptor[0] == 'L' || descriptor[0] == '[') {
890 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
891 return false;
892 }
893 }
894 return true;
895}
896
897// Returns true if classes referenced by the descriptor are the
898// same classes in klass1 as they are in klass2.
899bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700900 const Class* klass1,
901 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700902 CHECK(descriptor != NULL);
903 CHECK(klass1 != NULL);
904 CHECK(klass2 != NULL);
905#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700906 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700907 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700908 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700909 // TODO: found2 == NULL
910 // TODO: lookup found1 in initiating loader list
911 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700912 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700913 if (found1 == found2) {
914 return true;
915 } else {
916 return false;
917 }
918 }
919#endif
920 return true;
921}
922
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700923bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700924 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
925 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
926 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
927 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700928
929 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstromf615a612011-07-23 12:50:34 -0700930 const DexFile::TypeList* type_list1 = dex1->GetProtoParameters(proto1);
931 const DexFile::TypeList* type_list2 = dex2->GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700932 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
933 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
934 if (arity1 != arity2) {
935 return false;
936 }
937
938 for (size_t i = 0; i < arity1; ++i) {
939 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
940 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700941 const char* type1 = dex1->dexStringByTypeIdx(type_idx1);
942 const char* type2 = dex2->dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700943 if (strcmp(type1, type2) != 0) {
944 return false;
945 }
946 }
947
948 return true;
949}
950
951bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700952 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
953 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
954 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
955 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
956 const char* type1 = dex1->dexStringByTypeIdx(proto1.return_type_idx_);
957 const char* type2 = dex2->dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700958 return (strcmp(type1, type2) == 0);
959}
960
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700961bool ClassLinker::InitializeSuperClass(Class* klass) {
962 CHECK(klass != NULL);
963 // TODO: assert klass lock is acquired
964 if (!klass->IsInterface() && klass->HasSuperClass()) {
965 Class* super_class = klass->GetSuperClass();
966 if (super_class->GetStatus() != Class::kStatusInitialized) {
967 CHECK(!super_class->IsInterface());
968 klass->MonitorExit();
969 bool super_initialized = InitializeClass(super_class);
970 klass->MonitorEnter();
971 // TODO: check for a pending exception
972 if (!super_initialized) {
973 klass->SetStatus(Class::kStatusError);
974 klass->NotifyAll();
975 return false;
976 }
977 }
978 }
979 return true;
980}
981
982void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700983 size_t num_static_fields = klass->NumStaticFields();
984 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700985 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700986 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700987 DexCache* dex_cache = klass->GetDexCache();
988 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700989 return;
990 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700991 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstromf615a612011-07-23 12:50:34 -0700992 const DexFile* dex_file = FindDexFile(dex_cache);
993 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
994 CHECK(dex_class_def != NULL);
995 const byte* addr = dex_file->GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700996 size_t array_size = DecodeUnsignedLeb128(&addr);
997 for (size_t i = 0; i < array_size; ++i) {
998 StaticField* field = klass->GetStaticField(i);
999 JValue value;
Brian Carlstromf615a612011-07-23 12:50:34 -07001000 DexFile::ValueType type = dex_file->ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001001 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001002 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001003 field->SetByte(value.b);
1004 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001005 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001006 field->SetShort(value.s);
1007 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001008 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001009 field->SetChar(value.c);
1010 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001011 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001012 field->SetInt(value.i);
1013 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001014 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001015 field->SetLong(value.j);
1016 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001017 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001018 field->SetFloat(value.f);
1019 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001020 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001021 field->SetDouble(value.d);
1022 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001023 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001024 uint32_t string_idx = value.i;
1025 String* resolved = ResolveString(klass, string_idx);
1026 field->SetObject(resolved);
1027 break;
1028 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001029 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001030 field->SetBoolean(value.z);
1031 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001032 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001033 field->SetObject(value.l);
1034 break;
1035 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001036 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001037 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001038 }
1039}
1040
Brian Carlstromf615a612011-07-23 12:50:34 -07001041bool ClassLinker::LinkClass(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001042 CHECK(klass->status_ == Class::kStatusIdx ||
1043 klass->status_ == Class::kStatusLoaded);
1044 if (klass->status_ == Class::kStatusIdx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001045 if (!LinkInterfaces(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001046 return false;
1047 }
1048 }
1049 if (!LinkSuperClass(klass)) {
1050 return false;
1051 }
1052 if (!LinkMethods(klass)) {
1053 return false;
1054 }
1055 if (!LinkInstanceFields(klass)) {
1056 return false;
1057 }
1058 CreateReferenceOffsets(klass);
1059 CHECK_EQ(klass->status_, Class::kStatusLoaded);
1060 klass->status_ = Class::kStatusResolved;
1061 return true;
1062}
1063
Brian Carlstromf615a612011-07-23 12:50:34 -07001064bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001065 // Mark the class as loaded.
1066 klass->status_ = Class::kStatusLoaded;
Brian Carlstromf615a612011-07-23 12:50:34 -07001067 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1068 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001069 if (super_class == NULL) {
1070 LG << "Failed to resolve superclass";
1071 return false;
1072 }
1073 klass->super_class_ = super_class; // TODO: write barrier
1074 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001075 if (klass->NumInterfaces() > 0) {
1076 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1077 uint32_t idx = klass->interfaces_idx_[i];
1078 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1079 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001080 LG << "Failed to resolve interface";
1081 return false;
1082 }
1083 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001084 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001085 LG << "Inaccessible interface";
1086 return false;
1087 }
1088 }
1089 }
1090 return true;
1091}
1092
1093bool ClassLinker::LinkSuperClass(Class* klass) {
1094 CHECK(!klass->IsPrimitive());
1095 const Class* super = klass->GetSuperClass();
1096 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1097 if (super != NULL) {
1098 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1099 return false;
1100 }
1101 // TODO: clear finalize attribute
1102 return true;
1103 }
1104 if (super == NULL) {
1105 LG << "No superclass defined"; // TODO: LinkageError
1106 return false;
1107 }
1108 // Verify
1109 if (super->IsFinal()) {
1110 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
1111 return false;
1112 }
1113 if (super->IsInterface()) {
1114 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
1115 return false;
1116 }
1117 if (!klass->CanAccess(super)) {
1118 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
1119 return false;
1120 }
1121 return true;
1122}
1123
1124// Populate the class vtable and itable.
1125bool ClassLinker::LinkMethods(Class* klass) {
1126 if (klass->IsInterface()) {
1127 // No vtable.
1128 size_t count = klass->NumVirtualMethods();
1129 if (!IsUint(16, count)) {
1130 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1131 return false;
1132 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001133 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001134 klass->GetVirtualMethod(i)->method_index_ = i;
1135 }
1136 } else {
1137 // Link virtual method tables
1138 LinkVirtualMethods(klass);
1139
1140 // Link interface method tables
1141 LinkInterfaceMethods(klass);
1142
1143 // Insert stubs.
1144 LinkAbstractMethods(klass);
1145 }
1146 return true;
1147}
1148
1149bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001150 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001151 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001152 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1153 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001154 // TODO: do not assign to the vtable field until it is fully constructed.
1155 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001156 // See if any of our virtual methods override the superclass.
1157 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1158 Method* local_method = klass->GetVirtualMethod(i);
1159 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001160 for (; j < actual_count; ++j) {
1161 Method* super_method = klass->vtable_->Get(j);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001162 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001163 // Verify
1164 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001165 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001166 return false;
1167 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001168 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001169 local_method->method_index_ = j;
1170 break;
1171 }
1172 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001173 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001174 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001175 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001176 local_method->method_index_ = actual_count;
1177 actual_count += 1;
1178 }
1179 }
1180 if (!IsUint(16, actual_count)) {
1181 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1182 return false;
1183 }
1184 CHECK_LE(actual_count, max_count);
1185 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001186 // TODO: do not assign to the vtable field until it is fully constructed.
1187 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001188 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001189 } else {
1190 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001191 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1192 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1193 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001194 LG << "Too many methods"; // TODO: VirtualMachineError
1195 return false;
1196 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001197 // TODO: do not assign to the vtable field until it is fully constructed.
1198 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1199 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001200 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1202 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001203 }
1204 return true;
1205}
1206
1207bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1208 int pool_offset = 0;
1209 int pool_size = 0;
1210 int miranda_count = 0;
1211 int miranda_alloc = 0;
1212 size_t super_ifcount;
1213 if (klass->HasSuperClass()) {
1214 super_ifcount = klass->GetSuperClass()->iftable_count_;
1215 } else {
1216 super_ifcount = 0;
1217 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001218 size_t ifcount = super_ifcount;
1219 ifcount += klass->NumInterfaces();
1220 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1221 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001222 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001223 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001224 DCHECK(klass->iftable_count_ == 0);
1225 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001226 return true;
1227 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001228 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1229 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001230 if (super_ifcount != 0) {
1231 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1232 sizeof(InterfaceEntry) * super_ifcount);
1233 }
1234 // Flatten the interface inheritance hierarchy.
1235 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001236 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1237 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001238 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001239 if (!interf->IsInterface()) {
1240 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1241 return false;
1242 }
1243 klass->iftable_[idx++].SetClass(interf);
1244 for (size_t j = 0; j < interf->iftable_count_; j++) {
1245 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1246 }
1247 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001248 CHECK_EQ(idx, ifcount);
1249 klass->iftable_count_ = ifcount;
1250 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001251 return true;
1252 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001253 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001254 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1255 }
1256 if (pool_size == 0) {
1257 return true;
1258 }
1259 klass->ifvi_pool_count_ = pool_size;
1260 klass->ifvi_pool_ = new uint32_t[pool_size];
1261 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001262 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001263 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1264 Class* interface = klass->iftable_[i].GetClass();
1265 pool_offset += interface->NumVirtualMethods(); // end here
1266 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1267 Method* interface_method = interface->GetVirtualMethod(j);
1268 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001269 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
1270 if (HasSameNameAndPrototype(interface_method, klass->vtable_->Get(k))) {
1271 if (!klass->vtable_->Get(k)->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001272 LG << "Implementation not public";
1273 return false;
1274 }
1275 klass->iftable_[i].method_index_array_[j] = k;
1276 break;
1277 }
1278 }
1279 if (k < 0) {
1280 if (miranda_count == miranda_alloc) {
1281 miranda_alloc += 8;
1282 if (miranda_list.empty()) {
1283 miranda_list.resize(miranda_alloc);
1284 } else {
1285 miranda_list.resize(miranda_alloc);
1286 }
1287 }
1288 int mir;
1289 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001290 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001291 break;
1292 }
1293 }
1294 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001295 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001296 if (mir == miranda_count) {
1297 miranda_list[miranda_count++] = interface_method;
1298 }
1299 }
1300 }
1301 }
1302 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001303 int old_method_count = klass->NumVirtualMethods();
1304 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001305 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001306
1307 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001308 int old_vtable_count = klass->vtable_->GetLength();
1309 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001310 // TODO: do not assign to the vtable field until it is fully constructed.
1311 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001312
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001313 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001314 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001315 memcpy(meth, miranda_list[i], sizeof(Method));
1316 meth->klass_ = klass;
1317 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001318 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1319 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001320 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001321 }
1322 }
1323 return true;
1324}
1325
1326void ClassLinker::LinkAbstractMethods(Class* klass) {
1327 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1328 Method* method = klass->GetVirtualMethod(i);
1329 if (method->IsAbstract()) {
1330 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1331 }
1332 }
1333}
1334
1335bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001336 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001337 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001338 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001339 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001340 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001341 }
1342 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001343 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001344 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001345 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001346 InstanceField* pField = klass->GetInstanceField(i);
1347 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001348 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001349 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1350 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 char rc = refField->GetType();
1352 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001353 klass->SetInstanceField(i, refField);
1354 klass->SetInstanceField(j, pField);
1355 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001356 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001357 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 break;
1359 }
1360 }
1361 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001362 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001363 }
1364 if (c != '[' && c != 'L') {
1365 break;
1366 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001367 pField->SetOffset(field_offset);
1368 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001369 }
1370
1371 // Now we want to pack all of the double-wide fields together. If
1372 // we're not aligned, though, we want to shuffle one 32-bit field
1373 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001374 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001375 InstanceField* pField = klass->GetInstanceField(i);
1376 char c = pField->GetType();
1377
1378 if (c != 'J' && c != 'D') {
1379 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001380 DCHECK(c != '[');
1381 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001382 pField->SetOffset(field_offset);
1383 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001384 i++;
1385 } else {
1386 // Next field is 64-bit, so search for a 32-bit field we can
1387 // swap into it.
1388 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001389 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1390 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001391 char rc = singleField->GetType();
1392 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001393 klass->SetInstanceField(i, singleField);
1394 klass->SetInstanceField(j, pField);
1395 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001396 pField->SetOffset(field_offset);
1397 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001398 found = true;
1399 i++;
1400 break;
1401 }
1402 }
1403 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001404 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001405 }
1406 }
1407 }
1408
1409 // Alignment is good, shuffle any double-wide fields forward, and
1410 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001411 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001412 for ( ; i < klass->NumInstanceFields(); i++) {
1413 InstanceField* pField = klass->GetInstanceField(i);
1414 char c = pField->GetType();
1415 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001416 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1417 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001418 char rc = doubleField->GetType();
1419 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001420 klass->SetInstanceField(i, doubleField);
1421 klass->SetInstanceField(j, pField);
1422 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001423 c = rc;
1424 break;
1425 }
1426 }
1427 } else {
1428 // This is a double-wide field, leave it be.
1429 }
1430
Brian Carlstroma0808032011-07-18 00:39:23 -07001431 pField->SetOffset(field_offset);
1432 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001433 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001434 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001435 }
1436
1437#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001438 // Make sure that all reference fields appear before
1439 // non-reference fields, and all double-wide fields are aligned.
1440 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001441 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001442 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001443 char c = pField->GetType();
1444
1445 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001446 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001447 }
1448
1449 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001450 if (!seen_non_ref) {
1451 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001452 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001453 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001454 } else {
1455 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001456 }
1457 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001458 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001459 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001460 }
1461#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001462 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001463 return true;
1464}
1465
1466// Set the bitmap of reference offsets, refOffsets, from the ifields
1467// list.
1468void ClassLinker::CreateReferenceOffsets(Class* klass) {
1469 uint32_t reference_offsets = 0;
1470 if (klass->HasSuperClass()) {
1471 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1472 }
1473 // If our superclass overflowed, we don't stand a chance.
1474 if (reference_offsets != CLASS_WALK_SUPER) {
1475 // All of the fields that contain object references are guaranteed
1476 // to be at the beginning of the ifields list.
1477 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1478 // Note that, per the comment on struct InstField, f->byteOffset
1479 // is the offset from the beginning of obj, not the offset into
1480 // obj->instanceData.
1481 const InstanceField* field = klass->GetInstanceField(i);
1482 size_t byte_offset = field->GetOffset();
1483 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001484 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001485 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1486 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001487 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001488 reference_offsets |= new_bit;
1489 } else {
1490 reference_offsets = CLASS_WALK_SUPER;
1491 break;
1492 }
1493 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001494 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001495 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001496}
1497
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001498Class* ClassLinker::ResolveClass(const Class* referrer,
1499 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001500 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001501 DexCache* dex_cache = referrer->GetDexCache();
1502 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001503 if (resolved != NULL) {
1504 return resolved;
1505 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001506 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001507 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001508 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001509 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001510 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001511 }
1512 if (resolved != NULL) {
1513 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001514 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001515 if (check->GetClassLoader() != NULL) {
1516 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1517 return NULL;
1518 }
1519 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001520 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001521 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001522 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001523 }
1524 return resolved;
1525}
1526
1527Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1528 /*MethodType*/ int method_type) {
1529 CHECK(false);
1530 return NULL;
1531}
1532
Carl Shapiro69759ea2011-07-21 18:13:35 -07001533String* ClassLinker::ResolveString(const Class* referring,
1534 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001535 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1536 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001537 int32_t utf16_length = dex_file->GetStringLength(string_id);
1538 const char* utf8_data = dex_file->GetStringData(string_id);
1539 String* new_string = AllocStringFromModifiedUtf8(utf16_length, utf8_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001540 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001541 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001542 return new_string;
1543}
1544
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001545} // namespace art