blob: 09997884302b5d8d6231089dac5f7c3107e4932f [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "class_linker.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004
5#include <vector>
6#include <utility>
7
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "casts.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07009#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "dex_verifier.h"
11#include "heap.h"
12#include "logging.h"
13#include "monitor.h"
14#include "object.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070015#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "scoped_ptr.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070017#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "thread.h"
19#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
22
Brian Carlstroma663ea52011-08-19 23:33:41 -070023const char* ClassLinker::class_roots_descriptors_[kClassRootsMax] = {
24 "Ljava/lang/Class;",
25 "Ljava/lang/Object;",
26 "[Ljava/lang/Object;",
27 "Ljava/lang/String;",
28 "Ljava/lang/reflect/Field;",
29 "Ljava/lang/reflect/Method;",
30 "Ljava/lang/ClassLoader;",
31 "Ldalvik/system/BaseDexClassLoader;",
32 "Ldalvik/system/PathClassLoader;",
33 "Z",
34 "B",
35 "C",
36 "D",
37 "F",
38 "I",
39 "J",
40 "S",
41 "V",
42 "[Z",
43 "[B",
44 "[C",
45 "[D",
46 "[F",
47 "[I",
48 "[J",
49 "[S",
50};
51
52ClassLinker* ClassLinker::Create(const std::vector<DexFile*>& boot_class_path, Space* space) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070053 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstroma663ea52011-08-19 23:33:41 -070054 if (space == NULL) {
55 class_linker->Init(boot_class_path);
56 } else {
57 class_linker->Init(boot_class_path, space);
58 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070059 // TODO: check for failure during initialization
60 return class_linker.release();
61}
62
Brian Carlstroma663ea52011-08-19 23:33:41 -070063
Carl Shapiro2ed144c2011-07-26 16:52:08 -070064void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070065 CHECK(!init_done_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070066
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070067 // java_lang_Class comes first, its needed for AllocClass
Brian Carlstrom4873d462011-08-21 15:23:39 -070068 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(ClassClass)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070069 CHECK(java_lang_Class != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -070070 java_lang_Class->class_size_ = sizeof(ClassClass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070071 java_lang_Class->klass_ = java_lang_Class;
72 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070073
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070074 // java_lang_Object comes next so that object_array_class can be created
Brian Carlstrom4873d462011-08-21 15:23:39 -070075 Class* java_lang_Object = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070076 CHECK(java_lang_Object != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070077 // backfill Object as the super class of Class
78 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070079 // mark as non-primitive for object_array_class
80 java_lang_Object->primitive_type_ = Class::kPrimNot;
Brian Carlstroma0808032011-07-18 00:39:23 -070081
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070082 // object_array_class is for root_classes to provide the storage for these classes
Brian Carlstrom4873d462011-08-21 15:23:39 -070083 Class* object_array_class = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070084 CHECK(object_array_class != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070085 object_array_class->component_type_ = java_lang_Object;
Brian Carlstroma0808032011-07-18 00:39:23 -070086
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070087 // String and char[] are necessary so that FindClass can assign names to members
Brian Carlstrom4873d462011-08-21 15:23:39 -070088 Class* java_lang_String = AllocClass(java_lang_Class, sizeof(StringClass));
Jesse Wilson14150742011-07-29 19:04:44 -040089 CHECK(java_lang_String != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070090 CHECK_LT(java_lang_String->object_size_, sizeof(String));
Jesse Wilson14150742011-07-29 19:04:44 -040091 java_lang_String->object_size_ = sizeof(String);
Brian Carlstroma663ea52011-08-19 23:33:41 -070092 String::SetClass(java_lang_String);
Brian Carlstrom4873d462011-08-21 15:23:39 -070093 Class* char_array_class = AllocClass(java_lang_Class, sizeof(Class));
Jesse Wilson8989d992011-08-02 13:39:42 -070094 CHECK(char_array_class != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070095 CharArray::SetArrayClass(char_array_class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070096 // Now String::Alloc* can be used
97
98 // backfill Class descriptors missing until this point
99 java_lang_Class->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Class;");
100 java_lang_Object->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Object;");
101 object_array_class->descriptor_ = String::AllocFromModifiedUtf8("[Ljava/lang/Object;");
102 java_lang_String->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/String;");
103 char_array_class->descriptor_ = String::AllocFromModifiedUtf8("[C");
Jesse Wilson14150742011-07-29 19:04:44 -0400104
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700105 // Field and Method are necessary so that FindClass can link members
Brian Carlstrom4873d462011-08-21 15:23:39 -0700106 Class* java_lang_reflect_Field = AllocClass(java_lang_Class, sizeof(FieldClass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700107 CHECK(java_lang_reflect_Field != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700108 java_lang_reflect_Field->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;");
Jesse Wilson35baaab2011-08-10 16:18:03 -0400109 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
110 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700111 Class* java_lang_reflect_Method = AllocClass(java_lang_Class, sizeof(MethodClass));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700112 java_lang_reflect_Method->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700113 CHECK(java_lang_reflect_Method != NULL);
114 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
115 java_lang_reflect_Method->object_size_ = sizeof(Method);
116
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700117 // create storage for root classes, save away our work so far
118 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700119 SetClassRoot(kJavaLangClass, java_lang_Class);
120 SetClassRoot(kJavaLangObject, java_lang_Object);
121 SetClassRoot(kObjectArrayClass, object_array_class);
122 SetClassRoot(kJavaLangString, java_lang_String);
123 SetClassRoot(kCharArrayClass, char_array_class);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700124 SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field);
125 SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700126 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -0700127
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700128 // setup boot_class_path_ now that we can use AllocObjectArray to
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700129 // create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700130 for (size_t i = 0; i != boot_class_path.size(); ++i) {
131 AppendToBootClassPath(boot_class_path[i]);
132 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700133 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700134
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700135 // run Class, Field, and Method through FindSystemClass.
136 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700137 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700138 Class* Class_class = FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700139 CHECK_EQ(java_lang_Class, Class_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700140 CHECK_LT(java_lang_Class->object_size_, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700141 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700142 Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700143 CHECK_EQ(java_lang_reflect_Field, Field_class);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400144 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
145 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700146 Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700147 CHECK_EQ(java_lang_reflect_Method, Method_class);
148 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700149 java_lang_reflect_Method->object_size_ = sizeof(Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700150
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700151 // Object and String just need more minimal setup, since they do not have extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700152 Class* Object_class = FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700153 CHECK_EQ(java_lang_Object, Object_class);
154 CHECK_EQ(java_lang_Object->object_size_, sizeof(Object));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700155 Class* String_class = FindSystemClass("Ljava/lang/String;");
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700156 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700157 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700158
159 // Setup the ClassLoaders, adjusting the object_size_ as necessary
160 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
161 CHECK(java_lang_ClassLoader != NULL);
162 CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader));
163 java_lang_ClassLoader->object_size_ = sizeof(ClassLoader);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700164 SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700165 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
166 CHECK(dalvik_system_BaseDexClassLoader != NULL);
167 CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700168 SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700169 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
170 CHECK(dalvik_system_PathClassLoader != NULL);
171 CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700172 SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700173
174 // Setup a single, global copy of "interfaces" and "iftable" for
175 // reuse across array classes
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700176 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700177 CHECK(java_lang_Cloneable != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700178 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700179 CHECK(java_io_Serializable != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700180
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700181 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700182 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700183 array_interfaces_->Set(0, java_lang_Cloneable);
184 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700185
186 // We assume that Cloneable/Serializable don't have superinterfaces --
187 // normally we'd have to crawl up and explicitly list all of the
188 // supers as well. These interfaces don't have any methods, so we
189 // don't have to worry about the ifviPool either.
190 array_iftable_ = new InterfaceEntry[2];
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700191 array_iftable_[0].SetClass(array_interfaces_->Get(0));
192 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700193 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700194
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700195 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700196 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
197 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700198 CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0));
199 CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700200
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700201 // Setup the primitive type classes.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700202 SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
203 SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B"));
204 SetClassRoot(kPrimitiveChar, CreatePrimitiveClass("C"));
205 SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D"));
206 SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F"));
207 SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I"));
208 SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J"));
209 SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S"));
210 SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V"));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700211 // now we can use FindSystemClass for anything, including for "[C"
212
Jesse Wilson7833bd22011-08-09 18:31:44 -0400213 // run char[], int[] and long[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700214 Class* found_char_array_class = FindSystemClass("[C");
215 CHECK_EQ(char_array_class, found_char_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700216
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700217 // Initialize all the other primitive array types for PrimitiveArray::Alloc.
218 // These are easy because everything we need has already been set up.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700219 SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z"));
220 SetClassRoot(kByteArrayClass, FindSystemClass("[B"));
221 SetClassRoot(kDoubleArrayClass, FindSystemClass("[D"));
222 SetClassRoot(kFloatArrayClass, FindSystemClass("[F"));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700223 SetClassRoot(kIntArrayClass, FindSystemClass("[I"));
224 SetClassRoot(kLongArrayClass, FindSystemClass("[J"));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700225 SetClassRoot(kShortArrayClass, FindSystemClass("[S"));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700226 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
227 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
228 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
229 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700230 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
231 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700232 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
233
Brian Carlstroma663ea52011-08-19 23:33:41 -0700234 FinishInit();
235}
236
237void ClassLinker::FinishInit() {
238 // ensure all class_roots_ are initialized
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700239 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700240 ClassRoot class_root = static_cast<ClassRoot>(i);
241 Class* klass = GetClassRoot(class_root);
242 CHECK(klass != NULL);
243 DCHECK(klass->IsArray() || klass->IsPrimitive() || klass->dex_cache_ != NULL);
244 // note SetClassRoot does additional validation.
245 // if possible add new checks there to catch errors early
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700246 }
247
248 // disable the slow paths in FindClass and CreatePrimitiveClass now
249 // that Object, Class, and Object[] are setup
250 init_done_ = true;
251}
252
Brian Carlstroma663ea52011-08-19 23:33:41 -0700253struct ClassLinker::InitCallbackState {
254 ClassLinker* class_linker;
255
256 Class* class_roots[kClassRootsMax];
257
258 typedef std::tr1::unordered_map<std::string, ClassRoot> Table;
259 Table descriptor_to_class_root;
260
261 struct DexCacheHash {
262 size_t operator()(art::DexCache* const& obj) const {
263 return reinterpret_cast<size_t>(&obj);
264 }
265 };
266 typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set;
267 Set dex_caches;
268};
269
270void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path, Space* space) {
271 CHECK(!init_done_);
272
273 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
274 DCHECK(heap_bitmap != NULL);
275
276 InitCallbackState state;
277 state.class_linker = this;
278 for (size_t i = 0; i < kClassRootsMax; i++) {
279 ClassRoot class_root = static_cast<ClassRoot>(i);
280 state.descriptor_to_class_root[GetClassRootDescriptor(class_root)] = class_root;
281 }
282
283 // reinit clases_ table
284 heap_bitmap->Walk(InitCallback, &state);
285
286 // reinit class_roots_
287 Class* object_array_class = state.class_roots[kObjectArrayClass];
288 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
289 for (size_t i = 0; i < kClassRootsMax; i++) {
290 ClassRoot class_root = static_cast<ClassRoot>(i);
291 SetClassRoot(class_root, state.class_roots[class_root]);
292 }
293
294 // reinit intern_table_
295 ObjectArray<Object>* interned_array = space->GetImageHeader().GetInternedArray();
296 for (int32_t i = 0; i < interned_array->GetLength(); i++) {
297 String* string = interned_array->Get(i)->AsString();
298 intern_table_.Register(string);
299 }
300
301 // reinit array_interfaces_ from any array class instance, they should all be ==
302 array_interfaces_ = GetClassRoot(kObjectArrayClass)->interfaces_;
303 DCHECK(array_interfaces_ == GetClassRoot(kBooleanArrayClass)->interfaces_);
304
305 // build a map from location to DexCache to match up with DexFile::GetLocation
306 std::tr1::unordered_map<std::string, DexCache*> location_to_dex_cache;
307 typedef InitCallbackState::Set::const_iterator It; // TODO: C++0x auto
308 for (It it = state.dex_caches.begin(), end = state.dex_caches.end(); it != end; ++it) {
309 DexCache* dex_cache = *it;
310 std::string location = dex_cache->GetLocation()->ToModifiedUtf8();
311 location_to_dex_cache[location] = dex_cache;
312 }
313
314 // reinit boot_class_path with DexFile arguments and found DexCaches
315 for (size_t i = 0; i != boot_class_path.size(); ++i) {
316 DexFile* dex_file = boot_class_path[i];
317 DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()];
318 AppendToBootClassPath(dex_file, dex_cache);
319 }
320
321 String::SetClass(GetClassRoot(kJavaLangString));
322 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
323 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
324 CharArray::SetArrayClass(GetClassRoot(kCharArrayClass));
325 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
326 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
327 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
328 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
329 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
330
331 FinishInit();
332}
333
Brian Carlstrom4873d462011-08-21 15:23:39 -0700334void ClassLinker::InitCallback(Object* obj, void *arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700335 DCHECK(obj != NULL);
336 DCHECK(arg != NULL);
337 InitCallbackState* state = reinterpret_cast<InitCallbackState*>(arg);
338
339 if (!obj->IsClass()) {
340 return;
341 }
342 Class* klass = obj->AsClass();
343 CHECK(klass->class_loader_ == NULL);
344
345 std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8();
346
347 // restore class to ClassLinker::classes_ table
348 state->class_linker->InsertClass(descriptor, klass);
349
350 // note DexCache to match with DexFile later
351 DexCache* dex_cache = klass->GetDexCache();
352 if (dex_cache != NULL) {
353 state->dex_caches.insert(dex_cache);
354 } else {
355 DCHECK(klass->IsArray() || klass->IsPrimitive());
356 }
357
358 // check if this is a root, if so, register it
359 typedef InitCallbackState::Table::const_iterator It; // TODO: C++0x auto
360 It it = state->descriptor_to_class_root.find(descriptor);
361 if (it != state->descriptor_to_class_root.end()) {
362 ClassRoot class_root = it->second;
363 state->class_roots[class_root] = klass;
364 }
365}
366
367// Keep in sync with InitCallback. Anything we visit, we need to
368// reinit references to when reinitializing a ClassLinker from a
369// mapped image.
370void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) const {
371
Brian Carlstromb88e9442011-07-28 15:15:51 -0700372 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700373
374 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700375 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700376 }
377
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700378 {
379 MutexLock mu(classes_lock_);
380 typedef Table::const_iterator It; // TODO: C++0x auto
381 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
382 root_visitor(it->second, arg);
383 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700384 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700385
386 intern_table_.VisitRoots(root_visitor, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700387
Brian Carlstromb88e9442011-07-28 15:15:51 -0700388 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700389}
390
Brian Carlstroma663ea52011-08-19 23:33:41 -0700391DexCache* ClassLinker::AllocDexCache(const DexFile* dex_file) {
392 DexCache* dex_cache = down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
393 dex_cache->Init(String::AllocFromModifiedUtf8(dex_file->GetLocation().c_str()),
394 AllocObjectArray<String>(dex_file->NumStringIds()),
395 AllocObjectArray<Class>(dex_file->NumTypeIds()),
396 AllocObjectArray<Method>(dex_file->NumMethodIds()),
397 AllocObjectArray<Field>(dex_file->NumFieldIds()));
398 return dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700399}
400
Brian Carlstrom4873d462011-08-21 15:23:39 -0700401Class* ClassLinker::AllocClass(Class* java_lang_Class, size_t class_size) {
402 DCHECK_GE(class_size, sizeof(Class));
403 Class* klass = Heap::AllocObject(java_lang_Class, class_size)->AsClass();
404 klass->class_size_ = class_size;
405 return klass;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700406}
407
Brian Carlstrom4873d462011-08-21 15:23:39 -0700408Class* ClassLinker::AllocClass(size_t class_size) {
409 return AllocClass(GetClassRoot(kJavaLangClass), class_size);
Brian Carlstroma0808032011-07-18 00:39:23 -0700410}
411
Jesse Wilson35baaab2011-08-10 16:18:03 -0400412Field* ClassLinker::AllocField() {
413 return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->NewInstance());
Brian Carlstroma0808032011-07-18 00:39:23 -0700414}
415
416Method* ClassLinker::AllocMethod() {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700417 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700418}
419
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700420// TODO: remove once we can use java.lang.Class.getSystemClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700421PathClassLoader* ClassLinker::AllocPathClassLoader(std::vector<const DexFile*> dex_files) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700422 PathClassLoader* cl = down_cast<PathClassLoader*>(GetClassRoot(kDalvikSystemPathClassLoader)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700423 cl->SetClassPath(dex_files);
424 return cl;
Carl Shapiro565f5072011-07-10 13:39:43 -0700425}
426
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700427Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700428 ClassLoader* class_loader) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700429 // TODO: remove this contrived parent class loader check when we have a real ClassLoader.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700430 if (class_loader != NULL) {
431 Class* klass = FindClass(descriptor, NULL);
432 if (klass != NULL) {
433 return klass;
434 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700435 Thread::Current()->ClearException();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700436 }
437
Carl Shapirob5573532011-07-12 18:22:59 -0700438 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700439 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700440 CHECK(!self->IsExceptionPending());
441 // Find the class in the loaded classes table.
442 Class* klass = LookupClass(descriptor, class_loader);
443 if (klass == NULL) {
444 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700445 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700446 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700447 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700448 DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_);
449 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700450 if (pair.second == NULL) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700451 std::string name(PrintableString(descriptor));
452 self->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
453 "Class %s not found in class loader %p", name.c_str(), class_loader);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700454 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700455 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700456 const DexFile& dex_file = *pair.first;
457 const DexFile::ClassDef& dex_class_def = *pair.second;
458 DexCache* dex_cache = FindDexCache(pair.first);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700459 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700460 if (!init_done_) {
461 // finish up init of hand crafted class_roots_
462 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700463 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700464 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700465 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400466 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700467 klass = GetClassRoot(kJavaLangString);
468 } else if (descriptor == "Ljava/lang/reflect/Field;") {
469 klass = GetClassRoot(kJavaLangReflectField);
470 } else if (descriptor == "Ljava/lang/reflect/Method;") {
471 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700472 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700473 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700474 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700475 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700476 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Carl Shapiro565f5072011-07-10 13:39:43 -0700477 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700478 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700479 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700480 // Check for a pending exception during load
481 if (self->IsExceptionPending()) {
482 // TODO: free native allocations in klass
483 return NULL;
484 }
485 {
486 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700487 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700488 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700489 bool success = InsertClass(descriptor, klass); // TODO: just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700490 if (!success) {
491 // We may fail to insert if we raced with another thread.
492 klass->clinit_thread_id_ = 0;
493 // TODO: free native allocations in klass
494 klass = LookupClass(descriptor, class_loader);
495 CHECK(klass != NULL);
496 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700497 // Finish loading (if necessary) by finding parents
498 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
499 // Loading failed.
500 // TODO: CHECK(self->IsExceptionPending());
501 lock.NotifyAll();
502 return NULL;
503 }
504 CHECK(klass->IsLoaded());
505 // Link the class (if necessary)
506 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700507 // Linking failed.
508 // TODO: CHECK(self->IsExceptionPending());
509 lock.NotifyAll();
510 return NULL;
511 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700512 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700513 }
514 }
515 }
516 // Link the class if it has not already been linked.
517 if (!klass->IsLinked() && !klass->IsErroneous()) {
518 ObjectLock lock(klass);
519 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700520 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700521 self->ThrowNewException("Ljava/lang/ClassCircularityError;", NULL); // TODO: detail
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700522 return NULL;
523 }
524 // Wait for the pending initialization to complete.
525 while (!klass->IsLinked() && !klass->IsErroneous()) {
526 lock.Wait();
527 }
528 }
529 if (klass->IsErroneous()) {
530 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
531 return NULL;
532 }
533 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700534 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535 CHECK(!self->IsExceptionPending());
536 return klass;
537}
538
Brian Carlstrom4873d462011-08-21 15:23:39 -0700539// Precomputes size that will be needed for Class, matching LinkStaticFields
540size_t ClassLinker::SizeOfClass(const DexFile& dex_file,
541 const DexFile::ClassDef& dex_class_def) {
542 const byte* class_data = dex_file.GetClassData(dex_class_def);
543 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
544 size_t num_static_fields = header.static_fields_size_;
545 size_t num_ref = 0;
546 size_t num_32 = 0;
547 size_t num_64 = 0;
548 if (num_static_fields != 0) {
549 uint32_t last_idx = 0;
550 for (size_t i = 0; i < num_static_fields; ++i) {
551 DexFile::Field dex_field;
552 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
553 const DexFile::FieldId& field_id = dex_file.GetFieldId(dex_field.field_idx_);
554 const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_);
555 char c = descriptor[0];
556 if (c == 'L' || c == '[') {
557 num_ref++;
558 } else if (c == 'J' || c == 'D') {
559 num_64++;
560 } else {
561 num_32++;
562 }
563 }
564 }
565
566 // start with generic class data
567 size_t size = sizeof(Class);
568 // follow with reference fields which must be contiguous at start
569 size += (num_ref * sizeof(uint32_t));
570 // if there are 64-bit fields to add, make sure they are aligned
571 if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment
572 if (num_32 != 0) {
573 // use an available 32-bit field for padding
574 num_32--;
575 }
576 size += sizeof(uint32_t); // either way, we are adding a word
577 DCHECK_EQ(size, RoundUp(size, 8));
578 }
579 // tack on any 64-bit fields now that alignment is assured
580 size += (num_64 * sizeof(uint64_t));
581 // tack on any remaining 32-bit fields
582 size += (num_32 * sizeof(uint32_t));
583 return size;
584}
585
Brian Carlstromf615a612011-07-23 12:50:34 -0700586void ClassLinker::LoadClass(const DexFile& dex_file,
587 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700588 Class* klass,
589 ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700590 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700591 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700592 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700593 const byte* class_data = dex_file.GetClassData(dex_class_def);
594 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700595
Brian Carlstromf615a612011-07-23 12:50:34 -0700596 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700597 CHECK(descriptor != NULL);
598
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700599 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700600 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -0700601 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700602 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700603 klass->primitive_type_ = Class::kPrimNot;
604 klass->status_ = Class::kStatusIdx;
605
606 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700607 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700608
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700609 size_t num_static_fields = header.static_fields_size_;
610 size_t num_instance_fields = header.instance_fields_size_;
611 size_t num_direct_methods = header.direct_methods_size_;
612 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700613
Brian Carlstromf615a612011-07-23 12:50:34 -0700614 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700615
616 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700617 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700618
619 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700620 DCHECK(klass->sfields_ == NULL);
621 if (num_static_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400622 klass->sfields_ = AllocObjectArray<Field>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700623 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700624 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700625 DexFile::Field dex_field;
626 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400627 Field* sfield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700628 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700629 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700630 }
631 }
632
633 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700634 DCHECK(klass->ifields_ == NULL);
635 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700636 // TODO: allocate on the object heap.
Jesse Wilson35baaab2011-08-10 16:18:03 -0400637 klass->ifields_ = AllocObjectArray<Field>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700638 uint32_t last_idx = 0;
639 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700640 DexFile::Field dex_field;
641 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400642 Field* ifield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700643 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700644 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700645 }
646 }
647
648 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700649 DCHECK(klass->direct_methods_ == NULL);
650 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700651 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700652 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700653 uint32_t last_idx = 0;
654 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700655 DexFile::Method dex_method;
656 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700657 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700658 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700659 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700660 // TODO: register maps
661 }
662 }
663
664 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700665 DCHECK(klass->virtual_methods_ == NULL);
666 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700667 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700668 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700669 uint32_t last_idx = 0;
670 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700671 DexFile::Method dex_method;
672 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700673 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700674 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700675 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700676 // TODO: register maps
677 }
678 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700679}
680
Brian Carlstromf615a612011-07-23 12:50:34 -0700681void ClassLinker::LoadInterfaces(const DexFile& dex_file,
682 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700683 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700684 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700685 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700686 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700687 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700688 DCHECK(klass->interfaces_idx_ == NULL);
689 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700690 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700691 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700692 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700693 }
694 }
695}
696
Brian Carlstromf615a612011-07-23 12:50:34 -0700697void ClassLinker::LoadField(const DexFile& dex_file,
698 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700699 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700700 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700701 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400702 dst->declaring_class_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700703 dst->name_ = ResolveString(klass, field_id.name_idx_, dex_file);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700704 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Jesse Wilson35baaab2011-08-10 16:18:03 -0400705 // TODO: Assign dst->type_.
Brian Carlstrom934486c2011-07-12 23:42:50 -0700706 dst->access_flags_ = src.access_flags_;
707}
708
Brian Carlstromf615a612011-07-23 12:50:34 -0700709void ClassLinker::LoadMethod(const DexFile& dex_file,
710 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700711 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700712 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700713 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400714 dst->declaring_class_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700715 dst->name_ = ResolveString(klass, method_id.name_idx_, dex_file);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700716 {
717 int32_t utf16_length;
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700718 scoped_array<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700719 &utf16_length));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700720 dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700721 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700722 dst->proto_idx_ = method_id.proto_idx_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700723 dst->code_off_ = src.code_off_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700724 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700725 dst->access_flags_ = src.access_flags_;
726
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700727 dst->dex_cache_strings_ = klass->dex_cache_->GetStrings();
728 dst->dex_cache_classes_ = klass->dex_cache_->GetClasses();
729 dst->dex_cache_methods_ = klass->dex_cache_->GetMethods();
730 dst->dex_cache_fields_ = klass->dex_cache_->GetFields();
731
Brian Carlstrom934486c2011-07-12 23:42:50 -0700732 // TODO: check for finalize method
733
Brian Carlstromf615a612011-07-23 12:50:34 -0700734 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700735 if (code_item != NULL) {
736 dst->num_registers_ = code_item->registers_size_;
737 dst->num_ins_ = code_item->ins_size_;
738 dst->num_outs_ = code_item->outs_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700739 } else {
740 uint16_t num_args = dst->NumArgRegisters();
741 if (!dst->IsStatic()) {
742 ++num_args;
743 }
744 dst->num_registers_ = dst->num_ins_ + num_args;
745 // TODO: native methods
746 }
747}
748
Brian Carlstroma663ea52011-08-19 23:33:41 -0700749void ClassLinker::AppendToBootClassPath(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700750 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700751 AppendToBootClassPath(dex_file, AllocDexCache(dex_file));
752}
753
754void ClassLinker::AppendToBootClassPath(const DexFile* dex_file, DexCache* dex_cache) {
755 CHECK(dex_file != NULL);
756 CHECK(dex_cache != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700757 boot_class_path_.push_back(dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700758 RegisterDexFile(dex_file, dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700759}
760
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700761void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700762 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700763 RegisterDexFile(dex_file, AllocDexCache(dex_file));
764}
765
766void ClassLinker::RegisterDexFile(const DexFile* dex_file, DexCache* dex_cache) {
767 CHECK(dex_file != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700768 CHECK(dex_cache != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700769 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700770 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700771}
772
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700773const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700774 for (size_t i = 0; i != dex_caches_.size(); ++i) {
775 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700776 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700777 }
778 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700779 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700780 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700781}
782
Brian Carlstromf615a612011-07-23 12:50:34 -0700783DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700784 for (size_t i = 0; i != dex_files_.size(); ++i) {
785 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700786 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700787 }
788 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700789 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700790 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700791}
792
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700793Class* ClassLinker::CreatePrimitiveClass(const char* descriptor) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700794 Class* klass = AllocClass(sizeof(Class));
Carl Shapiro565f5072011-07-10 13:39:43 -0700795 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700796 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700797 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700798 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Carl Shapiro565f5072011-07-10 13:39:43 -0700799 klass->status_ = Class::kStatusInitialized;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700800 bool success = InsertClass(descriptor, klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700801 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700802 return klass;
803}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700804
Brian Carlstrombe977852011-07-19 14:54:54 -0700805// Create an array class (i.e. the class object for the array, not the
806// array itself). "descriptor" looks like "[C" or "[[[[B" or
807// "[Ljava/lang/String;".
808//
809// If "descriptor" refers to an array of primitives, look up the
810// primitive type's internally-generated class object.
811//
812// "loader" is the class loader of the class that's referring to us. It's
813// used to ensure that we're looking for the element type in the right
814// context. It does NOT become the class loader for the array class; that
815// always comes from the base element class.
816//
817// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700818Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700819 ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700820{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700821 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700822
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700823 // Identify the underlying element class and the array dimension depth.
824 Class* component_type_ = NULL;
825 int array_rank;
826 if (descriptor[1] == '[') {
827 // array of arrays; keep descriptor and grab stuff from parent
828 Class* outer = FindClass(descriptor.substr(1), class_loader);
829 if (outer != NULL) {
830 // want the base class, not "outer", in our component_type_
831 component_type_ = outer->component_type_;
832 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700833 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700834 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700835 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700836 } else {
837 array_rank = 1;
838 if (descriptor[1] == 'L') {
839 // array of objects; strip off "[" and look up descriptor.
840 const StringPiece subDescriptor = descriptor.substr(1);
841 component_type_ = FindClass(subDescriptor, class_loader);
842 } else {
843 // array of a primitive type
844 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700845 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700846 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700847
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700848 if (component_type_ == NULL) {
849 // failed
850 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
851 return NULL;
852 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700853
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700854 // See if the component type is already loaded. Array classes are
855 // always associated with the class loader of their underlying
856 // element type -- an array of Strings goes with the loader for
857 // java/lang/String -- so we need to look for it there. (The
858 // caller should have checked for the existence of the class
859 // before calling here, but they did so with *their* class loader,
860 // not the component type's loader.)
861 //
862 // If we find it, the caller adds "loader" to the class' initiating
863 // loader list, which should prevent us from going through this again.
864 //
865 // This call is unnecessary if "loader" and "component_type_->class_loader_"
866 // are the same, because our caller (FindClass) just did the
867 // lookup. (Even if we get this wrong we still have correct behavior,
868 // because we effectively do this lookup again when we add the new
869 // class to the hash table --- necessary because of possible races with
870 // other threads.)
871 if (class_loader != component_type_->class_loader_) {
872 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
873 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700874 return new_class;
875 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700876 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700877
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700878 // Fill out the fields in the Class.
879 //
880 // It is possible to execute some methods against arrays, because
881 // all arrays are subclasses of java_lang_Object_, so we need to set
882 // up a vtable. We can just point at the one in java_lang_Object_.
883 //
884 // Array classes are simple enough that we don't need to do a full
885 // link step.
886
887 Class* new_class = NULL;
888 if (!init_done_) {
889 if (descriptor == "[Ljava/lang/Object;") {
890 new_class = GetClassRoot(kObjectArrayClass);
891 } else if (descriptor == "[C") {
892 new_class = GetClassRoot(kCharArrayClass);
893 }
894 }
895 if (new_class == NULL) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700896 new_class = AllocClass(sizeof(Class));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700897 if (new_class == NULL) {
898 return NULL;
899 }
900 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700901 new_class->descriptor_ = String::AllocFromModifiedUtf8(descriptor.ToString().c_str());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700902 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
903 new_class->super_class_ = java_lang_Object;
904 new_class->vtable_ = java_lang_Object->vtable_;
905 new_class->primitive_type_ = Class::kPrimNot;
906 new_class->component_type_ = component_type_;
907 new_class->class_loader_ = component_type_->class_loader_;
908 new_class->array_rank_ = array_rank;
909 new_class->status_ = Class::kStatusInitialized;
910 // don't need to set new_class->object_size_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700911 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700912
913
914 // All arrays have java/lang/Cloneable and java/io/Serializable as
915 // interfaces. We need to set that up here, so that stuff like
916 // "instanceof" works right.
917 //
918 // Note: The GC could run during the call to FindSystemClass,
919 // so we need to make sure the class object is GC-valid while we're in
920 // there. Do this by clearing the interface list so the GC will just
921 // think that the entries are null.
922
923
924 // Use the single, global copies of "interfaces" and "iftable"
925 // (remember not to free them for arrays).
926 DCHECK(array_interfaces_ != NULL);
927 new_class->interfaces_ = array_interfaces_;
928 new_class->iftable_count_ = 2;
929 DCHECK(array_iftable_ != NULL);
930 new_class->iftable_ = array_iftable_;
931
932 // Inherit access flags from the component type. Arrays can't be
933 // used as a superclass or interface, so we want to add "final"
934 // and remove "interface".
935 //
936 // Don't inherit any non-standard flags (e.g., kAccFinal)
937 // from component_type_. We assume that the array class does not
938 // override finalize().
939 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
940 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
941
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700942 if (InsertClass(descriptor, new_class)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700943 return new_class;
944 }
945 // Another thread must have loaded the class after we
946 // started but before we finished. Abandon what we've
947 // done.
948 //
949 // (Yes, this happens.)
950
951 // Grab the winning class.
952 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
953 DCHECK(other_class != NULL);
954 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700955}
956
957Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700958 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700959 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700960 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700961 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700962 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700963 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700964 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700965 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700966 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700967 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700968 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700969 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700970 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700971 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700972 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700973 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700974 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700975 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700976 return GetClassRoot(kPrimitiveVoid);
Carl Shapiro744ad052011-08-06 15:53:36 -0700977 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700978 std::string printable_type(PrintableChar(type));
979 Thread::Current()->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
980 "Not a primitive type: %s", printable_type.c_str());
981 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700982}
983
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700984bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) {
985 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700986 MutexLock mu(classes_lock_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700987 Table::iterator it = classes_.insert(std::make_pair(hash, klass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700988 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700989}
990
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700991Class* ClassLinker::LookupClass(const StringPiece& descriptor, ClassLoader* class_loader) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700992 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700993 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700994 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700995 for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700996 Class* klass = it->second;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700997 if (klass->descriptor_->Equals(descriptor) && klass->class_loader_ == class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700998 return klass;
999 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001000 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001001 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001002}
1003
1004bool ClassLinker::InitializeClass(Class* klass) {
1005 CHECK(klass->GetStatus() == Class::kStatusResolved ||
1006 klass->GetStatus() == Class::kStatusError);
1007
Carl Shapirob5573532011-07-12 18:22:59 -07001008 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001009
1010 {
1011 ObjectLock lock(klass);
1012
1013 if (klass->GetStatus() < Class::kStatusVerified) {
1014 if (klass->IsErroneous()) {
1015 LG << "re-initializing failed class"; // TODO: throw
1016 return false;
1017 }
1018
1019 CHECK(klass->GetStatus() == Class::kStatusResolved);
1020
1021 klass->status_ = Class::kStatusVerifying;
1022 if (!DexVerify::VerifyClass(klass)) {
1023 LG << "Verification failed"; // TODO: ThrowVerifyError
1024 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001025 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001026 klass->SetStatus(Class::kStatusError);
1027 return false;
1028 }
1029
1030 klass->SetStatus(Class::kStatusVerified);
1031 }
1032
1033 if (klass->status_ == Class::kStatusInitialized) {
1034 return true;
1035 }
1036
1037 while (klass->status_ == Class::kStatusInitializing) {
1038 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -07001039 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001040 LG << "recursive <clinit>";
1041 return true;
1042 }
1043
1044 CHECK(!self->IsExceptionPending());
1045
1046 lock.Wait(); // TODO: check for interruption
1047
1048 // When we wake up, repeat the test for init-in-progress. If
1049 // there's an exception pending (only possible if
1050 // "interruptShouldThrow" was set), bail out.
1051 if (self->IsExceptionPending()) {
1052 CHECK(false);
1053 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
1054 klass->SetStatus(Class::kStatusError);
1055 return false;
1056 }
1057 if (klass->GetStatus() == Class::kStatusInitializing) {
1058 continue;
1059 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001060 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001061 klass->GetStatus() == Class::kStatusError);
1062 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001063 // The caller wants an exception, but it was thrown in a
1064 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001065 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
1066 return false;
1067 }
1068 return true; // otherwise, initialized
1069 }
1070
1071 // see if we failed previously
1072 if (klass->IsErroneous()) {
1073 // might be wise to unlock before throwing; depends on which class
1074 // it is that we have locked
1075
1076 // TODO: throwEarlierClassFailure(klass);
1077 return false;
1078 }
1079
1080 if (!ValidateSuperClassDescriptors(klass)) {
1081 klass->SetStatus(Class::kStatusError);
1082 return false;
1083 }
1084
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001085 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001086
Carl Shapirob5573532011-07-12 18:22:59 -07001087 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001088 klass->status_ = Class::kStatusInitializing;
1089 }
1090
1091 if (!InitializeSuperClass(klass)) {
1092 return false;
1093 }
1094
1095 InitializeStaticFields(klass);
1096
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001097 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098 if (clinit != NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001099 // JValue unused;
1100 // TODO: dvmCallMethod(self, method, NULL, &unused);
Elliott Hughes53b61312011-08-12 18:28:20 -07001101 // UNIMPLEMENTED(FATAL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001102 }
1103
1104 {
1105 ObjectLock lock(klass);
1106
1107 if (self->IsExceptionPending()) {
1108 klass->SetStatus(Class::kStatusError);
1109 } else {
1110 klass->SetStatus(Class::kStatusInitialized);
1111 }
1112 lock.NotifyAll();
1113 }
1114
1115 return true;
1116}
1117
1118bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
1119 if (klass->IsInterface()) {
1120 return true;
1121 }
1122 // begin with the methods local to the superclass
1123 if (klass->HasSuperClass() &&
1124 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
1125 const Class* super = klass->GetSuperClass();
1126 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001127 const Method* method = super->GetVirtualMethod(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001128 if (method != super->GetVirtualMethod(i) &&
1129 !HasSameMethodDescriptorClasses(method, super, klass)) {
1130 LG << "Classes resolve differently in superclass";
1131 return false;
1132 }
1133 }
1134 }
1135 for (size_t i = 0; i < klass->iftable_count_; ++i) {
1136 const InterfaceEntry* iftable = &klass->iftable_[i];
1137 Class* interface = iftable->GetClass();
1138 if (klass->GetClassLoader() != interface->GetClassLoader()) {
1139 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1140 uint32_t vtable_index = iftable->method_index_array_[j];
1141 const Method* method = klass->GetVirtualMethod(vtable_index);
1142 if (!HasSameMethodDescriptorClasses(method, interface,
1143 method->GetClass())) {
1144 LG << "Classes resolve differently in interface"; // TODO: LinkageError
1145 return false;
1146 }
1147 }
1148 }
1149 }
1150 return true;
1151}
1152
1153bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001154 const Class* klass1,
1155 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001156 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
1157 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001158 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001159 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001160 const char* descriptor = it->GetDescriptor();
1161 if (descriptor == NULL) {
1162 break;
1163 }
1164 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1165 // Found a non-primitive type.
1166 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1167 return false;
1168 }
1169 }
1170 }
1171 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001172 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001173 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1174 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1175 return false;
1176 }
1177 }
1178 return true;
1179}
1180
1181// Returns true if classes referenced by the descriptor are the
1182// same classes in klass1 as they are in klass2.
1183bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001184 const Class* klass1,
1185 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001186 CHECK(descriptor != NULL);
1187 CHECK(klass1 != NULL);
1188 CHECK(klass2 != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001189 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001190 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001191 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001192 // TODO: found2 == NULL
1193 // TODO: lookup found1 in initiating loader list
1194 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -07001195 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001196 if (found1 == found2) {
1197 return true;
1198 } else {
1199 return false;
1200 }
1201 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001202 return true;
1203}
1204
1205bool ClassLinker::InitializeSuperClass(Class* klass) {
1206 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001207 if (!klass->IsInterface() && klass->HasSuperClass()) {
1208 Class* super_class = klass->GetSuperClass();
1209 if (super_class->GetStatus() != Class::kStatusInitialized) {
1210 CHECK(!super_class->IsInterface());
1211 klass->MonitorExit();
1212 bool super_initialized = InitializeClass(super_class);
1213 klass->MonitorEnter();
1214 // TODO: check for a pending exception
1215 if (!super_initialized) {
1216 klass->SetStatus(Class::kStatusError);
1217 klass->NotifyAll();
1218 return false;
1219 }
1220 }
1221 }
1222 return true;
1223}
1224
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001225bool ClassLinker::EnsureInitialized(Class* c) {
1226 CHECK(c != NULL);
1227 if (c->IsInitialized()) {
1228 return true;
1229 }
1230
1231 c->MonitorExit();
1232 InitializeClass(c);
1233 c->MonitorEnter();
1234 return !Thread::Current()->IsExceptionPending();
1235}
1236
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001237void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001238 size_t num_static_fields = klass->NumStaticFields();
1239 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001240 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001241 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001242 DexCache* dex_cache = klass->GetDexCache();
Brian Carlstrom4873d462011-08-21 15:23:39 -07001243 // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray?
Brian Carlstromf615a612011-07-23 12:50:34 -07001244 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001245 return;
1246 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001247 const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001248 const DexFile& dex_file = FindDexFile(dex_cache);
1249 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001250 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001251 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001252 if (addr == NULL) {
1253 // All this class' static fields have default values.
1254 return;
1255 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001256 size_t array_size = DecodeUnsignedLeb128(&addr);
1257 for (size_t i = 0; i < array_size; ++i) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001258 Field* field = klass->GetStaticField(i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001259 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001260 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001261 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001262 case DexFile::kByte:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001263 field->SetByte(NULL, value.b);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001264 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001265 case DexFile::kShort:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001266 field->SetShort(NULL, value.s);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001267 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001268 case DexFile::kChar:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001269 field->SetChar(NULL, value.c);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001270 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001271 case DexFile::kInt:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001272 field->SetInt(NULL, value.i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001273 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001274 case DexFile::kLong:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001275 field->SetLong(NULL, value.j);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001276 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001277 case DexFile::kFloat:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001278 field->SetFloat(NULL, value.f);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001279 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001280 case DexFile::kDouble:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001281 field->SetDouble(NULL, value.d);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001282 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001283 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001284 uint32_t string_idx = value.i;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001285 String* resolved = ResolveString(klass, string_idx, dex_file);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001286 field->SetObject(NULL, resolved);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001287 break;
1288 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001289 case DexFile::kBoolean:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001290 field->SetBoolean(NULL, value.z);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001291 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001292 case DexFile::kNull:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001293 field->SetObject(NULL, value.l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001294 break;
1295 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001296 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001297 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001298 }
1299}
1300
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001301bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1302 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001303 if (!LinkSuperClass(klass)) {
1304 return false;
1305 }
1306 if (!LinkMethods(klass)) {
1307 return false;
1308 }
1309 if (!LinkInstanceFields(klass)) {
1310 return false;
1311 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001312 if (!LinkStaticFields(klass)) {
1313 return false;
1314 }
1315 CreateReferenceInstanceOffsets(klass);
1316 CreateReferenceStaticOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001317 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001318 klass->status_ = Class::kStatusResolved;
1319 return true;
1320}
1321
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001322bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1323 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001324 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1325 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001326 if (super_class == NULL) {
1327 LG << "Failed to resolve superclass";
1328 return false;
1329 }
1330 klass->super_class_ = super_class; // TODO: write barrier
1331 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001332 if (klass->NumInterfaces() > 0) {
1333 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1334 uint32_t idx = klass->interfaces_idx_[i];
1335 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1336 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001337 LG << "Failed to resolve interface";
1338 return false;
1339 }
1340 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001341 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001342 LG << "Inaccessible interface";
1343 return false;
1344 }
1345 }
1346 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001347 // Mark the class as loaded.
1348 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001349 return true;
1350}
1351
1352bool ClassLinker::LinkSuperClass(Class* klass) {
1353 CHECK(!klass->IsPrimitive());
1354 const Class* super = klass->GetSuperClass();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001355 if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001356 if (super != NULL) {
1357 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1358 return false;
1359 }
1360 // TODO: clear finalize attribute
1361 return true;
1362 }
1363 if (super == NULL) {
1364 LG << "No superclass defined"; // TODO: LinkageError
1365 return false;
1366 }
1367 // Verify
1368 if (super->IsFinal()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001369 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001370 return false;
1371 }
1372 if (super->IsInterface()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001373 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001374 return false;
1375 }
1376 if (!klass->CanAccess(super)) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001377 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001378 return false;
1379 }
1380 return true;
1381}
1382
1383// Populate the class vtable and itable.
1384bool ClassLinker::LinkMethods(Class* klass) {
1385 if (klass->IsInterface()) {
1386 // No vtable.
1387 size_t count = klass->NumVirtualMethods();
1388 if (!IsUint(16, count)) {
1389 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1390 return false;
1391 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001392 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001393 klass->GetVirtualMethod(i)->method_index_ = i;
1394 }
1395 } else {
1396 // Link virtual method tables
1397 LinkVirtualMethods(klass);
1398
1399 // Link interface method tables
1400 LinkInterfaceMethods(klass);
1401
1402 // Insert stubs.
1403 LinkAbstractMethods(klass);
1404 }
1405 return true;
1406}
1407
1408bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001409 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001410 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001411 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1412 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001413 // TODO: do not assign to the vtable field until it is fully constructed.
1414 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001415 // See if any of our virtual methods override the superclass.
1416 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1417 Method* local_method = klass->GetVirtualMethod(i);
1418 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001419 for (; j < actual_count; ++j) {
1420 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001421 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001422 // Verify
1423 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001424 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001425 return false;
1426 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001427 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001428 local_method->method_index_ = j;
1429 break;
1430 }
1431 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001432 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001433 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001434 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001435 local_method->method_index_ = actual_count;
1436 actual_count += 1;
1437 }
1438 }
1439 if (!IsUint(16, actual_count)) {
1440 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1441 return false;
1442 }
1443 CHECK_LE(actual_count, max_count);
1444 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001445 // TODO: do not assign to the vtable field until it is fully constructed.
1446 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001447 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001448 } else {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001449 CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001450 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001451 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001452 LG << "Too many methods"; // TODO: VirtualMachineError
1453 return false;
1454 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001455 // TODO: do not assign to the vtable field until it is fully constructed.
1456 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1457 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001458 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1460 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001461 }
1462 return true;
1463}
1464
1465bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1466 int pool_offset = 0;
1467 int pool_size = 0;
1468 int miranda_count = 0;
1469 int miranda_alloc = 0;
1470 size_t super_ifcount;
1471 if (klass->HasSuperClass()) {
1472 super_ifcount = klass->GetSuperClass()->iftable_count_;
1473 } else {
1474 super_ifcount = 0;
1475 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001476 size_t ifcount = super_ifcount;
1477 ifcount += klass->NumInterfaces();
1478 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1479 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001480 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001481 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001482 DCHECK(klass->iftable_count_ == 0);
1483 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001484 return true;
1485 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001486 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1487 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001488 if (super_ifcount != 0) {
1489 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1490 sizeof(InterfaceEntry) * super_ifcount);
1491 }
1492 // Flatten the interface inheritance hierarchy.
1493 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001494 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1495 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001496 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001497 if (!interf->IsInterface()) {
1498 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1499 return false;
1500 }
1501 klass->iftable_[idx++].SetClass(interf);
1502 for (size_t j = 0; j < interf->iftable_count_; j++) {
1503 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1504 }
1505 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001506 CHECK_EQ(idx, ifcount);
1507 klass->iftable_count_ = ifcount;
1508 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001509 return true;
1510 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001511 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001512 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1513 }
1514 if (pool_size == 0) {
1515 return true;
1516 }
1517 klass->ifvi_pool_count_ = pool_size;
1518 klass->ifvi_pool_ = new uint32_t[pool_size];
1519 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001520 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001521 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1522 Class* interface = klass->iftable_[i].GetClass();
1523 pool_offset += interface->NumVirtualMethods(); // end here
1524 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1525 Method* interface_method = interface->GetVirtualMethod(j);
1526 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001527 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001528 Method* vtable_method = klass->vtable_->Get(k);
1529 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1530 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001531 LG << "Implementation not public";
1532 return false;
1533 }
1534 klass->iftable_[i].method_index_array_[j] = k;
1535 break;
1536 }
1537 }
1538 if (k < 0) {
1539 if (miranda_count == miranda_alloc) {
1540 miranda_alloc += 8;
1541 if (miranda_list.empty()) {
1542 miranda_list.resize(miranda_alloc);
1543 } else {
1544 miranda_list.resize(miranda_alloc);
1545 }
1546 }
1547 int mir;
1548 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001549 Method* miranda_method = miranda_list[mir];
1550 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001551 break;
1552 }
1553 }
1554 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001555 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001556 if (mir == miranda_count) {
1557 miranda_list[miranda_count++] = interface_method;
1558 }
1559 }
1560 }
1561 }
1562 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001563 int old_method_count = klass->NumVirtualMethods();
1564 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001565 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001566
1567 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001568 int old_vtable_count = klass->vtable_->GetLength();
1569 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001570 // TODO: do not assign to the vtable field until it is fully constructed.
1571 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001572
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001573 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001574 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001575 memcpy(meth, miranda_list[i], sizeof(Method));
1576 meth->klass_ = klass;
1577 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001578 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1579 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001580 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001581 }
1582 }
1583 return true;
1584}
1585
1586void ClassLinker::LinkAbstractMethods(Class* klass) {
1587 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1588 Method* method = klass->GetVirtualMethod(i);
1589 if (method->IsAbstract()) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001590 LG << "AbstractMethodError";
1591 method->code_off_ = 0xFFFFFFFF;
1592 // TODO: throw AbstractMethodError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001593 }
1594 }
1595}
1596
1597bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001598 CHECK(klass != NULL);
1599 size_t field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001600 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001601 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001602 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001603 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001604 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001605 return LinkFields(field_offset,
1606 klass->num_reference_instance_fields_,
1607 klass->NumInstanceFields(),
1608 klass->ifields_,
1609 klass->object_size_);
1610}
1611
1612bool ClassLinker::LinkStaticFields(Class* klass) {
1613 CHECK(klass != NULL);
1614 size_t allocated_class_size = klass->class_size_;
1615 size_t field_offset = OFFSETOF_MEMBER(Class, fields_);
1616 bool success = LinkFields(field_offset,
1617 klass->num_reference_static_fields_,
1618 klass->NumStaticFields(),
1619 klass->sfields_,
1620 klass->class_size_);
1621 CHECK_EQ(allocated_class_size, klass->class_size_);
1622 return success;
1623}
1624
1625bool ClassLinker::LinkFields(size_t field_offset,
1626 size_t& num_reference_fields,
1627 size_t num_fields,
1628 ObjectArray<Field>* fields,
1629 size_t& size) {
1630 CHECK((num_fields == 0) == (fields == NULL));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001631 // Move references to the front.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001632 num_reference_fields = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001633 size_t i = 0;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001634 for ( ; i < num_fields; i++) {
1635 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001636 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001637 if (c != '[' && c != 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001638 for (size_t j = num_fields - 1; j > i; j--) {
1639 Field* refField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001640 char rc = refField->GetType();
1641 if (rc == '[' || rc == 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001642 fields->Set(i, refField);
1643 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001644 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001645 c = rc;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001646 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001647 break;
1648 }
1649 }
1650 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001651 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001652 }
1653 if (c != '[' && c != 'L') {
1654 break;
1655 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001656 pField->SetOffset(field_offset);
1657 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001658 }
1659
1660 // Now we want to pack all of the double-wide fields together. If
1661 // we're not aligned, though, we want to shuffle one 32-bit field
1662 // into place. If we can't find one, we'll have to pad it.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001663 if (i != num_fields && (field_offset & 0x04) != 0) {
1664 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001665 char c = pField->GetType();
1666
1667 if (c != 'J' && c != 'D') {
1668 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001669 DCHECK(c != '[');
1670 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001671 pField->SetOffset(field_offset);
1672 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001673 i++;
1674 } else {
1675 // Next field is 64-bit, so search for a 32-bit field we can
1676 // swap into it.
1677 bool found = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001678 for (size_t j = num_fields - 1; j > i; j--) {
1679 Field* singleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001680 char rc = singleField->GetType();
1681 if (rc != 'J' && rc != 'D') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001682 fields->Set(i, singleField);
1683 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001684 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001685 pField->SetOffset(field_offset);
1686 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001687 found = true;
1688 i++;
1689 break;
1690 }
1691 }
1692 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001693 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001694 }
1695 }
1696 }
1697
1698 // Alignment is good, shuffle any double-wide fields forward, and
1699 // finish assigning field offsets to all fields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001700 DCHECK(i == num_fields || (field_offset & 0x04) == 0);
1701 for ( ; i < num_fields; i++) {
1702 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001703 char c = pField->GetType();
1704 if (c != 'D' && c != 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001705 for (size_t j = num_fields - 1; j > i; j--) {
1706 Field* doubleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001707 char rc = doubleField->GetType();
1708 if (rc == 'D' || rc == 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001709 fields->Set(i, doubleField);
1710 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001711 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001712 c = rc;
1713 break;
1714 }
1715 }
1716 } else {
1717 // This is a double-wide field, leave it be.
1718 }
1719
Brian Carlstroma0808032011-07-18 00:39:23 -07001720 pField->SetOffset(field_offset);
1721 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001722 if (c == 'J' || c == 'D') {
Brian Carlstroma0808032011-07-18 00:39:23 -07001723 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001724 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001725 }
1726
1727#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001728 // Make sure that all reference fields appear before
1729 // non-reference fields, and all double-wide fields are aligned.
1730 bool seen_non_ref = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001731 for (i = 0; i < num_fields; i++) {
1732 Field *pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001733 char c = pField->GetType();
1734
1735 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001736 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001737 }
1738
1739 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001740 if (!seen_non_ref) {
1741 seen_non_ref = true;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001742 DCHECK_EQ(num_reference_fields, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001743 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001744 } else {
1745 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001746 }
1747 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001748 if (!seen_non_ref) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001749 DCHECK_EQ(num_fields, num_reference_fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001750 }
1751#endif
Brian Carlstrom4873d462011-08-21 15:23:39 -07001752 size = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001753 return true;
1754}
1755
1756// Set the bitmap of reference offsets, refOffsets, from the ifields
1757// list.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001758void ClassLinker::CreateReferenceInstanceOffsets(Class* klass) {
1759 klass->reference_instance_offsets_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001760 if (klass->HasSuperClass()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001761 klass->reference_instance_offsets_ = klass->GetSuperClass()->GetReferenceInstanceOffsets();
1762 // If our superclass overflowed, we don't stand a chance.
1763 if (klass->reference_instance_offsets_ == CLASS_WALK_SUPER) {
1764 return;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001765 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001766 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001767 CreateReferenceOffsets(klass->reference_instance_offsets_,
1768 klass->NumReferenceInstanceFields(),
1769 klass->ifields_);
1770}
1771
1772void ClassLinker::CreateReferenceStaticOffsets(Class* klass) {
1773 klass->reference_static_offsets_ = 0;
1774 CreateReferenceOffsets(klass->reference_static_offsets_,
1775 klass->NumReferenceStaticFields(),
1776 klass->sfields_);
1777}
1778
1779void ClassLinker::CreateReferenceOffsets(uint32_t& reference_offsets,
1780 size_t num_reference_fields,
1781 const ObjectArray<Field>* fields) {
1782 // All of the fields that contain object references are guaranteed
1783 // to be at the beginning of the fields list.
1784 for (size_t i = 0; i < num_reference_fields; ++i) {
1785 // Note that byte_offset is the offset from the beginning of
1786 // object, not the offset into instance data
1787 const Field* field = fields->Get(i);
1788 size_t byte_offset = field->GetOffset();
1789 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
1790 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
1791 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1792 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
1793 CHECK_NE(new_bit, 0U);
1794 reference_offsets |= new_bit;
1795 } else {
1796 reference_offsets = CLASS_WALK_SUPER;
1797 break;
1798 }
1799 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001800}
1801
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001802Class* ClassLinker::ResolveClass(const Class* referrer,
1803 uint32_t class_idx,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001804 const DexFile& dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001805 DexCache* dex_cache = referrer->GetDexCache();
1806 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001807 if (resolved != NULL) {
1808 return resolved;
1809 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001810 const char* descriptor = dex_file.dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001811 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001812 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001813 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001814 resolved = FindClass(descriptor, referrer->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001815 }
1816 if (resolved != NULL) {
1817 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001818 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001819 if (check->GetClassLoader() != NULL) {
1820 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1821 return NULL;
1822 }
1823 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001824 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001825 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001826 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001827 }
1828 return resolved;
1829}
1830
1831Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1832 /*MethodType*/ int method_type) {
1833 CHECK(false);
1834 return NULL;
1835}
1836
Carl Shapiro69759ea2011-07-21 18:13:35 -07001837String* ClassLinker::ResolveString(const Class* referring,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001838 uint32_t string_idx,
1839 const DexFile& dex_file) {
1840 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1841 int32_t utf16_length = dex_file.GetStringLength(string_id);
1842 const char* utf8_data = dex_file.GetStringData(string_id);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001843 String* string = intern_table_.Intern(utf16_length, utf8_data);
1844 referring->GetDexCache()->SetResolvedString(string_idx, string);
1845 return string;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001846}
1847
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001848} // namespace art