blob: 3907d07ca638d78ea153d0d30fadb6519a6affaf [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "class_linker.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004
5#include <vector>
6#include <utility>
7
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "casts.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07009#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "dex_verifier.h"
11#include "heap.h"
12#include "logging.h"
13#include "monitor.h"
14#include "object.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070015#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "scoped_ptr.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070017#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "thread.h"
19#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
22
Brian Carlstroma663ea52011-08-19 23:33:41 -070023const char* ClassLinker::class_roots_descriptors_[kClassRootsMax] = {
24 "Ljava/lang/Class;",
25 "Ljava/lang/Object;",
26 "[Ljava/lang/Object;",
27 "Ljava/lang/String;",
28 "Ljava/lang/reflect/Field;",
29 "Ljava/lang/reflect/Method;",
30 "Ljava/lang/ClassLoader;",
31 "Ldalvik/system/BaseDexClassLoader;",
32 "Ldalvik/system/PathClassLoader;",
33 "Z",
34 "B",
35 "C",
36 "D",
37 "F",
38 "I",
39 "J",
40 "S",
41 "V",
42 "[Z",
43 "[B",
44 "[C",
45 "[D",
46 "[F",
47 "[I",
48 "[J",
49 "[S",
50};
51
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070052ClassLinker* ClassLinker::Create(const std::vector<const DexFile*>& boot_class_path, Space* space) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070053 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstroma663ea52011-08-19 23:33:41 -070054 if (space == NULL) {
55 class_linker->Init(boot_class_path);
56 } else {
57 class_linker->Init(boot_class_path, space);
58 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070059 // TODO: check for failure during initialization
60 return class_linker.release();
61}
62
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070063ClassLinker::ClassLinker()
64 : classes_lock_(Mutex::Create("ClassLinker::Lock")),
65 class_roots_(NULL),
66 init_done_(false) {
67}
Brian Carlstroma663ea52011-08-19 23:33:41 -070068
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070069void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070070 CHECK(!init_done_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070071
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070072 // java_lang_Class comes first, its needed for AllocClass
Brian Carlstrom4873d462011-08-21 15:23:39 -070073 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(ClassClass)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070074 CHECK(java_lang_Class != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -070075 java_lang_Class->class_size_ = sizeof(ClassClass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070076 java_lang_Class->klass_ = java_lang_Class;
77 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070078
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070079 // java_lang_Object comes next so that object_array_class can be created
Brian Carlstrom4873d462011-08-21 15:23:39 -070080 Class* java_lang_Object = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070081 CHECK(java_lang_Object != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070082 // backfill Object as the super class of Class
83 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070084 // mark as non-primitive for object_array_class
85 java_lang_Object->primitive_type_ = Class::kPrimNot;
Brian Carlstroma0808032011-07-18 00:39:23 -070086
Elliott Hughesc1674ed2011-08-25 18:09:09 -070087 // Object[] is for DexCache and int[] is for various Class members.
Brian Carlstrom4873d462011-08-21 15:23:39 -070088 Class* object_array_class = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070089 CHECK(object_array_class != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070090 object_array_class->component_type_ = java_lang_Object;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070091 Class* int_array_class = AllocClass(java_lang_Class, sizeof(Class));
92 CHECK(int_array_class != NULL);
93 IntArray::SetArrayClass(int_array_class);
Brian Carlstroma0808032011-07-18 00:39:23 -070094
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070095 // String and char[] are necessary so that FindClass can assign names to members
Brian Carlstrom4873d462011-08-21 15:23:39 -070096 Class* java_lang_String = AllocClass(java_lang_Class, sizeof(StringClass));
Jesse Wilson14150742011-07-29 19:04:44 -040097 CHECK(java_lang_String != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070098 CHECK_LT(java_lang_String->object_size_, sizeof(String));
Jesse Wilson14150742011-07-29 19:04:44 -040099 java_lang_String->object_size_ = sizeof(String);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700100 String::SetClass(java_lang_String);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700101 Class* char_array_class = AllocClass(java_lang_Class, sizeof(Class));
Jesse Wilson8989d992011-08-02 13:39:42 -0700102 CHECK(char_array_class != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700103 CharArray::SetArrayClass(char_array_class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700104 // Now String::Alloc* can be used
105
106 // backfill Class descriptors missing until this point
107 java_lang_Class->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Class;");
108 java_lang_Object->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Object;");
109 object_array_class->descriptor_ = String::AllocFromModifiedUtf8("[Ljava/lang/Object;");
110 java_lang_String->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/String;");
111 char_array_class->descriptor_ = String::AllocFromModifiedUtf8("[C");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700112 int_array_class->descriptor_ = String::AllocFromModifiedUtf8("[I");
Jesse Wilson14150742011-07-29 19:04:44 -0400113
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700114 // Field and Method are necessary so that FindClass can link members
Brian Carlstrom4873d462011-08-21 15:23:39 -0700115 Class* java_lang_reflect_Field = AllocClass(java_lang_Class, sizeof(FieldClass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700116 CHECK(java_lang_reflect_Field != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700117 java_lang_reflect_Field->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;");
Jesse Wilson35baaab2011-08-10 16:18:03 -0400118 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
119 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700120 Class* java_lang_reflect_Method = AllocClass(java_lang_Class, sizeof(MethodClass));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700121 java_lang_reflect_Method->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700122 CHECK(java_lang_reflect_Method != NULL);
123 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
124 java_lang_reflect_Method->object_size_ = sizeof(Method);
125
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700126 // create storage for root classes, save away our work so far
127 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700128 SetClassRoot(kJavaLangClass, java_lang_Class);
129 SetClassRoot(kJavaLangObject, java_lang_Object);
130 SetClassRoot(kObjectArrayClass, object_array_class);
131 SetClassRoot(kJavaLangString, java_lang_String);
132 SetClassRoot(kCharArrayClass, char_array_class);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700133 SetClassRoot(kIntArrayClass, int_array_class);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700134 SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field);
135 SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700136 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -0700137
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700138 // setup boot_class_path_ now that we can use AllocObjectArray to
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700139 // create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700140 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700141 const DexFile* dex_file = boot_class_path[i];
142 CHECK(dex_file != NULL);
143 AppendToBootClassPath(*dex_file);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700144 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700145 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700146
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700147 // run Class, Field, and Method through FindSystemClass.
148 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700149 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700150 Class* Class_class = FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700151 CHECK_EQ(java_lang_Class, Class_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700152 CHECK_LT(java_lang_Class->object_size_, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700153 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700154 Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700155 CHECK_EQ(java_lang_reflect_Field, Field_class);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400156 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
157 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700158 Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700159 CHECK_EQ(java_lang_reflect_Method, Method_class);
160 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700161 java_lang_reflect_Method->object_size_ = sizeof(Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700162
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700163 // Object and String just need more minimal setup, since they do not have extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700164 Class* Object_class = FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700165 CHECK_EQ(java_lang_Object, Object_class);
166 CHECK_EQ(java_lang_Object->object_size_, sizeof(Object));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700167 Class* String_class = FindSystemClass("Ljava/lang/String;");
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700168 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700169 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700170
171 // Setup the ClassLoaders, adjusting the object_size_ as necessary
172 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
173 CHECK(java_lang_ClassLoader != NULL);
174 CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader));
175 java_lang_ClassLoader->object_size_ = sizeof(ClassLoader);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700176 SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700177 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
178 CHECK(dalvik_system_BaseDexClassLoader != NULL);
179 CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700180 SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700181 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
182 CHECK(dalvik_system_PathClassLoader != NULL);
183 CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700184 SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700185 PathClassLoader::SetClass(dalvik_system_PathClassLoader);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700186
187 // Setup a single, global copy of "interfaces" and "iftable" for
188 // reuse across array classes
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700189 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700190 CHECK(java_lang_Cloneable != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700191 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700192 CHECK(java_io_Serializable != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700193
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700194 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700195 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700196 array_interfaces_->Set(0, java_lang_Cloneable);
197 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700198
199 // We assume that Cloneable/Serializable don't have superinterfaces --
200 // normally we'd have to crawl up and explicitly list all of the
201 // supers as well. These interfaces don't have any methods, so we
202 // don't have to worry about the ifviPool either.
203 array_iftable_ = new InterfaceEntry[2];
Brian Carlstrom30b94452011-08-25 21:35:26 -0700204 array_iftable_[0].SetInterface(array_interfaces_->Get(0));
205 array_iftable_[1].SetInterface(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700206 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700207
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700208 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700209 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
210 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700211 CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0));
212 CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700213
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700214 // Setup the primitive type classes.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700215 SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
216 SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B"));
217 SetClassRoot(kPrimitiveChar, CreatePrimitiveClass("C"));
218 SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D"));
219 SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F"));
220 SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I"));
221 SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J"));
222 SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S"));
223 SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V"));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700224 // now we can use FindSystemClass for anything, including for "[C"
225
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700226 // run char[] and int[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700227 Class* found_char_array_class = FindSystemClass("[C");
228 CHECK_EQ(char_array_class, found_char_array_class);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700229 Class* found_int_array_class = FindSystemClass("[I");
230 CHECK_EQ(int_array_class, found_int_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700231
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700232 // Initialize all the other primitive array types for PrimitiveArray::Alloc.
233 // These are easy because everything we need has already been set up.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700234 SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z"));
235 SetClassRoot(kByteArrayClass, FindSystemClass("[B"));
236 SetClassRoot(kDoubleArrayClass, FindSystemClass("[D"));
237 SetClassRoot(kFloatArrayClass, FindSystemClass("[F"));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700238 SetClassRoot(kLongArrayClass, FindSystemClass("[J"));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700239 SetClassRoot(kShortArrayClass, FindSystemClass("[S"));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700240 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
241 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
242 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
243 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700244 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700245 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
246
Brian Carlstroma663ea52011-08-19 23:33:41 -0700247 FinishInit();
248}
249
250void ClassLinker::FinishInit() {
251 // ensure all class_roots_ are initialized
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700252 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700253 ClassRoot class_root = static_cast<ClassRoot>(i);
254 Class* klass = GetClassRoot(class_root);
255 CHECK(klass != NULL);
256 DCHECK(klass->IsArray() || klass->IsPrimitive() || klass->dex_cache_ != NULL);
257 // note SetClassRoot does additional validation.
258 // if possible add new checks there to catch errors early
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700259 }
260
261 // disable the slow paths in FindClass and CreatePrimitiveClass now
262 // that Object, Class, and Object[] are setup
263 init_done_ = true;
264}
265
Brian Carlstroma663ea52011-08-19 23:33:41 -0700266struct ClassLinker::InitCallbackState {
267 ClassLinker* class_linker;
268
269 Class* class_roots[kClassRootsMax];
270
271 typedef std::tr1::unordered_map<std::string, ClassRoot> Table;
272 Table descriptor_to_class_root;
273
274 struct DexCacheHash {
275 size_t operator()(art::DexCache* const& obj) const {
276 return reinterpret_cast<size_t>(&obj);
277 }
278 };
279 typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set;
280 Set dex_caches;
281};
282
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700283void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path, Space* space) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700284 CHECK(!init_done_);
285
286 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
287 DCHECK(heap_bitmap != NULL);
288
289 InitCallbackState state;
290 state.class_linker = this;
291 for (size_t i = 0; i < kClassRootsMax; i++) {
292 ClassRoot class_root = static_cast<ClassRoot>(i);
293 state.descriptor_to_class_root[GetClassRootDescriptor(class_root)] = class_root;
294 }
295
296 // reinit clases_ table
297 heap_bitmap->Walk(InitCallback, &state);
298
299 // reinit class_roots_
300 Class* object_array_class = state.class_roots[kObjectArrayClass];
301 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
302 for (size_t i = 0; i < kClassRootsMax; i++) {
303 ClassRoot class_root = static_cast<ClassRoot>(i);
304 SetClassRoot(class_root, state.class_roots[class_root]);
305 }
306
307 // reinit intern_table_
308 ObjectArray<Object>* interned_array = space->GetImageHeader().GetInternedArray();
309 for (int32_t i = 0; i < interned_array->GetLength(); i++) {
310 String* string = interned_array->Get(i)->AsString();
311 intern_table_.Register(string);
312 }
313
314 // reinit array_interfaces_ from any array class instance, they should all be ==
315 array_interfaces_ = GetClassRoot(kObjectArrayClass)->interfaces_;
316 DCHECK(array_interfaces_ == GetClassRoot(kBooleanArrayClass)->interfaces_);
317
318 // build a map from location to DexCache to match up with DexFile::GetLocation
319 std::tr1::unordered_map<std::string, DexCache*> location_to_dex_cache;
320 typedef InitCallbackState::Set::const_iterator It; // TODO: C++0x auto
321 for (It it = state.dex_caches.begin(), end = state.dex_caches.end(); it != end; ++it) {
322 DexCache* dex_cache = *it;
323 std::string location = dex_cache->GetLocation()->ToModifiedUtf8();
324 location_to_dex_cache[location] = dex_cache;
325 }
326
327 // reinit boot_class_path with DexFile arguments and found DexCaches
328 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700329 const DexFile* dex_file = boot_class_path[i];
330 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700331 DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700332 AppendToBootClassPath(*dex_file, dex_cache);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700333 }
334
335 String::SetClass(GetClassRoot(kJavaLangString));
336 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
337 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
338 CharArray::SetArrayClass(GetClassRoot(kCharArrayClass));
339 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
340 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
341 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
342 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
343 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700344 PathClassLoader::SetClass(GetClassRoot(kDalvikSystemPathClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700345
346 FinishInit();
347}
348
Brian Carlstrom4873d462011-08-21 15:23:39 -0700349void ClassLinker::InitCallback(Object* obj, void *arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700350 DCHECK(obj != NULL);
351 DCHECK(arg != NULL);
352 InitCallbackState* state = reinterpret_cast<InitCallbackState*>(arg);
353
354 if (!obj->IsClass()) {
355 return;
356 }
357 Class* klass = obj->AsClass();
358 CHECK(klass->class_loader_ == NULL);
359
360 std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8();
361
362 // restore class to ClassLinker::classes_ table
363 state->class_linker->InsertClass(descriptor, klass);
364
365 // note DexCache to match with DexFile later
366 DexCache* dex_cache = klass->GetDexCache();
367 if (dex_cache != NULL) {
368 state->dex_caches.insert(dex_cache);
369 } else {
370 DCHECK(klass->IsArray() || klass->IsPrimitive());
371 }
372
373 // check if this is a root, if so, register it
374 typedef InitCallbackState::Table::const_iterator It; // TODO: C++0x auto
375 It it = state->descriptor_to_class_root.find(descriptor);
376 if (it != state->descriptor_to_class_root.end()) {
377 ClassRoot class_root = it->second;
378 state->class_roots[class_root] = klass;
379 }
380}
381
382// Keep in sync with InitCallback. Anything we visit, we need to
383// reinit references to when reinitializing a ClassLinker from a
384// mapped image.
385void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) const {
386
Brian Carlstromb88e9442011-07-28 15:15:51 -0700387 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700388
389 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700390 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700391 }
392
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700393 {
394 MutexLock mu(classes_lock_);
395 typedef Table::const_iterator It; // TODO: C++0x auto
396 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
397 root_visitor(it->second, arg);
398 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700399 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700400
401 intern_table_.VisitRoots(root_visitor, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700402
Brian Carlstromb88e9442011-07-28 15:15:51 -0700403 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700404}
405
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700406ClassLinker::~ClassLinker() {
407 delete classes_lock_;
408 String::ResetClass();
409 BooleanArray::ResetArrayClass();
410 ByteArray::ResetArrayClass();
411 CharArray::ResetArrayClass();
412 DoubleArray::ResetArrayClass();
413 FloatArray::ResetArrayClass();
414 IntArray::ResetArrayClass();
415 LongArray::ResetArrayClass();
416 ShortArray::ResetArrayClass();
417 PathClassLoader::ResetClass();
418}
419
420DexCache* ClassLinker::AllocDexCache(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700421 DexCache* dex_cache = down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700422 dex_cache->Init(String::AllocFromModifiedUtf8(dex_file.GetLocation().c_str()),
423 AllocObjectArray<String>(dex_file.NumStringIds()),
424 AllocObjectArray<Class>(dex_file.NumTypeIds()),
425 AllocObjectArray<Method>(dex_file.NumMethodIds()),
426 AllocObjectArray<Field>(dex_file.NumFieldIds()));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700427 return dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700428}
429
Brian Carlstrom4873d462011-08-21 15:23:39 -0700430Class* ClassLinker::AllocClass(Class* java_lang_Class, size_t class_size) {
431 DCHECK_GE(class_size, sizeof(Class));
432 Class* klass = Heap::AllocObject(java_lang_Class, class_size)->AsClass();
433 klass->class_size_ = class_size;
434 return klass;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700435}
436
Brian Carlstrom4873d462011-08-21 15:23:39 -0700437Class* ClassLinker::AllocClass(size_t class_size) {
438 return AllocClass(GetClassRoot(kJavaLangClass), class_size);
Brian Carlstroma0808032011-07-18 00:39:23 -0700439}
440
Jesse Wilson35baaab2011-08-10 16:18:03 -0400441Field* ClassLinker::AllocField() {
442 return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->NewInstance());
Brian Carlstroma0808032011-07-18 00:39:23 -0700443}
444
445Method* ClassLinker::AllocMethod() {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700446 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700447}
448
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700449Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700450 const ClassLoader* class_loader) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700451 // TODO: remove this contrived parent class loader check when we have a real ClassLoader.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700452 if (class_loader != NULL) {
453 Class* klass = FindClass(descriptor, NULL);
454 if (klass != NULL) {
455 return klass;
456 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700457 Thread::Current()->ClearException();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700458 }
459
Carl Shapirob5573532011-07-12 18:22:59 -0700460 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700461 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700462 CHECK(!self->IsExceptionPending());
463 // Find the class in the loaded classes table.
464 Class* klass = LookupClass(descriptor, class_loader);
465 if (klass == NULL) {
466 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700467 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700468 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700469 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700470 const DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700471 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700472 if (pair.second == NULL) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700473 std::string name(PrintableString(descriptor));
474 self->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
475 "Class %s not found in class loader %p", name.c_str(), class_loader);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700476 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700477 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700478 const DexFile& dex_file = *pair.first;
479 const DexFile::ClassDef& dex_class_def = *pair.second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700480 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700481 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700482 if (!init_done_) {
483 // finish up init of hand crafted class_roots_
484 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700485 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700486 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700487 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400488 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700489 klass = GetClassRoot(kJavaLangString);
490 } else if (descriptor == "Ljava/lang/reflect/Field;") {
491 klass = GetClassRoot(kJavaLangReflectField);
492 } else if (descriptor == "Ljava/lang/reflect/Method;") {
493 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700494 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700495 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700496 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700497 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700498 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Carl Shapiro565f5072011-07-10 13:39:43 -0700499 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700500 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700501 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700502 // Check for a pending exception during load
503 if (self->IsExceptionPending()) {
504 // TODO: free native allocations in klass
505 return NULL;
506 }
507 {
508 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700509 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700510 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700511 bool success = InsertClass(descriptor, klass); // TODO: just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700512 if (!success) {
513 // We may fail to insert if we raced with another thread.
514 klass->clinit_thread_id_ = 0;
515 // TODO: free native allocations in klass
516 klass = LookupClass(descriptor, class_loader);
517 CHECK(klass != NULL);
518 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700519 // Finish loading (if necessary) by finding parents
520 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
521 // Loading failed.
522 // TODO: CHECK(self->IsExceptionPending());
523 lock.NotifyAll();
524 return NULL;
525 }
526 CHECK(klass->IsLoaded());
527 // Link the class (if necessary)
528 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700529 // Linking failed.
530 // TODO: CHECK(self->IsExceptionPending());
531 lock.NotifyAll();
532 return NULL;
533 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700534 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535 }
536 }
537 }
538 // Link the class if it has not already been linked.
539 if (!klass->IsLinked() && !klass->IsErroneous()) {
540 ObjectLock lock(klass);
541 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700542 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700543 self->ThrowNewException("Ljava/lang/ClassCircularityError;", NULL); // TODO: detail
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700544 return NULL;
545 }
546 // Wait for the pending initialization to complete.
547 while (!klass->IsLinked() && !klass->IsErroneous()) {
548 lock.Wait();
549 }
550 }
551 if (klass->IsErroneous()) {
552 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
553 return NULL;
554 }
555 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700556 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700557 CHECK(!self->IsExceptionPending());
558 return klass;
559}
560
Brian Carlstrom4873d462011-08-21 15:23:39 -0700561// Precomputes size that will be needed for Class, matching LinkStaticFields
562size_t ClassLinker::SizeOfClass(const DexFile& dex_file,
563 const DexFile::ClassDef& dex_class_def) {
564 const byte* class_data = dex_file.GetClassData(dex_class_def);
565 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
566 size_t num_static_fields = header.static_fields_size_;
567 size_t num_ref = 0;
568 size_t num_32 = 0;
569 size_t num_64 = 0;
570 if (num_static_fields != 0) {
571 uint32_t last_idx = 0;
572 for (size_t i = 0; i < num_static_fields; ++i) {
573 DexFile::Field dex_field;
574 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
575 const DexFile::FieldId& field_id = dex_file.GetFieldId(dex_field.field_idx_);
576 const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_);
577 char c = descriptor[0];
578 if (c == 'L' || c == '[') {
579 num_ref++;
580 } else if (c == 'J' || c == 'D') {
581 num_64++;
582 } else {
583 num_32++;
584 }
585 }
586 }
587
588 // start with generic class data
589 size_t size = sizeof(Class);
590 // follow with reference fields which must be contiguous at start
591 size += (num_ref * sizeof(uint32_t));
592 // if there are 64-bit fields to add, make sure they are aligned
593 if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment
594 if (num_32 != 0) {
595 // use an available 32-bit field for padding
596 num_32--;
597 }
598 size += sizeof(uint32_t); // either way, we are adding a word
599 DCHECK_EQ(size, RoundUp(size, 8));
600 }
601 // tack on any 64-bit fields now that alignment is assured
602 size += (num_64 * sizeof(uint64_t));
603 // tack on any remaining 32-bit fields
604 size += (num_32 * sizeof(uint32_t));
605 return size;
606}
607
Brian Carlstromf615a612011-07-23 12:50:34 -0700608void ClassLinker::LoadClass(const DexFile& dex_file,
609 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700610 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700611 const ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700612 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700613 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700614 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700615 const byte* class_data = dex_file.GetClassData(dex_class_def);
616 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700617
Brian Carlstromf615a612011-07-23 12:50:34 -0700618 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700619 CHECK(descriptor != NULL);
620
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700621 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700622 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -0700623 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700624 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700625 klass->primitive_type_ = Class::kPrimNot;
626 klass->status_ = Class::kStatusIdx;
627
628 klass->super_class_ = NULL;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700629 klass->super_class_type_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700630
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700631 size_t num_static_fields = header.static_fields_size_;
632 size_t num_instance_fields = header.instance_fields_size_;
633 size_t num_direct_methods = header.direct_methods_size_;
634 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700635
Brian Carlstromf615a612011-07-23 12:50:34 -0700636 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700637
638 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700639 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700640
641 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700642 DCHECK(klass->sfields_ == NULL);
643 if (num_static_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400644 klass->sfields_ = AllocObjectArray<Field>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700645 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700646 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700647 DexFile::Field dex_field;
648 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400649 Field* sfield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700650 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700651 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700652 }
653 }
654
655 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700656 DCHECK(klass->ifields_ == NULL);
657 if (num_instance_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400658 klass->ifields_ = AllocObjectArray<Field>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700659 uint32_t last_idx = 0;
660 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700661 DexFile::Field dex_field;
662 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400663 Field* ifield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700664 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700665 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700666 }
667 }
668
669 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700670 DCHECK(klass->direct_methods_ == NULL);
671 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700672 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700673 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700674 uint32_t last_idx = 0;
675 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700676 DexFile::Method dex_method;
677 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700678 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700679 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700680 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700681 // TODO: register maps
682 }
683 }
684
685 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700686 DCHECK(klass->virtual_methods_ == NULL);
687 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700688 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700689 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700690 uint32_t last_idx = 0;
691 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700692 DexFile::Method dex_method;
693 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700694 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700695 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700696 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700697 // TODO: register maps
698 }
699 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700700}
701
Brian Carlstromf615a612011-07-23 12:50:34 -0700702void ClassLinker::LoadInterfaces(const DexFile& dex_file,
703 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700704 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700705 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700706 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700707 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700708 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700709 DCHECK(klass->interfaces_type_idx_ == NULL);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700710 klass->interfaces_type_idx_ = IntArray::Alloc(list->Size());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700711 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700712 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700713 klass->interfaces_type_idx_->Set(i, type_item.type_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700714 }
715 }
716}
717
Brian Carlstromf615a612011-07-23 12:50:34 -0700718void ClassLinker::LoadField(const DexFile& dex_file,
719 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700720 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700721 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700722 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400723 dst->declaring_class_ = klass;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700724 dst->name_ = ResolveString(dex_file, field_id.name_idx_, klass->GetDexCache());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700725 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Jesse Wilson35baaab2011-08-10 16:18:03 -0400726 // TODO: Assign dst->type_.
Brian Carlstrom934486c2011-07-12 23:42:50 -0700727 dst->access_flags_ = src.access_flags_;
728}
729
Brian Carlstromf615a612011-07-23 12:50:34 -0700730void ClassLinker::LoadMethod(const DexFile& dex_file,
731 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700732 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700733 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700734 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400735 dst->declaring_class_ = klass;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700736 dst->name_ = ResolveString(dex_file, method_id.name_idx_, klass->GetDexCache());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700737 {
738 int32_t utf16_length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700739 std::string utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_, &utf16_length));
740 dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.c_str());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700741 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700742 dst->proto_idx_ = method_id.proto_idx_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700743 dst->code_off_ = src.code_off_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700744 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700745 dst->access_flags_ = src.access_flags_;
746
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700747 dst->dex_cache_strings_ = klass->dex_cache_->GetStrings();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700748 dst->dex_cache_types_ = klass->dex_cache_->GetTypes();
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700749 dst->dex_cache_methods_ = klass->dex_cache_->GetMethods();
750 dst->dex_cache_fields_ = klass->dex_cache_->GetFields();
751
Brian Carlstrom934486c2011-07-12 23:42:50 -0700752 // TODO: check for finalize method
753
Brian Carlstromf615a612011-07-23 12:50:34 -0700754 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700755 if (code_item != NULL) {
756 dst->num_registers_ = code_item->registers_size_;
757 dst->num_ins_ = code_item->ins_size_;
758 dst->num_outs_ = code_item->outs_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700759 } else {
760 uint16_t num_args = dst->NumArgRegisters();
761 if (!dst->IsStatic()) {
762 ++num_args;
763 }
764 dst->num_registers_ = dst->num_ins_ + num_args;
765 // TODO: native methods
766 }
767}
768
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700769void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700770 AppendToBootClassPath(dex_file, AllocDexCache(dex_file));
771}
772
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700773void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700774 CHECK(dex_cache != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700775 boot_class_path_.push_back(&dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700776 RegisterDexFile(dex_file, dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700777}
778
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700779void ClassLinker::RegisterDexFile(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700780 RegisterDexFile(dex_file, AllocDexCache(dex_file));
781}
782
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700783void ClassLinker::RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700784 CHECK(dex_cache != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700785 dex_files_.push_back(&dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700786 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700787}
788
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700789const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700790 for (size_t i = 0; i != dex_caches_.size(); ++i) {
791 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700792 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700793 }
794 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700795 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700796 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700797}
798
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700799DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700800 for (size_t i = 0; i != dex_files_.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700801 if (dex_files_[i] == &dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700802 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700803 }
804 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700805 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700806 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700807}
808
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700809Class* ClassLinker::CreatePrimitiveClass(const char* descriptor) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700810 Class* klass = AllocClass(sizeof(Class));
Carl Shapiro565f5072011-07-10 13:39:43 -0700811 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700812 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700813 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700814 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Carl Shapiro565f5072011-07-10 13:39:43 -0700815 klass->status_ = Class::kStatusInitialized;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700816 bool success = InsertClass(descriptor, klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700817 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700818 return klass;
819}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700820
Brian Carlstrombe977852011-07-19 14:54:54 -0700821// Create an array class (i.e. the class object for the array, not the
822// array itself). "descriptor" looks like "[C" or "[[[[B" or
823// "[Ljava/lang/String;".
824//
825// If "descriptor" refers to an array of primitives, look up the
826// primitive type's internally-generated class object.
827//
828// "loader" is the class loader of the class that's referring to us. It's
829// used to ensure that we're looking for the element type in the right
830// context. It does NOT become the class loader for the array class; that
831// always comes from the base element class.
832//
833// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700834Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700835 const ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700836{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700837 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700838
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700839 // Identify the underlying element class and the array dimension depth.
840 Class* component_type_ = NULL;
841 int array_rank;
842 if (descriptor[1] == '[') {
843 // array of arrays; keep descriptor and grab stuff from parent
844 Class* outer = FindClass(descriptor.substr(1), class_loader);
845 if (outer != NULL) {
846 // want the base class, not "outer", in our component_type_
847 component_type_ = outer->component_type_;
848 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700849 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700850 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700851 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700852 } else {
853 array_rank = 1;
854 if (descriptor[1] == 'L') {
855 // array of objects; strip off "[" and look up descriptor.
856 const StringPiece subDescriptor = descriptor.substr(1);
857 component_type_ = FindClass(subDescriptor, class_loader);
858 } else {
859 // array of a primitive type
860 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700861 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700862 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700863
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700864 if (component_type_ == NULL) {
865 // failed
866 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
867 return NULL;
868 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700869
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700870 // See if the component type is already loaded. Array classes are
871 // always associated with the class loader of their underlying
872 // element type -- an array of Strings goes with the loader for
873 // java/lang/String -- so we need to look for it there. (The
874 // caller should have checked for the existence of the class
875 // before calling here, but they did so with *their* class loader,
876 // not the component type's loader.)
877 //
878 // If we find it, the caller adds "loader" to the class' initiating
879 // loader list, which should prevent us from going through this again.
880 //
881 // This call is unnecessary if "loader" and "component_type_->class_loader_"
882 // are the same, because our caller (FindClass) just did the
883 // lookup. (Even if we get this wrong we still have correct behavior,
884 // because we effectively do this lookup again when we add the new
885 // class to the hash table --- necessary because of possible races with
886 // other threads.)
887 if (class_loader != component_type_->class_loader_) {
888 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
889 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700890 return new_class;
891 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700892 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700893
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700894 // Fill out the fields in the Class.
895 //
896 // It is possible to execute some methods against arrays, because
897 // all arrays are subclasses of java_lang_Object_, so we need to set
898 // up a vtable. We can just point at the one in java_lang_Object_.
899 //
900 // Array classes are simple enough that we don't need to do a full
901 // link step.
902
903 Class* new_class = NULL;
904 if (!init_done_) {
905 if (descriptor == "[Ljava/lang/Object;") {
906 new_class = GetClassRoot(kObjectArrayClass);
907 } else if (descriptor == "[C") {
908 new_class = GetClassRoot(kCharArrayClass);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700909 } else if (descriptor == "[I") {
910 new_class = GetClassRoot(kIntArrayClass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700911 }
912 }
913 if (new_class == NULL) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700914 new_class = AllocClass(sizeof(Class));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700915 if (new_class == NULL) {
916 return NULL;
917 }
918 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700919 new_class->descriptor_ = String::AllocFromModifiedUtf8(descriptor.ToString().c_str());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700920 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
921 new_class->super_class_ = java_lang_Object;
922 new_class->vtable_ = java_lang_Object->vtable_;
923 new_class->primitive_type_ = Class::kPrimNot;
924 new_class->component_type_ = component_type_;
925 new_class->class_loader_ = component_type_->class_loader_;
926 new_class->array_rank_ = array_rank;
927 new_class->status_ = Class::kStatusInitialized;
928 // don't need to set new_class->object_size_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700929 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700930
931
932 // All arrays have java/lang/Cloneable and java/io/Serializable as
933 // interfaces. We need to set that up here, so that stuff like
934 // "instanceof" works right.
935 //
936 // Note: The GC could run during the call to FindSystemClass,
937 // so we need to make sure the class object is GC-valid while we're in
938 // there. Do this by clearing the interface list so the GC will just
939 // think that the entries are null.
940
941
942 // Use the single, global copies of "interfaces" and "iftable"
943 // (remember not to free them for arrays).
944 DCHECK(array_interfaces_ != NULL);
945 new_class->interfaces_ = array_interfaces_;
946 new_class->iftable_count_ = 2;
947 DCHECK(array_iftable_ != NULL);
948 new_class->iftable_ = array_iftable_;
949
950 // Inherit access flags from the component type. Arrays can't be
951 // used as a superclass or interface, so we want to add "final"
952 // and remove "interface".
953 //
954 // Don't inherit any non-standard flags (e.g., kAccFinal)
955 // from component_type_. We assume that the array class does not
956 // override finalize().
957 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
958 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
959
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700960 if (InsertClass(descriptor, new_class)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700961 return new_class;
962 }
963 // Another thread must have loaded the class after we
964 // started but before we finished. Abandon what we've
965 // done.
966 //
967 // (Yes, this happens.)
968
969 // Grab the winning class.
970 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
971 DCHECK(other_class != NULL);
972 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700973}
974
975Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700976 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700977 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700978 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700979 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700980 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700981 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700982 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700983 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700984 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700985 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700986 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700987 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700988 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700989 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700990 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700991 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700992 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700993 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700994 return GetClassRoot(kPrimitiveVoid);
Carl Shapiro744ad052011-08-06 15:53:36 -0700995 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700996 std::string printable_type(PrintableChar(type));
997 Thread::Current()->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
998 "Not a primitive type: %s", printable_type.c_str());
999 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001000}
1001
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001002bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) {
1003 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001004 MutexLock mu(classes_lock_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001005 Table::iterator it = classes_.insert(std::make_pair(hash, klass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001006 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001007}
1008
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001009Class* ClassLinker::LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001010 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001011 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001012 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001013 for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001014 Class* klass = it->second;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001015 if (klass->descriptor_->Equals(descriptor) && klass->class_loader_ == class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001016 return klass;
1017 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001018 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001019 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001020}
1021
1022bool ClassLinker::InitializeClass(Class* klass) {
1023 CHECK(klass->GetStatus() == Class::kStatusResolved ||
1024 klass->GetStatus() == Class::kStatusError);
1025
Carl Shapirob5573532011-07-12 18:22:59 -07001026 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001027
1028 {
1029 ObjectLock lock(klass);
1030
1031 if (klass->GetStatus() < Class::kStatusVerified) {
1032 if (klass->IsErroneous()) {
1033 LG << "re-initializing failed class"; // TODO: throw
1034 return false;
1035 }
1036
1037 CHECK(klass->GetStatus() == Class::kStatusResolved);
1038
1039 klass->status_ = Class::kStatusVerifying;
1040 if (!DexVerify::VerifyClass(klass)) {
1041 LG << "Verification failed"; // TODO: ThrowVerifyError
1042 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001043 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001044 klass->SetStatus(Class::kStatusError);
1045 return false;
1046 }
1047
1048 klass->SetStatus(Class::kStatusVerified);
1049 }
1050
1051 if (klass->status_ == Class::kStatusInitialized) {
1052 return true;
1053 }
1054
1055 while (klass->status_ == Class::kStatusInitializing) {
1056 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -07001057 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001058 LG << "recursive <clinit>";
1059 return true;
1060 }
1061
1062 CHECK(!self->IsExceptionPending());
1063
1064 lock.Wait(); // TODO: check for interruption
1065
1066 // When we wake up, repeat the test for init-in-progress. If
1067 // there's an exception pending (only possible if
1068 // "interruptShouldThrow" was set), bail out.
1069 if (self->IsExceptionPending()) {
1070 CHECK(false);
1071 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
1072 klass->SetStatus(Class::kStatusError);
1073 return false;
1074 }
1075 if (klass->GetStatus() == Class::kStatusInitializing) {
1076 continue;
1077 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001078 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001079 klass->GetStatus() == Class::kStatusError);
1080 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001081 // The caller wants an exception, but it was thrown in a
1082 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001083 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
1084 return false;
1085 }
1086 return true; // otherwise, initialized
1087 }
1088
1089 // see if we failed previously
1090 if (klass->IsErroneous()) {
1091 // might be wise to unlock before throwing; depends on which class
1092 // it is that we have locked
1093
1094 // TODO: throwEarlierClassFailure(klass);
1095 return false;
1096 }
1097
1098 if (!ValidateSuperClassDescriptors(klass)) {
1099 klass->SetStatus(Class::kStatusError);
1100 return false;
1101 }
1102
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001103 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001104
Carl Shapirob5573532011-07-12 18:22:59 -07001105 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001106 klass->status_ = Class::kStatusInitializing;
1107 }
1108
1109 if (!InitializeSuperClass(klass)) {
1110 return false;
1111 }
1112
1113 InitializeStaticFields(klass);
1114
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001115 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001116 if (clinit != NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001117 // JValue unused;
1118 // TODO: dvmCallMethod(self, method, NULL, &unused);
Elliott Hughes53b61312011-08-12 18:28:20 -07001119 // UNIMPLEMENTED(FATAL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001120 }
1121
1122 {
1123 ObjectLock lock(klass);
1124
1125 if (self->IsExceptionPending()) {
1126 klass->SetStatus(Class::kStatusError);
1127 } else {
1128 klass->SetStatus(Class::kStatusInitialized);
1129 }
1130 lock.NotifyAll();
1131 }
1132
1133 return true;
1134}
1135
1136bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
1137 if (klass->IsInterface()) {
1138 return true;
1139 }
1140 // begin with the methods local to the superclass
1141 if (klass->HasSuperClass() &&
1142 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
1143 const Class* super = klass->GetSuperClass();
1144 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001145 const Method* method = super->GetVirtualMethod(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001146 if (method != super->GetVirtualMethod(i) &&
1147 !HasSameMethodDescriptorClasses(method, super, klass)) {
1148 LG << "Classes resolve differently in superclass";
1149 return false;
1150 }
1151 }
1152 }
1153 for (size_t i = 0; i < klass->iftable_count_; ++i) {
1154 const InterfaceEntry* iftable = &klass->iftable_[i];
Brian Carlstrom30b94452011-08-25 21:35:26 -07001155 Class* interface = iftable->GetInterface();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001156 if (klass->GetClassLoader() != interface->GetClassLoader()) {
1157 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1158 uint32_t vtable_index = iftable->method_index_array_[j];
1159 const Method* method = klass->GetVirtualMethod(vtable_index);
1160 if (!HasSameMethodDescriptorClasses(method, interface,
1161 method->GetClass())) {
1162 LG << "Classes resolve differently in interface"; // TODO: LinkageError
1163 return false;
1164 }
1165 }
1166 }
1167 }
1168 return true;
1169}
1170
1171bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001172 const Class* klass1,
1173 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001174 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
1175 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001176 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001177 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001178 const char* descriptor = it->GetDescriptor();
1179 if (descriptor == NULL) {
1180 break;
1181 }
1182 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1183 // Found a non-primitive type.
1184 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1185 return false;
1186 }
1187 }
1188 }
1189 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001190 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001191 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1192 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1193 return false;
1194 }
1195 }
1196 return true;
1197}
1198
1199// Returns true if classes referenced by the descriptor are the
1200// same classes in klass1 as they are in klass2.
1201bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001202 const Class* klass1,
1203 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001204 CHECK(descriptor != NULL);
1205 CHECK(klass1 != NULL);
1206 CHECK(klass2 != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001207 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001208 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001209 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001210 // TODO: found2 == NULL
1211 // TODO: lookup found1 in initiating loader list
1212 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -07001213 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001214 if (found1 == found2) {
1215 return true;
1216 } else {
1217 return false;
1218 }
1219 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001220 return true;
1221}
1222
1223bool ClassLinker::InitializeSuperClass(Class* klass) {
1224 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001225 if (!klass->IsInterface() && klass->HasSuperClass()) {
1226 Class* super_class = klass->GetSuperClass();
1227 if (super_class->GetStatus() != Class::kStatusInitialized) {
1228 CHECK(!super_class->IsInterface());
1229 klass->MonitorExit();
1230 bool super_initialized = InitializeClass(super_class);
1231 klass->MonitorEnter();
1232 // TODO: check for a pending exception
1233 if (!super_initialized) {
1234 klass->SetStatus(Class::kStatusError);
1235 klass->NotifyAll();
1236 return false;
1237 }
1238 }
1239 }
1240 return true;
1241}
1242
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001243bool ClassLinker::EnsureInitialized(Class* c) {
1244 CHECK(c != NULL);
1245 if (c->IsInitialized()) {
1246 return true;
1247 }
1248
1249 c->MonitorExit();
1250 InitializeClass(c);
1251 c->MonitorEnter();
1252 return !Thread::Current()->IsExceptionPending();
1253}
1254
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001255void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001256 size_t num_static_fields = klass->NumStaticFields();
1257 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001258 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001259 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001260 DexCache* dex_cache = klass->GetDexCache();
Brian Carlstrom4873d462011-08-21 15:23:39 -07001261 // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray?
Brian Carlstromf615a612011-07-23 12:50:34 -07001262 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001263 return;
1264 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001265 const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001266 const DexFile& dex_file = FindDexFile(dex_cache);
1267 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001268 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001269 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001270 if (addr == NULL) {
1271 // All this class' static fields have default values.
1272 return;
1273 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001274 size_t array_size = DecodeUnsignedLeb128(&addr);
1275 for (size_t i = 0; i < array_size; ++i) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001276 Field* field = klass->GetStaticField(i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001277 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001278 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001279 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001280 case DexFile::kByte:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001281 field->SetByte(NULL, value.b);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001282 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001283 case DexFile::kShort:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001284 field->SetShort(NULL, value.s);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001285 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001286 case DexFile::kChar:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001287 field->SetChar(NULL, value.c);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001288 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001289 case DexFile::kInt:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001290 field->SetInt(NULL, value.i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001291 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001292 case DexFile::kLong:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001293 field->SetLong(NULL, value.j);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001294 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001295 case DexFile::kFloat:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001296 field->SetFloat(NULL, value.f);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001297 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001298 case DexFile::kDouble:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001299 field->SetDouble(NULL, value.d);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001300 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001301 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001302 uint32_t string_idx = value.i;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001303 String* resolved = ResolveString(dex_file, string_idx, klass->GetDexCache());
Brian Carlstrom4873d462011-08-21 15:23:39 -07001304 field->SetObject(NULL, resolved);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001305 break;
1306 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001307 case DexFile::kBoolean:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001308 field->SetBoolean(NULL, value.z);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001309 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001310 case DexFile::kNull:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001311 field->SetObject(NULL, value.l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001312 break;
1313 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001314 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001315 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001316 }
1317}
1318
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001319bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1320 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001321 if (!LinkSuperClass(klass)) {
1322 return false;
1323 }
1324 if (!LinkMethods(klass)) {
1325 return false;
1326 }
1327 if (!LinkInstanceFields(klass)) {
1328 return false;
1329 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001330 if (!LinkStaticFields(klass)) {
1331 return false;
1332 }
1333 CreateReferenceInstanceOffsets(klass);
1334 CreateReferenceStaticOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001335 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001336 klass->status_ = Class::kStatusResolved;
1337 return true;
1338}
1339
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001340bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1341 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001342 if (klass->super_class_type_idx_ != DexFile::kDexNoIndex) {
1343 Class* super_class = ResolveType(dex_file, klass->super_class_type_idx_, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001344 if (super_class == NULL) {
1345 LG << "Failed to resolve superclass";
1346 return false;
1347 }
1348 klass->super_class_ = super_class; // TODO: write barrier
1349 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001350 if (klass->NumInterfaces() > 0) {
1351 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001352 uint32_t type_idx = klass->interfaces_type_idx_->Get(i);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001353 klass->SetInterface(i, ResolveType(dex_file, type_idx, klass));
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001354 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001355 LG << "Failed to resolve interface";
1356 return false;
1357 }
1358 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001359 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001360 LG << "Inaccessible interface";
1361 return false;
1362 }
1363 }
1364 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001365 // Mark the class as loaded.
1366 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001367 return true;
1368}
1369
1370bool ClassLinker::LinkSuperClass(Class* klass) {
1371 CHECK(!klass->IsPrimitive());
1372 const Class* super = klass->GetSuperClass();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001373 if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001374 if (super != NULL) {
1375 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1376 return false;
1377 }
1378 // TODO: clear finalize attribute
1379 return true;
1380 }
1381 if (super == NULL) {
1382 LG << "No superclass defined"; // TODO: LinkageError
1383 return false;
1384 }
1385 // Verify
1386 if (super->IsFinal()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001387 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001388 return false;
1389 }
1390 if (super->IsInterface()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001391 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001392 return false;
1393 }
1394 if (!klass->CanAccess(super)) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001395 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001396 return false;
1397 }
1398 return true;
1399}
1400
1401// Populate the class vtable and itable.
1402bool ClassLinker::LinkMethods(Class* klass) {
1403 if (klass->IsInterface()) {
1404 // No vtable.
1405 size_t count = klass->NumVirtualMethods();
1406 if (!IsUint(16, count)) {
1407 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1408 return false;
1409 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001410 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001411 klass->GetVirtualMethod(i)->method_index_ = i;
1412 }
1413 } else {
1414 // Link virtual method tables
1415 LinkVirtualMethods(klass);
1416
1417 // Link interface method tables
1418 LinkInterfaceMethods(klass);
1419
1420 // Insert stubs.
1421 LinkAbstractMethods(klass);
1422 }
1423 return true;
1424}
1425
1426bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001427 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001428 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001429 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1430 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001431 // TODO: do not assign to the vtable field until it is fully constructed.
1432 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001433 // See if any of our virtual methods override the superclass.
1434 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1435 Method* local_method = klass->GetVirtualMethod(i);
1436 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001437 for (; j < actual_count; ++j) {
1438 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001439 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001440 // Verify
1441 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001442 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001443 return false;
1444 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001445 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001446 local_method->method_index_ = j;
1447 break;
1448 }
1449 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001450 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001451 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001452 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001453 local_method->method_index_ = actual_count;
1454 actual_count += 1;
1455 }
1456 }
1457 if (!IsUint(16, actual_count)) {
1458 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1459 return false;
1460 }
1461 CHECK_LE(actual_count, max_count);
1462 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001463 // TODO: do not assign to the vtable field until it is fully constructed.
1464 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001465 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001466 } else {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001467 CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001468 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001469 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001470 LG << "Too many methods"; // TODO: VirtualMachineError
1471 return false;
1472 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001473 // TODO: do not assign to the vtable field until it is fully constructed.
1474 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1475 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001476 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001477 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1478 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001479 }
1480 return true;
1481}
1482
1483bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1484 int pool_offset = 0;
1485 int pool_size = 0;
1486 int miranda_count = 0;
1487 int miranda_alloc = 0;
1488 size_t super_ifcount;
1489 if (klass->HasSuperClass()) {
1490 super_ifcount = klass->GetSuperClass()->iftable_count_;
1491 } else {
1492 super_ifcount = 0;
1493 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001494 size_t ifcount = super_ifcount;
1495 ifcount += klass->NumInterfaces();
1496 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1497 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001498 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001499 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001500 DCHECK(klass->iftable_count_ == 0);
1501 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001502 return true;
1503 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001504 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1505 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001506 if (super_ifcount != 0) {
1507 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1508 sizeof(InterfaceEntry) * super_ifcount);
1509 }
1510 // Flatten the interface inheritance hierarchy.
1511 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001512 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1513 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001514 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001515 if (!interf->IsInterface()) {
1516 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1517 return false;
1518 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001519 klass->iftable_[idx++].SetInterface(interf);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001520 for (size_t j = 0; j < interf->iftable_count_; j++) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001521 klass->iftable_[idx++].SetInterface(interf->iftable_[j].GetInterface());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001522 }
1523 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001524 CHECK_EQ(idx, ifcount);
1525 klass->iftable_count_ = ifcount;
1526 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001527 return true;
1528 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001529 for (size_t i = super_ifcount; i < ifcount; i++) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001530 pool_size += klass->iftable_[i].GetInterface()->NumVirtualMethods();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001531 }
1532 if (pool_size == 0) {
1533 return true;
1534 }
1535 klass->ifvi_pool_count_ = pool_size;
1536 klass->ifvi_pool_ = new uint32_t[pool_size];
1537 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001538 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001539 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001540 Class* interface = klass->iftable_[i].GetInterface();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001541 pool_offset += interface->NumVirtualMethods(); // end here
1542 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1543 Method* interface_method = interface->GetVirtualMethod(j);
1544 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001545 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001546 Method* vtable_method = klass->vtable_->Get(k);
1547 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1548 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001549 LG << "Implementation not public";
1550 return false;
1551 }
1552 klass->iftable_[i].method_index_array_[j] = k;
1553 break;
1554 }
1555 }
1556 if (k < 0) {
1557 if (miranda_count == miranda_alloc) {
1558 miranda_alloc += 8;
1559 if (miranda_list.empty()) {
1560 miranda_list.resize(miranda_alloc);
1561 } else {
1562 miranda_list.resize(miranda_alloc);
1563 }
1564 }
1565 int mir;
1566 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001567 Method* miranda_method = miranda_list[mir];
1568 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001569 break;
1570 }
1571 }
1572 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001573 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001574 if (mir == miranda_count) {
1575 miranda_list[miranda_count++] = interface_method;
1576 }
1577 }
1578 }
1579 }
1580 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001581 int old_method_count = klass->NumVirtualMethods();
1582 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001583 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001584
1585 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001586 int old_vtable_count = klass->vtable_->GetLength();
1587 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001588 // TODO: do not assign to the vtable field until it is fully constructed.
1589 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001590
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001591 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001592 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001593 memcpy(meth, miranda_list[i], sizeof(Method));
1594 meth->klass_ = klass;
1595 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001596 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1597 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001598 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001599 }
1600 }
1601 return true;
1602}
1603
1604void ClassLinker::LinkAbstractMethods(Class* klass) {
1605 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1606 Method* method = klass->GetVirtualMethod(i);
1607 if (method->IsAbstract()) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001608 LG << "AbstractMethodError";
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001609 // TODO: throw AbstractMethodError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001610 }
1611 }
1612}
1613
1614bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001615 CHECK(klass != NULL);
1616 size_t field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001617 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001618 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001619 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001620 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001621 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001622 return LinkFields(field_offset,
1623 klass->num_reference_instance_fields_,
1624 klass->NumInstanceFields(),
1625 klass->ifields_,
1626 klass->object_size_);
1627}
1628
1629bool ClassLinker::LinkStaticFields(Class* klass) {
1630 CHECK(klass != NULL);
1631 size_t allocated_class_size = klass->class_size_;
1632 size_t field_offset = OFFSETOF_MEMBER(Class, fields_);
1633 bool success = LinkFields(field_offset,
1634 klass->num_reference_static_fields_,
1635 klass->NumStaticFields(),
1636 klass->sfields_,
1637 klass->class_size_);
1638 CHECK_EQ(allocated_class_size, klass->class_size_);
1639 return success;
1640}
1641
1642bool ClassLinker::LinkFields(size_t field_offset,
1643 size_t& num_reference_fields,
1644 size_t num_fields,
1645 ObjectArray<Field>* fields,
1646 size_t& size) {
1647 CHECK((num_fields == 0) == (fields == NULL));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001648 // Move references to the front.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001649 num_reference_fields = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001650 size_t i = 0;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001651 for ( ; i < num_fields; i++) {
1652 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001653 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001654 if (c != '[' && c != 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001655 for (size_t j = num_fields - 1; j > i; j--) {
1656 Field* refField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001657 char rc = refField->GetType();
1658 if (rc == '[' || rc == 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001659 fields->Set(i, refField);
1660 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001661 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001662 c = rc;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001663 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001664 break;
1665 }
1666 }
1667 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001668 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001669 }
1670 if (c != '[' && c != 'L') {
1671 break;
1672 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001673 pField->SetOffset(field_offset);
1674 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001675 }
1676
1677 // Now we want to pack all of the double-wide fields together. If
1678 // we're not aligned, though, we want to shuffle one 32-bit field
1679 // into place. If we can't find one, we'll have to pad it.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001680 if (i != num_fields && (field_offset & 0x04) != 0) {
1681 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001682 char c = pField->GetType();
1683
1684 if (c != 'J' && c != 'D') {
1685 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001686 DCHECK(c != '[');
1687 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001688 pField->SetOffset(field_offset);
1689 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001690 i++;
1691 } else {
1692 // Next field is 64-bit, so search for a 32-bit field we can
1693 // swap into it.
1694 bool found = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001695 for (size_t j = num_fields - 1; j > i; j--) {
1696 Field* singleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001697 char rc = singleField->GetType();
1698 if (rc != 'J' && rc != 'D') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001699 fields->Set(i, singleField);
1700 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001701 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001702 pField->SetOffset(field_offset);
1703 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001704 found = true;
1705 i++;
1706 break;
1707 }
1708 }
1709 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001710 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001711 }
1712 }
1713 }
1714
1715 // Alignment is good, shuffle any double-wide fields forward, and
1716 // finish assigning field offsets to all fields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001717 DCHECK(i == num_fields || (field_offset & 0x04) == 0);
1718 for ( ; i < num_fields; i++) {
1719 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001720 char c = pField->GetType();
1721 if (c != 'D' && c != 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001722 for (size_t j = num_fields - 1; j > i; j--) {
1723 Field* doubleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001724 char rc = doubleField->GetType();
1725 if (rc == 'D' || rc == 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001726 fields->Set(i, doubleField);
1727 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001728 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001729 c = rc;
1730 break;
1731 }
1732 }
1733 } else {
1734 // This is a double-wide field, leave it be.
1735 }
1736
Brian Carlstroma0808032011-07-18 00:39:23 -07001737 pField->SetOffset(field_offset);
1738 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001739 if (c == 'J' || c == 'D') {
Brian Carlstroma0808032011-07-18 00:39:23 -07001740 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001741 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001742 }
1743
1744#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001745 // Make sure that all reference fields appear before
1746 // non-reference fields, and all double-wide fields are aligned.
1747 bool seen_non_ref = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001748 for (i = 0; i < num_fields; i++) {
1749 Field *pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001750 char c = pField->GetType();
1751
1752 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001753 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001754 }
1755
1756 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001757 if (!seen_non_ref) {
1758 seen_non_ref = true;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001759 DCHECK_EQ(num_reference_fields, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001760 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001761 } else {
1762 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001763 }
1764 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001765 if (!seen_non_ref) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001766 DCHECK_EQ(num_fields, num_reference_fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001767 }
1768#endif
Brian Carlstrom4873d462011-08-21 15:23:39 -07001769 size = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001770 return true;
1771}
1772
1773// Set the bitmap of reference offsets, refOffsets, from the ifields
1774// list.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001775void ClassLinker::CreateReferenceInstanceOffsets(Class* klass) {
1776 klass->reference_instance_offsets_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001777 if (klass->HasSuperClass()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001778 klass->reference_instance_offsets_ = klass->GetSuperClass()->GetReferenceInstanceOffsets();
1779 // If our superclass overflowed, we don't stand a chance.
1780 if (klass->reference_instance_offsets_ == CLASS_WALK_SUPER) {
1781 return;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001782 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001783 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001784 CreateReferenceOffsets(klass->reference_instance_offsets_,
1785 klass->NumReferenceInstanceFields(),
1786 klass->ifields_);
1787}
1788
1789void ClassLinker::CreateReferenceStaticOffsets(Class* klass) {
1790 klass->reference_static_offsets_ = 0;
1791 CreateReferenceOffsets(klass->reference_static_offsets_,
1792 klass->NumReferenceStaticFields(),
1793 klass->sfields_);
1794}
1795
1796void ClassLinker::CreateReferenceOffsets(uint32_t& reference_offsets,
1797 size_t num_reference_fields,
1798 const ObjectArray<Field>* fields) {
1799 // All of the fields that contain object references are guaranteed
1800 // to be at the beginning of the fields list.
1801 for (size_t i = 0; i < num_reference_fields; ++i) {
1802 // Note that byte_offset is the offset from the beginning of
1803 // object, not the offset into instance data
1804 const Field* field = fields->Get(i);
1805 size_t byte_offset = field->GetOffset();
1806 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
1807 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
1808 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1809 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
1810 CHECK_NE(new_bit, 0U);
1811 reference_offsets |= new_bit;
1812 } else {
1813 reference_offsets = CLASS_WALK_SUPER;
1814 break;
1815 }
1816 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001817}
1818
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001819String* ClassLinker::ResolveString(const DexFile& dex_file,
1820 uint32_t string_idx,
1821 DexCache* dex_cache) {
1822 String* resolved = dex_cache->GetResolvedString(string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001823 if (resolved != NULL) {
1824 return resolved;
1825 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001826 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1827 int32_t utf16_length = dex_file.GetStringLength(string_id);
1828 const char* utf8_data = dex_file.GetStringData(string_id);
1829 String* string = intern_table_.Intern(utf16_length, utf8_data);
1830 dex_cache->SetResolvedString(string_idx, string);
1831 return string;
1832}
1833
1834Class* ClassLinker::ResolveType(const DexFile& dex_file,
1835 uint32_t type_idx,
1836 DexCache* dex_cache,
1837 const ClassLoader* class_loader) {
1838 Class* resolved = dex_cache->GetResolvedType(type_idx);
1839 if (resolved != NULL) {
1840 return resolved;
1841 }
1842 const char* descriptor = dex_file.dexStringByTypeIdx(type_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001843 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001844 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001845 } else {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001846 resolved = FindClass(descriptor, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001847 }
1848 if (resolved != NULL) {
1849 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001850 if (dex_cache != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001851 if (check->GetClassLoader() != NULL) {
1852 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1853 return NULL;
1854 }
1855 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001856 dex_cache->SetResolvedType(type_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001857 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001858 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001859 }
1860 return resolved;
1861}
1862
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001863Method* ClassLinker::ResolveMethod(const DexFile& dex_file,
1864 uint32_t method_idx,
1865 DexCache* dex_cache,
1866 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001867 bool is_direct) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001868 Method* resolved = dex_cache->GetResolvedMethod(method_idx);
1869 if (resolved != NULL) {
1870 return resolved;
1871 }
1872 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
1873 Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader);
1874 if (klass == NULL) {
1875 return NULL;
1876 }
1877
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001878 const char* name = dex_file.dexStringById(method_id.name_idx_);
Elliott Hughes0c424cb2011-08-26 10:16:25 -07001879 std::string signature(dex_file.CreateMethodDescriptor(method_id.proto_idx_, NULL));
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001880 if (is_direct) {
1881 resolved = klass->FindDirectMethod(name, signature);
1882 } else {
1883 resolved = klass->FindVirtualMethod(name, signature);
1884 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001885 if (resolved != NULL) {
1886 dex_cache->SetResolvedMethod(method_idx, resolved);
1887 } else {
1888 // DCHECK(Thread::Current()->IsExceptionPending());
1889 }
1890 return resolved;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001891}
1892
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001893Field* ClassLinker::ResolveField(const DexFile& dex_file,
1894 uint32_t field_idx,
1895 DexCache* dex_cache,
1896 const ClassLoader* class_loader,
1897 bool is_static) {
1898 Field* resolved = dex_cache->GetResolvedField(field_idx);
1899 if (resolved != NULL) {
1900 return resolved;
1901 }
1902 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx);
1903 Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader);
1904 if (klass == NULL) {
1905 return NULL;
1906 }
1907
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001908 const char* name = dex_file.dexStringById(field_id.name_idx_);
1909 const char* type = dex_file.dexStringByTypeIdx(field_id.type_idx_);
1910 if (is_static) {
1911 resolved = klass->FindStaticField(name, type);
1912 } else {
1913 resolved = klass->FindInstanceField(name, type);
1914 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001915 if (resolved != NULL) {
1916 dex_cache->SetResolvedfield(field_idx, resolved);
1917 } else {
1918 // DCHECK(Thread::Current()->IsExceptionPending());
1919 }
1920 return resolved;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001921}
1922
Elliott Hughese27955c2011-08-26 15:21:24 -07001923size_t ClassLinker::NumLoadedClasses() const {
1924 MutexLock mu(classes_lock_);
1925 return classes_.size();
1926}
1927
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001928} // namespace art