blob: 31a7d1007cfa849e35b33fb48f6a4826fb054da1 [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();
Elliott Hughes850162c2012-01-12 18:46:43 -0800147 std::string name("classes.dex extracted in memory from ");
148 name += location;
149 UniquePtr<MemMap> map(MemMap::MapAnonymous(name.c_str(), NULL, length, PROT_READ | PROT_WRITE));
Brian Carlstrom89521892011-12-07 22:05:07 -0800150 if (map.get() == NULL) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800151 LOG(ERROR) << "mmap classes.dex for \"" << location << "\" failed";
Brian Carlstrom89521892011-12-07 22:05:07 -0800152 return NULL;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700153 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800154
155 // Extract classes.dex
156 bool success = zip_entry->ExtractToMemory(*map.get());
157 if (!success) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800158 LOG(ERROR) << "Failed to extract classes.dex from '" << location << "' to memory";
Brian Carlstrom89521892011-12-07 22:05:07 -0800159 return NULL;
160 }
161
162 return OpenMemory(location, map.release());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700163}
164
Brian Carlstrom89521892011-12-07 22:05:07 -0800165const DexFile* DexFile::OpenMemory(const byte* base,
166 size_t length,
167 const std::string& location,
168 MemMap* mem_map) {
169 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
170 UniquePtr<DexFile> dex_file(new DexFile(base, length, location, mem_map));
Brian Carlstromf615a612011-07-23 12:50:34 -0700171 if (!dex_file->Init()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700172 return NULL;
173 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700174 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700175 }
176}
177
Jesse Wilson6bf19152011-09-29 13:12:33 -0400178DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700179 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
180 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
181 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
182 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400183}
184
185jobject DexFile::GetDexObject(JNIEnv* env) const {
186 MutexLock mu(dex_object_lock_);
187 if (dex_object_ != NULL) {
188 return dex_object_;
189 }
190
191 void* address = const_cast<void*>(reinterpret_cast<const void*>(base_));
192 jobject byte_buffer = env->NewDirectByteBuffer(address, length_);
193 if (byte_buffer == NULL) {
194 return NULL;
195 }
196
197 jclass c = env->FindClass("com/android/dex/Dex");
198 if (c == NULL) {
199 return NULL;
200 }
201
202 jmethodID mid = env->GetStaticMethodID(c, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
203 if (mid == NULL) {
204 return NULL;
205 }
206
207 jvalue args[1];
208 args[0].l = byte_buffer;
209 jobject local = env->CallStaticObjectMethodA(c, mid, args);
210 if (local == NULL) {
211 return NULL;
212 }
213
214 dex_object_ = env->NewGlobalRef(local);
215 return dex_object_;
216}
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700217
Brian Carlstromf615a612011-07-23 12:50:34 -0700218bool DexFile::Init() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700219 InitMembers();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800220 if (!CheckMagicAndVersion()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700221 return false;
222 }
223 InitIndex();
224 return true;
225}
226
Brian Carlstromf615a612011-07-23 12:50:34 -0700227void DexFile::InitMembers() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700228 const byte* b = base_;
229 header_ = reinterpret_cast<const Header*>(b);
230 const Header* h = header_;
231 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
232 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
233 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
234 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
235 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
236 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800237 DCHECK_EQ(length_, header_->file_size_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700238}
239
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800240bool DexFile::CheckMagicAndVersion() {
241 CHECK(header_->magic_ != NULL) << GetLocation();
242 if (!IsMagicValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800243 LOG(ERROR) << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800244 << " " << header_->magic_[0]
245 << " " << header_->magic_[1]
246 << " " << header_->magic_[2]
247 << " " << header_->magic_[3];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700248 return false;
249 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800250 if (!IsVersionValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800251 LOG(ERROR) << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800252 << " " << header_->magic_[4]
253 << " " << header_->magic_[5]
254 << " " << header_->magic_[6]
255 << " " << header_->magic_[7];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700256 return false;
257 }
258 return true;
259}
260
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800261bool DexFile::IsMagicValid(const byte* magic) {
262 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
263}
264
265bool DexFile::IsVersionValid(const byte* magic) {
266 const byte* version = &magic[sizeof(kDexMagic)];
267 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
268}
269
Ian Rogersd81871c2011-10-03 13:57:23 -0700270uint32_t DexFile::GetVersion() const {
271 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
272 return atoi(version);
273}
274
Ian Rogers0571d352011-11-03 19:51:38 -0700275int32_t DexFile::GetStringLength(const StringId& string_id) const {
276 const byte* ptr = base_ + string_id.string_data_off_;
277 return DecodeUnsignedLeb128(&ptr);
278}
279
280// Returns a pointer to the UTF-8 string data referred to by the given string_id.
281const char* DexFile::GetStringDataAndLength(const StringId& string_id, int32_t* length) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800282 CHECK(length != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700283 const byte* ptr = base_ + string_id.string_data_off_;
284 *length = DecodeUnsignedLeb128(&ptr);
285 return reinterpret_cast<const char*>(ptr);
286}
287
Brian Carlstromf615a612011-07-23 12:50:34 -0700288void DexFile::InitIndex() {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800289 CHECK_EQ(index_.size(), 0U) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700290 for (size_t i = 0; i < NumClassDefs(); ++i) {
291 const ClassDef& class_def = GetClassDef(i);
292 const char* descriptor = GetClassDescriptor(class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700293 index_[descriptor] = i;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700294 }
295}
296
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297bool DexFile::FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700298 Index::const_iterator it = index_.find(descriptor);
299 if (it == index_.end()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700300 return false;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700301 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700302 idx = it->second;
303 return true;
304}
305
306const DexFile::ClassDef* DexFile::FindClassDef(const StringPiece& descriptor) const {
307 uint32_t idx;
308 if (FindClassDefIndex(descriptor, idx)) {
309 return &GetClassDef(idx);
310 }
311 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700312}
313
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800314const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
315 const DexFile::StringId& name,
316 const DexFile::TypeId& type) const {
317 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
318 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
319 const uint32_t name_idx = GetIndexForStringId(name);
320 const uint16_t type_idx = GetIndexForTypeId(type);
321 uint32_t lo = 0;
322 uint32_t hi = NumFieldIds() - 1;
323 while (hi >= lo) {
324 uint32_t mid = (hi + lo) / 2;
325 const DexFile::FieldId& field = GetFieldId(mid);
326 if (class_idx > field.class_idx_) {
327 lo = mid + 1;
328 } else if (class_idx < field.class_idx_) {
329 hi = mid - 1;
330 } else {
331 if (name_idx > field.name_idx_) {
332 lo = mid + 1;
333 } else if (name_idx < field.name_idx_) {
334 hi = mid - 1;
335 } else {
336 if (type_idx > field.type_idx_) {
337 lo = mid + 1;
338 } else if (type_idx < field.type_idx_) {
339 hi = mid - 1;
340 } else {
341 return &field;
342 }
343 }
344 }
345 }
346 return NULL;
347}
348
349const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700350 const DexFile::StringId& name,
351 const DexFile::ProtoId& signature) const {
352 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800353 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700354 const uint32_t name_idx = GetIndexForStringId(name);
355 const uint16_t proto_idx = GetIndexForProtoId(signature);
356 uint32_t lo = 0;
357 uint32_t hi = NumMethodIds() - 1;
358 while (hi >= lo) {
359 uint32_t mid = (hi + lo) / 2;
360 const DexFile::MethodId& method = GetMethodId(mid);
361 if (class_idx > method.class_idx_) {
362 lo = mid + 1;
363 } else if (class_idx < method.class_idx_) {
364 hi = mid - 1;
365 } else {
366 if (name_idx > method.name_idx_) {
367 lo = mid + 1;
368 } else if (name_idx < method.name_idx_) {
369 hi = mid - 1;
370 } else {
371 if (proto_idx > method.proto_idx_) {
372 lo = mid + 1;
373 } else if (proto_idx < method.proto_idx_) {
374 hi = mid - 1;
375 } else {
376 return &method;
377 }
378 }
379 }
380 }
381 return NULL;
382}
383
384const DexFile::StringId* DexFile::FindStringId(const std::string& string) const {
385 uint32_t lo = 0;
386 uint32_t hi = NumStringIds() - 1;
387 while (hi >= lo) {
388 uint32_t mid = (hi + lo) / 2;
389 int32_t length;
390 const DexFile::StringId& str_id = GetStringId(mid);
391 const char* str = GetStringDataAndLength(str_id, &length);
392 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string.c_str(), str);
393 if (compare > 0) {
394 lo = mid + 1;
395 } else if (compare < 0) {
396 hi = mid - 1;
397 } else {
398 return &str_id;
399 }
400 }
401 return NULL;
402}
403
404const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
405 uint32_t lo = 0;
406 uint32_t hi = NumTypeIds() - 1;
407 while (hi >= lo) {
408 uint32_t mid = (hi + lo) / 2;
409 const TypeId& type_id = GetTypeId(mid);
410 if (string_idx > type_id.descriptor_idx_) {
411 lo = mid + 1;
412 } else if (string_idx < type_id.descriptor_idx_) {
413 hi = mid - 1;
414 } else {
415 return &type_id;
416 }
417 }
418 return NULL;
419}
420
421const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800422 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700423 uint32_t lo = 0;
424 uint32_t hi = NumProtoIds() - 1;
425 while (hi >= lo) {
426 uint32_t mid = (hi + lo) / 2;
427 const DexFile::ProtoId& proto = GetProtoId(mid);
428 int compare = return_type_idx - proto.return_type_idx_;
429 if (compare == 0) {
430 DexFileParameterIterator it(*this, proto);
431 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800432 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
433 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700434 it.Next();
435 i++;
436 }
437 if (compare == 0) {
438 if (it.HasNext()) {
439 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700441 compare = 1;
442 }
443 }
444 }
445 if (compare > 0) {
446 lo = mid + 1;
447 } else if (compare < 0) {
448 hi = mid - 1;
449 } else {
450 return &proto;
451 }
452 }
453 return NULL;
454}
455
456// Given a signature place the type ids into the given vector
457bool DexFile::CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
458 const std::string& signature) const {
459 if (signature[0] != '(') {
460 return false;
461 }
462 size_t offset = 1;
463 size_t end = signature.size();
464 bool process_return = false;
465 while (offset < end) {
466 char c = signature[offset];
467 offset++;
468 if (c == ')') {
469 process_return = true;
470 continue;
471 }
472 std::string descriptor;
473 descriptor += c;
474 while (c == '[') { // process array prefix
475 if (offset >= end) { // expect some descriptor following [
476 return false;
477 }
478 c = signature[offset];
479 offset++;
480 descriptor += c;
481 }
482 if (c == 'L') { // process type descriptors
483 do {
484 if (offset >= end) { // unexpected early termination of descriptor
485 return false;
486 }
487 c = signature[offset];
488 offset++;
489 descriptor += c;
490 } while (c != ';');
491 }
492 const DexFile::StringId* string_id = FindStringId(descriptor);
493 if (string_id == NULL) {
494 return false;
495 }
496 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
497 if (type_id == NULL) {
498 return false;
499 }
500 uint16_t type_idx = GetIndexForTypeId(*type_id);
501 if (!process_return) {
502 param_type_idxs->push_back(type_idx);
503 } else {
504 *return_type_idx = type_idx;
505 return offset == end; // return true if the signature had reached a sensible end
506 }
507 }
508 return false; // failed to correctly parse return type
509}
510
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700511// Materializes the method descriptor for a method prototype. Method
512// descriptors are not stored directly in the dex file. Instead, one
513// must assemble the descriptor from references in the prototype.
Ian Rogers0571d352011-11-03 19:51:38 -0700514std::string DexFile::CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700515 const ProtoId& proto_id = GetProtoId(proto_idx);
516 std::string descriptor;
517 descriptor.push_back('(');
518 const TypeList* type_list = GetProtoParameters(proto_id);
519 size_t parameter_length = 0;
520 if (type_list != NULL) {
521 // A non-zero number of arguments. Append the type names.
522 for (size_t i = 0; i < type_list->Size(); ++i) {
523 const TypeItem& type_item = type_list->GetTypeItem(i);
524 uint32_t type_idx = type_item.type_idx_;
525 int32_t type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700526 const char* name = StringByTypeIdx(type_idx, &type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700527 parameter_length += type_length;
528 descriptor.append(name);
529 }
530 }
531 descriptor.push_back(')');
532 uint32_t return_type_idx = proto_id.return_type_idx_;
533 int32_t return_type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700534 const char* name = StringByTypeIdx(return_type_idx, &return_type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700535 descriptor.append(name);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700536 if (unicode_length != NULL) {
537 *unicode_length = parameter_length + return_type_length + 2; // 2 for ( and )
538 }
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700539 return descriptor;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700540}
541
Carl Shapiro1fb86202011-06-27 17:43:13 -0700542
Shih-wei Liao195487c2011-08-20 13:29:04 -0700543int32_t DexFile::GetLineNumFromPC(const art::Method* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700544 // For native method, lineno should be -2 to indicate it is native. Note that
545 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700546 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700547 return -2;
548 }
549
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700550 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800551 DCHECK(code_item != NULL) << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700552
553 // A method with no line number info should return -1
554 LineNumFromPcContext context(rel_pc, -1);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800555 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
556 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700557 return context.line_num_;
558}
559
Ian Rogers0571d352011-11-03 19:51:38 -0700560int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
561 uint32_t address){
562 // Note: Signed type is important for max and min.
563 int32_t min = 0;
564 int32_t max = tries_size - 1;
565
566 while (max >= min) {
567 int32_t mid = (min + max) / 2;
568 const TryItem* pTry = DexFile::GetTryItems(code_item, mid);
569 uint32_t start = pTry->start_addr_;
570 if (address < start) {
571 max = mid - 1;
572 } else {
573 uint32_t end = start + pTry->insn_count_;
574 if (address >= end) {
575 min = mid + 1;
576 } else { // We have a winner!
577 return (int32_t) pTry->handler_off_;
578 }
579 }
580 }
581 // No match.
582 return -1;
583}
584
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800585void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700586 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
587 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700588 uint32_t line = DecodeUnsignedLeb128(&stream);
589 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
590 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
591 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700592 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700593
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800594 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700595 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800596 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700597 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800598 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800599 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700600 local_in_reg[arg_reg].start_address_ = 0;
601 local_in_reg[arg_reg].is_live_ = true;
602 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700603 arg_reg++;
604 }
605
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800606 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700607 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700608 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700609 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800610 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700611 return;
612 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800613 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700614 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800615 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700616 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700617 local_in_reg[arg_reg].name_ = name;
618 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800619 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700620 local_in_reg[arg_reg].start_address_ = address;
621 local_in_reg[arg_reg].is_live_ = true;
622 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700623 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700624 case 'D':
625 case 'J':
626 arg_reg += 2;
627 break;
628 default:
629 arg_reg += 1;
630 break;
631 }
632 }
633
Ian Rogers0571d352011-11-03 19:51:38 -0700634 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800635 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700636 return;
637 }
638
639 for (;;) {
640 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700641 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700642 uint16_t name_idx;
643 uint16_t descriptor_idx;
644 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700645
Shih-wei Liao195487c2011-08-20 13:29:04 -0700646 switch (opcode) {
647 case DBG_END_SEQUENCE:
648 return;
649
650 case DBG_ADVANCE_PC:
651 address += DecodeUnsignedLeb128(&stream);
652 break;
653
654 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700655 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700656 break;
657
658 case DBG_START_LOCAL:
659 case DBG_START_LOCAL_EXTENDED:
660 reg = DecodeUnsignedLeb128(&stream);
661 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700662 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800663 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700664 return;
665 }
666
jeffhaof8728872011-10-28 19:11:13 -0700667 name_idx = DecodeUnsignedLeb128P1(&stream);
668 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
669 if (opcode == DBG_START_LOCAL_EXTENDED) {
670 signature_idx = DecodeUnsignedLeb128P1(&stream);
671 }
672
Shih-wei Liao195487c2011-08-20 13:29:04 -0700673 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700674 if (need_locals) {
675 InvokeLocalCbIfLive(cnxt, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700676
Ian Rogers0571d352011-11-03 19:51:38 -0700677 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
678 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700679 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700680 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700681 }
682 local_in_reg[reg].start_address_ = address;
683 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700684 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700685 break;
686
687 case DBG_END_LOCAL:
688 reg = DecodeUnsignedLeb128(&stream);
689 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700690 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800691 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700692 return;
693 }
694
Elliott Hughes30646832011-10-13 16:59:46 -0700695 if (need_locals) {
696 InvokeLocalCbIfLive(cnxt, reg, address, local_in_reg, local_cb);
697 local_in_reg[reg].is_live_ = false;
698 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700699 break;
700
701 case DBG_RESTART_LOCAL:
702 reg = DecodeUnsignedLeb128(&stream);
703 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700704 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800705 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700706 return;
707 }
708
Elliott Hughes30646832011-10-13 16:59:46 -0700709 if (need_locals) {
710 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800711 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700712 return;
713 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700714
Elliott Hughes30646832011-10-13 16:59:46 -0700715 // If the register is live, the "restart" is superfluous,
716 // and we don't want to mess with the existing start address.
717 if (!local_in_reg[reg].is_live_) {
718 local_in_reg[reg].start_address_ = address;
719 local_in_reg[reg].is_live_ = true;
720 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700721 }
722 break;
723
724 case DBG_SET_PROLOGUE_END:
725 case DBG_SET_EPILOGUE_BEGIN:
726 case DBG_SET_FILE:
727 break;
728
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700729 default: {
730 int adjopcode = opcode - DBG_FIRST_SPECIAL;
731
Shih-wei Liao195487c2011-08-20 13:29:04 -0700732 address += adjopcode / DBG_LINE_RANGE;
733 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
734
735 if (posCb != NULL) {
736 if (posCb(cnxt, address, line)) {
737 // early exit
738 return;
739 }
740 }
741 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700742 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700743 }
744 }
745}
746
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800747void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700748 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
749 void* cnxt) const {
750 const byte* stream = GetDebugInfoStream(code_item);
751 LocalInfo local_in_reg[code_item->registers_size_];
752
753 if (stream != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800754 DecodeDebugInfo0(code_item, is_static, method_idx, posCb, local_cb, cnxt, stream, local_in_reg);
Ian Rogers0571d352011-11-03 19:51:38 -0700755 }
756 for (int reg = 0; reg < code_item->registers_size_; reg++) {
757 InvokeLocalCbIfLive(cnxt, reg, code_item->insns_size_in_code_units_, local_in_reg, local_cb);
758 }
759}
760
761bool DexFile::LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num) {
762 LineNumFromPcContext* context = (LineNumFromPcContext*) cnxt;
763
764 // We know that this callback will be called in
765 // ascending address order, so keep going until we find
766 // a match or we've just gone past it.
767 if (address > context->address_) {
768 // The line number from the previous positions callback
769 // wil be the final result.
770 return true;
771 } else {
772 context->line_num_ = line_num;
773 return address == context->address_;
774 }
775}
776
777// Decodes the header section from the class data bytes.
778void ClassDataItemIterator::ReadClassDataHeader() {
779 CHECK(ptr_pos_ != NULL);
780 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
781 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
782 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
783 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
784}
785
786void ClassDataItemIterator::ReadClassDataField() {
787 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
788 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
789}
790
791void ClassDataItemIterator::ReadClassDataMethod() {
792 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
793 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
794 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
795}
796
797// Read a signed integer. "zwidth" is the zero-based byte count.
798static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
799 int32_t val = 0;
800 for (int i = zwidth; i >= 0; --i) {
801 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
802 }
803 val >>= (3 - zwidth) * 8;
804 return val;
805}
806
807// Read an unsigned integer. "zwidth" is the zero-based byte count,
808// "fill_on_right" indicates which side we want to zero-fill from.
809static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
810 uint32_t val = 0;
811 if (!fill_on_right) {
812 for (int i = zwidth; i >= 0; --i) {
813 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
814 }
815 val >>= (3 - zwidth) * 8;
816 } else {
817 for (int i = zwidth; i >= 0; --i) {
818 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
819 }
820 }
821 return val;
822}
823
824// Read a signed long. "zwidth" is the zero-based byte count.
825static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
826 int64_t val = 0;
827 for (int i = zwidth; i >= 0; --i) {
828 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
829 }
830 val >>= (7 - zwidth) * 8;
831 return val;
832}
833
834// Read an unsigned long. "zwidth" is the zero-based byte count,
835// "fill_on_right" indicates which side we want to zero-fill from.
836static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
837 uint64_t val = 0;
838 if (!fill_on_right) {
839 for (int i = zwidth; i >= 0; --i) {
840 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
841 }
842 val >>= (7 - zwidth) * 8;
843 } else {
844 for (int i = zwidth; i >= 0; --i) {
845 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
846 }
847 }
848 return val;
849}
850
851EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
852 DexCache* dex_cache, ClassLinker* linker, const DexFile::ClassDef& class_def) :
853 dex_file_(dex_file), dex_cache_(dex_cache), linker_(linker), array_size_(), pos_(-1), type_(0) {
854 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
855 if (ptr_ == NULL) {
856 array_size_ = 0;
857 } else {
858 array_size_ = DecodeUnsignedLeb128(&ptr_);
859 }
860 if (array_size_ > 0) {
861 Next();
862 }
863}
864
865void EncodedStaticFieldValueIterator::Next() {
866 pos_++;
867 if (pos_ >= array_size_) {
868 return;
869 }
870 byte value_type = *ptr_++;
871 byte value_arg = value_type >> kEncodedValueArgShift;
872 size_t width = value_arg + 1; // assume and correct later
873 type_ = value_type & kEncodedValueTypeMask;
874 switch (type_) {
875 case kBoolean:
876 jval_.i = (value_arg != 0) ? 1 : 0;
877 width = 0;
878 break;
879 case kByte:
880 jval_.i = ReadSignedInt(ptr_, value_arg);
881 CHECK(IsInt(8, jval_.i));
882 break;
883 case kShort:
884 jval_.i = ReadSignedInt(ptr_, value_arg);
885 CHECK(IsInt(16, jval_.i));
886 break;
887 case kChar:
888 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
889 CHECK(IsUint(16, jval_.i));
890 break;
891 case kInt:
892 jval_.i = ReadSignedInt(ptr_, value_arg);
893 break;
894 case kLong:
895 jval_.j = ReadSignedLong(ptr_, value_arg);
896 break;
897 case kFloat:
898 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
899 break;
900 case kDouble:
901 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
902 break;
903 case kString:
904 case kType:
905 case kMethod:
906 case kEnum:
907 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
908 break;
909 case kField:
910 case kArray:
911 case kAnnotation:
912 UNIMPLEMENTED(FATAL) << ": type " << type_;
913 break;
914 case kNull:
915 jval_.l = NULL;
916 width = 0;
917 break;
918 default:
919 LOG(FATAL) << "Unreached";
920 }
921 ptr_ += width;
922}
923
924void EncodedStaticFieldValueIterator::ReadValueToField(Field* field) const {
925 switch (type_) {
926 case kBoolean: field->SetBoolean(NULL, jval_.z); break;
927 case kByte: field->SetByte(NULL, jval_.b); break;
928 case kShort: field->SetShort(NULL, jval_.s); break;
929 case kChar: field->SetChar(NULL, jval_.c); break;
930 case kInt: field->SetInt(NULL, jval_.i); break;
931 case kLong: field->SetLong(NULL, jval_.j); break;
932 case kFloat: field->SetFloat(NULL, jval_.f); break;
933 case kDouble: field->SetDouble(NULL, jval_.d); break;
934 case kNull: field->SetObject(NULL, NULL); break;
935 case kString: {
936 String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_);
937 field->SetObject(NULL, resolved);
938 break;
939 }
940 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
941 }
942}
943
944CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
945 handler_.address_ = -1;
946 int32_t offset = -1;
947
948 // Short-circuit the overwhelmingly common cases.
949 switch (code_item.tries_size_) {
950 case 0:
951 break;
952 case 1: {
953 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
954 uint32_t start = tries->start_addr_;
955 if (address >= start) {
956 uint32_t end = start + tries->insn_count_;
957 if (address < end) {
958 offset = tries->handler_off_;
959 }
960 }
961 break;
962 }
963 default:
964 offset = DexFile::FindCatchHandlerOffset(code_item, code_item.tries_size_, address);
965 }
966 if (offset >= 0) {
967 const byte* handler_data = DexFile::GetCatchHandlerData(code_item, offset);
968 Init(handler_data);
969 } else {
970 // Not found, initialize as empty
971 current_data_ = NULL;
972 remaining_count_ = -1;
973 catch_all_ = false;
974 DCHECK(!HasNext());
975 }
976}
977
978void CatchHandlerIterator::Init(const byte* handler_data) {
979 current_data_ = handler_data;
980 remaining_count_ = DecodeSignedLeb128(&current_data_);
981
982 // If remaining_count_ is non-positive, then it is the negative of
983 // the number of catch types, and the catches are followed by a
984 // catch-all handler.
985 if (remaining_count_ <= 0) {
986 catch_all_ = true;
987 remaining_count_ = -remaining_count_;
988 } else {
989 catch_all_ = false;
990 }
991 Next();
992}
993
994void CatchHandlerIterator::Next() {
995 if (remaining_count_ > 0) {
996 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
997 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
998 remaining_count_--;
999 return;
1000 }
1001
1002 if (catch_all_) {
1003 handler_.type_idx_ = DexFile::kDexNoIndex16;
1004 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1005 catch_all_ = false;
1006 return;
1007 }
1008
1009 // no more handler
1010 remaining_count_ = -1;
1011}
1012
Carl Shapiro1fb86202011-06-27 17:43:13 -07001013} // namespace art