blob: b8cd891e454e2f418050d3aa47e46a891ec9a6a8 [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
106 // Setup the char[] class to be used for String
Brian Carlstrom4873d462011-08-21 15:23:39 -0700107 Class* char_array_class = AllocClass(java_lang_Class, sizeof(Class));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700108 char_array_class->SetArrayRank(1);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700109 CharArray::SetArrayClass(char_array_class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700110
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700111 // Setup String
112 Class* java_lang_String = AllocClass(java_lang_Class, sizeof(StringClass));
113 String::SetClass(java_lang_String);
114 java_lang_String->SetObjectSize(sizeof(String));
115 java_lang_String->SetStatus(Class::kStatusResolved);
Jesse Wilson14150742011-07-29 19:04:44 -0400116
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700117 // Backfill Class descriptors missing until this point
118 // TODO: intern these strings
119 java_lang_Class->SetDescriptor(
120 String::AllocFromModifiedUtf8("Ljava/lang/Class;"));
121 java_lang_Object->SetDescriptor(
122 String::AllocFromModifiedUtf8("Ljava/lang/Object;"));
123 object_array_class->SetDescriptor(
124 String::AllocFromModifiedUtf8("[Ljava/lang/Object;"));
125 java_lang_String->SetDescriptor(
126 String::AllocFromModifiedUtf8("Ljava/lang/String;"));
127 char_array_class->SetDescriptor(String::AllocFromModifiedUtf8("[C"));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700128
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700129 // Create storage for root classes, save away our work so far (requires
130 // descriptors)
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700131 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700132 SetClassRoot(kJavaLangClass, java_lang_Class);
133 SetClassRoot(kJavaLangObject, java_lang_Object);
134 SetClassRoot(kObjectArrayClass, object_array_class);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700135 SetClassRoot(kCharArrayClass, char_array_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700136 SetClassRoot(kJavaLangString, java_lang_String);
137
138 // Setup the primitive type classes.
139 SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z", Class::kPrimBoolean));
140 SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B", Class::kPrimByte));
141 SetClassRoot(kPrimitiveChar, CreatePrimitiveClass("C", Class::kPrimChar));
142 SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S", Class::kPrimShort));
143 SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I", Class::kPrimInt));
144 SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J", Class::kPrimLong));
145 SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F", Class::kPrimFloat));
146 SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D", Class::kPrimDouble));
147 SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V", Class::kPrimVoid));
148
149 // Backfill component type of char[]
150 char_array_class->SetComponentType(GetClassRoot(kPrimitiveChar));
151
152 // Create array interface entries to populate once we can load system classes
153 array_interfaces_ = AllocObjectArray<Class>(2);
154 array_iftable_ = new InterfaceEntry[2];
155
156 // Create int array type for AllocDexCache (done in AppendToBootClassPath)
157 Class* int_array_class = AllocClass(java_lang_Class, sizeof(Class));
158 int_array_class->SetArrayRank(1);
159 int_array_class->SetDescriptor(String::AllocFromModifiedUtf8("[I"));
160 int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt));
161 IntArray::SetArrayClass(int_array_class);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700162 SetClassRoot(kIntArrayClass, int_array_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700163
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700164 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -0700165
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700166 // setup boot_class_path_ and register class_path now that we can
167 // use AllocObjectArray to create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700168 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700169 const DexFile* dex_file = boot_class_path[i];
170 CHECK(dex_file != NULL);
171 AppendToBootClassPath(*dex_file);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700172 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700173 for (size_t i = 0; i != class_path.size(); ++i) {
174 const DexFile* dex_file = class_path[i];
175 CHECK(dex_file != NULL);
176 RegisterDexFile(*dex_file);
177 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700178
179 // Field and Method are necessary so that FindClass can link members
180 Class* java_lang_reflect_Field = AllocClass(java_lang_Class, sizeof(FieldClass));
181 CHECK(java_lang_reflect_Field != NULL);
182 java_lang_reflect_Field->SetDescriptor(String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;"));
183 java_lang_reflect_Field->SetObjectSize(sizeof(Field));
184 SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field);
185 java_lang_reflect_Field->SetStatus(Class::kStatusResolved);
186 Field::SetClass(java_lang_reflect_Field);
187
188 Class* java_lang_reflect_Method = AllocClass(java_lang_Class, sizeof(MethodClass));
189 java_lang_reflect_Method->SetDescriptor(String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;"));
190 CHECK(java_lang_reflect_Method != NULL);
191 java_lang_reflect_Method->SetObjectSize(sizeof(Method));
192 SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method);
193 java_lang_reflect_Method->SetStatus(Class::kStatusResolved);
194 Method::SetClass(java_lang_reflect_Method);
195
196 // now we can use FindSystemClass
197
198 // Object and String just need more minimal setup, since they do not have
199 // extra C++ fields.
200 java_lang_Object->SetStatus(Class::kStatusNotReady);
201 Class* Object_class = FindSystemClass("Ljava/lang/Object;");
202 CHECK_EQ(java_lang_Object, Object_class);
203 CHECK_EQ(java_lang_Object->GetObjectSize(), sizeof(Object));
204 java_lang_String->SetStatus(Class::kStatusNotReady);
205 Class* String_class = FindSystemClass("Ljava/lang/String;");
206 CHECK_EQ(java_lang_String, String_class);
207 CHECK_EQ(java_lang_String->GetObjectSize(), sizeof(String));
208
209 // Setup the primitive array type classes - can't be done until Object has
210 // a vtable
211 SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z"));
212 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
213
214 SetClassRoot(kByteArrayClass, FindSystemClass("[B"));
215 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
216
217 Class* found_char_array_class = FindSystemClass("[C");
218 CHECK_EQ(char_array_class, found_char_array_class);
219
220 SetClassRoot(kShortArrayClass, FindSystemClass("[S"));
221 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
222
223 Class* found_int_array_class = FindSystemClass("[I");
224 CHECK_EQ(int_array_class, found_int_array_class);
225
226 SetClassRoot(kLongArrayClass, FindSystemClass("[J"));
227 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
228
229 SetClassRoot(kFloatArrayClass, FindSystemClass("[F"));
230 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
231
232 SetClassRoot(kDoubleArrayClass, FindSystemClass("[D"));
233 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
234
235 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
236 CHECK_EQ(object_array_class, found_object_array_class);
237
238 // Setup the single, global copies of "interfaces" and "iftable"
239 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
240 CHECK(java_lang_Cloneable != NULL);
241 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
242 CHECK(java_io_Serializable != NULL);
243 CHECK(array_interfaces_ != NULL);
244 array_interfaces_->Set(0, java_lang_Cloneable);
245 array_interfaces_->Set(1, java_io_Serializable);
246 // We assume that Cloneable/Serializable don't have superinterfaces --
247 // normally we'd have to crawl up and explicitly list all of the
248 // supers as well. These interfaces don't have any methods, so we
249 // don't have to worry about the ifviPool either.
250 array_iftable_[0].SetInterface(array_interfaces_->Get(0));
251 array_iftable_[1].SetInterface(array_interfaces_->Get(1));
252
253 // Sanity check Object[]'s interfaces
254 CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0));
255 CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1));
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700256
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700257 // run Class, Field, and Method through FindSystemClass.
258 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700259 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700260 Class* Class_class = FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700261 CHECK_EQ(java_lang_Class, Class_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700262 // No sanity check on size as Class is variably sized
263
264 java_lang_reflect_Field->SetStatus(Class::kStatusNotReady);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700265 Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700266 CHECK_EQ(java_lang_reflect_Field, Field_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 CHECK_LT(java_lang_reflect_Field->GetObjectSize(), sizeof(Field));
268 java_lang_reflect_Field->SetObjectSize(sizeof(Field));
269
270 java_lang_reflect_Method->SetStatus(Class::kStatusNotReady);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700271 Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700272 CHECK_EQ(java_lang_reflect_Method, Method_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700273 CHECK_LT(java_lang_reflect_Method->GetObjectSize(), sizeof(Method));
274 java_lang_reflect_Method->SetObjectSize(sizeof(Method));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700275
276 // java.lang.ref classes need to be specially flagged, but otherwise are normal classes
277 Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700278 java_lang_ref_FinalizerReference->SetAccessFlags(
279 java_lang_ref_FinalizerReference->GetAccessFlags() |
280 kAccClassIsReference | kAccClassIsFinalizerReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700281 Class* java_lang_ref_PhantomReference = FindSystemClass("Ljava/lang/ref/PhantomReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700282 java_lang_ref_PhantomReference->SetAccessFlags(
283 java_lang_ref_PhantomReference->GetAccessFlags() |
284 kAccClassIsReference | kAccClassIsPhantomReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700285 Class* java_lang_ref_SoftReference = FindSystemClass("Ljava/lang/ref/SoftReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700286 java_lang_ref_SoftReference->SetAccessFlags(
287 java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700288 Class* java_lang_ref_WeakReference = FindSystemClass("Ljava/lang/ref/WeakReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 java_lang_ref_WeakReference->SetAccessFlags(
290 java_lang_ref_WeakReference->GetAccessFlags() |
291 kAccClassIsReference | kAccClassIsWeakReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700292
293 // Let the heap know some key offsets into java.lang.ref instances
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 // NB we hard code the field indexes here rather than using FindInstanceField
295 // as the types of the field can't be resolved prior to the runtime being
296 // fully initialized
Brian Carlstrom1f870082011-08-23 16:02:11 -0700297 Class* java_lang_ref_Reference = FindSystemClass("Ljava/lang/ref/Reference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700298
299 Field* pendingNext = java_lang_ref_Reference->GetInstanceField(0);
300 CHECK(pendingNext->GetName()->Equals("pendingNext"));
301 CHECK(ResolveType(pendingNext->GetTypeIdx(), pendingNext) ==
302 java_lang_ref_Reference);
303
304 Field* queue = java_lang_ref_Reference->GetInstanceField(1);
305 CHECK(queue->GetName()->Equals("queue"));
306 CHECK(ResolveType(queue->GetTypeIdx(), queue) ==
307 FindSystemClass("Ljava/lang/ref/ReferenceQueue;"));
308
309 Field* queueNext = java_lang_ref_Reference->GetInstanceField(2);
310 CHECK(queueNext->GetName()->Equals("queueNext"));
311 CHECK(ResolveType(queueNext->GetTypeIdx(), queueNext) ==
312 java_lang_ref_Reference);
313
314 Field* referent = java_lang_ref_Reference->GetInstanceField(3);
315 CHECK(referent->GetName()->Equals("referent"));
316 CHECK(ResolveType(referent->GetTypeIdx(), referent) ==
317 java_lang_Object);
318
319 Field* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2);
320 CHECK(zombie->GetName()->Equals("zombie"));
321 CHECK(ResolveType(zombie->GetTypeIdx(), zombie) ==
322 java_lang_Object);
323
Brian Carlstrom1f870082011-08-23 16:02:11 -0700324 Heap::SetReferenceOffsets(referent->GetOffset(),
325 queue->GetOffset(),
326 queueNext->GetOffset(),
327 pendingNext->GetOffset(),
328 zombie->GetOffset());
329
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330 // Setup the ClassLoaders, adjusting the object_size_ as necessary
331 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
332 CHECK_LT(java_lang_ClassLoader->GetObjectSize(), sizeof(ClassLoader));
333 java_lang_ClassLoader->SetObjectSize(sizeof(ClassLoader));
334 SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader);
335
336 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
337 CHECK_EQ(dalvik_system_BaseDexClassLoader->GetObjectSize(), sizeof(BaseDexClassLoader));
338 SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
339
340 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
341 CHECK_EQ(dalvik_system_PathClassLoader->GetObjectSize(), sizeof(PathClassLoader));
342 SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
343 PathClassLoader::SetClass(dalvik_system_PathClassLoader);
344
345 // Set up java.lang.StackTraceElement as a convenience
Brian Carlstrom1f870082011-08-23 16:02:11 -0700346 SetClassRoot(kJavaLangStackTraceElement, FindSystemClass("Ljava/lang/StackTraceElement;"));
347 SetClassRoot(kJavaLangStackTraceElementArrayClass, FindSystemClass("[Ljava/lang/StackTraceElement;"));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700348 StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700349
Brian Carlstroma663ea52011-08-19 23:33:41 -0700350 FinishInit();
351}
352
353void ClassLinker::FinishInit() {
354 // ensure all class_roots_ are initialized
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700355 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700356 ClassRoot class_root = static_cast<ClassRoot>(i);
357 Class* klass = GetClassRoot(class_root);
358 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700359 DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700360 // note SetClassRoot does additional validation.
361 // if possible add new checks there to catch errors early
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700362 }
363
364 // disable the slow paths in FindClass and CreatePrimitiveClass now
365 // that Object, Class, and Object[] are setup
366 init_done_ = true;
367}
368
Brian Carlstroma663ea52011-08-19 23:33:41 -0700369struct ClassLinker::InitCallbackState {
370 ClassLinker* class_linker;
371
372 Class* class_roots[kClassRootsMax];
373
374 typedef std::tr1::unordered_map<std::string, ClassRoot> Table;
375 Table descriptor_to_class_root;
376
Brian Carlstroma663ea52011-08-19 23:33:41 -0700377 typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set;
378 Set dex_caches;
379};
380
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700381void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path,
382 const std::vector<const DexFile*>& class_path,
383 Space* space) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700384 CHECK(!init_done_);
385
386 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
387 DCHECK(heap_bitmap != NULL);
388
389 InitCallbackState state;
390 state.class_linker = this;
391 for (size_t i = 0; i < kClassRootsMax; i++) {
392 ClassRoot class_root = static_cast<ClassRoot>(i);
393 state.descriptor_to_class_root[GetClassRootDescriptor(class_root)] = class_root;
394 }
395
396 // reinit clases_ table
397 heap_bitmap->Walk(InitCallback, &state);
398
399 // reinit class_roots_
400 Class* object_array_class = state.class_roots[kObjectArrayClass];
401 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
402 for (size_t i = 0; i < kClassRootsMax; i++) {
403 ClassRoot class_root = static_cast<ClassRoot>(i);
404 SetClassRoot(class_root, state.class_roots[class_root]);
405 }
406
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700407 // reinit intern table
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700408 // TODO: remove interned_array, make all strings in image interned (and remove space argument)
Brian Carlstroma663ea52011-08-19 23:33:41 -0700409 ObjectArray<Object>* interned_array = space->GetImageHeader().GetInternedArray();
410 for (int32_t i = 0; i < interned_array->GetLength(); i++) {
411 String* string = interned_array->Get(i)->AsString();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700412 intern_table_->RegisterStrong(string);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700413 }
414
415 // reinit array_interfaces_ from any array class instance, they should all be ==
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700416 array_interfaces_ = GetClassRoot(kObjectArrayClass)->GetInterfaces();
417 DCHECK(array_interfaces_ == GetClassRoot(kBooleanArrayClass)->GetInterfaces());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700418
419 // build a map from location to DexCache to match up with DexFile::GetLocation
420 std::tr1::unordered_map<std::string, DexCache*> location_to_dex_cache;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421 typedef InitCallbackState::Set::const_iterator It; // TODO: C++0x auto
Brian Carlstroma663ea52011-08-19 23:33:41 -0700422 for (It it = state.dex_caches.begin(), end = state.dex_caches.end(); it != end; ++it) {
423 DexCache* dex_cache = *it;
424 std::string location = dex_cache->GetLocation()->ToModifiedUtf8();
425 location_to_dex_cache[location] = dex_cache;
426 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700427 CHECK_EQ(boot_class_path.size() + class_path.size(),
428 location_to_dex_cache.size());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700429
430 // reinit boot_class_path with DexFile arguments and found DexCaches
431 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700432 const DexFile* dex_file = boot_class_path[i];
433 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700434 DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()];
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700435 CHECK(dex_cache != NULL) << dex_file->GetLocation();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700436 AppendToBootClassPath(*dex_file, dex_cache);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700437 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700438
439 // register class_path with DexFile arguments and found DexCaches
440 for (size_t i = 0; i != class_path.size(); ++i) {
441 const DexFile* dex_file = class_path[i];
442 CHECK(dex_file != NULL);
443 DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()];
444 CHECK(dex_cache != NULL) << dex_file->GetLocation();
445 RegisterDexFile(*dex_file, dex_cache);
446 }
447
Brian Carlstroma663ea52011-08-19 23:33:41 -0700448 String::SetClass(GetClassRoot(kJavaLangString));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700449 Field::SetClass(GetClassRoot(kJavaLangReflectField));
450 Method::SetClass(GetClassRoot(kJavaLangReflectMethod));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700451 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
452 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
453 CharArray::SetArrayClass(GetClassRoot(kCharArrayClass));
454 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
455 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
456 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
457 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
458 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700459 PathClassLoader::SetClass(GetClassRoot(kDalvikSystemPathClassLoader));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700460 StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700461
462 FinishInit();
463}
464
Brian Carlstrom4873d462011-08-21 15:23:39 -0700465void ClassLinker::InitCallback(Object* obj, void *arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700466 DCHECK(obj != NULL);
467 DCHECK(arg != NULL);
468 InitCallbackState* state = reinterpret_cast<InitCallbackState*>(arg);
469
470 if (!obj->IsClass()) {
471 return;
472 }
473 Class* klass = obj->AsClass();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700474 // TODO: restore ClassLoader's list of DexFiles after image load
475 // CHECK(klass->GetClassLoader() == NULL);
476 const ClassLoader* class_loader = klass->GetClassLoader();
477 if (class_loader != NULL) {
478 // TODO: replace this hack with something based on command line arguments
479 Thread::Current()->SetClassLoaderOverride(class_loader);
480 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700481
482 std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700483 // restore class to ClassLinker::classes_ table
484 state->class_linker->InsertClass(descriptor, klass);
485
486 // note DexCache to match with DexFile later
487 DexCache* dex_cache = klass->GetDexCache();
488 if (dex_cache != NULL) {
489 state->dex_caches.insert(dex_cache);
490 } else {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700491 DCHECK(klass->IsArrayClass() || klass->IsPrimitive());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700492 }
493
494 // check if this is a root, if so, register it
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700495 typedef InitCallbackState::Table::const_iterator It; // TODO: C++0x auto
Brian Carlstroma663ea52011-08-19 23:33:41 -0700496 It it = state->descriptor_to_class_root.find(descriptor);
497 if (it != state->descriptor_to_class_root.end()) {
498 ClassRoot class_root = it->second;
499 state->class_roots[class_root] = klass;
500 }
501}
502
503// Keep in sync with InitCallback. Anything we visit, we need to
504// reinit references to when reinitializing a ClassLinker from a
505// mapped image.
Elliott Hughes410c0c82011-09-01 17:58:25 -0700506void ClassLinker::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
507 visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700508
509 for (size_t i = 0; i < dex_caches_.size(); i++) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700510 visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700511 }
512
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700513 {
514 MutexLock mu(classes_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700515 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700516 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700517 visitor(it->second, arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700518 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700519 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700520
Elliott Hughes410c0c82011-09-01 17:58:25 -0700521 visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700522}
523
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700524ClassLinker::~ClassLinker() {
525 delete classes_lock_;
526 String::ResetClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700527 Field::ResetClass();
528 Method::ResetClass();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700529 BooleanArray::ResetArrayClass();
530 ByteArray::ResetArrayClass();
531 CharArray::ResetArrayClass();
532 DoubleArray::ResetArrayClass();
533 FloatArray::ResetArrayClass();
534 IntArray::ResetArrayClass();
535 LongArray::ResetArrayClass();
536 ShortArray::ResetArrayClass();
537 PathClassLoader::ResetClass();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700538 StackTraceElement::ResetClass();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700539}
540
541DexCache* ClassLinker::AllocDexCache(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700542 DexCache* dex_cache = down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::LengthAsArray()));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700543 dex_cache->Init(String::AllocFromModifiedUtf8(dex_file.GetLocation().c_str()),
544 AllocObjectArray<String>(dex_file.NumStringIds()),
545 AllocObjectArray<Class>(dex_file.NumTypeIds()),
546 AllocObjectArray<Method>(dex_file.NumMethodIds()),
Brian Carlstrom83db7722011-08-26 17:32:56 -0700547 AllocObjectArray<Field>(dex_file.NumFieldIds()),
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700548 AllocCodeAndDirectMethods(dex_file.NumMethodIds()),
549 AllocObjectArray<StaticStorageBase>(dex_file.NumTypeIds()));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700550 return dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700551}
552
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700553CodeAndDirectMethods* ClassLinker::AllocCodeAndDirectMethods(size_t length) {
554 return down_cast<CodeAndDirectMethods*>(IntArray::Alloc(CodeAndDirectMethods::LengthAsArray(length)));
Brian Carlstrom83db7722011-08-26 17:32:56 -0700555}
556
Brian Carlstrom4873d462011-08-21 15:23:39 -0700557Class* ClassLinker::AllocClass(Class* java_lang_Class, size_t class_size) {
558 DCHECK_GE(class_size, sizeof(Class));
559 Class* klass = Heap::AllocObject(java_lang_Class, class_size)->AsClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700560 klass->SetPrimitiveType(Class::kPrimNot); // default to not being primitive
561 klass->SetClassSize(class_size);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700562 return klass;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700563}
564
Brian Carlstrom4873d462011-08-21 15:23:39 -0700565Class* ClassLinker::AllocClass(size_t class_size) {
566 return AllocClass(GetClassRoot(kJavaLangClass), class_size);
Brian Carlstroma0808032011-07-18 00:39:23 -0700567}
568
Jesse Wilson35baaab2011-08-10 16:18:03 -0400569Field* ClassLinker::AllocField() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700570 return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->AllocObject());
Brian Carlstroma0808032011-07-18 00:39:23 -0700571}
572
573Method* ClassLinker::AllocMethod() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700574 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->AllocObject());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700575}
576
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700577ObjectArray<StackTraceElement>* ClassLinker::AllocStackTraceElementArray(size_t length) {
578 return ObjectArray<StackTraceElement>::Alloc(
579 GetClassRoot(kJavaLangStackTraceElementArrayClass),
580 length);
581}
582
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700583Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700584 const ClassLoader* class_loader) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700585 // TODO: remove this contrived parent class loader check when we have a real ClassLoader.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700586 if (class_loader != NULL) {
587 Class* klass = FindClass(descriptor, NULL);
588 if (klass != NULL) {
589 return klass;
590 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700591 Thread::Current()->ClearException();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700592 }
593
Carl Shapirob5573532011-07-12 18:22:59 -0700594 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700595 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700596 CHECK(!self->IsExceptionPending());
597 // Find the class in the loaded classes table.
598 Class* klass = LookupClass(descriptor, class_loader);
599 if (klass == NULL) {
600 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700601 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700602 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700603 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700604 const DexFile::ClassPath& class_path = ((class_loader != NULL)
605 ? ClassLoader::GetClassPath(class_loader)
606 : boot_class_path_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700607 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700608 if (pair.second == NULL) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700609 std::string name(PrintableString(descriptor));
610 self->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
611 "Class %s not found in class loader %p", name.c_str(), class_loader);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700612 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700613 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700614 const DexFile& dex_file = *pair.first;
615 const DexFile::ClassDef& dex_class_def = *pair.second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700616 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700617 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700618 if (!init_done_) {
619 // finish up init of hand crafted class_roots_
620 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700621 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700622 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700623 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400624 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700625 klass = GetClassRoot(kJavaLangString);
626 } else if (descriptor == "Ljava/lang/reflect/Field;") {
627 klass = GetClassRoot(kJavaLangReflectField);
628 } else if (descriptor == "Ljava/lang/reflect/Method;") {
629 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700630 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700631 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700632 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700633 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700634 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Carl Shapiro565f5072011-07-10 13:39:43 -0700635 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700636 if (!klass->IsLinked()) {
637 klass->SetDexCache(dex_cache);
638 LoadClass(dex_file, dex_class_def, klass, class_loader);
639 // Check for a pending exception during load
640 if (self->IsExceptionPending()) {
641 // TODO: free native allocations in klass
642 return NULL;
643 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700644 ObjectLock lock(klass);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700645 klass->SetClinitThreadId(self->GetId());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700646 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700647 bool success = InsertClass(descriptor, klass); // TODO: just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700648 if (!success) {
649 // We may fail to insert if we raced with another thread.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700650 klass->SetClinitThreadId(0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700651 // TODO: free native allocations in klass
652 klass = LookupClass(descriptor, class_loader);
653 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700654 return klass;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700655 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700656 // Finish loading (if necessary) by finding parents
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700657 CHECK(!klass->IsLoaded());
658 if (!LoadSuperAndInterfaces(klass, dex_file)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700659 // Loading failed.
660 // TODO: CHECK(self->IsExceptionPending());
661 lock.NotifyAll();
662 return NULL;
663 }
664 CHECK(klass->IsLoaded());
665 // Link the class (if necessary)
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700666 CHECK(!klass->IsLinked());
667 if (!LinkClass(klass)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700668 // Linking failed.
669 // TODO: CHECK(self->IsExceptionPending());
670 lock.NotifyAll();
671 return NULL;
672 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700673 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700674 }
675 }
676 }
677 // Link the class if it has not already been linked.
678 if (!klass->IsLinked() && !klass->IsErroneous()) {
679 ObjectLock lock(klass);
680 // Check for circular dependencies between classes.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700681 if (!klass->IsLinked() && klass->GetClinitThreadId() == self->GetId()) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700682 self->ThrowNewException("Ljava/lang/ClassCircularityError;", NULL); // TODO: detail
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700683 return NULL;
684 }
685 // Wait for the pending initialization to complete.
686 while (!klass->IsLinked() && !klass->IsErroneous()) {
687 lock.Wait();
688 }
689 }
690 if (klass->IsErroneous()) {
691 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
692 return NULL;
693 }
694 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700695 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700696 CHECK(!self->IsExceptionPending());
697 return klass;
698}
699
Brian Carlstrom4873d462011-08-21 15:23:39 -0700700// Precomputes size that will be needed for Class, matching LinkStaticFields
701size_t ClassLinker::SizeOfClass(const DexFile& dex_file,
702 const DexFile::ClassDef& dex_class_def) {
703 const byte* class_data = dex_file.GetClassData(dex_class_def);
704 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
705 size_t num_static_fields = header.static_fields_size_;
706 size_t num_ref = 0;
707 size_t num_32 = 0;
708 size_t num_64 = 0;
709 if (num_static_fields != 0) {
710 uint32_t last_idx = 0;
711 for (size_t i = 0; i < num_static_fields; ++i) {
712 DexFile::Field dex_field;
713 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
714 const DexFile::FieldId& field_id = dex_file.GetFieldId(dex_field.field_idx_);
715 const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_);
716 char c = descriptor[0];
717 if (c == 'L' || c == '[') {
718 num_ref++;
719 } else if (c == 'J' || c == 'D') {
720 num_64++;
721 } else {
722 num_32++;
723 }
724 }
725 }
726
727 // start with generic class data
728 size_t size = sizeof(Class);
729 // follow with reference fields which must be contiguous at start
730 size += (num_ref * sizeof(uint32_t));
731 // if there are 64-bit fields to add, make sure they are aligned
732 if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment
733 if (num_32 != 0) {
734 // use an available 32-bit field for padding
735 num_32--;
736 }
737 size += sizeof(uint32_t); // either way, we are adding a word
738 DCHECK_EQ(size, RoundUp(size, 8));
739 }
740 // tack on any 64-bit fields now that alignment is assured
741 size += (num_64 * sizeof(uint64_t));
742 // tack on any remaining 32-bit fields
743 size += (num_32 * sizeof(uint32_t));
744 return size;
745}
746
Brian Carlstromf615a612011-07-23 12:50:34 -0700747void ClassLinker::LoadClass(const DexFile& dex_file,
748 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700749 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700750 const ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700751 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700752 CHECK(klass->GetDexCache() != NULL);
753 CHECK_EQ(Class::kStatusNotReady, klass->GetStatus());
Brian Carlstromf615a612011-07-23 12:50:34 -0700754 const byte* class_data = dex_file.GetClassData(dex_class_def);
755 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700756
Brian Carlstromf615a612011-07-23 12:50:34 -0700757 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700758 CHECK(descriptor != NULL);
759
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700760 klass->SetClass(GetClassRoot(kJavaLangClass));
761 if (klass->GetDescriptor() != NULL) {
762 DCHECK(klass->GetDescriptor()->Equals(descriptor));
763 } else {
764 klass->SetDescriptor(String::AllocFromModifiedUtf8(descriptor));
765 }
766 uint32_t access_flags = dex_class_def.access_flags_;
767 // Make sure there aren't any "bonus" flags set, since we use them for runtime
768 // state.
769 CHECK_EQ(access_flags & ~kAccClassFlagsMask, 0U);
770 klass->SetAccessFlags(access_flags);
771 klass->SetClassLoader(class_loader);
772 DCHECK(klass->GetPrimitiveType() == Class::kPrimNot);
773 klass->SetStatus(Class::kStatusIdx);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700774
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700775 klass->SetSuperClassTypeIdx(dex_class_def.superclass_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700776
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700777 size_t num_static_fields = header.static_fields_size_;
778 size_t num_instance_fields = header.instance_fields_size_;
779 size_t num_direct_methods = header.direct_methods_size_;
780 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700781
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700782 klass->SetSourceFile(dex_file.dexGetSourceFile(dex_class_def));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700783
784 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700785 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700786
787 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700788 if (num_static_fields != 0) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700789 klass->SetSFields(AllocObjectArray<Field>(num_static_fields));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700790 uint32_t last_idx = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700791 for (size_t i = 0; i < num_static_fields; ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700792 DexFile::Field dex_field;
793 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400794 Field* sfield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700795 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700796 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700797 }
798 }
799
800 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700801 if (num_instance_fields != 0) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700802 klass->SetIFields(AllocObjectArray<Field>(num_instance_fields));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700803 uint32_t last_idx = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700804 for (size_t i = 0; i < num_instance_fields; ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700805 DexFile::Field dex_field;
806 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400807 Field* ifield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700808 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700809 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700810 }
811 }
812
813 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700814 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700815 // TODO: append direct methods to class object
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700816 klass->SetDirectMethods(AllocObjectArray<Method>(num_direct_methods));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700817 uint32_t last_idx = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700818 for (size_t i = 0; i < num_direct_methods; ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700819 DexFile::Method dex_method;
820 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700821 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700822 klass->SetDirectMethod(i, meth);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700823 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700824 // TODO: register maps
825 }
826 }
827
828 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700829 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700830 // TODO: append virtual methods to class object
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700831 klass->SetVirtualMethods(AllocObjectArray<Method>(num_virtual_methods));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700832 uint32_t last_idx = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700833 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700834 DexFile::Method dex_method;
835 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700836 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700837 klass->SetVirtualMethod(i, meth);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700838 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700839 // TODO: register maps
840 }
841 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700842}
843
Brian Carlstromf615a612011-07-23 12:50:34 -0700844void ClassLinker::LoadInterfaces(const DexFile& dex_file,
845 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700846 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700847 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700848 if (list != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700849 klass->SetInterfaces(AllocObjectArray<Class>(list->Size()));
850 IntArray* interfaces_idx = IntArray::Alloc(list->Size());
851 klass->SetInterfacesTypeIdx(interfaces_idx);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700852 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700853 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700854 interfaces_idx->Set(i, type_item.type_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700855 }
856 }
857}
858
Brian Carlstromf615a612011-07-23 12:50:34 -0700859void ClassLinker::LoadField(const DexFile& dex_file,
860 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700861 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700862 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700863 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700864 dst->SetDeclaringClass(klass);
865 dst->SetName(ResolveString(dex_file, field_id.name_idx_, klass->GetDexCache()));
866 dst->SetTypeIdx(field_id.type_idx_);
867 dst->SetAccessFlags(src.access_flags_);
868
869 // In order to access primitive types using GetTypeDuringLinking we need to
870 // ensure they are resolved into the dex cache
871 const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_);
872 if (descriptor[1] == '\0') {
873 // only the descriptors of primitive types should be 1 character long
874 Class* resolved = ResolveType(dex_file, field_id.type_idx_, klass);
875 DCHECK(resolved->IsPrimitive());
876 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700877}
878
Brian Carlstromf615a612011-07-23 12:50:34 -0700879void ClassLinker::LoadMethod(const DexFile& dex_file,
880 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700881 Class* klass,
Brian Carlstrom1f870082011-08-23 16:02:11 -0700882 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700883 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700884 dst->SetDeclaringClass(klass);
885 dst->SetName(ResolveString(dex_file, method_id.name_idx_, klass->GetDexCache()));
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700886 {
887 int32_t utf16_length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700888 std::string utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_, &utf16_length));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700889 dst->SetSignature(String::AllocFromModifiedUtf8(utf16_length, utf8.c_str()));
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700890 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700891 dst->SetProtoIdx(method_id.proto_idx_);
892 dst->SetCodeItemOffset(src.code_off_);
893 const char* shorty = dex_file.GetShorty(method_id.proto_idx_);
894 dst->SetShorty(shorty);
895 dst->SetAccessFlags(src.access_flags_);
896 dst->SetReturnTypeIdx(dex_file.GetProtoId(method_id.proto_idx_).return_type_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700897
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700898 dst->SetDexCacheStrings(klass->GetDexCache()->GetStrings());
899 dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes());
900 dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods());
901 dst->SetDexCacheResolvedFields(klass->GetDexCache()->GetResolvedFields());
902 dst->SetDexCacheCodeAndDirectMethods(klass->GetDexCache()->GetCodeAndDirectMethods());
903 dst->SetDexCacheInitializedStaticStorage(klass->GetDexCache()->GetInitializedStaticStorage());
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700904
Brian Carlstrom934486c2011-07-12 23:42:50 -0700905 // TODO: check for finalize method
906
Brian Carlstromf615a612011-07-23 12:50:34 -0700907 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700908 if (code_item != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700909 dst->SetNumRegisters(code_item->registers_size_);
910 dst->SetNumIns(code_item->ins_size_);
911 dst->SetNumOuts(code_item->outs_size_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700912 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700913 uint16_t num_args = Method::NumArgRegisters(shorty);
914 if ((src.access_flags_ & kAccStatic) != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700915 ++num_args;
916 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700917 dst->SetNumRegisters(num_args);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700918 // TODO: native methods
919 }
920}
921
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700922void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700923 AppendToBootClassPath(dex_file, AllocDexCache(dex_file));
924}
925
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700926void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700927 CHECK(dex_cache != NULL) << dex_file.GetLocation();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700928 boot_class_path_.push_back(&dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700929 RegisterDexFile(dex_file, dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700930}
931
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700932void ClassLinker::RegisterDexFile(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700933 RegisterDexFile(dex_file, AllocDexCache(dex_file));
934}
935
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700936void ClassLinker::RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700937 CHECK(dex_cache != NULL) << dex_file.GetLocation();
938 CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation()));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700939 dex_files_.push_back(&dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700940 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700941}
942
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700943const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700944 for (size_t i = 0; i != dex_caches_.size(); ++i) {
945 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700946 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700947 }
948 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700949 CHECK(false) << "Failed to find DexFile for DexCache " << dex_cache->GetLocation()->ToModifiedUtf8();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700950 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700951}
952
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700953DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700954 for (size_t i = 0; i != dex_files_.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700955 if (dex_files_[i] == &dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700956 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700957 }
958 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700959 CHECK(false) << "Failed to find DexCache for DexFile " << dex_file.GetLocation();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700960 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700961}
962
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700963Class* ClassLinker::CreatePrimitiveClass(const char* descriptor,
964 Class::PrimitiveType type) {
965 // TODO: deduce one argument from the other
Brian Carlstrom4873d462011-08-21 15:23:39 -0700966 Class* klass = AllocClass(sizeof(Class));
Carl Shapiro565f5072011-07-10 13:39:43 -0700967 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700968 klass->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract);
969 klass->SetDescriptor(String::AllocFromModifiedUtf8(descriptor));
970 klass->SetPrimitiveType(type);
971 klass->SetStatus(Class::kStatusInitialized);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700972 bool success = InsertClass(descriptor, klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700973 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700974 return klass;
975}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700976
Brian Carlstrombe977852011-07-19 14:54:54 -0700977// Create an array class (i.e. the class object for the array, not the
978// array itself). "descriptor" looks like "[C" or "[[[[B" or
979// "[Ljava/lang/String;".
980//
981// If "descriptor" refers to an array of primitives, look up the
982// primitive type's internally-generated class object.
983//
984// "loader" is the class loader of the class that's referring to us. It's
985// used to ensure that we're looking for the element type in the right
986// context. It does NOT become the class loader for the array class; that
987// always comes from the base element class.
988//
989// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700990Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700991 const ClassLoader* class_loader) {
992 CHECK_EQ('[', descriptor[0]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700993
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700994 // Identify the underlying element class and the array dimension depth.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700995 Class* component_type = NULL;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700996 int array_rank;
997 if (descriptor[1] == '[') {
998 // array of arrays; keep descriptor and grab stuff from parent
999 Class* outer = FindClass(descriptor.substr(1), class_loader);
1000 if (outer != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001001 // want the base class, not "outer", in our component_type
1002 component_type = outer->GetComponentType();
1003 array_rank = outer->GetArrayRank() + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001004 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001005 DCHECK(component_type == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001006 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001007 } else {
1008 array_rank = 1;
1009 if (descriptor[1] == 'L') {
1010 // array of objects; strip off "[" and look up descriptor.
1011 const StringPiece subDescriptor = descriptor.substr(1);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001012 component_type = FindClass(subDescriptor, class_loader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001013 } else {
1014 // array of a primitive type
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001015 component_type = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001016 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001017 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001018
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001019 if (component_type == NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001020 // failed
1021 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
1022 return NULL;
1023 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001024
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001025 // See if the component type is already loaded. Array classes are
1026 // always associated with the class loader of their underlying
1027 // element type -- an array of Strings goes with the loader for
1028 // java/lang/String -- so we need to look for it there. (The
1029 // caller should have checked for the existence of the class
1030 // before calling here, but they did so with *their* class loader,
1031 // not the component type's loader.)
1032 //
1033 // If we find it, the caller adds "loader" to the class' initiating
1034 // loader list, which should prevent us from going through this again.
1035 //
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001036 // This call is unnecessary if "loader" and "component_type->GetClassLoader()"
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001037 // are the same, because our caller (FindClass) just did the
1038 // lookup. (Even if we get this wrong we still have correct behavior,
1039 // because we effectively do this lookup again when we add the new
1040 // class to the hash table --- necessary because of possible races with
1041 // other threads.)
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001042 if (class_loader != component_type->GetClassLoader()) {
1043 Class* new_class = LookupClass(descriptor, component_type->GetClassLoader());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001044 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001045 return new_class;
1046 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001047 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001048
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001049 // Fill out the fields in the Class.
1050 //
1051 // It is possible to execute some methods against arrays, because
1052 // all arrays are subclasses of java_lang_Object_, so we need to set
1053 // up a vtable. We can just point at the one in java_lang_Object_.
1054 //
1055 // Array classes are simple enough that we don't need to do a full
1056 // link step.
1057
1058 Class* new_class = NULL;
1059 if (!init_done_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001060 // Classes that were hand created, ie not by FindSystemClass
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001061 if (descriptor == "[Ljava/lang/Object;") {
1062 new_class = GetClassRoot(kObjectArrayClass);
1063 } else if (descriptor == "[C") {
1064 new_class = GetClassRoot(kCharArrayClass);
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001065 } else if (descriptor == "[I") {
1066 new_class = GetClassRoot(kIntArrayClass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001067 }
1068 }
1069 if (new_class == NULL) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001070 new_class = AllocClass(sizeof(Class));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001071 if (new_class == NULL) {
1072 return NULL;
1073 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001074 new_class->SetArrayRank(array_rank);
1075 new_class->SetComponentType(component_type);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001076 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001077 DCHECK_LE(1, new_class->GetArrayRank());
1078 DCHECK(new_class->GetComponentType() != NULL);
1079 new_class->SetDescriptor(String::AllocFromModifiedUtf8(descriptor.ToString().c_str()));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001080 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001081 new_class->SetSuperClass(java_lang_Object);
1082 new_class->SetVTable(java_lang_Object->GetVTable());
1083 new_class->SetPrimitiveType(Class::kPrimNot);
1084 new_class->SetClassLoader(component_type->GetClassLoader());
1085 new_class->SetStatus(Class::kStatusInitialized);
1086 // don't need to set new_class->SetObjectSize(..)
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001087 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001088
1089
1090 // All arrays have java/lang/Cloneable and java/io/Serializable as
1091 // interfaces. We need to set that up here, so that stuff like
1092 // "instanceof" works right.
1093 //
1094 // Note: The GC could run during the call to FindSystemClass,
1095 // so we need to make sure the class object is GC-valid while we're in
1096 // there. Do this by clearing the interface list so the GC will just
1097 // think that the entries are null.
1098
1099
1100 // Use the single, global copies of "interfaces" and "iftable"
1101 // (remember not to free them for arrays).
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001102 new_class->SetInterfaces(array_interfaces_);
1103 new_class->SetIFTableCount(2);
1104 new_class->SetIFTable(array_iftable_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001105
1106 // Inherit access flags from the component type. Arrays can't be
1107 // used as a superclass or interface, so we want to add "final"
1108 // and remove "interface".
1109 //
1110 // Don't inherit any non-standard flags (e.g., kAccFinal)
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001111 // from component_type. We assume that the array class does not
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001112 // override finalize().
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001113 new_class->SetAccessFlags(((new_class->GetComponentType()->GetAccessFlags() &
1114 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001115
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001116 if (InsertClass(descriptor, new_class)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001117 return new_class;
1118 }
1119 // Another thread must have loaded the class after we
1120 // started but before we finished. Abandon what we've
1121 // done.
1122 //
1123 // (Yes, this happens.)
1124
1125 // Grab the winning class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001126 Class* other_class = LookupClass(descriptor, component_type->GetClassLoader());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001127 DCHECK(other_class != NULL);
1128 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001129}
1130
1131Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -07001132 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001133 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001134 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001135 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001136 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001137 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001138 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001139 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001140 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001141 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001142 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001143 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001144 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001145 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001146 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001147 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001148 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001149 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001150 return GetClassRoot(kPrimitiveVoid);
Carl Shapiro744ad052011-08-06 15:53:36 -07001151 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001152 std::string printable_type(PrintableChar(type));
1153 Thread::Current()->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
1154 "Not a primitive type: %s", printable_type.c_str());
1155 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001156}
1157
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001158bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) {
1159 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001160 MutexLock mu(classes_lock_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001161 Table::iterator it = classes_.insert(std::make_pair(hash, klass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001162 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001163}
1164
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001165Class* ClassLinker::LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001166 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001167 MutexLock mu(classes_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001168 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001169 for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001170 Class* klass = it->second;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001171 if (klass->GetDescriptor()->Equals(descriptor) && klass->GetClassLoader() == class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001172 return klass;
1173 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001174 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001175 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001176}
1177
1178bool ClassLinker::InitializeClass(Class* klass) {
1179 CHECK(klass->GetStatus() == Class::kStatusResolved ||
Elliott Hughesf5ecf062011-09-06 17:37:59 -07001180 klass->GetStatus() == Class::kStatusError) << klass->GetStatus();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001181
Carl Shapirob5573532011-07-12 18:22:59 -07001182 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001183
1184 {
1185 ObjectLock lock(klass);
1186
1187 if (klass->GetStatus() < Class::kStatusVerified) {
1188 if (klass->IsErroneous()) {
1189 LG << "re-initializing failed class"; // TODO: throw
1190 return false;
1191 }
1192
1193 CHECK(klass->GetStatus() == Class::kStatusResolved);
1194
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001195 klass->SetStatus(Class::kStatusVerifying);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001196 if (!DexVerify::VerifyClass(klass)) {
1197 LG << "Verification failed"; // TODO: ThrowVerifyError
1198 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001199 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001200 klass->SetStatus(Class::kStatusError);
1201 return false;
1202 }
1203
1204 klass->SetStatus(Class::kStatusVerified);
1205 }
1206
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001207 if (klass->GetStatus() == Class::kStatusInitialized) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001208 return true;
1209 }
1210
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001211 while (klass->GetStatus() == Class::kStatusInitializing) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001212 // we caught somebody else in the act; was it us?
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001213 if (klass->GetClinitThreadId() == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001214 LG << "recursive <clinit>";
1215 return true;
1216 }
1217
1218 CHECK(!self->IsExceptionPending());
1219
1220 lock.Wait(); // TODO: check for interruption
1221
1222 // When we wake up, repeat the test for init-in-progress. If
1223 // there's an exception pending (only possible if
1224 // "interruptShouldThrow" was set), bail out.
1225 if (self->IsExceptionPending()) {
1226 CHECK(false);
1227 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
1228 klass->SetStatus(Class::kStatusError);
1229 return false;
1230 }
1231 if (klass->GetStatus() == Class::kStatusInitializing) {
1232 continue;
1233 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001234 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001235 klass->GetStatus() == Class::kStatusError);
1236 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001237 // The caller wants an exception, but it was thrown in a
1238 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001239 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
1240 return false;
1241 }
1242 return true; // otherwise, initialized
1243 }
1244
1245 // see if we failed previously
1246 if (klass->IsErroneous()) {
1247 // might be wise to unlock before throwing; depends on which class
1248 // it is that we have locked
1249
1250 // TODO: throwEarlierClassFailure(klass);
1251 return false;
1252 }
1253
1254 if (!ValidateSuperClassDescriptors(klass)) {
1255 klass->SetStatus(Class::kStatusError);
1256 return false;
1257 }
1258
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001259 DCHECK(klass->GetStatus() < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001260
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001261 klass->SetClinitThreadId(self->GetId());
1262 klass->SetStatus(Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001263 }
1264
1265 if (!InitializeSuperClass(klass)) {
1266 return false;
1267 }
1268
1269 InitializeStaticFields(klass);
1270
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001271 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001272 if (clinit != NULL) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -07001273 clinit->Invoke(self, NULL, NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001274 }
1275
1276 {
1277 ObjectLock lock(klass);
1278
1279 if (self->IsExceptionPending()) {
1280 klass->SetStatus(Class::kStatusError);
1281 } else {
1282 klass->SetStatus(Class::kStatusInitialized);
1283 }
1284 lock.NotifyAll();
1285 }
1286
1287 return true;
1288}
1289
1290bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
1291 if (klass->IsInterface()) {
1292 return true;
1293 }
1294 // begin with the methods local to the superclass
1295 if (klass->HasSuperClass() &&
1296 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
1297 const Class* super = klass->GetSuperClass();
1298 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001299 const Method* method = super->GetVirtualMethod(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001300 if (method != super->GetVirtualMethod(i) &&
1301 !HasSameMethodDescriptorClasses(method, super, klass)) {
1302 LG << "Classes resolve differently in superclass";
1303 return false;
1304 }
1305 }
1306 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001307 for (size_t i = 0; i < klass->GetIFTableCount(); ++i) {
1308 const InterfaceEntry* iftable = &klass->GetIFTable()[i];
Brian Carlstrom30b94452011-08-25 21:35:26 -07001309 Class* interface = iftable->GetInterface();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001310 if (klass->GetClassLoader() != interface->GetClassLoader()) {
1311 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001312 uint32_t vtable_index = iftable->GetMethodIndexArray()[j];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001313 const Method* method = klass->GetVirtualMethod(vtable_index);
1314 if (!HasSameMethodDescriptorClasses(method, interface,
1315 method->GetClass())) {
1316 LG << "Classes resolve differently in interface"; // TODO: LinkageError
1317 return false;
1318 }
1319 }
1320 }
1321 }
1322 return true;
1323}
1324
1325bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001326 const Class* klass1,
1327 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001328 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001329 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->GetProtoIdx());
Brian Carlstromf615a612011-07-23 12:50:34 -07001330 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001331 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001332 const char* descriptor = it->GetDescriptor();
1333 if (descriptor == NULL) {
1334 break;
1335 }
1336 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1337 // Found a non-primitive type.
1338 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1339 return false;
1340 }
1341 }
1342 }
1343 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001344 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001345 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1346 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1347 return false;
1348 }
1349 }
1350 return true;
1351}
1352
1353// Returns true if classes referenced by the descriptor are the
1354// same classes in klass1 as they are in klass2.
1355bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001356 const Class* klass1,
1357 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 CHECK(descriptor != NULL);
1359 CHECK(klass1 != NULL);
1360 CHECK(klass2 != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001361 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001362 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001363 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001364 // TODO: found2 == NULL
1365 // TODO: lookup found1 in initiating loader list
1366 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -07001367 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001368 if (found1 == found2) {
1369 return true;
1370 } else {
1371 return false;
1372 }
1373 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001374 return true;
1375}
1376
1377bool ClassLinker::InitializeSuperClass(Class* klass) {
1378 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001379 if (!klass->IsInterface() && klass->HasSuperClass()) {
1380 Class* super_class = klass->GetSuperClass();
1381 if (super_class->GetStatus() != Class::kStatusInitialized) {
1382 CHECK(!super_class->IsInterface());
1383 klass->MonitorExit();
1384 bool super_initialized = InitializeClass(super_class);
1385 klass->MonitorEnter();
1386 // TODO: check for a pending exception
1387 if (!super_initialized) {
1388 klass->SetStatus(Class::kStatusError);
1389 klass->NotifyAll();
1390 return false;
1391 }
1392 }
1393 }
1394 return true;
1395}
1396
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001397bool ClassLinker::EnsureInitialized(Class* c) {
1398 CHECK(c != NULL);
1399 if (c->IsInitialized()) {
1400 return true;
1401 }
1402
1403 c->MonitorExit();
1404 InitializeClass(c);
1405 c->MonitorEnter();
1406 return !Thread::Current()->IsExceptionPending();
1407}
1408
Brian Carlstromb9edb842011-08-28 16:31:06 -07001409StaticStorageBase* ClassLinker::InitializeStaticStorageFromCode(uint32_t type_idx,
1410 const Method* referrer) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001411 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1412 Class* klass = class_linker->ResolveType(type_idx, referrer);
1413 if (klass == NULL) {
1414 UNIMPLEMENTED(FATAL) << "throw exception due to unresolved class";
1415 }
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001416 // If we are the <clinit> of this class, just return our storage.
1417 //
1418 // Do not set the DexCache InitializedStaticStorage, since that
1419 // implies <clinit> has finished running.
1420 if (klass == referrer->GetDeclaringClass() && referrer->GetName()->Equals("<clinit>")) {
1421 return klass;
1422 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001423 if (!class_linker->EnsureInitialized(klass)) {
1424 CHECK(Thread::Current()->IsExceptionPending());
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001425 UNIMPLEMENTED(FATAL) << "throw exception due to class initialization problem";
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001426 }
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001427 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001428 return klass;
1429}
1430
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001431void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001432 size_t num_static_fields = klass->NumStaticFields();
1433 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001434 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001435 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001436 DexCache* dex_cache = klass->GetDexCache();
Brian Carlstrom4873d462011-08-21 15:23:39 -07001437 // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray?
Brian Carlstromf615a612011-07-23 12:50:34 -07001438 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001439 return;
1440 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001441 const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001442 const DexFile& dex_file = FindDexFile(dex_cache);
1443 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001444 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001445 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001446 if (addr == NULL) {
1447 // All this class' static fields have default values.
1448 return;
1449 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001450 size_t array_size = DecodeUnsignedLeb128(&addr);
1451 for (size_t i = 0; i < array_size; ++i) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001452 Field* field = klass->GetStaticField(i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001453 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001454 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001455 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001456 case DexFile::kByte:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001457 field->SetByte(NULL, value.b);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001458 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001459 case DexFile::kShort:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001460 field->SetShort(NULL, value.s);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001461 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001462 case DexFile::kChar:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001463 field->SetChar(NULL, value.c);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001464 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001465 case DexFile::kInt:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001466 field->SetInt(NULL, value.i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001467 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001468 case DexFile::kLong:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001469 field->SetLong(NULL, value.j);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001470 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001471 case DexFile::kFloat:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001472 field->SetFloat(NULL, value.f);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001473 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001474 case DexFile::kDouble:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001475 field->SetDouble(NULL, value.d);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001476 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001477 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001478 uint32_t string_idx = value.i;
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001479 const String* resolved = ResolveString(dex_file, string_idx, klass->GetDexCache());
Brian Carlstrom4873d462011-08-21 15:23:39 -07001480 field->SetObject(NULL, resolved);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001481 break;
1482 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001483 case DexFile::kBoolean:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001484 field->SetBoolean(NULL, value.z);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001485 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001486 case DexFile::kNull:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001487 field->SetObject(NULL, value.l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001488 break;
1489 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001490 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001491 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001492 }
1493}
1494
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001495bool ClassLinker::LinkClass(Class* klass) {
1496 CHECK_EQ(Class::kStatusLoaded, klass->GetStatus());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001497 if (!LinkSuperClass(klass)) {
1498 return false;
1499 }
1500 if (!LinkMethods(klass)) {
1501 return false;
1502 }
1503 if (!LinkInstanceFields(klass)) {
1504 return false;
1505 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001506 if (!LinkStaticFields(klass)) {
1507 return false;
1508 }
1509 CreateReferenceInstanceOffsets(klass);
1510 CreateReferenceStaticOffsets(klass);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001511 CHECK_EQ(Class::kStatusLoaded, klass->GetStatus());
1512 klass->SetStatus(Class::kStatusResolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001513 return true;
1514}
1515
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001516bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001517 CHECK_EQ(Class::kStatusIdx, klass->GetStatus());
1518 if (klass->GetSuperClassTypeIdx() != DexFile::kDexNoIndex) {
1519 Class* super_class = ResolveType(dex_file, klass->GetSuperClassTypeIdx(), klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001520 if (super_class == NULL) {
1521 LG << "Failed to resolve superclass";
1522 return false;
1523 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001524 klass->SetSuperClass(super_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001525 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001526 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1527 uint32_t idx = klass->GetInterfacesTypeIdx()->Get(i);
1528 Class *interface = ResolveType(dex_file, idx, klass);
1529 klass->SetInterface(i, interface);
1530 if (interface == NULL) {
1531 LG << "Failed to resolve interface";
1532 return false;
1533 }
1534 // Verify
1535 if (!klass->CanAccess(interface)) {
1536 LG << "Inaccessible interface";
1537 return false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001538 }
1539 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001540 // Mark the class as loaded.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001541 klass->SetStatus(Class::kStatusLoaded);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001542 return true;
1543}
1544
1545bool ClassLinker::LinkSuperClass(Class* klass) {
1546 CHECK(!klass->IsPrimitive());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001547 Class* super = klass->GetSuperClass();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001548 if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001549 if (super != NULL) {
1550 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1551 return false;
1552 }
1553 // TODO: clear finalize attribute
1554 return true;
1555 }
1556 if (super == NULL) {
1557 LG << "No superclass defined"; // TODO: LinkageError
1558 return false;
1559 }
1560 // Verify
1561 if (super->IsFinal()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001562 LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001563 return false;
1564 }
1565 if (super->IsInterface()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001566 LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001567 return false;
1568 }
1569 if (!klass->CanAccess(super)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001570 LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001571 return false;
1572 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001573#ifndef NDEBUG
1574 // Ensure super classes are fully resolved prior to resolving fields..
1575 while (super != NULL) {
1576 CHECK(super->IsLinked());
1577 super = super->GetSuperClass();
1578 }
1579#endif
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001580 return true;
1581}
1582
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001583// Populate the class vtable and itable. Compute return type indices.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001584bool ClassLinker::LinkMethods(Class* klass) {
1585 if (klass->IsInterface()) {
1586 // No vtable.
1587 size_t count = klass->NumVirtualMethods();
1588 if (!IsUint(16, count)) {
1589 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1590 return false;
1591 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001592 for (size_t i = 0; i < count; ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001593 klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001594 }
1595 } else {
1596 // Link virtual method tables
1597 LinkVirtualMethods(klass);
1598
1599 // Link interface method tables
1600 LinkInterfaceMethods(klass);
1601
1602 // Insert stubs.
1603 LinkAbstractMethods(klass);
1604 }
1605 return true;
1606}
1607
1608bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001609 if (klass->HasSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001610 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->GetVTable()->GetLength();
1611 size_t actual_count = klass->GetSuperClass()->GetVTable()->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001612 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001613 // TODO: do not assign to the vtable field until it is fully constructed.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001614 ObjectArray<Method>* vtable = klass->GetSuperClass()->GetVTable()->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001615 // See if any of our virtual methods override the superclass.
1616 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001617 Method* local_method = klass->GetVirtualMethodDuringLinking(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001618 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001619 for (; j < actual_count; ++j) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001620 Method* super_method = vtable->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001621 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001622 // Verify
1623 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001624 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001625 return false;
1626 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001627 vtable->Set(j, local_method);
1628 local_method->SetMethodIndex(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001629 break;
1630 }
1631 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001632 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001633 // Not overriding, append.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001634 vtable->Set(actual_count, local_method);
1635 local_method->SetMethodIndex(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001636 actual_count += 1;
1637 }
1638 }
1639 if (!IsUint(16, actual_count)) {
1640 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1641 return false;
1642 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001643 // Shrink vtable if possible
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001644 CHECK_LE(actual_count, max_count);
1645 if (actual_count < max_count) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001646 vtable = vtable->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001647 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001648 klass->SetVTable(vtable);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001649 } else {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001650 CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001651 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001652 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001653 LG << "Too many methods"; // TODO: VirtualMachineError
1654 return false;
1655 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001656 ObjectArray<Method>* vtable = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001657 for (size_t i = 0; i < num_virtual_methods; ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001658 Method* virtual_method = klass->GetVirtualMethodDuringLinking(i);
1659 vtable->Set(i, virtual_method);
1660 virtual_method->SetMethodIndex(i & 0xFFFF);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001661 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001662 klass->SetVTable(vtable);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001663 }
1664 return true;
1665}
1666
1667bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1668 int pool_offset = 0;
1669 int pool_size = 0;
1670 int miranda_count = 0;
1671 int miranda_alloc = 0;
1672 size_t super_ifcount;
1673 if (klass->HasSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001674 super_ifcount = klass->GetSuperClass()->GetIFTableCount();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001675 } else {
1676 super_ifcount = 0;
1677 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001678 size_t ifcount = super_ifcount;
1679 ifcount += klass->NumInterfaces();
1680 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001681 ifcount += klass->GetInterface(i)->GetIFTableCount();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001682 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001683 if (ifcount == 0) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001684 // TODO: enable these asserts with klass status validation
1685 // DCHECK(klass->GetIFTableCount() == 0);
1686 // DCHECK(klass->GetIFTable() == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001687 return true;
1688 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001689 InterfaceEntry* iftable = new InterfaceEntry[ifcount];
1690 memset(iftable, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001691 if (super_ifcount != 0) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001692 memcpy(iftable, klass->GetSuperClass()->GetIFTable(),
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001693 sizeof(InterfaceEntry) * super_ifcount);
1694 }
1695 // Flatten the interface inheritance hierarchy.
1696 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001697 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1698 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001699 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001700 if (!interf->IsInterface()) {
1701 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1702 return false;
1703 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001704 iftable[idx++].SetInterface(interf);
1705 for (size_t j = 0; j < interf->GetIFTableCount(); j++) {
1706 iftable[idx++].SetInterface(interf->GetIFTable()[j].GetInterface());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001707 }
1708 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001709 klass->SetIFTable(iftable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001710 CHECK_EQ(idx, ifcount);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001711 klass->SetIFTableCount(ifcount);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001712 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001713 return true;
1714 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001715 for (size_t i = super_ifcount; i < ifcount; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001716 pool_size += iftable[i].GetInterface()->NumVirtualMethods();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001717 }
1718 if (pool_size == 0) {
1719 return true;
1720 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001721 klass->SetIfviPoolCount(pool_size);
1722 uint32_t* ifvi_pool = new uint32_t[pool_size];
1723 klass->SetIfviPool(ifvi_pool);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001724 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001725 for (size_t i = super_ifcount; i < ifcount; ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001726 iftable[i].SetMethodIndexArray(ifvi_pool + pool_offset);
1727 Class* interface = iftable[i].GetInterface();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001728 pool_offset += interface->NumVirtualMethods(); // end here
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001729 ObjectArray<Method>* vtable = klass->GetVTableDuringLinking();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001730 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1731 Method* interface_method = interface->GetVirtualMethod(j);
1732 int k; // must be signed
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001733 for (k = vtable->GetLength() - 1; k >= 0; --k) {
1734 Method* vtable_method = vtable->Get(k);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001735 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1736 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001737 LG << "Implementation not public";
1738 return false;
1739 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001740 iftable[i].GetMethodIndexArray()[j] = k;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001741 break;
1742 }
1743 }
1744 if (k < 0) {
1745 if (miranda_count == miranda_alloc) {
1746 miranda_alloc += 8;
1747 if (miranda_list.empty()) {
1748 miranda_list.resize(miranda_alloc);
1749 } else {
1750 miranda_list.resize(miranda_alloc);
1751 }
1752 }
1753 int mir;
1754 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001755 Method* miranda_method = miranda_list[mir];
1756 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001757 break;
1758 }
1759 }
1760 // point the interface table at a phantom slot index
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001761 iftable[i].GetMethodIndexArray()[j] =
1762 vtable->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001763 if (mir == miranda_count) {
1764 miranda_list[miranda_count++] = interface_method;
1765 }
1766 }
1767 }
1768 }
1769 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001770 int old_method_count = klass->NumVirtualMethods();
1771 int new_method_count = old_method_count + miranda_count;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001772 klass->SetVirtualMethods(
1773 klass->GetVirtualMethods()->CopyOf(new_method_count));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001774
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001775 ObjectArray<Method>* vtable = klass->GetVTableDuringLinking();
1776 CHECK(vtable != NULL);
1777 int old_vtable_count = vtable->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001778 int new_vtable_count = old_vtable_count + miranda_count;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001779 vtable = vtable->CopyOf(new_vtable_count);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001780 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001781 Method* meth = AllocMethod();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001782 // TODO: this shouldn't be a memcpy
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001783 memcpy(meth, miranda_list[i], sizeof(Method));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001784 meth->SetDeclaringClass(klass);
1785 meth->SetAccessFlags(meth->GetAccessFlags() | kAccMiranda);
1786 meth->SetMethodIndex(0xFFFF & (old_vtable_count + i));
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001787 klass->SetVirtualMethod(old_method_count + i, meth);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001788 vtable->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001789 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001790 // TODO: do not assign to the vtable field until it is fully constructed.
1791 klass->SetVTable(vtable);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001792 }
1793 return true;
1794}
1795
1796void ClassLinker::LinkAbstractMethods(Class* klass) {
1797 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001798 Method* method = klass->GetVirtualMethodDuringLinking(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001799 if (method->IsAbstract()) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001800 LG << "AbstractMethodError";
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001801 // TODO: throw AbstractMethodError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001802 }
1803 }
1804}
1805
1806bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001807 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001808 return LinkFields(klass, true);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001809}
1810
1811bool ClassLinker::LinkStaticFields(Class* klass) {
1812 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001813 size_t allocated_class_size = klass->GetClassSize();
1814 bool success = LinkFields(klass, false);
1815 CHECK_EQ(allocated_class_size, klass->GetClassSize());
Brian Carlstrom4873d462011-08-21 15:23:39 -07001816 return success;
1817}
1818
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001819bool ClassLinker::LinkFields(Class *klass, bool instance) {
1820 size_t num_fields =
1821 instance ? klass->NumInstanceFields() : klass->NumStaticFields();
1822
1823 ObjectArray<Field>* fields =
1824 instance ? klass->GetIFields() : klass->GetSFields();
1825 // Fields updated at end of LinkFields
1826 size_t num_reference_fields;
1827 size_t size;
1828
1829 // Initialize size and field_offset
1830 MemberOffset field_offset = Class::FieldsOffset();
1831 if (instance) {
1832 Class* super_class = klass->GetSuperClass();
1833 if (super_class != NULL) {
1834 CHECK(super_class->IsLinked());
1835 field_offset = MemberOffset(super_class->GetObjectSize());
1836 if (field_offset.Uint32Value() == 0u) {
1837 field_offset = OFFSET_OF_OBJECT_MEMBER(DataObject, fields_);
1838 }
1839 } else {
1840 field_offset = OFFSET_OF_OBJECT_MEMBER(DataObject, fields_);
1841 }
1842 size = field_offset.Uint32Value();
1843 } else {
1844 size = klass->GetClassSize();
1845 }
1846 DCHECK_LE(CLASS_SMALLEST_OFFSET, size);
1847
Brian Carlstrom4873d462011-08-21 15:23:39 -07001848 CHECK((num_fields == 0) == (fields == NULL));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001849
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001850 // Move references to the front.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001851 size_t i = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001852 num_reference_fields = 0;
1853 for (; i < num_fields; i++) {
1854 Field* field = fields->Get(i);
1855 const Class* field_type = field->GetTypeDuringLinking();
1856 // if a field's type at this point is NULL it isn't primitive
1857 if (field_type != NULL && field_type->IsPrimitive()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001858 for (size_t j = num_fields - 1; j > i; j--) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001859 Field* ref_field = fields->Get(j);
1860 const Class* ref_field_type = ref_field->GetTypeDuringLinking();
1861 if (ref_field_type == NULL || !ref_field_type->IsPrimitive()) {
1862 fields->Set(i, ref_field);
1863 fields->Set(j, field);
1864 field = ref_field;
1865 field_type = ref_field_type;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001866 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001867 break;
1868 }
1869 }
1870 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001871 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001872 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001873 if (field_type != NULL && field_type->IsPrimitive()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001874 break;
1875 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001876 field->SetOffset(field_offset);
1877 field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001878 }
1879
1880 // Now we want to pack all of the double-wide fields together. If
1881 // we're not aligned, though, we want to shuffle one 32-bit field
1882 // into place. If we can't find one, we'll have to pad it.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001883 if (i != num_fields && !IsAligned(field_offset.Uint32Value(), 8)) {
1884 Field* field = fields->Get(i);
1885 const Class* c = field->GetTypeDuringLinking();
1886 CHECK(c != NULL); // should only be working on primitive types
1887 if (!c->IsPrimitiveLong() && !c->IsPrimitiveDouble()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001888 // The field that comes next is 32-bit, so just advance past it.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001889 DCHECK(c->IsPrimitive());
1890 field->SetOffset(field_offset);
1891 field_offset = MemberOffset(field_offset.Uint32Value() +
1892 sizeof(uint32_t));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001893 i++;
1894 } else {
1895 // Next field is 64-bit, so search for a 32-bit field we can
1896 // swap into it.
1897 bool found = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001898 for (size_t j = num_fields - 1; j > i; j--) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001899 Field* single_field = fields->Get(j);
1900 const Class* rc = single_field->GetTypeDuringLinking();
1901 CHECK(rc != NULL); // should only be working on primitive types
1902 if (!rc->IsPrimitiveLong() && !rc->IsPrimitiveDouble()) {
1903 fields->Set(i, single_field);
1904 fields->Set(j, field);
1905 field = single_field;
1906 field->SetOffset(field_offset);
1907 field_offset = MemberOffset(field_offset.Uint32Value() +
1908 sizeof(uint32_t));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001909 found = true;
1910 i++;
1911 break;
1912 }
1913 }
1914 if (!found) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001915 field_offset = MemberOffset(field_offset.Uint32Value() +
1916 sizeof(uint32_t));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001917 }
1918 }
1919 }
1920
1921 // Alignment is good, shuffle any double-wide fields forward, and
1922 // finish assigning field offsets to all fields.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001923 DCHECK(i == num_fields || IsAligned(field_offset.Uint32Value(), 4));
Brian Carlstrom4873d462011-08-21 15:23:39 -07001924 for ( ; i < num_fields; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001925 Field* field = fields->Get(i);
1926 const Class* c = field->GetTypeDuringLinking();
1927 CHECK(c != NULL); // should only be working on primitive types
1928 if (!c->IsPrimitiveDouble() && !c->IsPrimitiveLong()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001929 for (size_t j = num_fields - 1; j > i; j--) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001930 Field* double_field = fields->Get(j);
1931 const Class* rc = double_field->GetTypeDuringLinking();
1932 CHECK(rc != NULL); // should only be working on primitive types
1933 if (rc->IsPrimitiveDouble() || rc->IsPrimitiveLong()) {
1934 fields->Set(i, double_field);
1935 fields->Set(j, field);
1936 field = double_field;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001937 c = rc;
1938 break;
1939 }
1940 }
1941 } else {
1942 // This is a double-wide field, leave it be.
1943 }
1944
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001945 field->SetOffset(field_offset);
1946 if (c->IsPrimitiveLong() || c->IsPrimitiveDouble()) {
1947 field_offset = MemberOffset(field_offset.Uint32Value() +
1948 sizeof(uint64_t));
1949 } else {
1950 field_offset = MemberOffset(field_offset.Uint32Value() +
1951 sizeof(uint32_t));
Brian Carlstrom4873d462011-08-21 15:23:39 -07001952 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001953 }
1954
1955#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001956 // Make sure that all reference fields appear before
1957 // non-reference fields, and all double-wide fields are aligned.
1958 bool seen_non_ref = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001959 for (i = 0; i < num_fields; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001960 Field* field = fields->Get(i);
1961 const Class* c = field->GetTypeDuringLinking();
1962 if (c != NULL && c->IsPrimitive()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001963 if (!seen_non_ref) {
1964 seen_non_ref = true;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001965 DCHECK_EQ(num_reference_fields, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001966 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001967 } else {
1968 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001969 }
1970 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001971 if (!seen_non_ref) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001972 DCHECK_EQ(num_fields, num_reference_fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001973 }
1974#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001975 size = field_offset.Uint32Value();
1976 DCHECK_LE(CLASS_SMALLEST_OFFSET, size);
1977 // Update klass
1978 if(instance) {
1979 klass->SetNumReferenceInstanceFields(num_reference_fields);
1980 if(!klass->IsVariableSize()) {
1981 klass->SetObjectSize(size);
1982 }
1983 } else {
1984 klass->SetNumReferenceStaticFields(num_reference_fields);
1985 klass->SetClassSize(size);
1986 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001987 return true;
1988}
1989
1990// Set the bitmap of reference offsets, refOffsets, from the ifields
1991// list.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001992void ClassLinker::CreateReferenceInstanceOffsets(Class* klass) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001993 uint32_t reference_offsets = 0;
1994 Class* super_class = klass->GetSuperClass();
1995 if (super_class != NULL) {
1996 reference_offsets = super_class->GetReferenceInstanceOffsets();
Brian Carlstrom4873d462011-08-21 15:23:39 -07001997 // If our superclass overflowed, we don't stand a chance.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001998 if (reference_offsets == CLASS_WALK_SUPER) {
1999 klass->SetReferenceInstanceOffsets(reference_offsets);
Brian Carlstrom4873d462011-08-21 15:23:39 -07002000 return;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002001 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002002 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002003 CreateReferenceOffsets(klass, true, reference_offsets);
Brian Carlstrom4873d462011-08-21 15:23:39 -07002004}
2005
2006void ClassLinker::CreateReferenceStaticOffsets(Class* klass) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002007 CreateReferenceOffsets(klass, false, 0);
Brian Carlstrom4873d462011-08-21 15:23:39 -07002008}
2009
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002010void ClassLinker::CreateReferenceOffsets(Class* klass, bool instance,
2011 uint32_t reference_offsets) {
2012 size_t num_reference_fields =
2013 instance ? klass->NumReferenceInstanceFieldsDuringLinking()
2014 : klass->NumReferenceStaticFieldsDuringLinking();
2015 const ObjectArray<Field>* fields =
2016 instance ? klass->GetIFields() : klass->GetSFields();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002017 // All of the fields that contain object references are guaranteed
2018 // to be at the beginning of the fields list.
2019 for (size_t i = 0; i < num_reference_fields; ++i) {
2020 // Note that byte_offset is the offset from the beginning of
2021 // object, not the offset into instance data
2022 const Field* field = fields->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002023 MemberOffset byte_offset = field->GetOffsetDuringLinking();
2024 CHECK_GE(byte_offset.Uint32Value(), CLASS_SMALLEST_OFFSET);
2025 CHECK_EQ(byte_offset.Uint32Value() & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
2026 if (CLASS_CAN_ENCODE_OFFSET(byte_offset.Uint32Value())) {
2027 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset.Uint32Value());
Brian Carlstrom4873d462011-08-21 15:23:39 -07002028 CHECK_NE(new_bit, 0U);
2029 reference_offsets |= new_bit;
2030 } else {
2031 reference_offsets = CLASS_WALK_SUPER;
2032 break;
2033 }
2034 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002035 // Update fields in klass
2036 if (instance) {
2037 klass->SetReferenceInstanceOffsets(reference_offsets);
2038 } else {
2039 klass->SetReferenceStaticOffsets(reference_offsets);
2040 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002041}
2042
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002043String* ClassLinker::ResolveString(const DexFile& dex_file,
Elliott Hughescf4c6c42011-09-01 15:16:42 -07002044 uint32_t string_idx, DexCache* dex_cache) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002045 String* resolved = dex_cache->GetResolvedString(string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002046 if (resolved != NULL) {
2047 return resolved;
2048 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002049 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
2050 int32_t utf16_length = dex_file.GetStringLength(string_id);
2051 const char* utf8_data = dex_file.GetStringData(string_id);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002052 // TODO: remote the const_cast below
2053 String* string = const_cast<String*>(intern_table_->InternStrong(utf16_length, utf8_data));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002054 dex_cache->SetResolvedString(string_idx, string);
2055 return string;
2056}
2057
2058Class* ClassLinker::ResolveType(const DexFile& dex_file,
2059 uint32_t type_idx,
2060 DexCache* dex_cache,
2061 const ClassLoader* class_loader) {
2062 Class* resolved = dex_cache->GetResolvedType(type_idx);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002063 if (resolved == NULL) {
2064 const char* descriptor = dex_file.dexStringByTypeIdx(type_idx);
2065 if (descriptor[1] == '\0') {
2066 // only the descriptors of primitive types should be 1 character long
2067 resolved = FindPrimitiveClass(descriptor[0]);
2068 } else {
2069 resolved = FindClass(descriptor, class_loader);
2070 }
2071 if (resolved != NULL) {
2072 Class* check = resolved->IsArrayClass() ? resolved->GetComponentType() : resolved;
2073 if (dex_cache != check->GetDexCache()) {
2074 if (check->GetClassLoader() != NULL) {
2075 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
2076 resolved = NULL;
2077 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002078 }
2079 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002080 if (resolved != NULL) {
2081 dex_cache->SetResolvedType(type_idx, resolved);
2082 } else {
2083 DCHECK(Thread::Current()->IsExceptionPending());
2084 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002085 }
2086 return resolved;
2087}
2088
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002089Method* ClassLinker::ResolveMethod(const DexFile& dex_file,
2090 uint32_t method_idx,
2091 DexCache* dex_cache,
2092 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002093 bool is_direct) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002094 Method* resolved = dex_cache->GetResolvedMethod(method_idx);
2095 if (resolved != NULL) {
2096 return resolved;
2097 }
2098 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
2099 Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader);
2100 if (klass == NULL) {
2101 return NULL;
2102 }
2103
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002104 const char* name = dex_file.dexStringById(method_id.name_idx_);
Elliott Hughes0c424cb2011-08-26 10:16:25 -07002105 std::string signature(dex_file.CreateMethodDescriptor(method_id.proto_idx_, NULL));
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002106 if (is_direct) {
2107 resolved = klass->FindDirectMethod(name, signature);
2108 } else {
2109 resolved = klass->FindVirtualMethod(name, signature);
2110 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002111 if (resolved != NULL) {
2112 dex_cache->SetResolvedMethod(method_idx, resolved);
2113 } else {
2114 // DCHECK(Thread::Current()->IsExceptionPending());
2115 }
2116 return resolved;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002117}
2118
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002119Field* ClassLinker::ResolveField(const DexFile& dex_file,
2120 uint32_t field_idx,
2121 DexCache* dex_cache,
2122 const ClassLoader* class_loader,
2123 bool is_static) {
2124 Field* resolved = dex_cache->GetResolvedField(field_idx);
2125 if (resolved != NULL) {
2126 return resolved;
2127 }
2128 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx);
2129 Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader);
2130 if (klass == NULL) {
2131 return NULL;
2132 }
2133
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002134 const char* name = dex_file.dexStringById(field_id.name_idx_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002135 Class* field_type = ResolveType(dex_file, field_id.type_idx_, dex_cache, class_loader);
2136 // TODO: LinkageError?
2137 CHECK(field_type != NULL);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002138 if (is_static) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002139 resolved = klass->FindStaticField(name, field_type);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002140 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002141 resolved = klass->FindInstanceField(name, field_type);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07002142 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002143 if (resolved != NULL) {
2144 dex_cache->SetResolvedfield(field_idx, resolved);
2145 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002146 // TODO: DCHECK(Thread::Current()->IsExceptionPending());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002147 }
2148 return resolved;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002149}
2150
Elliott Hughese27955c2011-08-26 15:21:24 -07002151size_t ClassLinker::NumLoadedClasses() const {
2152 MutexLock mu(classes_lock_);
2153 return classes_.size();
2154}
2155
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002156} // namespace art