blob: 09454d872d1f00b79444346a49674eea17ea9ab0 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: cshapiro@google.com (Carl Shapiro)
3
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07004#include "class_linker.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005
6#include <vector>
7#include <utility>
8
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "casts.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070010#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "dex_verifier.h"
12#include "heap.h"
13#include "logging.h"
14#include "monitor.h"
15#include "object.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070016#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "scoped_ptr.h"
18#include "thread.h"
19#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
22
Carl Shapiro2ed144c2011-07-26 16:52:08 -070023ClassLinker* ClassLinker::Create(const std::vector<DexFile*>& boot_class_path) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070024 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025 class_linker->Init(boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070026 // TODO: check for failure during initialization
27 return class_linker.release();
28}
29
Carl Shapiro2ed144c2011-07-26 16:52:08 -070030void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070031 init_done_ = false;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070033 // java_lang_Class comes first, its needed for AllocClass
34 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(Class)));
35 CHECK(java_lang_Class != NULL);
36 java_lang_Class->descriptor_ = "Ljava/lang/Class;";
37 java_lang_Class->object_size_ = sizeof(Class);
38 java_lang_Class->klass_ = java_lang_Class;
39 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070040
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070041 // java_lang_Object comes next so that object_array_class can be created
42 Class* java_lang_Object = AllocClass(java_lang_Class);
43 CHECK(java_lang_Object != NULL);
44 java_lang_Object->descriptor_ = "Ljava/lang/Object;";
45 // backfill Object as the super class of Class
46 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstroma0808032011-07-18 00:39:23 -070047
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070048 // object_array_class is for root_classes to provide the storage for these classes
49 Class* object_array_class = AllocClass(java_lang_Class);
50 CHECK(object_array_class != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -070051
Jesse Wilson8989d992011-08-02 13:39:42 -070052 // string and char[] are necessary so that FindClass can assign names to members
Jesse Wilson14150742011-07-29 19:04:44 -040053 Class* java_lang_String = AllocClass(java_lang_Class);
54 CHECK(java_lang_String != NULL);
55 java_lang_String->object_size_ = sizeof(String);
Jesse Wilson8989d992011-08-02 13:39:42 -070056 Class* char_array_class = AllocClass(java_lang_Class);
57 CHECK(char_array_class != NULL);
Jesse Wilson14150742011-07-29 19:04:44 -040058
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070059 // create storage for root classes, save away our work so far
60 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
61 class_roots_->Set(kJavaLangClass, java_lang_Class);
62 class_roots_->Set(kJavaLangObject, java_lang_Object);
63 class_roots_->Set(kObjectArrayClass, object_array_class);
Jesse Wilson14150742011-07-29 19:04:44 -040064 class_roots_->Set(kJavaLangString, java_lang_String);
Jesse Wilson8989d992011-08-02 13:39:42 -070065 class_roots_->Set(kCharArrayClass, char_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070066 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -070067
Jesse Wilson8989d992011-08-02 13:39:42 -070068 String::InitClasses(java_lang_String, char_array_class);
69 // Now AllocString* can be used
70
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070071 // setup boot_class_path_ now that we can use AllocObjectArray to
72 // DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -070073 for (size_t i = 0; i != boot_class_path.size(); ++i) {
74 AppendToBootClassPath(boot_class_path[i]);
75 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070076 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -070077
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070078 // run Object and Class to setup their dex_cache_ fields and register them in classes_.
79 // we also override their object_size_ values to accommodate the extra C++ fields.
80 Class* Object_class = FindSystemClass(java_lang_Object->GetDescriptor());
81 CHECK_EQ(java_lang_Object, Object_class);
82 CHECK_LE(java_lang_Object->object_size_, sizeof(Object));
83 java_lang_Object->object_size_ = sizeof(Object);
84 Class* Class_class = FindSystemClass(java_lang_Class->GetDescriptor());
85 CHECK_EQ(java_lang_Class, Class_class);
86 CHECK_LE(java_lang_Class->object_size_, sizeof(Class));
87 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom913af1b2011-07-23 21:41:13 -070088
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070089 // set special sizes for these C++ extended classes (Field, Method, String).
90 // we also remember them in class_roots_ to construct them within ClassLinker
91 Class* java_lang_reflect_Field = FindSystemClass("Ljava/lang/reflect/Field;");
92 CHECK(java_lang_reflect_Field != NULL);
93 CHECK_LE(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField)));
94 java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField));
95 class_roots_->Set(kJavaLangReflectField, java_lang_reflect_Field);
96
97 Class* java_lang_reflect_Method = FindSystemClass("Ljava/lang/reflect/Method;");
98 CHECK(java_lang_reflect_Method != NULL);
99 CHECK_LE(java_lang_reflect_Method->object_size_, sizeof(Method));
100 java_lang_reflect_Method->object_size_ = sizeof(Method);
101 class_roots_->Set(kJavaLangReflectMethod, java_lang_reflect_Method);
102
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700103 Class* String_class = FindSystemClass("Ljava/lang/String;");
104 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700105 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
106 java_lang_String->object_size_ = sizeof(String);
107 class_roots_->Set(kJavaLangString, java_lang_String);
108
109 // Setup a single, global copy of "interfaces" and "iftable" for
110 // reuse across array classes
111 Class* java_lang_Cloneable = AllocClass();
112 CHECK(java_lang_Cloneable != NULL);
113 java_lang_Cloneable->descriptor_ = "Ljava/lang/Cloneable;";
114
115 Class* java_io_Serializable = AllocClass();
116 CHECK(java_io_Serializable != NULL);
117 java_io_Serializable->descriptor_ = "Ljava/io/Serializable;";
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700118
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700119 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700120 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700121 array_interfaces_->Set(0, java_lang_Cloneable);
122 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700123
124 // We assume that Cloneable/Serializable don't have superinterfaces --
125 // normally we'd have to crawl up and explicitly list all of the
126 // supers as well. These interfaces don't have any methods, so we
127 // don't have to worry about the ifviPool either.
128 array_iftable_ = new InterfaceEntry[2];
129 CHECK(array_iftable_ != NULL);
130 memset(array_iftable_, 0, sizeof(InterfaceEntry) * 2);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700131 array_iftable_[0].SetClass(array_interfaces_->Get(0));
132 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700133 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700134
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700135 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700136 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
137 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700138
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700139 // Setup the primitive type classes.
140 class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B"));
141 class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C"));
142 class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D"));
143 class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F"));
144 class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I"));
145 class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J"));
146 class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S"));
147 class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
148 class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V"));
149 // now we can use FindSystemClass for anything, including for "[C"
150
Jesse Wilson8989d992011-08-02 13:39:42 -0700151 // run char[] through FindClass to complete initialization
152 Class* found_char_array_class = FindSystemClass("[C");
153 CHECK_EQ(char_array_class, found_char_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700154
155 // ensure all class_roots_ were initialized
156 for (size_t i = 0; i < kClassRootsMax; i++) {
157 CHECK(class_roots_->Get(i) != NULL);
158 }
159
160 // disable the slow paths in FindClass and CreatePrimitiveClass now
161 // that Object, Class, and Object[] are setup
162 init_done_ = true;
163}
164
Brian Carlstromb88e9442011-07-28 15:15:51 -0700165void ClassLinker::VisitRoots(RootVistor* root_visitor, void* arg) {
166 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700167
168 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700169 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700170 }
171
172 // TODO: acquire classes_lock_
173 typedef Table::const_iterator It; // TODO: C++0x auto
174 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700175 root_visitor(it->second, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700176 }
177 // TODO: release classes_lock_
178
Brian Carlstromb88e9442011-07-28 15:15:51 -0700179 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700180}
181
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700182DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700183 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700184}
185
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700186Class* ClassLinker::AllocClass(Class* java_lang_Class) {
187 return down_cast<Class*>(Object::Alloc(java_lang_Class));
188}
189
190Class* ClassLinker::AllocClass() {
191 return AllocClass(class_roots_->Get(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700192}
193
194StaticField* ClassLinker::AllocStaticField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700195 return down_cast<StaticField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700196 sizeof(StaticField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700197}
198
199InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700200 return down_cast<InstanceField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700201 sizeof(InstanceField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700202}
203
204Method* ClassLinker::AllocMethod() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700205 return down_cast<Method*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectMethod),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700206 sizeof(Method)));
Carl Shapiro565f5072011-07-10 13:39:43 -0700207}
208
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700209Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700210 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700211 const DexFile* dex_file) {
Carl Shapirob5573532011-07-12 18:22:59 -0700212 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700213 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700214 CHECK(!self->IsExceptionPending());
215 // Find the class in the loaded classes table.
216 Class* klass = LookupClass(descriptor, class_loader);
217 if (klass == NULL) {
218 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700219 if (descriptor[0] == '[') {
Brian Carlstromf615a612011-07-23 12:50:34 -0700220 return CreateArrayClass(descriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700221 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700222 ClassPathEntry pair;
Brian Carlstromf615a612011-07-23 12:50:34 -0700223 if (dex_file == NULL) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700224 pair = FindInBootClassPath(descriptor);
225 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700226 pair.first = dex_file;
227 pair.second = dex_file->FindClassDef(descriptor);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700228 }
229 if (pair.second == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700230 LG << "Class " << descriptor << " not found"; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700231 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700233 const DexFile* dex_file = pair.first;
234 const DexFile::ClassDef* dex_class_def = pair.second;
235 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700237 if (!init_done_) {
238 // finish up init of hand crafted class_roots_
239 if (descriptor == "Ljava/lang/Object;") {
240 klass = class_roots_->Get(kJavaLangObject);
241 } else if (descriptor == "Ljava/lang/Class;") {
242 klass = class_roots_->Get(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400243 } else if (descriptor == "Ljava/lang/String;") {
244 klass = class_roots_->Get(kJavaLangString);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700245 } else {
246 klass = AllocClass();
247 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700248 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700249 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700250 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700251 klass->dex_cache_ = dex_cache;
Brian Carlstromf615a612011-07-23 12:50:34 -0700252 LoadClass(*dex_file, *dex_class_def, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700253 // Check for a pending exception during load
254 if (self->IsExceptionPending()) {
255 // TODO: free native allocations in klass
256 return NULL;
257 }
258 {
259 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700260 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700261 // Add the newly loaded class to the loaded classes table.
262 bool success = InsertClass(klass);
263 if (!success) {
264 // We may fail to insert if we raced with another thread.
265 klass->clinit_thread_id_ = 0;
266 // TODO: free native allocations in klass
267 klass = LookupClass(descriptor, class_loader);
268 CHECK(klass != NULL);
269 } else {
270 // Link the class.
Brian Carlstromf615a612011-07-23 12:50:34 -0700271 if (!LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700272 // Linking failed.
273 // TODO: CHECK(self->IsExceptionPending());
274 lock.NotifyAll();
275 return NULL;
276 }
277 }
278 }
279 }
280 // Link the class if it has not already been linked.
281 if (!klass->IsLinked() && !klass->IsErroneous()) {
282 ObjectLock lock(klass);
283 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700284 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700285 LG << "Recursive link"; // TODO: ClassCircularityError
286 return NULL;
287 }
288 // Wait for the pending initialization to complete.
289 while (!klass->IsLinked() && !klass->IsErroneous()) {
290 lock.Wait();
291 }
292 }
293 if (klass->IsErroneous()) {
294 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
295 return NULL;
296 }
297 // Return the loaded class. No exceptions should be pending.
298 CHECK(!self->IsExceptionPending());
299 return klass;
300}
301
Brian Carlstromf615a612011-07-23 12:50:34 -0700302void ClassLinker::LoadClass(const DexFile& dex_file,
303 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700304 Class* klass) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700305 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700306 CHECK(klass->dex_cache_ != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700307 const byte* class_data = dex_file.GetClassData(dex_class_def);
308 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700309
Brian Carlstromf615a612011-07-23 12:50:34 -0700310 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700311 CHECK(descriptor != NULL);
312
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700313 klass->klass_ = class_roots_->Get(kJavaLangClass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700314 klass->descriptor_.set(descriptor);
315 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700316 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700317 klass->class_loader_ = NULL; // TODO
318 klass->primitive_type_ = Class::kPrimNot;
319 klass->status_ = Class::kStatusIdx;
320
321 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700322 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700323
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700324 size_t num_static_fields = header.static_fields_size_;
325 size_t num_instance_fields = header.instance_fields_size_;
326 size_t num_direct_methods = header.direct_methods_size_;
327 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700328
Brian Carlstromf615a612011-07-23 12:50:34 -0700329 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700330
331 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700332 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700333
334 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700335 DCHECK(klass->sfields_ == NULL);
336 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700337 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700338 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700339 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700340 DexFile::Field dex_field;
341 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700342 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700343 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700344 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700345 }
346 }
347
348 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700349 DCHECK(klass->ifields_ == NULL);
350 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700351 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700352 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700353 uint32_t last_idx = 0;
354 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700355 DexFile::Field dex_field;
356 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700357 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700358 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700359 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700360 }
361 }
362
363 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700364 DCHECK(klass->direct_methods_ == NULL);
365 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700366 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700367 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700368 uint32_t last_idx = 0;
369 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700370 DexFile::Method dex_method;
371 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700372 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700373 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700374 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700375 // TODO: register maps
376 }
377 }
378
379 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700380 DCHECK(klass->virtual_methods_ == NULL);
381 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700382 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700383 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700384 uint32_t last_idx = 0;
385 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700386 DexFile::Method dex_method;
387 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700388 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700389 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700390 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700391 // TODO: register maps
392 }
393 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700394}
395
Brian Carlstromf615a612011-07-23 12:50:34 -0700396void ClassLinker::LoadInterfaces(const DexFile& dex_file,
397 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700398 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700399 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700400 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700401 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700402 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700403 DCHECK(klass->interfaces_idx_ == NULL);
404 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700405 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700406 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700407 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700408 }
409 }
410}
411
Brian Carlstromf615a612011-07-23 12:50:34 -0700412void ClassLinker::LoadField(const DexFile& dex_file,
413 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700414 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700415 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700416 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700417 dst->klass_ = klass;
Jesse Wilson14150742011-07-29 19:04:44 -0400418 dst->java_name_ = ResolveString(klass, field_id.name_idx_);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700419 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700420 dst->access_flags_ = src.access_flags_;
421}
422
Brian Carlstromf615a612011-07-23 12:50:34 -0700423void ClassLinker::LoadMethod(const DexFile& dex_file,
424 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700425 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700426 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700427 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700428 dst->klass_ = klass;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700429 dst->java_name_ = ResolveString(klass, method_id.name_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700430 dst->proto_idx_ = method_id.proto_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700431 dst->shorty_.set(dex_file.GetShorty(method_id.proto_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700432 dst->access_flags_ = src.access_flags_;
433
434 // TODO: check for finalize method
435
Brian Carlstromf615a612011-07-23 12:50:34 -0700436 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700437 if (code_item != NULL) {
438 dst->num_registers_ = code_item->registers_size_;
439 dst->num_ins_ = code_item->ins_size_;
440 dst->num_outs_ = code_item->outs_size_;
441 dst->insns_ = code_item->insns_;
442 } else {
443 uint16_t num_args = dst->NumArgRegisters();
444 if (!dst->IsStatic()) {
445 ++num_args;
446 }
447 dst->num_registers_ = dst->num_ins_ + num_args;
448 // TODO: native methods
449 }
450}
451
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700452ClassLinker::ClassPathEntry ClassLinker::FindInBootClassPath(const StringPiece& descriptor) {
453 for (size_t i = 0; i != boot_class_path_.size(); ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700454 const DexFile* dex_file = boot_class_path_[i];
Brian Carlstromf615a612011-07-23 12:50:34 -0700455 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
456 if (dex_class_def != NULL) {
457 return ClassPathEntry(dex_file, dex_class_def);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700458 }
459 }
Brian Carlstroma0808032011-07-18 00:39:23 -0700460 return ClassPathEntry(NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700461}
462
Brian Carlstromf615a612011-07-23 12:50:34 -0700463void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700464 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700465 boot_class_path_.push_back(dex_file);
466 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700467}
468
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700469void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700470 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700471 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700472 DexCache* dex_cache = AllocDexCache();
473 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700474 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
475 AllocObjectArray<Class>(dex_file->NumTypeIds()),
476 AllocObjectArray<Method>(dex_file->NumMethodIds()),
477 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700478 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700479}
480
Brian Carlstromf615a612011-07-23 12:50:34 -0700481const DexFile* ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700482 CHECK(dex_cache != NULL);
483 for (size_t i = 0; i != dex_caches_.size(); ++i) {
484 if (dex_caches_[i] == dex_cache) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700485 return dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700486 }
487 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700488 CHECK(false) << "Could not find DexFile";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700489 return NULL;
490}
491
Brian Carlstromf615a612011-07-23 12:50:34 -0700492DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
493 CHECK(dex_file != NULL);
494 for (size_t i = 0; i != dex_files_.size(); ++i) {
495 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700496 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700497 }
498 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700499 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700500 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700501}
502
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700503Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700504 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700505 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700506 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700507 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
508 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700509 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700510 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700511 bool success = InsertClass(klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700512 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700513 return klass;
514}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700515
Brian Carlstrombe977852011-07-19 14:54:54 -0700516// Create an array class (i.e. the class object for the array, not the
517// array itself). "descriptor" looks like "[C" or "[[[[B" or
518// "[Ljava/lang/String;".
519//
520// If "descriptor" refers to an array of primitives, look up the
521// primitive type's internally-generated class object.
522//
523// "loader" is the class loader of the class that's referring to us. It's
524// used to ensure that we're looking for the element type in the right
525// context. It does NOT become the class loader for the array class; that
526// always comes from the base element class.
527//
528// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700529Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
530 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700531 const DexFile* dex_file)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700532{
533 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700534
Brian Carlstrombe977852011-07-19 14:54:54 -0700535 // Identify the underlying element class and the array dimension depth.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700536 Class* component_type_ = NULL;
537 int array_rank;
538 if (descriptor[1] == '[') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700539 // array of arrays; keep descriptor and grab stuff from parent
Brian Carlstromf615a612011-07-23 12:50:34 -0700540 Class* outer = FindClass(descriptor.substr(1), class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700541 if (outer != NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700542 // want the base class, not "outer", in our component_type_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700543 component_type_ = outer->component_type_;
544 array_rank = outer->array_rank_ + 1;
545 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700546 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700547 }
548 } else {
549 array_rank = 1;
550 if (descriptor[1] == 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700551 // array of objects; strip off "[" and look up descriptor.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700552 const StringPiece subDescriptor = descriptor.substr(1);
Brian Carlstromf615a612011-07-23 12:50:34 -0700553 component_type_ = FindClass(subDescriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700554 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700555 // array of a primitive type
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700556 component_type_ = FindPrimitiveClass(descriptor[1]);
557 }
558 }
559
560 if (component_type_ == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700561 // failed
Brian Carlstrom8ecd08c2011-07-27 17:50:51 -0700562 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700563 return NULL;
564 }
565
Brian Carlstrombe977852011-07-19 14:54:54 -0700566 // See if the component type is already loaded. Array classes are
567 // always associated with the class loader of their underlying
568 // element type -- an array of Strings goes with the loader for
569 // java/lang/String -- so we need to look for it there. (The
570 // caller should have checked for the existence of the class
571 // before calling here, but they did so with *their* class loader,
572 // not the component type's loader.)
573 //
574 // If we find it, the caller adds "loader" to the class' initiating
575 // loader list, which should prevent us from going through this again.
576 //
577 // This call is unnecessary if "loader" and "component_type_->class_loader_"
578 // are the same, because our caller (FindClass) just did the
579 // lookup. (Even if we get this wrong we still have correct behavior,
580 // because we effectively do this lookup again when we add the new
581 // class to the hash table --- necessary because of possible races with
582 // other threads.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700583 if (class_loader != component_type_->class_loader_) {
584 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
585 if (new_class != NULL) {
586 return new_class;
587 }
588 }
589
Brian Carlstrombe977852011-07-19 14:54:54 -0700590 // Fill out the fields in the Class.
591 //
592 // It is possible to execute some methods against arrays, because
593 // all arrays are subclasses of java_lang_Object_, so we need to set
594 // up a vtable. We can just point at the one in java_lang_Object_.
595 //
596 // Array classes are simple enough that we don't need to do a full
597 // link step.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700598
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700599 Class* new_class = NULL;
Jesse Wilson8989d992011-08-02 13:39:42 -0700600 if (!init_done_) {
601 if (descriptor == "[Ljava/lang/Object;") {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700602 new_class = class_roots_->Get(kObjectArrayClass);
603 CHECK(new_class);
Jesse Wilson8989d992011-08-02 13:39:42 -0700604 } else if (descriptor == "[C") {
605 new_class = class_roots_->Get(kCharArrayClass);
606 CHECK(new_class);
607 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700608 }
609 if (new_class == NULL) {
610 new_class = AllocClass();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700611 if (new_class == NULL) {
612 return NULL;
613 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700614 }
615 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
616 descriptor.size());
617 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
618 new_class->descriptor_alloc_->size());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700619 Class* java_lang_Object = class_roots_->Get(kJavaLangObject);
620 new_class->super_class_ = java_lang_Object;
621 new_class->vtable_ = java_lang_Object->vtable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700622 new_class->primitive_type_ = Class::kPrimNot;
623 new_class->component_type_ = component_type_;
624 new_class->class_loader_ = component_type_->class_loader_;
625 new_class->array_rank_ = array_rank;
626 new_class->status_ = Class::kStatusInitialized;
Brian Carlstrombe977852011-07-19 14:54:54 -0700627 // don't need to set new_class->object_size_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700628
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700629
Brian Carlstrombe977852011-07-19 14:54:54 -0700630 // All arrays have java/lang/Cloneable and java/io/Serializable as
631 // interfaces. We need to set that up here, so that stuff like
632 // "instanceof" works right.
633 //
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700634 // Note: The GC could run during the call to FindSystemClass,
Brian Carlstrombe977852011-07-19 14:54:54 -0700635 // so we need to make sure the class object is GC-valid while we're in
636 // there. Do this by clearing the interface list so the GC will just
637 // think that the entries are null.
Brian Carlstrombe977852011-07-19 14:54:54 -0700638
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700639
640 // Use the single, global copies of "interfaces" and "iftable"
641 // (remember not to free them for arrays).
642 DCHECK(array_interfaces_ != NULL);
643 new_class->interfaces_ = array_interfaces_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700644 new_class->iftable_count_ = 2;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700645 DCHECK(array_iftable_ != NULL);
646 new_class->iftable_ = array_iftable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700647
Brian Carlstrombe977852011-07-19 14:54:54 -0700648 // Inherit access flags from the component type. Arrays can't be
649 // used as a superclass or interface, so we want to add "final"
650 // and remove "interface".
651 //
652 // Don't inherit any non-standard flags (e.g., kAccFinal)
653 // from component_type_. We assume that the array class does not
654 // override finalize().
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700655 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
656 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
657
658 if (InsertClass(new_class)) {
659 return new_class;
660 }
Brian Carlstrombe977852011-07-19 14:54:54 -0700661 // Another thread must have loaded the class after we
662 // started but before we finished. Abandon what we've
663 // done.
664 //
665 // (Yes, this happens.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700666
Brian Carlstrombe977852011-07-19 14:54:54 -0700667 // Grab the winning class.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700668 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
669 DCHECK(other_class != NULL);
670 return other_class;
671}
672
673Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700674 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700675 case 'B':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700676 DCHECK(class_roots_->Get(kPrimitiveByte) != NULL);
677 return class_roots_->Get(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700678 case 'C':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700679 DCHECK(class_roots_->Get(kPrimitiveChar) != NULL);
680 return class_roots_->Get(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700681 case 'D':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700682 DCHECK(class_roots_->Get(kPrimitiveDouble) != NULL);
683 return class_roots_->Get(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700684 case 'F':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700685 DCHECK(class_roots_->Get(kPrimitiveFloat) != NULL);
686 return class_roots_->Get(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700687 case 'I':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700688 DCHECK(class_roots_->Get(kPrimitiveInt) != NULL);
689 return class_roots_->Get(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700690 case 'J':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700691 DCHECK(class_roots_->Get(kPrimitiveLong) != NULL);
692 return class_roots_->Get(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700693 case 'S':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700694 DCHECK(class_roots_->Get(kPrimitiveShort) != NULL);
695 return class_roots_->Get(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700696 case 'Z':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700697 DCHECK(class_roots_->Get(kPrimitiveBoolean) != NULL);
698 return class_roots_->Get(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700699 case 'V':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700700 DCHECK(class_roots_->Get(kPrimitiveVoid) != NULL);
701 return class_roots_->Get(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700702 case 'L':
703 case '[':
704 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700705 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700706 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700707 };
708 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700709}
710
711bool ClassLinker::InsertClass(Class* klass) {
712 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700713 const StringPiece& key = klass->GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700714 bool success = classes_.insert(std::make_pair(key, klass)).second;
715 // TODO: release classes_lock_
716 return success;
717}
718
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700719Class* ClassLinker::LookupClass(const StringPiece& descriptor, Object* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700720 // TODO: acquire classes_lock_
721 Table::iterator it = classes_.find(descriptor);
722 // TODO: release classes_lock_
723 if (it == classes_.end()) {
724 return NULL;
725 } else {
726 return (*it).second;
727 }
728}
729
730bool ClassLinker::InitializeClass(Class* klass) {
731 CHECK(klass->GetStatus() == Class::kStatusResolved ||
732 klass->GetStatus() == Class::kStatusError);
733
Carl Shapirob5573532011-07-12 18:22:59 -0700734 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700735
736 {
737 ObjectLock lock(klass);
738
739 if (klass->GetStatus() < Class::kStatusVerified) {
740 if (klass->IsErroneous()) {
741 LG << "re-initializing failed class"; // TODO: throw
742 return false;
743 }
744
745 CHECK(klass->GetStatus() == Class::kStatusResolved);
746
747 klass->status_ = Class::kStatusVerifying;
748 if (!DexVerify::VerifyClass(klass)) {
749 LG << "Verification failed"; // TODO: ThrowVerifyError
750 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700751 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
752 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700753 klass->SetStatus(Class::kStatusError);
754 return false;
755 }
756
757 klass->SetStatus(Class::kStatusVerified);
758 }
759
760 if (klass->status_ == Class::kStatusInitialized) {
761 return true;
762 }
763
764 while (klass->status_ == Class::kStatusInitializing) {
765 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700766 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700767 LG << "recursive <clinit>";
768 return true;
769 }
770
771 CHECK(!self->IsExceptionPending());
772
773 lock.Wait(); // TODO: check for interruption
774
775 // When we wake up, repeat the test for init-in-progress. If
776 // there's an exception pending (only possible if
777 // "interruptShouldThrow" was set), bail out.
778 if (self->IsExceptionPending()) {
779 CHECK(false);
780 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
781 klass->SetStatus(Class::kStatusError);
782 return false;
783 }
784 if (klass->GetStatus() == Class::kStatusInitializing) {
785 continue;
786 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700787 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700788 klass->GetStatus() == Class::kStatusError);
789 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700790 // The caller wants an exception, but it was thrown in a
791 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700792 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
793 return false;
794 }
795 return true; // otherwise, initialized
796 }
797
798 // see if we failed previously
799 if (klass->IsErroneous()) {
800 // might be wise to unlock before throwing; depends on which class
801 // it is that we have locked
802
803 // TODO: throwEarlierClassFailure(klass);
804 return false;
805 }
806
807 if (!ValidateSuperClassDescriptors(klass)) {
808 klass->SetStatus(Class::kStatusError);
809 return false;
810 }
811
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700812 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700813
Carl Shapirob5573532011-07-12 18:22:59 -0700814 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700815 klass->status_ = Class::kStatusInitializing;
816 }
817
818 if (!InitializeSuperClass(klass)) {
819 return false;
820 }
821
822 InitializeStaticFields(klass);
823
824 Method* clinit = klass->FindDirectMethodLocally("<clinit>", "()V");
825 if (clinit != NULL) {
826 } else {
827 // JValue unused;
828 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700829 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700830 }
831
832 {
833 ObjectLock lock(klass);
834
835 if (self->IsExceptionPending()) {
836 klass->SetStatus(Class::kStatusError);
837 } else {
838 klass->SetStatus(Class::kStatusInitialized);
839 }
840 lock.NotifyAll();
841 }
842
843 return true;
844}
845
846bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
847 if (klass->IsInterface()) {
848 return true;
849 }
850 // begin with the methods local to the superclass
851 if (klass->HasSuperClass() &&
852 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
853 const Class* super = klass->GetSuperClass();
854 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
855 const Method* method = klass->GetVirtualMethod(i);
856 if (method != super->GetVirtualMethod(i) &&
857 !HasSameMethodDescriptorClasses(method, super, klass)) {
858 LG << "Classes resolve differently in superclass";
859 return false;
860 }
861 }
862 }
863 for (size_t i = 0; i < klass->iftable_count_; ++i) {
864 const InterfaceEntry* iftable = &klass->iftable_[i];
865 Class* interface = iftable->GetClass();
866 if (klass->GetClassLoader() != interface->GetClassLoader()) {
867 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
868 uint32_t vtable_index = iftable->method_index_array_[j];
869 const Method* method = klass->GetVirtualMethod(vtable_index);
870 if (!HasSameMethodDescriptorClasses(method, interface,
871 method->GetClass())) {
872 LG << "Classes resolve differently in interface"; // TODO: LinkageError
873 return false;
874 }
875 }
876 }
877 }
878 return true;
879}
880
881bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700882 const Class* klass1,
883 const Class* klass2) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700884 const DexFile* dex_file = FindDexFile(method->GetClass()->GetDexCache());
885 const DexFile::ProtoId& proto_id = dex_file->GetProtoId(method->proto_idx_);
886 DexFile::ParameterIterator *it;
887 for (it = dex_file->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700888 const char* descriptor = it->GetDescriptor();
889 if (descriptor == NULL) {
890 break;
891 }
892 if (descriptor[0] == 'L' || descriptor[0] == '[') {
893 // Found a non-primitive type.
894 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
895 return false;
896 }
897 }
898 }
899 // Check the return type
Brian Carlstromf615a612011-07-23 12:50:34 -0700900 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700901 if (descriptor[0] == 'L' || descriptor[0] == '[') {
902 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
903 return false;
904 }
905 }
906 return true;
907}
908
909// Returns true if classes referenced by the descriptor are the
910// same classes in klass1 as they are in klass2.
911bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700912 const Class* klass1,
913 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700914 CHECK(descriptor != NULL);
915 CHECK(klass1 != NULL);
916 CHECK(klass2 != NULL);
917#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700918 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700919 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700920 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700921 // TODO: found2 == NULL
922 // TODO: lookup found1 in initiating loader list
923 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700924 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700925 if (found1 == found2) {
926 return true;
927 } else {
928 return false;
929 }
930 }
931#endif
932 return true;
933}
934
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700935bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700936 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
937 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
938 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
939 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700940
941 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstromf615a612011-07-23 12:50:34 -0700942 const DexFile::TypeList* type_list1 = dex1->GetProtoParameters(proto1);
943 const DexFile::TypeList* type_list2 = dex2->GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700944 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
945 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
946 if (arity1 != arity2) {
947 return false;
948 }
949
950 for (size_t i = 0; i < arity1; ++i) {
951 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
952 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700953 const char* type1 = dex1->dexStringByTypeIdx(type_idx1);
954 const char* type2 = dex2->dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700955 if (strcmp(type1, type2) != 0) {
956 return false;
957 }
958 }
959
960 return true;
961}
962
963bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700964 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
965 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
966 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
967 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
968 const char* type1 = dex1->dexStringByTypeIdx(proto1.return_type_idx_);
969 const char* type2 = dex2->dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700970 return (strcmp(type1, type2) == 0);
971}
972
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700973bool ClassLinker::InitializeSuperClass(Class* klass) {
974 CHECK(klass != NULL);
975 // TODO: assert klass lock is acquired
976 if (!klass->IsInterface() && klass->HasSuperClass()) {
977 Class* super_class = klass->GetSuperClass();
978 if (super_class->GetStatus() != Class::kStatusInitialized) {
979 CHECK(!super_class->IsInterface());
980 klass->MonitorExit();
981 bool super_initialized = InitializeClass(super_class);
982 klass->MonitorEnter();
983 // TODO: check for a pending exception
984 if (!super_initialized) {
985 klass->SetStatus(Class::kStatusError);
986 klass->NotifyAll();
987 return false;
988 }
989 }
990 }
991 return true;
992}
993
994void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700995 size_t num_static_fields = klass->NumStaticFields();
996 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700997 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700998 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700999 DexCache* dex_cache = klass->GetDexCache();
1000 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001001 return;
1002 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001003 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstromf615a612011-07-23 12:50:34 -07001004 const DexFile* dex_file = FindDexFile(dex_cache);
1005 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
1006 CHECK(dex_class_def != NULL);
1007 const byte* addr = dex_file->GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001008 size_t array_size = DecodeUnsignedLeb128(&addr);
1009 for (size_t i = 0; i < array_size; ++i) {
1010 StaticField* field = klass->GetStaticField(i);
1011 JValue value;
Brian Carlstromf615a612011-07-23 12:50:34 -07001012 DexFile::ValueType type = dex_file->ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001013 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001014 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001015 field->SetByte(value.b);
1016 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001017 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001018 field->SetShort(value.s);
1019 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001020 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001021 field->SetChar(value.c);
1022 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001023 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001024 field->SetInt(value.i);
1025 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001026 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001027 field->SetLong(value.j);
1028 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001029 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001030 field->SetFloat(value.f);
1031 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001032 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001033 field->SetDouble(value.d);
1034 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001035 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001036 uint32_t string_idx = value.i;
1037 String* resolved = ResolveString(klass, string_idx);
1038 field->SetObject(resolved);
1039 break;
1040 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001041 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001042 field->SetBoolean(value.z);
1043 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001044 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001045 field->SetObject(value.l);
1046 break;
1047 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001048 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001049 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001050 }
1051}
1052
Brian Carlstromf615a612011-07-23 12:50:34 -07001053bool ClassLinker::LinkClass(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001054 CHECK(klass->status_ == Class::kStatusIdx ||
1055 klass->status_ == Class::kStatusLoaded);
1056 if (klass->status_ == Class::kStatusIdx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001057 if (!LinkInterfaces(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001058 return false;
1059 }
1060 }
1061 if (!LinkSuperClass(klass)) {
1062 return false;
1063 }
1064 if (!LinkMethods(klass)) {
1065 return false;
1066 }
1067 if (!LinkInstanceFields(klass)) {
1068 return false;
1069 }
1070 CreateReferenceOffsets(klass);
1071 CHECK_EQ(klass->status_, Class::kStatusLoaded);
1072 klass->status_ = Class::kStatusResolved;
1073 return true;
1074}
1075
Brian Carlstromf615a612011-07-23 12:50:34 -07001076bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001077 // Mark the class as loaded.
1078 klass->status_ = Class::kStatusLoaded;
Brian Carlstromf615a612011-07-23 12:50:34 -07001079 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1080 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001081 if (super_class == NULL) {
1082 LG << "Failed to resolve superclass";
1083 return false;
1084 }
1085 klass->super_class_ = super_class; // TODO: write barrier
1086 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001087 if (klass->NumInterfaces() > 0) {
1088 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1089 uint32_t idx = klass->interfaces_idx_[i];
1090 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1091 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001092 LG << "Failed to resolve interface";
1093 return false;
1094 }
1095 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001096 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001097 LG << "Inaccessible interface";
1098 return false;
1099 }
1100 }
1101 }
1102 return true;
1103}
1104
1105bool ClassLinker::LinkSuperClass(Class* klass) {
1106 CHECK(!klass->IsPrimitive());
1107 const Class* super = klass->GetSuperClass();
1108 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1109 if (super != NULL) {
1110 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1111 return false;
1112 }
1113 // TODO: clear finalize attribute
1114 return true;
1115 }
1116 if (super == NULL) {
1117 LG << "No superclass defined"; // TODO: LinkageError
1118 return false;
1119 }
1120 // Verify
1121 if (super->IsFinal()) {
1122 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
1123 return false;
1124 }
1125 if (super->IsInterface()) {
1126 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
1127 return false;
1128 }
1129 if (!klass->CanAccess(super)) {
1130 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
1131 return false;
1132 }
1133 return true;
1134}
1135
1136// Populate the class vtable and itable.
1137bool ClassLinker::LinkMethods(Class* klass) {
1138 if (klass->IsInterface()) {
1139 // No vtable.
1140 size_t count = klass->NumVirtualMethods();
1141 if (!IsUint(16, count)) {
1142 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1143 return false;
1144 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001145 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001146 klass->GetVirtualMethod(i)->method_index_ = i;
1147 }
1148 } else {
1149 // Link virtual method tables
1150 LinkVirtualMethods(klass);
1151
1152 // Link interface method tables
1153 LinkInterfaceMethods(klass);
1154
1155 // Insert stubs.
1156 LinkAbstractMethods(klass);
1157 }
1158 return true;
1159}
1160
1161bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001162 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001163 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001164 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1165 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001166 // TODO: do not assign to the vtable field until it is fully constructed.
1167 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001168 // See if any of our virtual methods override the superclass.
1169 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1170 Method* local_method = klass->GetVirtualMethod(i);
1171 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001172 for (; j < actual_count; ++j) {
1173 Method* super_method = klass->vtable_->Get(j);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001174 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001175 // Verify
1176 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001177 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001178 return false;
1179 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001180 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001181 local_method->method_index_ = j;
1182 break;
1183 }
1184 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001185 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001186 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001187 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001188 local_method->method_index_ = actual_count;
1189 actual_count += 1;
1190 }
1191 }
1192 if (!IsUint(16, actual_count)) {
1193 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1194 return false;
1195 }
1196 CHECK_LE(actual_count, max_count);
1197 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001198 // TODO: do not assign to the vtable field until it is fully constructed.
1199 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001200 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201 } else {
1202 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001203 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1204 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1205 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001206 LG << "Too many methods"; // TODO: VirtualMachineError
1207 return false;
1208 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001209 // TODO: do not assign to the vtable field until it is fully constructed.
1210 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1211 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001212 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001213 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1214 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001215 }
1216 return true;
1217}
1218
1219bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1220 int pool_offset = 0;
1221 int pool_size = 0;
1222 int miranda_count = 0;
1223 int miranda_alloc = 0;
1224 size_t super_ifcount;
1225 if (klass->HasSuperClass()) {
1226 super_ifcount = klass->GetSuperClass()->iftable_count_;
1227 } else {
1228 super_ifcount = 0;
1229 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001230 size_t ifcount = super_ifcount;
1231 ifcount += klass->NumInterfaces();
1232 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1233 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001234 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001235 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001236 DCHECK(klass->iftable_count_ == 0);
1237 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001238 return true;
1239 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001240 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1241 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001242 if (super_ifcount != 0) {
1243 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1244 sizeof(InterfaceEntry) * super_ifcount);
1245 }
1246 // Flatten the interface inheritance hierarchy.
1247 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001248 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1249 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001250 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001251 if (!interf->IsInterface()) {
1252 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1253 return false;
1254 }
1255 klass->iftable_[idx++].SetClass(interf);
1256 for (size_t j = 0; j < interf->iftable_count_; j++) {
1257 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1258 }
1259 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001260 CHECK_EQ(idx, ifcount);
1261 klass->iftable_count_ = ifcount;
1262 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001263 return true;
1264 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001265 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001266 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1267 }
1268 if (pool_size == 0) {
1269 return true;
1270 }
1271 klass->ifvi_pool_count_ = pool_size;
1272 klass->ifvi_pool_ = new uint32_t[pool_size];
1273 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001274 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001275 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1276 Class* interface = klass->iftable_[i].GetClass();
1277 pool_offset += interface->NumVirtualMethods(); // end here
1278 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1279 Method* interface_method = interface->GetVirtualMethod(j);
1280 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001281 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
1282 if (HasSameNameAndPrototype(interface_method, klass->vtable_->Get(k))) {
1283 if (!klass->vtable_->Get(k)->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001284 LG << "Implementation not public";
1285 return false;
1286 }
1287 klass->iftable_[i].method_index_array_[j] = k;
1288 break;
1289 }
1290 }
1291 if (k < 0) {
1292 if (miranda_count == miranda_alloc) {
1293 miranda_alloc += 8;
1294 if (miranda_list.empty()) {
1295 miranda_list.resize(miranda_alloc);
1296 } else {
1297 miranda_list.resize(miranda_alloc);
1298 }
1299 }
1300 int mir;
1301 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001302 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001303 break;
1304 }
1305 }
1306 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001307 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001308 if (mir == miranda_count) {
1309 miranda_list[miranda_count++] = interface_method;
1310 }
1311 }
1312 }
1313 }
1314 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001315 int old_method_count = klass->NumVirtualMethods();
1316 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001317 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001318
1319 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001320 int old_vtable_count = klass->vtable_->GetLength();
1321 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001322 // TODO: do not assign to the vtable field until it is fully constructed.
1323 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001324
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001325 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001326 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001327 memcpy(meth, miranda_list[i], sizeof(Method));
1328 meth->klass_ = klass;
1329 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001330 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1331 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001332 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001333 }
1334 }
1335 return true;
1336}
1337
1338void ClassLinker::LinkAbstractMethods(Class* klass) {
1339 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1340 Method* method = klass->GetVirtualMethod(i);
1341 if (method->IsAbstract()) {
1342 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1343 }
1344 }
1345}
1346
1347bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001348 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001349 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001350 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001352 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001353 }
1354 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001355 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001356 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001357 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 InstanceField* pField = klass->GetInstanceField(i);
1359 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001360 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001361 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1362 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001363 char rc = refField->GetType();
1364 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001365 klass->SetInstanceField(i, refField);
1366 klass->SetInstanceField(j, pField);
1367 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001368 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001369 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001370 break;
1371 }
1372 }
1373 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001374 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001375 }
1376 if (c != '[' && c != 'L') {
1377 break;
1378 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001379 pField->SetOffset(field_offset);
1380 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001381 }
1382
1383 // Now we want to pack all of the double-wide fields together. If
1384 // we're not aligned, though, we want to shuffle one 32-bit field
1385 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001386 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001387 InstanceField* pField = klass->GetInstanceField(i);
1388 char c = pField->GetType();
1389
1390 if (c != 'J' && c != 'D') {
1391 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001392 DCHECK(c != '[');
1393 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001394 pField->SetOffset(field_offset);
1395 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001396 i++;
1397 } else {
1398 // Next field is 64-bit, so search for a 32-bit field we can
1399 // swap into it.
1400 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001401 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1402 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001403 char rc = singleField->GetType();
1404 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001405 klass->SetInstanceField(i, singleField);
1406 klass->SetInstanceField(j, pField);
1407 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001408 pField->SetOffset(field_offset);
1409 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001410 found = true;
1411 i++;
1412 break;
1413 }
1414 }
1415 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001416 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001417 }
1418 }
1419 }
1420
1421 // Alignment is good, shuffle any double-wide fields forward, and
1422 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001423 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001424 for ( ; i < klass->NumInstanceFields(); i++) {
1425 InstanceField* pField = klass->GetInstanceField(i);
1426 char c = pField->GetType();
1427 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001428 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1429 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001430 char rc = doubleField->GetType();
1431 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001432 klass->SetInstanceField(i, doubleField);
1433 klass->SetInstanceField(j, pField);
1434 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001435 c = rc;
1436 break;
1437 }
1438 }
1439 } else {
1440 // This is a double-wide field, leave it be.
1441 }
1442
Brian Carlstroma0808032011-07-18 00:39:23 -07001443 pField->SetOffset(field_offset);
1444 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001445 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001446 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001447 }
1448
1449#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001450 // Make sure that all reference fields appear before
1451 // non-reference fields, and all double-wide fields are aligned.
1452 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001453 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001454 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001455 char c = pField->GetType();
1456
1457 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001458 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459 }
1460
1461 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001462 if (!seen_non_ref) {
1463 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001464 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001465 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001466 } else {
1467 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001468 }
1469 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001470 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001471 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001472 }
1473#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001474 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001475 return true;
1476}
1477
1478// Set the bitmap of reference offsets, refOffsets, from the ifields
1479// list.
1480void ClassLinker::CreateReferenceOffsets(Class* klass) {
1481 uint32_t reference_offsets = 0;
1482 if (klass->HasSuperClass()) {
1483 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1484 }
1485 // If our superclass overflowed, we don't stand a chance.
1486 if (reference_offsets != CLASS_WALK_SUPER) {
1487 // All of the fields that contain object references are guaranteed
1488 // to be at the beginning of the ifields list.
1489 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1490 // Note that, per the comment on struct InstField, f->byteOffset
1491 // is the offset from the beginning of obj, not the offset into
1492 // obj->instanceData.
1493 const InstanceField* field = klass->GetInstanceField(i);
1494 size_t byte_offset = field->GetOffset();
1495 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001496 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001497 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1498 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001499 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001500 reference_offsets |= new_bit;
1501 } else {
1502 reference_offsets = CLASS_WALK_SUPER;
1503 break;
1504 }
1505 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001506 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001507 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001508}
1509
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001510Class* ClassLinker::ResolveClass(const Class* referrer,
1511 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001512 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001513 DexCache* dex_cache = referrer->GetDexCache();
1514 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001515 if (resolved != NULL) {
1516 return resolved;
1517 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001518 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001519 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001520 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001521 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001522 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001523 }
1524 if (resolved != NULL) {
1525 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001526 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001527 if (check->GetClassLoader() != NULL) {
1528 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1529 return NULL;
1530 }
1531 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001532 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001533 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001534 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001535 }
1536 return resolved;
1537}
1538
1539Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1540 /*MethodType*/ int method_type) {
1541 CHECK(false);
1542 return NULL;
1543}
1544
Carl Shapiro69759ea2011-07-21 18:13:35 -07001545String* ClassLinker::ResolveString(const Class* referring,
1546 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001547 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1548 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001549 int32_t utf16_length = dex_file->GetStringLength(string_id);
1550 const char* utf8_data = dex_file->GetStringData(string_id);
Jesse Wilson8989d992011-08-02 13:39:42 -07001551 String* new_string = String::AllocFromModifiedUtf8(utf16_length, utf8_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001552 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001553 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001554 return new_string;
1555}
1556
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001557} // namespace art