blob: e59926829c46e031c1c3a6b830ae735d51c7e8d8 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "dex_file.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07004
5#include <fcntl.h>
Brian Carlstrom1f870082011-08-23 16:02:11 -07006#include <limits.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -07007#include <stdio.h>
Ian Rogersd81871c2011-10-03 13:57:23 -07008#include <stdlib.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07009#include <string.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070010#include <sys/file.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070011#include <sys/mman.h>
12#include <sys/stat.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070013
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include <map>
15
16#include "UniquePtr.h"
Ian Rogers0571d352011-11-03 19:51:38 -070017#include "class_linker.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "globals.h"
Ian Rogers0571d352011-11-03 19:51:38 -070019#include "leb128.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020#include "logging.h"
21#include "object.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070022#include "os.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070023#include "stringprintf.h"
24#include "thread.h"
Ian Rogers0571d352011-11-03 19:51:38 -070025#include "utf.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070026#include "utils.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070027#include "zip_archive.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070028
29namespace art {
30
Brian Carlstromf615a612011-07-23 12:50:34 -070031const byte DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
32const byte DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070033
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070034DexFile::ClassPathEntry DexFile::FindInClassPath(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070035 const ClassPath& class_path) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070036 for (size_t i = 0; i != class_path.size(); ++i) {
37 const DexFile* dex_file = class_path[i];
38 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
39 if (dex_class_def != NULL) {
40 return ClassPathEntry(dex_file, dex_class_def);
41 }
42 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070043 // TODO: remove reinterpret_cast when issue with -std=gnu++0x host issue resolved
Brian Carlstrom7e93b502011-08-04 14:16:22 -070044 return ClassPathEntry(reinterpret_cast<const DexFile*>(NULL),
45 reinterpret_cast<const DexFile::ClassDef*>(NULL));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070046}
47
Brian Carlstromae826982011-11-09 01:33:42 -080048void DexFile::OpenDexFiles(const std::vector<const char*>& dex_filenames,
Brian Carlstrom78128a62011-09-15 17:21:19 -070049 std::vector<const DexFile*>& dex_files,
50 const std::string& strip_location_prefix) {
51 for (size_t i = 0; i < dex_filenames.size(); i++) {
52 const char* dex_filename = dex_filenames[i];
53 const DexFile* dex_file = Open(dex_filename, strip_location_prefix);
54 if (dex_file == NULL) {
55 fprintf(stderr, "could not open .dex from file %s\n", dex_filename);
56 exit(EXIT_FAILURE);
57 }
58 dex_files.push_back(dex_file);
59 }
60}
61
Brian Carlstrom16192862011-09-12 17:50:06 -070062const DexFile* DexFile::Open(const std::string& filename,
63 const std::string& strip_location_prefix) {
jeffhao262bf462011-10-20 18:36:32 -070064 if (IsValidZipFilename(filename)) {
Brian Carlstrom16192862011-09-12 17:50:06 -070065 return DexFile::OpenZip(filename, strip_location_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070066 }
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070067 if (!IsValidDexFilename(filename)) {
68 LOG(WARNING) << "Attempting to open dex file with unknown extension '" << filename << "'";
69 }
70 return DexFile::OpenFile(filename, filename, strip_location_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070071}
72
jeffhaob4df5142011-09-19 20:25:32 -070073void DexFile::ChangePermissions(int prot) const {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070074 if (mprotect(mem_map_->GetAddress(), mem_map_->GetLength(), prot) != 0) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -080075 PLOG(FATAL) << "Failed to change dex file permissions to " << prot << " for " << GetLocation();
jeffhaob4df5142011-09-19 20:25:32 -070076 }
77}
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070078
Brian Carlstrom89521892011-12-07 22:05:07 -080079const std::string StripLocationPrefix(const std::string& original_location,
80 const std::string& strip_location_prefix) {
81 StringPiece location = original_location;
82 if (!location.starts_with(strip_location_prefix)) {
83 LOG(ERROR) << location << " does not start with " << strip_location_prefix;
84 return "";
85 }
86 location.remove_prefix(strip_location_prefix.size());
87 return location.ToString();
88}
89
Brian Carlstrom16192862011-09-12 17:50:06 -070090const DexFile* DexFile::OpenFile(const std::string& filename,
91 const std::string& original_location,
92 const std::string& strip_location_prefix) {
Brian Carlstrom89521892011-12-07 22:05:07 -080093 std::string location(StripLocationPrefix(original_location, strip_location_prefix));
94 if (location.empty()) {
Brian Carlstrom16192862011-09-12 17:50:06 -070095 return NULL;
96 }
Brian Carlstrom89521892011-12-07 22:05:07 -080097
Brian Carlstromb0460ea2011-07-29 10:08:05 -070098 int fd = open(filename.c_str(), O_RDONLY); // TODO: scoped_fd
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070099 if (fd == -1) {
100 PLOG(ERROR) << "open(\"" << filename << "\", O_RDONLY) failed";
101 return NULL;
102 }
103 struct stat sbuf;
104 memset(&sbuf, 0, sizeof(sbuf));
105 if (fstat(fd, &sbuf) == -1) {
106 PLOG(ERROR) << "fstat \"" << filename << "\" failed";
107 close(fd);
108 return NULL;
109 }
110 size_t length = sbuf.st_size;
Brian Carlstrom89521892011-12-07 22:05:07 -0800111 UniquePtr<MemMap> map(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0));
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700112 if (map.get() == NULL) {
113 LOG(ERROR) << "mmap \"" << filename << "\" failed";
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700114 close(fd);
115 return NULL;
116 }
117 close(fd);
Brian Carlstrom89521892011-12-07 22:05:07 -0800118 return OpenMemory(location, map.release());
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700119}
120
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700121const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700122
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700123// Open classes.dex from within a .zip, .jar, .apk, ...
Brian Carlstrom16192862011-09-12 17:50:06 -0700124const DexFile* DexFile::OpenZip(const std::string& filename,
125 const std::string& strip_location_prefix) {
Brian Carlstrom89521892011-12-07 22:05:07 -0800126 std::string location(StripLocationPrefix(filename, strip_location_prefix));
127 if (location.empty()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700128 return NULL;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700129 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700130
Elliott Hughes90a33692011-08-30 13:27:07 -0700131 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(filename));
132 if (zip_archive.get() == NULL) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700133 LOG(ERROR) << "Failed to open " << filename << " when looking for classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700134 return NULL;
135 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800136 return DexFile::Open(*zip_archive.get(), location);
137}
138
139const DexFile* DexFile::Open(const ZipArchive& zip_archive, const std::string& location) {
140 UniquePtr<ZipEntry> zip_entry(zip_archive.Find(kClassesDex));
Elliott Hughes90a33692011-08-30 13:27:07 -0700141 if (zip_entry.get() == NULL) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800142 LOG(ERROR) << "Failed to find classes.dex within " << location;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700143 return NULL;
144 }
145
Brian Carlstrom89521892011-12-07 22:05:07 -0800146 uint32_t length = zip_entry->GetUncompressedLength();
147 UniquePtr<MemMap> map(MemMap::MapAnonymous("classes.dex extracted in memory",
148 NULL,
149 length,
150 PROT_READ | PROT_WRITE));
151 if (map.get() == NULL) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800152 LOG(ERROR) << "mmap classes.dex for \"" << location << "\" failed";
Brian Carlstrom89521892011-12-07 22:05:07 -0800153 return NULL;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700154 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800155
156 // Extract classes.dex
157 bool success = zip_entry->ExtractToMemory(*map.get());
158 if (!success) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800159 LOG(ERROR) << "Failed to extract classes.dex from '" << location << "' to memory";
Brian Carlstrom89521892011-12-07 22:05:07 -0800160 return NULL;
161 }
162
163 return OpenMemory(location, map.release());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700164}
165
Brian Carlstrom89521892011-12-07 22:05:07 -0800166const DexFile* DexFile::OpenMemory(const byte* base,
167 size_t length,
168 const std::string& location,
169 MemMap* mem_map) {
170 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
171 UniquePtr<DexFile> dex_file(new DexFile(base, length, location, mem_map));
Brian Carlstromf615a612011-07-23 12:50:34 -0700172 if (!dex_file->Init()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700173 return NULL;
174 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700175 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700176 }
177}
178
Jesse Wilson6bf19152011-09-29 13:12:33 -0400179DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700180 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
181 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
182 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
183 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400184}
185
186jobject DexFile::GetDexObject(JNIEnv* env) const {
187 MutexLock mu(dex_object_lock_);
188 if (dex_object_ != NULL) {
189 return dex_object_;
190 }
191
192 void* address = const_cast<void*>(reinterpret_cast<const void*>(base_));
193 jobject byte_buffer = env->NewDirectByteBuffer(address, length_);
194 if (byte_buffer == NULL) {
195 return NULL;
196 }
197
198 jclass c = env->FindClass("com/android/dex/Dex");
199 if (c == NULL) {
200 return NULL;
201 }
202
203 jmethodID mid = env->GetStaticMethodID(c, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
204 if (mid == NULL) {
205 return NULL;
206 }
207
208 jvalue args[1];
209 args[0].l = byte_buffer;
210 jobject local = env->CallStaticObjectMethodA(c, mid, args);
211 if (local == NULL) {
212 return NULL;
213 }
214
215 dex_object_ = env->NewGlobalRef(local);
216 return dex_object_;
217}
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700218
Brian Carlstromf615a612011-07-23 12:50:34 -0700219bool DexFile::Init() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700220 InitMembers();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800221 if (!CheckMagicAndVersion()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700222 return false;
223 }
224 InitIndex();
225 return true;
226}
227
Brian Carlstromf615a612011-07-23 12:50:34 -0700228void DexFile::InitMembers() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700229 const byte* b = base_;
230 header_ = reinterpret_cast<const Header*>(b);
231 const Header* h = header_;
232 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
233 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
234 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
235 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
236 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
237 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800238 DCHECK_EQ(length_, header_->file_size_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700239}
240
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800241bool DexFile::CheckMagicAndVersion() {
242 CHECK(header_->magic_ != NULL) << GetLocation();
243 if (!IsMagicValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800244 LOG(ERROR) << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800245 << " " << header_->magic_[0]
246 << " " << header_->magic_[1]
247 << " " << header_->magic_[2]
248 << " " << header_->magic_[3];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700249 return false;
250 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800251 if (!IsVersionValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800252 LOG(ERROR) << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800253 << " " << header_->magic_[4]
254 << " " << header_->magic_[5]
255 << " " << header_->magic_[6]
256 << " " << header_->magic_[7];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700257 return false;
258 }
259 return true;
260}
261
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800262bool DexFile::IsMagicValid(const byte* magic) {
263 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
264}
265
266bool DexFile::IsVersionValid(const byte* magic) {
267 const byte* version = &magic[sizeof(kDexMagic)];
268 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
269}
270
Ian Rogersd81871c2011-10-03 13:57:23 -0700271uint32_t DexFile::GetVersion() const {
272 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
273 return atoi(version);
274}
275
Ian Rogers0571d352011-11-03 19:51:38 -0700276int32_t DexFile::GetStringLength(const StringId& string_id) const {
277 const byte* ptr = base_ + string_id.string_data_off_;
278 return DecodeUnsignedLeb128(&ptr);
279}
280
281// Returns a pointer to the UTF-8 string data referred to by the given string_id.
282const char* DexFile::GetStringDataAndLength(const StringId& string_id, int32_t* length) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800283 CHECK(length != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700284 const byte* ptr = base_ + string_id.string_data_off_;
285 *length = DecodeUnsignedLeb128(&ptr);
286 return reinterpret_cast<const char*>(ptr);
287}
288
Brian Carlstromf615a612011-07-23 12:50:34 -0700289void DexFile::InitIndex() {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800290 CHECK_EQ(index_.size(), 0U) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700291 for (size_t i = 0; i < NumClassDefs(); ++i) {
292 const ClassDef& class_def = GetClassDef(i);
293 const char* descriptor = GetClassDescriptor(class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294 index_[descriptor] = i;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700295 }
296}
297
Brian Carlstrome24fa612011-09-29 00:53:55 -0700298bool DexFile::FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700299 Index::const_iterator it = index_.find(descriptor);
300 if (it == index_.end()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700301 return false;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700302 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700303 idx = it->second;
304 return true;
305}
306
307const DexFile::ClassDef* DexFile::FindClassDef(const StringPiece& descriptor) const {
308 uint32_t idx;
309 if (FindClassDefIndex(descriptor, idx)) {
310 return &GetClassDef(idx);
311 }
312 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700313}
314
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800315const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
316 const DexFile::StringId& name,
317 const DexFile::TypeId& type) const {
318 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
319 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
320 const uint32_t name_idx = GetIndexForStringId(name);
321 const uint16_t type_idx = GetIndexForTypeId(type);
322 uint32_t lo = 0;
323 uint32_t hi = NumFieldIds() - 1;
324 while (hi >= lo) {
325 uint32_t mid = (hi + lo) / 2;
326 const DexFile::FieldId& field = GetFieldId(mid);
327 if (class_idx > field.class_idx_) {
328 lo = mid + 1;
329 } else if (class_idx < field.class_idx_) {
330 hi = mid - 1;
331 } else {
332 if (name_idx > field.name_idx_) {
333 lo = mid + 1;
334 } else if (name_idx < field.name_idx_) {
335 hi = mid - 1;
336 } else {
337 if (type_idx > field.type_idx_) {
338 lo = mid + 1;
339 } else if (type_idx < field.type_idx_) {
340 hi = mid - 1;
341 } else {
342 return &field;
343 }
344 }
345 }
346 }
347 return NULL;
348}
349
350const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700351 const DexFile::StringId& name,
352 const DexFile::ProtoId& signature) const {
353 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800354 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700355 const uint32_t name_idx = GetIndexForStringId(name);
356 const uint16_t proto_idx = GetIndexForProtoId(signature);
357 uint32_t lo = 0;
358 uint32_t hi = NumMethodIds() - 1;
359 while (hi >= lo) {
360 uint32_t mid = (hi + lo) / 2;
361 const DexFile::MethodId& method = GetMethodId(mid);
362 if (class_idx > method.class_idx_) {
363 lo = mid + 1;
364 } else if (class_idx < method.class_idx_) {
365 hi = mid - 1;
366 } else {
367 if (name_idx > method.name_idx_) {
368 lo = mid + 1;
369 } else if (name_idx < method.name_idx_) {
370 hi = mid - 1;
371 } else {
372 if (proto_idx > method.proto_idx_) {
373 lo = mid + 1;
374 } else if (proto_idx < method.proto_idx_) {
375 hi = mid - 1;
376 } else {
377 return &method;
378 }
379 }
380 }
381 }
382 return NULL;
383}
384
385const DexFile::StringId* DexFile::FindStringId(const std::string& string) const {
386 uint32_t lo = 0;
387 uint32_t hi = NumStringIds() - 1;
388 while (hi >= lo) {
389 uint32_t mid = (hi + lo) / 2;
390 int32_t length;
391 const DexFile::StringId& str_id = GetStringId(mid);
392 const char* str = GetStringDataAndLength(str_id, &length);
393 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string.c_str(), str);
394 if (compare > 0) {
395 lo = mid + 1;
396 } else if (compare < 0) {
397 hi = mid - 1;
398 } else {
399 return &str_id;
400 }
401 }
402 return NULL;
403}
404
405const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
406 uint32_t lo = 0;
407 uint32_t hi = NumTypeIds() - 1;
408 while (hi >= lo) {
409 uint32_t mid = (hi + lo) / 2;
410 const TypeId& type_id = GetTypeId(mid);
411 if (string_idx > type_id.descriptor_idx_) {
412 lo = mid + 1;
413 } else if (string_idx < type_id.descriptor_idx_) {
414 hi = mid - 1;
415 } else {
416 return &type_id;
417 }
418 }
419 return NULL;
420}
421
422const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800423 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700424 uint32_t lo = 0;
425 uint32_t hi = NumProtoIds() - 1;
426 while (hi >= lo) {
427 uint32_t mid = (hi + lo) / 2;
428 const DexFile::ProtoId& proto = GetProtoId(mid);
429 int compare = return_type_idx - proto.return_type_idx_;
430 if (compare == 0) {
431 DexFileParameterIterator it(*this, proto);
432 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800433 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
434 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700435 it.Next();
436 i++;
437 }
438 if (compare == 0) {
439 if (it.HasNext()) {
440 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800441 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700442 compare = 1;
443 }
444 }
445 }
446 if (compare > 0) {
447 lo = mid + 1;
448 } else if (compare < 0) {
449 hi = mid - 1;
450 } else {
451 return &proto;
452 }
453 }
454 return NULL;
455}
456
457// Given a signature place the type ids into the given vector
458bool DexFile::CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
459 const std::string& signature) const {
460 if (signature[0] != '(') {
461 return false;
462 }
463 size_t offset = 1;
464 size_t end = signature.size();
465 bool process_return = false;
466 while (offset < end) {
467 char c = signature[offset];
468 offset++;
469 if (c == ')') {
470 process_return = true;
471 continue;
472 }
473 std::string descriptor;
474 descriptor += c;
475 while (c == '[') { // process array prefix
476 if (offset >= end) { // expect some descriptor following [
477 return false;
478 }
479 c = signature[offset];
480 offset++;
481 descriptor += c;
482 }
483 if (c == 'L') { // process type descriptors
484 do {
485 if (offset >= end) { // unexpected early termination of descriptor
486 return false;
487 }
488 c = signature[offset];
489 offset++;
490 descriptor += c;
491 } while (c != ';');
492 }
493 const DexFile::StringId* string_id = FindStringId(descriptor);
494 if (string_id == NULL) {
495 return false;
496 }
497 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
498 if (type_id == NULL) {
499 return false;
500 }
501 uint16_t type_idx = GetIndexForTypeId(*type_id);
502 if (!process_return) {
503 param_type_idxs->push_back(type_idx);
504 } else {
505 *return_type_idx = type_idx;
506 return offset == end; // return true if the signature had reached a sensible end
507 }
508 }
509 return false; // failed to correctly parse return type
510}
511
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700512// Materializes the method descriptor for a method prototype. Method
513// descriptors are not stored directly in the dex file. Instead, one
514// must assemble the descriptor from references in the prototype.
Ian Rogers0571d352011-11-03 19:51:38 -0700515std::string DexFile::CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700516 const ProtoId& proto_id = GetProtoId(proto_idx);
517 std::string descriptor;
518 descriptor.push_back('(');
519 const TypeList* type_list = GetProtoParameters(proto_id);
520 size_t parameter_length = 0;
521 if (type_list != NULL) {
522 // A non-zero number of arguments. Append the type names.
523 for (size_t i = 0; i < type_list->Size(); ++i) {
524 const TypeItem& type_item = type_list->GetTypeItem(i);
525 uint32_t type_idx = type_item.type_idx_;
526 int32_t type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700527 const char* name = StringByTypeIdx(type_idx, &type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700528 parameter_length += type_length;
529 descriptor.append(name);
530 }
531 }
532 descriptor.push_back(')');
533 uint32_t return_type_idx = proto_id.return_type_idx_;
534 int32_t return_type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700535 const char* name = StringByTypeIdx(return_type_idx, &return_type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700536 descriptor.append(name);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700537 if (unicode_length != NULL) {
538 *unicode_length = parameter_length + return_type_length + 2; // 2 for ( and )
539 }
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700540 return descriptor;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700541}
542
Carl Shapiro1fb86202011-06-27 17:43:13 -0700543
Shih-wei Liao195487c2011-08-20 13:29:04 -0700544int32_t DexFile::GetLineNumFromPC(const art::Method* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700545 // For native method, lineno should be -2 to indicate it is native. Note that
546 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700547 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700548 return -2;
549 }
550
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700551 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800552 DCHECK(code_item != NULL) << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700553
554 // A method with no line number info should return -1
555 LineNumFromPcContext context(rel_pc, -1);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800556 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
557 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700558 return context.line_num_;
559}
560
Ian Rogers0571d352011-11-03 19:51:38 -0700561int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
562 uint32_t address){
563 // Note: Signed type is important for max and min.
564 int32_t min = 0;
565 int32_t max = tries_size - 1;
566
567 while (max >= min) {
568 int32_t mid = (min + max) / 2;
569 const TryItem* pTry = DexFile::GetTryItems(code_item, mid);
570 uint32_t start = pTry->start_addr_;
571 if (address < start) {
572 max = mid - 1;
573 } else {
574 uint32_t end = start + pTry->insn_count_;
575 if (address >= end) {
576 min = mid + 1;
577 } else { // We have a winner!
578 return (int32_t) pTry->handler_off_;
579 }
580 }
581 }
582 // No match.
583 return -1;
584}
585
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800586void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700587 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
588 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700589 uint32_t line = DecodeUnsignedLeb128(&stream);
590 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
591 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
592 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700593 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700594
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800595 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700596 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800597 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700598 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800599 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800600 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700601 local_in_reg[arg_reg].start_address_ = 0;
602 local_in_reg[arg_reg].is_live_ = true;
603 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700604 arg_reg++;
605 }
606
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800607 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700608 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700609 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700610 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800611 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700612 return;
613 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800614 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700615 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800616 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700617 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700618 local_in_reg[arg_reg].name_ = name;
619 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800620 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700621 local_in_reg[arg_reg].start_address_ = address;
622 local_in_reg[arg_reg].is_live_ = true;
623 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700624 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700625 case 'D':
626 case 'J':
627 arg_reg += 2;
628 break;
629 default:
630 arg_reg += 1;
631 break;
632 }
633 }
634
Ian Rogers0571d352011-11-03 19:51:38 -0700635 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800636 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700637 return;
638 }
639
640 for (;;) {
641 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700642 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700643 uint16_t name_idx;
644 uint16_t descriptor_idx;
645 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700646
Shih-wei Liao195487c2011-08-20 13:29:04 -0700647 switch (opcode) {
648 case DBG_END_SEQUENCE:
649 return;
650
651 case DBG_ADVANCE_PC:
652 address += DecodeUnsignedLeb128(&stream);
653 break;
654
655 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700656 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700657 break;
658
659 case DBG_START_LOCAL:
660 case DBG_START_LOCAL_EXTENDED:
661 reg = DecodeUnsignedLeb128(&stream);
662 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700663 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800664 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700665 return;
666 }
667
jeffhaof8728872011-10-28 19:11:13 -0700668 name_idx = DecodeUnsignedLeb128P1(&stream);
669 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
670 if (opcode == DBG_START_LOCAL_EXTENDED) {
671 signature_idx = DecodeUnsignedLeb128P1(&stream);
672 }
673
Shih-wei Liao195487c2011-08-20 13:29:04 -0700674 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700675 if (need_locals) {
676 InvokeLocalCbIfLive(cnxt, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700677
Ian Rogers0571d352011-11-03 19:51:38 -0700678 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
679 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700680 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700681 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700682 }
683 local_in_reg[reg].start_address_ = address;
684 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700685 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700686 break;
687
688 case DBG_END_LOCAL:
689 reg = DecodeUnsignedLeb128(&stream);
690 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700691 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800692 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700693 return;
694 }
695
Elliott Hughes30646832011-10-13 16:59:46 -0700696 if (need_locals) {
697 InvokeLocalCbIfLive(cnxt, reg, address, local_in_reg, local_cb);
698 local_in_reg[reg].is_live_ = false;
699 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700700 break;
701
702 case DBG_RESTART_LOCAL:
703 reg = DecodeUnsignedLeb128(&stream);
704 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700705 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800706 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700707 return;
708 }
709
Elliott Hughes30646832011-10-13 16:59:46 -0700710 if (need_locals) {
711 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800712 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700713 return;
714 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700715
Elliott Hughes30646832011-10-13 16:59:46 -0700716 // If the register is live, the "restart" is superfluous,
717 // and we don't want to mess with the existing start address.
718 if (!local_in_reg[reg].is_live_) {
719 local_in_reg[reg].start_address_ = address;
720 local_in_reg[reg].is_live_ = true;
721 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700722 }
723 break;
724
725 case DBG_SET_PROLOGUE_END:
726 case DBG_SET_EPILOGUE_BEGIN:
727 case DBG_SET_FILE:
728 break;
729
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700730 default: {
731 int adjopcode = opcode - DBG_FIRST_SPECIAL;
732
Shih-wei Liao195487c2011-08-20 13:29:04 -0700733 address += adjopcode / DBG_LINE_RANGE;
734 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
735
736 if (posCb != NULL) {
737 if (posCb(cnxt, address, line)) {
738 // early exit
739 return;
740 }
741 }
742 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700743 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700744 }
745 }
746}
747
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800748void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700749 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
750 void* cnxt) const {
751 const byte* stream = GetDebugInfoStream(code_item);
752 LocalInfo local_in_reg[code_item->registers_size_];
753
754 if (stream != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800755 DecodeDebugInfo0(code_item, is_static, method_idx, posCb, local_cb, cnxt, stream, local_in_reg);
Ian Rogers0571d352011-11-03 19:51:38 -0700756 }
757 for (int reg = 0; reg < code_item->registers_size_; reg++) {
758 InvokeLocalCbIfLive(cnxt, reg, code_item->insns_size_in_code_units_, local_in_reg, local_cb);
759 }
760}
761
762bool DexFile::LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num) {
763 LineNumFromPcContext* context = (LineNumFromPcContext*) cnxt;
764
765 // We know that this callback will be called in
766 // ascending address order, so keep going until we find
767 // a match or we've just gone past it.
768 if (address > context->address_) {
769 // The line number from the previous positions callback
770 // wil be the final result.
771 return true;
772 } else {
773 context->line_num_ = line_num;
774 return address == context->address_;
775 }
776}
777
778// Decodes the header section from the class data bytes.
779void ClassDataItemIterator::ReadClassDataHeader() {
780 CHECK(ptr_pos_ != NULL);
781 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
782 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
783 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
784 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
785}
786
787void ClassDataItemIterator::ReadClassDataField() {
788 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
789 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
790}
791
792void ClassDataItemIterator::ReadClassDataMethod() {
793 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
794 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
795 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
796}
797
798// Read a signed integer. "zwidth" is the zero-based byte count.
799static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
800 int32_t val = 0;
801 for (int i = zwidth; i >= 0; --i) {
802 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
803 }
804 val >>= (3 - zwidth) * 8;
805 return val;
806}
807
808// Read an unsigned integer. "zwidth" is the zero-based byte count,
809// "fill_on_right" indicates which side we want to zero-fill from.
810static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
811 uint32_t val = 0;
812 if (!fill_on_right) {
813 for (int i = zwidth; i >= 0; --i) {
814 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
815 }
816 val >>= (3 - zwidth) * 8;
817 } else {
818 for (int i = zwidth; i >= 0; --i) {
819 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
820 }
821 }
822 return val;
823}
824
825// Read a signed long. "zwidth" is the zero-based byte count.
826static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
827 int64_t val = 0;
828 for (int i = zwidth; i >= 0; --i) {
829 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
830 }
831 val >>= (7 - zwidth) * 8;
832 return val;
833}
834
835// Read an unsigned long. "zwidth" is the zero-based byte count,
836// "fill_on_right" indicates which side we want to zero-fill from.
837static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
838 uint64_t val = 0;
839 if (!fill_on_right) {
840 for (int i = zwidth; i >= 0; --i) {
841 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
842 }
843 val >>= (7 - zwidth) * 8;
844 } else {
845 for (int i = zwidth; i >= 0; --i) {
846 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
847 }
848 }
849 return val;
850}
851
852EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
853 DexCache* dex_cache, ClassLinker* linker, const DexFile::ClassDef& class_def) :
854 dex_file_(dex_file), dex_cache_(dex_cache), linker_(linker), array_size_(), pos_(-1), type_(0) {
855 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
856 if (ptr_ == NULL) {
857 array_size_ = 0;
858 } else {
859 array_size_ = DecodeUnsignedLeb128(&ptr_);
860 }
861 if (array_size_ > 0) {
862 Next();
863 }
864}
865
866void EncodedStaticFieldValueIterator::Next() {
867 pos_++;
868 if (pos_ >= array_size_) {
869 return;
870 }
871 byte value_type = *ptr_++;
872 byte value_arg = value_type >> kEncodedValueArgShift;
873 size_t width = value_arg + 1; // assume and correct later
874 type_ = value_type & kEncodedValueTypeMask;
875 switch (type_) {
876 case kBoolean:
877 jval_.i = (value_arg != 0) ? 1 : 0;
878 width = 0;
879 break;
880 case kByte:
881 jval_.i = ReadSignedInt(ptr_, value_arg);
882 CHECK(IsInt(8, jval_.i));
883 break;
884 case kShort:
885 jval_.i = ReadSignedInt(ptr_, value_arg);
886 CHECK(IsInt(16, jval_.i));
887 break;
888 case kChar:
889 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
890 CHECK(IsUint(16, jval_.i));
891 break;
892 case kInt:
893 jval_.i = ReadSignedInt(ptr_, value_arg);
894 break;
895 case kLong:
896 jval_.j = ReadSignedLong(ptr_, value_arg);
897 break;
898 case kFloat:
899 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
900 break;
901 case kDouble:
902 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
903 break;
904 case kString:
905 case kType:
906 case kMethod:
907 case kEnum:
908 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
909 break;
910 case kField:
911 case kArray:
912 case kAnnotation:
913 UNIMPLEMENTED(FATAL) << ": type " << type_;
914 break;
915 case kNull:
916 jval_.l = NULL;
917 width = 0;
918 break;
919 default:
920 LOG(FATAL) << "Unreached";
921 }
922 ptr_ += width;
923}
924
925void EncodedStaticFieldValueIterator::ReadValueToField(Field* field) const {
926 switch (type_) {
927 case kBoolean: field->SetBoolean(NULL, jval_.z); break;
928 case kByte: field->SetByte(NULL, jval_.b); break;
929 case kShort: field->SetShort(NULL, jval_.s); break;
930 case kChar: field->SetChar(NULL, jval_.c); break;
931 case kInt: field->SetInt(NULL, jval_.i); break;
932 case kLong: field->SetLong(NULL, jval_.j); break;
933 case kFloat: field->SetFloat(NULL, jval_.f); break;
934 case kDouble: field->SetDouble(NULL, jval_.d); break;
935 case kNull: field->SetObject(NULL, NULL); break;
936 case kString: {
937 String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_);
938 field->SetObject(NULL, resolved);
939 break;
940 }
941 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
942 }
943}
944
945CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
946 handler_.address_ = -1;
947 int32_t offset = -1;
948
949 // Short-circuit the overwhelmingly common cases.
950 switch (code_item.tries_size_) {
951 case 0:
952 break;
953 case 1: {
954 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
955 uint32_t start = tries->start_addr_;
956 if (address >= start) {
957 uint32_t end = start + tries->insn_count_;
958 if (address < end) {
959 offset = tries->handler_off_;
960 }
961 }
962 break;
963 }
964 default:
965 offset = DexFile::FindCatchHandlerOffset(code_item, code_item.tries_size_, address);
966 }
967 if (offset >= 0) {
968 const byte* handler_data = DexFile::GetCatchHandlerData(code_item, offset);
969 Init(handler_data);
970 } else {
971 // Not found, initialize as empty
972 current_data_ = NULL;
973 remaining_count_ = -1;
974 catch_all_ = false;
975 DCHECK(!HasNext());
976 }
977}
978
979void CatchHandlerIterator::Init(const byte* handler_data) {
980 current_data_ = handler_data;
981 remaining_count_ = DecodeSignedLeb128(&current_data_);
982
983 // If remaining_count_ is non-positive, then it is the negative of
984 // the number of catch types, and the catches are followed by a
985 // catch-all handler.
986 if (remaining_count_ <= 0) {
987 catch_all_ = true;
988 remaining_count_ = -remaining_count_;
989 } else {
990 catch_all_ = false;
991 }
992 Next();
993}
994
995void CatchHandlerIterator::Next() {
996 if (remaining_count_ > 0) {
997 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
998 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
999 remaining_count_--;
1000 return;
1001 }
1002
1003 if (catch_all_) {
1004 handler_.type_idx_ = DexFile::kDexNoIndex16;
1005 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1006 catch_all_ = false;
1007 return;
1008 }
1009
1010 // no more handler
1011 remaining_count_ = -1;
1012}
1013
Carl Shapiro1fb86202011-06-27 17:43:13 -07001014} // namespace art