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