blob: 0df0cb778a7a7be2c14242a5a91b7e0381017d46 [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
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07005#include <string>
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07006#include <utility>
Elliott Hughes90a33692011-08-30 13:27:07 -07007#include <vector>
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07008
Elliott Hughes90a33692011-08-30 13:27:07 -07009#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "casts.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070011#include "class_loader.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070012#include "dex_cache.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070013#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "dex_verifier.h"
15#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070016#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "logging.h"
18#include "monitor.h"
19#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070020#include "runtime.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070021#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "thread.h"
23#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070024
25namespace art {
26
Brian Carlstroma663ea52011-08-19 23:33:41 -070027const char* ClassLinker::class_roots_descriptors_[kClassRootsMax] = {
28 "Ljava/lang/Class;",
29 "Ljava/lang/Object;",
30 "[Ljava/lang/Object;",
31 "Ljava/lang/String;",
32 "Ljava/lang/reflect/Field;",
33 "Ljava/lang/reflect/Method;",
34 "Ljava/lang/ClassLoader;",
35 "Ldalvik/system/BaseDexClassLoader;",
36 "Ldalvik/system/PathClassLoader;",
Shih-wei Liao55df06b2011-08-26 14:39:27 -070037 "Ljava/lang/StackTraceElement;",
Brian Carlstroma663ea52011-08-19 23:33:41 -070038 "Z",
39 "B",
40 "C",
41 "D",
42 "F",
43 "I",
44 "J",
45 "S",
46 "V",
47 "[Z",
48 "[B",
49 "[C",
50 "[D",
51 "[F",
52 "[I",
53 "[J",
54 "[S",
Shih-wei Liao55df06b2011-08-26 14:39:27 -070055 "[Ljava/lang/StackTraceElement;",
Brian Carlstroma663ea52011-08-19 23:33:41 -070056};
57
Elliott Hughescf4c6c42011-09-01 15:16:42 -070058ClassLinker* ClassLinker::Create(const std::vector<const DexFile*>& boot_class_path,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070059 const std::vector<const DexFile*>& class_path,
60 InternTable* intern_table, Space* space) {
61 CHECK_NE(boot_class_path.size(), 0U);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070062 UniquePtr<ClassLinker> class_linker(new ClassLinker(intern_table));
Brian Carlstroma663ea52011-08-19 23:33:41 -070063 if (space == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070064 class_linker->Init(boot_class_path, class_path);
Brian Carlstroma663ea52011-08-19 23:33:41 -070065 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070066 class_linker->Init(boot_class_path, class_path, space);
Brian Carlstroma663ea52011-08-19 23:33:41 -070067 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070068 // TODO: check for failure during initialization
69 return class_linker.release();
70}
71
Elliott Hughescf4c6c42011-09-01 15:16:42 -070072ClassLinker::ClassLinker(InternTable* intern_table)
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070073 : classes_lock_(Mutex::Create("ClassLinker::Lock")),
74 class_roots_(NULL),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070075 array_interfaces_(NULL),
76 array_iftable_(NULL),
Elliott Hughescf4c6c42011-09-01 15:16:42 -070077 init_done_(false),
78 intern_table_(intern_table) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070079}
Brian Carlstroma663ea52011-08-19 23:33:41 -070080
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070081void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path,
82 const std::vector<const DexFile*>& class_path) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070083 CHECK(!init_done_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070084
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070085 // java_lang_Class comes first, its needed for AllocClass
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070086 Class* java_lang_Class = down_cast<Class*>(
87 Heap::AllocObject(NULL, sizeof(ClassClass)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070088 CHECK(java_lang_Class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070089 java_lang_Class->SetClass(java_lang_Class);
90 java_lang_Class->SetClassSize(sizeof(ClassClass));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070091 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070092
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070093 // java_lang_Object comes next so that object_array_class can be created
Brian Carlstrom4873d462011-08-21 15:23:39 -070094 Class* java_lang_Object = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070095 CHECK(java_lang_Object != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070096 // backfill Object as the super class of Class
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070097 java_lang_Class->SetSuperClass(java_lang_Object);
98 java_lang_Object->SetStatus(Class::kStatusLoaded);
Brian Carlstroma0808032011-07-18 00:39:23 -070099
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700100 // Object[] next to hold class roots
Brian Carlstrom4873d462011-08-21 15:23:39 -0700101 Class* object_array_class = AllocClass(java_lang_Class, sizeof(Class));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700102 object_array_class->SetArrayRank(1);
103 object_array_class->SetComponentType(java_lang_Object);
Brian Carlstroma0808032011-07-18 00:39:23 -0700104
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700105 // Setup the char[] class to be used for String
Brian Carlstrom4873d462011-08-21 15:23:39 -0700106 Class* char_array_class = AllocClass(java_lang_Class, sizeof(Class));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700107 char_array_class->SetArrayRank(1);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700108 CharArray::SetArrayClass(char_array_class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700109
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700110 // Setup String
111 Class* java_lang_String = AllocClass(java_lang_Class, sizeof(StringClass));
112 String::SetClass(java_lang_String);
113 java_lang_String->SetObjectSize(sizeof(String));
114 java_lang_String->SetStatus(Class::kStatusResolved);
Jesse Wilson14150742011-07-29 19:04:44 -0400115
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700116 // Backfill Class descriptors missing until this point
117 // TODO: intern these strings
118 java_lang_Class->SetDescriptor(
119 String::AllocFromModifiedUtf8("Ljava/lang/Class;"));
120 java_lang_Object->SetDescriptor(
121 String::AllocFromModifiedUtf8("Ljava/lang/Object;"));
122 object_array_class->SetDescriptor(
123 String::AllocFromModifiedUtf8("[Ljava/lang/Object;"));
124 java_lang_String->SetDescriptor(
125 String::AllocFromModifiedUtf8("Ljava/lang/String;"));
126 char_array_class->SetDescriptor(String::AllocFromModifiedUtf8("[C"));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700127
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700128 // Create storage for root classes, save away our work so far (requires
129 // descriptors)
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700130 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700131 SetClassRoot(kJavaLangClass, java_lang_Class);
132 SetClassRoot(kJavaLangObject, java_lang_Object);
133 SetClassRoot(kObjectArrayClass, object_array_class);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700134 SetClassRoot(kCharArrayClass, char_array_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700135 SetClassRoot(kJavaLangString, java_lang_String);
136
137 // Setup the primitive type classes.
138 SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z", Class::kPrimBoolean));
139 SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B", Class::kPrimByte));
140 SetClassRoot(kPrimitiveChar, CreatePrimitiveClass("C", Class::kPrimChar));
141 SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S", Class::kPrimShort));
142 SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I", Class::kPrimInt));
143 SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J", Class::kPrimLong));
144 SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F", Class::kPrimFloat));
145 SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D", Class::kPrimDouble));
146 SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V", Class::kPrimVoid));
147
148 // Backfill component type of char[]
149 char_array_class->SetComponentType(GetClassRoot(kPrimitiveChar));
150
151 // Create array interface entries to populate once we can load system classes
152 array_interfaces_ = AllocObjectArray<Class>(2);
153 array_iftable_ = new InterfaceEntry[2];
154
155 // Create int array type for AllocDexCache (done in AppendToBootClassPath)
156 Class* int_array_class = AllocClass(java_lang_Class, sizeof(Class));
157 int_array_class->SetArrayRank(1);
158 int_array_class->SetDescriptor(String::AllocFromModifiedUtf8("[I"));
159 int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt));
160 IntArray::SetArrayClass(int_array_class);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700161 SetClassRoot(kIntArrayClass, int_array_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700162
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700163 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -0700164
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700165 // setup boot_class_path_ and register class_path now that we can
166 // use AllocObjectArray to create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700167 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700168 const DexFile* dex_file = boot_class_path[i];
169 CHECK(dex_file != NULL);
170 AppendToBootClassPath(*dex_file);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700171 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700172 for (size_t i = 0; i != class_path.size(); ++i) {
173 const DexFile* dex_file = class_path[i];
174 CHECK(dex_file != NULL);
175 RegisterDexFile(*dex_file);
176 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700177
178 // Field and Method are necessary so that FindClass can link members
179 Class* java_lang_reflect_Field = AllocClass(java_lang_Class, sizeof(FieldClass));
180 CHECK(java_lang_reflect_Field != NULL);
181 java_lang_reflect_Field->SetDescriptor(String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;"));
182 java_lang_reflect_Field->SetObjectSize(sizeof(Field));
183 SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field);
184 java_lang_reflect_Field->SetStatus(Class::kStatusResolved);
185 Field::SetClass(java_lang_reflect_Field);
186
187 Class* java_lang_reflect_Method = AllocClass(java_lang_Class, sizeof(MethodClass));
188 java_lang_reflect_Method->SetDescriptor(String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;"));
189 CHECK(java_lang_reflect_Method != NULL);
190 java_lang_reflect_Method->SetObjectSize(sizeof(Method));
191 SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method);
192 java_lang_reflect_Method->SetStatus(Class::kStatusResolved);
193 Method::SetClass(java_lang_reflect_Method);
194
195 // now we can use FindSystemClass
196
197 // Object and String just need more minimal setup, since they do not have
198 // extra C++ fields.
199 java_lang_Object->SetStatus(Class::kStatusNotReady);
200 Class* Object_class = FindSystemClass("Ljava/lang/Object;");
201 CHECK_EQ(java_lang_Object, Object_class);
202 CHECK_EQ(java_lang_Object->GetObjectSize(), sizeof(Object));
203 java_lang_String->SetStatus(Class::kStatusNotReady);
204 Class* String_class = FindSystemClass("Ljava/lang/String;");
205 CHECK_EQ(java_lang_String, String_class);
206 CHECK_EQ(java_lang_String->GetObjectSize(), sizeof(String));
207
208 // Setup the primitive array type classes - can't be done until Object has
209 // a vtable
210 SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z"));
211 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
212
213 SetClassRoot(kByteArrayClass, FindSystemClass("[B"));
214 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
215
216 Class* found_char_array_class = FindSystemClass("[C");
217 CHECK_EQ(char_array_class, found_char_array_class);
218
219 SetClassRoot(kShortArrayClass, FindSystemClass("[S"));
220 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
221
222 Class* found_int_array_class = FindSystemClass("[I");
223 CHECK_EQ(int_array_class, found_int_array_class);
224
225 SetClassRoot(kLongArrayClass, FindSystemClass("[J"));
226 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
227
228 SetClassRoot(kFloatArrayClass, FindSystemClass("[F"));
229 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
230
231 SetClassRoot(kDoubleArrayClass, FindSystemClass("[D"));
232 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
233
234 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
235 CHECK_EQ(object_array_class, found_object_array_class);
236
237 // Setup the single, global copies of "interfaces" and "iftable"
238 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
239 CHECK(java_lang_Cloneable != NULL);
240 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
241 CHECK(java_io_Serializable != NULL);
242 CHECK(array_interfaces_ != NULL);
243 array_interfaces_->Set(0, java_lang_Cloneable);
244 array_interfaces_->Set(1, java_io_Serializable);
245 // We assume that Cloneable/Serializable don't have superinterfaces --
246 // normally we'd have to crawl up and explicitly list all of the
247 // supers as well. These interfaces don't have any methods, so we
248 // don't have to worry about the ifviPool either.
249 array_iftable_[0].SetInterface(array_interfaces_->Get(0));
250 array_iftable_[1].SetInterface(array_interfaces_->Get(1));
251
252 // Sanity check Object[]'s interfaces
253 CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0));
254 CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1));
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700255
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700256 // run Class, Field, and Method through FindSystemClass.
257 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700258 Class* Class_class = FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700259 CHECK_EQ(java_lang_Class, Class_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700260 // No sanity check on size as Class is variably sized
261
262 java_lang_reflect_Field->SetStatus(Class::kStatusNotReady);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700263 Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700264 CHECK_EQ(java_lang_reflect_Field, Field_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700265
266 java_lang_reflect_Method->SetStatus(Class::kStatusNotReady);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700267 Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700268 CHECK_EQ(java_lang_reflect_Method, Method_class);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700269
270 // java.lang.ref classes need to be specially flagged, but otherwise are normal classes
271 Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700272 java_lang_ref_FinalizerReference->SetAccessFlags(
273 java_lang_ref_FinalizerReference->GetAccessFlags() |
274 kAccClassIsReference | kAccClassIsFinalizerReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700275 Class* java_lang_ref_PhantomReference = FindSystemClass("Ljava/lang/ref/PhantomReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700276 java_lang_ref_PhantomReference->SetAccessFlags(
277 java_lang_ref_PhantomReference->GetAccessFlags() |
278 kAccClassIsReference | kAccClassIsPhantomReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700279 Class* java_lang_ref_SoftReference = FindSystemClass("Ljava/lang/ref/SoftReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700280 java_lang_ref_SoftReference->SetAccessFlags(
281 java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700282 Class* java_lang_ref_WeakReference = FindSystemClass("Ljava/lang/ref/WeakReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 java_lang_ref_WeakReference->SetAccessFlags(
284 java_lang_ref_WeakReference->GetAccessFlags() |
285 kAccClassIsReference | kAccClassIsWeakReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700286
287 // Let the heap know some key offsets into java.lang.ref instances
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700288 // NB we hard code the field indexes here rather than using FindInstanceField
289 // as the types of the field can't be resolved prior to the runtime being
290 // fully initialized
Brian Carlstrom1f870082011-08-23 16:02:11 -0700291 Class* java_lang_ref_Reference = FindSystemClass("Ljava/lang/ref/Reference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700292
293 Field* pendingNext = java_lang_ref_Reference->GetInstanceField(0);
294 CHECK(pendingNext->GetName()->Equals("pendingNext"));
295 CHECK(ResolveType(pendingNext->GetTypeIdx(), pendingNext) ==
296 java_lang_ref_Reference);
297
298 Field* queue = java_lang_ref_Reference->GetInstanceField(1);
299 CHECK(queue->GetName()->Equals("queue"));
300 CHECK(ResolveType(queue->GetTypeIdx(), queue) ==
301 FindSystemClass("Ljava/lang/ref/ReferenceQueue;"));
302
303 Field* queueNext = java_lang_ref_Reference->GetInstanceField(2);
304 CHECK(queueNext->GetName()->Equals("queueNext"));
305 CHECK(ResolveType(queueNext->GetTypeIdx(), queueNext) ==
306 java_lang_ref_Reference);
307
308 Field* referent = java_lang_ref_Reference->GetInstanceField(3);
309 CHECK(referent->GetName()->Equals("referent"));
310 CHECK(ResolveType(referent->GetTypeIdx(), referent) ==
311 java_lang_Object);
312
313 Field* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2);
314 CHECK(zombie->GetName()->Equals("zombie"));
315 CHECK(ResolveType(zombie->GetTypeIdx(), zombie) ==
316 java_lang_Object);
317
Brian Carlstrom1f870082011-08-23 16:02:11 -0700318 Heap::SetReferenceOffsets(referent->GetOffset(),
319 queue->GetOffset(),
320 queueNext->GetOffset(),
321 pendingNext->GetOffset(),
322 zombie->GetOffset());
323
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700324 // Setup the ClassLoaders, adjusting the object_size_ as necessary
325 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
326 CHECK_LT(java_lang_ClassLoader->GetObjectSize(), sizeof(ClassLoader));
327 java_lang_ClassLoader->SetObjectSize(sizeof(ClassLoader));
328 SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader);
329
330 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
331 CHECK_EQ(dalvik_system_BaseDexClassLoader->GetObjectSize(), sizeof(BaseDexClassLoader));
332 SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
333
334 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
335 CHECK_EQ(dalvik_system_PathClassLoader->GetObjectSize(), sizeof(PathClassLoader));
336 SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
337 PathClassLoader::SetClass(dalvik_system_PathClassLoader);
338
339 // Set up java.lang.StackTraceElement as a convenience
Brian Carlstrom1f870082011-08-23 16:02:11 -0700340 SetClassRoot(kJavaLangStackTraceElement, FindSystemClass("Ljava/lang/StackTraceElement;"));
341 SetClassRoot(kJavaLangStackTraceElementArrayClass, FindSystemClass("[Ljava/lang/StackTraceElement;"));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700342 StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700343
Brian Carlstroma663ea52011-08-19 23:33:41 -0700344 FinishInit();
345}
346
347void ClassLinker::FinishInit() {
348 // ensure all class_roots_ are initialized
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700349 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700350 ClassRoot class_root = static_cast<ClassRoot>(i);
351 Class* klass = GetClassRoot(class_root);
352 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353 DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700354 // note SetClassRoot does additional validation.
355 // if possible add new checks there to catch errors early
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700356 }
357
358 // disable the slow paths in FindClass and CreatePrimitiveClass now
359 // that Object, Class, and Object[] are setup
360 init_done_ = true;
361}
362
Brian Carlstroma663ea52011-08-19 23:33:41 -0700363struct ClassLinker::InitCallbackState {
364 ClassLinker* class_linker;
365
366 Class* class_roots[kClassRootsMax];
367
368 typedef std::tr1::unordered_map<std::string, ClassRoot> Table;
369 Table descriptor_to_class_root;
370
Brian Carlstroma663ea52011-08-19 23:33:41 -0700371 typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set;
372 Set dex_caches;
373};
374
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700375void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path,
376 const std::vector<const DexFile*>& class_path,
377 Space* space) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700378 CHECK(!init_done_);
379
380 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
381 DCHECK(heap_bitmap != NULL);
382
383 InitCallbackState state;
384 state.class_linker = this;
385 for (size_t i = 0; i < kClassRootsMax; i++) {
386 ClassRoot class_root = static_cast<ClassRoot>(i);
387 state.descriptor_to_class_root[GetClassRootDescriptor(class_root)] = class_root;
388 }
389
390 // reinit clases_ table
391 heap_bitmap->Walk(InitCallback, &state);
392
393 // reinit class_roots_
394 Class* object_array_class = state.class_roots[kObjectArrayClass];
395 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
396 for (size_t i = 0; i < kClassRootsMax; i++) {
397 ClassRoot class_root = static_cast<ClassRoot>(i);
398 SetClassRoot(class_root, state.class_roots[class_root]);
399 }
400
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700401 // reinit intern table
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700402 // TODO: remove interned_array, make all strings in image interned (and remove space argument)
Brian Carlstroma663ea52011-08-19 23:33:41 -0700403 ObjectArray<Object>* interned_array = space->GetImageHeader().GetInternedArray();
404 for (int32_t i = 0; i < interned_array->GetLength(); i++) {
405 String* string = interned_array->Get(i)->AsString();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700406 intern_table_->RegisterStrong(string);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700407 }
408
409 // reinit array_interfaces_ from any array class instance, they should all be ==
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700410 array_interfaces_ = GetClassRoot(kObjectArrayClass)->GetInterfaces();
411 DCHECK(array_interfaces_ == GetClassRoot(kBooleanArrayClass)->GetInterfaces());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700412
413 // build a map from location to DexCache to match up with DexFile::GetLocation
414 std::tr1::unordered_map<std::string, DexCache*> location_to_dex_cache;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700415 typedef InitCallbackState::Set::const_iterator It; // TODO: C++0x auto
Brian Carlstroma663ea52011-08-19 23:33:41 -0700416 for (It it = state.dex_caches.begin(), end = state.dex_caches.end(); it != end; ++it) {
417 DexCache* dex_cache = *it;
418 std::string location = dex_cache->GetLocation()->ToModifiedUtf8();
419 location_to_dex_cache[location] = dex_cache;
420 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700421 CHECK_EQ(boot_class_path.size() + class_path.size(),
422 location_to_dex_cache.size());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700423
424 // reinit boot_class_path with DexFile arguments and found DexCaches
425 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700426 const DexFile* dex_file = boot_class_path[i];
427 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700428 DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()];
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700429 CHECK(dex_cache != NULL) << dex_file->GetLocation();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700430 AppendToBootClassPath(*dex_file, dex_cache);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700431 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700432
433 // register class_path with DexFile arguments and found DexCaches
434 for (size_t i = 0; i != class_path.size(); ++i) {
435 const DexFile* dex_file = class_path[i];
436 CHECK(dex_file != NULL);
437 DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()];
438 CHECK(dex_cache != NULL) << dex_file->GetLocation();
439 RegisterDexFile(*dex_file, dex_cache);
440 }
441
Brian Carlstroma663ea52011-08-19 23:33:41 -0700442 String::SetClass(GetClassRoot(kJavaLangString));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700443 Field::SetClass(GetClassRoot(kJavaLangReflectField));
444 Method::SetClass(GetClassRoot(kJavaLangReflectMethod));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700445 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
446 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
447 CharArray::SetArrayClass(GetClassRoot(kCharArrayClass));
448 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
449 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
450 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
451 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
452 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700453 PathClassLoader::SetClass(GetClassRoot(kDalvikSystemPathClassLoader));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700454 StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700455
456 FinishInit();
457}
458
Brian Carlstrom4873d462011-08-21 15:23:39 -0700459void ClassLinker::InitCallback(Object* obj, void *arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700460 DCHECK(obj != NULL);
461 DCHECK(arg != NULL);
462 InitCallbackState* state = reinterpret_cast<InitCallbackState*>(arg);
463
464 if (!obj->IsClass()) {
465 return;
466 }
467 Class* klass = obj->AsClass();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700468 // TODO: restore ClassLoader's list of DexFiles after image load
469 // CHECK(klass->GetClassLoader() == NULL);
470 const ClassLoader* class_loader = klass->GetClassLoader();
471 if (class_loader != NULL) {
472 // TODO: replace this hack with something based on command line arguments
473 Thread::Current()->SetClassLoaderOverride(class_loader);
474 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700475
476 std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700477 // restore class to ClassLinker::classes_ table
478 state->class_linker->InsertClass(descriptor, klass);
479
480 // note DexCache to match with DexFile later
481 DexCache* dex_cache = klass->GetDexCache();
482 if (dex_cache != NULL) {
483 state->dex_caches.insert(dex_cache);
484 } else {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700485 DCHECK(klass->IsArrayClass() || klass->IsPrimitive());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700486 }
487
488 // check if this is a root, if so, register it
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700489 typedef InitCallbackState::Table::const_iterator It; // TODO: C++0x auto
Brian Carlstroma663ea52011-08-19 23:33:41 -0700490 It it = state->descriptor_to_class_root.find(descriptor);
491 if (it != state->descriptor_to_class_root.end()) {
492 ClassRoot class_root = it->second;
493 state->class_roots[class_root] = klass;
494 }
495}
496
497// Keep in sync with InitCallback. Anything we visit, we need to
498// reinit references to when reinitializing a ClassLinker from a
499// mapped image.
Elliott Hughes410c0c82011-09-01 17:58:25 -0700500void ClassLinker::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
501 visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700502
503 for (size_t i = 0; i < dex_caches_.size(); i++) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700504 visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700505 }
506
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700507 {
508 MutexLock mu(classes_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700509 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700510 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700511 visitor(it->second, arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700512 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700513 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700514
Elliott Hughes410c0c82011-09-01 17:58:25 -0700515 visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700516}
517
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700518ClassLinker::~ClassLinker() {
519 delete classes_lock_;
520 String::ResetClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700521 Field::ResetClass();
522 Method::ResetClass();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700523 BooleanArray::ResetArrayClass();
524 ByteArray::ResetArrayClass();
525 CharArray::ResetArrayClass();
526 DoubleArray::ResetArrayClass();
527 FloatArray::ResetArrayClass();
528 IntArray::ResetArrayClass();
529 LongArray::ResetArrayClass();
530 ShortArray::ResetArrayClass();
531 PathClassLoader::ResetClass();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700532 StackTraceElement::ResetClass();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700533}
534
535DexCache* ClassLinker::AllocDexCache(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700536 DexCache* dex_cache = down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::LengthAsArray()));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700537 dex_cache->Init(String::AllocFromModifiedUtf8(dex_file.GetLocation().c_str()),
538 AllocObjectArray<String>(dex_file.NumStringIds()),
539 AllocObjectArray<Class>(dex_file.NumTypeIds()),
540 AllocObjectArray<Method>(dex_file.NumMethodIds()),
Brian Carlstrom83db7722011-08-26 17:32:56 -0700541 AllocObjectArray<Field>(dex_file.NumFieldIds()),
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700542 AllocCodeAndDirectMethods(dex_file.NumMethodIds()),
543 AllocObjectArray<StaticStorageBase>(dex_file.NumTypeIds()));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700544 return dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700545}
546
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700547CodeAndDirectMethods* ClassLinker::AllocCodeAndDirectMethods(size_t length) {
548 return down_cast<CodeAndDirectMethods*>(IntArray::Alloc(CodeAndDirectMethods::LengthAsArray(length)));
Brian Carlstrom83db7722011-08-26 17:32:56 -0700549}
550
Brian Carlstrom4873d462011-08-21 15:23:39 -0700551Class* ClassLinker::AllocClass(Class* java_lang_Class, size_t class_size) {
552 DCHECK_GE(class_size, sizeof(Class));
553 Class* klass = Heap::AllocObject(java_lang_Class, class_size)->AsClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700554 klass->SetPrimitiveType(Class::kPrimNot); // default to not being primitive
555 klass->SetClassSize(class_size);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700556 return klass;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700557}
558
Brian Carlstrom4873d462011-08-21 15:23:39 -0700559Class* ClassLinker::AllocClass(size_t class_size) {
560 return AllocClass(GetClassRoot(kJavaLangClass), class_size);
Brian Carlstroma0808032011-07-18 00:39:23 -0700561}
562
Jesse Wilson35baaab2011-08-10 16:18:03 -0400563Field* ClassLinker::AllocField() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700564 return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->AllocObject());
Brian Carlstroma0808032011-07-18 00:39:23 -0700565}
566
567Method* ClassLinker::AllocMethod() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700568 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->AllocObject());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700569}
570
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700571ObjectArray<StackTraceElement>* ClassLinker::AllocStackTraceElementArray(size_t length) {
572 return ObjectArray<StackTraceElement>::Alloc(
573 GetClassRoot(kJavaLangStackTraceElementArrayClass),
574 length);
575}
576
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700577Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700578 const ClassLoader* class_loader) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700579 // TODO: remove this contrived parent class loader check when we have a real ClassLoader.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700580 if (class_loader != NULL) {
581 Class* klass = FindClass(descriptor, NULL);
582 if (klass != NULL) {
583 return klass;
584 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700585 Thread::Current()->ClearException();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700586 }
587
Carl Shapirob5573532011-07-12 18:22:59 -0700588 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700589 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700590 CHECK(!self->IsExceptionPending());
591 // Find the class in the loaded classes table.
592 Class* klass = LookupClass(descriptor, class_loader);
593 if (klass == NULL) {
594 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700595 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700596 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700597 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700598 const DexFile::ClassPath& class_path = ((class_loader != NULL)
599 ? ClassLoader::GetClassPath(class_loader)
600 : boot_class_path_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700601 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700602 if (pair.second == NULL) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700603 std::string name(PrintableString(descriptor));
604 self->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
605 "Class %s not found in class loader %p", name.c_str(), class_loader);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700606 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700607 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700608 const DexFile& dex_file = *pair.first;
609 const DexFile::ClassDef& dex_class_def = *pair.second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700610 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700611 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700612 if (!init_done_) {
613 // finish up init of hand crafted class_roots_
614 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700615 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700616 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700617 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400618 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700619 klass = GetClassRoot(kJavaLangString);
620 } else if (descriptor == "Ljava/lang/reflect/Field;") {
621 klass = GetClassRoot(kJavaLangReflectField);
622 } else if (descriptor == "Ljava/lang/reflect/Method;") {
623 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700624 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700625 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700626 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700627 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700628 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Carl Shapiro565f5072011-07-10 13:39:43 -0700629 }
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700630 if (!klass->IsResolved()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700631 klass->SetDexCache(dex_cache);
632 LoadClass(dex_file, dex_class_def, klass, class_loader);
633 // Check for a pending exception during load
634 if (self->IsExceptionPending()) {
635 // TODO: free native allocations in klass
636 return NULL;
637 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700638 ObjectLock lock(klass);
Elliott Hughesdcc24742011-09-07 14:02:44 -0700639 klass->SetClinitThreadId(self->GetTid());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700640 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700641 bool success = InsertClass(descriptor, klass); // TODO: just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700642 if (!success) {
643 // We may fail to insert if we raced with another thread.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700644 klass->SetClinitThreadId(0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700645 // TODO: free native allocations in klass
646 klass = LookupClass(descriptor, class_loader);
647 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700648 return klass;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700649 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700650 // Finish loading (if necessary) by finding parents
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700651 CHECK(!klass->IsLoaded());
652 if (!LoadSuperAndInterfaces(klass, dex_file)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700653 // Loading failed.
654 // TODO: CHECK(self->IsExceptionPending());
655 lock.NotifyAll();
656 return NULL;
657 }
658 CHECK(klass->IsLoaded());
659 // Link the class (if necessary)
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700660 CHECK(!klass->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700661 if (!LinkClass(klass)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700662 // Linking failed.
663 // TODO: CHECK(self->IsExceptionPending());
664 lock.NotifyAll();
665 return NULL;
666 }
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700667 CHECK(klass->IsResolved());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700668 }
669 }
670 }
671 // Link the class if it has not already been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700672 if (!klass->IsResolved() && !klass->IsErroneous()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700673 ObjectLock lock(klass);
674 // Check for circular dependencies between classes.
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700675 if (!klass->IsResolved() && klass->GetClinitThreadId() == self->GetTid()) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700676 self->ThrowNewException("Ljava/lang/ClassCircularityError;", NULL); // TODO: detail
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700677 return NULL;
678 }
679 // Wait for the pending initialization to complete.
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700680 while (!klass->IsResolved() && !klass->IsErroneous()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700681 lock.Wait();
682 }
683 }
684 if (klass->IsErroneous()) {
685 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
686 return NULL;
687 }
688 // Return the loaded class. No exceptions should be pending.
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700689 CHECK(klass->IsResolved());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700690 CHECK(!self->IsExceptionPending());
691 return klass;
692}
693
Brian Carlstrom4873d462011-08-21 15:23:39 -0700694// Precomputes size that will be needed for Class, matching LinkStaticFields
695size_t ClassLinker::SizeOfClass(const DexFile& dex_file,
696 const DexFile::ClassDef& dex_class_def) {
697 const byte* class_data = dex_file.GetClassData(dex_class_def);
698 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
699 size_t num_static_fields = header.static_fields_size_;
700 size_t num_ref = 0;
701 size_t num_32 = 0;
702 size_t num_64 = 0;
703 if (num_static_fields != 0) {
704 uint32_t last_idx = 0;
705 for (size_t i = 0; i < num_static_fields; ++i) {
706 DexFile::Field dex_field;
707 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
708 const DexFile::FieldId& field_id = dex_file.GetFieldId(dex_field.field_idx_);
709 const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_);
710 char c = descriptor[0];
711 if (c == 'L' || c == '[') {
712 num_ref++;
713 } else if (c == 'J' || c == 'D') {
714 num_64++;
715 } else {
716 num_32++;
717 }
718 }
719 }
720
721 // start with generic class data
722 size_t size = sizeof(Class);
723 // follow with reference fields which must be contiguous at start
724 size += (num_ref * sizeof(uint32_t));
725 // if there are 64-bit fields to add, make sure they are aligned
726 if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment
727 if (num_32 != 0) {
728 // use an available 32-bit field for padding
729 num_32--;
730 }
731 size += sizeof(uint32_t); // either way, we are adding a word
732 DCHECK_EQ(size, RoundUp(size, 8));
733 }
734 // tack on any 64-bit fields now that alignment is assured
735 size += (num_64 * sizeof(uint64_t));
736 // tack on any remaining 32-bit fields
737 size += (num_32 * sizeof(uint32_t));
738 return size;
739}
740
Brian Carlstromf615a612011-07-23 12:50:34 -0700741void ClassLinker::LoadClass(const DexFile& dex_file,
742 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700743 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700744 const ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700745 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700746 CHECK(klass->GetDexCache() != NULL);
747 CHECK_EQ(Class::kStatusNotReady, klass->GetStatus());
Brian Carlstromf615a612011-07-23 12:50:34 -0700748 const byte* class_data = dex_file.GetClassData(dex_class_def);
749 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700750
Brian Carlstromf615a612011-07-23 12:50:34 -0700751 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700752 CHECK(descriptor != NULL);
753
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700754 klass->SetClass(GetClassRoot(kJavaLangClass));
755 if (klass->GetDescriptor() != NULL) {
756 DCHECK(klass->GetDescriptor()->Equals(descriptor));
757 } else {
758 klass->SetDescriptor(String::AllocFromModifiedUtf8(descriptor));
759 }
760 uint32_t access_flags = dex_class_def.access_flags_;
761 // Make sure there aren't any "bonus" flags set, since we use them for runtime
762 // state.
763 CHECK_EQ(access_flags & ~kAccClassFlagsMask, 0U);
764 klass->SetAccessFlags(access_flags);
765 klass->SetClassLoader(class_loader);
766 DCHECK(klass->GetPrimitiveType() == Class::kPrimNot);
767 klass->SetStatus(Class::kStatusIdx);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700768
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700769 klass->SetSuperClassTypeIdx(dex_class_def.superclass_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700770
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700771 size_t num_static_fields = header.static_fields_size_;
772 size_t num_instance_fields = header.instance_fields_size_;
773 size_t num_direct_methods = header.direct_methods_size_;
774 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700775
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700776 klass->SetSourceFile(dex_file.dexGetSourceFile(dex_class_def));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700777
778 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700779 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700780
781 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700782 if (num_static_fields != 0) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700783 klass->SetSFields(AllocObjectArray<Field>(num_static_fields));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700784 uint32_t last_idx = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700785 for (size_t i = 0; i < num_static_fields; ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700786 DexFile::Field dex_field;
787 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400788 Field* sfield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700789 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700790 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700791 }
792 }
793
794 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700795 if (num_instance_fields != 0) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700796 klass->SetIFields(AllocObjectArray<Field>(num_instance_fields));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700797 uint32_t last_idx = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700798 for (size_t i = 0; i < num_instance_fields; ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700799 DexFile::Field dex_field;
800 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400801 Field* ifield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700802 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700803 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700804 }
805 }
806
807 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700808 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700809 // TODO: append direct methods to class object
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700810 klass->SetDirectMethods(AllocObjectArray<Method>(num_direct_methods));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700811 uint32_t last_idx = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700812 for (size_t i = 0; i < num_direct_methods; ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700813 DexFile::Method dex_method;
814 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700815 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700816 klass->SetDirectMethod(i, meth);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700817 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700818 // TODO: register maps
819 }
820 }
821
822 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700823 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700824 // TODO: append virtual methods to class object
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700825 klass->SetVirtualMethods(AllocObjectArray<Method>(num_virtual_methods));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700826 uint32_t last_idx = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700827 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700828 DexFile::Method dex_method;
829 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700830 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700831 klass->SetVirtualMethod(i, meth);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700832 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700833 // TODO: register maps
834 }
835 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700836}
837
Brian Carlstromf615a612011-07-23 12:50:34 -0700838void ClassLinker::LoadInterfaces(const DexFile& dex_file,
839 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700840 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700841 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700842 if (list != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700843 klass->SetInterfaces(AllocObjectArray<Class>(list->Size()));
844 IntArray* interfaces_idx = IntArray::Alloc(list->Size());
845 klass->SetInterfacesTypeIdx(interfaces_idx);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700846 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700847 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700848 interfaces_idx->Set(i, type_item.type_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700849 }
850 }
851}
852
Brian Carlstromf615a612011-07-23 12:50:34 -0700853void ClassLinker::LoadField(const DexFile& dex_file,
854 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700855 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700856 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700857 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700858 dst->SetDeclaringClass(klass);
859 dst->SetName(ResolveString(dex_file, field_id.name_idx_, klass->GetDexCache()));
860 dst->SetTypeIdx(field_id.type_idx_);
861 dst->SetAccessFlags(src.access_flags_);
862
863 // In order to access primitive types using GetTypeDuringLinking we need to
864 // ensure they are resolved into the dex cache
865 const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_);
866 if (descriptor[1] == '\0') {
867 // only the descriptors of primitive types should be 1 character long
868 Class* resolved = ResolveType(dex_file, field_id.type_idx_, klass);
869 DCHECK(resolved->IsPrimitive());
870 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700871}
872
Brian Carlstromf615a612011-07-23 12:50:34 -0700873void ClassLinker::LoadMethod(const DexFile& dex_file,
874 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700875 Class* klass,
Brian Carlstrom1f870082011-08-23 16:02:11 -0700876 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700877 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700878 dst->SetDeclaringClass(klass);
879 dst->SetName(ResolveString(dex_file, method_id.name_idx_, klass->GetDexCache()));
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700880 {
881 int32_t utf16_length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700882 std::string utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_, &utf16_length));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700883 dst->SetSignature(String::AllocFromModifiedUtf8(utf16_length, utf8.c_str()));
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700884 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700885 dst->SetProtoIdx(method_id.proto_idx_);
886 dst->SetCodeItemOffset(src.code_off_);
887 const char* shorty = dex_file.GetShorty(method_id.proto_idx_);
888 dst->SetShorty(shorty);
889 dst->SetAccessFlags(src.access_flags_);
890 dst->SetReturnTypeIdx(dex_file.GetProtoId(method_id.proto_idx_).return_type_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700891
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700892 dst->SetDexCacheStrings(klass->GetDexCache()->GetStrings());
893 dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes());
894 dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods());
895 dst->SetDexCacheResolvedFields(klass->GetDexCache()->GetResolvedFields());
896 dst->SetDexCacheCodeAndDirectMethods(klass->GetDexCache()->GetCodeAndDirectMethods());
897 dst->SetDexCacheInitializedStaticStorage(klass->GetDexCache()->GetInitializedStaticStorage());
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700898
Brian Carlstrom934486c2011-07-12 23:42:50 -0700899 // TODO: check for finalize method
900
Brian Carlstromf615a612011-07-23 12:50:34 -0700901 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700902 if (code_item != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700903 dst->SetNumRegisters(code_item->registers_size_);
904 dst->SetNumIns(code_item->ins_size_);
905 dst->SetNumOuts(code_item->outs_size_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700906 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700907 uint16_t num_args = Method::NumArgRegisters(shorty);
908 if ((src.access_flags_ & kAccStatic) != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700909 ++num_args;
910 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700911 dst->SetNumRegisters(num_args);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700912 // TODO: native methods
913 }
914}
915
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700916void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700917 AppendToBootClassPath(dex_file, AllocDexCache(dex_file));
918}
919
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700920void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700921 CHECK(dex_cache != NULL) << dex_file.GetLocation();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700922 boot_class_path_.push_back(&dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700923 RegisterDexFile(dex_file, dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700924}
925
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700926void ClassLinker::RegisterDexFile(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700927 RegisterDexFile(dex_file, AllocDexCache(dex_file));
928}
929
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700930void ClassLinker::RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700931 CHECK(dex_cache != NULL) << dex_file.GetLocation();
932 CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation()));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700933 dex_files_.push_back(&dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700934 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700935}
936
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700937const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700938 for (size_t i = 0; i != dex_caches_.size(); ++i) {
939 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700940 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700941 }
942 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700943 CHECK(false) << "Failed to find DexFile for DexCache " << dex_cache->GetLocation()->ToModifiedUtf8();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700944 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700945}
946
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700947DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700948 for (size_t i = 0; i != dex_files_.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700949 if (dex_files_[i] == &dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700950 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700951 }
952 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700953 CHECK(false) << "Failed to find DexCache for DexFile " << dex_file.GetLocation();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700954 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700955}
956
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700957Class* ClassLinker::CreatePrimitiveClass(const char* descriptor,
958 Class::PrimitiveType type) {
959 // TODO: deduce one argument from the other
Brian Carlstrom4873d462011-08-21 15:23:39 -0700960 Class* klass = AllocClass(sizeof(Class));
Carl Shapiro565f5072011-07-10 13:39:43 -0700961 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700962 klass->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract);
963 klass->SetDescriptor(String::AllocFromModifiedUtf8(descriptor));
964 klass->SetPrimitiveType(type);
965 klass->SetStatus(Class::kStatusInitialized);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700966 bool success = InsertClass(descriptor, klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700967 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700968 return klass;
969}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700970
Brian Carlstrombe977852011-07-19 14:54:54 -0700971// Create an array class (i.e. the class object for the array, not the
972// array itself). "descriptor" looks like "[C" or "[[[[B" or
973// "[Ljava/lang/String;".
974//
975// If "descriptor" refers to an array of primitives, look up the
976// primitive type's internally-generated class object.
977//
978// "loader" is the class loader of the class that's referring to us. It's
979// used to ensure that we're looking for the element type in the right
980// context. It does NOT become the class loader for the array class; that
981// always comes from the base element class.
982//
983// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700984Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700985 const ClassLoader* class_loader) {
986 CHECK_EQ('[', descriptor[0]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700987
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700988 // Identify the underlying element class and the array dimension depth.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700989 Class* component_type = NULL;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700990 int array_rank;
991 if (descriptor[1] == '[') {
992 // array of arrays; keep descriptor and grab stuff from parent
993 Class* outer = FindClass(descriptor.substr(1), class_loader);
994 if (outer != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700995 // want the base class, not "outer", in our component_type
996 component_type = outer->GetComponentType();
997 array_rank = outer->GetArrayRank() + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700998 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700999 DCHECK(component_type == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001000 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001001 } else {
1002 array_rank = 1;
1003 if (descriptor[1] == 'L') {
1004 // array of objects; strip off "[" and look up descriptor.
1005 const StringPiece subDescriptor = descriptor.substr(1);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001006 component_type = FindClass(subDescriptor, class_loader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001007 } else {
1008 // array of a primitive type
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001009 component_type = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001010 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001011 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001012
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001013 if (component_type == NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001014 // failed
1015 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
1016 return NULL;
1017 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001018
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001019 // See if the component type is already loaded. Array classes are
1020 // always associated with the class loader of their underlying
1021 // element type -- an array of Strings goes with the loader for
1022 // java/lang/String -- so we need to look for it there. (The
1023 // caller should have checked for the existence of the class
1024 // before calling here, but they did so with *their* class loader,
1025 // not the component type's loader.)
1026 //
1027 // If we find it, the caller adds "loader" to the class' initiating
1028 // loader list, which should prevent us from going through this again.
1029 //
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001030 // This call is unnecessary if "loader" and "component_type->GetClassLoader()"
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001031 // are the same, because our caller (FindClass) just did the
1032 // lookup. (Even if we get this wrong we still have correct behavior,
1033 // because we effectively do this lookup again when we add the new
1034 // class to the hash table --- necessary because of possible races with
1035 // other threads.)
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001036 if (class_loader != component_type->GetClassLoader()) {
1037 Class* new_class = LookupClass(descriptor, component_type->GetClassLoader());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001038 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001039 return new_class;
1040 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001041 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001042
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001043 // Fill out the fields in the Class.
1044 //
1045 // It is possible to execute some methods against arrays, because
1046 // all arrays are subclasses of java_lang_Object_, so we need to set
1047 // up a vtable. We can just point at the one in java_lang_Object_.
1048 //
1049 // Array classes are simple enough that we don't need to do a full
1050 // link step.
1051
1052 Class* new_class = NULL;
1053 if (!init_done_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001054 // Classes that were hand created, ie not by FindSystemClass
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001055 if (descriptor == "[Ljava/lang/Object;") {
1056 new_class = GetClassRoot(kObjectArrayClass);
1057 } else if (descriptor == "[C") {
1058 new_class = GetClassRoot(kCharArrayClass);
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001059 } else if (descriptor == "[I") {
1060 new_class = GetClassRoot(kIntArrayClass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001061 }
1062 }
1063 if (new_class == NULL) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001064 new_class = AllocClass(sizeof(Class));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001065 if (new_class == NULL) {
1066 return NULL;
1067 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001068 new_class->SetArrayRank(array_rank);
1069 new_class->SetComponentType(component_type);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001070 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001071 DCHECK_LE(1, new_class->GetArrayRank());
1072 DCHECK(new_class->GetComponentType() != NULL);
Brian Carlstrom693267a2011-09-06 09:25:34 -07001073 if (new_class->GetDescriptor() != NULL) {
1074 DCHECK(new_class->GetDescriptor()->Equals(descriptor));
1075 } else {
1076 new_class->SetDescriptor(String::AllocFromModifiedUtf8(descriptor.ToString().c_str()));
1077 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001078 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001079 new_class->SetSuperClass(java_lang_Object);
1080 new_class->SetVTable(java_lang_Object->GetVTable());
1081 new_class->SetPrimitiveType(Class::kPrimNot);
1082 new_class->SetClassLoader(component_type->GetClassLoader());
1083 new_class->SetStatus(Class::kStatusInitialized);
1084 // don't need to set new_class->SetObjectSize(..)
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001085 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001086
1087
1088 // All arrays have java/lang/Cloneable and java/io/Serializable as
1089 // interfaces. We need to set that up here, so that stuff like
1090 // "instanceof" works right.
1091 //
1092 // Note: The GC could run during the call to FindSystemClass,
1093 // so we need to make sure the class object is GC-valid while we're in
1094 // there. Do this by clearing the interface list so the GC will just
1095 // think that the entries are null.
1096
1097
1098 // Use the single, global copies of "interfaces" and "iftable"
1099 // (remember not to free them for arrays).
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001100 new_class->SetInterfaces(array_interfaces_);
1101 new_class->SetIFTableCount(2);
1102 new_class->SetIFTable(array_iftable_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001103
1104 // Inherit access flags from the component type. Arrays can't be
1105 // used as a superclass or interface, so we want to add "final"
1106 // and remove "interface".
1107 //
1108 // Don't inherit any non-standard flags (e.g., kAccFinal)
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001109 // from component_type. We assume that the array class does not
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001110 // override finalize().
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001111 new_class->SetAccessFlags(((new_class->GetComponentType()->GetAccessFlags() &
1112 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001113
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001114 if (InsertClass(descriptor, new_class)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001115 return new_class;
1116 }
1117 // Another thread must have loaded the class after we
1118 // started but before we finished. Abandon what we've
1119 // done.
1120 //
1121 // (Yes, this happens.)
1122
1123 // Grab the winning class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001124 Class* other_class = LookupClass(descriptor, component_type->GetClassLoader());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001125 DCHECK(other_class != NULL);
1126 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001127}
1128
1129Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -07001130 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001131 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001132 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001133 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001134 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001135 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001136 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001137 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001138 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001139 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001140 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001141 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001142 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001143 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001144 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001145 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001146 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001147 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001148 return GetClassRoot(kPrimitiveVoid);
Carl Shapiro744ad052011-08-06 15:53:36 -07001149 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001150 std::string printable_type(PrintableChar(type));
1151 Thread::Current()->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
1152 "Not a primitive type: %s", printable_type.c_str());
1153 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001154}
1155
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001156bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) {
1157 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001158 MutexLock mu(classes_lock_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001159 Table::iterator it = classes_.insert(std::make_pair(hash, klass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001160 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001161}
1162
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001163Class* ClassLinker::LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001164 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001165 MutexLock mu(classes_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001166 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001167 for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001168 Class* klass = it->second;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001169 if (klass->GetDescriptor()->Equals(descriptor) && klass->GetClassLoader() == class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001170 return klass;
1171 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001172 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001173 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001174}
1175
1176bool ClassLinker::InitializeClass(Class* klass) {
1177 CHECK(klass->GetStatus() == Class::kStatusResolved ||
Elliott Hughesf5ecf062011-09-06 17:37:59 -07001178 klass->GetStatus() == Class::kStatusError) << klass->GetStatus();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001179
Carl Shapirob5573532011-07-12 18:22:59 -07001180 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001181
1182 {
1183 ObjectLock lock(klass);
1184
1185 if (klass->GetStatus() < Class::kStatusVerified) {
1186 if (klass->IsErroneous()) {
1187 LG << "re-initializing failed class"; // TODO: throw
1188 return false;
1189 }
1190
1191 CHECK(klass->GetStatus() == Class::kStatusResolved);
1192
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001193 klass->SetStatus(Class::kStatusVerifying);
jeffhaobdb76512011-09-07 11:43:16 -07001194 if (!DexVerifier::VerifyClass(klass)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001195 LG << "Verification failed"; // TODO: ThrowVerifyError
1196 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001197 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001198 klass->SetStatus(Class::kStatusError);
1199 return false;
1200 }
1201
1202 klass->SetStatus(Class::kStatusVerified);
1203 }
1204
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001205 if (klass->GetStatus() == Class::kStatusInitialized) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001206 return true;
1207 }
1208
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001209 while (klass->GetStatus() == Class::kStatusInitializing) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001210 // we caught somebody else in the act; was it us?
Elliott Hughesdcc24742011-09-07 14:02:44 -07001211 if (klass->GetClinitThreadId() == self->GetTid()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001212 LG << "recursive <clinit>";
1213 return true;
1214 }
1215
1216 CHECK(!self->IsExceptionPending());
1217
1218 lock.Wait(); // TODO: check for interruption
1219
1220 // When we wake up, repeat the test for init-in-progress. If
1221 // there's an exception pending (only possible if
1222 // "interruptShouldThrow" was set), bail out.
1223 if (self->IsExceptionPending()) {
1224 CHECK(false);
1225 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
1226 klass->SetStatus(Class::kStatusError);
1227 return false;
1228 }
1229 if (klass->GetStatus() == Class::kStatusInitializing) {
1230 continue;
1231 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001232 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001233 klass->GetStatus() == Class::kStatusError);
1234 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001235 // The caller wants an exception, but it was thrown in a
1236 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001237 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
1238 return false;
1239 }
1240 return true; // otherwise, initialized
1241 }
1242
1243 // see if we failed previously
1244 if (klass->IsErroneous()) {
1245 // might be wise to unlock before throwing; depends on which class
1246 // it is that we have locked
1247
1248 // TODO: throwEarlierClassFailure(klass);
1249 return false;
1250 }
1251
1252 if (!ValidateSuperClassDescriptors(klass)) {
1253 klass->SetStatus(Class::kStatusError);
1254 return false;
1255 }
1256
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001257 DCHECK(klass->GetStatus() < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001258
Elliott Hughesdcc24742011-09-07 14:02:44 -07001259 klass->SetClinitThreadId(self->GetTid());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001260 klass->SetStatus(Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001261 }
1262
1263 if (!InitializeSuperClass(klass)) {
1264 return false;
1265 }
1266
1267 InitializeStaticFields(klass);
1268
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001269 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001270 if (clinit != NULL) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -07001271 clinit->Invoke(self, NULL, NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001272 }
1273
1274 {
1275 ObjectLock lock(klass);
1276
1277 if (self->IsExceptionPending()) {
1278 klass->SetStatus(Class::kStatusError);
1279 } else {
1280 klass->SetStatus(Class::kStatusInitialized);
1281 }
1282 lock.NotifyAll();
1283 }
1284
1285 return true;
1286}
1287
1288bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
1289 if (klass->IsInterface()) {
1290 return true;
1291 }
1292 // begin with the methods local to the superclass
1293 if (klass->HasSuperClass() &&
1294 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
1295 const Class* super = klass->GetSuperClass();
1296 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001297 const Method* method = super->GetVirtualMethod(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001298 if (method != super->GetVirtualMethod(i) &&
1299 !HasSameMethodDescriptorClasses(method, super, klass)) {
1300 LG << "Classes resolve differently in superclass";
1301 return false;
1302 }
1303 }
1304 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001305 for (size_t i = 0; i < klass->GetIFTableCount(); ++i) {
1306 const InterfaceEntry* iftable = &klass->GetIFTable()[i];
Brian Carlstrom30b94452011-08-25 21:35:26 -07001307 Class* interface = iftable->GetInterface();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001308 if (klass->GetClassLoader() != interface->GetClassLoader()) {
1309 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001310 uint32_t vtable_index = iftable->GetMethodIndexArray()[j];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001311 const Method* method = klass->GetVirtualMethod(vtable_index);
1312 if (!HasSameMethodDescriptorClasses(method, interface,
1313 method->GetClass())) {
1314 LG << "Classes resolve differently in interface"; // TODO: LinkageError
1315 return false;
1316 }
1317 }
1318 }
1319 }
1320 return true;
1321}
1322
1323bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001324 const Class* klass1,
1325 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001326 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001327 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->GetProtoIdx());
Brian Carlstromf615a612011-07-23 12:50:34 -07001328 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001329 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001330 const char* descriptor = it->GetDescriptor();
1331 if (descriptor == NULL) {
1332 break;
1333 }
1334 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1335 // Found a non-primitive type.
1336 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1337 return false;
1338 }
1339 }
1340 }
1341 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001342 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001343 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1344 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1345 return false;
1346 }
1347 }
1348 return true;
1349}
1350
1351// Returns true if classes referenced by the descriptor are the
1352// same classes in klass1 as they are in klass2.
1353bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001354 const Class* klass1,
1355 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001356 CHECK(descriptor != NULL);
1357 CHECK(klass1 != NULL);
1358 CHECK(klass2 != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001359 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001360 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001361 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001362 // TODO: found2 == NULL
1363 // TODO: lookup found1 in initiating loader list
1364 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -07001365 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001366 if (found1 == found2) {
1367 return true;
1368 } else {
1369 return false;
1370 }
1371 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001372 return true;
1373}
1374
1375bool ClassLinker::InitializeSuperClass(Class* klass) {
1376 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001377 if (!klass->IsInterface() && klass->HasSuperClass()) {
1378 Class* super_class = klass->GetSuperClass();
1379 if (super_class->GetStatus() != Class::kStatusInitialized) {
1380 CHECK(!super_class->IsInterface());
1381 klass->MonitorExit();
1382 bool super_initialized = InitializeClass(super_class);
1383 klass->MonitorEnter();
1384 // TODO: check for a pending exception
1385 if (!super_initialized) {
1386 klass->SetStatus(Class::kStatusError);
1387 klass->NotifyAll();
1388 return false;
1389 }
1390 }
1391 }
1392 return true;
1393}
1394
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001395bool ClassLinker::EnsureInitialized(Class* c) {
1396 CHECK(c != NULL);
1397 if (c->IsInitialized()) {
1398 return true;
1399 }
1400
1401 c->MonitorExit();
1402 InitializeClass(c);
1403 c->MonitorEnter();
1404 return !Thread::Current()->IsExceptionPending();
1405}
1406
Brian Carlstromb9edb842011-08-28 16:31:06 -07001407StaticStorageBase* ClassLinker::InitializeStaticStorageFromCode(uint32_t type_idx,
1408 const Method* referrer) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001409 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1410 Class* klass = class_linker->ResolveType(type_idx, referrer);
1411 if (klass == NULL) {
1412 UNIMPLEMENTED(FATAL) << "throw exception due to unresolved class";
1413 }
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001414 // If we are the <clinit> of this class, just return our storage.
1415 //
1416 // Do not set the DexCache InitializedStaticStorage, since that
1417 // implies <clinit> has finished running.
1418 if (klass == referrer->GetDeclaringClass() && referrer->GetName()->Equals("<clinit>")) {
1419 return klass;
1420 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001421 if (!class_linker->EnsureInitialized(klass)) {
1422 CHECK(Thread::Current()->IsExceptionPending());
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001423 UNIMPLEMENTED(FATAL) << "throw exception due to class initialization problem";
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001424 }
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001425 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001426 return klass;
1427}
1428
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001429void ClassLinker::ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
1430 Class* c, std::map<int, Field*>& field_map) {
1431 const ClassLoader* cl = c->GetClassLoader();
1432 const byte* class_data = dex_file.GetClassData(dex_class_def);
1433 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
1434 uint32_t last_idx = 0;
1435 for (size_t i = 0; i < header.static_fields_size_; ++i) {
1436 DexFile::Field dex_field;
1437 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
1438 field_map[i] = ResolveField(dex_file, dex_field.field_idx_, c->GetDexCache(), cl, true);
1439 }
1440}
1441
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001442void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001443 size_t num_static_fields = klass->NumStaticFields();
1444 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001445 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001446 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001447 DexCache* dex_cache = klass->GetDexCache();
Brian Carlstrom4873d462011-08-21 15:23:39 -07001448 // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray?
Brian Carlstromf615a612011-07-23 12:50:34 -07001449 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001450 return;
1451 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001452 const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001453 const DexFile& dex_file = FindDexFile(dex_cache);
1454 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001455 CHECK(dex_class_def != NULL);
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001456
1457 // We reordered the fields, so we need to be able to map the field indexes to the right fields.
1458 std::map<int, Field*> field_map;
1459 ConstructFieldMap(dex_file, *dex_class_def, klass, field_map);
1460
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001461 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001462 if (addr == NULL) {
1463 // All this class' static fields have default values.
1464 return;
1465 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001466 size_t array_size = DecodeUnsignedLeb128(&addr);
1467 for (size_t i = 0; i < array_size; ++i) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001468 Field* field = field_map[i];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001469 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001470 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001471 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001472 case DexFile::kByte:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001473 field->SetByte(NULL, value.b);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001474 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001475 case DexFile::kShort:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001476 field->SetShort(NULL, value.s);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001477 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001478 case DexFile::kChar:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001479 field->SetChar(NULL, value.c);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001480 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001481 case DexFile::kInt:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001482 field->SetInt(NULL, value.i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001483 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001484 case DexFile::kLong:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001485 field->SetLong(NULL, value.j);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001486 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001487 case DexFile::kFloat:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001488 field->SetFloat(NULL, value.f);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001489 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001490 case DexFile::kDouble:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001491 field->SetDouble(NULL, value.d);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001492 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001493 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001494 uint32_t string_idx = value.i;
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001495 const String* resolved = ResolveString(dex_file, string_idx, klass->GetDexCache());
Brian Carlstrom4873d462011-08-21 15:23:39 -07001496 field->SetObject(NULL, resolved);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001497 break;
1498 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001499 case DexFile::kBoolean:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001500 field->SetBoolean(NULL, value.z);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001501 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001502 case DexFile::kNull:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001503 field->SetObject(NULL, value.l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001504 break;
1505 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001506 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001507 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001508 }
1509}
1510
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001511bool ClassLinker::LinkClass(Class* klass) {
1512 CHECK_EQ(Class::kStatusLoaded, klass->GetStatus());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001513 if (!LinkSuperClass(klass)) {
1514 return false;
1515 }
1516 if (!LinkMethods(klass)) {
1517 return false;
1518 }
1519 if (!LinkInstanceFields(klass)) {
1520 return false;
1521 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001522 if (!LinkStaticFields(klass)) {
1523 return false;
1524 }
1525 CreateReferenceInstanceOffsets(klass);
1526 CreateReferenceStaticOffsets(klass);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001527 CHECK_EQ(Class::kStatusLoaded, klass->GetStatus());
1528 klass->SetStatus(Class::kStatusResolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001529 return true;
1530}
1531
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001532bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001533 CHECK_EQ(Class::kStatusIdx, klass->GetStatus());
1534 if (klass->GetSuperClassTypeIdx() != DexFile::kDexNoIndex) {
1535 Class* super_class = ResolveType(dex_file, klass->GetSuperClassTypeIdx(), klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001536 if (super_class == NULL) {
1537 LG << "Failed to resolve superclass";
1538 return false;
1539 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001540 klass->SetSuperClass(super_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001541 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001542 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1543 uint32_t idx = klass->GetInterfacesTypeIdx()->Get(i);
1544 Class *interface = ResolveType(dex_file, idx, klass);
1545 klass->SetInterface(i, interface);
1546 if (interface == NULL) {
1547 LG << "Failed to resolve interface";
1548 return false;
1549 }
1550 // Verify
1551 if (!klass->CanAccess(interface)) {
1552 LG << "Inaccessible interface";
1553 return false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001554 }
1555 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001556 // Mark the class as loaded.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001557 klass->SetStatus(Class::kStatusLoaded);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001558 return true;
1559}
1560
1561bool ClassLinker::LinkSuperClass(Class* klass) {
1562 CHECK(!klass->IsPrimitive());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001563 Class* super = klass->GetSuperClass();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001564 if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001565 if (super != NULL) {
1566 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1567 return false;
1568 }
1569 // TODO: clear finalize attribute
1570 return true;
1571 }
1572 if (super == NULL) {
1573 LG << "No superclass defined"; // TODO: LinkageError
1574 return false;
1575 }
1576 // Verify
1577 if (super->IsFinal()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001578 LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001579 return false;
1580 }
1581 if (super->IsInterface()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001582 LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001583 return false;
1584 }
1585 if (!klass->CanAccess(super)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001586 LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001587 return false;
1588 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001589#ifndef NDEBUG
1590 // Ensure super classes are fully resolved prior to resolving fields..
1591 while (super != NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001592 CHECK(super->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001593 super = super->GetSuperClass();
1594 }
1595#endif
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001596 return true;
1597}
1598
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001599// Populate the class vtable and itable. Compute return type indices.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001600bool ClassLinker::LinkMethods(Class* klass) {
1601 if (klass->IsInterface()) {
1602 // No vtable.
1603 size_t count = klass->NumVirtualMethods();
1604 if (!IsUint(16, count)) {
1605 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1606 return false;
1607 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001608 for (size_t i = 0; i < count; ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001609 klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001610 }
jeffhaobdb76512011-09-07 11:43:16 -07001611 // Link interface method tables
1612 LinkInterfaceMethods(klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001613 } else {
1614 // Link virtual method tables
1615 LinkVirtualMethods(klass);
1616
1617 // Link interface method tables
1618 LinkInterfaceMethods(klass);
1619
1620 // Insert stubs.
1621 LinkAbstractMethods(klass);
1622 }
1623 return true;
1624}
1625
1626bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001627 if (klass->HasSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001628 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->GetVTable()->GetLength();
1629 size_t actual_count = klass->GetSuperClass()->GetVTable()->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001630 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001631 // TODO: do not assign to the vtable field until it is fully constructed.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001632 ObjectArray<Method>* vtable = klass->GetSuperClass()->GetVTable()->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001633 // See if any of our virtual methods override the superclass.
1634 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001635 Method* local_method = klass->GetVirtualMethodDuringLinking(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001636 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001637 for (; j < actual_count; ++j) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001638 Method* super_method = vtable->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001639 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001640 // Verify
1641 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001642 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001643 return false;
1644 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001645 vtable->Set(j, local_method);
1646 local_method->SetMethodIndex(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001647 break;
1648 }
1649 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001650 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001651 // Not overriding, append.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001652 vtable->Set(actual_count, local_method);
1653 local_method->SetMethodIndex(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001654 actual_count += 1;
1655 }
1656 }
1657 if (!IsUint(16, actual_count)) {
1658 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1659 return false;
1660 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001661 // Shrink vtable if possible
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001662 CHECK_LE(actual_count, max_count);
1663 if (actual_count < max_count) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001664 vtable = vtable->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001665 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001666 klass->SetVTable(vtable);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001667 } else {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001668 CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001669 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001670 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001671 LG << "Too many methods"; // TODO: VirtualMachineError
1672 return false;
1673 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001674 ObjectArray<Method>* vtable = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001675 for (size_t i = 0; i < num_virtual_methods; ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001676 Method* virtual_method = klass->GetVirtualMethodDuringLinking(i);
1677 vtable->Set(i, virtual_method);
1678 virtual_method->SetMethodIndex(i & 0xFFFF);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001679 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001680 klass->SetVTable(vtable);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001681 }
1682 return true;
1683}
1684
1685bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1686 int pool_offset = 0;
1687 int pool_size = 0;
1688 int miranda_count = 0;
1689 int miranda_alloc = 0;
1690 size_t super_ifcount;
1691 if (klass->HasSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001692 super_ifcount = klass->GetSuperClass()->GetIFTableCount();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001693 } else {
1694 super_ifcount = 0;
1695 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001696 size_t ifcount = super_ifcount;
1697 ifcount += klass->NumInterfaces();
1698 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001699 ifcount += klass->GetInterface(i)->GetIFTableCount();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001700 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001701 if (ifcount == 0) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001702 // TODO: enable these asserts with klass status validation
1703 // DCHECK(klass->GetIFTableCount() == 0);
1704 // DCHECK(klass->GetIFTable() == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001705 return true;
1706 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001707 InterfaceEntry* iftable = new InterfaceEntry[ifcount];
1708 memset(iftable, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001709 if (super_ifcount != 0) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001710 memcpy(iftable, klass->GetSuperClass()->GetIFTable(),
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001711 sizeof(InterfaceEntry) * super_ifcount);
1712 }
1713 // Flatten the interface inheritance hierarchy.
1714 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001715 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1716 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001717 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001718 if (!interf->IsInterface()) {
1719 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1720 return false;
1721 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001722 iftable[idx++].SetInterface(interf);
1723 for (size_t j = 0; j < interf->GetIFTableCount(); j++) {
1724 iftable[idx++].SetInterface(interf->GetIFTable()[j].GetInterface());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001725 }
1726 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001727 klass->SetIFTable(iftable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001728 CHECK_EQ(idx, ifcount);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001729 klass->SetIFTableCount(ifcount);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001730 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001731 return true;
1732 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001733 for (size_t i = super_ifcount; i < ifcount; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001734 pool_size += iftable[i].GetInterface()->NumVirtualMethods();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001735 }
1736 if (pool_size == 0) {
1737 return true;
1738 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001739 klass->SetIfviPoolCount(pool_size);
1740 uint32_t* ifvi_pool = new uint32_t[pool_size];
1741 klass->SetIfviPool(ifvi_pool);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001742 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001743 for (size_t i = super_ifcount; i < ifcount; ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001744 iftable[i].SetMethodIndexArray(ifvi_pool + pool_offset);
1745 Class* interface = iftable[i].GetInterface();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001746 pool_offset += interface->NumVirtualMethods(); // end here
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001747 ObjectArray<Method>* vtable = klass->GetVTableDuringLinking();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001748 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1749 Method* interface_method = interface->GetVirtualMethod(j);
1750 int k; // must be signed
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001751 for (k = vtable->GetLength() - 1; k >= 0; --k) {
1752 Method* vtable_method = vtable->Get(k);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001753 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1754 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001755 LG << "Implementation not public";
1756 return false;
1757 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001758 iftable[i].GetMethodIndexArray()[j] = k;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001759 break;
1760 }
1761 }
1762 if (k < 0) {
1763 if (miranda_count == miranda_alloc) {
1764 miranda_alloc += 8;
1765 if (miranda_list.empty()) {
1766 miranda_list.resize(miranda_alloc);
1767 } else {
1768 miranda_list.resize(miranda_alloc);
1769 }
1770 }
1771 int mir;
1772 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001773 Method* miranda_method = miranda_list[mir];
1774 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001775 break;
1776 }
1777 }
1778 // point the interface table at a phantom slot index
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001779 iftable[i].GetMethodIndexArray()[j] =
1780 vtable->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001781 if (mir == miranda_count) {
1782 miranda_list[miranda_count++] = interface_method;
1783 }
1784 }
1785 }
1786 }
1787 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001788 int old_method_count = klass->NumVirtualMethods();
1789 int new_method_count = old_method_count + miranda_count;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001790 klass->SetVirtualMethods(
1791 klass->GetVirtualMethods()->CopyOf(new_method_count));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001792
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001793 ObjectArray<Method>* vtable = klass->GetVTableDuringLinking();
1794 CHECK(vtable != NULL);
1795 int old_vtable_count = vtable->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001796 int new_vtable_count = old_vtable_count + miranda_count;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001797 vtable = vtable->CopyOf(new_vtable_count);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001798 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001799 Method* meth = AllocMethod();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001800 // TODO: this shouldn't be a memcpy
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001801 memcpy(meth, miranda_list[i], sizeof(Method));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001802 meth->SetDeclaringClass(klass);
1803 meth->SetAccessFlags(meth->GetAccessFlags() | kAccMiranda);
1804 meth->SetMethodIndex(0xFFFF & (old_vtable_count + i));
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001805 klass->SetVirtualMethod(old_method_count + i, meth);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001806 vtable->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001807 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001808 // TODO: do not assign to the vtable field until it is fully constructed.
1809 klass->SetVTable(vtable);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001810 }
1811 return true;
1812}
1813
1814void ClassLinker::LinkAbstractMethods(Class* klass) {
1815 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001816 Method* method = klass->GetVirtualMethodDuringLinking(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001817 if (method->IsAbstract()) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001818 LG << "AbstractMethodError";
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001819 // TODO: throw AbstractMethodError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001820 }
1821 }
1822}
1823
1824bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001825 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001826 return LinkFields(klass, true);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001827}
1828
1829bool ClassLinker::LinkStaticFields(Class* klass) {
1830 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001831 size_t allocated_class_size = klass->GetClassSize();
1832 bool success = LinkFields(klass, false);
1833 CHECK_EQ(allocated_class_size, klass->GetClassSize());
Brian Carlstrom4873d462011-08-21 15:23:39 -07001834 return success;
1835}
1836
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001837bool ClassLinker::LinkFields(Class* klass, bool instance) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001838 size_t num_fields =
1839 instance ? klass->NumInstanceFields() : klass->NumStaticFields();
1840
1841 ObjectArray<Field>* fields =
1842 instance ? klass->GetIFields() : klass->GetSFields();
1843 // Fields updated at end of LinkFields
1844 size_t num_reference_fields;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001845
1846 // Initialize size and field_offset
Brian Carlstrom693267a2011-09-06 09:25:34 -07001847 size_t size;
1848 MemberOffset field_offset(0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001849 if (instance) {
1850 Class* super_class = klass->GetSuperClass();
1851 if (super_class != NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001852 CHECK(super_class->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001853 field_offset = MemberOffset(super_class->GetObjectSize());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001854 }
1855 size = field_offset.Uint32Value();
1856 } else {
1857 size = klass->GetClassSize();
Brian Carlstrom693267a2011-09-06 09:25:34 -07001858 field_offset = Class::FieldsOffset();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001859 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001860
Brian Carlstrom4873d462011-08-21 15:23:39 -07001861 CHECK((num_fields == 0) == (fields == NULL));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001862
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001863 // Move references to the front.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001864 size_t i = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001865 num_reference_fields = 0;
1866 for (; i < num_fields; i++) {
1867 Field* field = fields->Get(i);
1868 const Class* field_type = field->GetTypeDuringLinking();
1869 // if a field's type at this point is NULL it isn't primitive
1870 if (field_type != NULL && field_type->IsPrimitive()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001871 for (size_t j = num_fields - 1; j > i; j--) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001872 Field* ref_field = fields->Get(j);
1873 const Class* ref_field_type = ref_field->GetTypeDuringLinking();
1874 if (ref_field_type == NULL || !ref_field_type->IsPrimitive()) {
1875 fields->Set(i, ref_field);
1876 fields->Set(j, field);
1877 field = ref_field;
1878 field_type = ref_field_type;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001879 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001880 break;
1881 }
1882 }
1883 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001884 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001885 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001886 if (field_type != NULL && field_type->IsPrimitive()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001887 break;
1888 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001889 field->SetOffset(field_offset);
1890 field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001891 }
1892
1893 // Now we want to pack all of the double-wide fields together. If
1894 // we're not aligned, though, we want to shuffle one 32-bit field
1895 // into place. If we can't find one, we'll have to pad it.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001896 if (i != num_fields && !IsAligned(field_offset.Uint32Value(), 8)) {
1897 Field* field = fields->Get(i);
1898 const Class* c = field->GetTypeDuringLinking();
1899 CHECK(c != NULL); // should only be working on primitive types
1900 if (!c->IsPrimitiveLong() && !c->IsPrimitiveDouble()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001901 // The field that comes next is 32-bit, so just advance past it.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001902 DCHECK(c->IsPrimitive());
1903 field->SetOffset(field_offset);
1904 field_offset = MemberOffset(field_offset.Uint32Value() +
1905 sizeof(uint32_t));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001906 i++;
1907 } else {
1908 // Next field is 64-bit, so search for a 32-bit field we can
1909 // swap into it.
1910 bool found = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001911 for (size_t j = num_fields - 1; j > i; j--) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001912 Field* single_field = fields->Get(j);
1913 const Class* rc = single_field->GetTypeDuringLinking();
1914 CHECK(rc != NULL); // should only be working on primitive types
1915 if (!rc->IsPrimitiveLong() && !rc->IsPrimitiveDouble()) {
1916 fields->Set(i, single_field);
1917 fields->Set(j, field);
1918 field = single_field;
1919 field->SetOffset(field_offset);
1920 field_offset = MemberOffset(field_offset.Uint32Value() +
1921 sizeof(uint32_t));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001922 found = true;
1923 i++;
1924 break;
1925 }
1926 }
1927 if (!found) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001928 field_offset = MemberOffset(field_offset.Uint32Value() +
1929 sizeof(uint32_t));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001930 }
1931 }
1932 }
1933
1934 // Alignment is good, shuffle any double-wide fields forward, and
1935 // finish assigning field offsets to all fields.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001936 DCHECK(i == num_fields || IsAligned(field_offset.Uint32Value(), 4));
Brian Carlstrom4873d462011-08-21 15:23:39 -07001937 for ( ; i < num_fields; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001938 Field* field = fields->Get(i);
1939 const Class* c = field->GetTypeDuringLinking();
1940 CHECK(c != NULL); // should only be working on primitive types
1941 if (!c->IsPrimitiveDouble() && !c->IsPrimitiveLong()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001942 for (size_t j = num_fields - 1; j > i; j--) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001943 Field* double_field = fields->Get(j);
1944 const Class* rc = double_field->GetTypeDuringLinking();
1945 CHECK(rc != NULL); // should only be working on primitive types
1946 if (rc->IsPrimitiveDouble() || rc->IsPrimitiveLong()) {
1947 fields->Set(i, double_field);
1948 fields->Set(j, field);
1949 field = double_field;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001950 c = rc;
1951 break;
1952 }
1953 }
1954 } else {
1955 // This is a double-wide field, leave it be.
1956 }
1957
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001958 field->SetOffset(field_offset);
1959 if (c->IsPrimitiveLong() || c->IsPrimitiveDouble()) {
1960 field_offset = MemberOffset(field_offset.Uint32Value() +
1961 sizeof(uint64_t));
1962 } else {
1963 field_offset = MemberOffset(field_offset.Uint32Value() +
1964 sizeof(uint32_t));
Brian Carlstrom4873d462011-08-21 15:23:39 -07001965 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001966 }
1967
1968#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001969 // Make sure that all reference fields appear before
1970 // non-reference fields, and all double-wide fields are aligned.
1971 bool seen_non_ref = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001972 for (i = 0; i < num_fields; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001973 Field* field = fields->Get(i);
1974 const Class* c = field->GetTypeDuringLinking();
1975 if (c != NULL && c->IsPrimitive()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001976 if (!seen_non_ref) {
1977 seen_non_ref = true;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001978 DCHECK_EQ(num_reference_fields, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001979 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001980 } else {
1981 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001982 }
1983 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001984 if (!seen_non_ref) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001985 DCHECK_EQ(num_fields, num_reference_fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001986 }
1987#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001988 size = field_offset.Uint32Value();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001989 // Update klass
1990 if(instance) {
1991 klass->SetNumReferenceInstanceFields(num_reference_fields);
1992 if(!klass->IsVariableSize()) {
1993 klass->SetObjectSize(size);
1994 }
1995 } else {
1996 klass->SetNumReferenceStaticFields(num_reference_fields);
1997 klass->SetClassSize(size);
1998 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001999 return true;
2000}
2001
2002// Set the bitmap of reference offsets, refOffsets, from the ifields
2003// list.
Brian Carlstrom4873d462011-08-21 15:23:39 -07002004void ClassLinker::CreateReferenceInstanceOffsets(Class* klass) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002005 uint32_t reference_offsets = 0;
2006 Class* super_class = klass->GetSuperClass();
2007 if (super_class != NULL) {
2008 reference_offsets = super_class->GetReferenceInstanceOffsets();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002009 // If our superclass overflowed, we don't stand a chance.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002010 if (reference_offsets == CLASS_WALK_SUPER) {
2011 klass->SetReferenceInstanceOffsets(reference_offsets);
Brian Carlstrom4873d462011-08-21 15:23:39 -07002012 return;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002013 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002014 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002015 CreateReferenceOffsets(klass, true, reference_offsets);
Brian Carlstrom4873d462011-08-21 15:23:39 -07002016}
2017
2018void ClassLinker::CreateReferenceStaticOffsets(Class* klass) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002019 CreateReferenceOffsets(klass, false, 0);
Brian Carlstrom4873d462011-08-21 15:23:39 -07002020}
2021
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002022void ClassLinker::CreateReferenceOffsets(Class* klass, bool instance,
2023 uint32_t reference_offsets) {
2024 size_t num_reference_fields =
2025 instance ? klass->NumReferenceInstanceFieldsDuringLinking()
2026 : klass->NumReferenceStaticFieldsDuringLinking();
2027 const ObjectArray<Field>* fields =
2028 instance ? klass->GetIFields() : klass->GetSFields();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002029 // All of the fields that contain object references are guaranteed
2030 // to be at the beginning of the fields list.
2031 for (size_t i = 0; i < num_reference_fields; ++i) {
2032 // Note that byte_offset is the offset from the beginning of
2033 // object, not the offset into instance data
2034 const Field* field = fields->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002035 MemberOffset byte_offset = field->GetOffsetDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002036 CHECK_EQ(byte_offset.Uint32Value() & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
2037 if (CLASS_CAN_ENCODE_OFFSET(byte_offset.Uint32Value())) {
2038 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset.Uint32Value());
Brian Carlstrom4873d462011-08-21 15:23:39 -07002039 CHECK_NE(new_bit, 0U);
2040 reference_offsets |= new_bit;
2041 } else {
2042 reference_offsets = CLASS_WALK_SUPER;
2043 break;
2044 }
2045 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002046 // Update fields in klass
2047 if (instance) {
2048 klass->SetReferenceInstanceOffsets(reference_offsets);
2049 } else {
2050 klass->SetReferenceStaticOffsets(reference_offsets);
2051 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002052}
2053
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002054String* ClassLinker::ResolveString(const DexFile& dex_file,
Elliott Hughescf4c6c42011-09-01 15:16:42 -07002055 uint32_t string_idx, DexCache* dex_cache) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002056 String* resolved = dex_cache->GetResolvedString(string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002057 if (resolved != NULL) {
2058 return resolved;
2059 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002060 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
2061 int32_t utf16_length = dex_file.GetStringLength(string_id);
2062 const char* utf8_data = dex_file.GetStringData(string_id);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002063 // TODO: remote the const_cast below
2064 String* string = const_cast<String*>(intern_table_->InternStrong(utf16_length, utf8_data));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002065 dex_cache->SetResolvedString(string_idx, string);
2066 return string;
2067}
2068
2069Class* ClassLinker::ResolveType(const DexFile& dex_file,
2070 uint32_t type_idx,
2071 DexCache* dex_cache,
2072 const ClassLoader* class_loader) {
2073 Class* resolved = dex_cache->GetResolvedType(type_idx);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002074 if (resolved == NULL) {
2075 const char* descriptor = dex_file.dexStringByTypeIdx(type_idx);
2076 if (descriptor[1] == '\0') {
2077 // only the descriptors of primitive types should be 1 character long
2078 resolved = FindPrimitiveClass(descriptor[0]);
2079 } else {
2080 resolved = FindClass(descriptor, class_loader);
2081 }
2082 if (resolved != NULL) {
2083 Class* check = resolved->IsArrayClass() ? resolved->GetComponentType() : resolved;
2084 if (dex_cache != check->GetDexCache()) {
2085 if (check->GetClassLoader() != NULL) {
2086 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
2087 resolved = NULL;
2088 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002089 }
2090 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002091 if (resolved != NULL) {
2092 dex_cache->SetResolvedType(type_idx, resolved);
2093 } else {
2094 DCHECK(Thread::Current()->IsExceptionPending());
2095 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002096 }
2097 return resolved;
2098}
2099
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002100Method* ClassLinker::ResolveMethod(const DexFile& dex_file,
2101 uint32_t method_idx,
2102 DexCache* dex_cache,
2103 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002104 bool is_direct) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002105 Method* resolved = dex_cache->GetResolvedMethod(method_idx);
2106 if (resolved != NULL) {
2107 return resolved;
2108 }
2109 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
2110 Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader);
2111 if (klass == NULL) {
2112 return NULL;
2113 }
2114
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002115 const char* name = dex_file.dexStringById(method_id.name_idx_);
Elliott Hughes0c424cb2011-08-26 10:16:25 -07002116 std::string signature(dex_file.CreateMethodDescriptor(method_id.proto_idx_, NULL));
jeffhaobdb76512011-09-07 11:43:16 -07002117 if (klass->IsInterface()) {
2118 resolved = klass->FindInterfaceMethod(name, signature);
2119 } else if (is_direct) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002120 resolved = klass->FindDirectMethod(name, signature);
2121 } else {
2122 resolved = klass->FindVirtualMethod(name, signature);
2123 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002124 if (resolved != NULL) {
2125 dex_cache->SetResolvedMethod(method_idx, resolved);
2126 } else {
2127 // DCHECK(Thread::Current()->IsExceptionPending());
2128 }
2129 return resolved;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002130}
2131
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002132Field* ClassLinker::ResolveField(const DexFile& dex_file,
2133 uint32_t field_idx,
2134 DexCache* dex_cache,
2135 const ClassLoader* class_loader,
2136 bool is_static) {
2137 Field* resolved = dex_cache->GetResolvedField(field_idx);
2138 if (resolved != NULL) {
2139 return resolved;
2140 }
2141 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx);
2142 Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader);
2143 if (klass == NULL) {
2144 return NULL;
2145 }
2146
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002147 const char* name = dex_file.dexStringById(field_id.name_idx_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002148 Class* field_type = ResolveType(dex_file, field_id.type_idx_, dex_cache, class_loader);
2149 // TODO: LinkageError?
2150 CHECK(field_type != NULL);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002151 if (is_static) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002152 resolved = klass->FindStaticField(name, field_type);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002153 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002154 resolved = klass->FindInstanceField(name, field_type);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002155 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002156 if (resolved != NULL) {
2157 dex_cache->SetResolvedfield(field_idx, resolved);
2158 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002159 // TODO: DCHECK(Thread::Current()->IsExceptionPending());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002160 }
2161 return resolved;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002162}
2163
Elliott Hughese27955c2011-08-26 15:21:24 -07002164size_t ClassLinker::NumLoadedClasses() const {
2165 MutexLock mu(classes_lock_);
2166 return classes_.size();
2167}
2168
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002169} // namespace art