blob: b257967c49fe780dfb686a9a2971b1d9b583ea7f [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
152void ClassLinker::VisitRoots(RootVistor* rootVisitor, void* arg) {
153 for (size_t i = 0; i < kClassRootsMax; i++) {
154 rootVisitor(class_roots_->Get(i), arg);
155 }
156
157 for (size_t i = 0; i < dex_caches_.size(); i++) {
158 rootVisitor(dex_caches_[i], arg);
159 }
160
161 // TODO: acquire classes_lock_
162 typedef Table::const_iterator It; // TODO: C++0x auto
163 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
164 rootVisitor(it->second, arg);
165 }
166 // TODO: release classes_lock_
167
168 rootVisitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700169}
170
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700171DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700172 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700173}
174
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700175Class* ClassLinker::AllocClass(Class* java_lang_Class) {
176 return down_cast<Class*>(Object::Alloc(java_lang_Class));
177}
178
179Class* ClassLinker::AllocClass() {
180 return AllocClass(class_roots_->Get(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700181}
182
183StaticField* ClassLinker::AllocStaticField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700184 return down_cast<StaticField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700185 sizeof(StaticField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700186}
187
188InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700189 return down_cast<InstanceField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700190 sizeof(InstanceField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700191}
192
193Method* ClassLinker::AllocMethod() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700194 return down_cast<Method*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectMethod),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700195 sizeof(Method)));
Carl Shapiro565f5072011-07-10 13:39:43 -0700196}
197
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700198String* ClassLinker::AllocStringFromModifiedUtf8(int32_t utf16_length,
199 const char* utf8_data_in) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700200 return String::AllocFromModifiedUtf8(class_roots_->Get(kJavaLangString),
201 class_roots_->Get(kCharArrayClass),
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700202 utf16_length,
203 utf8_data_in);
204}
205
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700206Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700207 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700208 const DexFile* dex_file) {
Carl Shapirob5573532011-07-12 18:22:59 -0700209 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700210 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700211 CHECK(!self->IsExceptionPending());
212 // Find the class in the loaded classes table.
213 Class* klass = LookupClass(descriptor, class_loader);
214 if (klass == NULL) {
215 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700216 if (descriptor[0] == '[') {
Brian Carlstromf615a612011-07-23 12:50:34 -0700217 return CreateArrayClass(descriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700218 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700219 ClassPathEntry pair;
Brian Carlstromf615a612011-07-23 12:50:34 -0700220 if (dex_file == NULL) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700221 pair = FindInBootClassPath(descriptor);
222 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700223 pair.first = dex_file;
224 pair.second = dex_file->FindClassDef(descriptor);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700225 }
226 if (pair.second == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700227 LG << "Class " << descriptor << " not found"; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700228 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700230 const DexFile* dex_file = pair.first;
231 const DexFile::ClassDef* dex_class_def = pair.second;
232 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700233 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700234 if (!init_done_) {
235 // finish up init of hand crafted class_roots_
236 if (descriptor == "Ljava/lang/Object;") {
237 klass = class_roots_->Get(kJavaLangObject);
238 } else if (descriptor == "Ljava/lang/Class;") {
239 klass = class_roots_->Get(kJavaLangClass);
240 } else {
241 klass = AllocClass();
242 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700243 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700244 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700245 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700246 klass->dex_cache_ = dex_cache;
Brian Carlstromf615a612011-07-23 12:50:34 -0700247 LoadClass(*dex_file, *dex_class_def, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700248 // Check for a pending exception during load
249 if (self->IsExceptionPending()) {
250 // TODO: free native allocations in klass
251 return NULL;
252 }
253 {
254 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700255 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700256 // Add the newly loaded class to the loaded classes table.
257 bool success = InsertClass(klass);
258 if (!success) {
259 // We may fail to insert if we raced with another thread.
260 klass->clinit_thread_id_ = 0;
261 // TODO: free native allocations in klass
262 klass = LookupClass(descriptor, class_loader);
263 CHECK(klass != NULL);
264 } else {
265 // Link the class.
Brian Carlstromf615a612011-07-23 12:50:34 -0700266 if (!LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700267 // Linking failed.
268 // TODO: CHECK(self->IsExceptionPending());
269 lock.NotifyAll();
270 return NULL;
271 }
272 }
273 }
274 }
275 // Link the class if it has not already been linked.
276 if (!klass->IsLinked() && !klass->IsErroneous()) {
277 ObjectLock lock(klass);
278 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700279 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700280 LG << "Recursive link"; // TODO: ClassCircularityError
281 return NULL;
282 }
283 // Wait for the pending initialization to complete.
284 while (!klass->IsLinked() && !klass->IsErroneous()) {
285 lock.Wait();
286 }
287 }
288 if (klass->IsErroneous()) {
289 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
290 return NULL;
291 }
292 // Return the loaded class. No exceptions should be pending.
293 CHECK(!self->IsExceptionPending());
294 return klass;
295}
296
Brian Carlstromf615a612011-07-23 12:50:34 -0700297void ClassLinker::LoadClass(const DexFile& dex_file,
298 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700299 Class* klass) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700300 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700301 CHECK(klass->dex_cache_ != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700302 const byte* class_data = dex_file.GetClassData(dex_class_def);
303 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700304
Brian Carlstromf615a612011-07-23 12:50:34 -0700305 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700306 CHECK(descriptor != NULL);
307
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700308 klass->klass_ = class_roots_->Get(kJavaLangClass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700309 klass->descriptor_.set(descriptor);
310 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700311 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700312 klass->class_loader_ = NULL; // TODO
313 klass->primitive_type_ = Class::kPrimNot;
314 klass->status_ = Class::kStatusIdx;
315
316 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700317 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700318
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700319 size_t num_static_fields = header.static_fields_size_;
320 size_t num_instance_fields = header.instance_fields_size_;
321 size_t num_direct_methods = header.direct_methods_size_;
322 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700323
Brian Carlstromf615a612011-07-23 12:50:34 -0700324 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700325
326 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700327 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700328
329 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700330 DCHECK(klass->sfields_ == NULL);
331 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700332 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700333 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700334 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700335 DexFile::Field dex_field;
336 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700337 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700338 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700339 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700340 }
341 }
342
343 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700344 DCHECK(klass->ifields_ == NULL);
345 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700346 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700347 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700348 uint32_t last_idx = 0;
349 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700350 DexFile::Field dex_field;
351 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700352 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700353 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700354 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700355 }
356 }
357
358 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700359 DCHECK(klass->direct_methods_ == NULL);
360 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700361 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700362 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700363 uint32_t last_idx = 0;
364 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700365 DexFile::Method dex_method;
366 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700367 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700368 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700369 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700370 // TODO: register maps
371 }
372 }
373
374 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700375 DCHECK(klass->virtual_methods_ == NULL);
376 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700377 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700378 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700379 uint32_t last_idx = 0;
380 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700381 DexFile::Method dex_method;
382 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700383 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700384 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700385 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700386 // TODO: register maps
387 }
388 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700389}
390
Brian Carlstromf615a612011-07-23 12:50:34 -0700391void ClassLinker::LoadInterfaces(const DexFile& dex_file,
392 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700393 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700394 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700395 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700396 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700397 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700398 DCHECK(klass->interfaces_idx_ == NULL);
399 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700400 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700401 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700402 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700403 }
404 }
405}
406
Brian Carlstromf615a612011-07-23 12:50:34 -0700407void ClassLinker::LoadField(const DexFile& dex_file,
408 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700409 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700410 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700411 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700412 dst->klass_ = klass;
Brian Carlstromae3ac012011-07-27 01:30:28 -0700413 dst->name_.set(dex_file.dexStringById(field_id.name_idx_));
414 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700415 dst->access_flags_ = src.access_flags_;
416}
417
Brian Carlstromf615a612011-07-23 12:50:34 -0700418void ClassLinker::LoadMethod(const DexFile& dex_file,
419 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700420 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700421 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700422 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700423 dst->klass_ = klass;
Brian Carlstromf615a612011-07-23 12:50:34 -0700424 dst->name_.set(dex_file.dexStringById(method_id.name_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700425 dst->proto_idx_ = method_id.proto_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700426 dst->shorty_.set(dex_file.GetShorty(method_id.proto_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700427 dst->access_flags_ = src.access_flags_;
428
429 // TODO: check for finalize method
430
Brian Carlstromf615a612011-07-23 12:50:34 -0700431 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700432 if (code_item != NULL) {
433 dst->num_registers_ = code_item->registers_size_;
434 dst->num_ins_ = code_item->ins_size_;
435 dst->num_outs_ = code_item->outs_size_;
436 dst->insns_ = code_item->insns_;
437 } else {
438 uint16_t num_args = dst->NumArgRegisters();
439 if (!dst->IsStatic()) {
440 ++num_args;
441 }
442 dst->num_registers_ = dst->num_ins_ + num_args;
443 // TODO: native methods
444 }
445}
446
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700447ClassLinker::ClassPathEntry ClassLinker::FindInBootClassPath(const StringPiece& descriptor) {
448 for (size_t i = 0; i != boot_class_path_.size(); ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700449 const DexFile* dex_file = boot_class_path_[i];
Brian Carlstromf615a612011-07-23 12:50:34 -0700450 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
451 if (dex_class_def != NULL) {
452 return ClassPathEntry(dex_file, dex_class_def);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700453 }
454 }
Brian Carlstroma0808032011-07-18 00:39:23 -0700455 return ClassPathEntry(NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700456}
457
Brian Carlstromf615a612011-07-23 12:50:34 -0700458void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700459 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700460 boot_class_path_.push_back(dex_file);
461 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700462}
463
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700464void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700465 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700466 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700467 DexCache* dex_cache = AllocDexCache();
468 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700469 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
470 AllocObjectArray<Class>(dex_file->NumTypeIds()),
471 AllocObjectArray<Method>(dex_file->NumMethodIds()),
472 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700473 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700474}
475
Brian Carlstromf615a612011-07-23 12:50:34 -0700476const DexFile* ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700477 CHECK(dex_cache != NULL);
478 for (size_t i = 0; i != dex_caches_.size(); ++i) {
479 if (dex_caches_[i] == dex_cache) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700480 return dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700481 }
482 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700483 CHECK(false) << "Could not find DexFile";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700484 return NULL;
485}
486
Brian Carlstromf615a612011-07-23 12:50:34 -0700487DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
488 CHECK(dex_file != NULL);
489 for (size_t i = 0; i != dex_files_.size(); ++i) {
490 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700491 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700492 }
493 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700494 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700495 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700496}
497
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700498Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700499 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700500 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700501 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700502 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
503 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700504 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700505 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700506 bool success = InsertClass(klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700507 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700508 return klass;
509}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700510
Brian Carlstrombe977852011-07-19 14:54:54 -0700511// Create an array class (i.e. the class object for the array, not the
512// array itself). "descriptor" looks like "[C" or "[[[[B" or
513// "[Ljava/lang/String;".
514//
515// If "descriptor" refers to an array of primitives, look up the
516// primitive type's internally-generated class object.
517//
518// "loader" is the class loader of the class that's referring to us. It's
519// used to ensure that we're looking for the element type in the right
520// context. It does NOT become the class loader for the array class; that
521// always comes from the base element class.
522//
523// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700524Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
525 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700526 const DexFile* dex_file)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700527{
528 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700529
Brian Carlstrombe977852011-07-19 14:54:54 -0700530 // Identify the underlying element class and the array dimension depth.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700531 Class* component_type_ = NULL;
532 int array_rank;
533 if (descriptor[1] == '[') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700534 // array of arrays; keep descriptor and grab stuff from parent
Brian Carlstromf615a612011-07-23 12:50:34 -0700535 Class* outer = FindClass(descriptor.substr(1), class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700536 if (outer != NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700537 // want the base class, not "outer", in our component_type_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700538 component_type_ = outer->component_type_;
539 array_rank = outer->array_rank_ + 1;
540 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700541 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700542 }
543 } else {
544 array_rank = 1;
545 if (descriptor[1] == 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700546 // array of objects; strip off "[" and look up descriptor.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700547 const StringPiece subDescriptor = descriptor.substr(1);
Brian Carlstromf615a612011-07-23 12:50:34 -0700548 component_type_ = FindClass(subDescriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700549 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700550 // array of a primitive type
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700551 component_type_ = FindPrimitiveClass(descriptor[1]);
552 }
553 }
554
555 if (component_type_ == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700556 // failed
Brian Carlstrom8ecd08c2011-07-27 17:50:51 -0700557 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700558 return NULL;
559 }
560
Brian Carlstrombe977852011-07-19 14:54:54 -0700561 // See if the component type is already loaded. Array classes are
562 // always associated with the class loader of their underlying
563 // element type -- an array of Strings goes with the loader for
564 // java/lang/String -- so we need to look for it there. (The
565 // caller should have checked for the existence of the class
566 // before calling here, but they did so with *their* class loader,
567 // not the component type's loader.)
568 //
569 // If we find it, the caller adds "loader" to the class' initiating
570 // loader list, which should prevent us from going through this again.
571 //
572 // This call is unnecessary if "loader" and "component_type_->class_loader_"
573 // are the same, because our caller (FindClass) just did the
574 // lookup. (Even if we get this wrong we still have correct behavior,
575 // because we effectively do this lookup again when we add the new
576 // class to the hash table --- necessary because of possible races with
577 // other threads.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700578 if (class_loader != component_type_->class_loader_) {
579 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
580 if (new_class != NULL) {
581 return new_class;
582 }
583 }
584
Brian Carlstrombe977852011-07-19 14:54:54 -0700585 // Fill out the fields in the Class.
586 //
587 // It is possible to execute some methods against arrays, because
588 // all arrays are subclasses of java_lang_Object_, so we need to set
589 // up a vtable. We can just point at the one in java_lang_Object_.
590 //
591 // Array classes are simple enough that we don't need to do a full
592 // link step.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700593
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700594 Class* new_class = NULL;
595 if (!init_done_ && descriptor == "[Ljava/lang/Object;") {
596 new_class = class_roots_->Get(kObjectArrayClass);
597 CHECK(new_class);
598 }
599 if (new_class == NULL) {
600 new_class = AllocClass();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700601 if (new_class == NULL) {
602 return NULL;
603 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700604 }
605 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
606 descriptor.size());
607 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
608 new_class->descriptor_alloc_->size());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700609 Class* java_lang_Object = class_roots_->Get(kJavaLangObject);
610 new_class->super_class_ = java_lang_Object;
611 new_class->vtable_ = java_lang_Object->vtable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700612 new_class->primitive_type_ = Class::kPrimNot;
613 new_class->component_type_ = component_type_;
614 new_class->class_loader_ = component_type_->class_loader_;
615 new_class->array_rank_ = array_rank;
616 new_class->status_ = Class::kStatusInitialized;
Brian Carlstrombe977852011-07-19 14:54:54 -0700617 // don't need to set new_class->object_size_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700618
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700619
Brian Carlstrombe977852011-07-19 14:54:54 -0700620 // All arrays have java/lang/Cloneable and java/io/Serializable as
621 // interfaces. We need to set that up here, so that stuff like
622 // "instanceof" works right.
623 //
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700624 // Note: The GC could run during the call to FindSystemClass,
Brian Carlstrombe977852011-07-19 14:54:54 -0700625 // so we need to make sure the class object is GC-valid while we're in
626 // there. Do this by clearing the interface list so the GC will just
627 // think that the entries are null.
Brian Carlstrombe977852011-07-19 14:54:54 -0700628
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700629
630 // Use the single, global copies of "interfaces" and "iftable"
631 // (remember not to free them for arrays).
632 DCHECK(array_interfaces_ != NULL);
633 new_class->interfaces_ = array_interfaces_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700634 new_class->iftable_count_ = 2;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700635 DCHECK(array_iftable_ != NULL);
636 new_class->iftable_ = array_iftable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700637
Brian Carlstrombe977852011-07-19 14:54:54 -0700638 // Inherit access flags from the component type. Arrays can't be
639 // used as a superclass or interface, so we want to add "final"
640 // and remove "interface".
641 //
642 // Don't inherit any non-standard flags (e.g., kAccFinal)
643 // from component_type_. We assume that the array class does not
644 // override finalize().
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700645 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
646 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
647
648 if (InsertClass(new_class)) {
649 return new_class;
650 }
Brian Carlstrombe977852011-07-19 14:54:54 -0700651 // Another thread must have loaded the class after we
652 // started but before we finished. Abandon what we've
653 // done.
654 //
655 // (Yes, this happens.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700656
Brian Carlstrombe977852011-07-19 14:54:54 -0700657 // Grab the winning class.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700658 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
659 DCHECK(other_class != NULL);
660 return other_class;
661}
662
663Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700664 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700665 case 'B':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700666 DCHECK(class_roots_->Get(kPrimitiveByte) != NULL);
667 return class_roots_->Get(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700668 case 'C':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700669 DCHECK(class_roots_->Get(kPrimitiveChar) != NULL);
670 return class_roots_->Get(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700671 case 'D':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700672 DCHECK(class_roots_->Get(kPrimitiveDouble) != NULL);
673 return class_roots_->Get(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700674 case 'F':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700675 DCHECK(class_roots_->Get(kPrimitiveFloat) != NULL);
676 return class_roots_->Get(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700677 case 'I':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700678 DCHECK(class_roots_->Get(kPrimitiveInt) != NULL);
679 return class_roots_->Get(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700680 case 'J':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700681 DCHECK(class_roots_->Get(kPrimitiveLong) != NULL);
682 return class_roots_->Get(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700683 case 'S':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700684 DCHECK(class_roots_->Get(kPrimitiveShort) != NULL);
685 return class_roots_->Get(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700686 case 'Z':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700687 DCHECK(class_roots_->Get(kPrimitiveBoolean) != NULL);
688 return class_roots_->Get(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700689 case 'V':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700690 DCHECK(class_roots_->Get(kPrimitiveVoid) != NULL);
691 return class_roots_->Get(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700692 case 'L':
693 case '[':
694 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700695 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700696 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700697 };
698 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700699}
700
701bool ClassLinker::InsertClass(Class* klass) {
702 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700703 const StringPiece& key = klass->GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700704 bool success = classes_.insert(std::make_pair(key, klass)).second;
705 // TODO: release classes_lock_
706 return success;
707}
708
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700709Class* ClassLinker::LookupClass(const StringPiece& descriptor, Object* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700710 // TODO: acquire classes_lock_
711 Table::iterator it = classes_.find(descriptor);
712 // TODO: release classes_lock_
713 if (it == classes_.end()) {
714 return NULL;
715 } else {
716 return (*it).second;
717 }
718}
719
720bool ClassLinker::InitializeClass(Class* klass) {
721 CHECK(klass->GetStatus() == Class::kStatusResolved ||
722 klass->GetStatus() == Class::kStatusError);
723
Carl Shapirob5573532011-07-12 18:22:59 -0700724 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700725
726 {
727 ObjectLock lock(klass);
728
729 if (klass->GetStatus() < Class::kStatusVerified) {
730 if (klass->IsErroneous()) {
731 LG << "re-initializing failed class"; // TODO: throw
732 return false;
733 }
734
735 CHECK(klass->GetStatus() == Class::kStatusResolved);
736
737 klass->status_ = Class::kStatusVerifying;
738 if (!DexVerify::VerifyClass(klass)) {
739 LG << "Verification failed"; // TODO: ThrowVerifyError
740 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700741 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
742 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700743 klass->SetStatus(Class::kStatusError);
744 return false;
745 }
746
747 klass->SetStatus(Class::kStatusVerified);
748 }
749
750 if (klass->status_ == Class::kStatusInitialized) {
751 return true;
752 }
753
754 while (klass->status_ == Class::kStatusInitializing) {
755 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700756 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700757 LG << "recursive <clinit>";
758 return true;
759 }
760
761 CHECK(!self->IsExceptionPending());
762
763 lock.Wait(); // TODO: check for interruption
764
765 // When we wake up, repeat the test for init-in-progress. If
766 // there's an exception pending (only possible if
767 // "interruptShouldThrow" was set), bail out.
768 if (self->IsExceptionPending()) {
769 CHECK(false);
770 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
771 klass->SetStatus(Class::kStatusError);
772 return false;
773 }
774 if (klass->GetStatus() == Class::kStatusInitializing) {
775 continue;
776 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700777 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700778 klass->GetStatus() == Class::kStatusError);
779 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700780 // The caller wants an exception, but it was thrown in a
781 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700782 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
783 return false;
784 }
785 return true; // otherwise, initialized
786 }
787
788 // see if we failed previously
789 if (klass->IsErroneous()) {
790 // might be wise to unlock before throwing; depends on which class
791 // it is that we have locked
792
793 // TODO: throwEarlierClassFailure(klass);
794 return false;
795 }
796
797 if (!ValidateSuperClassDescriptors(klass)) {
798 klass->SetStatus(Class::kStatusError);
799 return false;
800 }
801
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700802 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700803
Carl Shapirob5573532011-07-12 18:22:59 -0700804 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700805 klass->status_ = Class::kStatusInitializing;
806 }
807
808 if (!InitializeSuperClass(klass)) {
809 return false;
810 }
811
812 InitializeStaticFields(klass);
813
814 Method* clinit = klass->FindDirectMethodLocally("<clinit>", "()V");
815 if (clinit != NULL) {
816 } else {
817 // JValue unused;
818 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700819 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700820 }
821
822 {
823 ObjectLock lock(klass);
824
825 if (self->IsExceptionPending()) {
826 klass->SetStatus(Class::kStatusError);
827 } else {
828 klass->SetStatus(Class::kStatusInitialized);
829 }
830 lock.NotifyAll();
831 }
832
833 return true;
834}
835
836bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
837 if (klass->IsInterface()) {
838 return true;
839 }
840 // begin with the methods local to the superclass
841 if (klass->HasSuperClass() &&
842 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
843 const Class* super = klass->GetSuperClass();
844 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
845 const Method* method = klass->GetVirtualMethod(i);
846 if (method != super->GetVirtualMethod(i) &&
847 !HasSameMethodDescriptorClasses(method, super, klass)) {
848 LG << "Classes resolve differently in superclass";
849 return false;
850 }
851 }
852 }
853 for (size_t i = 0; i < klass->iftable_count_; ++i) {
854 const InterfaceEntry* iftable = &klass->iftable_[i];
855 Class* interface = iftable->GetClass();
856 if (klass->GetClassLoader() != interface->GetClassLoader()) {
857 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
858 uint32_t vtable_index = iftable->method_index_array_[j];
859 const Method* method = klass->GetVirtualMethod(vtable_index);
860 if (!HasSameMethodDescriptorClasses(method, interface,
861 method->GetClass())) {
862 LG << "Classes resolve differently in interface"; // TODO: LinkageError
863 return false;
864 }
865 }
866 }
867 }
868 return true;
869}
870
871bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700872 const Class* klass1,
873 const Class* klass2) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700874 const DexFile* dex_file = FindDexFile(method->GetClass()->GetDexCache());
875 const DexFile::ProtoId& proto_id = dex_file->GetProtoId(method->proto_idx_);
876 DexFile::ParameterIterator *it;
877 for (it = dex_file->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700878 const char* descriptor = it->GetDescriptor();
879 if (descriptor == NULL) {
880 break;
881 }
882 if (descriptor[0] == 'L' || descriptor[0] == '[') {
883 // Found a non-primitive type.
884 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
885 return false;
886 }
887 }
888 }
889 // Check the return type
Brian Carlstromf615a612011-07-23 12:50:34 -0700890 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700891 if (descriptor[0] == 'L' || descriptor[0] == '[') {
892 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
893 return false;
894 }
895 }
896 return true;
897}
898
899// Returns true if classes referenced by the descriptor are the
900// same classes in klass1 as they are in klass2.
901bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700902 const Class* klass1,
903 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700904 CHECK(descriptor != NULL);
905 CHECK(klass1 != NULL);
906 CHECK(klass2 != NULL);
907#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700908 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700909 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700910 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700911 // TODO: found2 == NULL
912 // TODO: lookup found1 in initiating loader list
913 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700914 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700915 if (found1 == found2) {
916 return true;
917 } else {
918 return false;
919 }
920 }
921#endif
922 return true;
923}
924
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700925bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700926 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
927 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
928 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
929 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700930
931 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstromf615a612011-07-23 12:50:34 -0700932 const DexFile::TypeList* type_list1 = dex1->GetProtoParameters(proto1);
933 const DexFile::TypeList* type_list2 = dex2->GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700934 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
935 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
936 if (arity1 != arity2) {
937 return false;
938 }
939
940 for (size_t i = 0; i < arity1; ++i) {
941 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
942 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700943 const char* type1 = dex1->dexStringByTypeIdx(type_idx1);
944 const char* type2 = dex2->dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700945 if (strcmp(type1, type2) != 0) {
946 return false;
947 }
948 }
949
950 return true;
951}
952
953bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700954 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
955 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
956 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
957 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
958 const char* type1 = dex1->dexStringByTypeIdx(proto1.return_type_idx_);
959 const char* type2 = dex2->dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700960 return (strcmp(type1, type2) == 0);
961}
962
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700963bool ClassLinker::InitializeSuperClass(Class* klass) {
964 CHECK(klass != NULL);
965 // TODO: assert klass lock is acquired
966 if (!klass->IsInterface() && klass->HasSuperClass()) {
967 Class* super_class = klass->GetSuperClass();
968 if (super_class->GetStatus() != Class::kStatusInitialized) {
969 CHECK(!super_class->IsInterface());
970 klass->MonitorExit();
971 bool super_initialized = InitializeClass(super_class);
972 klass->MonitorEnter();
973 // TODO: check for a pending exception
974 if (!super_initialized) {
975 klass->SetStatus(Class::kStatusError);
976 klass->NotifyAll();
977 return false;
978 }
979 }
980 }
981 return true;
982}
983
984void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700985 size_t num_static_fields = klass->NumStaticFields();
986 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700987 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700988 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700989 DexCache* dex_cache = klass->GetDexCache();
990 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700991 return;
992 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700993 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstromf615a612011-07-23 12:50:34 -0700994 const DexFile* dex_file = FindDexFile(dex_cache);
995 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
996 CHECK(dex_class_def != NULL);
997 const byte* addr = dex_file->GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700998 size_t array_size = DecodeUnsignedLeb128(&addr);
999 for (size_t i = 0; i < array_size; ++i) {
1000 StaticField* field = klass->GetStaticField(i);
1001 JValue value;
Brian Carlstromf615a612011-07-23 12:50:34 -07001002 DexFile::ValueType type = dex_file->ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001003 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001004 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001005 field->SetByte(value.b);
1006 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001007 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001008 field->SetShort(value.s);
1009 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001010 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001011 field->SetChar(value.c);
1012 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001013 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001014 field->SetInt(value.i);
1015 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001016 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001017 field->SetLong(value.j);
1018 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001019 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001020 field->SetFloat(value.f);
1021 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001022 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001023 field->SetDouble(value.d);
1024 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001025 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001026 uint32_t string_idx = value.i;
1027 String* resolved = ResolveString(klass, string_idx);
1028 field->SetObject(resolved);
1029 break;
1030 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001031 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001032 field->SetBoolean(value.z);
1033 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001034 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001035 field->SetObject(value.l);
1036 break;
1037 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001038 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001039 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001040 }
1041}
1042
Brian Carlstromf615a612011-07-23 12:50:34 -07001043bool ClassLinker::LinkClass(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001044 CHECK(klass->status_ == Class::kStatusIdx ||
1045 klass->status_ == Class::kStatusLoaded);
1046 if (klass->status_ == Class::kStatusIdx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001047 if (!LinkInterfaces(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001048 return false;
1049 }
1050 }
1051 if (!LinkSuperClass(klass)) {
1052 return false;
1053 }
1054 if (!LinkMethods(klass)) {
1055 return false;
1056 }
1057 if (!LinkInstanceFields(klass)) {
1058 return false;
1059 }
1060 CreateReferenceOffsets(klass);
1061 CHECK_EQ(klass->status_, Class::kStatusLoaded);
1062 klass->status_ = Class::kStatusResolved;
1063 return true;
1064}
1065
Brian Carlstromf615a612011-07-23 12:50:34 -07001066bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001067 // Mark the class as loaded.
1068 klass->status_ = Class::kStatusLoaded;
Brian Carlstromf615a612011-07-23 12:50:34 -07001069 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1070 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001071 if (super_class == NULL) {
1072 LG << "Failed to resolve superclass";
1073 return false;
1074 }
1075 klass->super_class_ = super_class; // TODO: write barrier
1076 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001077 if (klass->NumInterfaces() > 0) {
1078 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1079 uint32_t idx = klass->interfaces_idx_[i];
1080 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1081 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001082 LG << "Failed to resolve interface";
1083 return false;
1084 }
1085 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001086 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087 LG << "Inaccessible interface";
1088 return false;
1089 }
1090 }
1091 }
1092 return true;
1093}
1094
1095bool ClassLinker::LinkSuperClass(Class* klass) {
1096 CHECK(!klass->IsPrimitive());
1097 const Class* super = klass->GetSuperClass();
1098 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1099 if (super != NULL) {
1100 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1101 return false;
1102 }
1103 // TODO: clear finalize attribute
1104 return true;
1105 }
1106 if (super == NULL) {
1107 LG << "No superclass defined"; // TODO: LinkageError
1108 return false;
1109 }
1110 // Verify
1111 if (super->IsFinal()) {
1112 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
1113 return false;
1114 }
1115 if (super->IsInterface()) {
1116 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
1117 return false;
1118 }
1119 if (!klass->CanAccess(super)) {
1120 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
1121 return false;
1122 }
1123 return true;
1124}
1125
1126// Populate the class vtable and itable.
1127bool ClassLinker::LinkMethods(Class* klass) {
1128 if (klass->IsInterface()) {
1129 // No vtable.
1130 size_t count = klass->NumVirtualMethods();
1131 if (!IsUint(16, count)) {
1132 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1133 return false;
1134 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001135 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001136 klass->GetVirtualMethod(i)->method_index_ = i;
1137 }
1138 } else {
1139 // Link virtual method tables
1140 LinkVirtualMethods(klass);
1141
1142 // Link interface method tables
1143 LinkInterfaceMethods(klass);
1144
1145 // Insert stubs.
1146 LinkAbstractMethods(klass);
1147 }
1148 return true;
1149}
1150
1151bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001152 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001153 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001154 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1155 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001156 // TODO: do not assign to the vtable field until it is fully constructed.
1157 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001158 // See if any of our virtual methods override the superclass.
1159 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1160 Method* local_method = klass->GetVirtualMethod(i);
1161 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001162 for (; j < actual_count; ++j) {
1163 Method* super_method = klass->vtable_->Get(j);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001164 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001165 // Verify
1166 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001167 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001168 return false;
1169 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001170 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001171 local_method->method_index_ = j;
1172 break;
1173 }
1174 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001175 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001176 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001177 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001178 local_method->method_index_ = actual_count;
1179 actual_count += 1;
1180 }
1181 }
1182 if (!IsUint(16, actual_count)) {
1183 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1184 return false;
1185 }
1186 CHECK_LE(actual_count, max_count);
1187 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001188 // TODO: do not assign to the vtable field until it is fully constructed.
1189 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001190 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001191 } else {
1192 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001193 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1194 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1195 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001196 LG << "Too many methods"; // TODO: VirtualMachineError
1197 return false;
1198 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001199 // TODO: do not assign to the vtable field until it is fully constructed.
1200 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1201 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001202 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001203 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1204 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001205 }
1206 return true;
1207}
1208
1209bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1210 int pool_offset = 0;
1211 int pool_size = 0;
1212 int miranda_count = 0;
1213 int miranda_alloc = 0;
1214 size_t super_ifcount;
1215 if (klass->HasSuperClass()) {
1216 super_ifcount = klass->GetSuperClass()->iftable_count_;
1217 } else {
1218 super_ifcount = 0;
1219 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001220 size_t ifcount = super_ifcount;
1221 ifcount += klass->NumInterfaces();
1222 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1223 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001224 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001225 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001226 DCHECK(klass->iftable_count_ == 0);
1227 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001228 return true;
1229 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001230 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1231 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001232 if (super_ifcount != 0) {
1233 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1234 sizeof(InterfaceEntry) * super_ifcount);
1235 }
1236 // Flatten the interface inheritance hierarchy.
1237 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001238 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1239 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001240 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001241 if (!interf->IsInterface()) {
1242 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1243 return false;
1244 }
1245 klass->iftable_[idx++].SetClass(interf);
1246 for (size_t j = 0; j < interf->iftable_count_; j++) {
1247 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1248 }
1249 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001250 CHECK_EQ(idx, ifcount);
1251 klass->iftable_count_ = ifcount;
1252 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001253 return true;
1254 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001255 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001256 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1257 }
1258 if (pool_size == 0) {
1259 return true;
1260 }
1261 klass->ifvi_pool_count_ = pool_size;
1262 klass->ifvi_pool_ = new uint32_t[pool_size];
1263 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001264 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001265 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1266 Class* interface = klass->iftable_[i].GetClass();
1267 pool_offset += interface->NumVirtualMethods(); // end here
1268 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1269 Method* interface_method = interface->GetVirtualMethod(j);
1270 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001271 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
1272 if (HasSameNameAndPrototype(interface_method, klass->vtable_->Get(k))) {
1273 if (!klass->vtable_->Get(k)->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001274 LG << "Implementation not public";
1275 return false;
1276 }
1277 klass->iftable_[i].method_index_array_[j] = k;
1278 break;
1279 }
1280 }
1281 if (k < 0) {
1282 if (miranda_count == miranda_alloc) {
1283 miranda_alloc += 8;
1284 if (miranda_list.empty()) {
1285 miranda_list.resize(miranda_alloc);
1286 } else {
1287 miranda_list.resize(miranda_alloc);
1288 }
1289 }
1290 int mir;
1291 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001292 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001293 break;
1294 }
1295 }
1296 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001297 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001298 if (mir == miranda_count) {
1299 miranda_list[miranda_count++] = interface_method;
1300 }
1301 }
1302 }
1303 }
1304 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001305 int old_method_count = klass->NumVirtualMethods();
1306 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001307 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001308
1309 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001310 int old_vtable_count = klass->vtable_->GetLength();
1311 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001312 // TODO: do not assign to the vtable field until it is fully constructed.
1313 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001314
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001315 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001316 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001317 memcpy(meth, miranda_list[i], sizeof(Method));
1318 meth->klass_ = klass;
1319 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001320 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1321 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001322 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001323 }
1324 }
1325 return true;
1326}
1327
1328void ClassLinker::LinkAbstractMethods(Class* klass) {
1329 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1330 Method* method = klass->GetVirtualMethod(i);
1331 if (method->IsAbstract()) {
1332 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1333 }
1334 }
1335}
1336
1337bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001338 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001339 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001340 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001341 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001342 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001343 }
1344 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001345 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001346 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001347 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001348 InstanceField* pField = klass->GetInstanceField(i);
1349 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001350 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001351 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1352 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001353 char rc = refField->GetType();
1354 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001355 klass->SetInstanceField(i, refField);
1356 klass->SetInstanceField(j, pField);
1357 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001359 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001360 break;
1361 }
1362 }
1363 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001364 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001365 }
1366 if (c != '[' && c != 'L') {
1367 break;
1368 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001369 pField->SetOffset(field_offset);
1370 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001371 }
1372
1373 // Now we want to pack all of the double-wide fields together. If
1374 // we're not aligned, though, we want to shuffle one 32-bit field
1375 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001376 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001377 InstanceField* pField = klass->GetInstanceField(i);
1378 char c = pField->GetType();
1379
1380 if (c != 'J' && c != 'D') {
1381 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001382 DCHECK(c != '[');
1383 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001384 pField->SetOffset(field_offset);
1385 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001386 i++;
1387 } else {
1388 // Next field is 64-bit, so search for a 32-bit field we can
1389 // swap into it.
1390 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001391 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1392 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001393 char rc = singleField->GetType();
1394 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001395 klass->SetInstanceField(i, singleField);
1396 klass->SetInstanceField(j, pField);
1397 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001398 pField->SetOffset(field_offset);
1399 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001400 found = true;
1401 i++;
1402 break;
1403 }
1404 }
1405 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001406 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001407 }
1408 }
1409 }
1410
1411 // Alignment is good, shuffle any double-wide fields forward, and
1412 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001413 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001414 for ( ; i < klass->NumInstanceFields(); i++) {
1415 InstanceField* pField = klass->GetInstanceField(i);
1416 char c = pField->GetType();
1417 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001418 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1419 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001420 char rc = doubleField->GetType();
1421 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001422 klass->SetInstanceField(i, doubleField);
1423 klass->SetInstanceField(j, pField);
1424 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001425 c = rc;
1426 break;
1427 }
1428 }
1429 } else {
1430 // This is a double-wide field, leave it be.
1431 }
1432
Brian Carlstroma0808032011-07-18 00:39:23 -07001433 pField->SetOffset(field_offset);
1434 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001435 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001436 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001437 }
1438
1439#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001440 // Make sure that all reference fields appear before
1441 // non-reference fields, and all double-wide fields are aligned.
1442 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001443 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001444 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001445 char c = pField->GetType();
1446
1447 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001448 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001449 }
1450
1451 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001452 if (!seen_non_ref) {
1453 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001454 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001455 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001456 } else {
1457 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001458 }
1459 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001460 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001461 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001462 }
1463#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001464 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001465 return true;
1466}
1467
1468// Set the bitmap of reference offsets, refOffsets, from the ifields
1469// list.
1470void ClassLinker::CreateReferenceOffsets(Class* klass) {
1471 uint32_t reference_offsets = 0;
1472 if (klass->HasSuperClass()) {
1473 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1474 }
1475 // If our superclass overflowed, we don't stand a chance.
1476 if (reference_offsets != CLASS_WALK_SUPER) {
1477 // All of the fields that contain object references are guaranteed
1478 // to be at the beginning of the ifields list.
1479 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1480 // Note that, per the comment on struct InstField, f->byteOffset
1481 // is the offset from the beginning of obj, not the offset into
1482 // obj->instanceData.
1483 const InstanceField* field = klass->GetInstanceField(i);
1484 size_t byte_offset = field->GetOffset();
1485 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001486 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001487 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1488 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001489 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001490 reference_offsets |= new_bit;
1491 } else {
1492 reference_offsets = CLASS_WALK_SUPER;
1493 break;
1494 }
1495 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001496 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001497 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001498}
1499
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001500Class* ClassLinker::ResolveClass(const Class* referrer,
1501 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001502 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001503 DexCache* dex_cache = referrer->GetDexCache();
1504 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001505 if (resolved != NULL) {
1506 return resolved;
1507 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001508 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001509 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001510 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001511 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001512 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001513 }
1514 if (resolved != NULL) {
1515 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001516 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001517 if (check->GetClassLoader() != NULL) {
1518 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1519 return NULL;
1520 }
1521 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001522 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001523 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001524 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001525 }
1526 return resolved;
1527}
1528
1529Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1530 /*MethodType*/ int method_type) {
1531 CHECK(false);
1532 return NULL;
1533}
1534
Carl Shapiro69759ea2011-07-21 18:13:35 -07001535String* ClassLinker::ResolveString(const Class* referring,
1536 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001537 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1538 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001539 int32_t utf16_length = dex_file->GetStringLength(string_id);
1540 const char* utf8_data = dex_file->GetStringData(string_id);
1541 String* new_string = AllocStringFromModifiedUtf8(utf16_length, utf8_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001542 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001543 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001544 return new_string;
1545}
1546
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001547} // namespace art