blob: f37df3fdb37ce223614838586f05f9d2a4aeeb46 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "class_linker.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004
5#include <vector>
6#include <utility>
7
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "casts.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07009#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "dex_verifier.h"
11#include "heap.h"
12#include "logging.h"
13#include "monitor.h"
14#include "object.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070015#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "scoped_ptr.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070017#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "thread.h"
19#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
22
Brian Carlstroma663ea52011-08-19 23:33:41 -070023const char* ClassLinker::class_roots_descriptors_[kClassRootsMax] = {
24 "Ljava/lang/Class;",
25 "Ljava/lang/Object;",
26 "[Ljava/lang/Object;",
27 "Ljava/lang/String;",
28 "Ljava/lang/reflect/Field;",
29 "Ljava/lang/reflect/Method;",
30 "Ljava/lang/ClassLoader;",
31 "Ldalvik/system/BaseDexClassLoader;",
32 "Ldalvik/system/PathClassLoader;",
33 "Z",
34 "B",
35 "C",
36 "D",
37 "F",
38 "I",
39 "J",
40 "S",
41 "V",
42 "[Z",
43 "[B",
44 "[C",
45 "[D",
46 "[F",
47 "[I",
48 "[J",
49 "[S",
50};
51
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070052ClassLinker* ClassLinker::Create(const std::vector<const DexFile*>& boot_class_path, Space* space) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070053 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstroma663ea52011-08-19 23:33:41 -070054 if (space == NULL) {
55 class_linker->Init(boot_class_path);
56 } else {
57 class_linker->Init(boot_class_path, space);
58 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070059 // TODO: check for failure during initialization
60 return class_linker.release();
61}
62
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070063ClassLinker::ClassLinker()
64 : classes_lock_(Mutex::Create("ClassLinker::Lock")),
65 class_roots_(NULL),
66 init_done_(false) {
67}
Brian Carlstroma663ea52011-08-19 23:33:41 -070068
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070069void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070070 CHECK(!init_done_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070071
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070072 // java_lang_Class comes first, its needed for AllocClass
Brian Carlstrom4873d462011-08-21 15:23:39 -070073 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(ClassClass)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070074 CHECK(java_lang_Class != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -070075 java_lang_Class->class_size_ = sizeof(ClassClass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070076 java_lang_Class->klass_ = java_lang_Class;
77 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070078
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070079 // java_lang_Object comes next so that object_array_class can be created
Brian Carlstrom4873d462011-08-21 15:23:39 -070080 Class* java_lang_Object = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070081 CHECK(java_lang_Object != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070082 // backfill Object as the super class of Class
83 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070084 // mark as non-primitive for object_array_class
85 java_lang_Object->primitive_type_ = Class::kPrimNot;
Brian Carlstroma0808032011-07-18 00:39:23 -070086
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070087 // object_array_class is for root_classes to provide the storage for these classes
Brian Carlstrom4873d462011-08-21 15:23:39 -070088 Class* object_array_class = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070089 CHECK(object_array_class != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070090 object_array_class->component_type_ = java_lang_Object;
Brian Carlstroma0808032011-07-18 00:39:23 -070091
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070092 // String and char[] are necessary so that FindClass can assign names to members
Brian Carlstrom4873d462011-08-21 15:23:39 -070093 Class* java_lang_String = AllocClass(java_lang_Class, sizeof(StringClass));
Jesse Wilson14150742011-07-29 19:04:44 -040094 CHECK(java_lang_String != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070095 CHECK_LT(java_lang_String->object_size_, sizeof(String));
Jesse Wilson14150742011-07-29 19:04:44 -040096 java_lang_String->object_size_ = sizeof(String);
Brian Carlstroma663ea52011-08-19 23:33:41 -070097 String::SetClass(java_lang_String);
Brian Carlstrom4873d462011-08-21 15:23:39 -070098 Class* char_array_class = AllocClass(java_lang_Class, sizeof(Class));
Jesse Wilson8989d992011-08-02 13:39:42 -070099 CHECK(char_array_class != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700100 CharArray::SetArrayClass(char_array_class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700101 // Now String::Alloc* can be used
102
103 // backfill Class descriptors missing until this point
104 java_lang_Class->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Class;");
105 java_lang_Object->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Object;");
106 object_array_class->descriptor_ = String::AllocFromModifiedUtf8("[Ljava/lang/Object;");
107 java_lang_String->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/String;");
108 char_array_class->descriptor_ = String::AllocFromModifiedUtf8("[C");
Jesse Wilson14150742011-07-29 19:04:44 -0400109
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700110 // Field and Method are necessary so that FindClass can link members
Brian Carlstrom4873d462011-08-21 15:23:39 -0700111 Class* java_lang_reflect_Field = AllocClass(java_lang_Class, sizeof(FieldClass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700112 CHECK(java_lang_reflect_Field != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700113 java_lang_reflect_Field->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;");
Jesse Wilson35baaab2011-08-10 16:18:03 -0400114 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
115 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700116 Class* java_lang_reflect_Method = AllocClass(java_lang_Class, sizeof(MethodClass));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700117 java_lang_reflect_Method->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700118 CHECK(java_lang_reflect_Method != NULL);
119 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
120 java_lang_reflect_Method->object_size_ = sizeof(Method);
121
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700122 // create storage for root classes, save away our work so far
123 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700124 SetClassRoot(kJavaLangClass, java_lang_Class);
125 SetClassRoot(kJavaLangObject, java_lang_Object);
126 SetClassRoot(kObjectArrayClass, object_array_class);
127 SetClassRoot(kJavaLangString, java_lang_String);
128 SetClassRoot(kCharArrayClass, char_array_class);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700129 SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field);
130 SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700131 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -0700132
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700133 // setup boot_class_path_ now that we can use AllocObjectArray to
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700134 // create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700135 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700136 const DexFile* dex_file = boot_class_path[i];
137 CHECK(dex_file != NULL);
138 AppendToBootClassPath(*dex_file);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700139 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700140 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700141
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700142 // run Class, Field, and Method through FindSystemClass.
143 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700144 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700145 Class* Class_class = FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700146 CHECK_EQ(java_lang_Class, Class_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700147 CHECK_LT(java_lang_Class->object_size_, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700148 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700149 Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700150 CHECK_EQ(java_lang_reflect_Field, Field_class);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400151 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
152 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700153 Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700154 CHECK_EQ(java_lang_reflect_Method, Method_class);
155 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700156 java_lang_reflect_Method->object_size_ = sizeof(Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700157
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700158 // Object and String just need more minimal setup, since they do not have extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700159 Class* Object_class = FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700160 CHECK_EQ(java_lang_Object, Object_class);
161 CHECK_EQ(java_lang_Object->object_size_, sizeof(Object));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700162 Class* String_class = FindSystemClass("Ljava/lang/String;");
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700163 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700164 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700165
166 // Setup the ClassLoaders, adjusting the object_size_ as necessary
167 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
168 CHECK(java_lang_ClassLoader != NULL);
169 CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader));
170 java_lang_ClassLoader->object_size_ = sizeof(ClassLoader);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700171 SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700172 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
173 CHECK(dalvik_system_BaseDexClassLoader != NULL);
174 CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700175 SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700176 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
177 CHECK(dalvik_system_PathClassLoader != NULL);
178 CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700179 SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700180 PathClassLoader::SetClass(dalvik_system_PathClassLoader);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700181
182 // Setup a single, global copy of "interfaces" and "iftable" for
183 // reuse across array classes
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700184 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700185 CHECK(java_lang_Cloneable != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700186 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700187 CHECK(java_io_Serializable != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700188
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700189 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700190 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700191 array_interfaces_->Set(0, java_lang_Cloneable);
192 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700193
194 // We assume that Cloneable/Serializable don't have superinterfaces --
195 // normally we'd have to crawl up and explicitly list all of the
196 // supers as well. These interfaces don't have any methods, so we
197 // don't have to worry about the ifviPool either.
198 array_iftable_ = new InterfaceEntry[2];
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700199 array_iftable_[0].SetClass(array_interfaces_->Get(0));
200 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700201 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700202
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700203 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700204 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
205 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700206 CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0));
207 CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700208
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700209 // Setup the primitive type classes.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700210 SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
211 SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B"));
212 SetClassRoot(kPrimitiveChar, CreatePrimitiveClass("C"));
213 SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D"));
214 SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F"));
215 SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I"));
216 SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J"));
217 SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S"));
218 SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V"));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700219 // now we can use FindSystemClass for anything, including for "[C"
220
Jesse Wilson7833bd22011-08-09 18:31:44 -0400221 // run char[], int[] and long[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700222 Class* found_char_array_class = FindSystemClass("[C");
223 CHECK_EQ(char_array_class, found_char_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700224
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700225 // Initialize all the other primitive array types for PrimitiveArray::Alloc.
226 // These are easy because everything we need has already been set up.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700227 SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z"));
228 SetClassRoot(kByteArrayClass, FindSystemClass("[B"));
229 SetClassRoot(kDoubleArrayClass, FindSystemClass("[D"));
230 SetClassRoot(kFloatArrayClass, FindSystemClass("[F"));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700231 SetClassRoot(kIntArrayClass, FindSystemClass("[I"));
232 SetClassRoot(kLongArrayClass, FindSystemClass("[J"));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700233 SetClassRoot(kShortArrayClass, FindSystemClass("[S"));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700234 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
235 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
236 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
237 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700238 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
239 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700240 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
241
Brian Carlstroma663ea52011-08-19 23:33:41 -0700242 FinishInit();
243}
244
245void ClassLinker::FinishInit() {
246 // ensure all class_roots_ are initialized
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700247 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700248 ClassRoot class_root = static_cast<ClassRoot>(i);
249 Class* klass = GetClassRoot(class_root);
250 CHECK(klass != NULL);
251 DCHECK(klass->IsArray() || klass->IsPrimitive() || klass->dex_cache_ != NULL);
252 // note SetClassRoot does additional validation.
253 // if possible add new checks there to catch errors early
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700254 }
255
256 // disable the slow paths in FindClass and CreatePrimitiveClass now
257 // that Object, Class, and Object[] are setup
258 init_done_ = true;
259}
260
Brian Carlstroma663ea52011-08-19 23:33:41 -0700261struct ClassLinker::InitCallbackState {
262 ClassLinker* class_linker;
263
264 Class* class_roots[kClassRootsMax];
265
266 typedef std::tr1::unordered_map<std::string, ClassRoot> Table;
267 Table descriptor_to_class_root;
268
269 struct DexCacheHash {
270 size_t operator()(art::DexCache* const& obj) const {
271 return reinterpret_cast<size_t>(&obj);
272 }
273 };
274 typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set;
275 Set dex_caches;
276};
277
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700278void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path, Space* space) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700279 CHECK(!init_done_);
280
281 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
282 DCHECK(heap_bitmap != NULL);
283
284 InitCallbackState state;
285 state.class_linker = this;
286 for (size_t i = 0; i < kClassRootsMax; i++) {
287 ClassRoot class_root = static_cast<ClassRoot>(i);
288 state.descriptor_to_class_root[GetClassRootDescriptor(class_root)] = class_root;
289 }
290
291 // reinit clases_ table
292 heap_bitmap->Walk(InitCallback, &state);
293
294 // reinit class_roots_
295 Class* object_array_class = state.class_roots[kObjectArrayClass];
296 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
297 for (size_t i = 0; i < kClassRootsMax; i++) {
298 ClassRoot class_root = static_cast<ClassRoot>(i);
299 SetClassRoot(class_root, state.class_roots[class_root]);
300 }
301
302 // reinit intern_table_
303 ObjectArray<Object>* interned_array = space->GetImageHeader().GetInternedArray();
304 for (int32_t i = 0; i < interned_array->GetLength(); i++) {
305 String* string = interned_array->Get(i)->AsString();
306 intern_table_.Register(string);
307 }
308
309 // reinit array_interfaces_ from any array class instance, they should all be ==
310 array_interfaces_ = GetClassRoot(kObjectArrayClass)->interfaces_;
311 DCHECK(array_interfaces_ == GetClassRoot(kBooleanArrayClass)->interfaces_);
312
313 // build a map from location to DexCache to match up with DexFile::GetLocation
314 std::tr1::unordered_map<std::string, DexCache*> location_to_dex_cache;
315 typedef InitCallbackState::Set::const_iterator It; // TODO: C++0x auto
316 for (It it = state.dex_caches.begin(), end = state.dex_caches.end(); it != end; ++it) {
317 DexCache* dex_cache = *it;
318 std::string location = dex_cache->GetLocation()->ToModifiedUtf8();
319 location_to_dex_cache[location] = dex_cache;
320 }
321
322 // reinit boot_class_path with DexFile arguments and found DexCaches
323 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700324 const DexFile* dex_file = boot_class_path[i];
325 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700326 DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700327 AppendToBootClassPath(*dex_file, dex_cache);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700328 }
329
330 String::SetClass(GetClassRoot(kJavaLangString));
331 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
332 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
333 CharArray::SetArrayClass(GetClassRoot(kCharArrayClass));
334 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
335 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
336 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
337 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
338 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700339 PathClassLoader::SetClass(GetClassRoot(kDalvikSystemPathClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700340
341 FinishInit();
342}
343
Brian Carlstrom4873d462011-08-21 15:23:39 -0700344void ClassLinker::InitCallback(Object* obj, void *arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700345 DCHECK(obj != NULL);
346 DCHECK(arg != NULL);
347 InitCallbackState* state = reinterpret_cast<InitCallbackState*>(arg);
348
349 if (!obj->IsClass()) {
350 return;
351 }
352 Class* klass = obj->AsClass();
353 CHECK(klass->class_loader_ == NULL);
354
355 std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8();
356
357 // restore class to ClassLinker::classes_ table
358 state->class_linker->InsertClass(descriptor, klass);
359
360 // note DexCache to match with DexFile later
361 DexCache* dex_cache = klass->GetDexCache();
362 if (dex_cache != NULL) {
363 state->dex_caches.insert(dex_cache);
364 } else {
365 DCHECK(klass->IsArray() || klass->IsPrimitive());
366 }
367
368 // check if this is a root, if so, register it
369 typedef InitCallbackState::Table::const_iterator It; // TODO: C++0x auto
370 It it = state->descriptor_to_class_root.find(descriptor);
371 if (it != state->descriptor_to_class_root.end()) {
372 ClassRoot class_root = it->second;
373 state->class_roots[class_root] = klass;
374 }
375}
376
377// Keep in sync with InitCallback. Anything we visit, we need to
378// reinit references to when reinitializing a ClassLinker from a
379// mapped image.
380void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) const {
381
Brian Carlstromb88e9442011-07-28 15:15:51 -0700382 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700383
384 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700385 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700386 }
387
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700388 {
389 MutexLock mu(classes_lock_);
390 typedef Table::const_iterator It; // TODO: C++0x auto
391 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
392 root_visitor(it->second, arg);
393 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700394 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700395
396 intern_table_.VisitRoots(root_visitor, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700397
Brian Carlstromb88e9442011-07-28 15:15:51 -0700398 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700399}
400
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700401ClassLinker::~ClassLinker() {
402 delete classes_lock_;
403 String::ResetClass();
404 BooleanArray::ResetArrayClass();
405 ByteArray::ResetArrayClass();
406 CharArray::ResetArrayClass();
407 DoubleArray::ResetArrayClass();
408 FloatArray::ResetArrayClass();
409 IntArray::ResetArrayClass();
410 LongArray::ResetArrayClass();
411 ShortArray::ResetArrayClass();
412 PathClassLoader::ResetClass();
413}
414
415DexCache* ClassLinker::AllocDexCache(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700416 DexCache* dex_cache = down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700417 dex_cache->Init(String::AllocFromModifiedUtf8(dex_file.GetLocation().c_str()),
418 AllocObjectArray<String>(dex_file.NumStringIds()),
419 AllocObjectArray<Class>(dex_file.NumTypeIds()),
420 AllocObjectArray<Method>(dex_file.NumMethodIds()),
421 AllocObjectArray<Field>(dex_file.NumFieldIds()));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700422 return dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700423}
424
Brian Carlstrom4873d462011-08-21 15:23:39 -0700425Class* ClassLinker::AllocClass(Class* java_lang_Class, size_t class_size) {
426 DCHECK_GE(class_size, sizeof(Class));
427 Class* klass = Heap::AllocObject(java_lang_Class, class_size)->AsClass();
428 klass->class_size_ = class_size;
429 return klass;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700430}
431
Brian Carlstrom4873d462011-08-21 15:23:39 -0700432Class* ClassLinker::AllocClass(size_t class_size) {
433 return AllocClass(GetClassRoot(kJavaLangClass), class_size);
Brian Carlstroma0808032011-07-18 00:39:23 -0700434}
435
Jesse Wilson35baaab2011-08-10 16:18:03 -0400436Field* ClassLinker::AllocField() {
437 return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->NewInstance());
Brian Carlstroma0808032011-07-18 00:39:23 -0700438}
439
440Method* ClassLinker::AllocMethod() {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700441 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700442}
443
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700444Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700445 const ClassLoader* class_loader) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700446 // TODO: remove this contrived parent class loader check when we have a real ClassLoader.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700447 if (class_loader != NULL) {
448 Class* klass = FindClass(descriptor, NULL);
449 if (klass != NULL) {
450 return klass;
451 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700452 Thread::Current()->ClearException();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700453 }
454
Carl Shapirob5573532011-07-12 18:22:59 -0700455 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700456 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700457 CHECK(!self->IsExceptionPending());
458 // Find the class in the loaded classes table.
459 Class* klass = LookupClass(descriptor, class_loader);
460 if (klass == NULL) {
461 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700462 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700463 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700464 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700465 const DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700466 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700467 if (pair.second == NULL) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700468 std::string name(PrintableString(descriptor));
469 self->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
470 "Class %s not found in class loader %p", name.c_str(), class_loader);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700471 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700472 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700473 const DexFile& dex_file = *pair.first;
474 const DexFile::ClassDef& dex_class_def = *pair.second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700475 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700476 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700477 if (!init_done_) {
478 // finish up init of hand crafted class_roots_
479 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700480 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700481 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700482 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400483 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700484 klass = GetClassRoot(kJavaLangString);
485 } else if (descriptor == "Ljava/lang/reflect/Field;") {
486 klass = GetClassRoot(kJavaLangReflectField);
487 } else if (descriptor == "Ljava/lang/reflect/Method;") {
488 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700489 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700490 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700491 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700492 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700493 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Carl Shapiro565f5072011-07-10 13:39:43 -0700494 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700495 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700496 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700497 // Check for a pending exception during load
498 if (self->IsExceptionPending()) {
499 // TODO: free native allocations in klass
500 return NULL;
501 }
502 {
503 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700504 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700505 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700506 bool success = InsertClass(descriptor, klass); // TODO: just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700507 if (!success) {
508 // We may fail to insert if we raced with another thread.
509 klass->clinit_thread_id_ = 0;
510 // TODO: free native allocations in klass
511 klass = LookupClass(descriptor, class_loader);
512 CHECK(klass != NULL);
513 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700514 // Finish loading (if necessary) by finding parents
515 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
516 // Loading failed.
517 // TODO: CHECK(self->IsExceptionPending());
518 lock.NotifyAll();
519 return NULL;
520 }
521 CHECK(klass->IsLoaded());
522 // Link the class (if necessary)
523 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700524 // Linking failed.
525 // TODO: CHECK(self->IsExceptionPending());
526 lock.NotifyAll();
527 return NULL;
528 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700529 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700530 }
531 }
532 }
533 // Link the class if it has not already been linked.
534 if (!klass->IsLinked() && !klass->IsErroneous()) {
535 ObjectLock lock(klass);
536 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700537 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700538 self->ThrowNewException("Ljava/lang/ClassCircularityError;", NULL); // TODO: detail
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700539 return NULL;
540 }
541 // Wait for the pending initialization to complete.
542 while (!klass->IsLinked() && !klass->IsErroneous()) {
543 lock.Wait();
544 }
545 }
546 if (klass->IsErroneous()) {
547 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
548 return NULL;
549 }
550 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700551 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700552 CHECK(!self->IsExceptionPending());
553 return klass;
554}
555
Brian Carlstrom4873d462011-08-21 15:23:39 -0700556// Precomputes size that will be needed for Class, matching LinkStaticFields
557size_t ClassLinker::SizeOfClass(const DexFile& dex_file,
558 const DexFile::ClassDef& dex_class_def) {
559 const byte* class_data = dex_file.GetClassData(dex_class_def);
560 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
561 size_t num_static_fields = header.static_fields_size_;
562 size_t num_ref = 0;
563 size_t num_32 = 0;
564 size_t num_64 = 0;
565 if (num_static_fields != 0) {
566 uint32_t last_idx = 0;
567 for (size_t i = 0; i < num_static_fields; ++i) {
568 DexFile::Field dex_field;
569 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
570 const DexFile::FieldId& field_id = dex_file.GetFieldId(dex_field.field_idx_);
571 const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_);
572 char c = descriptor[0];
573 if (c == 'L' || c == '[') {
574 num_ref++;
575 } else if (c == 'J' || c == 'D') {
576 num_64++;
577 } else {
578 num_32++;
579 }
580 }
581 }
582
583 // start with generic class data
584 size_t size = sizeof(Class);
585 // follow with reference fields which must be contiguous at start
586 size += (num_ref * sizeof(uint32_t));
587 // if there are 64-bit fields to add, make sure they are aligned
588 if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment
589 if (num_32 != 0) {
590 // use an available 32-bit field for padding
591 num_32--;
592 }
593 size += sizeof(uint32_t); // either way, we are adding a word
594 DCHECK_EQ(size, RoundUp(size, 8));
595 }
596 // tack on any 64-bit fields now that alignment is assured
597 size += (num_64 * sizeof(uint64_t));
598 // tack on any remaining 32-bit fields
599 size += (num_32 * sizeof(uint32_t));
600 return size;
601}
602
Brian Carlstromf615a612011-07-23 12:50:34 -0700603void ClassLinker::LoadClass(const DexFile& dex_file,
604 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700605 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700606 const ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700607 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700608 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700609 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700610 const byte* class_data = dex_file.GetClassData(dex_class_def);
611 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700612
Brian Carlstromf615a612011-07-23 12:50:34 -0700613 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700614 CHECK(descriptor != NULL);
615
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700616 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700617 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -0700618 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700619 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700620 klass->primitive_type_ = Class::kPrimNot;
621 klass->status_ = Class::kStatusIdx;
622
623 klass->super_class_ = NULL;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700624 klass->super_class_type_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700625
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700626 size_t num_static_fields = header.static_fields_size_;
627 size_t num_instance_fields = header.instance_fields_size_;
628 size_t num_direct_methods = header.direct_methods_size_;
629 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700630
Brian Carlstromf615a612011-07-23 12:50:34 -0700631 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700632
633 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700634 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700635
636 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700637 DCHECK(klass->sfields_ == NULL);
638 if (num_static_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400639 klass->sfields_ = AllocObjectArray<Field>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700640 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700641 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700642 DexFile::Field dex_field;
643 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400644 Field* sfield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700645 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700646 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700647 }
648 }
649
650 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700651 DCHECK(klass->ifields_ == NULL);
652 if (num_instance_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400653 klass->ifields_ = AllocObjectArray<Field>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700654 uint32_t last_idx = 0;
655 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700656 DexFile::Field dex_field;
657 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400658 Field* ifield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700659 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700660 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700661 }
662 }
663
664 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700665 DCHECK(klass->direct_methods_ == NULL);
666 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700667 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700668 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700669 uint32_t last_idx = 0;
670 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700671 DexFile::Method dex_method;
672 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700673 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700674 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700675 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700676 // TODO: register maps
677 }
678 }
679
680 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700681 DCHECK(klass->virtual_methods_ == NULL);
682 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700683 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700684 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700685 uint32_t last_idx = 0;
686 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700687 DexFile::Method dex_method;
688 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700689 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700690 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700691 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700692 // TODO: register maps
693 }
694 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700695}
696
Brian Carlstromf615a612011-07-23 12:50:34 -0700697void ClassLinker::LoadInterfaces(const DexFile& dex_file,
698 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700699 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700700 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700701 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700702 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700703 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700704 DCHECK(klass->interfaces_type_idx_ == NULL);
705 klass->interfaces_type_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700706 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700707 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700708 klass->interfaces_type_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700709 }
710 }
711}
712
Brian Carlstromf615a612011-07-23 12:50:34 -0700713void ClassLinker::LoadField(const DexFile& dex_file,
714 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700715 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700716 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700717 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400718 dst->declaring_class_ = klass;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700719 dst->name_ = ResolveString(dex_file, field_id.name_idx_, klass->GetDexCache());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700720 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Jesse Wilson35baaab2011-08-10 16:18:03 -0400721 // TODO: Assign dst->type_.
Brian Carlstrom934486c2011-07-12 23:42:50 -0700722 dst->access_flags_ = src.access_flags_;
723}
724
Brian Carlstromf615a612011-07-23 12:50:34 -0700725void ClassLinker::LoadMethod(const DexFile& dex_file,
726 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700727 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700728 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700729 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400730 dst->declaring_class_ = klass;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700731 dst->name_ = ResolveString(dex_file, method_id.name_idx_, klass->GetDexCache());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700732 {
733 int32_t utf16_length;
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700734 scoped_array<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700735 &utf16_length));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700736 dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700737 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700738 dst->proto_idx_ = method_id.proto_idx_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700739 dst->code_off_ = src.code_off_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700740 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700741 dst->access_flags_ = src.access_flags_;
742
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700743 dst->dex_cache_strings_ = klass->dex_cache_->GetStrings();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700744 dst->dex_cache_types_ = klass->dex_cache_->GetTypes();
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700745 dst->dex_cache_methods_ = klass->dex_cache_->GetMethods();
746 dst->dex_cache_fields_ = klass->dex_cache_->GetFields();
747
Brian Carlstrom934486c2011-07-12 23:42:50 -0700748 // TODO: check for finalize method
749
Brian Carlstromf615a612011-07-23 12:50:34 -0700750 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700751 if (code_item != NULL) {
752 dst->num_registers_ = code_item->registers_size_;
753 dst->num_ins_ = code_item->ins_size_;
754 dst->num_outs_ = code_item->outs_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700755 } else {
756 uint16_t num_args = dst->NumArgRegisters();
757 if (!dst->IsStatic()) {
758 ++num_args;
759 }
760 dst->num_registers_ = dst->num_ins_ + num_args;
761 // TODO: native methods
762 }
763}
764
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700765void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700766 AppendToBootClassPath(dex_file, AllocDexCache(dex_file));
767}
768
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700769void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700770 CHECK(dex_cache != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700771 boot_class_path_.push_back(&dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700772 RegisterDexFile(dex_file, dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700773}
774
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700775void ClassLinker::RegisterDexFile(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700776 RegisterDexFile(dex_file, AllocDexCache(dex_file));
777}
778
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700779void ClassLinker::RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700780 CHECK(dex_cache != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700781 dex_files_.push_back(&dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700782 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700783}
784
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700785const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700786 for (size_t i = 0; i != dex_caches_.size(); ++i) {
787 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700788 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700789 }
790 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700791 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700792 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700793}
794
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700795DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700796 for (size_t i = 0; i != dex_files_.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700797 if (dex_files_[i] == &dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700798 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700799 }
800 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700801 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700802 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700803}
804
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700805Class* ClassLinker::CreatePrimitiveClass(const char* descriptor) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700806 Class* klass = AllocClass(sizeof(Class));
Carl Shapiro565f5072011-07-10 13:39:43 -0700807 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700808 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700809 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700810 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Carl Shapiro565f5072011-07-10 13:39:43 -0700811 klass->status_ = Class::kStatusInitialized;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700812 bool success = InsertClass(descriptor, klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700813 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700814 return klass;
815}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700816
Brian Carlstrombe977852011-07-19 14:54:54 -0700817// Create an array class (i.e. the class object for the array, not the
818// array itself). "descriptor" looks like "[C" or "[[[[B" or
819// "[Ljava/lang/String;".
820//
821// If "descriptor" refers to an array of primitives, look up the
822// primitive type's internally-generated class object.
823//
824// "loader" is the class loader of the class that's referring to us. It's
825// used to ensure that we're looking for the element type in the right
826// context. It does NOT become the class loader for the array class; that
827// always comes from the base element class.
828//
829// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700830Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700831 const ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700832{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700833 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700834
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700835 // Identify the underlying element class and the array dimension depth.
836 Class* component_type_ = NULL;
837 int array_rank;
838 if (descriptor[1] == '[') {
839 // array of arrays; keep descriptor and grab stuff from parent
840 Class* outer = FindClass(descriptor.substr(1), class_loader);
841 if (outer != NULL) {
842 // want the base class, not "outer", in our component_type_
843 component_type_ = outer->component_type_;
844 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700845 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700846 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700847 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700848 } else {
849 array_rank = 1;
850 if (descriptor[1] == 'L') {
851 // array of objects; strip off "[" and look up descriptor.
852 const StringPiece subDescriptor = descriptor.substr(1);
853 component_type_ = FindClass(subDescriptor, class_loader);
854 } else {
855 // array of a primitive type
856 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700857 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700858 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700859
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700860 if (component_type_ == NULL) {
861 // failed
862 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
863 return NULL;
864 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700865
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700866 // See if the component type is already loaded. Array classes are
867 // always associated with the class loader of their underlying
868 // element type -- an array of Strings goes with the loader for
869 // java/lang/String -- so we need to look for it there. (The
870 // caller should have checked for the existence of the class
871 // before calling here, but they did so with *their* class loader,
872 // not the component type's loader.)
873 //
874 // If we find it, the caller adds "loader" to the class' initiating
875 // loader list, which should prevent us from going through this again.
876 //
877 // This call is unnecessary if "loader" and "component_type_->class_loader_"
878 // are the same, because our caller (FindClass) just did the
879 // lookup. (Even if we get this wrong we still have correct behavior,
880 // because we effectively do this lookup again when we add the new
881 // class to the hash table --- necessary because of possible races with
882 // other threads.)
883 if (class_loader != component_type_->class_loader_) {
884 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
885 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700886 return new_class;
887 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700888 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700889
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700890 // Fill out the fields in the Class.
891 //
892 // It is possible to execute some methods against arrays, because
893 // all arrays are subclasses of java_lang_Object_, so we need to set
894 // up a vtable. We can just point at the one in java_lang_Object_.
895 //
896 // Array classes are simple enough that we don't need to do a full
897 // link step.
898
899 Class* new_class = NULL;
900 if (!init_done_) {
901 if (descriptor == "[Ljava/lang/Object;") {
902 new_class = GetClassRoot(kObjectArrayClass);
903 } else if (descriptor == "[C") {
904 new_class = GetClassRoot(kCharArrayClass);
905 }
906 }
907 if (new_class == NULL) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700908 new_class = AllocClass(sizeof(Class));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700909 if (new_class == NULL) {
910 return NULL;
911 }
912 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700913 new_class->descriptor_ = String::AllocFromModifiedUtf8(descriptor.ToString().c_str());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700914 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
915 new_class->super_class_ = java_lang_Object;
916 new_class->vtable_ = java_lang_Object->vtable_;
917 new_class->primitive_type_ = Class::kPrimNot;
918 new_class->component_type_ = component_type_;
919 new_class->class_loader_ = component_type_->class_loader_;
920 new_class->array_rank_ = array_rank;
921 new_class->status_ = Class::kStatusInitialized;
922 // don't need to set new_class->object_size_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700923 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700924
925
926 // All arrays have java/lang/Cloneable and java/io/Serializable as
927 // interfaces. We need to set that up here, so that stuff like
928 // "instanceof" works right.
929 //
930 // Note: The GC could run during the call to FindSystemClass,
931 // so we need to make sure the class object is GC-valid while we're in
932 // there. Do this by clearing the interface list so the GC will just
933 // think that the entries are null.
934
935
936 // Use the single, global copies of "interfaces" and "iftable"
937 // (remember not to free them for arrays).
938 DCHECK(array_interfaces_ != NULL);
939 new_class->interfaces_ = array_interfaces_;
940 new_class->iftable_count_ = 2;
941 DCHECK(array_iftable_ != NULL);
942 new_class->iftable_ = array_iftable_;
943
944 // Inherit access flags from the component type. Arrays can't be
945 // used as a superclass or interface, so we want to add "final"
946 // and remove "interface".
947 //
948 // Don't inherit any non-standard flags (e.g., kAccFinal)
949 // from component_type_. We assume that the array class does not
950 // override finalize().
951 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
952 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
953
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700954 if (InsertClass(descriptor, new_class)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700955 return new_class;
956 }
957 // Another thread must have loaded the class after we
958 // started but before we finished. Abandon what we've
959 // done.
960 //
961 // (Yes, this happens.)
962
963 // Grab the winning class.
964 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
965 DCHECK(other_class != NULL);
966 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700967}
968
969Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700970 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700971 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700972 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700973 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700974 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700975 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700976 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700977 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700978 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700979 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700980 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700981 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700982 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700983 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700984 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700985 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700986 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700987 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700988 return GetClassRoot(kPrimitiveVoid);
Carl Shapiro744ad052011-08-06 15:53:36 -0700989 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700990 std::string printable_type(PrintableChar(type));
991 Thread::Current()->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
992 "Not a primitive type: %s", printable_type.c_str());
993 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700994}
995
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700996bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) {
997 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700998 MutexLock mu(classes_lock_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700999 Table::iterator it = classes_.insert(std::make_pair(hash, klass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001000 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001001}
1002
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001003Class* ClassLinker::LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001004 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001005 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001006 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001007 for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001008 Class* klass = it->second;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001009 if (klass->descriptor_->Equals(descriptor) && klass->class_loader_ == class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001010 return klass;
1011 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001012 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001013 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001014}
1015
1016bool ClassLinker::InitializeClass(Class* klass) {
1017 CHECK(klass->GetStatus() == Class::kStatusResolved ||
1018 klass->GetStatus() == Class::kStatusError);
1019
Carl Shapirob5573532011-07-12 18:22:59 -07001020 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001021
1022 {
1023 ObjectLock lock(klass);
1024
1025 if (klass->GetStatus() < Class::kStatusVerified) {
1026 if (klass->IsErroneous()) {
1027 LG << "re-initializing failed class"; // TODO: throw
1028 return false;
1029 }
1030
1031 CHECK(klass->GetStatus() == Class::kStatusResolved);
1032
1033 klass->status_ = Class::kStatusVerifying;
1034 if (!DexVerify::VerifyClass(klass)) {
1035 LG << "Verification failed"; // TODO: ThrowVerifyError
1036 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001037 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001038 klass->SetStatus(Class::kStatusError);
1039 return false;
1040 }
1041
1042 klass->SetStatus(Class::kStatusVerified);
1043 }
1044
1045 if (klass->status_ == Class::kStatusInitialized) {
1046 return true;
1047 }
1048
1049 while (klass->status_ == Class::kStatusInitializing) {
1050 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -07001051 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001052 LG << "recursive <clinit>";
1053 return true;
1054 }
1055
1056 CHECK(!self->IsExceptionPending());
1057
1058 lock.Wait(); // TODO: check for interruption
1059
1060 // When we wake up, repeat the test for init-in-progress. If
1061 // there's an exception pending (only possible if
1062 // "interruptShouldThrow" was set), bail out.
1063 if (self->IsExceptionPending()) {
1064 CHECK(false);
1065 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
1066 klass->SetStatus(Class::kStatusError);
1067 return false;
1068 }
1069 if (klass->GetStatus() == Class::kStatusInitializing) {
1070 continue;
1071 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001072 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001073 klass->GetStatus() == Class::kStatusError);
1074 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001075 // The caller wants an exception, but it was thrown in a
1076 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001077 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
1078 return false;
1079 }
1080 return true; // otherwise, initialized
1081 }
1082
1083 // see if we failed previously
1084 if (klass->IsErroneous()) {
1085 // might be wise to unlock before throwing; depends on which class
1086 // it is that we have locked
1087
1088 // TODO: throwEarlierClassFailure(klass);
1089 return false;
1090 }
1091
1092 if (!ValidateSuperClassDescriptors(klass)) {
1093 klass->SetStatus(Class::kStatusError);
1094 return false;
1095 }
1096
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001097 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098
Carl Shapirob5573532011-07-12 18:22:59 -07001099 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001100 klass->status_ = Class::kStatusInitializing;
1101 }
1102
1103 if (!InitializeSuperClass(klass)) {
1104 return false;
1105 }
1106
1107 InitializeStaticFields(klass);
1108
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001109 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001110 if (clinit != NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001111 // JValue unused;
1112 // TODO: dvmCallMethod(self, method, NULL, &unused);
Elliott Hughes53b61312011-08-12 18:28:20 -07001113 // UNIMPLEMENTED(FATAL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001114 }
1115
1116 {
1117 ObjectLock lock(klass);
1118
1119 if (self->IsExceptionPending()) {
1120 klass->SetStatus(Class::kStatusError);
1121 } else {
1122 klass->SetStatus(Class::kStatusInitialized);
1123 }
1124 lock.NotifyAll();
1125 }
1126
1127 return true;
1128}
1129
1130bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
1131 if (klass->IsInterface()) {
1132 return true;
1133 }
1134 // begin with the methods local to the superclass
1135 if (klass->HasSuperClass() &&
1136 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
1137 const Class* super = klass->GetSuperClass();
1138 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001139 const Method* method = super->GetVirtualMethod(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001140 if (method != super->GetVirtualMethod(i) &&
1141 !HasSameMethodDescriptorClasses(method, super, klass)) {
1142 LG << "Classes resolve differently in superclass";
1143 return false;
1144 }
1145 }
1146 }
1147 for (size_t i = 0; i < klass->iftable_count_; ++i) {
1148 const InterfaceEntry* iftable = &klass->iftable_[i];
1149 Class* interface = iftable->GetClass();
1150 if (klass->GetClassLoader() != interface->GetClassLoader()) {
1151 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1152 uint32_t vtable_index = iftable->method_index_array_[j];
1153 const Method* method = klass->GetVirtualMethod(vtable_index);
1154 if (!HasSameMethodDescriptorClasses(method, interface,
1155 method->GetClass())) {
1156 LG << "Classes resolve differently in interface"; // TODO: LinkageError
1157 return false;
1158 }
1159 }
1160 }
1161 }
1162 return true;
1163}
1164
1165bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001166 const Class* klass1,
1167 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001168 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
1169 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001170 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001171 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001172 const char* descriptor = it->GetDescriptor();
1173 if (descriptor == NULL) {
1174 break;
1175 }
1176 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1177 // Found a non-primitive type.
1178 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1179 return false;
1180 }
1181 }
1182 }
1183 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001184 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001185 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1186 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1187 return false;
1188 }
1189 }
1190 return true;
1191}
1192
1193// Returns true if classes referenced by the descriptor are the
1194// same classes in klass1 as they are in klass2.
1195bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001196 const Class* klass1,
1197 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001198 CHECK(descriptor != NULL);
1199 CHECK(klass1 != NULL);
1200 CHECK(klass2 != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001201 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001202 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001203 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001204 // TODO: found2 == NULL
1205 // TODO: lookup found1 in initiating loader list
1206 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -07001207 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001208 if (found1 == found2) {
1209 return true;
1210 } else {
1211 return false;
1212 }
1213 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001214 return true;
1215}
1216
1217bool ClassLinker::InitializeSuperClass(Class* klass) {
1218 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001219 if (!klass->IsInterface() && klass->HasSuperClass()) {
1220 Class* super_class = klass->GetSuperClass();
1221 if (super_class->GetStatus() != Class::kStatusInitialized) {
1222 CHECK(!super_class->IsInterface());
1223 klass->MonitorExit();
1224 bool super_initialized = InitializeClass(super_class);
1225 klass->MonitorEnter();
1226 // TODO: check for a pending exception
1227 if (!super_initialized) {
1228 klass->SetStatus(Class::kStatusError);
1229 klass->NotifyAll();
1230 return false;
1231 }
1232 }
1233 }
1234 return true;
1235}
1236
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001237bool ClassLinker::EnsureInitialized(Class* c) {
1238 CHECK(c != NULL);
1239 if (c->IsInitialized()) {
1240 return true;
1241 }
1242
1243 c->MonitorExit();
1244 InitializeClass(c);
1245 c->MonitorEnter();
1246 return !Thread::Current()->IsExceptionPending();
1247}
1248
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001249void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001250 size_t num_static_fields = klass->NumStaticFields();
1251 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001252 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001253 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001254 DexCache* dex_cache = klass->GetDexCache();
Brian Carlstrom4873d462011-08-21 15:23:39 -07001255 // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray?
Brian Carlstromf615a612011-07-23 12:50:34 -07001256 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001257 return;
1258 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001259 const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001260 const DexFile& dex_file = FindDexFile(dex_cache);
1261 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001262 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001263 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001264 if (addr == NULL) {
1265 // All this class' static fields have default values.
1266 return;
1267 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001268 size_t array_size = DecodeUnsignedLeb128(&addr);
1269 for (size_t i = 0; i < array_size; ++i) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001270 Field* field = klass->GetStaticField(i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001271 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001272 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001273 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001274 case DexFile::kByte:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001275 field->SetByte(NULL, value.b);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001276 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001277 case DexFile::kShort:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001278 field->SetShort(NULL, value.s);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001279 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001280 case DexFile::kChar:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001281 field->SetChar(NULL, value.c);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001282 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001283 case DexFile::kInt:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001284 field->SetInt(NULL, value.i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001285 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001286 case DexFile::kLong:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001287 field->SetLong(NULL, value.j);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001288 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001289 case DexFile::kFloat:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001290 field->SetFloat(NULL, value.f);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001291 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001292 case DexFile::kDouble:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001293 field->SetDouble(NULL, value.d);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001294 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001295 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001296 uint32_t string_idx = value.i;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001297 String* resolved = ResolveString(dex_file, string_idx, klass->GetDexCache());
Brian Carlstrom4873d462011-08-21 15:23:39 -07001298 field->SetObject(NULL, resolved);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001299 break;
1300 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001301 case DexFile::kBoolean:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001302 field->SetBoolean(NULL, value.z);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001303 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001304 case DexFile::kNull:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001305 field->SetObject(NULL, value.l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001306 break;
1307 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001308 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001309 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001310 }
1311}
1312
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001313bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1314 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001315 if (!LinkSuperClass(klass)) {
1316 return false;
1317 }
1318 if (!LinkMethods(klass)) {
1319 return false;
1320 }
1321 if (!LinkInstanceFields(klass)) {
1322 return false;
1323 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001324 if (!LinkStaticFields(klass)) {
1325 return false;
1326 }
1327 CreateReferenceInstanceOffsets(klass);
1328 CreateReferenceStaticOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001329 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001330 klass->status_ = Class::kStatusResolved;
1331 return true;
1332}
1333
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001334bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1335 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001336 if (klass->super_class_type_idx_ != DexFile::kDexNoIndex) {
1337 Class* super_class = ResolveType(dex_file, klass->super_class_type_idx_, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001338 if (super_class == NULL) {
1339 LG << "Failed to resolve superclass";
1340 return false;
1341 }
1342 klass->super_class_ = super_class; // TODO: write barrier
1343 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001344 if (klass->NumInterfaces() > 0) {
1345 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001346 uint32_t type_idx = klass->interfaces_type_idx_[i];
1347 klass->SetInterface(i, ResolveType(dex_file, type_idx, klass));
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001348 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001349 LG << "Failed to resolve interface";
1350 return false;
1351 }
1352 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001353 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001354 LG << "Inaccessible interface";
1355 return false;
1356 }
1357 }
1358 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001359 // Mark the class as loaded.
1360 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001361 return true;
1362}
1363
1364bool ClassLinker::LinkSuperClass(Class* klass) {
1365 CHECK(!klass->IsPrimitive());
1366 const Class* super = klass->GetSuperClass();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001367 if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001368 if (super != NULL) {
1369 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1370 return false;
1371 }
1372 // TODO: clear finalize attribute
1373 return true;
1374 }
1375 if (super == NULL) {
1376 LG << "No superclass defined"; // TODO: LinkageError
1377 return false;
1378 }
1379 // Verify
1380 if (super->IsFinal()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001381 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001382 return false;
1383 }
1384 if (super->IsInterface()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001385 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001386 return false;
1387 }
1388 if (!klass->CanAccess(super)) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001389 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001390 return false;
1391 }
1392 return true;
1393}
1394
1395// Populate the class vtable and itable.
1396bool ClassLinker::LinkMethods(Class* klass) {
1397 if (klass->IsInterface()) {
1398 // No vtable.
1399 size_t count = klass->NumVirtualMethods();
1400 if (!IsUint(16, count)) {
1401 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1402 return false;
1403 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001404 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001405 klass->GetVirtualMethod(i)->method_index_ = i;
1406 }
1407 } else {
1408 // Link virtual method tables
1409 LinkVirtualMethods(klass);
1410
1411 // Link interface method tables
1412 LinkInterfaceMethods(klass);
1413
1414 // Insert stubs.
1415 LinkAbstractMethods(klass);
1416 }
1417 return true;
1418}
1419
1420bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001421 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001422 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001423 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1424 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001425 // TODO: do not assign to the vtable field until it is fully constructed.
1426 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001427 // See if any of our virtual methods override the superclass.
1428 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1429 Method* local_method = klass->GetVirtualMethod(i);
1430 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001431 for (; j < actual_count; ++j) {
1432 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001433 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001434 // Verify
1435 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001436 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001437 return false;
1438 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001439 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001440 local_method->method_index_ = j;
1441 break;
1442 }
1443 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001444 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001445 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001446 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001447 local_method->method_index_ = actual_count;
1448 actual_count += 1;
1449 }
1450 }
1451 if (!IsUint(16, actual_count)) {
1452 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1453 return false;
1454 }
1455 CHECK_LE(actual_count, max_count);
1456 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001457 // TODO: do not assign to the vtable field until it is fully constructed.
1458 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001460 } else {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001461 CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001462 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001463 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001464 LG << "Too many methods"; // TODO: VirtualMachineError
1465 return false;
1466 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001467 // TODO: do not assign to the vtable field until it is fully constructed.
1468 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1469 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001470 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001471 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1472 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001473 }
1474 return true;
1475}
1476
1477bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1478 int pool_offset = 0;
1479 int pool_size = 0;
1480 int miranda_count = 0;
1481 int miranda_alloc = 0;
1482 size_t super_ifcount;
1483 if (klass->HasSuperClass()) {
1484 super_ifcount = klass->GetSuperClass()->iftable_count_;
1485 } else {
1486 super_ifcount = 0;
1487 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001488 size_t ifcount = super_ifcount;
1489 ifcount += klass->NumInterfaces();
1490 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1491 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001492 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001493 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001494 DCHECK(klass->iftable_count_ == 0);
1495 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001496 return true;
1497 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001498 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1499 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001500 if (super_ifcount != 0) {
1501 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1502 sizeof(InterfaceEntry) * super_ifcount);
1503 }
1504 // Flatten the interface inheritance hierarchy.
1505 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001506 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1507 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001508 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001509 if (!interf->IsInterface()) {
1510 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1511 return false;
1512 }
1513 klass->iftable_[idx++].SetClass(interf);
1514 for (size_t j = 0; j < interf->iftable_count_; j++) {
1515 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1516 }
1517 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001518 CHECK_EQ(idx, ifcount);
1519 klass->iftable_count_ = ifcount;
1520 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001521 return true;
1522 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001523 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001524 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1525 }
1526 if (pool_size == 0) {
1527 return true;
1528 }
1529 klass->ifvi_pool_count_ = pool_size;
1530 klass->ifvi_pool_ = new uint32_t[pool_size];
1531 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001532 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001533 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1534 Class* interface = klass->iftable_[i].GetClass();
1535 pool_offset += interface->NumVirtualMethods(); // end here
1536 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1537 Method* interface_method = interface->GetVirtualMethod(j);
1538 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001539 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001540 Method* vtable_method = klass->vtable_->Get(k);
1541 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1542 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001543 LG << "Implementation not public";
1544 return false;
1545 }
1546 klass->iftable_[i].method_index_array_[j] = k;
1547 break;
1548 }
1549 }
1550 if (k < 0) {
1551 if (miranda_count == miranda_alloc) {
1552 miranda_alloc += 8;
1553 if (miranda_list.empty()) {
1554 miranda_list.resize(miranda_alloc);
1555 } else {
1556 miranda_list.resize(miranda_alloc);
1557 }
1558 }
1559 int mir;
1560 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001561 Method* miranda_method = miranda_list[mir];
1562 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001563 break;
1564 }
1565 }
1566 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001567 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001568 if (mir == miranda_count) {
1569 miranda_list[miranda_count++] = interface_method;
1570 }
1571 }
1572 }
1573 }
1574 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001575 int old_method_count = klass->NumVirtualMethods();
1576 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001577 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001578
1579 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001580 int old_vtable_count = klass->vtable_->GetLength();
1581 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001582 // TODO: do not assign to the vtable field until it is fully constructed.
1583 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001584
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001585 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001586 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001587 memcpy(meth, miranda_list[i], sizeof(Method));
1588 meth->klass_ = klass;
1589 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001590 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1591 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001592 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001593 }
1594 }
1595 return true;
1596}
1597
1598void ClassLinker::LinkAbstractMethods(Class* klass) {
1599 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1600 Method* method = klass->GetVirtualMethod(i);
1601 if (method->IsAbstract()) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001602 LG << "AbstractMethodError";
1603 method->code_off_ = 0xFFFFFFFF;
1604 // TODO: throw AbstractMethodError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001605 }
1606 }
1607}
1608
1609bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001610 CHECK(klass != NULL);
1611 size_t field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001612 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001613 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001614 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001615 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001616 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001617 return LinkFields(field_offset,
1618 klass->num_reference_instance_fields_,
1619 klass->NumInstanceFields(),
1620 klass->ifields_,
1621 klass->object_size_);
1622}
1623
1624bool ClassLinker::LinkStaticFields(Class* klass) {
1625 CHECK(klass != NULL);
1626 size_t allocated_class_size = klass->class_size_;
1627 size_t field_offset = OFFSETOF_MEMBER(Class, fields_);
1628 bool success = LinkFields(field_offset,
1629 klass->num_reference_static_fields_,
1630 klass->NumStaticFields(),
1631 klass->sfields_,
1632 klass->class_size_);
1633 CHECK_EQ(allocated_class_size, klass->class_size_);
1634 return success;
1635}
1636
1637bool ClassLinker::LinkFields(size_t field_offset,
1638 size_t& num_reference_fields,
1639 size_t num_fields,
1640 ObjectArray<Field>* fields,
1641 size_t& size) {
1642 CHECK((num_fields == 0) == (fields == NULL));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001643 // Move references to the front.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001644 num_reference_fields = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001645 size_t i = 0;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001646 for ( ; i < num_fields; i++) {
1647 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001648 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001649 if (c != '[' && c != 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001650 for (size_t j = num_fields - 1; j > i; j--) {
1651 Field* refField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001652 char rc = refField->GetType();
1653 if (rc == '[' || rc == 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001654 fields->Set(i, refField);
1655 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001656 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001657 c = rc;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001658 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001659 break;
1660 }
1661 }
1662 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001663 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001664 }
1665 if (c != '[' && c != 'L') {
1666 break;
1667 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001668 pField->SetOffset(field_offset);
1669 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001670 }
1671
1672 // Now we want to pack all of the double-wide fields together. If
1673 // we're not aligned, though, we want to shuffle one 32-bit field
1674 // into place. If we can't find one, we'll have to pad it.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001675 if (i != num_fields && (field_offset & 0x04) != 0) {
1676 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001677 char c = pField->GetType();
1678
1679 if (c != 'J' && c != 'D') {
1680 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001681 DCHECK(c != '[');
1682 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001683 pField->SetOffset(field_offset);
1684 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001685 i++;
1686 } else {
1687 // Next field is 64-bit, so search for a 32-bit field we can
1688 // swap into it.
1689 bool found = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001690 for (size_t j = num_fields - 1; j > i; j--) {
1691 Field* singleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001692 char rc = singleField->GetType();
1693 if (rc != 'J' && rc != 'D') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001694 fields->Set(i, singleField);
1695 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001696 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001697 pField->SetOffset(field_offset);
1698 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001699 found = true;
1700 i++;
1701 break;
1702 }
1703 }
1704 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001705 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001706 }
1707 }
1708 }
1709
1710 // Alignment is good, shuffle any double-wide fields forward, and
1711 // finish assigning field offsets to all fields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001712 DCHECK(i == num_fields || (field_offset & 0x04) == 0);
1713 for ( ; i < num_fields; i++) {
1714 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001715 char c = pField->GetType();
1716 if (c != 'D' && c != 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001717 for (size_t j = num_fields - 1; j > i; j--) {
1718 Field* doubleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001719 char rc = doubleField->GetType();
1720 if (rc == 'D' || rc == 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001721 fields->Set(i, doubleField);
1722 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001723 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001724 c = rc;
1725 break;
1726 }
1727 }
1728 } else {
1729 // This is a double-wide field, leave it be.
1730 }
1731
Brian Carlstroma0808032011-07-18 00:39:23 -07001732 pField->SetOffset(field_offset);
1733 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001734 if (c == 'J' || c == 'D') {
Brian Carlstroma0808032011-07-18 00:39:23 -07001735 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001736 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001737 }
1738
1739#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001740 // Make sure that all reference fields appear before
1741 // non-reference fields, and all double-wide fields are aligned.
1742 bool seen_non_ref = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001743 for (i = 0; i < num_fields; i++) {
1744 Field *pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001745 char c = pField->GetType();
1746
1747 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001748 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001749 }
1750
1751 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001752 if (!seen_non_ref) {
1753 seen_non_ref = true;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001754 DCHECK_EQ(num_reference_fields, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001755 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001756 } else {
1757 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001758 }
1759 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001760 if (!seen_non_ref) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001761 DCHECK_EQ(num_fields, num_reference_fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001762 }
1763#endif
Brian Carlstrom4873d462011-08-21 15:23:39 -07001764 size = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001765 return true;
1766}
1767
1768// Set the bitmap of reference offsets, refOffsets, from the ifields
1769// list.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001770void ClassLinker::CreateReferenceInstanceOffsets(Class* klass) {
1771 klass->reference_instance_offsets_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001772 if (klass->HasSuperClass()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001773 klass->reference_instance_offsets_ = klass->GetSuperClass()->GetReferenceInstanceOffsets();
1774 // If our superclass overflowed, we don't stand a chance.
1775 if (klass->reference_instance_offsets_ == CLASS_WALK_SUPER) {
1776 return;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001777 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001778 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001779 CreateReferenceOffsets(klass->reference_instance_offsets_,
1780 klass->NumReferenceInstanceFields(),
1781 klass->ifields_);
1782}
1783
1784void ClassLinker::CreateReferenceStaticOffsets(Class* klass) {
1785 klass->reference_static_offsets_ = 0;
1786 CreateReferenceOffsets(klass->reference_static_offsets_,
1787 klass->NumReferenceStaticFields(),
1788 klass->sfields_);
1789}
1790
1791void ClassLinker::CreateReferenceOffsets(uint32_t& reference_offsets,
1792 size_t num_reference_fields,
1793 const ObjectArray<Field>* fields) {
1794 // All of the fields that contain object references are guaranteed
1795 // to be at the beginning of the fields list.
1796 for (size_t i = 0; i < num_reference_fields; ++i) {
1797 // Note that byte_offset is the offset from the beginning of
1798 // object, not the offset into instance data
1799 const Field* field = fields->Get(i);
1800 size_t byte_offset = field->GetOffset();
1801 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
1802 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
1803 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1804 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
1805 CHECK_NE(new_bit, 0U);
1806 reference_offsets |= new_bit;
1807 } else {
1808 reference_offsets = CLASS_WALK_SUPER;
1809 break;
1810 }
1811 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001812}
1813
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001814String* ClassLinker::ResolveString(const DexFile& dex_file,
1815 uint32_t string_idx,
1816 DexCache* dex_cache) {
1817 String* resolved = dex_cache->GetResolvedString(string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001818 if (resolved != NULL) {
1819 return resolved;
1820 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001821 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1822 int32_t utf16_length = dex_file.GetStringLength(string_id);
1823 const char* utf8_data = dex_file.GetStringData(string_id);
1824 String* string = intern_table_.Intern(utf16_length, utf8_data);
1825 dex_cache->SetResolvedString(string_idx, string);
1826 return string;
1827}
1828
1829Class* ClassLinker::ResolveType(const DexFile& dex_file,
1830 uint32_t type_idx,
1831 DexCache* dex_cache,
1832 const ClassLoader* class_loader) {
1833 Class* resolved = dex_cache->GetResolvedType(type_idx);
1834 if (resolved != NULL) {
1835 return resolved;
1836 }
1837 const char* descriptor = dex_file.dexStringByTypeIdx(type_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001838 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001839 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001840 } else {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001841 resolved = FindClass(descriptor, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001842 }
1843 if (resolved != NULL) {
1844 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001845 if (dex_cache != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001846 if (check->GetClassLoader() != NULL) {
1847 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1848 return NULL;
1849 }
1850 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001851 dex_cache->SetResolvedType(type_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001852 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001853 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001854 }
1855 return resolved;
1856}
1857
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001858Method* ClassLinker::ResolveMethod(const DexFile& dex_file,
1859 uint32_t method_idx,
1860 DexCache* dex_cache,
1861 const ClassLoader* class_loader,
1862 /*MethodType*/ int method_type) {
1863 Method* resolved = dex_cache->GetResolvedMethod(method_idx);
1864 if (resolved != NULL) {
1865 return resolved;
1866 }
1867 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
1868 Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader);
1869 if (klass == NULL) {
1870 return NULL;
1871 }
1872
1873 // TODO resolve using class, method_id, and method type.
1874 // resolved = ...
1875 if (resolved != NULL) {
1876 dex_cache->SetResolvedMethod(method_idx, resolved);
1877 } else {
1878 // DCHECK(Thread::Current()->IsExceptionPending());
1879 }
1880 return resolved;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001881}
1882
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001883Field* ClassLinker::ResolveField(const DexFile& dex_file,
1884 uint32_t field_idx,
1885 DexCache* dex_cache,
1886 const ClassLoader* class_loader,
1887 bool is_static) {
1888 Field* resolved = dex_cache->GetResolvedField(field_idx);
1889 if (resolved != NULL) {
1890 return resolved;
1891 }
1892 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx);
1893 Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader);
1894 if (klass == NULL) {
1895 return NULL;
1896 }
1897
1898 // TODO resolve using class, field_id, and is_static.
1899 // resolved = ...
1900 if (resolved != NULL) {
1901 dex_cache->SetResolvedfield(field_idx, resolved);
1902 } else {
1903 // DCHECK(Thread::Current()->IsExceptionPending());
1904 }
1905 return resolved;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001906}
1907
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001908} // namespace art