blob: 2b6500ef8c4e30d359b596d08c3469d978ff47a4 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "src/dex_file.h"
4#include "src/heap.h"
5#include "src/globals.h"
6#include "src/logging.h"
7#include "src/object.h"
8#include "src/raw_dex_file.h"
9
10namespace art {
11
Carl Shapiro80d4dde2011-06-28 16:24:07 -070012DexFile* DexFile::OpenFile(const char* filename) {
13 RawDexFile* raw = RawDexFile::OpenFile(filename);
14 return Open(raw);
15}
16
17DexFile* DexFile::OpenBase64(const char* base64) {
18 RawDexFile* raw = RawDexFile::OpenBase64(base64);
19 return Open(raw);
20}
21
22DexFile* DexFile::Open(RawDexFile* raw) {
Carl Shapiro1fb86202011-06-27 17:43:13 -070023 if (raw == NULL) {
24 return NULL;
25 }
26 DexFile* dex_file = new DexFile(raw);
27 dex_file->Init();
28 return dex_file;
29}
30
31DexFile::~DexFile() {
Carl Shapiro1fb86202011-06-27 17:43:13 -070032 delete[] strings_;
33 delete[] classes_;
34 delete[] methods_;
35 delete[] fields_;
36}
37
38void DexFile::Init() {
39 num_strings_ = raw_->NumStringIds();
40 strings_ = new String*[num_strings_];
41
42 num_classes_ = raw_->NumTypeIds();
43 classes_ = new Class*[num_classes_];
44
45 num_methods_ = raw_->NumMethodIds();
46 methods_ = new Method*[num_methods_];
47
48 num_fields_ = raw_->NumFieldIds();
49 fields_ = new Field*[num_fields_];
50}
51
52Class* DexFile::LoadClass(const char* descriptor) {
53 const RawDexFile::ClassDef* class_def = raw_->FindClassDef(descriptor);
54 if (class_def == NULL) {
55 return NULL;
56 } else {
57 return LoadClass(*class_def);
58 }
59}
60
61Class* DexFile::LoadClass(const RawDexFile::ClassDef& class_def) {
62 const byte* class_data = raw_->GetClassData(class_def);
Carl Shapiro3ee755d2011-06-28 12:11:04 -070063 RawDexFile::ClassDataHeader header = raw_->ReadClassDataHeader(&class_data);
Carl Shapiro1fb86202011-06-27 17:43:13 -070064
65 const char* descriptor = raw_->GetClassDescriptor(class_def);
66 CHECK(descriptor != NULL);
67
68 // Allocate storage for the new class object.
69 size_t size = Class::Size(header.static_fields_size_);
70 Class* klass = Heap::AllocClass(size);
Carl Shapiro3ee755d2011-06-28 12:11:04 -070071 CHECK(klass != NULL); // TODO: throw an OOME
Carl Shapiro1fb86202011-06-27 17:43:13 -070072
Carl Shapiro3ee755d2011-06-28 12:11:04 -070073 klass->klass_ = NULL; // TODO
Carl Shapiro1fb86202011-06-27 17:43:13 -070074 klass->descriptor_ = descriptor;
75 klass->access_flags_ = class_def.access_flags_;
76 klass->class_loader_ = NULL; // TODO
77 klass->dex_file_ = this;
78 klass->primitive_type_ = Class::kPrimNot;
79 klass->status_ = Class::kClassIdx;
80
81 klass->super_ = reinterpret_cast<Class*>(class_def.superclass_idx_);
82
Carl Shapiro3ee755d2011-06-28 12:11:04 -070083 klass->num_sfields_ = header.static_fields_size_;
84 klass->num_ifields_ = header.instance_fields_size_;
85 klass->num_dmethods_ = header.direct_methods_size_;
86 klass->num_vmethods_ = header.virtual_methods_size_;
87
88 klass->source_file_ = raw_->dexGetSourceFile(class_def);
89
Carl Shapiro1fb86202011-06-27 17:43:13 -070090 // Load class interfaces.
Carl Shapiro3ee755d2011-06-28 12:11:04 -070091 LoadInterfaces(class_def, klass);
Carl Shapiro1fb86202011-06-27 17:43:13 -070092
93 // Load static fields.
Carl Shapiro3ee755d2011-06-28 12:11:04 -070094 if (klass->num_sfields_ != 0) {
95 uint32_t last_idx = 0;
96 for (size_t i = 0; i < klass->num_sfields_; ++i) {
97 RawDexFile::Field raw_field;
98 raw_->dexReadClassDataField(&class_data, &raw_field, &last_idx);
99 LoadField(klass, raw_field, &klass->sfields_[i]);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700100 }
101 }
102
103 // Load instance fields.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700104 if (klass->num_ifields_ != 0) {
105 // TODO: append instance fields to class object
106 klass->ifields_ = new IField[klass->num_ifields_];
107 uint32_t last_idx = 0;
108 for (size_t i = 0; i < klass->num_ifields_; ++i) {
109 RawDexFile::Field raw_field;
110 raw_->dexReadClassDataField(&class_data, &raw_field, &last_idx);
111 LoadField(klass, raw_field, &klass->ifields_[i]);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700112 }
113 }
114
115 // Load direct methods.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700116 if (klass->num_dmethods_ != 0) {
117 // TODO: append direct methods to class object
118 klass->dmethods_ = new Method[klass->num_dmethods_];
119 uint32_t last_idx = 0;
120 for (size_t i = 0; i < klass->num_dmethods_; ++i) {
121 RawDexFile::Method raw_method;
122 raw_->dexReadClassDataMethod(&class_data, &raw_method, &last_idx);
123 LoadMethod(klass, raw_method, &klass->dmethods_[i]);
124 // TODO: register maps
125 }
126 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700127
128 // Load virtual methods.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700129 if (klass->num_vmethods_ != 0) {
130 // TODO: append virtual methods to class object
131 klass->vmethods_ = new Method[klass->num_vmethods_];
132 memset(klass->vmethods_, 0xff, sizeof(Method));
133 uint32_t last_idx = 0;
134 for (size_t i = 0; i < klass->num_vmethods_; ++i) {
135 RawDexFile::Method raw_method;
136 raw_->dexReadClassDataMethod(&class_data, &raw_method, &last_idx);
137 LoadMethod(klass, raw_method, &klass->vmethods_[i]);
138 // TODO: register maps
139 }
140 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700141
142 return klass;
143}
144
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700145void DexFile::LoadInterfaces(const RawDexFile::ClassDef& class_def,
146 Class* klass) {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700147 const RawDexFile::TypeList* list = raw_->GetInterfacesList(class_def);
148 if (list != NULL) {
149 klass->interface_count_ = list->Size();
150 klass->interfaces_ = new Class*[list->Size()];
151 for (size_t i = 0; i < list->Size(); ++i) {
152 const RawDexFile::TypeItem& type_item = list->GetTypeItem(i);
153 klass->interfaces_[i] = reinterpret_cast<Class*>(type_item.type_idx_);
154 }
155 }
156}
157
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700158void DexFile::LoadField(Class* klass, const RawDexFile::Field& src,
159 Field* dst) {
160 const RawDexFile::FieldId& field_id = raw_->GetFieldId(src.field_idx_);
161 dst->klass_ = klass;
162 dst->name_ = raw_->dexStringById(field_id.name_idx_);
163 dst->signature_ = raw_->dexStringByTypeIdx(field_id.type_idx_);
164 dst->access_flags_ = src.access_flags_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700165}
166
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700167void DexFile::LoadMethod(Class* klass, const RawDexFile::Method& src,
Carl Shapiro1fb86202011-06-27 17:43:13 -0700168 Method* dst) {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700169 const RawDexFile::MethodId& method_id = raw_->GetMethodId(src.method_idx_);
170 dst->klass_ = klass;
171 dst->name_ = raw_->dexStringById(method_id.name_idx_);
172 dst->dex_file_ = this;
173 dst->proto_idx_ = method_id.proto_idx_;
174 dst->shorty_ = raw_->GetShorty(method_id.proto_idx_);
175 dst->access_flags_ = src.access_flags_;
176
177 // TODO: check for finalize method
178
179 const RawDexFile::Code* code_item = raw_->GetCode(src);
180 if (code_item != NULL) {
181 dst->num_registers_ = code_item->registers_size_;
182 dst->num_ins_ = code_item->ins_size_;
183 dst->num_outs_ = code_item->outs_size_;
184 dst->insns_ = code_item->insns_;
185 } else {
186 uint16_t num_args = dst->NumArgRegisters();
187 if (!dst->IsStatic()) {
188 ++num_args;
189 }
190 dst->num_registers_ = dst->num_ins_ + num_args;
191 // TODO: native methods
192 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700193}
194
Carl Shapiro1fb86202011-06-27 17:43:13 -0700195} // namespace art