Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5 | #include <string> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 7 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 9 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "casts.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 11 | #include "class_loader.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 12 | #include "dex_cache.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 13 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 14 | #include "dex_verifier.h" |
| 15 | #include "heap.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 16 | #include "intern_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "logging.h" |
| 18 | #include "monitor.h" |
| 19 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 20 | #include "runtime.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 21 | #include "space.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 22 | #include "thread.h" |
| 23 | #include "utils.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 27 | const char* ClassLinker::class_roots_descriptors_[kClassRootsMax] = { |
| 28 | "Ljava/lang/Class;", |
| 29 | "Ljava/lang/Object;", |
| 30 | "[Ljava/lang/Object;", |
| 31 | "Ljava/lang/String;", |
| 32 | "Ljava/lang/reflect/Field;", |
| 33 | "Ljava/lang/reflect/Method;", |
| 34 | "Ljava/lang/ClassLoader;", |
| 35 | "Ldalvik/system/BaseDexClassLoader;", |
| 36 | "Ldalvik/system/PathClassLoader;", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 37 | "Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 38 | "Z", |
| 39 | "B", |
| 40 | "C", |
| 41 | "D", |
| 42 | "F", |
| 43 | "I", |
| 44 | "J", |
| 45 | "S", |
| 46 | "V", |
| 47 | "[Z", |
| 48 | "[B", |
| 49 | "[C", |
| 50 | "[D", |
| 51 | "[F", |
| 52 | "[I", |
| 53 | "[J", |
| 54 | "[S", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 55 | "[Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 58 | ClassLinker* ClassLinker::Create(const std::vector<const DexFile*>& boot_class_path, |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 59 | const std::vector<const DexFile*>& class_path, |
| 60 | InternTable* intern_table, Space* space) { |
| 61 | CHECK_NE(boot_class_path.size(), 0U); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 62 | UniquePtr<ClassLinker> class_linker(new ClassLinker(intern_table)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 63 | if (space == NULL) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 64 | class_linker->Init(boot_class_path, class_path); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 65 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 66 | class_linker->Init(boot_class_path, class_path, space); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 67 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 68 | // TODO: check for failure during initialization |
| 69 | return class_linker.release(); |
| 70 | } |
| 71 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 72 | ClassLinker::ClassLinker(InternTable* intern_table) |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 73 | : classes_lock_(Mutex::Create("ClassLinker::Lock")), |
| 74 | class_roots_(NULL), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 75 | array_interfaces_(NULL), |
| 76 | array_iftable_(NULL), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 77 | init_done_(false), |
| 78 | intern_table_(intern_table) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 79 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 80 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 81 | void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path, |
| 82 | const std::vector<const DexFile*>& class_path) { |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 83 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 84 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 85 | // java_lang_Class comes first, its needed for AllocClass |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 86 | Class* java_lang_Class = down_cast<Class*>( |
| 87 | Heap::AllocObject(NULL, sizeof(ClassClass))); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 88 | CHECK(java_lang_Class != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 89 | java_lang_Class->SetClass(java_lang_Class); |
| 90 | java_lang_Class->SetClassSize(sizeof(ClassClass)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 91 | // AllocClass(Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 92 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 93 | // java_lang_Object comes next so that object_array_class can be created |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 94 | Class* java_lang_Object = AllocClass(java_lang_Class, sizeof(Class)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 95 | CHECK(java_lang_Object != NULL); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 96 | // backfill Object as the super class of Class |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 97 | java_lang_Class->SetSuperClass(java_lang_Object); |
| 98 | java_lang_Object->SetStatus(Class::kStatusLoaded); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 99 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 100 | // Object[] next to hold class roots |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 101 | Class* object_array_class = AllocClass(java_lang_Class, sizeof(Class)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 102 | object_array_class->SetArrayRank(1); |
| 103 | object_array_class->SetComponentType(java_lang_Object); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 104 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 105 | |
| 106 | // Setup the char[] class to be used for String |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 107 | Class* char_array_class = AllocClass(java_lang_Class, sizeof(Class)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 108 | char_array_class->SetArrayRank(1); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 109 | CharArray::SetArrayClass(char_array_class); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 110 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 111 | // Setup String |
| 112 | Class* java_lang_String = AllocClass(java_lang_Class, sizeof(StringClass)); |
| 113 | String::SetClass(java_lang_String); |
| 114 | java_lang_String->SetObjectSize(sizeof(String)); |
| 115 | java_lang_String->SetStatus(Class::kStatusResolved); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 116 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 117 | // Backfill Class descriptors missing until this point |
| 118 | // TODO: intern these strings |
| 119 | java_lang_Class->SetDescriptor( |
| 120 | String::AllocFromModifiedUtf8("Ljava/lang/Class;")); |
| 121 | java_lang_Object->SetDescriptor( |
| 122 | String::AllocFromModifiedUtf8("Ljava/lang/Object;")); |
| 123 | object_array_class->SetDescriptor( |
| 124 | String::AllocFromModifiedUtf8("[Ljava/lang/Object;")); |
| 125 | java_lang_String->SetDescriptor( |
| 126 | String::AllocFromModifiedUtf8("Ljava/lang/String;")); |
| 127 | char_array_class->SetDescriptor(String::AllocFromModifiedUtf8("[C")); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 128 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 129 | // Create storage for root classes, save away our work so far (requires |
| 130 | // descriptors) |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 131 | class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 132 | SetClassRoot(kJavaLangClass, java_lang_Class); |
| 133 | SetClassRoot(kJavaLangObject, java_lang_Object); |
| 134 | SetClassRoot(kObjectArrayClass, object_array_class); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 135 | SetClassRoot(kCharArrayClass, char_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 136 | SetClassRoot(kJavaLangString, java_lang_String); |
| 137 | |
| 138 | // Setup the primitive type classes. |
| 139 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z", Class::kPrimBoolean)); |
| 140 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B", Class::kPrimByte)); |
| 141 | SetClassRoot(kPrimitiveChar, CreatePrimitiveClass("C", Class::kPrimChar)); |
| 142 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S", Class::kPrimShort)); |
| 143 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I", Class::kPrimInt)); |
| 144 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J", Class::kPrimLong)); |
| 145 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F", Class::kPrimFloat)); |
| 146 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D", Class::kPrimDouble)); |
| 147 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V", Class::kPrimVoid)); |
| 148 | |
| 149 | // Backfill component type of char[] |
| 150 | char_array_class->SetComponentType(GetClassRoot(kPrimitiveChar)); |
| 151 | |
| 152 | // Create array interface entries to populate once we can load system classes |
| 153 | array_interfaces_ = AllocObjectArray<Class>(2); |
| 154 | array_iftable_ = new InterfaceEntry[2]; |
| 155 | |
| 156 | // Create int array type for AllocDexCache (done in AppendToBootClassPath) |
| 157 | Class* int_array_class = AllocClass(java_lang_Class, sizeof(Class)); |
| 158 | int_array_class->SetArrayRank(1); |
| 159 | int_array_class->SetDescriptor(String::AllocFromModifiedUtf8("[I")); |
| 160 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
| 161 | IntArray::SetArrayClass(int_array_class); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 162 | SetClassRoot(kIntArrayClass, int_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 163 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 164 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 165 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 166 | // setup boot_class_path_ and register class_path now that we can |
| 167 | // use AllocObjectArray to create DexCache instances |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 168 | for (size_t i = 0; i != boot_class_path.size(); ++i) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 169 | const DexFile* dex_file = boot_class_path[i]; |
| 170 | CHECK(dex_file != NULL); |
| 171 | AppendToBootClassPath(*dex_file); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 172 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 173 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 174 | const DexFile* dex_file = class_path[i]; |
| 175 | CHECK(dex_file != NULL); |
| 176 | RegisterDexFile(*dex_file); |
| 177 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 178 | |
| 179 | // Field and Method are necessary so that FindClass can link members |
| 180 | Class* java_lang_reflect_Field = AllocClass(java_lang_Class, sizeof(FieldClass)); |
| 181 | CHECK(java_lang_reflect_Field != NULL); |
| 182 | java_lang_reflect_Field->SetDescriptor(String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;")); |
| 183 | java_lang_reflect_Field->SetObjectSize(sizeof(Field)); |
| 184 | SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field); |
| 185 | java_lang_reflect_Field->SetStatus(Class::kStatusResolved); |
| 186 | Field::SetClass(java_lang_reflect_Field); |
| 187 | |
| 188 | Class* java_lang_reflect_Method = AllocClass(java_lang_Class, sizeof(MethodClass)); |
| 189 | java_lang_reflect_Method->SetDescriptor(String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;")); |
| 190 | CHECK(java_lang_reflect_Method != NULL); |
| 191 | java_lang_reflect_Method->SetObjectSize(sizeof(Method)); |
| 192 | SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method); |
| 193 | java_lang_reflect_Method->SetStatus(Class::kStatusResolved); |
| 194 | Method::SetClass(java_lang_reflect_Method); |
| 195 | |
| 196 | // now we can use FindSystemClass |
| 197 | |
| 198 | // Object and String just need more minimal setup, since they do not have |
| 199 | // extra C++ fields. |
| 200 | java_lang_Object->SetStatus(Class::kStatusNotReady); |
| 201 | Class* Object_class = FindSystemClass("Ljava/lang/Object;"); |
| 202 | CHECK_EQ(java_lang_Object, Object_class); |
| 203 | CHECK_EQ(java_lang_Object->GetObjectSize(), sizeof(Object)); |
| 204 | java_lang_String->SetStatus(Class::kStatusNotReady); |
| 205 | Class* String_class = FindSystemClass("Ljava/lang/String;"); |
| 206 | CHECK_EQ(java_lang_String, String_class); |
| 207 | CHECK_EQ(java_lang_String->GetObjectSize(), sizeof(String)); |
| 208 | |
| 209 | // Setup the primitive array type classes - can't be done until Object has |
| 210 | // a vtable |
| 211 | SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z")); |
| 212 | BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 213 | |
| 214 | SetClassRoot(kByteArrayClass, FindSystemClass("[B")); |
| 215 | ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 216 | |
| 217 | Class* found_char_array_class = FindSystemClass("[C"); |
| 218 | CHECK_EQ(char_array_class, found_char_array_class); |
| 219 | |
| 220 | SetClassRoot(kShortArrayClass, FindSystemClass("[S")); |
| 221 | ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 222 | |
| 223 | Class* found_int_array_class = FindSystemClass("[I"); |
| 224 | CHECK_EQ(int_array_class, found_int_array_class); |
| 225 | |
| 226 | SetClassRoot(kLongArrayClass, FindSystemClass("[J")); |
| 227 | LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 228 | |
| 229 | SetClassRoot(kFloatArrayClass, FindSystemClass("[F")); |
| 230 | FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 231 | |
| 232 | SetClassRoot(kDoubleArrayClass, FindSystemClass("[D")); |
| 233 | DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 234 | |
| 235 | Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;"); |
| 236 | CHECK_EQ(object_array_class, found_object_array_class); |
| 237 | |
| 238 | // Setup the single, global copies of "interfaces" and "iftable" |
| 239 | Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;"); |
| 240 | CHECK(java_lang_Cloneable != NULL); |
| 241 | Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;"); |
| 242 | CHECK(java_io_Serializable != NULL); |
| 243 | CHECK(array_interfaces_ != NULL); |
| 244 | array_interfaces_->Set(0, java_lang_Cloneable); |
| 245 | array_interfaces_->Set(1, java_io_Serializable); |
| 246 | // We assume that Cloneable/Serializable don't have superinterfaces -- |
| 247 | // normally we'd have to crawl up and explicitly list all of the |
| 248 | // supers as well. These interfaces don't have any methods, so we |
| 249 | // don't have to worry about the ifviPool either. |
| 250 | array_iftable_[0].SetInterface(array_interfaces_->Get(0)); |
| 251 | array_iftable_[1].SetInterface(array_interfaces_->Get(1)); |
| 252 | |
| 253 | // Sanity check Object[]'s interfaces |
| 254 | CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0)); |
| 255 | CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1)); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 256 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 257 | // run Class, Field, and Method through FindSystemClass. |
| 258 | // this initializes their dex_cache_ fields and register them in classes_. |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 259 | // we also override their object_size_ values to accommodate the extra C++ fields. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 260 | Class* Class_class = FindSystemClass("Ljava/lang/Class;"); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 261 | CHECK_EQ(java_lang_Class, Class_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 262 | // No sanity check on size as Class is variably sized |
| 263 | |
| 264 | java_lang_reflect_Field->SetStatus(Class::kStatusNotReady); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 265 | Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;"); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 266 | CHECK_EQ(java_lang_reflect_Field, Field_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 267 | CHECK_LT(java_lang_reflect_Field->GetObjectSize(), sizeof(Field)); |
| 268 | java_lang_reflect_Field->SetObjectSize(sizeof(Field)); |
| 269 | |
| 270 | java_lang_reflect_Method->SetStatus(Class::kStatusNotReady); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 271 | Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;"); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 272 | CHECK_EQ(java_lang_reflect_Method, Method_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 273 | CHECK_LT(java_lang_reflect_Method->GetObjectSize(), sizeof(Method)); |
| 274 | java_lang_reflect_Method->SetObjectSize(sizeof(Method)); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 275 | |
| 276 | // java.lang.ref classes need to be specially flagged, but otherwise are normal classes |
| 277 | Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 278 | java_lang_ref_FinalizerReference->SetAccessFlags( |
| 279 | java_lang_ref_FinalizerReference->GetAccessFlags() | |
| 280 | kAccClassIsReference | kAccClassIsFinalizerReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 281 | Class* java_lang_ref_PhantomReference = FindSystemClass("Ljava/lang/ref/PhantomReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 282 | java_lang_ref_PhantomReference->SetAccessFlags( |
| 283 | java_lang_ref_PhantomReference->GetAccessFlags() | |
| 284 | kAccClassIsReference | kAccClassIsPhantomReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 285 | Class* java_lang_ref_SoftReference = FindSystemClass("Ljava/lang/ref/SoftReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 286 | java_lang_ref_SoftReference->SetAccessFlags( |
| 287 | java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 288 | Class* java_lang_ref_WeakReference = FindSystemClass("Ljava/lang/ref/WeakReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 289 | java_lang_ref_WeakReference->SetAccessFlags( |
| 290 | java_lang_ref_WeakReference->GetAccessFlags() | |
| 291 | kAccClassIsReference | kAccClassIsWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 292 | |
| 293 | // Let the heap know some key offsets into java.lang.ref instances |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 294 | // NB we hard code the field indexes here rather than using FindInstanceField |
| 295 | // as the types of the field can't be resolved prior to the runtime being |
| 296 | // fully initialized |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 297 | Class* java_lang_ref_Reference = FindSystemClass("Ljava/lang/ref/Reference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 298 | |
| 299 | Field* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
| 300 | CHECK(pendingNext->GetName()->Equals("pendingNext")); |
| 301 | CHECK(ResolveType(pendingNext->GetTypeIdx(), pendingNext) == |
| 302 | java_lang_ref_Reference); |
| 303 | |
| 304 | Field* queue = java_lang_ref_Reference->GetInstanceField(1); |
| 305 | CHECK(queue->GetName()->Equals("queue")); |
| 306 | CHECK(ResolveType(queue->GetTypeIdx(), queue) == |
| 307 | FindSystemClass("Ljava/lang/ref/ReferenceQueue;")); |
| 308 | |
| 309 | Field* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
| 310 | CHECK(queueNext->GetName()->Equals("queueNext")); |
| 311 | CHECK(ResolveType(queueNext->GetTypeIdx(), queueNext) == |
| 312 | java_lang_ref_Reference); |
| 313 | |
| 314 | Field* referent = java_lang_ref_Reference->GetInstanceField(3); |
| 315 | CHECK(referent->GetName()->Equals("referent")); |
| 316 | CHECK(ResolveType(referent->GetTypeIdx(), referent) == |
| 317 | java_lang_Object); |
| 318 | |
| 319 | Field* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
| 320 | CHECK(zombie->GetName()->Equals("zombie")); |
| 321 | CHECK(ResolveType(zombie->GetTypeIdx(), zombie) == |
| 322 | java_lang_Object); |
| 323 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 324 | Heap::SetReferenceOffsets(referent->GetOffset(), |
| 325 | queue->GetOffset(), |
| 326 | queueNext->GetOffset(), |
| 327 | pendingNext->GetOffset(), |
| 328 | zombie->GetOffset()); |
| 329 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 330 | // Setup the ClassLoaders, adjusting the object_size_ as necessary |
| 331 | Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;"); |
| 332 | CHECK_LT(java_lang_ClassLoader->GetObjectSize(), sizeof(ClassLoader)); |
| 333 | java_lang_ClassLoader->SetObjectSize(sizeof(ClassLoader)); |
| 334 | SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader); |
| 335 | |
| 336 | Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;"); |
| 337 | CHECK_EQ(dalvik_system_BaseDexClassLoader->GetObjectSize(), sizeof(BaseDexClassLoader)); |
| 338 | SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader); |
| 339 | |
| 340 | Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;"); |
| 341 | CHECK_EQ(dalvik_system_PathClassLoader->GetObjectSize(), sizeof(PathClassLoader)); |
| 342 | SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader); |
| 343 | PathClassLoader::SetClass(dalvik_system_PathClassLoader); |
| 344 | |
| 345 | // Set up java.lang.StackTraceElement as a convenience |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 346 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass("Ljava/lang/StackTraceElement;")); |
| 347 | SetClassRoot(kJavaLangStackTraceElementArrayClass, FindSystemClass("[Ljava/lang/StackTraceElement;")); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 348 | StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 349 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 350 | FinishInit(); |
| 351 | } |
| 352 | |
| 353 | void ClassLinker::FinishInit() { |
| 354 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 355 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 356 | ClassRoot class_root = static_cast<ClassRoot>(i); |
| 357 | Class* klass = GetClassRoot(class_root); |
| 358 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 359 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != NULL); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 360 | // note SetClassRoot does additional validation. |
| 361 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 365 | // that Object, Class, and Object[] are setup |
| 366 | init_done_ = true; |
| 367 | } |
| 368 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 369 | struct ClassLinker::InitCallbackState { |
| 370 | ClassLinker* class_linker; |
| 371 | |
| 372 | Class* class_roots[kClassRootsMax]; |
| 373 | |
| 374 | typedef std::tr1::unordered_map<std::string, ClassRoot> Table; |
| 375 | Table descriptor_to_class_root; |
| 376 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 377 | typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set; |
| 378 | Set dex_caches; |
| 379 | }; |
| 380 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 381 | void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path, |
| 382 | const std::vector<const DexFile*>& class_path, |
| 383 | Space* space) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 384 | CHECK(!init_done_); |
| 385 | |
| 386 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 387 | DCHECK(heap_bitmap != NULL); |
| 388 | |
| 389 | InitCallbackState state; |
| 390 | state.class_linker = this; |
| 391 | for (size_t i = 0; i < kClassRootsMax; i++) { |
| 392 | ClassRoot class_root = static_cast<ClassRoot>(i); |
| 393 | state.descriptor_to_class_root[GetClassRootDescriptor(class_root)] = class_root; |
| 394 | } |
| 395 | |
| 396 | // reinit clases_ table |
| 397 | heap_bitmap->Walk(InitCallback, &state); |
| 398 | |
| 399 | // reinit class_roots_ |
| 400 | Class* object_array_class = state.class_roots[kObjectArrayClass]; |
| 401 | class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax); |
| 402 | for (size_t i = 0; i < kClassRootsMax; i++) { |
| 403 | ClassRoot class_root = static_cast<ClassRoot>(i); |
| 404 | SetClassRoot(class_root, state.class_roots[class_root]); |
| 405 | } |
| 406 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 407 | // reinit intern table |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 408 | // TODO: remove interned_array, make all strings in image interned (and remove space argument) |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 409 | ObjectArray<Object>* interned_array = space->GetImageHeader().GetInternedArray(); |
| 410 | for (int32_t i = 0; i < interned_array->GetLength(); i++) { |
| 411 | String* string = interned_array->Get(i)->AsString(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 412 | intern_table_->RegisterStrong(string); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // reinit array_interfaces_ from any array class instance, they should all be == |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 416 | array_interfaces_ = GetClassRoot(kObjectArrayClass)->GetInterfaces(); |
| 417 | DCHECK(array_interfaces_ == GetClassRoot(kBooleanArrayClass)->GetInterfaces()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 418 | |
| 419 | // build a map from location to DexCache to match up with DexFile::GetLocation |
| 420 | std::tr1::unordered_map<std::string, DexCache*> location_to_dex_cache; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 421 | typedef InitCallbackState::Set::const_iterator It; // TODO: C++0x auto |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 422 | for (It it = state.dex_caches.begin(), end = state.dex_caches.end(); it != end; ++it) { |
| 423 | DexCache* dex_cache = *it; |
| 424 | std::string location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 425 | location_to_dex_cache[location] = dex_cache; |
| 426 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 427 | CHECK_EQ(boot_class_path.size() + class_path.size(), |
| 428 | location_to_dex_cache.size()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 429 | |
| 430 | // reinit boot_class_path with DexFile arguments and found DexCaches |
| 431 | for (size_t i = 0; i != boot_class_path.size(); ++i) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 432 | const DexFile* dex_file = boot_class_path[i]; |
| 433 | CHECK(dex_file != NULL); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 434 | DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()]; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 435 | CHECK(dex_cache != NULL) << dex_file->GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 436 | AppendToBootClassPath(*dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 437 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 438 | |
| 439 | // register class_path with DexFile arguments and found DexCaches |
| 440 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 441 | const DexFile* dex_file = class_path[i]; |
| 442 | CHECK(dex_file != NULL); |
| 443 | DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()]; |
| 444 | CHECK(dex_cache != NULL) << dex_file->GetLocation(); |
| 445 | RegisterDexFile(*dex_file, dex_cache); |
| 446 | } |
| 447 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 448 | String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 449 | Field::SetClass(GetClassRoot(kJavaLangReflectField)); |
| 450 | Method::SetClass(GetClassRoot(kJavaLangReflectMethod)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 451 | BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 452 | ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 453 | CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 454 | DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 455 | FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 456 | IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 457 | LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 458 | ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 459 | PathClassLoader::SetClass(GetClassRoot(kDalvikSystemPathClassLoader)); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 460 | StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 461 | |
| 462 | FinishInit(); |
| 463 | } |
| 464 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 465 | void ClassLinker::InitCallback(Object* obj, void *arg) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 466 | DCHECK(obj != NULL); |
| 467 | DCHECK(arg != NULL); |
| 468 | InitCallbackState* state = reinterpret_cast<InitCallbackState*>(arg); |
| 469 | |
| 470 | if (!obj->IsClass()) { |
| 471 | return; |
| 472 | } |
| 473 | Class* klass = obj->AsClass(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 474 | // TODO: restore ClassLoader's list of DexFiles after image load |
| 475 | // CHECK(klass->GetClassLoader() == NULL); |
| 476 | const ClassLoader* class_loader = klass->GetClassLoader(); |
| 477 | if (class_loader != NULL) { |
| 478 | // TODO: replace this hack with something based on command line arguments |
| 479 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 480 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 481 | |
| 482 | std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 483 | // restore class to ClassLinker::classes_ table |
| 484 | state->class_linker->InsertClass(descriptor, klass); |
| 485 | |
| 486 | // note DexCache to match with DexFile later |
| 487 | DexCache* dex_cache = klass->GetDexCache(); |
| 488 | if (dex_cache != NULL) { |
| 489 | state->dex_caches.insert(dex_cache); |
| 490 | } else { |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 491 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // check if this is a root, if so, register it |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 495 | typedef InitCallbackState::Table::const_iterator It; // TODO: C++0x auto |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 496 | It it = state->descriptor_to_class_root.find(descriptor); |
| 497 | if (it != state->descriptor_to_class_root.end()) { |
| 498 | ClassRoot class_root = it->second; |
| 499 | state->class_roots[class_root] = klass; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 504 | // reinit references to when reinitializing a ClassLinker from a |
| 505 | // mapped image. |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 506 | void ClassLinker::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 507 | visitor(class_roots_, arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 508 | |
| 509 | for (size_t i = 0; i < dex_caches_.size(); i++) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 510 | visitor(dex_caches_[i], arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 513 | { |
| 514 | MutexLock mu(classes_lock_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 515 | typedef Table::const_iterator It; // TODO: C++0x auto |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 516 | for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 517 | visitor(it->second, arg); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 518 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 519 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 520 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 521 | visitor(array_interfaces_, arg); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 524 | ClassLinker::~ClassLinker() { |
| 525 | delete classes_lock_; |
| 526 | String::ResetClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 527 | Field::ResetClass(); |
| 528 | Method::ResetClass(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 529 | BooleanArray::ResetArrayClass(); |
| 530 | ByteArray::ResetArrayClass(); |
| 531 | CharArray::ResetArrayClass(); |
| 532 | DoubleArray::ResetArrayClass(); |
| 533 | FloatArray::ResetArrayClass(); |
| 534 | IntArray::ResetArrayClass(); |
| 535 | LongArray::ResetArrayClass(); |
| 536 | ShortArray::ResetArrayClass(); |
| 537 | PathClassLoader::ResetClass(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 538 | StackTraceElement::ResetClass(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | DexCache* ClassLinker::AllocDexCache(const DexFile& dex_file) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 542 | DexCache* dex_cache = down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::LengthAsArray())); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 543 | dex_cache->Init(String::AllocFromModifiedUtf8(dex_file.GetLocation().c_str()), |
| 544 | AllocObjectArray<String>(dex_file.NumStringIds()), |
| 545 | AllocObjectArray<Class>(dex_file.NumTypeIds()), |
| 546 | AllocObjectArray<Method>(dex_file.NumMethodIds()), |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 547 | AllocObjectArray<Field>(dex_file.NumFieldIds()), |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 548 | AllocCodeAndDirectMethods(dex_file.NumMethodIds()), |
| 549 | AllocObjectArray<StaticStorageBase>(dex_file.NumTypeIds())); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 550 | return dex_cache; |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 553 | CodeAndDirectMethods* ClassLinker::AllocCodeAndDirectMethods(size_t length) { |
| 554 | return down_cast<CodeAndDirectMethods*>(IntArray::Alloc(CodeAndDirectMethods::LengthAsArray(length))); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 557 | Class* ClassLinker::AllocClass(Class* java_lang_Class, size_t class_size) { |
| 558 | DCHECK_GE(class_size, sizeof(Class)); |
| 559 | Class* klass = Heap::AllocObject(java_lang_Class, class_size)->AsClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 560 | klass->SetPrimitiveType(Class::kPrimNot); // default to not being primitive |
| 561 | klass->SetClassSize(class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 562 | return klass; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 565 | Class* ClassLinker::AllocClass(size_t class_size) { |
| 566 | return AllocClass(GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 569 | Field* ClassLinker::AllocField() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 570 | return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->AllocObject()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | Method* ClassLinker::AllocMethod() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 574 | return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->AllocObject()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 577 | ObjectArray<StackTraceElement>* ClassLinker::AllocStackTraceElementArray(size_t length) { |
| 578 | return ObjectArray<StackTraceElement>::Alloc( |
| 579 | GetClassRoot(kJavaLangStackTraceElementArrayClass), |
| 580 | length); |
| 581 | } |
| 582 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 583 | Class* ClassLinker::FindClass(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 584 | const ClassLoader* class_loader) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 585 | // TODO: remove this contrived parent class loader check when we have a real ClassLoader. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 586 | if (class_loader != NULL) { |
| 587 | Class* klass = FindClass(descriptor, NULL); |
| 588 | if (klass != NULL) { |
| 589 | return klass; |
| 590 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 591 | Thread::Current()->ClearException(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 594 | Thread* self = Thread::Current(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 595 | DCHECK(self != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 596 | CHECK(!self->IsExceptionPending()); |
| 597 | // Find the class in the loaded classes table. |
| 598 | Class* klass = LookupClass(descriptor, class_loader); |
| 599 | if (klass == NULL) { |
| 600 | // Class is not yet loaded. |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 601 | if (descriptor[0] == '[') { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 602 | return CreateArrayClass(descriptor, class_loader); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 603 | } |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 604 | const DexFile::ClassPath& class_path = ((class_loader != NULL) |
| 605 | ? ClassLoader::GetClassPath(class_loader) |
| 606 | : boot_class_path_); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 607 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 608 | if (pair.second == NULL) { |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 609 | std::string name(PrintableString(descriptor)); |
| 610 | self->ThrowNewException("Ljava/lang/NoClassDefFoundError;", |
| 611 | "Class %s not found in class loader %p", name.c_str(), class_loader); |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 612 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 613 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 614 | const DexFile& dex_file = *pair.first; |
| 615 | const DexFile::ClassDef& dex_class_def = *pair.second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 616 | DexCache* dex_cache = FindDexCache(dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 617 | // Load the class from the dex file. |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 618 | if (!init_done_) { |
| 619 | // finish up init of hand crafted class_roots_ |
| 620 | if (descriptor == "Ljava/lang/Object;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 621 | klass = GetClassRoot(kJavaLangObject); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 622 | } else if (descriptor == "Ljava/lang/Class;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 623 | klass = GetClassRoot(kJavaLangClass); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 624 | } else if (descriptor == "Ljava/lang/String;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 625 | klass = GetClassRoot(kJavaLangString); |
| 626 | } else if (descriptor == "Ljava/lang/reflect/Field;") { |
| 627 | klass = GetClassRoot(kJavaLangReflectField); |
| 628 | } else if (descriptor == "Ljava/lang/reflect/Method;") { |
| 629 | klass = GetClassRoot(kJavaLangReflectMethod); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 630 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 631 | klass = AllocClass(SizeOfClass(dex_file, dex_class_def)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 632 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 633 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 634 | klass = AllocClass(SizeOfClass(dex_file, dex_class_def)); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 635 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 636 | if (!klass->IsLinked()) { |
| 637 | klass->SetDexCache(dex_cache); |
| 638 | LoadClass(dex_file, dex_class_def, klass, class_loader); |
| 639 | // Check for a pending exception during load |
| 640 | if (self->IsExceptionPending()) { |
| 641 | // TODO: free native allocations in klass |
| 642 | return NULL; |
| 643 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 644 | ObjectLock lock(klass); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 645 | klass->SetClinitThreadId(self->GetTid()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 646 | // Add the newly loaded class to the loaded classes table. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 647 | bool success = InsertClass(descriptor, klass); // TODO: just return collision |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 648 | if (!success) { |
| 649 | // We may fail to insert if we raced with another thread. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 650 | klass->SetClinitThreadId(0); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 651 | // TODO: free native allocations in klass |
| 652 | klass = LookupClass(descriptor, class_loader); |
| 653 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 654 | return klass; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 655 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 656 | // Finish loading (if necessary) by finding parents |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 657 | CHECK(!klass->IsLoaded()); |
| 658 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 659 | // Loading failed. |
| 660 | // TODO: CHECK(self->IsExceptionPending()); |
| 661 | lock.NotifyAll(); |
| 662 | return NULL; |
| 663 | } |
| 664 | CHECK(klass->IsLoaded()); |
| 665 | // Link the class (if necessary) |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 666 | CHECK(!klass->IsLinked()); |
| 667 | if (!LinkClass(klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 668 | // Linking failed. |
| 669 | // TODO: CHECK(self->IsExceptionPending()); |
| 670 | lock.NotifyAll(); |
| 671 | return NULL; |
| 672 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 673 | CHECK(klass->IsLinked()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | } |
| 677 | // Link the class if it has not already been linked. |
| 678 | if (!klass->IsLinked() && !klass->IsErroneous()) { |
| 679 | ObjectLock lock(klass); |
| 680 | // Check for circular dependencies between classes. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 681 | if (!klass->IsLinked() && klass->GetClinitThreadId() == self->GetTid()) { |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 682 | self->ThrowNewException("Ljava/lang/ClassCircularityError;", NULL); // TODO: detail |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 683 | return NULL; |
| 684 | } |
| 685 | // Wait for the pending initialization to complete. |
| 686 | while (!klass->IsLinked() && !klass->IsErroneous()) { |
| 687 | lock.Wait(); |
| 688 | } |
| 689 | } |
| 690 | if (klass->IsErroneous()) { |
| 691 | LG << "EarlierClassFailure"; // TODO: EarlierClassFailure |
| 692 | return NULL; |
| 693 | } |
| 694 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 695 | CHECK(klass->IsLinked()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 696 | CHECK(!self->IsExceptionPending()); |
| 697 | return klass; |
| 698 | } |
| 699 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 700 | // Precomputes size that will be needed for Class, matching LinkStaticFields |
| 701 | size_t ClassLinker::SizeOfClass(const DexFile& dex_file, |
| 702 | const DexFile::ClassDef& dex_class_def) { |
| 703 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
| 704 | DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data); |
| 705 | size_t num_static_fields = header.static_fields_size_; |
| 706 | size_t num_ref = 0; |
| 707 | size_t num_32 = 0; |
| 708 | size_t num_64 = 0; |
| 709 | if (num_static_fields != 0) { |
| 710 | uint32_t last_idx = 0; |
| 711 | for (size_t i = 0; i < num_static_fields; ++i) { |
| 712 | DexFile::Field dex_field; |
| 713 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
| 714 | const DexFile::FieldId& field_id = dex_file.GetFieldId(dex_field.field_idx_); |
| 715 | const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_); |
| 716 | char c = descriptor[0]; |
| 717 | if (c == 'L' || c == '[') { |
| 718 | num_ref++; |
| 719 | } else if (c == 'J' || c == 'D') { |
| 720 | num_64++; |
| 721 | } else { |
| 722 | num_32++; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | // start with generic class data |
| 728 | size_t size = sizeof(Class); |
| 729 | // follow with reference fields which must be contiguous at start |
| 730 | size += (num_ref * sizeof(uint32_t)); |
| 731 | // if there are 64-bit fields to add, make sure they are aligned |
| 732 | if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment |
| 733 | if (num_32 != 0) { |
| 734 | // use an available 32-bit field for padding |
| 735 | num_32--; |
| 736 | } |
| 737 | size += sizeof(uint32_t); // either way, we are adding a word |
| 738 | DCHECK_EQ(size, RoundUp(size, 8)); |
| 739 | } |
| 740 | // tack on any 64-bit fields now that alignment is assured |
| 741 | size += (num_64 * sizeof(uint64_t)); |
| 742 | // tack on any remaining 32-bit fields |
| 743 | size += (num_32 * sizeof(uint32_t)); |
| 744 | return size; |
| 745 | } |
| 746 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 747 | void ClassLinker::LoadClass(const DexFile& dex_file, |
| 748 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 749 | Class* klass, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 750 | const ClassLoader* class_loader) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 751 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 752 | CHECK(klass->GetDexCache() != NULL); |
| 753 | CHECK_EQ(Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 754 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
| 755 | DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 756 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 757 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 758 | CHECK(descriptor != NULL); |
| 759 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 760 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
| 761 | if (klass->GetDescriptor() != NULL) { |
| 762 | DCHECK(klass->GetDescriptor()->Equals(descriptor)); |
| 763 | } else { |
| 764 | klass->SetDescriptor(String::AllocFromModifiedUtf8(descriptor)); |
| 765 | } |
| 766 | uint32_t access_flags = dex_class_def.access_flags_; |
| 767 | // Make sure there aren't any "bonus" flags set, since we use them for runtime |
| 768 | // state. |
| 769 | CHECK_EQ(access_flags & ~kAccClassFlagsMask, 0U); |
| 770 | klass->SetAccessFlags(access_flags); |
| 771 | klass->SetClassLoader(class_loader); |
| 772 | DCHECK(klass->GetPrimitiveType() == Class::kPrimNot); |
| 773 | klass->SetStatus(Class::kStatusIdx); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 774 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 775 | klass->SetSuperClassTypeIdx(dex_class_def.superclass_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 776 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 777 | size_t num_static_fields = header.static_fields_size_; |
| 778 | size_t num_instance_fields = header.instance_fields_size_; |
| 779 | size_t num_direct_methods = header.direct_methods_size_; |
| 780 | size_t num_virtual_methods = header.virtual_methods_size_; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 781 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 782 | klass->SetSourceFile(dex_file.dexGetSourceFile(dex_class_def)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 783 | |
| 784 | // Load class interfaces. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 785 | LoadInterfaces(dex_file, dex_class_def, klass); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 786 | |
| 787 | // Load static fields. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 788 | if (num_static_fields != 0) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 789 | klass->SetSFields(AllocObjectArray<Field>(num_static_fields)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 790 | uint32_t last_idx = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 791 | for (size_t i = 0; i < num_static_fields; ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 792 | DexFile::Field dex_field; |
| 793 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 794 | Field* sfield = AllocField(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 795 | klass->SetStaticField(i, sfield); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 796 | LoadField(dex_file, dex_field, klass, sfield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
| 800 | // Load instance fields. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 801 | if (num_instance_fields != 0) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 802 | klass->SetIFields(AllocObjectArray<Field>(num_instance_fields)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 803 | uint32_t last_idx = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 804 | for (size_t i = 0; i < num_instance_fields; ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 805 | DexFile::Field dex_field; |
| 806 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 807 | Field* ifield = AllocField(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 808 | klass->SetInstanceField(i, ifield); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 809 | LoadField(dex_file, dex_field, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | // Load direct methods. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 814 | if (num_direct_methods != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 815 | // TODO: append direct methods to class object |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 816 | klass->SetDirectMethods(AllocObjectArray<Method>(num_direct_methods)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 817 | uint32_t last_idx = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 818 | for (size_t i = 0; i < num_direct_methods; ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 819 | DexFile::Method dex_method; |
| 820 | dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 821 | Method* meth = AllocMethod(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 822 | klass->SetDirectMethod(i, meth); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 823 | LoadMethod(dex_file, dex_method, klass, meth); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 824 | // TODO: register maps |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | // Load virtual methods. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 829 | if (num_virtual_methods != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 830 | // TODO: append virtual methods to class object |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 831 | klass->SetVirtualMethods(AllocObjectArray<Method>(num_virtual_methods)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 832 | uint32_t last_idx = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 833 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 834 | DexFile::Method dex_method; |
| 835 | dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 836 | Method* meth = AllocMethod(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 837 | klass->SetVirtualMethod(i, meth); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 838 | LoadMethod(dex_file, dex_method, klass, meth); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 839 | // TODO: register maps |
| 840 | } |
| 841 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 844 | void ClassLinker::LoadInterfaces(const DexFile& dex_file, |
| 845 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 846 | Class* klass) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 847 | const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 848 | if (list != NULL) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 849 | klass->SetInterfaces(AllocObjectArray<Class>(list->Size())); |
| 850 | IntArray* interfaces_idx = IntArray::Alloc(list->Size()); |
| 851 | klass->SetInterfacesTypeIdx(interfaces_idx); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 852 | for (size_t i = 0; i < list->Size(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 853 | const DexFile::TypeItem& type_item = list->GetTypeItem(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 854 | interfaces_idx->Set(i, type_item.type_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 859 | void ClassLinker::LoadField(const DexFile& dex_file, |
| 860 | const DexFile::Field& src, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 861 | Class* klass, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 862 | Field* dst) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 863 | const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 864 | dst->SetDeclaringClass(klass); |
| 865 | dst->SetName(ResolveString(dex_file, field_id.name_idx_, klass->GetDexCache())); |
| 866 | dst->SetTypeIdx(field_id.type_idx_); |
| 867 | dst->SetAccessFlags(src.access_flags_); |
| 868 | |
| 869 | // In order to access primitive types using GetTypeDuringLinking we need to |
| 870 | // ensure they are resolved into the dex cache |
| 871 | const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_); |
| 872 | if (descriptor[1] == '\0') { |
| 873 | // only the descriptors of primitive types should be 1 character long |
| 874 | Class* resolved = ResolveType(dex_file, field_id.type_idx_, klass); |
| 875 | DCHECK(resolved->IsPrimitive()); |
| 876 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 879 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
| 880 | const DexFile::Method& src, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 881 | Class* klass, |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 882 | Method* dst) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 883 | const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 884 | dst->SetDeclaringClass(klass); |
| 885 | dst->SetName(ResolveString(dex_file, method_id.name_idx_, klass->GetDexCache())); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 886 | { |
| 887 | int32_t utf16_length; |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 888 | std::string utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_, &utf16_length)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 889 | dst->SetSignature(String::AllocFromModifiedUtf8(utf16_length, utf8.c_str())); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 890 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 891 | dst->SetProtoIdx(method_id.proto_idx_); |
| 892 | dst->SetCodeItemOffset(src.code_off_); |
| 893 | const char* shorty = dex_file.GetShorty(method_id.proto_idx_); |
| 894 | dst->SetShorty(shorty); |
| 895 | dst->SetAccessFlags(src.access_flags_); |
| 896 | dst->SetReturnTypeIdx(dex_file.GetProtoId(method_id.proto_idx_).return_type_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 897 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 898 | dst->SetDexCacheStrings(klass->GetDexCache()->GetStrings()); |
| 899 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes()); |
| 900 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods()); |
| 901 | dst->SetDexCacheResolvedFields(klass->GetDexCache()->GetResolvedFields()); |
| 902 | dst->SetDexCacheCodeAndDirectMethods(klass->GetDexCache()->GetCodeAndDirectMethods()); |
| 903 | dst->SetDexCacheInitializedStaticStorage(klass->GetDexCache()->GetInitializedStaticStorage()); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 904 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 905 | // TODO: check for finalize method |
| 906 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 907 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 908 | if (code_item != NULL) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 909 | dst->SetNumRegisters(code_item->registers_size_); |
| 910 | dst->SetNumIns(code_item->ins_size_); |
| 911 | dst->SetNumOuts(code_item->outs_size_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 912 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 913 | uint16_t num_args = Method::NumArgRegisters(shorty); |
| 914 | if ((src.access_flags_ & kAccStatic) != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 915 | ++num_args; |
| 916 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 917 | dst->SetNumRegisters(num_args); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 918 | // TODO: native methods |
| 919 | } |
| 920 | } |
| 921 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 922 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 923 | AppendToBootClassPath(dex_file, AllocDexCache(dex_file)); |
| 924 | } |
| 925 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 926 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 927 | CHECK(dex_cache != NULL) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 928 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 929 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 932 | void ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 933 | RegisterDexFile(dex_file, AllocDexCache(dex_file)); |
| 934 | } |
| 935 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 936 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 937 | CHECK(dex_cache != NULL) << dex_file.GetLocation(); |
| 938 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 939 | dex_files_.push_back(&dex_file); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 940 | dex_caches_.push_back(dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 943 | const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 944 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
| 945 | if (dex_caches_[i] == dex_cache) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 946 | return *dex_files_[i]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 947 | } |
| 948 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 949 | CHECK(false) << "Failed to find DexFile for DexCache " << dex_cache->GetLocation()->ToModifiedUtf8(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 950 | return *dex_files_[-1]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 953 | DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 954 | for (size_t i = 0; i != dex_files_.size(); ++i) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 955 | if (dex_files_[i] == &dex_file) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 956 | return dex_caches_[i]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 957 | } |
| 958 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 959 | CHECK(false) << "Failed to find DexCache for DexFile " << dex_file.GetLocation(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 960 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 963 | Class* ClassLinker::CreatePrimitiveClass(const char* descriptor, |
| 964 | Class::PrimitiveType type) { |
| 965 | // TODO: deduce one argument from the other |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 966 | Class* klass = AllocClass(sizeof(Class)); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 967 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 968 | klass->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
| 969 | klass->SetDescriptor(String::AllocFromModifiedUtf8(descriptor)); |
| 970 | klass->SetPrimitiveType(type); |
| 971 | klass->SetStatus(Class::kStatusInitialized); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 972 | bool success = InsertClass(descriptor, klass); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 973 | CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed"; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 974 | return klass; |
| 975 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 976 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 977 | // Create an array class (i.e. the class object for the array, not the |
| 978 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 979 | // "[Ljava/lang/String;". |
| 980 | // |
| 981 | // If "descriptor" refers to an array of primitives, look up the |
| 982 | // primitive type's internally-generated class object. |
| 983 | // |
| 984 | // "loader" is the class loader of the class that's referring to us. It's |
| 985 | // used to ensure that we're looking for the element type in the right |
| 986 | // context. It does NOT become the class loader for the array class; that |
| 987 | // always comes from the base element class. |
| 988 | // |
| 989 | // Returns NULL with an exception raised on failure. |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 990 | Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 991 | const ClassLoader* class_loader) { |
| 992 | CHECK_EQ('[', descriptor[0]); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 993 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 994 | // Identify the underlying element class and the array dimension depth. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 995 | Class* component_type = NULL; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 996 | int array_rank; |
| 997 | if (descriptor[1] == '[') { |
| 998 | // array of arrays; keep descriptor and grab stuff from parent |
| 999 | Class* outer = FindClass(descriptor.substr(1), class_loader); |
| 1000 | if (outer != NULL) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1001 | // want the base class, not "outer", in our component_type |
| 1002 | component_type = outer->GetComponentType(); |
| 1003 | array_rank = outer->GetArrayRank() + 1; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1004 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1005 | DCHECK(component_type == NULL); // make sure we fail |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1006 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1007 | } else { |
| 1008 | array_rank = 1; |
| 1009 | if (descriptor[1] == 'L') { |
| 1010 | // array of objects; strip off "[" and look up descriptor. |
| 1011 | const StringPiece subDescriptor = descriptor.substr(1); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1012 | component_type = FindClass(subDescriptor, class_loader); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1013 | } else { |
| 1014 | // array of a primitive type |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1015 | component_type = FindPrimitiveClass(descriptor[1]); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1016 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1017 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1018 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1019 | if (component_type == NULL) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1020 | // failed |
| 1021 | // DCHECK(Thread::Current()->IsExceptionPending()); // TODO |
| 1022 | return NULL; |
| 1023 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1024 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1025 | // See if the component type is already loaded. Array classes are |
| 1026 | // always associated with the class loader of their underlying |
| 1027 | // element type -- an array of Strings goes with the loader for |
| 1028 | // java/lang/String -- so we need to look for it there. (The |
| 1029 | // caller should have checked for the existence of the class |
| 1030 | // before calling here, but they did so with *their* class loader, |
| 1031 | // not the component type's loader.) |
| 1032 | // |
| 1033 | // If we find it, the caller adds "loader" to the class' initiating |
| 1034 | // loader list, which should prevent us from going through this again. |
| 1035 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1036 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1037 | // are the same, because our caller (FindClass) just did the |
| 1038 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 1039 | // because we effectively do this lookup again when we add the new |
| 1040 | // class to the hash table --- necessary because of possible races with |
| 1041 | // other threads.) |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1042 | if (class_loader != component_type->GetClassLoader()) { |
| 1043 | Class* new_class = LookupClass(descriptor, component_type->GetClassLoader()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1044 | if (new_class != NULL) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1045 | return new_class; |
| 1046 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1047 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1048 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1049 | // Fill out the fields in the Class. |
| 1050 | // |
| 1051 | // It is possible to execute some methods against arrays, because |
| 1052 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 1053 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 1054 | // |
| 1055 | // Array classes are simple enough that we don't need to do a full |
| 1056 | // link step. |
| 1057 | |
| 1058 | Class* new_class = NULL; |
| 1059 | if (!init_done_) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1060 | // Classes that were hand created, ie not by FindSystemClass |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1061 | if (descriptor == "[Ljava/lang/Object;") { |
| 1062 | new_class = GetClassRoot(kObjectArrayClass); |
| 1063 | } else if (descriptor == "[C") { |
| 1064 | new_class = GetClassRoot(kCharArrayClass); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 1065 | } else if (descriptor == "[I") { |
| 1066 | new_class = GetClassRoot(kIntArrayClass); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | if (new_class == NULL) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1070 | new_class = AllocClass(sizeof(Class)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1071 | if (new_class == NULL) { |
| 1072 | return NULL; |
| 1073 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1074 | new_class->SetArrayRank(array_rank); |
| 1075 | new_class->SetComponentType(component_type); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1076 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1077 | DCHECK_LE(1, new_class->GetArrayRank()); |
| 1078 | DCHECK(new_class->GetComponentType() != NULL); |
| 1079 | new_class->SetDescriptor(String::AllocFromModifiedUtf8(descriptor.ToString().c_str())); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1080 | Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1081 | new_class->SetSuperClass(java_lang_Object); |
| 1082 | new_class->SetVTable(java_lang_Object->GetVTable()); |
| 1083 | new_class->SetPrimitiveType(Class::kPrimNot); |
| 1084 | new_class->SetClassLoader(component_type->GetClassLoader()); |
| 1085 | new_class->SetStatus(Class::kStatusInitialized); |
| 1086 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1087 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1088 | |
| 1089 | |
| 1090 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 1091 | // interfaces. We need to set that up here, so that stuff like |
| 1092 | // "instanceof" works right. |
| 1093 | // |
| 1094 | // Note: The GC could run during the call to FindSystemClass, |
| 1095 | // so we need to make sure the class object is GC-valid while we're in |
| 1096 | // there. Do this by clearing the interface list so the GC will just |
| 1097 | // think that the entries are null. |
| 1098 | |
| 1099 | |
| 1100 | // Use the single, global copies of "interfaces" and "iftable" |
| 1101 | // (remember not to free them for arrays). |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1102 | new_class->SetInterfaces(array_interfaces_); |
| 1103 | new_class->SetIFTableCount(2); |
| 1104 | new_class->SetIFTable(array_iftable_); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1105 | |
| 1106 | // Inherit access flags from the component type. Arrays can't be |
| 1107 | // used as a superclass or interface, so we want to add "final" |
| 1108 | // and remove "interface". |
| 1109 | // |
| 1110 | // Don't inherit any non-standard flags (e.g., kAccFinal) |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1111 | // from component_type. We assume that the array class does not |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1112 | // override finalize(). |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1113 | new_class->SetAccessFlags(((new_class->GetComponentType()->GetAccessFlags() & |
| 1114 | ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1115 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1116 | if (InsertClass(descriptor, new_class)) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1117 | return new_class; |
| 1118 | } |
| 1119 | // Another thread must have loaded the class after we |
| 1120 | // started but before we finished. Abandon what we've |
| 1121 | // done. |
| 1122 | // |
| 1123 | // (Yes, this happens.) |
| 1124 | |
| 1125 | // Grab the winning class. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1126 | Class* other_class = LookupClass(descriptor, component_type->GetClassLoader()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1127 | DCHECK(other_class != NULL); |
| 1128 | return other_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | Class* ClassLinker::FindPrimitiveClass(char type) { |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 1132 | switch (type) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1133 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1134 | return GetClassRoot(kPrimitiveByte); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1135 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1136 | return GetClassRoot(kPrimitiveChar); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1137 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1138 | return GetClassRoot(kPrimitiveDouble); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1139 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1140 | return GetClassRoot(kPrimitiveFloat); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1141 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1142 | return GetClassRoot(kPrimitiveInt); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1143 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1144 | return GetClassRoot(kPrimitiveLong); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1145 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1146 | return GetClassRoot(kPrimitiveShort); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1147 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1148 | return GetClassRoot(kPrimitiveBoolean); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1149 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1150 | return GetClassRoot(kPrimitiveVoid); |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 1151 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1152 | std::string printable_type(PrintableChar(type)); |
| 1153 | Thread::Current()->ThrowNewException("Ljava/lang/NoClassDefFoundError;", |
| 1154 | "Not a primitive type: %s", printable_type.c_str()); |
| 1155 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1158 | bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) { |
| 1159 | size_t hash = StringPieceHash()(descriptor); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1160 | MutexLock mu(classes_lock_); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1161 | Table::iterator it = classes_.insert(std::make_pair(hash, klass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1162 | return ((*it).second == klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1165 | Class* ClassLinker::LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1166 | size_t hash = StringPieceHash()(descriptor); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1167 | MutexLock mu(classes_lock_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1168 | typedef Table::const_iterator It; // TODO: C++0x auto |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1169 | for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1170 | Class* klass = it->second; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1171 | if (klass->GetDescriptor()->Equals(descriptor) && klass->GetClassLoader() == class_loader) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1172 | return klass; |
| 1173 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1174 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1175 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | bool ClassLinker::InitializeClass(Class* klass) { |
| 1179 | CHECK(klass->GetStatus() == Class::kStatusResolved || |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 1180 | klass->GetStatus() == Class::kStatusError) << klass->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1181 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 1182 | Thread* self = Thread::Current(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1183 | |
| 1184 | { |
| 1185 | ObjectLock lock(klass); |
| 1186 | |
| 1187 | if (klass->GetStatus() < Class::kStatusVerified) { |
| 1188 | if (klass->IsErroneous()) { |
| 1189 | LG << "re-initializing failed class"; // TODO: throw |
| 1190 | return false; |
| 1191 | } |
| 1192 | |
| 1193 | CHECK(klass->GetStatus() == Class::kStatusResolved); |
| 1194 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1195 | klass->SetStatus(Class::kStatusVerifying); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame^] | 1196 | if (!DexVerifier::VerifyClass(klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1197 | LG << "Verification failed"; // TODO: ThrowVerifyError |
| 1198 | Object* exception = self->GetException(); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1199 | klass->SetVerifyErrorClass(exception->GetClass()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1200 | klass->SetStatus(Class::kStatusError); |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
| 1204 | klass->SetStatus(Class::kStatusVerified); |
| 1205 | } |
| 1206 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1207 | if (klass->GetStatus() == Class::kStatusInitialized) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1208 | return true; |
| 1209 | } |
| 1210 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1211 | while (klass->GetStatus() == Class::kStatusInitializing) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1212 | // we caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 1213 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1214 | LG << "recursive <clinit>"; |
| 1215 | return true; |
| 1216 | } |
| 1217 | |
| 1218 | CHECK(!self->IsExceptionPending()); |
| 1219 | |
| 1220 | lock.Wait(); // TODO: check for interruption |
| 1221 | |
| 1222 | // When we wake up, repeat the test for init-in-progress. If |
| 1223 | // there's an exception pending (only possible if |
| 1224 | // "interruptShouldThrow" was set), bail out. |
| 1225 | if (self->IsExceptionPending()) { |
| 1226 | CHECK(false); |
| 1227 | LG << "Exception in initialization."; // TODO: ExceptionInInitializerError |
| 1228 | klass->SetStatus(Class::kStatusError); |
| 1229 | return false; |
| 1230 | } |
| 1231 | if (klass->GetStatus() == Class::kStatusInitializing) { |
| 1232 | continue; |
| 1233 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1234 | DCHECK(klass->GetStatus() == Class::kStatusInitialized || |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1235 | klass->GetStatus() == Class::kStatusError); |
| 1236 | if (klass->IsErroneous()) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1237 | // The caller wants an exception, but it was thrown in a |
| 1238 | // different thread. Synthesize one here. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1239 | LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError |
| 1240 | return false; |
| 1241 | } |
| 1242 | return true; // otherwise, initialized |
| 1243 | } |
| 1244 | |
| 1245 | // see if we failed previously |
| 1246 | if (klass->IsErroneous()) { |
| 1247 | // might be wise to unlock before throwing; depends on which class |
| 1248 | // it is that we have locked |
| 1249 | |
| 1250 | // TODO: throwEarlierClassFailure(klass); |
| 1251 | return false; |
| 1252 | } |
| 1253 | |
| 1254 | if (!ValidateSuperClassDescriptors(klass)) { |
| 1255 | klass->SetStatus(Class::kStatusError); |
| 1256 | return false; |
| 1257 | } |
| 1258 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1259 | DCHECK(klass->GetStatus() < Class::kStatusInitializing); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1260 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 1261 | klass->SetClinitThreadId(self->GetTid()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1262 | klass->SetStatus(Class::kStatusInitializing); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | if (!InitializeSuperClass(klass)) { |
| 1266 | return false; |
| 1267 | } |
| 1268 | |
| 1269 | InitializeStaticFields(klass); |
| 1270 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1271 | Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1272 | if (clinit != NULL) { |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 1273 | clinit->Invoke(self, NULL, NULL, NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | { |
| 1277 | ObjectLock lock(klass); |
| 1278 | |
| 1279 | if (self->IsExceptionPending()) { |
| 1280 | klass->SetStatus(Class::kStatusError); |
| 1281 | } else { |
| 1282 | klass->SetStatus(Class::kStatusInitialized); |
| 1283 | } |
| 1284 | lock.NotifyAll(); |
| 1285 | } |
| 1286 | |
| 1287 | return true; |
| 1288 | } |
| 1289 | |
| 1290 | bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) { |
| 1291 | if (klass->IsInterface()) { |
| 1292 | return true; |
| 1293 | } |
| 1294 | // begin with the methods local to the superclass |
| 1295 | if (klass->HasSuperClass() && |
| 1296 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
| 1297 | const Class* super = klass->GetSuperClass(); |
| 1298 | for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) { |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 1299 | const Method* method = super->GetVirtualMethod(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1300 | if (method != super->GetVirtualMethod(i) && |
| 1301 | !HasSameMethodDescriptorClasses(method, super, klass)) { |
| 1302 | LG << "Classes resolve differently in superclass"; |
| 1303 | return false; |
| 1304 | } |
| 1305 | } |
| 1306 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1307 | for (size_t i = 0; i < klass->GetIFTableCount(); ++i) { |
| 1308 | const InterfaceEntry* iftable = &klass->GetIFTable()[i]; |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1309 | Class* interface = iftable->GetInterface(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1310 | if (klass->GetClassLoader() != interface->GetClassLoader()) { |
| 1311 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1312 | uint32_t vtable_index = iftable->GetMethodIndexArray()[j]; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1313 | const Method* method = klass->GetVirtualMethod(vtable_index); |
| 1314 | if (!HasSameMethodDescriptorClasses(method, interface, |
| 1315 | method->GetClass())) { |
| 1316 | LG << "Classes resolve differently in interface"; // TODO: LinkageError |
| 1317 | return false; |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | return true; |
| 1323 | } |
| 1324 | |
| 1325 | bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1326 | const Class* klass1, |
| 1327 | const Class* klass2) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1328 | const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1329 | const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->GetProtoIdx()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1330 | DexFile::ParameterIterator *it; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1331 | for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1332 | const char* descriptor = it->GetDescriptor(); |
| 1333 | if (descriptor == NULL) { |
| 1334 | break; |
| 1335 | } |
| 1336 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 1337 | // Found a non-primitive type. |
| 1338 | if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) { |
| 1339 | return false; |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | // Check the return type |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1344 | const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1345 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 1346 | if (HasSameDescriptorClasses(descriptor, klass1, klass2)) { |
| 1347 | return false; |
| 1348 | } |
| 1349 | } |
| 1350 | return true; |
| 1351 | } |
| 1352 | |
| 1353 | // Returns true if classes referenced by the descriptor are the |
| 1354 | // same classes in klass1 as they are in klass2. |
| 1355 | bool ClassLinker::HasSameDescriptorClasses(const char* descriptor, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1356 | const Class* klass1, |
| 1357 | const Class* klass2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1358 | CHECK(descriptor != NULL); |
| 1359 | CHECK(klass1 != NULL); |
| 1360 | CHECK(klass2 != NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1361 | Class* found1 = FindClass(descriptor, klass1->GetClassLoader()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1362 | // TODO: found1 == NULL |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1363 | Class* found2 = FindClass(descriptor, klass2->GetClassLoader()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1364 | // TODO: found2 == NULL |
| 1365 | // TODO: lookup found1 in initiating loader list |
| 1366 | if (found1 == NULL || found2 == NULL) { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 1367 | Thread::Current()->ClearException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1368 | if (found1 == found2) { |
| 1369 | return true; |
| 1370 | } else { |
| 1371 | return false; |
| 1372 | } |
| 1373 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1374 | return true; |
| 1375 | } |
| 1376 | |
| 1377 | bool ClassLinker::InitializeSuperClass(Class* klass) { |
| 1378 | CHECK(klass != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1379 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 1380 | Class* super_class = klass->GetSuperClass(); |
| 1381 | if (super_class->GetStatus() != Class::kStatusInitialized) { |
| 1382 | CHECK(!super_class->IsInterface()); |
| 1383 | klass->MonitorExit(); |
| 1384 | bool super_initialized = InitializeClass(super_class); |
| 1385 | klass->MonitorEnter(); |
| 1386 | // TODO: check for a pending exception |
| 1387 | if (!super_initialized) { |
| 1388 | klass->SetStatus(Class::kStatusError); |
| 1389 | klass->NotifyAll(); |
| 1390 | return false; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | return true; |
| 1395 | } |
| 1396 | |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 1397 | bool ClassLinker::EnsureInitialized(Class* c) { |
| 1398 | CHECK(c != NULL); |
| 1399 | if (c->IsInitialized()) { |
| 1400 | return true; |
| 1401 | } |
| 1402 | |
| 1403 | c->MonitorExit(); |
| 1404 | InitializeClass(c); |
| 1405 | c->MonitorEnter(); |
| 1406 | return !Thread::Current()->IsExceptionPending(); |
| 1407 | } |
| 1408 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 1409 | StaticStorageBase* ClassLinker::InitializeStaticStorageFromCode(uint32_t type_idx, |
| 1410 | const Method* referrer) { |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1411 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1412 | Class* klass = class_linker->ResolveType(type_idx, referrer); |
| 1413 | if (klass == NULL) { |
| 1414 | UNIMPLEMENTED(FATAL) << "throw exception due to unresolved class"; |
| 1415 | } |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 1416 | // If we are the <clinit> of this class, just return our storage. |
| 1417 | // |
| 1418 | // Do not set the DexCache InitializedStaticStorage, since that |
| 1419 | // implies <clinit> has finished running. |
| 1420 | if (klass == referrer->GetDeclaringClass() && referrer->GetName()->Equals("<clinit>")) { |
| 1421 | return klass; |
| 1422 | } |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1423 | if (!class_linker->EnsureInitialized(klass)) { |
| 1424 | CHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 1425 | UNIMPLEMENTED(FATAL) << "throw exception due to class initialization problem"; |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1426 | } |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 1427 | referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1428 | return klass; |
| 1429 | } |
| 1430 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1431 | void ClassLinker::InitializeStaticFields(Class* klass) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1432 | size_t num_static_fields = klass->NumStaticFields(); |
| 1433 | if (num_static_fields == 0) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1434 | return; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1435 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1436 | DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1437 | // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray? |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1438 | if (dex_cache == NULL) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1439 | return; |
| 1440 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1441 | const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1442 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 1443 | const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1444 | CHECK(dex_class_def != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1445 | const byte* addr = dex_file.GetEncodedArray(*dex_class_def); |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 1446 | if (addr == NULL) { |
| 1447 | // All this class' static fields have default values. |
| 1448 | return; |
| 1449 | } |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1450 | size_t array_size = DecodeUnsignedLeb128(&addr); |
| 1451 | for (size_t i = 0; i < array_size; ++i) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1452 | Field* field = klass->GetStaticField(i); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1453 | JValue value; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1454 | DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1455 | switch (type) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1456 | case DexFile::kByte: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1457 | field->SetByte(NULL, value.b); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1458 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1459 | case DexFile::kShort: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1460 | field->SetShort(NULL, value.s); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1461 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1462 | case DexFile::kChar: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1463 | field->SetChar(NULL, value.c); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1464 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1465 | case DexFile::kInt: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1466 | field->SetInt(NULL, value.i); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1467 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1468 | case DexFile::kLong: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1469 | field->SetLong(NULL, value.j); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1470 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1471 | case DexFile::kFloat: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1472 | field->SetFloat(NULL, value.f); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1473 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1474 | case DexFile::kDouble: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1475 | field->SetDouble(NULL, value.d); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1476 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1477 | case DexFile::kString: { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1478 | uint32_t string_idx = value.i; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1479 | const String* resolved = ResolveString(dex_file, string_idx, klass->GetDexCache()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1480 | field->SetObject(NULL, resolved); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1481 | break; |
| 1482 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1483 | case DexFile::kBoolean: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1484 | field->SetBoolean(NULL, value.z); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1485 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1486 | case DexFile::kNull: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1487 | field->SetObject(NULL, value.l); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1488 | break; |
| 1489 | default: |
Carl Shapiro | 606258b | 2011-07-09 16:09:09 -0700 | [diff] [blame] | 1490 | LOG(FATAL) << "Unknown type " << static_cast<int>(type); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1491 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1495 | bool ClassLinker::LinkClass(Class* klass) { |
| 1496 | CHECK_EQ(Class::kStatusLoaded, klass->GetStatus()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1497 | if (!LinkSuperClass(klass)) { |
| 1498 | return false; |
| 1499 | } |
| 1500 | if (!LinkMethods(klass)) { |
| 1501 | return false; |
| 1502 | } |
| 1503 | if (!LinkInstanceFields(klass)) { |
| 1504 | return false; |
| 1505 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1506 | if (!LinkStaticFields(klass)) { |
| 1507 | return false; |
| 1508 | } |
| 1509 | CreateReferenceInstanceOffsets(klass); |
| 1510 | CreateReferenceStaticOffsets(klass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1511 | CHECK_EQ(Class::kStatusLoaded, klass->GetStatus()); |
| 1512 | klass->SetStatus(Class::kStatusResolved); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1513 | return true; |
| 1514 | } |
| 1515 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1516 | bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1517 | CHECK_EQ(Class::kStatusIdx, klass->GetStatus()); |
| 1518 | if (klass->GetSuperClassTypeIdx() != DexFile::kDexNoIndex) { |
| 1519 | Class* super_class = ResolveType(dex_file, klass->GetSuperClassTypeIdx(), klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1520 | if (super_class == NULL) { |
| 1521 | LG << "Failed to resolve superclass"; |
| 1522 | return false; |
| 1523 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1524 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1525 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1526 | for (size_t i = 0; i < klass->NumInterfaces(); ++i) { |
| 1527 | uint32_t idx = klass->GetInterfacesTypeIdx()->Get(i); |
| 1528 | Class *interface = ResolveType(dex_file, idx, klass); |
| 1529 | klass->SetInterface(i, interface); |
| 1530 | if (interface == NULL) { |
| 1531 | LG << "Failed to resolve interface"; |
| 1532 | return false; |
| 1533 | } |
| 1534 | // Verify |
| 1535 | if (!klass->CanAccess(interface)) { |
| 1536 | LG << "Inaccessible interface"; |
| 1537 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1538 | } |
| 1539 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1540 | // Mark the class as loaded. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1541 | klass->SetStatus(Class::kStatusLoaded); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1542 | return true; |
| 1543 | } |
| 1544 | |
| 1545 | bool ClassLinker::LinkSuperClass(Class* klass) { |
| 1546 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1547 | Class* super = klass->GetSuperClass(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1548 | if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1549 | if (super != NULL) { |
| 1550 | LG << "Superclass must not be defined"; // TODO: ClassFormatError |
| 1551 | return false; |
| 1552 | } |
| 1553 | // TODO: clear finalize attribute |
| 1554 | return true; |
| 1555 | } |
| 1556 | if (super == NULL) { |
| 1557 | LG << "No superclass defined"; // TODO: LinkageError |
| 1558 | return false; |
| 1559 | } |
| 1560 | // Verify |
| 1561 | if (super->IsFinal()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1562 | LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1563 | return false; |
| 1564 | } |
| 1565 | if (super->IsInterface()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1566 | LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1567 | return false; |
| 1568 | } |
| 1569 | if (!klass->CanAccess(super)) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1570 | LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1571 | return false; |
| 1572 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1573 | #ifndef NDEBUG |
| 1574 | // Ensure super classes are fully resolved prior to resolving fields.. |
| 1575 | while (super != NULL) { |
| 1576 | CHECK(super->IsLinked()); |
| 1577 | super = super->GetSuperClass(); |
| 1578 | } |
| 1579 | #endif |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1580 | return true; |
| 1581 | } |
| 1582 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1583 | // Populate the class vtable and itable. Compute return type indices. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1584 | bool ClassLinker::LinkMethods(Class* klass) { |
| 1585 | if (klass->IsInterface()) { |
| 1586 | // No vtable. |
| 1587 | size_t count = klass->NumVirtualMethods(); |
| 1588 | if (!IsUint(16, count)) { |
| 1589 | LG << "Too many methods on interface"; // TODO: VirtualMachineError |
| 1590 | return false; |
| 1591 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 1592 | for (size_t i = 0; i < count; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1593 | klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1594 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame^] | 1595 | // Link interface method tables |
| 1596 | LinkInterfaceMethods(klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1597 | } else { |
| 1598 | // Link virtual method tables |
| 1599 | LinkVirtualMethods(klass); |
| 1600 | |
| 1601 | // Link interface method tables |
| 1602 | LinkInterfaceMethods(klass); |
| 1603 | |
| 1604 | // Insert stubs. |
| 1605 | LinkAbstractMethods(klass); |
| 1606 | } |
| 1607 | return true; |
| 1608 | } |
| 1609 | |
| 1610 | bool ClassLinker::LinkVirtualMethods(Class* klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1611 | if (klass->HasSuperClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1612 | uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->GetVTable()->GetLength(); |
| 1613 | size_t actual_count = klass->GetSuperClass()->GetVTable()->GetLength(); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1614 | CHECK_LE(actual_count, max_count); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1615 | // TODO: do not assign to the vtable field until it is fully constructed. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1616 | ObjectArray<Method>* vtable = klass->GetSuperClass()->GetVTable()->CopyOf(max_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1617 | // See if any of our virtual methods override the superclass. |
| 1618 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1619 | Method* local_method = klass->GetVirtualMethodDuringLinking(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1620 | size_t j = 0; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1621 | for (; j < actual_count; ++j) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1622 | Method* super_method = vtable->Get(j); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1623 | if (local_method->HasSameNameAndDescriptor(super_method)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1624 | // Verify |
| 1625 | if (super_method->IsFinal()) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1626 | LG << "Method overrides final method"; // TODO: VirtualMachineError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1627 | return false; |
| 1628 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1629 | vtable->Set(j, local_method); |
| 1630 | local_method->SetMethodIndex(j); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1631 | break; |
| 1632 | } |
| 1633 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1634 | if (j == actual_count) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1635 | // Not overriding, append. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1636 | vtable->Set(actual_count, local_method); |
| 1637 | local_method->SetMethodIndex(actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1638 | actual_count += 1; |
| 1639 | } |
| 1640 | } |
| 1641 | if (!IsUint(16, actual_count)) { |
| 1642 | LG << "Too many methods defined on class"; // TODO: VirtualMachineError |
| 1643 | return false; |
| 1644 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1645 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1646 | CHECK_LE(actual_count, max_count); |
| 1647 | if (actual_count < max_count) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1648 | vtable = vtable->CopyOf(actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1649 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1650 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1651 | } else { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1652 | CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;")); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1653 | uint32_t num_virtual_methods = klass->NumVirtualMethods(); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1654 | if (!IsUint(16, num_virtual_methods)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1655 | LG << "Too many methods"; // TODO: VirtualMachineError |
| 1656 | return false; |
| 1657 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1658 | ObjectArray<Method>* vtable = AllocObjectArray<Method>(num_virtual_methods); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1659 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1660 | Method* virtual_method = klass->GetVirtualMethodDuringLinking(i); |
| 1661 | vtable->Set(i, virtual_method); |
| 1662 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1663 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1664 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1665 | } |
| 1666 | return true; |
| 1667 | } |
| 1668 | |
| 1669 | bool ClassLinker::LinkInterfaceMethods(Class* klass) { |
| 1670 | int pool_offset = 0; |
| 1671 | int pool_size = 0; |
| 1672 | int miranda_count = 0; |
| 1673 | int miranda_alloc = 0; |
| 1674 | size_t super_ifcount; |
| 1675 | if (klass->HasSuperClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1676 | super_ifcount = klass->GetSuperClass()->GetIFTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1677 | } else { |
| 1678 | super_ifcount = 0; |
| 1679 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1680 | size_t ifcount = super_ifcount; |
| 1681 | ifcount += klass->NumInterfaces(); |
| 1682 | for (size_t i = 0; i < klass->NumInterfaces(); i++) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1683 | ifcount += klass->GetInterface(i)->GetIFTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1684 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1685 | if (ifcount == 0) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1686 | // TODO: enable these asserts with klass status validation |
| 1687 | // DCHECK(klass->GetIFTableCount() == 0); |
| 1688 | // DCHECK(klass->GetIFTable() == NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1689 | return true; |
| 1690 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1691 | InterfaceEntry* iftable = new InterfaceEntry[ifcount]; |
| 1692 | memset(iftable, 0x00, sizeof(InterfaceEntry) * ifcount); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1693 | if (super_ifcount != 0) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1694 | memcpy(iftable, klass->GetSuperClass()->GetIFTable(), |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1695 | sizeof(InterfaceEntry) * super_ifcount); |
| 1696 | } |
| 1697 | // Flatten the interface inheritance hierarchy. |
| 1698 | size_t idx = super_ifcount; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1699 | for (size_t i = 0; i < klass->NumInterfaces(); i++) { |
| 1700 | Class* interf = klass->GetInterface(i); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1701 | DCHECK(interf != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1702 | if (!interf->IsInterface()) { |
| 1703 | LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError |
| 1704 | return false; |
| 1705 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1706 | iftable[idx++].SetInterface(interf); |
| 1707 | for (size_t j = 0; j < interf->GetIFTableCount(); j++) { |
| 1708 | iftable[idx++].SetInterface(interf->GetIFTable()[j].GetInterface()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1709 | } |
| 1710 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1711 | klass->SetIFTable(iftable); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1712 | CHECK_EQ(idx, ifcount); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1713 | klass->SetIFTableCount(ifcount); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1714 | if (klass->IsInterface() || super_ifcount == ifcount) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1715 | return true; |
| 1716 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1717 | for (size_t i = super_ifcount; i < ifcount; i++) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1718 | pool_size += iftable[i].GetInterface()->NumVirtualMethods(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1719 | } |
| 1720 | if (pool_size == 0) { |
| 1721 | return true; |
| 1722 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1723 | klass->SetIfviPoolCount(pool_size); |
| 1724 | uint32_t* ifvi_pool = new uint32_t[pool_size]; |
| 1725 | klass->SetIfviPool(ifvi_pool); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1726 | std::vector<Method*> miranda_list; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1727 | for (size_t i = super_ifcount; i < ifcount; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1728 | iftable[i].SetMethodIndexArray(ifvi_pool + pool_offset); |
| 1729 | Class* interface = iftable[i].GetInterface(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1730 | pool_offset += interface->NumVirtualMethods(); // end here |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1731 | ObjectArray<Method>* vtable = klass->GetVTableDuringLinking(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1732 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
| 1733 | Method* interface_method = interface->GetVirtualMethod(j); |
| 1734 | int k; // must be signed |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1735 | for (k = vtable->GetLength() - 1; k >= 0; --k) { |
| 1736 | Method* vtable_method = vtable->Get(k); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1737 | if (interface_method->HasSameNameAndDescriptor(vtable_method)) { |
| 1738 | if (!vtable_method->IsPublic()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1739 | LG << "Implementation not public"; |
| 1740 | return false; |
| 1741 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1742 | iftable[i].GetMethodIndexArray()[j] = k; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1743 | break; |
| 1744 | } |
| 1745 | } |
| 1746 | if (k < 0) { |
| 1747 | if (miranda_count == miranda_alloc) { |
| 1748 | miranda_alloc += 8; |
| 1749 | if (miranda_list.empty()) { |
| 1750 | miranda_list.resize(miranda_alloc); |
| 1751 | } else { |
| 1752 | miranda_list.resize(miranda_alloc); |
| 1753 | } |
| 1754 | } |
| 1755 | int mir; |
| 1756 | for (mir = 0; mir < miranda_count; mir++) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1757 | Method* miranda_method = miranda_list[mir]; |
| 1758 | if (miranda_method->HasSameNameAndDescriptor(interface_method)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1759 | break; |
| 1760 | } |
| 1761 | } |
| 1762 | // point the interface table at a phantom slot index |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1763 | iftable[i].GetMethodIndexArray()[j] = |
| 1764 | vtable->GetLength() + mir; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1765 | if (mir == miranda_count) { |
| 1766 | miranda_list[miranda_count++] = interface_method; |
| 1767 | } |
| 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | if (miranda_count != 0) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1772 | int old_method_count = klass->NumVirtualMethods(); |
| 1773 | int new_method_count = old_method_count + miranda_count; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1774 | klass->SetVirtualMethods( |
| 1775 | klass->GetVirtualMethods()->CopyOf(new_method_count)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1776 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1777 | ObjectArray<Method>* vtable = klass->GetVTableDuringLinking(); |
| 1778 | CHECK(vtable != NULL); |
| 1779 | int old_vtable_count = vtable->GetLength(); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1780 | int new_vtable_count = old_vtable_count + miranda_count; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1781 | vtable = vtable->CopyOf(new_vtable_count); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1782 | for (int i = 0; i < miranda_count; i++) { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1783 | Method* meth = AllocMethod(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1784 | // TODO: this shouldn't be a memcpy |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1785 | memcpy(meth, miranda_list[i], sizeof(Method)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1786 | meth->SetDeclaringClass(klass); |
| 1787 | meth->SetAccessFlags(meth->GetAccessFlags() | kAccMiranda); |
| 1788 | meth->SetMethodIndex(0xFFFF & (old_vtable_count + i)); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1789 | klass->SetVirtualMethod(old_method_count + i, meth); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1790 | vtable->Set(old_vtable_count + i, meth); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1791 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1792 | // TODO: do not assign to the vtable field until it is fully constructed. |
| 1793 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1794 | } |
| 1795 | return true; |
| 1796 | } |
| 1797 | |
| 1798 | void ClassLinker::LinkAbstractMethods(Class* klass) { |
| 1799 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1800 | Method* method = klass->GetVirtualMethodDuringLinking(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1801 | if (method->IsAbstract()) { |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 1802 | LG << "AbstractMethodError"; |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 1803 | // TODO: throw AbstractMethodError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | bool ClassLinker::LinkInstanceFields(Class* klass) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1809 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1810 | return LinkFields(klass, true); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | bool ClassLinker::LinkStaticFields(Class* klass) { |
| 1814 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1815 | size_t allocated_class_size = klass->GetClassSize(); |
| 1816 | bool success = LinkFields(klass, false); |
| 1817 | CHECK_EQ(allocated_class_size, klass->GetClassSize()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1818 | return success; |
| 1819 | } |
| 1820 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1821 | bool ClassLinker::LinkFields(Class *klass, bool instance) { |
| 1822 | size_t num_fields = |
| 1823 | instance ? klass->NumInstanceFields() : klass->NumStaticFields(); |
| 1824 | |
| 1825 | ObjectArray<Field>* fields = |
| 1826 | instance ? klass->GetIFields() : klass->GetSFields(); |
| 1827 | // Fields updated at end of LinkFields |
| 1828 | size_t num_reference_fields; |
| 1829 | size_t size; |
| 1830 | |
| 1831 | // Initialize size and field_offset |
| 1832 | MemberOffset field_offset = Class::FieldsOffset(); |
| 1833 | if (instance) { |
| 1834 | Class* super_class = klass->GetSuperClass(); |
| 1835 | if (super_class != NULL) { |
| 1836 | CHECK(super_class->IsLinked()); |
| 1837 | field_offset = MemberOffset(super_class->GetObjectSize()); |
| 1838 | if (field_offset.Uint32Value() == 0u) { |
| 1839 | field_offset = OFFSET_OF_OBJECT_MEMBER(DataObject, fields_); |
| 1840 | } |
| 1841 | } else { |
| 1842 | field_offset = OFFSET_OF_OBJECT_MEMBER(DataObject, fields_); |
| 1843 | } |
| 1844 | size = field_offset.Uint32Value(); |
| 1845 | } else { |
| 1846 | size = klass->GetClassSize(); |
| 1847 | } |
| 1848 | DCHECK_LE(CLASS_SMALLEST_OFFSET, size); |
| 1849 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1850 | CHECK((num_fields == 0) == (fields == NULL)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1851 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1852 | // Move references to the front. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1853 | size_t i = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1854 | num_reference_fields = 0; |
| 1855 | for (; i < num_fields; i++) { |
| 1856 | Field* field = fields->Get(i); |
| 1857 | const Class* field_type = field->GetTypeDuringLinking(); |
| 1858 | // if a field's type at this point is NULL it isn't primitive |
| 1859 | if (field_type != NULL && field_type->IsPrimitive()) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1860 | for (size_t j = num_fields - 1; j > i; j--) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1861 | Field* ref_field = fields->Get(j); |
| 1862 | const Class* ref_field_type = ref_field->GetTypeDuringLinking(); |
| 1863 | if (ref_field_type == NULL || !ref_field_type->IsPrimitive()) { |
| 1864 | fields->Set(i, ref_field); |
| 1865 | fields->Set(j, field); |
| 1866 | field = ref_field; |
| 1867 | field_type = ref_field_type; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1868 | num_reference_fields++; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1869 | break; |
| 1870 | } |
| 1871 | } |
| 1872 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1873 | num_reference_fields++; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1874 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1875 | if (field_type != NULL && field_type->IsPrimitive()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1876 | break; |
| 1877 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1878 | field->SetOffset(field_offset); |
| 1879 | field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | // Now we want to pack all of the double-wide fields together. If |
| 1883 | // we're not aligned, though, we want to shuffle one 32-bit field |
| 1884 | // into place. If we can't find one, we'll have to pad it. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1885 | if (i != num_fields && !IsAligned(field_offset.Uint32Value(), 8)) { |
| 1886 | Field* field = fields->Get(i); |
| 1887 | const Class* c = field->GetTypeDuringLinking(); |
| 1888 | CHECK(c != NULL); // should only be working on primitive types |
| 1889 | if (!c->IsPrimitiveLong() && !c->IsPrimitiveDouble()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1890 | // The field that comes next is 32-bit, so just advance past it. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1891 | DCHECK(c->IsPrimitive()); |
| 1892 | field->SetOffset(field_offset); |
| 1893 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 1894 | sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1895 | i++; |
| 1896 | } else { |
| 1897 | // Next field is 64-bit, so search for a 32-bit field we can |
| 1898 | // swap into it. |
| 1899 | bool found = false; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1900 | for (size_t j = num_fields - 1; j > i; j--) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1901 | Field* single_field = fields->Get(j); |
| 1902 | const Class* rc = single_field->GetTypeDuringLinking(); |
| 1903 | CHECK(rc != NULL); // should only be working on primitive types |
| 1904 | if (!rc->IsPrimitiveLong() && !rc->IsPrimitiveDouble()) { |
| 1905 | fields->Set(i, single_field); |
| 1906 | fields->Set(j, field); |
| 1907 | field = single_field; |
| 1908 | field->SetOffset(field_offset); |
| 1909 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 1910 | sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1911 | found = true; |
| 1912 | i++; |
| 1913 | break; |
| 1914 | } |
| 1915 | } |
| 1916 | if (!found) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1917 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 1918 | sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | // Alignment is good, shuffle any double-wide fields forward, and |
| 1924 | // finish assigning field offsets to all fields. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1925 | DCHECK(i == num_fields || IsAligned(field_offset.Uint32Value(), 4)); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1926 | for ( ; i < num_fields; i++) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1927 | Field* field = fields->Get(i); |
| 1928 | const Class* c = field->GetTypeDuringLinking(); |
| 1929 | CHECK(c != NULL); // should only be working on primitive types |
| 1930 | if (!c->IsPrimitiveDouble() && !c->IsPrimitiveLong()) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1931 | for (size_t j = num_fields - 1; j > i; j--) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1932 | Field* double_field = fields->Get(j); |
| 1933 | const Class* rc = double_field->GetTypeDuringLinking(); |
| 1934 | CHECK(rc != NULL); // should only be working on primitive types |
| 1935 | if (rc->IsPrimitiveDouble() || rc->IsPrimitiveLong()) { |
| 1936 | fields->Set(i, double_field); |
| 1937 | fields->Set(j, field); |
| 1938 | field = double_field; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1939 | c = rc; |
| 1940 | break; |
| 1941 | } |
| 1942 | } |
| 1943 | } else { |
| 1944 | // This is a double-wide field, leave it be. |
| 1945 | } |
| 1946 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1947 | field->SetOffset(field_offset); |
| 1948 | if (c->IsPrimitiveLong() || c->IsPrimitiveDouble()) { |
| 1949 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 1950 | sizeof(uint64_t)); |
| 1951 | } else { |
| 1952 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 1953 | sizeof(uint32_t)); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1954 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | #ifndef NDEBUG |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1958 | // Make sure that all reference fields appear before |
| 1959 | // non-reference fields, and all double-wide fields are aligned. |
| 1960 | bool seen_non_ref = false; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1961 | for (i = 0; i < num_fields; i++) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1962 | Field* field = fields->Get(i); |
| 1963 | const Class* c = field->GetTypeDuringLinking(); |
| 1964 | if (c != NULL && c->IsPrimitive()) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1965 | if (!seen_non_ref) { |
| 1966 | seen_non_ref = true; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1967 | DCHECK_EQ(num_reference_fields, i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1968 | } |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1969 | } else { |
| 1970 | DCHECK(!seen_non_ref); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1971 | } |
| 1972 | } |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1973 | if (!seen_non_ref) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1974 | DCHECK_EQ(num_fields, num_reference_fields); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1975 | } |
| 1976 | #endif |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1977 | size = field_offset.Uint32Value(); |
| 1978 | DCHECK_LE(CLASS_SMALLEST_OFFSET, size); |
| 1979 | // Update klass |
| 1980 | if(instance) { |
| 1981 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
| 1982 | if(!klass->IsVariableSize()) { |
| 1983 | klass->SetObjectSize(size); |
| 1984 | } |
| 1985 | } else { |
| 1986 | klass->SetNumReferenceStaticFields(num_reference_fields); |
| 1987 | klass->SetClassSize(size); |
| 1988 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1989 | return true; |
| 1990 | } |
| 1991 | |
| 1992 | // Set the bitmap of reference offsets, refOffsets, from the ifields |
| 1993 | // list. |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1994 | void ClassLinker::CreateReferenceInstanceOffsets(Class* klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1995 | uint32_t reference_offsets = 0; |
| 1996 | Class* super_class = klass->GetSuperClass(); |
| 1997 | if (super_class != NULL) { |
| 1998 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1999 | // If our superclass overflowed, we don't stand a chance. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2000 | if (reference_offsets == CLASS_WALK_SUPER) { |
| 2001 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2002 | return; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2003 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2004 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2005 | CreateReferenceOffsets(klass, true, reference_offsets); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | void ClassLinker::CreateReferenceStaticOffsets(Class* klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2009 | CreateReferenceOffsets(klass, false, 0); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2010 | } |
| 2011 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2012 | void ClassLinker::CreateReferenceOffsets(Class* klass, bool instance, |
| 2013 | uint32_t reference_offsets) { |
| 2014 | size_t num_reference_fields = |
| 2015 | instance ? klass->NumReferenceInstanceFieldsDuringLinking() |
| 2016 | : klass->NumReferenceStaticFieldsDuringLinking(); |
| 2017 | const ObjectArray<Field>* fields = |
| 2018 | instance ? klass->GetIFields() : klass->GetSFields(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2019 | // All of the fields that contain object references are guaranteed |
| 2020 | // to be at the beginning of the fields list. |
| 2021 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 2022 | // Note that byte_offset is the offset from the beginning of |
| 2023 | // object, not the offset into instance data |
| 2024 | const Field* field = fields->Get(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2025 | MemberOffset byte_offset = field->GetOffsetDuringLinking(); |
| 2026 | CHECK_GE(byte_offset.Uint32Value(), CLASS_SMALLEST_OFFSET); |
| 2027 | CHECK_EQ(byte_offset.Uint32Value() & (CLASS_OFFSET_ALIGNMENT - 1), 0U); |
| 2028 | if (CLASS_CAN_ENCODE_OFFSET(byte_offset.Uint32Value())) { |
| 2029 | uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset.Uint32Value()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2030 | CHECK_NE(new_bit, 0U); |
| 2031 | reference_offsets |= new_bit; |
| 2032 | } else { |
| 2033 | reference_offsets = CLASS_WALK_SUPER; |
| 2034 | break; |
| 2035 | } |
| 2036 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2037 | // Update fields in klass |
| 2038 | if (instance) { |
| 2039 | klass->SetReferenceInstanceOffsets(reference_offsets); |
| 2040 | } else { |
| 2041 | klass->SetReferenceStaticOffsets(reference_offsets); |
| 2042 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2043 | } |
| 2044 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2045 | String* ClassLinker::ResolveString(const DexFile& dex_file, |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 2046 | uint32_t string_idx, DexCache* dex_cache) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2047 | String* resolved = dex_cache->GetResolvedString(string_idx); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2048 | if (resolved != NULL) { |
| 2049 | return resolved; |
| 2050 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2051 | const DexFile::StringId& string_id = dex_file.GetStringId(string_idx); |
| 2052 | int32_t utf16_length = dex_file.GetStringLength(string_id); |
| 2053 | const char* utf8_data = dex_file.GetStringData(string_id); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2054 | // TODO: remote the const_cast below |
| 2055 | String* string = const_cast<String*>(intern_table_->InternStrong(utf16_length, utf8_data)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2056 | dex_cache->SetResolvedString(string_idx, string); |
| 2057 | return string; |
| 2058 | } |
| 2059 | |
| 2060 | Class* ClassLinker::ResolveType(const DexFile& dex_file, |
| 2061 | uint32_t type_idx, |
| 2062 | DexCache* dex_cache, |
| 2063 | const ClassLoader* class_loader) { |
| 2064 | Class* resolved = dex_cache->GetResolvedType(type_idx); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2065 | if (resolved == NULL) { |
| 2066 | const char* descriptor = dex_file.dexStringByTypeIdx(type_idx); |
| 2067 | if (descriptor[1] == '\0') { |
| 2068 | // only the descriptors of primitive types should be 1 character long |
| 2069 | resolved = FindPrimitiveClass(descriptor[0]); |
| 2070 | } else { |
| 2071 | resolved = FindClass(descriptor, class_loader); |
| 2072 | } |
| 2073 | if (resolved != NULL) { |
| 2074 | Class* check = resolved->IsArrayClass() ? resolved->GetComponentType() : resolved; |
| 2075 | if (dex_cache != check->GetDexCache()) { |
| 2076 | if (check->GetClassLoader() != NULL) { |
| 2077 | LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError |
| 2078 | resolved = NULL; |
| 2079 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2080 | } |
| 2081 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2082 | if (resolved != NULL) { |
| 2083 | dex_cache->SetResolvedType(type_idx, resolved); |
| 2084 | } else { |
| 2085 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 2086 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2087 | } |
| 2088 | return resolved; |
| 2089 | } |
| 2090 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2091 | Method* ClassLinker::ResolveMethod(const DexFile& dex_file, |
| 2092 | uint32_t method_idx, |
| 2093 | DexCache* dex_cache, |
| 2094 | const ClassLoader* class_loader, |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2095 | bool is_direct) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2096 | Method* resolved = dex_cache->GetResolvedMethod(method_idx); |
| 2097 | if (resolved != NULL) { |
| 2098 | return resolved; |
| 2099 | } |
| 2100 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 2101 | Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
| 2102 | if (klass == NULL) { |
| 2103 | return NULL; |
| 2104 | } |
| 2105 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2106 | const char* name = dex_file.dexStringById(method_id.name_idx_); |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 2107 | std::string signature(dex_file.CreateMethodDescriptor(method_id.proto_idx_, NULL)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame^] | 2108 | if (klass->IsInterface()) { |
| 2109 | resolved = klass->FindInterfaceMethod(name, signature); |
| 2110 | } else if (is_direct) { |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2111 | resolved = klass->FindDirectMethod(name, signature); |
| 2112 | } else { |
| 2113 | resolved = klass->FindVirtualMethod(name, signature); |
| 2114 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2115 | if (resolved != NULL) { |
| 2116 | dex_cache->SetResolvedMethod(method_idx, resolved); |
| 2117 | } else { |
| 2118 | // DCHECK(Thread::Current()->IsExceptionPending()); |
| 2119 | } |
| 2120 | return resolved; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2123 | Field* ClassLinker::ResolveField(const DexFile& dex_file, |
| 2124 | uint32_t field_idx, |
| 2125 | DexCache* dex_cache, |
| 2126 | const ClassLoader* class_loader, |
| 2127 | bool is_static) { |
| 2128 | Field* resolved = dex_cache->GetResolvedField(field_idx); |
| 2129 | if (resolved != NULL) { |
| 2130 | return resolved; |
| 2131 | } |
| 2132 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 2133 | Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 2134 | if (klass == NULL) { |
| 2135 | return NULL; |
| 2136 | } |
| 2137 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2138 | const char* name = dex_file.dexStringById(field_id.name_idx_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2139 | Class* field_type = ResolveType(dex_file, field_id.type_idx_, dex_cache, class_loader); |
| 2140 | // TODO: LinkageError? |
| 2141 | CHECK(field_type != NULL); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2142 | if (is_static) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2143 | resolved = klass->FindStaticField(name, field_type); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2144 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2145 | resolved = klass->FindInstanceField(name, field_type); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2146 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2147 | if (resolved != NULL) { |
| 2148 | dex_cache->SetResolvedfield(field_idx, resolved); |
| 2149 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2150 | // TODO: DCHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2151 | } |
| 2152 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 2155 | size_t ClassLinker::NumLoadedClasses() const { |
| 2156 | MutexLock mu(classes_lock_); |
| 2157 | return classes_.size(); |
| 2158 | } |
| 2159 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2160 | } // namespace art |