blob: c28fc6588b4cb0705cfe07c5518d43dcac3369a1 [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) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400636 klass->ifields_ = AllocObjectArray<Field>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700637 uint32_t last_idx = 0;
638 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700639 DexFile::Field dex_field;
640 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400641 Field* ifield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700642 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700643 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700644 }
645 }
646
647 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700648 DCHECK(klass->direct_methods_ == NULL);
649 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700650 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700651 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700652 uint32_t last_idx = 0;
653 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700654 DexFile::Method dex_method;
655 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700656 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700657 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700658 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700659 // TODO: register maps
660 }
661 }
662
663 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700664 DCHECK(klass->virtual_methods_ == NULL);
665 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700666 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700667 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700668 uint32_t last_idx = 0;
669 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700670 DexFile::Method dex_method;
671 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700672 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700673 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700674 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700675 // TODO: register maps
676 }
677 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700678}
679
Brian Carlstromf615a612011-07-23 12:50:34 -0700680void ClassLinker::LoadInterfaces(const DexFile& dex_file,
681 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700682 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700683 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700684 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700685 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700686 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700687 DCHECK(klass->interfaces_idx_ == NULL);
688 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700689 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700690 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700691 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700692 }
693 }
694}
695
Brian Carlstromf615a612011-07-23 12:50:34 -0700696void ClassLinker::LoadField(const DexFile& dex_file,
697 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700698 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700699 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700700 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400701 dst->declaring_class_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700702 dst->name_ = ResolveString(klass, field_id.name_idx_, dex_file);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700703 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Jesse Wilson35baaab2011-08-10 16:18:03 -0400704 // TODO: Assign dst->type_.
Brian Carlstrom934486c2011-07-12 23:42:50 -0700705 dst->access_flags_ = src.access_flags_;
706}
707
Brian Carlstromf615a612011-07-23 12:50:34 -0700708void ClassLinker::LoadMethod(const DexFile& dex_file,
709 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700710 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700711 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700712 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400713 dst->declaring_class_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700714 dst->name_ = ResolveString(klass, method_id.name_idx_, dex_file);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700715 {
716 int32_t utf16_length;
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700717 scoped_array<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700718 &utf16_length));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700719 dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700720 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700721 dst->proto_idx_ = method_id.proto_idx_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700722 dst->code_off_ = src.code_off_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700723 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700724 dst->access_flags_ = src.access_flags_;
725
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700726 dst->dex_cache_strings_ = klass->dex_cache_->GetStrings();
727 dst->dex_cache_classes_ = klass->dex_cache_->GetClasses();
728 dst->dex_cache_methods_ = klass->dex_cache_->GetMethods();
729 dst->dex_cache_fields_ = klass->dex_cache_->GetFields();
730
Brian Carlstrom934486c2011-07-12 23:42:50 -0700731 // TODO: check for finalize method
732
Brian Carlstromf615a612011-07-23 12:50:34 -0700733 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700734 if (code_item != NULL) {
735 dst->num_registers_ = code_item->registers_size_;
736 dst->num_ins_ = code_item->ins_size_;
737 dst->num_outs_ = code_item->outs_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700738 } else {
739 uint16_t num_args = dst->NumArgRegisters();
740 if (!dst->IsStatic()) {
741 ++num_args;
742 }
743 dst->num_registers_ = dst->num_ins_ + num_args;
744 // TODO: native methods
745 }
746}
747
Brian Carlstroma663ea52011-08-19 23:33:41 -0700748void ClassLinker::AppendToBootClassPath(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700749 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700750 AppendToBootClassPath(dex_file, AllocDexCache(dex_file));
751}
752
753void ClassLinker::AppendToBootClassPath(const DexFile* dex_file, DexCache* dex_cache) {
754 CHECK(dex_file != NULL);
755 CHECK(dex_cache != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700756 boot_class_path_.push_back(dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700757 RegisterDexFile(dex_file, dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700758}
759
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700760void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700761 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700762 RegisterDexFile(dex_file, AllocDexCache(dex_file));
763}
764
765void ClassLinker::RegisterDexFile(const DexFile* dex_file, DexCache* dex_cache) {
766 CHECK(dex_file != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700767 CHECK(dex_cache != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700768 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700769 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700770}
771
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700772const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700773 for (size_t i = 0; i != dex_caches_.size(); ++i) {
774 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700775 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700776 }
777 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700778 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700779 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700780}
781
Brian Carlstromf615a612011-07-23 12:50:34 -0700782DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700783 for (size_t i = 0; i != dex_files_.size(); ++i) {
784 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700785 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700786 }
787 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700788 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700789 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700790}
791
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700792Class* ClassLinker::CreatePrimitiveClass(const char* descriptor) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700793 Class* klass = AllocClass(sizeof(Class));
Carl Shapiro565f5072011-07-10 13:39:43 -0700794 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700795 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700796 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700797 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Carl Shapiro565f5072011-07-10 13:39:43 -0700798 klass->status_ = Class::kStatusInitialized;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700799 bool success = InsertClass(descriptor, klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700800 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700801 return klass;
802}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700803
Brian Carlstrombe977852011-07-19 14:54:54 -0700804// Create an array class (i.e. the class object for the array, not the
805// array itself). "descriptor" looks like "[C" or "[[[[B" or
806// "[Ljava/lang/String;".
807//
808// If "descriptor" refers to an array of primitives, look up the
809// primitive type's internally-generated class object.
810//
811// "loader" is the class loader of the class that's referring to us. It's
812// used to ensure that we're looking for the element type in the right
813// context. It does NOT become the class loader for the array class; that
814// always comes from the base element class.
815//
816// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700817Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700818 ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700819{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700820 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700821
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700822 // Identify the underlying element class and the array dimension depth.
823 Class* component_type_ = NULL;
824 int array_rank;
825 if (descriptor[1] == '[') {
826 // array of arrays; keep descriptor and grab stuff from parent
827 Class* outer = FindClass(descriptor.substr(1), class_loader);
828 if (outer != NULL) {
829 // want the base class, not "outer", in our component_type_
830 component_type_ = outer->component_type_;
831 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700832 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700833 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700834 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700835 } else {
836 array_rank = 1;
837 if (descriptor[1] == 'L') {
838 // array of objects; strip off "[" and look up descriptor.
839 const StringPiece subDescriptor = descriptor.substr(1);
840 component_type_ = FindClass(subDescriptor, class_loader);
841 } else {
842 // array of a primitive type
843 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700844 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700845 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700846
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700847 if (component_type_ == NULL) {
848 // failed
849 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
850 return NULL;
851 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700852
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700853 // See if the component type is already loaded. Array classes are
854 // always associated with the class loader of their underlying
855 // element type -- an array of Strings goes with the loader for
856 // java/lang/String -- so we need to look for it there. (The
857 // caller should have checked for the existence of the class
858 // before calling here, but they did so with *their* class loader,
859 // not the component type's loader.)
860 //
861 // If we find it, the caller adds "loader" to the class' initiating
862 // loader list, which should prevent us from going through this again.
863 //
864 // This call is unnecessary if "loader" and "component_type_->class_loader_"
865 // are the same, because our caller (FindClass) just did the
866 // lookup. (Even if we get this wrong we still have correct behavior,
867 // because we effectively do this lookup again when we add the new
868 // class to the hash table --- necessary because of possible races with
869 // other threads.)
870 if (class_loader != component_type_->class_loader_) {
871 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
872 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700873 return new_class;
874 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700875 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700876
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700877 // Fill out the fields in the Class.
878 //
879 // It is possible to execute some methods against arrays, because
880 // all arrays are subclasses of java_lang_Object_, so we need to set
881 // up a vtable. We can just point at the one in java_lang_Object_.
882 //
883 // Array classes are simple enough that we don't need to do a full
884 // link step.
885
886 Class* new_class = NULL;
887 if (!init_done_) {
888 if (descriptor == "[Ljava/lang/Object;") {
889 new_class = GetClassRoot(kObjectArrayClass);
890 } else if (descriptor == "[C") {
891 new_class = GetClassRoot(kCharArrayClass);
892 }
893 }
894 if (new_class == NULL) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700895 new_class = AllocClass(sizeof(Class));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700896 if (new_class == NULL) {
897 return NULL;
898 }
899 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700900 new_class->descriptor_ = String::AllocFromModifiedUtf8(descriptor.ToString().c_str());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700901 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
902 new_class->super_class_ = java_lang_Object;
903 new_class->vtable_ = java_lang_Object->vtable_;
904 new_class->primitive_type_ = Class::kPrimNot;
905 new_class->component_type_ = component_type_;
906 new_class->class_loader_ = component_type_->class_loader_;
907 new_class->array_rank_ = array_rank;
908 new_class->status_ = Class::kStatusInitialized;
909 // don't need to set new_class->object_size_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700910 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700911
912
913 // All arrays have java/lang/Cloneable and java/io/Serializable as
914 // interfaces. We need to set that up here, so that stuff like
915 // "instanceof" works right.
916 //
917 // Note: The GC could run during the call to FindSystemClass,
918 // so we need to make sure the class object is GC-valid while we're in
919 // there. Do this by clearing the interface list so the GC will just
920 // think that the entries are null.
921
922
923 // Use the single, global copies of "interfaces" and "iftable"
924 // (remember not to free them for arrays).
925 DCHECK(array_interfaces_ != NULL);
926 new_class->interfaces_ = array_interfaces_;
927 new_class->iftable_count_ = 2;
928 DCHECK(array_iftable_ != NULL);
929 new_class->iftable_ = array_iftable_;
930
931 // Inherit access flags from the component type. Arrays can't be
932 // used as a superclass or interface, so we want to add "final"
933 // and remove "interface".
934 //
935 // Don't inherit any non-standard flags (e.g., kAccFinal)
936 // from component_type_. We assume that the array class does not
937 // override finalize().
938 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
939 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
940
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700941 if (InsertClass(descriptor, new_class)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700942 return new_class;
943 }
944 // Another thread must have loaded the class after we
945 // started but before we finished. Abandon what we've
946 // done.
947 //
948 // (Yes, this happens.)
949
950 // Grab the winning class.
951 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
952 DCHECK(other_class != NULL);
953 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700954}
955
956Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700957 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700958 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700959 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700960 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700961 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700962 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700963 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700964 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700965 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700966 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700967 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700968 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700969 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700970 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700971 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700972 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700973 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700974 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700975 return GetClassRoot(kPrimitiveVoid);
Carl Shapiro744ad052011-08-06 15:53:36 -0700976 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700977 std::string printable_type(PrintableChar(type));
978 Thread::Current()->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
979 "Not a primitive type: %s", printable_type.c_str());
980 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700981}
982
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700983bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) {
984 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700985 MutexLock mu(classes_lock_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700986 Table::iterator it = classes_.insert(std::make_pair(hash, klass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700987 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700988}
989
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700990Class* ClassLinker::LookupClass(const StringPiece& descriptor, ClassLoader* class_loader) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700991 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700992 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700993 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700994 for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700995 Class* klass = it->second;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700996 if (klass->descriptor_->Equals(descriptor) && klass->class_loader_ == class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700997 return klass;
998 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700999 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001000 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001001}
1002
1003bool ClassLinker::InitializeClass(Class* klass) {
1004 CHECK(klass->GetStatus() == Class::kStatusResolved ||
1005 klass->GetStatus() == Class::kStatusError);
1006
Carl Shapirob5573532011-07-12 18:22:59 -07001007 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001008
1009 {
1010 ObjectLock lock(klass);
1011
1012 if (klass->GetStatus() < Class::kStatusVerified) {
1013 if (klass->IsErroneous()) {
1014 LG << "re-initializing failed class"; // TODO: throw
1015 return false;
1016 }
1017
1018 CHECK(klass->GetStatus() == Class::kStatusResolved);
1019
1020 klass->status_ = Class::kStatusVerifying;
1021 if (!DexVerify::VerifyClass(klass)) {
1022 LG << "Verification failed"; // TODO: ThrowVerifyError
1023 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001024 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001025 klass->SetStatus(Class::kStatusError);
1026 return false;
1027 }
1028
1029 klass->SetStatus(Class::kStatusVerified);
1030 }
1031
1032 if (klass->status_ == Class::kStatusInitialized) {
1033 return true;
1034 }
1035
1036 while (klass->status_ == Class::kStatusInitializing) {
1037 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -07001038 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001039 LG << "recursive <clinit>";
1040 return true;
1041 }
1042
1043 CHECK(!self->IsExceptionPending());
1044
1045 lock.Wait(); // TODO: check for interruption
1046
1047 // When we wake up, repeat the test for init-in-progress. If
1048 // there's an exception pending (only possible if
1049 // "interruptShouldThrow" was set), bail out.
1050 if (self->IsExceptionPending()) {
1051 CHECK(false);
1052 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
1053 klass->SetStatus(Class::kStatusError);
1054 return false;
1055 }
1056 if (klass->GetStatus() == Class::kStatusInitializing) {
1057 continue;
1058 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001059 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001060 klass->GetStatus() == Class::kStatusError);
1061 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001062 // The caller wants an exception, but it was thrown in a
1063 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001064 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
1065 return false;
1066 }
1067 return true; // otherwise, initialized
1068 }
1069
1070 // see if we failed previously
1071 if (klass->IsErroneous()) {
1072 // might be wise to unlock before throwing; depends on which class
1073 // it is that we have locked
1074
1075 // TODO: throwEarlierClassFailure(klass);
1076 return false;
1077 }
1078
1079 if (!ValidateSuperClassDescriptors(klass)) {
1080 klass->SetStatus(Class::kStatusError);
1081 return false;
1082 }
1083
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001084 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001085
Carl Shapirob5573532011-07-12 18:22:59 -07001086 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087 klass->status_ = Class::kStatusInitializing;
1088 }
1089
1090 if (!InitializeSuperClass(klass)) {
1091 return false;
1092 }
1093
1094 InitializeStaticFields(klass);
1095
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001096 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001097 if (clinit != NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098 // JValue unused;
1099 // TODO: dvmCallMethod(self, method, NULL, &unused);
Elliott Hughes53b61312011-08-12 18:28:20 -07001100 // UNIMPLEMENTED(FATAL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001101 }
1102
1103 {
1104 ObjectLock lock(klass);
1105
1106 if (self->IsExceptionPending()) {
1107 klass->SetStatus(Class::kStatusError);
1108 } else {
1109 klass->SetStatus(Class::kStatusInitialized);
1110 }
1111 lock.NotifyAll();
1112 }
1113
1114 return true;
1115}
1116
1117bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
1118 if (klass->IsInterface()) {
1119 return true;
1120 }
1121 // begin with the methods local to the superclass
1122 if (klass->HasSuperClass() &&
1123 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
1124 const Class* super = klass->GetSuperClass();
1125 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001126 const Method* method = super->GetVirtualMethod(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001127 if (method != super->GetVirtualMethod(i) &&
1128 !HasSameMethodDescriptorClasses(method, super, klass)) {
1129 LG << "Classes resolve differently in superclass";
1130 return false;
1131 }
1132 }
1133 }
1134 for (size_t i = 0; i < klass->iftable_count_; ++i) {
1135 const InterfaceEntry* iftable = &klass->iftable_[i];
1136 Class* interface = iftable->GetClass();
1137 if (klass->GetClassLoader() != interface->GetClassLoader()) {
1138 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1139 uint32_t vtable_index = iftable->method_index_array_[j];
1140 const Method* method = klass->GetVirtualMethod(vtable_index);
1141 if (!HasSameMethodDescriptorClasses(method, interface,
1142 method->GetClass())) {
1143 LG << "Classes resolve differently in interface"; // TODO: LinkageError
1144 return false;
1145 }
1146 }
1147 }
1148 }
1149 return true;
1150}
1151
1152bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001153 const Class* klass1,
1154 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001155 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
1156 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001157 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001158 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001159 const char* descriptor = it->GetDescriptor();
1160 if (descriptor == NULL) {
1161 break;
1162 }
1163 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1164 // Found a non-primitive type.
1165 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1166 return false;
1167 }
1168 }
1169 }
1170 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001171 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001172 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1173 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1174 return false;
1175 }
1176 }
1177 return true;
1178}
1179
1180// Returns true if classes referenced by the descriptor are the
1181// same classes in klass1 as they are in klass2.
1182bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001183 const Class* klass1,
1184 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001185 CHECK(descriptor != NULL);
1186 CHECK(klass1 != NULL);
1187 CHECK(klass2 != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001188 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001189 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001190 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001191 // TODO: found2 == NULL
1192 // TODO: lookup found1 in initiating loader list
1193 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -07001194 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001195 if (found1 == found2) {
1196 return true;
1197 } else {
1198 return false;
1199 }
1200 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201 return true;
1202}
1203
1204bool ClassLinker::InitializeSuperClass(Class* klass) {
1205 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001206 if (!klass->IsInterface() && klass->HasSuperClass()) {
1207 Class* super_class = klass->GetSuperClass();
1208 if (super_class->GetStatus() != Class::kStatusInitialized) {
1209 CHECK(!super_class->IsInterface());
1210 klass->MonitorExit();
1211 bool super_initialized = InitializeClass(super_class);
1212 klass->MonitorEnter();
1213 // TODO: check for a pending exception
1214 if (!super_initialized) {
1215 klass->SetStatus(Class::kStatusError);
1216 klass->NotifyAll();
1217 return false;
1218 }
1219 }
1220 }
1221 return true;
1222}
1223
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001224bool ClassLinker::EnsureInitialized(Class* c) {
1225 CHECK(c != NULL);
1226 if (c->IsInitialized()) {
1227 return true;
1228 }
1229
1230 c->MonitorExit();
1231 InitializeClass(c);
1232 c->MonitorEnter();
1233 return !Thread::Current()->IsExceptionPending();
1234}
1235
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001236void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001237 size_t num_static_fields = klass->NumStaticFields();
1238 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001239 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001240 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001241 DexCache* dex_cache = klass->GetDexCache();
Brian Carlstrom4873d462011-08-21 15:23:39 -07001242 // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray?
Brian Carlstromf615a612011-07-23 12:50:34 -07001243 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001244 return;
1245 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001246 const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001247 const DexFile& dex_file = FindDexFile(dex_cache);
1248 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001249 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001250 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001251 if (addr == NULL) {
1252 // All this class' static fields have default values.
1253 return;
1254 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001255 size_t array_size = DecodeUnsignedLeb128(&addr);
1256 for (size_t i = 0; i < array_size; ++i) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001257 Field* field = klass->GetStaticField(i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001258 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001259 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001260 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001261 case DexFile::kByte:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001262 field->SetByte(NULL, value.b);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001263 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001264 case DexFile::kShort:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001265 field->SetShort(NULL, value.s);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001266 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001267 case DexFile::kChar:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001268 field->SetChar(NULL, value.c);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001269 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001270 case DexFile::kInt:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001271 field->SetInt(NULL, value.i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001272 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001273 case DexFile::kLong:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001274 field->SetLong(NULL, value.j);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001275 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001276 case DexFile::kFloat:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001277 field->SetFloat(NULL, value.f);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001278 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001279 case DexFile::kDouble:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001280 field->SetDouble(NULL, value.d);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001281 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001282 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001283 uint32_t string_idx = value.i;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001284 String* resolved = ResolveString(klass, string_idx, dex_file);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001285 field->SetObject(NULL, resolved);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001286 break;
1287 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001288 case DexFile::kBoolean:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001289 field->SetBoolean(NULL, value.z);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001290 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001291 case DexFile::kNull:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001292 field->SetObject(NULL, value.l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001293 break;
1294 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001295 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001296 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001297 }
1298}
1299
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001300bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1301 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001302 if (!LinkSuperClass(klass)) {
1303 return false;
1304 }
1305 if (!LinkMethods(klass)) {
1306 return false;
1307 }
1308 if (!LinkInstanceFields(klass)) {
1309 return false;
1310 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001311 if (!LinkStaticFields(klass)) {
1312 return false;
1313 }
1314 CreateReferenceInstanceOffsets(klass);
1315 CreateReferenceStaticOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001316 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001317 klass->status_ = Class::kStatusResolved;
1318 return true;
1319}
1320
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001321bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1322 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001323 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1324 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001325 if (super_class == NULL) {
1326 LG << "Failed to resolve superclass";
1327 return false;
1328 }
1329 klass->super_class_ = super_class; // TODO: write barrier
1330 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001331 if (klass->NumInterfaces() > 0) {
1332 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1333 uint32_t idx = klass->interfaces_idx_[i];
1334 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1335 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001336 LG << "Failed to resolve interface";
1337 return false;
1338 }
1339 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001340 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001341 LG << "Inaccessible interface";
1342 return false;
1343 }
1344 }
1345 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001346 // Mark the class as loaded.
1347 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001348 return true;
1349}
1350
1351bool ClassLinker::LinkSuperClass(Class* klass) {
1352 CHECK(!klass->IsPrimitive());
1353 const Class* super = klass->GetSuperClass();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001354 if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001355 if (super != NULL) {
1356 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1357 return false;
1358 }
1359 // TODO: clear finalize attribute
1360 return true;
1361 }
1362 if (super == NULL) {
1363 LG << "No superclass defined"; // TODO: LinkageError
1364 return false;
1365 }
1366 // Verify
1367 if (super->IsFinal()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001368 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001369 return false;
1370 }
1371 if (super->IsInterface()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001372 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001373 return false;
1374 }
1375 if (!klass->CanAccess(super)) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001376 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001377 return false;
1378 }
1379 return true;
1380}
1381
1382// Populate the class vtable and itable.
1383bool ClassLinker::LinkMethods(Class* klass) {
1384 if (klass->IsInterface()) {
1385 // No vtable.
1386 size_t count = klass->NumVirtualMethods();
1387 if (!IsUint(16, count)) {
1388 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1389 return false;
1390 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001391 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001392 klass->GetVirtualMethod(i)->method_index_ = i;
1393 }
1394 } else {
1395 // Link virtual method tables
1396 LinkVirtualMethods(klass);
1397
1398 // Link interface method tables
1399 LinkInterfaceMethods(klass);
1400
1401 // Insert stubs.
1402 LinkAbstractMethods(klass);
1403 }
1404 return true;
1405}
1406
1407bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001408 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001409 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001410 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1411 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001412 // TODO: do not assign to the vtable field until it is fully constructed.
1413 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001414 // See if any of our virtual methods override the superclass.
1415 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1416 Method* local_method = klass->GetVirtualMethod(i);
1417 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001418 for (; j < actual_count; ++j) {
1419 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001420 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001421 // Verify
1422 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001423 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001424 return false;
1425 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001426 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001427 local_method->method_index_ = j;
1428 break;
1429 }
1430 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001431 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001432 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001433 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001434 local_method->method_index_ = actual_count;
1435 actual_count += 1;
1436 }
1437 }
1438 if (!IsUint(16, actual_count)) {
1439 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1440 return false;
1441 }
1442 CHECK_LE(actual_count, max_count);
1443 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001444 // TODO: do not assign to the vtable field until it is fully constructed.
1445 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001446 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001447 } else {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001448 CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001449 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001450 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001451 LG << "Too many methods"; // TODO: VirtualMachineError
1452 return false;
1453 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001454 // TODO: do not assign to the vtable field until it is fully constructed.
1455 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1456 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001457 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001458 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1459 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001460 }
1461 return true;
1462}
1463
1464bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1465 int pool_offset = 0;
1466 int pool_size = 0;
1467 int miranda_count = 0;
1468 int miranda_alloc = 0;
1469 size_t super_ifcount;
1470 if (klass->HasSuperClass()) {
1471 super_ifcount = klass->GetSuperClass()->iftable_count_;
1472 } else {
1473 super_ifcount = 0;
1474 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001475 size_t ifcount = super_ifcount;
1476 ifcount += klass->NumInterfaces();
1477 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1478 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001479 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001480 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001481 DCHECK(klass->iftable_count_ == 0);
1482 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001483 return true;
1484 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001485 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1486 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001487 if (super_ifcount != 0) {
1488 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1489 sizeof(InterfaceEntry) * super_ifcount);
1490 }
1491 // Flatten the interface inheritance hierarchy.
1492 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001493 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1494 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001495 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001496 if (!interf->IsInterface()) {
1497 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1498 return false;
1499 }
1500 klass->iftable_[idx++].SetClass(interf);
1501 for (size_t j = 0; j < interf->iftable_count_; j++) {
1502 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1503 }
1504 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001505 CHECK_EQ(idx, ifcount);
1506 klass->iftable_count_ = ifcount;
1507 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001508 return true;
1509 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001510 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001511 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1512 }
1513 if (pool_size == 0) {
1514 return true;
1515 }
1516 klass->ifvi_pool_count_ = pool_size;
1517 klass->ifvi_pool_ = new uint32_t[pool_size];
1518 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001519 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001520 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1521 Class* interface = klass->iftable_[i].GetClass();
1522 pool_offset += interface->NumVirtualMethods(); // end here
1523 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1524 Method* interface_method = interface->GetVirtualMethod(j);
1525 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001526 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001527 Method* vtable_method = klass->vtable_->Get(k);
1528 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1529 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001530 LG << "Implementation not public";
1531 return false;
1532 }
1533 klass->iftable_[i].method_index_array_[j] = k;
1534 break;
1535 }
1536 }
1537 if (k < 0) {
1538 if (miranda_count == miranda_alloc) {
1539 miranda_alloc += 8;
1540 if (miranda_list.empty()) {
1541 miranda_list.resize(miranda_alloc);
1542 } else {
1543 miranda_list.resize(miranda_alloc);
1544 }
1545 }
1546 int mir;
1547 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001548 Method* miranda_method = miranda_list[mir];
1549 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001550 break;
1551 }
1552 }
1553 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001554 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001555 if (mir == miranda_count) {
1556 miranda_list[miranda_count++] = interface_method;
1557 }
1558 }
1559 }
1560 }
1561 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001562 int old_method_count = klass->NumVirtualMethods();
1563 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001564 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001565
1566 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001567 int old_vtable_count = klass->vtable_->GetLength();
1568 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001569 // TODO: do not assign to the vtable field until it is fully constructed.
1570 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001571
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001572 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001573 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001574 memcpy(meth, miranda_list[i], sizeof(Method));
1575 meth->klass_ = klass;
1576 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001577 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1578 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001579 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001580 }
1581 }
1582 return true;
1583}
1584
1585void ClassLinker::LinkAbstractMethods(Class* klass) {
1586 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1587 Method* method = klass->GetVirtualMethod(i);
1588 if (method->IsAbstract()) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001589 LG << "AbstractMethodError";
1590 method->code_off_ = 0xFFFFFFFF;
1591 // TODO: throw AbstractMethodError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001592 }
1593 }
1594}
1595
1596bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001597 CHECK(klass != NULL);
1598 size_t field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001599 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001600 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001601 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001602 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001603 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001604 return LinkFields(field_offset,
1605 klass->num_reference_instance_fields_,
1606 klass->NumInstanceFields(),
1607 klass->ifields_,
1608 klass->object_size_);
1609}
1610
1611bool ClassLinker::LinkStaticFields(Class* klass) {
1612 CHECK(klass != NULL);
1613 size_t allocated_class_size = klass->class_size_;
1614 size_t field_offset = OFFSETOF_MEMBER(Class, fields_);
1615 bool success = LinkFields(field_offset,
1616 klass->num_reference_static_fields_,
1617 klass->NumStaticFields(),
1618 klass->sfields_,
1619 klass->class_size_);
1620 CHECK_EQ(allocated_class_size, klass->class_size_);
1621 return success;
1622}
1623
1624bool ClassLinker::LinkFields(size_t field_offset,
1625 size_t& num_reference_fields,
1626 size_t num_fields,
1627 ObjectArray<Field>* fields,
1628 size_t& size) {
1629 CHECK((num_fields == 0) == (fields == NULL));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001630 // Move references to the front.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001631 num_reference_fields = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001632 size_t i = 0;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001633 for ( ; i < num_fields; i++) {
1634 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001635 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001636 if (c != '[' && c != 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001637 for (size_t j = num_fields - 1; j > i; j--) {
1638 Field* refField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001639 char rc = refField->GetType();
1640 if (rc == '[' || rc == 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001641 fields->Set(i, refField);
1642 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001643 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001644 c = rc;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001645 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001646 break;
1647 }
1648 }
1649 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001650 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001651 }
1652 if (c != '[' && c != 'L') {
1653 break;
1654 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001655 pField->SetOffset(field_offset);
1656 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001657 }
1658
1659 // Now we want to pack all of the double-wide fields together. If
1660 // we're not aligned, though, we want to shuffle one 32-bit field
1661 // into place. If we can't find one, we'll have to pad it.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001662 if (i != num_fields && (field_offset & 0x04) != 0) {
1663 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001664 char c = pField->GetType();
1665
1666 if (c != 'J' && c != 'D') {
1667 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001668 DCHECK(c != '[');
1669 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001670 pField->SetOffset(field_offset);
1671 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001672 i++;
1673 } else {
1674 // Next field is 64-bit, so search for a 32-bit field we can
1675 // swap into it.
1676 bool found = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001677 for (size_t j = num_fields - 1; j > i; j--) {
1678 Field* singleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001679 char rc = singleField->GetType();
1680 if (rc != 'J' && rc != 'D') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001681 fields->Set(i, singleField);
1682 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001683 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001684 pField->SetOffset(field_offset);
1685 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001686 found = true;
1687 i++;
1688 break;
1689 }
1690 }
1691 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001692 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001693 }
1694 }
1695 }
1696
1697 // Alignment is good, shuffle any double-wide fields forward, and
1698 // finish assigning field offsets to all fields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001699 DCHECK(i == num_fields || (field_offset & 0x04) == 0);
1700 for ( ; i < num_fields; i++) {
1701 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001702 char c = pField->GetType();
1703 if (c != 'D' && c != 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001704 for (size_t j = num_fields - 1; j > i; j--) {
1705 Field* doubleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001706 char rc = doubleField->GetType();
1707 if (rc == 'D' || rc == 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001708 fields->Set(i, doubleField);
1709 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001710 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001711 c = rc;
1712 break;
1713 }
1714 }
1715 } else {
1716 // This is a double-wide field, leave it be.
1717 }
1718
Brian Carlstroma0808032011-07-18 00:39:23 -07001719 pField->SetOffset(field_offset);
1720 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001721 if (c == 'J' || c == 'D') {
Brian Carlstroma0808032011-07-18 00:39:23 -07001722 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001723 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001724 }
1725
1726#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001727 // Make sure that all reference fields appear before
1728 // non-reference fields, and all double-wide fields are aligned.
1729 bool seen_non_ref = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001730 for (i = 0; i < num_fields; i++) {
1731 Field *pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001732 char c = pField->GetType();
1733
1734 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001735 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001736 }
1737
1738 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001739 if (!seen_non_ref) {
1740 seen_non_ref = true;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001741 DCHECK_EQ(num_reference_fields, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001742 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001743 } else {
1744 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001745 }
1746 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001747 if (!seen_non_ref) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001748 DCHECK_EQ(num_fields, num_reference_fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001749 }
1750#endif
Brian Carlstrom4873d462011-08-21 15:23:39 -07001751 size = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001752 return true;
1753}
1754
1755// Set the bitmap of reference offsets, refOffsets, from the ifields
1756// list.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001757void ClassLinker::CreateReferenceInstanceOffsets(Class* klass) {
1758 klass->reference_instance_offsets_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001759 if (klass->HasSuperClass()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001760 klass->reference_instance_offsets_ = klass->GetSuperClass()->GetReferenceInstanceOffsets();
1761 // If our superclass overflowed, we don't stand a chance.
1762 if (klass->reference_instance_offsets_ == CLASS_WALK_SUPER) {
1763 return;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001764 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001765 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001766 CreateReferenceOffsets(klass->reference_instance_offsets_,
1767 klass->NumReferenceInstanceFields(),
1768 klass->ifields_);
1769}
1770
1771void ClassLinker::CreateReferenceStaticOffsets(Class* klass) {
1772 klass->reference_static_offsets_ = 0;
1773 CreateReferenceOffsets(klass->reference_static_offsets_,
1774 klass->NumReferenceStaticFields(),
1775 klass->sfields_);
1776}
1777
1778void ClassLinker::CreateReferenceOffsets(uint32_t& reference_offsets,
1779 size_t num_reference_fields,
1780 const ObjectArray<Field>* fields) {
1781 // All of the fields that contain object references are guaranteed
1782 // to be at the beginning of the fields list.
1783 for (size_t i = 0; i < num_reference_fields; ++i) {
1784 // Note that byte_offset is the offset from the beginning of
1785 // object, not the offset into instance data
1786 const Field* field = fields->Get(i);
1787 size_t byte_offset = field->GetOffset();
1788 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
1789 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
1790 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1791 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
1792 CHECK_NE(new_bit, 0U);
1793 reference_offsets |= new_bit;
1794 } else {
1795 reference_offsets = CLASS_WALK_SUPER;
1796 break;
1797 }
1798 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001799}
1800
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001801Class* ClassLinker::ResolveClass(const Class* referrer,
1802 uint32_t class_idx,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001803 const DexFile& dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001804 DexCache* dex_cache = referrer->GetDexCache();
1805 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001806 if (resolved != NULL) {
1807 return resolved;
1808 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001809 const char* descriptor = dex_file.dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001810 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001811 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001812 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001813 resolved = FindClass(descriptor, referrer->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001814 }
1815 if (resolved != NULL) {
1816 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001817 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001818 if (check->GetClassLoader() != NULL) {
1819 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1820 return NULL;
1821 }
1822 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001823 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001824 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001825 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001826 }
1827 return resolved;
1828}
1829
1830Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1831 /*MethodType*/ int method_type) {
1832 CHECK(false);
1833 return NULL;
1834}
1835
Carl Shapiro69759ea2011-07-21 18:13:35 -07001836String* ClassLinker::ResolveString(const Class* referring,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001837 uint32_t string_idx,
1838 const DexFile& dex_file) {
1839 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1840 int32_t utf16_length = dex_file.GetStringLength(string_id);
1841 const char* utf8_data = dex_file.GetStringData(string_id);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001842 String* string = intern_table_.Intern(utf16_length, utf8_data);
1843 referring->GetDexCache()->SetResolvedString(string_idx, string);
1844 return string;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001845}
1846
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001847} // namespace art