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