blob: a205a067b087978333fcadd510a0c4b9557951af [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: cshapiro@google.com (Carl Shapiro)
3
4#include "src/class_linker.h"
5
6#include <vector>
7#include <utility>
8
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07009#include "src/casts.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070010#include "src/dex_verifier.h"
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070011#include "src/heap.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070012#include "src/logging.h"
13#include "src/monitor.h"
14#include "src/object.h"
15#include "src/raw_dex_file.h"
16#include "src/scoped_ptr.h"
17#include "src/thread.h"
18#include "src/utils.h"
19
20namespace art {
21
Carl Shapiro61e019d2011-07-14 16:53:09 -070022ClassLinker* ClassLinker::Create() {
23 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
24 class_linker->Init();
25 // TODO: check for failure during initialization
26 return class_linker.release();
27}
28
Carl Shapiro565f5072011-07-10 13:39:43 -070029void ClassLinker::Init() {
Brian Carlstrom934486c2011-07-12 23:42:50 -070030 // Allocate and partially initialize the Object and Class classes.
31 // Initialization will be completed when the definitions are loaded.
32 java_lang_Object_ = Heap::AllocClass(NULL);
33 java_lang_Object_->descriptor_ = "Ljava/lang/Object;";
34 java_lang_Class_ = Heap::AllocClass(NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070035 java_lang_Class_->descriptor_ = "Ljava/lang/Class;";
36
37 // Allocate and initialize the primitive type classes.
38 primitive_byte_ = CreatePrimitiveClass(kTypeByte, "B");
39 primitive_char_ = CreatePrimitiveClass(kTypeChar, "C");
40 primitive_double_ = CreatePrimitiveClass(kTypeDouble, "D");
41 primitive_float_ = CreatePrimitiveClass(kTypeFloat, "F");
42 primitive_int_ = CreatePrimitiveClass(kTypeInt, "I");
43 primitive_long_ = CreatePrimitiveClass(kTypeLong, "J");
44 primitive_short_ = CreatePrimitiveClass(kTypeShort, "S");
45 primitive_boolean_ = CreatePrimitiveClass(kTypeBoolean, "Z");
46 primitive_void_ = CreatePrimitiveClass(kTypeVoid, "V");
47}
48
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049Class* ClassLinker::FindClass(const char* descriptor,
50 Object* class_loader,
51 DexFile* dex_file) {
Carl Shapirob5573532011-07-12 18:22:59 -070052 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053 CHECK(!self->IsExceptionPending());
54 // Find the class in the loaded classes table.
55 Class* klass = LookupClass(descriptor, class_loader);
56 if (klass == NULL) {
57 // Class is not yet loaded.
58 if (dex_file == NULL) {
59 // No .dex file specified, search the class path.
60 dex_file = FindInClassPath(descriptor);
61 if (dex_file == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070062 LG << "Class " << descriptor << " really not found";
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070063 return NULL;
64 }
65 }
66 // Load the class from the dex file.
Brian Carlstrom934486c2011-07-12 23:42:50 -070067 if (!strcmp(descriptor, "Ljava/lang/Object;")) {
68 klass = java_lang_Object_;
69 klass->dex_file_ = dex_file;
70 } else if (!strcmp(descriptor, "Ljava/lang/Class;")) {
Carl Shapiro565f5072011-07-10 13:39:43 -070071 klass = java_lang_Class_;
Brian Carlstrom934486c2011-07-12 23:42:50 -070072 klass->dex_file_ = dex_file;
Carl Shapiro565f5072011-07-10 13:39:43 -070073 } else {
Brian Carlstrom934486c2011-07-12 23:42:50 -070074 klass = Heap::AllocClass(dex_file);
Carl Shapiro565f5072011-07-10 13:39:43 -070075 }
Brian Carlstrom934486c2011-07-12 23:42:50 -070076 bool is_loaded = LoadClass(descriptor, klass);
Carl Shapiro565f5072011-07-10 13:39:43 -070077 if (!is_loaded) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070078 // TODO: this occurs only when a dex file is provided.
79 LG << "Class not found"; // TODO: NoClassDefFoundError
80 return NULL;
81 }
82 // Check for a pending exception during load
83 if (self->IsExceptionPending()) {
84 // TODO: free native allocations in klass
85 return NULL;
86 }
87 {
88 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -070089 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070090 // Add the newly loaded class to the loaded classes table.
91 bool success = InsertClass(klass);
92 if (!success) {
93 // We may fail to insert if we raced with another thread.
94 klass->clinit_thread_id_ = 0;
95 // TODO: free native allocations in klass
96 klass = LookupClass(descriptor, class_loader);
97 CHECK(klass != NULL);
98 } else {
99 // Link the class.
100 if (!LinkClass(klass)) {
101 // Linking failed.
102 // TODO: CHECK(self->IsExceptionPending());
103 lock.NotifyAll();
104 return NULL;
105 }
106 }
107 }
108 }
109 // Link the class if it has not already been linked.
110 if (!klass->IsLinked() && !klass->IsErroneous()) {
111 ObjectLock lock(klass);
112 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700113 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700114 LG << "Recursive link"; // TODO: ClassCircularityError
115 return NULL;
116 }
117 // Wait for the pending initialization to complete.
118 while (!klass->IsLinked() && !klass->IsErroneous()) {
119 lock.Wait();
120 }
121 }
122 if (klass->IsErroneous()) {
123 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
124 return NULL;
125 }
126 // Return the loaded class. No exceptions should be pending.
127 CHECK(!self->IsExceptionPending());
128 return klass;
129}
130
Brian Carlstrom934486c2011-07-12 23:42:50 -0700131bool ClassLinker::LoadClass(const char* descriptor, Class* klass) {
132 const RawDexFile* raw = klass->GetDexFile()->GetRaw();
133 const RawDexFile::ClassDef* class_def = raw->FindClassDef(descriptor);
134 if (class_def == NULL) {
135 return false;
136 } else {
137 return LoadClass(*class_def, klass);
138 }
139}
140
141bool ClassLinker::LoadClass(const RawDexFile::ClassDef& class_def, Class* klass) {
142 CHECK(klass != NULL);
143 CHECK(klass->dex_file_ != NULL);
144 const RawDexFile* raw = klass->GetDexFile()->GetRaw();
145 const byte* class_data = raw->GetClassData(class_def);
146 RawDexFile::ClassDataHeader header = raw->ReadClassDataHeader(&class_data);
147
148 const char* descriptor = raw->GetClassDescriptor(class_def);
149 CHECK(descriptor != NULL);
150
151 klass->klass_ = java_lang_Class_;
152 klass->descriptor_.set(descriptor);
153 klass->descriptor_alloc_ = NULL;
154 klass->access_flags_ = class_def.access_flags_;
155 klass->class_loader_ = NULL; // TODO
156 klass->primitive_type_ = Class::kPrimNot;
157 klass->status_ = Class::kStatusIdx;
158
159 klass->super_class_ = NULL;
160 klass->super_class_idx_ = class_def.superclass_idx_;
161
162 klass->num_sfields_ = header.static_fields_size_;
163 klass->num_ifields_ = header.instance_fields_size_;
164 klass->num_direct_methods_ = header.direct_methods_size_;
165 klass->num_virtual_methods_ = header.virtual_methods_size_;
166
167 klass->source_file_ = raw->dexGetSourceFile(class_def);
168
169 // Load class interfaces.
170 LoadInterfaces(class_def, klass);
171
172 // Load static fields.
173 if (klass->num_sfields_ != 0) {
174 // TODO: allocate on the object heap.
175 klass->sfields_ = new StaticField[klass->NumStaticFields()]();
176 uint32_t last_idx = 0;
177 for (size_t i = 0; i < klass->num_sfields_; ++i) {
178 RawDexFile::Field raw_field;
179 raw->dexReadClassDataField(&class_data, &raw_field, &last_idx);
180 LoadField(klass, raw_field, &klass->sfields_[i]);
181 }
182 }
183
184 // Load instance fields.
185 if (klass->NumInstanceFields() != 0) {
186 // TODO: allocate on the object heap.
187 klass->ifields_ = new InstanceField[klass->NumInstanceFields()]();
188 uint32_t last_idx = 0;
189 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
190 RawDexFile::Field raw_field;
191 raw->dexReadClassDataField(&class_data, &raw_field, &last_idx);
192 LoadField(klass, raw_field, klass->GetInstanceField(i));
193 }
194 }
195
196 // Load direct methods.
197 if (klass->NumDirectMethods() != 0) {
198 // TODO: append direct methods to class object
199 klass->direct_methods_ = new Method[klass->NumDirectMethods()]();
200 uint32_t last_idx = 0;
201 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
202 RawDexFile::Method raw_method;
203 raw->dexReadClassDataMethod(&class_data, &raw_method, &last_idx);
204 LoadMethod(klass, raw_method, klass->GetDirectMethod(i));
205 // TODO: register maps
206 }
207 }
208
209 // Load virtual methods.
210 if (klass->NumVirtualMethods() != 0) {
211 // TODO: append virtual methods to class object
212 klass->virtual_methods_ = new Method[klass->NumVirtualMethods()]();
213 uint32_t last_idx = 0;
214 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
215 RawDexFile::Method raw_method;
216 raw->dexReadClassDataMethod(&class_data, &raw_method, &last_idx);
217 LoadMethod(klass, raw_method, klass->GetVirtualMethod(i));
218 // TODO: register maps
219 }
220 }
221
222 return klass;
223}
224
225void ClassLinker::LoadInterfaces(const RawDexFile::ClassDef& class_def,
226 Class* klass) {
227 const RawDexFile* raw = klass->GetDexFile()->GetRaw();
228 const RawDexFile::TypeList* list = raw->GetInterfacesList(class_def);
229 if (list != NULL) {
230 klass->interface_count_ = list->Size();
231 // TODO: allocate the interfaces array on the object heap.
232 klass->interfaces_ = new Class*[list->Size()]();
233 for (size_t i = 0; i < list->Size(); ++i) {
234 const RawDexFile::TypeItem& type_item = list->GetTypeItem(i);
235 klass->interfaces_[i] = reinterpret_cast<Class*>(type_item.type_idx_);
236 }
237 }
238}
239
240void ClassLinker::LoadField(Class* klass, const RawDexFile::Field& src,
241 Field* dst) {
242 const RawDexFile* raw = klass->GetDexFile()->GetRaw();
243 const RawDexFile::FieldId& field_id = raw->GetFieldId(src.field_idx_);
244 dst->klass_ = klass;
245 dst->name_ = raw->dexStringById(field_id.name_idx_);
246 dst->signature_ = raw->dexStringByTypeIdx(field_id.type_idx_);
247 dst->access_flags_ = src.access_flags_;
248}
249
250void ClassLinker::LoadMethod(Class* klass, const RawDexFile::Method& src,
251 Method* dst) {
252 const RawDexFile* raw = klass->GetDexFile()->GetRaw();
253 const RawDexFile::MethodId& method_id = raw->GetMethodId(src.method_idx_);
254 dst->klass_ = klass;
255 dst->name_.set(raw->dexStringById(method_id.name_idx_));
256 dst->proto_idx_ = method_id.proto_idx_;
257 dst->shorty_.set(raw->GetShorty(method_id.proto_idx_));
258 dst->access_flags_ = src.access_flags_;
259
260 // TODO: check for finalize method
261
262 const RawDexFile::CodeItem* code_item = raw->GetCodeItem(src);
263 if (code_item != NULL) {
264 dst->num_registers_ = code_item->registers_size_;
265 dst->num_ins_ = code_item->ins_size_;
266 dst->num_outs_ = code_item->outs_size_;
267 dst->insns_ = code_item->insns_;
268 } else {
269 uint16_t num_args = dst->NumArgRegisters();
270 if (!dst->IsStatic()) {
271 ++num_args;
272 }
273 dst->num_registers_ = dst->num_ins_ + num_args;
274 // TODO: native methods
275 }
276}
277
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700278DexFile* ClassLinker::FindInClassPath(const char* descriptor) {
279 for (size_t i = 0; i != class_path_.size(); ++i) {
280 DexFile* dex_file = class_path_[i];
281 if (dex_file->HasClass(descriptor)) {
282 return dex_file;
283 }
284 }
285 return NULL;
286}
287
288void ClassLinker::AppendToClassPath(DexFile* dex_file) {
289 class_path_.push_back(dex_file);
290}
291
Carl Shapiro565f5072011-07-10 13:39:43 -0700292Class* ClassLinker::CreatePrimitiveClass(JType type, const char* descriptor) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700293 Class* klass = Heap::AllocClass(NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -0700294 CHECK(klass != NULL);
295 klass->super_class_ = java_lang_Class_;
296 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
297 klass->descriptor_ = descriptor;
298 klass->status_ = Class::kStatusInitialized;
299 return klass;
300}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700301
Carl Shapiro565f5072011-07-10 13:39:43 -0700302Class* ClassLinker::FindPrimitiveClass(JType type) {
303 switch (type) {
304 case kTypeByte:
305 CHECK(primitive_byte_ != NULL);
306 return primitive_byte_;
307 case kTypeChar:
308 CHECK(primitive_char_ != NULL);
309 return primitive_char_;
310 case kTypeDouble:
311 CHECK(primitive_double_ != NULL);
312 return primitive_double_;
313 case kTypeFloat:
314 CHECK(primitive_float_ != NULL);
315 return primitive_float_;
316 case kTypeInt:
317 CHECK(primitive_int_ != NULL);
318 return primitive_int_;
319 case kTypeLong:
320 CHECK(primitive_long_ != NULL);
321 return primitive_long_;
322 case kTypeShort:
323 CHECK(primitive_short_ != NULL);
324 return primitive_short_;
325 case kTypeBoolean:
326 CHECK(primitive_boolean_ != NULL);
327 return primitive_boolean_;
328 case kTypeVoid:
329 CHECK(primitive_void_ != NULL);
330 return primitive_void_;
331 default:
332 LOG(FATAL) << "Unknown primitive type " << static_cast<int>(type);
333 };
334 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700335}
336
337bool ClassLinker::InsertClass(Class* klass) {
338 // TODO: acquire classes_lock_
339 const char* key = klass->GetDescriptor().data();
340 bool success = classes_.insert(std::make_pair(key, klass)).second;
341 // TODO: release classes_lock_
342 return success;
343}
344
345Class* ClassLinker::LookupClass(const char* descriptor, Object* class_loader) {
346 // TODO: acquire classes_lock_
347 Table::iterator it = classes_.find(descriptor);
348 // TODO: release classes_lock_
349 if (it == classes_.end()) {
350 return NULL;
351 } else {
352 return (*it).second;
353 }
354}
355
356bool ClassLinker::InitializeClass(Class* klass) {
357 CHECK(klass->GetStatus() == Class::kStatusResolved ||
358 klass->GetStatus() == Class::kStatusError);
359
Carl Shapirob5573532011-07-12 18:22:59 -0700360 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700361
362 {
363 ObjectLock lock(klass);
364
365 if (klass->GetStatus() < Class::kStatusVerified) {
366 if (klass->IsErroneous()) {
367 LG << "re-initializing failed class"; // TODO: throw
368 return false;
369 }
370
371 CHECK(klass->GetStatus() == Class::kStatusResolved);
372
373 klass->status_ = Class::kStatusVerifying;
374 if (!DexVerify::VerifyClass(klass)) {
375 LG << "Verification failed"; // TODO: ThrowVerifyError
376 Object* exception = self->GetException();
377 klass->SetObjectAt(OFFSETOF_MEMBER(Class, verify_error_class_),
378 exception->GetClass());
379 klass->SetStatus(Class::kStatusError);
380 return false;
381 }
382
383 klass->SetStatus(Class::kStatusVerified);
384 }
385
386 if (klass->status_ == Class::kStatusInitialized) {
387 return true;
388 }
389
390 while (klass->status_ == Class::kStatusInitializing) {
391 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700392 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700393 LG << "recursive <clinit>";
394 return true;
395 }
396
397 CHECK(!self->IsExceptionPending());
398
399 lock.Wait(); // TODO: check for interruption
400
401 // When we wake up, repeat the test for init-in-progress. If
402 // there's an exception pending (only possible if
403 // "interruptShouldThrow" was set), bail out.
404 if (self->IsExceptionPending()) {
405 CHECK(false);
406 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
407 klass->SetStatus(Class::kStatusError);
408 return false;
409 }
410 if (klass->GetStatus() == Class::kStatusInitializing) {
411 continue;
412 }
413 assert(klass->GetStatus() == Class::kStatusInitialized ||
414 klass->GetStatus() == Class::kStatusError);
415 if (klass->IsErroneous()) {
416 /*
417 * The caller wants an exception, but it was thrown in a
418 * different thread. Synthesize one here.
419 */
420 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
421 return false;
422 }
423 return true; // otherwise, initialized
424 }
425
426 // see if we failed previously
427 if (klass->IsErroneous()) {
428 // might be wise to unlock before throwing; depends on which class
429 // it is that we have locked
430
431 // TODO: throwEarlierClassFailure(klass);
432 return false;
433 }
434
435 if (!ValidateSuperClassDescriptors(klass)) {
436 klass->SetStatus(Class::kStatusError);
437 return false;
438 }
439
440 assert(klass->status < CLASS_INITIALIZING);
441
Carl Shapirob5573532011-07-12 18:22:59 -0700442 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700443 klass->status_ = Class::kStatusInitializing;
444 }
445
446 if (!InitializeSuperClass(klass)) {
447 return false;
448 }
449
450 InitializeStaticFields(klass);
451
452 Method* clinit = klass->FindDirectMethodLocally("<clinit>", "()V");
453 if (clinit != NULL) {
454 } else {
455 // JValue unused;
456 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700457 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700458 }
459
460 {
461 ObjectLock lock(klass);
462
463 if (self->IsExceptionPending()) {
464 klass->SetStatus(Class::kStatusError);
465 } else {
466 klass->SetStatus(Class::kStatusInitialized);
467 }
468 lock.NotifyAll();
469 }
470
471 return true;
472}
473
474bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
475 if (klass->IsInterface()) {
476 return true;
477 }
478 // begin with the methods local to the superclass
479 if (klass->HasSuperClass() &&
480 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
481 const Class* super = klass->GetSuperClass();
482 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
483 const Method* method = klass->GetVirtualMethod(i);
484 if (method != super->GetVirtualMethod(i) &&
485 !HasSameMethodDescriptorClasses(method, super, klass)) {
486 LG << "Classes resolve differently in superclass";
487 return false;
488 }
489 }
490 }
491 for (size_t i = 0; i < klass->iftable_count_; ++i) {
492 const InterfaceEntry* iftable = &klass->iftable_[i];
493 Class* interface = iftable->GetClass();
494 if (klass->GetClassLoader() != interface->GetClassLoader()) {
495 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
496 uint32_t vtable_index = iftable->method_index_array_[j];
497 const Method* method = klass->GetVirtualMethod(vtable_index);
498 if (!HasSameMethodDescriptorClasses(method, interface,
499 method->GetClass())) {
500 LG << "Classes resolve differently in interface"; // TODO: LinkageError
501 return false;
502 }
503 }
504 }
505 }
506 return true;
507}
508
509bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700510 const Class* klass1,
511 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700512 const RawDexFile* raw = method->GetClass()->GetDexFile()->GetRaw();
513 const RawDexFile::ProtoId& proto_id = raw->GetProtoId(method->proto_idx_);
514 RawDexFile::ParameterIterator *it;
515 for (it = raw->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
516 const char* descriptor = it->GetDescriptor();
517 if (descriptor == NULL) {
518 break;
519 }
520 if (descriptor[0] == 'L' || descriptor[0] == '[') {
521 // Found a non-primitive type.
522 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
523 return false;
524 }
525 }
526 }
527 // Check the return type
528 const char* descriptor = raw->GetReturnTypeDescriptor(proto_id);
529 if (descriptor[0] == 'L' || descriptor[0] == '[') {
530 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
531 return false;
532 }
533 }
534 return true;
535}
536
537// Returns true if classes referenced by the descriptor are the
538// same classes in klass1 as they are in klass2.
539bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700540 const Class* klass1,
541 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700542 CHECK(descriptor != NULL);
543 CHECK(klass1 != NULL);
544 CHECK(klass2 != NULL);
545#if 0
546 Class* found1 = FindClassNoInit(descriptor, klass1->GetClassLoader());
547 // TODO: found1 == NULL
548 Class* found2 = FindClassNoInit(descriptor, klass2->GetClassLoader());
549 // TODO: found2 == NULL
550 // TODO: lookup found1 in initiating loader list
551 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700552 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700553 if (found1 == found2) {
554 return true;
555 } else {
556 return false;
557 }
558 }
559#endif
560 return true;
561}
562
563bool ClassLinker::InitializeSuperClass(Class* klass) {
564 CHECK(klass != NULL);
565 // TODO: assert klass lock is acquired
566 if (!klass->IsInterface() && klass->HasSuperClass()) {
567 Class* super_class = klass->GetSuperClass();
568 if (super_class->GetStatus() != Class::kStatusInitialized) {
569 CHECK(!super_class->IsInterface());
570 klass->MonitorExit();
571 bool super_initialized = InitializeClass(super_class);
572 klass->MonitorEnter();
573 // TODO: check for a pending exception
574 if (!super_initialized) {
575 klass->SetStatus(Class::kStatusError);
576 klass->NotifyAll();
577 return false;
578 }
579 }
580 }
581 return true;
582}
583
584void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700585 size_t num_static_fields = klass->NumStaticFields();
586 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700587 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700588 }
589 DexFile* dex_file = klass->GetDexFile();
590 if (dex_file == NULL) {
591 return;
592 }
593 const char* descriptor = klass->GetDescriptor().data();
Brian Carlstrom934486c2011-07-12 23:42:50 -0700594 const RawDexFile* raw = dex_file->GetRaw();
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700595 const RawDexFile::ClassDef* class_def = raw->FindClassDef(descriptor);
596 CHECK(class_def != NULL);
597 const byte* addr = raw->GetEncodedArray(*class_def);
598 size_t array_size = DecodeUnsignedLeb128(&addr);
599 for (size_t i = 0; i < array_size; ++i) {
600 StaticField* field = klass->GetStaticField(i);
601 JValue value;
602 RawDexFile::ValueType type = raw->ReadEncodedValue(&addr, &value);
603 switch (type) {
604 case RawDexFile::kByte:
605 field->SetByte(value.b);
606 break;
607 case RawDexFile::kShort:
608 field->SetShort(value.s);
609 break;
610 case RawDexFile::kChar:
611 field->SetChar(value.c);
612 break;
613 case RawDexFile::kInt:
614 field->SetInt(value.i);
615 break;
616 case RawDexFile::kLong:
617 field->SetLong(value.j);
618 break;
619 case RawDexFile::kFloat:
620 field->SetFloat(value.f);
621 break;
622 case RawDexFile::kDouble:
623 field->SetDouble(value.d);
624 break;
625 case RawDexFile::kString: {
626 uint32_t string_idx = value.i;
627 String* resolved = ResolveString(klass, string_idx);
628 field->SetObject(resolved);
629 break;
630 }
631 case RawDexFile::kBoolean:
632 field->SetBoolean(value.z);
633 break;
634 case RawDexFile::kNull:
635 field->SetObject(value.l);
636 break;
637 default:
Carl Shapiro606258b2011-07-09 16:09:09 -0700638 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700639 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700640 }
641}
642
643bool ClassLinker::LinkClass(Class* klass) {
644 CHECK(klass->status_ == Class::kStatusIdx ||
645 klass->status_ == Class::kStatusLoaded);
646 if (klass->status_ == Class::kStatusIdx) {
647 if (!LinkInterfaces(klass)) {
648 return false;
649 }
650 }
651 if (!LinkSuperClass(klass)) {
652 return false;
653 }
654 if (!LinkMethods(klass)) {
655 return false;
656 }
657 if (!LinkInstanceFields(klass)) {
658 return false;
659 }
660 CreateReferenceOffsets(klass);
661 CHECK_EQ(klass->status_, Class::kStatusLoaded);
662 klass->status_ = Class::kStatusResolved;
663 return true;
664}
665
666bool ClassLinker::LinkInterfaces(Class* klass) {
667 scoped_array<uint32_t> interfaces_idx;
668 // TODO: store interfaces_idx in the Class object
669 // TODO: move this outside of link interfaces
670 if (klass->interface_count_ > 0) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700671 size_t length = klass->interface_count_ * sizeof(klass->interfaces_[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700672 interfaces_idx.reset(new uint32_t[klass->interface_count_]);
Carl Shapiro565f5072011-07-10 13:39:43 -0700673 memcpy(interfaces_idx.get(), klass->interfaces_, length);
674 memset(klass->interfaces_, 0xFF, length);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700675 }
676 // Mark the class as loaded.
677 klass->status_ = Class::kStatusLoaded;
678 if (klass->super_class_idx_ != RawDexFile::kDexNoIndex) {
679 Class* super_class = ResolveClass(klass, klass->super_class_idx_);
680 if (super_class == NULL) {
681 LG << "Failed to resolve superclass";
682 return false;
683 }
684 klass->super_class_ = super_class; // TODO: write barrier
685 }
686 if (klass->interface_count_ > 0) {
687 for (size_t i = 0; i < klass->interface_count_; ++i) {
688 uint32_t idx = interfaces_idx[i];
689 klass->interfaces_[i] = ResolveClass(klass, idx);
690 if (klass->interfaces_[i] == NULL) {
691 LG << "Failed to resolve interface";
692 return false;
693 }
694 // Verify
695 if (!klass->CanAccess(klass->interfaces_[i])) {
696 LG << "Inaccessible interface";
697 return false;
698 }
699 }
700 }
701 return true;
702}
703
704bool ClassLinker::LinkSuperClass(Class* klass) {
705 CHECK(!klass->IsPrimitive());
706 const Class* super = klass->GetSuperClass();
707 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
708 if (super != NULL) {
709 LG << "Superclass must not be defined"; // TODO: ClassFormatError
710 return false;
711 }
712 // TODO: clear finalize attribute
713 return true;
714 }
715 if (super == NULL) {
716 LG << "No superclass defined"; // TODO: LinkageError
717 return false;
718 }
719 // Verify
720 if (super->IsFinal()) {
721 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
722 return false;
723 }
724 if (super->IsInterface()) {
725 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
726 return false;
727 }
728 if (!klass->CanAccess(super)) {
729 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
730 return false;
731 }
732 return true;
733}
734
735// Populate the class vtable and itable.
736bool ClassLinker::LinkMethods(Class* klass) {
737 if (klass->IsInterface()) {
738 // No vtable.
739 size_t count = klass->NumVirtualMethods();
740 if (!IsUint(16, count)) {
741 LG << "Too many methods on interface"; // TODO: VirtualMachineError
742 return false;
743 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700744 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700745 klass->GetVirtualMethod(i)->method_index_ = i;
746 }
747 } else {
748 // Link virtual method tables
749 LinkVirtualMethods(klass);
750
751 // Link interface method tables
752 LinkInterfaceMethods(klass);
753
754 // Insert stubs.
755 LinkAbstractMethods(klass);
756 }
757 return true;
758}
759
760bool ClassLinker::LinkVirtualMethods(Class* klass) {
761 uint32_t max_count = klass->NumVirtualMethods();
762 if (klass->GetSuperClass() != NULL) {
763 max_count += klass->GetSuperClass()->NumVirtualMethods();
764 } else {
765 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
766 }
767 // TODO: do not assign to the vtable field until it is fully constructed.
768 // TODO: make this a vector<Method*> instead?
769 klass->vtable_ = new Method*[max_count];
770 if (klass->HasSuperClass()) {
771 memcpy(klass->vtable_,
772 klass->GetSuperClass()->vtable_,
773 klass->GetSuperClass()->vtable_count_ * sizeof(Method*));
774 size_t actual_count = klass->GetSuperClass()->vtable_count_;
775 // See if any of our virtual methods override the superclass.
776 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
777 Method* local_method = klass->GetVirtualMethod(i);
778 size_t j = 0;
779 for (; j < klass->GetSuperClass()->vtable_count_; ++j) {
780 const Method* super_method = klass->vtable_[j];
781 if (local_method->HasSameNameAndPrototype(super_method)) {
782 // Verify
783 if (super_method->IsFinal()) {
784 LG << "Method overrides final method"; // TODO: VirtualMachineError
785 return false;
786 }
787 klass->vtable_[j] = local_method;
788 local_method->method_index_ = j;
789 break;
790 }
791 }
792 if (j == klass->GetSuperClass()->vtable_count_) {
793 // Not overriding, append.
794 klass->vtable_[actual_count] = local_method;
795 local_method->method_index_ = actual_count;
796 actual_count += 1;
797 }
798 }
799 if (!IsUint(16, actual_count)) {
800 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
801 return false;
802 }
803 CHECK_LE(actual_count, max_count);
804 if (actual_count < max_count) {
805 Method** new_vtable = new Method*[actual_count];
806 memcpy(new_vtable, klass->vtable_, actual_count * sizeof(Method*));
Carl Shapiro565f5072011-07-10 13:39:43 -0700807 delete[] klass->vtable_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700808 klass->vtable_ = new_vtable;
809 LG << "shrunk vtable: "
810 << "was " << max_count << ", "
811 << "now " << actual_count;
812 }
813 klass->vtable_count_ = actual_count;
814 } else {
815 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
816 if (!IsUint(16, klass->NumVirtualMethods())) {
817 LG << "Too many methods"; // TODO: VirtualMachineError
818 return false;
819 }
820 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
821 klass->vtable_[i] = klass->GetVirtualMethod(i);
822 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
823 }
824 klass->vtable_count_ = klass->NumVirtualMethods();
825 }
826 return true;
827}
828
829bool ClassLinker::LinkInterfaceMethods(Class* klass) {
830 int pool_offset = 0;
831 int pool_size = 0;
832 int miranda_count = 0;
833 int miranda_alloc = 0;
834 size_t super_ifcount;
835 if (klass->HasSuperClass()) {
836 super_ifcount = klass->GetSuperClass()->iftable_count_;
837 } else {
838 super_ifcount = 0;
839 }
840 size_t ifCount = super_ifcount;
841 ifCount += klass->interface_count_;
842 for (size_t i = 0; i < klass->interface_count_; i++) {
843 ifCount += klass->interfaces_[i]->iftable_count_;
844 }
845 if (ifCount == 0) {
846 assert(klass->iftable_count_ == 0);
847 assert(klass->iftable == NULL);
848 return true;
849 }
850 klass->iftable_ = new InterfaceEntry[ifCount * sizeof(InterfaceEntry)];
851 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifCount);
852 if (super_ifcount != 0) {
853 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
854 sizeof(InterfaceEntry) * super_ifcount);
855 }
856 // Flatten the interface inheritance hierarchy.
857 size_t idx = super_ifcount;
858 for (size_t i = 0; i < klass->interface_count_; i++) {
859 Class* interf = klass->interfaces_[i];
860 assert(interf != NULL);
861 if (!interf->IsInterface()) {
862 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
863 return false;
864 }
865 klass->iftable_[idx++].SetClass(interf);
866 for (size_t j = 0; j < interf->iftable_count_; j++) {
867 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
868 }
869 }
870 CHECK_EQ(idx, ifCount);
871 klass->iftable_count_ = ifCount;
872 if (klass->IsInterface() || super_ifcount == ifCount) {
873 return true;
874 }
875 for (size_t i = super_ifcount; i < ifCount; i++) {
876 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
877 }
878 if (pool_size == 0) {
879 return true;
880 }
881 klass->ifvi_pool_count_ = pool_size;
882 klass->ifvi_pool_ = new uint32_t[pool_size];
883 std::vector<Method*> miranda_list;
884 for (size_t i = super_ifcount; i < ifCount; ++i) {
885 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
886 Class* interface = klass->iftable_[i].GetClass();
887 pool_offset += interface->NumVirtualMethods(); // end here
888 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
889 Method* interface_method = interface->GetVirtualMethod(j);
890 int k; // must be signed
891 for (k = klass->vtable_count_ - 1; k >= 0; --k) {
892 if (interface_method->HasSameNameAndPrototype(klass->vtable_[k])) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700893 if (!klass->vtable_[k]->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700894 LG << "Implementation not public";
895 return false;
896 }
897 klass->iftable_[i].method_index_array_[j] = k;
898 break;
899 }
900 }
901 if (k < 0) {
902 if (miranda_count == miranda_alloc) {
903 miranda_alloc += 8;
904 if (miranda_list.empty()) {
905 miranda_list.resize(miranda_alloc);
906 } else {
907 miranda_list.resize(miranda_alloc);
908 }
909 }
910 int mir;
911 for (mir = 0; mir < miranda_count; mir++) {
912 if (miranda_list[mir]->HasSameNameAndPrototype(interface_method)) {
913 break;
914 }
915 }
916 // point the interface table at a phantom slot index
917 klass->iftable_[i].method_index_array_[j] = klass->vtable_count_ + mir;
918 if (mir == miranda_count) {
919 miranda_list[miranda_count++] = interface_method;
920 }
921 }
922 }
923 }
924 if (miranda_count != 0) {
925 Method* newVirtualMethods;
926 Method* meth;
927 int oldMethodCount, oldVtableCount;
928 if (klass->virtual_methods_ == NULL) {
929 newVirtualMethods = new Method[klass->NumVirtualMethods() + miranda_count];
930
931 } else {
932 newVirtualMethods = new Method[klass->NumVirtualMethods() + miranda_count];
933 memcpy(newVirtualMethods,
934 klass->virtual_methods_,
935 klass->NumVirtualMethods() * sizeof(Method));
936
937 }
938 if (newVirtualMethods != klass->virtual_methods_) {
939 Method* meth = newVirtualMethods;
940 for (size_t i = 0; i < klass->NumVirtualMethods(); i++, meth++) {
941 klass->vtable_[meth->method_index_] = meth;
942 }
943 }
944 oldMethodCount = klass->NumVirtualMethods();
945 klass->virtual_methods_ = newVirtualMethods;
946 klass->num_virtual_methods_ += miranda_count;
947
948 CHECK(klass->vtable_ != NULL);
949 oldVtableCount = klass->vtable_count_;
950 klass->vtable_count_ += miranda_count;
951
952 meth = klass->virtual_methods_ + oldMethodCount;
953 for (int i = 0; i < miranda_count; i++, meth++) {
954 memcpy(meth, miranda_list[i], sizeof(Method));
955 meth->klass_ = klass;
956 meth->access_flags_ |= kAccMiranda;
957 meth->method_index_ = 0xFFFF & (oldVtableCount + i);
958 klass->vtable_[oldVtableCount + i] = meth;
959 }
960 }
961 return true;
962}
963
964void ClassLinker::LinkAbstractMethods(Class* klass) {
965 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
966 Method* method = klass->GetVirtualMethod(i);
967 if (method->IsAbstract()) {
968 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
969 }
970 }
971}
972
973bool ClassLinker::LinkInstanceFields(Class* klass) {
974 int fieldOffset;
975 if (klass->GetSuperClass() != NULL) {
976 fieldOffset = klass->GetSuperClass()->object_size_;
977 } else {
978 fieldOffset = OFFSETOF_MEMBER(DataObject, fields_);
979 }
980 // Move references to the front.
981 klass->num_reference_ifields_ = 0;
982 size_t i = 0;
983 size_t j = klass->NumInstanceFields() - 1;
984 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
985 InstanceField* pField = klass->GetInstanceField(i);
986 char c = pField->GetType();
987
988 if (c != '[' && c != 'L') {
989 while (j > i) {
990 InstanceField* refField = klass->GetInstanceField(j--);
991 char rc = refField->GetType();
992 if (rc == '[' || rc == 'L') {
993 pField->Swap(refField);
994 c = rc;
995 klass->num_reference_ifields_++;
996 break;
997 }
998 }
999 } else {
1000 klass->num_reference_ifields_++;
1001 }
1002 if (c != '[' && c != 'L') {
1003 break;
1004 }
1005 pField->SetOffset(fieldOffset);
1006 fieldOffset += sizeof(uint32_t);
1007 }
1008
1009 // Now we want to pack all of the double-wide fields together. If
1010 // we're not aligned, though, we want to shuffle one 32-bit field
1011 // into place. If we can't find one, we'll have to pad it.
1012 if (i != klass->NumInstanceFields() && (fieldOffset & 0x04) != 0) {
1013 InstanceField* pField = klass->GetInstanceField(i);
1014 char c = pField->GetType();
1015
1016 if (c != 'J' && c != 'D') {
1017 // The field that comes next is 32-bit, so just advance past it.
1018 assert(c != '[' && c != 'L');
1019 pField->SetOffset(fieldOffset);
1020 fieldOffset += sizeof(uint32_t);
1021 i++;
1022 } else {
1023 // Next field is 64-bit, so search for a 32-bit field we can
1024 // swap into it.
1025 bool found = false;
1026 j = klass->NumInstanceFields() - 1;
1027 while (j > i) {
1028 InstanceField* singleField = klass->GetInstanceField(j--);
1029 char rc = singleField->GetType();
1030 if (rc != 'J' && rc != 'D') {
1031 pField->Swap(singleField);
1032 pField->SetOffset(fieldOffset);
1033 fieldOffset += sizeof(uint32_t);
1034 found = true;
1035 i++;
1036 break;
1037 }
1038 }
1039 if (!found) {
1040 fieldOffset += sizeof(uint32_t);
1041 }
1042 }
1043 }
1044
1045 // Alignment is good, shuffle any double-wide fields forward, and
1046 // finish assigning field offsets to all fields.
1047 assert(i == klass->NumInstanceFields() || (fieldOffset & 0x04) == 0);
1048 j = klass->NumInstanceFields() - 1;
1049 for ( ; i < klass->NumInstanceFields(); i++) {
1050 InstanceField* pField = klass->GetInstanceField(i);
1051 char c = pField->GetType();
1052 if (c != 'D' && c != 'J') {
1053 while (j > i) {
1054 InstanceField* doubleField = klass->GetInstanceField(j--);
1055 char rc = doubleField->GetType();
1056 if (rc == 'D' || rc == 'J') {
1057 pField->Swap(doubleField);
1058 c = rc;
1059 break;
1060 }
1061 }
1062 } else {
1063 // This is a double-wide field, leave it be.
1064 }
1065
1066 pField->SetOffset(fieldOffset);
1067 fieldOffset += sizeof(uint32_t);
1068 if (c == 'J' || c == 'D')
1069 fieldOffset += sizeof(uint32_t);
1070 }
1071
1072#ifndef NDEBUG
1073 /* Make sure that all reference fields appear before
1074 * non-reference fields, and all double-wide fields are aligned.
1075 */
1076 j = 0; // seen non-ref
1077 for (i = 0; i < klass->NumInstanceFields(); i++) {
1078 InstanceField *pField = &klass->ifields[i];
1079 char c = pField->GetType();
1080
1081 if (c == 'D' || c == 'J') {
1082 assert((pField->offset_ & 0x07) == 0);
1083 }
1084
1085 if (c != '[' && c != 'L') {
1086 if (!j) {
1087 assert(i == klass->num_reference_ifields_);
1088 j = 1;
1089 }
1090 } else if (j) {
1091 assert(false);
1092 }
1093 }
1094 if (!j) {
1095 assert(klass->num_reference_ifields_ == klass->NumInstanceFields());
1096 }
1097#endif
1098
1099 klass->object_size_ = fieldOffset;
1100 return true;
1101}
1102
1103// Set the bitmap of reference offsets, refOffsets, from the ifields
1104// list.
1105void ClassLinker::CreateReferenceOffsets(Class* klass) {
1106 uint32_t reference_offsets = 0;
1107 if (klass->HasSuperClass()) {
1108 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1109 }
1110 // If our superclass overflowed, we don't stand a chance.
1111 if (reference_offsets != CLASS_WALK_SUPER) {
1112 // All of the fields that contain object references are guaranteed
1113 // to be at the beginning of the ifields list.
1114 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1115 // Note that, per the comment on struct InstField, f->byteOffset
1116 // is the offset from the beginning of obj, not the offset into
1117 // obj->instanceData.
1118 const InstanceField* field = klass->GetInstanceField(i);
1119 size_t byte_offset = field->GetOffset();
1120 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001121 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001122 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1123 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001124 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001125 reference_offsets |= new_bit;
1126 } else {
1127 reference_offsets = CLASS_WALK_SUPER;
1128 break;
1129 }
1130 }
1131 klass->SetReferenceOffsets(reference_offsets);
1132 }
1133}
1134
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001135Class* ClassLinker::ResolveClass(const Class* referrer, uint32_t class_idx) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001136 DexFile* dex_file = referrer->GetDexFile();
1137 Class* resolved = dex_file->GetResolvedClass(class_idx);
1138 if (resolved != NULL) {
1139 return resolved;
1140 }
1141 const char* descriptor = dex_file->GetRaw()->dexStringByTypeIdx(class_idx);
1142 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Carl Shapiro565f5072011-07-10 13:39:43 -07001143 JType type = static_cast<JType>(descriptor[0]);
1144 resolved = FindPrimitiveClass(type);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001145 } else {
1146 resolved = FindClass(descriptor, referrer->GetClassLoader(), NULL);
1147 }
1148 if (resolved != NULL) {
1149 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
1150 if (referrer->GetDexFile() != check->GetDexFile()) {
1151 if (check->GetClassLoader() != NULL) {
1152 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1153 return NULL;
1154 }
1155 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001156 dex_file->SetResolvedClass(resolved, class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001157 } else {
Carl Shapirob5573532011-07-12 18:22:59 -07001158 CHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001159 }
1160 return resolved;
1161}
1162
1163Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1164 /*MethodType*/ int method_type) {
1165 CHECK(false);
1166 return NULL;
1167}
1168
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001169String* ClassLinker::ResolveString(const Class* referring, uint32_t string_idx) {
1170 const RawDexFile* raw = referring->GetDexFile()->GetRaw();
1171 const RawDexFile::StringId& string_id = raw->GetStringId(string_idx);
1172 const char* string_data = raw->GetStringData(string_id);
1173 String* new_string = Heap::AllocStringFromModifiedUtf8(string_data);
1174 // TODO: intern the new string
1175 referring->GetDexFile()->SetResolvedString(new_string, string_idx);
1176 return new_string;
1177}
1178
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001179} // namespace art