blob: cf0bf204d047305a831c5460e43394dba5145046 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_DEX_FILE_H_
4#define ART_SRC_DEX_FILE_H_
5
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07006#include <map>
Elliott Hughes0c424cb2011-08-26 10:16:25 -07007#include <string>
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07008#include <vector>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07009
Elliott Hughes90a33692011-08-30 13:27:07 -070010#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "globals.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070012#include "leb128.h"
13#include "logging.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070014#include "stringpiece.h"
15#include "strutil.h"
Shih-wei Liao2fb97532011-08-11 16:17:23 -070016#include "utils.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070017
18namespace art {
19
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070020union JValue;
Shih-wei Liao195487c2011-08-20 13:29:04 -070021class String;
22class Method;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070023
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070024// TODO: move all of the macro functionality into the DexCache class.
Brian Carlstromf615a612011-07-23 12:50:34 -070025class DexFile {
Carl Shapiro1fb86202011-06-27 17:43:13 -070026 public:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070027 static const byte kDexMagic[];
28 static const byte kDexMagicVersion[];
29 static const size_t kSha1DigestSize = 20;
Carl Shapiro80d4dde2011-06-28 16:24:07 -070030
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070031 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
32 static const byte kEncodedValueArgShift = 5;
33
34 // The value of an invalid index.
35 static const uint32_t kDexNoIndex = 0xFFFFFFFF;
36
37 enum ValueType {
38 kByte = 0x00,
39 kShort = 0x02,
40 kChar = 0x03,
41 kInt = 0x04,
42 kLong = 0x06,
43 kFloat = 0x10,
44 kDouble = 0x11,
45 kString = 0x17,
46 kType = 0x18,
47 kField = 0x19,
48 kMethod = 0x1a,
49 kEnum = 0x1b,
50 kArray = 0x1c,
51 kAnnotation = 0x1d,
52 kNull = 0x1e,
53 kBoolean = 0x1f
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070054 };
Carl Shapiro1fb86202011-06-27 17:43:13 -070055
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070056 // Raw header_item.
57 struct Header {
58 uint8_t magic_[8];
59 uint32_t checksum_;
60 uint8_t signature_[kSha1DigestSize];
61 uint32_t file_size_; // length of entire file
62 uint32_t header_size_; // offset to start of next section
63 uint32_t endian_tag_;
64 uint32_t link_size_;
65 uint32_t link_off_;
66 uint32_t map_off_;
67 uint32_t string_ids_size_;
68 uint32_t string_ids_off_;
69 uint32_t type_ids_size_;
70 uint32_t type_ids_off_;
71 uint32_t proto_ids_size_;
72 uint32_t proto_ids_off_;
73 uint32_t field_ids_size_;
74 uint32_t field_ids_off_;
75 uint32_t method_ids_size_;
76 uint32_t method_ids_off_;
77 uint32_t class_defs_size_;
78 uint32_t class_defs_off_;
79 uint32_t data_size_;
80 uint32_t data_off_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070081 private:
82 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070083 };
Carl Shapiro1fb86202011-06-27 17:43:13 -070084
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070085 // Raw string_id_item.
86 struct StringId {
87 uint32_t string_data_off_; // offset in bytes from the base address
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070088 private:
89 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070090 };
91
92 // Raw type_id_item.
93 struct TypeId {
94 uint32_t descriptor_idx_; // index into string_ids
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070095 private:
96 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070097 };
98
99 // Raw field_id_item.
100 struct FieldId {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700101 uint16_t class_idx_; // index into type_ids_ list for defining class
102 uint16_t type_idx_; // index into type_ids_ for field type
103 uint32_t name_idx_; // index into string_ids_ for field name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700104 private:
105 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700106 };
107
108 // Raw method_id_item.
109 struct MethodId {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700110 uint16_t class_idx_; // index into type_ids_ list for defining class
111 uint16_t proto_idx_; // index into proto_ids_ for method prototype
112 uint32_t name_idx_; // index into string_ids_ for method name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700113 private:
114 DISALLOW_COPY_AND_ASSIGN(MethodId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700115 };
116
117 // Raw proto_id_item.
118 struct ProtoId {
119 uint32_t shorty_idx_; // index into string_ids for shorty descriptor
120 uint32_t return_type_idx_; // index into type_ids list for return type
121 uint32_t parameters_off_; // file offset to type_list for parameter types
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700122 private:
123 DISALLOW_COPY_AND_ASSIGN(ProtoId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700124 };
125
126 // Raw class_def_item.
127 struct ClassDef {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700128 uint32_t class_idx_; // index into type_ids_ for this class
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700129 uint32_t access_flags_;
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700130 uint32_t superclass_idx_; // index into type_ids_ for superclass
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700131 uint32_t interfaces_off_; // file offset to TypeList
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700132 uint32_t source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700133 uint32_t annotations_off_; // file offset to annotations_directory_item
134 uint32_t class_data_off_; // file offset to class_data_item
135 uint32_t static_values_off_; // file offset to EncodedArray
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700136 private:
137 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700138 };
139
140 // Raw type_item.
141 struct TypeItem {
142 uint16_t type_idx_; // index into type_ids section
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700143 private:
144 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700145 };
146
147 // Raw type_list.
148 class TypeList {
149 public:
150 uint32_t Size() const {
151 return size_;
152 }
153
154 const TypeItem& GetTypeItem(uint32_t idx) const {
155 CHECK_LT(idx, this->size_);
156 return this->list_[idx];
157 }
158
159 private:
160 uint32_t size_; // size of the list, in entries
161 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700162 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700163 };
164
165 class ParameterIterator { // TODO: stream
166 public:
Brian Carlstromf615a612011-07-23 12:50:34 -0700167 ParameterIterator(const DexFile& dex_file, const ProtoId& proto_id)
168 : dex_file_(dex_file), size_(0), pos_(0) {
169 type_list_ = dex_file_.GetProtoParameters(proto_id);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700170 if (type_list_ != NULL) {
171 size_ = type_list_->Size();
172 }
173 }
174 bool HasNext() const { return pos_ != size_; }
175 void Next() { ++pos_; }
176 const char* GetDescriptor() {
177 uint32_t type_idx = type_list_->GetTypeItem(pos_).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700178 return dex_file_.dexStringByTypeIdx(type_idx);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700179 }
180 private:
Brian Carlstromf615a612011-07-23 12:50:34 -0700181 const DexFile& dex_file_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700182 const TypeList* type_list_;
183 uint32_t size_;
184 uint32_t pos_;
185 DISALLOW_IMPLICIT_CONSTRUCTORS(ParameterIterator);
186 };
187
188 ParameterIterator* GetParameterIterator(const ProtoId& proto_id) const {
189 return new ParameterIterator(*this, proto_id);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700190 }
191
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700192 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
193 return dexStringByTypeIdx(proto_id.return_type_idx_);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700194 }
195
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700196 // Raw code_item.
197 struct CodeItem {
198 uint16_t registers_size_;
199 uint16_t ins_size_;
200 uint16_t outs_size_;
201 uint16_t tries_size_;
202 uint32_t debug_info_off_; // file offset to debug info stream
203 uint32_t insns_size_; // size of the insns array, in 2 byte code units
204 uint16_t insns_[1];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700205 private:
206 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700207 };
208
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700209 struct CatchHandlerItem {
210 uint32_t type_idx_; // type index of the caught exception type
211 uint32_t address_; // handler address
212 };
213
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700214 // Raw try_item.
215 struct TryItem {
216 uint32_t start_addr_;
217 uint16_t insn_count_;
218 uint16_t handler_off_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700219 private:
220 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700221 };
222
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700223 class CatchHandlerIterator {
224 public:
225 CatchHandlerIterator() {
226 remaining_count_ = -1;
227 catch_all_ = false;
228 }
229
230 CatchHandlerIterator(const byte* handler_data) {
231 current_data_ = handler_data;
jeffhaoba5ebb92011-08-25 17:24:37 -0700232 remaining_count_ = DecodeSignedLeb128(&current_data_);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700233
234 // If remaining_count_ is non-positive, then it is the negative of
235 // the number of catch types, and the catches are followed by a
236 // catch-all handler.
237 if (remaining_count_ <= 0) {
238 catch_all_ = true;
239 remaining_count_ = -remaining_count_;
240 } else {
241 catch_all_ = false;
242 }
243 Next();
244 }
245
Shih-wei Liaofe909f22011-08-12 19:20:26 -0700246 const CatchHandlerItem& Get() const {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700247 return handler_;
248 }
249
jeffhaoba5ebb92011-08-25 17:24:37 -0700250 const byte* GetData() const {
251 return current_data_;
252 }
253
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700254 void Next() {
255 if (remaining_count_ > 0) {
256 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
257 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
258 remaining_count_--;
259 return;
260 }
261
262 if (catch_all_) {
263 handler_.type_idx_ = kDexNoIndex;
264 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
265 catch_all_ = false;
266 return;
267 }
268
269 // no more handler
270 remaining_count_ = -1;
271 }
272
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700273 bool HasNext() const {
Shih-wei Liao4e5c0b92011-08-11 22:50:08 -0700274 return remaining_count_ == -1 && catch_all_ == false;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700275 }
276
277 private:
278 CatchHandlerItem handler_;
279 const byte *current_data_; // the current handlder in dex file.
280 int32_t remaining_count_; // number of handler not read.
281 bool catch_all_; // is there a handler that will catch all exceptions in case
282 // that all typed handler does not match.
283 };
284
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700285 // Partially decoded form of class_data_item.
286 struct ClassDataHeader {
287 uint32_t static_fields_size_; // the number of static fields
288 uint32_t instance_fields_size_; // the number of instance fields
289 uint32_t direct_methods_size_; // the number of direct methods
290 uint32_t virtual_methods_size_; // the number of virtual methods
291 };
292
293 // Decoded form of encoded_field.
294 struct Field {
295 uint32_t field_idx_; // index into the field_ids list for the identity of this field
296 uint32_t access_flags_; // access flags for the field
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700297 Field() {};
298 private:
299 DISALLOW_COPY_AND_ASSIGN(Field);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700300 };
301
302 // Decoded form of encoded_method.
303 struct Method {
304 uint32_t method_idx_;
305 uint32_t access_flags_;
306 uint32_t code_off_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700307 Method() {};
308 private:
309 DISALLOW_COPY_AND_ASSIGN(Method);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700310 };
311
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700312 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
313 typedef std::vector<const DexFile*> ClassPath;
314
315 // Search a collection of DexFiles for a descriptor
316 static ClassPathEntry FindInClassPath(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700317 const ClassPath& class_path);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700318
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700319 // Opens .dex file, guessing the format based on file extension
320 static const DexFile* Open(const std::string& filename);
321
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700322 // Opens a .dex file from the file system.
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700323 static const DexFile* OpenFile(const std::string& filename);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700324
325 // Opens a .jar, .zip, or .apk file from the file system.
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700326 static const DexFile* OpenZip(const std::string& filename);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700327
Brian Carlstroma663ea52011-08-19 23:33:41 -0700328 // Opens a .dex file from a new allocated pointer. location is used
329 // to identify the source, for example "/system/framework/core.jar"
330 // or "contrived-test-42". When initializing a ClassLinker from an
331 // image, the location is used to match DexCaches the image to their
332 // corresponding DexFiles.N
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700333 static const DexFile* OpenPtr(byte* ptr, size_t length, const std::string& location);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700334
335 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700336 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700337
Brian Carlstroma663ea52011-08-19 23:33:41 -0700338 const std::string& GetLocation() const {
339 return location_;
340 }
341
342 const Header& GetHeader() const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700343 CHECK(header_ != NULL);
344 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700345 }
346
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700347 // Looks up a class definition by its class descriptor.
348 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
349
350 // Returns the number of string identifiers in the .dex file.
351 size_t NumStringIds() const {
352 CHECK(header_ != NULL);
353 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700354 }
355
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700356 // Returns the number of type identifiers in the .dex file.
357 size_t NumTypeIds() const {
358 CHECK(header_ != NULL);
359 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700360 }
361
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700362 // Returns the number of prototype identifiers in the .dex file.
363 size_t NumProtoIds() const {
364 CHECK(header_ != NULL);
365 return header_->proto_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700366 }
367
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700368 // Returns the number of field identifiers in the .dex file.
369 size_t NumFieldIds() const {
370 CHECK(header_ != NULL);
371 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700372 }
373
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700374 // Returns the number of method identifiers in the .dex file.
375 size_t NumMethodIds() const {
376 CHECK(header_ != NULL);
377 return header_->method_ids_size_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700378 }
379
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700380 // Returns the number of class definitions in the .dex file.
381 size_t NumClassDefs() const {
382 CHECK(header_ != NULL);
383 return header_->class_defs_size_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700384 }
385
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700386 // Returns a pointer to the memory mapped class data.
387 // TODO: return a stream
388 const byte* GetClassData(const ClassDef& class_def) const {
389 if (class_def.class_data_off_ == 0) {
390 return NULL;
391 } else {
392 return base_ + class_def.class_data_off_;
393 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700394 }
395
Brian Carlstromf615a612011-07-23 12:50:34 -0700396 // Decodes the header section from the class data bytes.
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700397 ClassDataHeader ReadClassDataHeader(const byte** class_data) const {
398 CHECK(class_data != NULL);
399 ClassDataHeader header;
400 memset(&header, 0, sizeof(ClassDataHeader));
401 if (*class_data != NULL) {
402 header.static_fields_size_ = DecodeUnsignedLeb128(class_data);
403 header.instance_fields_size_ = DecodeUnsignedLeb128(class_data);
404 header.direct_methods_size_ = DecodeUnsignedLeb128(class_data);
405 header.virtual_methods_size_ = DecodeUnsignedLeb128(class_data);
406 }
407 return header;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700408 }
409
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700410 // Returns the class descriptor string of a class definition.
411 const char* GetClassDescriptor(const ClassDef& class_def) const {
412 return dexStringByTypeIdx(class_def.class_idx_);
413 }
414
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700415 // Returns the type descriptor string of a type id.
416 const char* GetTypeDescriptor(const TypeId& type_id) const {
417 return dexStringById(type_id.descriptor_idx_);
418 }
419
Brian Carlstromb9edb842011-08-28 16:31:06 -0700420 // Returns the class descriptor string of a field id.
421 const char* GetFieldClassDescriptor(const FieldId& field_id) const {
422 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
423 return GetTypeDescriptor(type_id);
424 }
425
426 // Returns the name of a field id.
427 const char* GetFieldName(const FieldId& field_id) const {
428 return dexStringById(field_id.name_idx_);
429 }
430
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700431 // Returns the class descriptor string of a method id.
432 const char* GetMethodClassDescriptor(const MethodId& method_id) const {
433 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
434 return GetTypeDescriptor(type_id);
435 }
436
437 // Returns the name of a method id.
438 const char* GetMethodName(const MethodId& method_id) const {
439 return dexStringById(method_id.name_idx_);
440 }
441
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700442 // Returns the StringId at the specified index.
443 const StringId& GetStringId(uint32_t idx) const {
444 CHECK_LT(idx, NumStringIds());
445 return string_ids_[idx];
446 }
447
448 // Returns the TypeId at the specified index.
449 const TypeId& GetTypeId(uint32_t idx) const {
450 CHECK_LT(idx, NumTypeIds());
451 return type_ids_[idx];
452 }
453
454 // Returns the FieldId at the specified index.
455 const FieldId& GetFieldId(uint32_t idx) const {
456 CHECK_LT(idx, NumFieldIds());
457 return field_ids_[idx];
458 }
459
460 // Returns the MethodId at the specified index.
461 const MethodId& GetMethodId(uint32_t idx) const {
462 CHECK_LT(idx, NumMethodIds());
463 return method_ids_[idx];
464 }
465
466 // Returns the ProtoId at the specified index.
467 const ProtoId& GetProtoId(uint32_t idx) const {
468 CHECK_LT(idx, NumProtoIds());
469 return proto_ids_[idx];
470 }
471
472 // Returns the ClassDef at the specified index.
473 const ClassDef& GetClassDef(uint32_t idx) const {
474 CHECK_LT(idx, NumClassDefs());
475 return class_defs_[idx];
476 }
477
478 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
479 if (class_def.interfaces_off_ == 0) {
480 return NULL;
481 } else {
482 const byte* addr = base_ + class_def.interfaces_off_;
483 return reinterpret_cast<const TypeList*>(addr);
484 }
485 }
486
487 const CodeItem* GetCodeItem(const Method& method) const {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700488 return GetCodeItem(method.code_off_);
489 }
490
491 const CodeItem* GetCodeItem(const uint32_t code_off_) const {
492 if (code_off_ == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700493 return NULL; // native or abstract method
494 } else {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700495 const byte* addr = base_ + code_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700496 return reinterpret_cast<const CodeItem*>(addr);
497 }
498 }
499
500 // Returns the short form method descriptor for the given prototype.
501 const char* GetShorty(uint32_t proto_idx) const {
502 const ProtoId& proto_id = GetProtoId(proto_idx);
503 return dexStringById(proto_id.shorty_idx_);
504 }
505
506 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
507 if (proto_id.parameters_off_ == 0) {
508 return NULL;
509 } else {
510 const byte* addr = base_ + proto_id.parameters_off_;
511 return reinterpret_cast<const TypeList*>(addr);
512 }
513 }
514
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700515 std::string CreateMethodDescriptor(uint32_t proto_idx, int32_t* unicode_length) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700516
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700517 const byte* GetEncodedArray(const ClassDef& class_def) const {
518 if (class_def.static_values_off_ == 0) {
519 return 0;
520 } else {
521 return base_ + class_def.static_values_off_;
522 }
523 }
524
525 int32_t GetStringLength(const StringId& string_id) const {
526 const byte* ptr = base_ + string_id.string_data_off_;
527 return DecodeUnsignedLeb128(&ptr);
528 }
529
530 ValueType ReadEncodedValue(const byte** encoded_value, JValue* value) const;
531
532 // From libdex...
533
534 // Returns a pointer to the UTF-8 string data referred to by the
535 // given string_id.
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700536 const char* GetStringData(const StringId& string_id, int32_t* length) const {
537 CHECK(length != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700538 const byte* ptr = base_ + string_id.string_data_off_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700539 *length = DecodeUnsignedLeb128(&ptr);
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700540 return reinterpret_cast<const char*>(ptr);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700541 }
542
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700543 const char* GetStringData(const StringId& string_id) const {
544 int32_t length;
545 return GetStringData(string_id, &length);
546 }
547
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700548 // return the UTF-8 encoded string with the specified string_id index
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700549 const char* dexStringById(uint32_t idx, int32_t* unicode_length) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700550 if (idx == kDexNoIndex) {
551 *unicode_length = 0;
552 return NULL;
553 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700554 const StringId& string_id = GetStringId(idx);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700555 return GetStringData(string_id, unicode_length);
556 }
557
558 const char* dexStringById(uint32_t idx) const {
559 int32_t unicode_length;
560 return dexStringById(idx, &unicode_length);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700561 }
562
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700563 String* dexArtStringById(int32_t idx) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700564
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700565 // Get the descriptor string associated with a given type index.
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700566 const char* dexStringByTypeIdx(uint32_t idx, int32_t* unicode_length) const {
567 const TypeId& type_id = GetTypeId(idx);
568 return dexStringById(type_id.descriptor_idx_, unicode_length);
569 }
570
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700571 const char* dexStringByTypeIdx(uint32_t idx) const {
572 const TypeId& type_id = GetTypeId(idx);
573 return dexStringById(type_id.descriptor_idx_);
574 }
575
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700576 String* dexArtStringByTypeIdx(int32_t idx) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700577 const TypeId& type_id = GetTypeId(idx);
578 return dexArtStringById(type_id.descriptor_idx_);
579 }
580
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700581 // TODO: encoded_field is actually a stream of bytes
582 void dexReadClassDataField(const byte** encoded_field,
Brian Carlstromf615a612011-07-23 12:50:34 -0700583 DexFile::Field* field,
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700584 uint32_t* last_idx) const {
585 uint32_t idx = *last_idx + DecodeUnsignedLeb128(encoded_field);
586 field->access_flags_ = DecodeUnsignedLeb128(encoded_field);
587 field->field_idx_ = idx;
588 *last_idx = idx;
589 }
590
591 // TODO: encoded_method is actually a stream of bytes
592 void dexReadClassDataMethod(const byte** encoded_method,
Brian Carlstromf615a612011-07-23 12:50:34 -0700593 DexFile::Method* method,
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700594 uint32_t* last_idx) const {
595 uint32_t idx = *last_idx + DecodeUnsignedLeb128(encoded_method);
596 method->access_flags_ = DecodeUnsignedLeb128(encoded_method);
597 method->code_off_ = DecodeUnsignedLeb128(encoded_method);
598 method->method_idx_ = idx;
599 *last_idx = idx;
600 }
601
jeffhaoba5ebb92011-08-25 17:24:37 -0700602 static const TryItem* dexGetTryItems(const CodeItem& code_item, uint32_t offset) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700603 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_];
604 return reinterpret_cast<const TryItem*>
605 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
606 }
607
608 // Get the base of the encoded data for the given DexCode.
jeffhaoba5ebb92011-08-25 17:24:37 -0700609 static const byte* dexGetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700610 const byte* handler_data = reinterpret_cast<const byte*>
611 (dexGetTryItems(code_item, code_item.tries_size_));
612 return handler_data + offset;
613 }
614
615 // Find the handler associated with a given address, if any.
616 // Initializes the given iterator and returns true if a match is
617 // found. Returns end if there is no applicable handler.
jeffhaoba5ebb92011-08-25 17:24:37 -0700618 static CatchHandlerIterator dexFindCatchHandler(const CodeItem& code_item, uint32_t address) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700619 CatchHandlerItem handler;
620 handler.address_ = -1;
621 int32_t offset = -1;
622
623 // Short-circuit the overwhelmingly common cases.
624 switch (code_item.tries_size_) {
625 case 0:
626 break;
627 case 1: {
628 const TryItem* tries = dexGetTryItems(code_item, 0);
629 uint32_t start = tries->start_addr_;
630 if (address < start)
631 break;
632
633 uint32_t end = start + tries->insn_count_;
634 if (address >= end)
635 break;
636
637 offset = tries->handler_off_;
638 break;
639 }
640 default:
641 offset = dexFindCatchHandlerOffset0(code_item, code_item.tries_size_, address);
642 }
643
644 if (offset >= 0) {
645 const byte* handler_data = dexGetCatchHandlerData(code_item, offset);
646 return CatchHandlerIterator(handler_data);
647 }
648 return CatchHandlerIterator();
649 }
650
jeffhaoba5ebb92011-08-25 17:24:37 -0700651 static int32_t dexFindCatchHandlerOffset0(const CodeItem &code_item,
652 int32_t tries_size,
653 uint32_t address) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700654 // Note: Signed type is important for max and min.
655 int32_t min = 0;
656 int32_t max = tries_size - 1;
657
658 while (max >= min) {
659 int32_t guess = (min + max) >> 1;
660 const TryItem* pTry = dexGetTryItems(code_item, guess);
661 uint32_t start = pTry->start_addr_;
662
663 if (address < start) {
664 max = guess - 1;
665 continue;
666 }
667
668 uint32_t end = start + pTry->insn_count_;
669 if (address >= end) {
670 min = guess + 1;
671 continue;
672 }
673
674 // We have a winner!
675 return (int32_t) pTry->handler_off_;
676 }
677
678 // No match.
679 return -1;
680 }
681
Shih-wei Liao195487c2011-08-20 13:29:04 -0700682 // Get the pointer to the start of the debugging data
683 const byte* dexGetDebugInfoStream(const CodeItem* code_item) const {
684 if (code_item->debug_info_off_ == 0) {
685 return NULL;
686 } else {
687 return base_ + code_item->debug_info_off_;
688 }
689 }
690
691 // Callback for "new position table entry".
692 // Returning true causes the decoder to stop early.
693 typedef bool (*DexDebugNewPositionCb)(void *cnxt, uint32_t address, uint32_t line_num);
694
695 // Callback for "new locals table entry". "signature" is an empty string
696 // if no signature is available for an entry.
697 typedef void (*DexDebugNewLocalCb)(void *cnxt, uint16_t reg,
698 uint32_t startAddress,
699 uint32_t endAddress,
700 const String* name,
701 const String* descriptor,
702 const String* signature);
703
704 static bool LineNumForPcCb(void *cnxt, uint32_t address, uint32_t line_num) {
705 LineNumFromPcContext *context = (LineNumFromPcContext *)cnxt;
706
707 // We know that this callback will be called in
708 // ascending address order, so keep going until we find
709 // a match or we've just gone past it.
710 if (address > context->address_) {
711 // The line number from the previous positions callback
712 // wil be the final result.
713 return true;
714 } else {
715 context->line_num_ = line_num;
716 return address == context->address_;
717 }
718 }
719
720
721 // Debug info opcodes and constants
722 enum {
723 DBG_END_SEQUENCE = 0x00,
724 DBG_ADVANCE_PC = 0x01,
725 DBG_ADVANCE_LINE = 0x02,
726 DBG_START_LOCAL = 0x03,
727 DBG_START_LOCAL_EXTENDED = 0x04,
728 DBG_END_LOCAL = 0x05,
729 DBG_RESTART_LOCAL = 0x06,
730 DBG_SET_PROLOGUE_END = 0x07,
731 DBG_SET_EPILOGUE_BEGIN = 0x08,
732 DBG_SET_FILE = 0x09,
733 DBG_FIRST_SPECIAL = 0x0a,
734 DBG_LINE_BASE = -4,
735 DBG_LINE_RANGE = 15,
736 };
737
738 struct LocalInfo {
739 LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0), is_live_(false) {}
740
741 // E.g., list
742 const String* name_;
743
744 // E.g., Ljava/util/LinkedList;
745 const String* descriptor_;
746
747 // E.g., java.util.LinkedList<java.lang.Integer>
748 const String* signature_;
749
750 // PC location where the local is first defined.
751 uint16_t start_address_;
752
753 // Is the local defined and live.
754 bool is_live_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700755
756 private:
757 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700758 };
759
760 struct LineNumFromPcContext {
761 LineNumFromPcContext(uint32_t address, uint32_t line_num) :
762 address_(address), line_num_(line_num) {}
763 uint32_t address_;
764 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700765 private:
766 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700767 };
768
769 void InvokeLocalCbIfLive(void *cnxt, int reg, uint32_t end_address,
770 LocalInfo *local_in_reg, DexDebugNewLocalCb local_cb) const {
771 if (local_cb != NULL && local_in_reg[reg].is_live_) {
772 local_cb(cnxt, reg, local_in_reg[reg].start_address_, end_address,
773 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
774 local_in_reg[reg].signature_);
775 }
776 }
777
778 // Determine the source file line number based on the program counter.
779 // "pc" is an offset, in 16-bit units, from the start of the method's code.
780 //
781 // Returns -1 if no match was found (possibly because the source files were
782 // compiled without "-g", so no line number information is present).
783 // Returns -2 for native methods (as expected in exception traces).
784 //
785 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
786 int32_t GetLineNumFromPC(const art::Method* method, uint32_t rel_pc) const;
787
788 void dexDecodeDebugInfo0(const CodeItem* code_item, const art::Method* method,
789 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
790 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const;
791
792 void dexDecodeDebugInfo(const CodeItem* code_item, const art::Method *method,
793 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
794 void* cnxt) const {
795 const byte* stream = dexGetDebugInfoStream(code_item);
796 LocalInfo local_in_reg[code_item->registers_size_];
797
798 if (stream != NULL) {
799 dexDecodeDebugInfo0(code_item, method, posCb, local_cb, cnxt, stream, local_in_reg);
800 }
801 for (int reg = 0; reg < code_item->registers_size_; reg++) {
802 InvokeLocalCbIfLive(cnxt, reg, code_item->insns_size_, local_in_reg, local_cb);
803 }
804 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700805
806 // TODO: const reference
807 uint32_t dexGetIndexForClassDef(const ClassDef* class_def) const {
808 CHECK_GE(class_def, class_defs_);
809 CHECK_LT(class_def, class_defs_ + header_->class_defs_size_);
810 return class_def - class_defs_;
811 }
812
813 const char* dexGetSourceFile(const ClassDef& class_def) const {
814 if (class_def.source_file_idx_ == 0xffffffff) {
815 return NULL;
816 } else {
817 return dexStringById(class_def.source_file_idx_);
818 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700819 }
820
Carl Shapiro1fb86202011-06-27 17:43:13 -0700821 private:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700822 // Helper class to deallocate underlying storage.
823 class Closer {
824 public:
825 virtual ~Closer();
826 };
827
828 // Helper class to deallocate mmap-backed .dex files.
829 class MmapCloser : public Closer {
830 public:
831 MmapCloser(void* addr, size_t length);
832 virtual ~MmapCloser();
833 private:
834 void* addr_;
835 size_t length_;
836 };
837
838 // Helper class for deallocating new/delete-backed .dex files.
839 class PtrCloser : public Closer {
840 public:
841 PtrCloser(byte* addr);
842 virtual ~PtrCloser();
843 private:
844 byte* addr_;
845 };
846
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700847 // Opens a .dex file at the given address.
848 static const DexFile* Open(const byte* dex_file,
849 size_t length,
850 const std::string& location,
851 Closer* closer);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700852
Brian Carlstroma663ea52011-08-19 23:33:41 -0700853 DexFile(const byte* addr, size_t length, const std::string& location, Closer* closer)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700854 : base_(addr),
855 length_(length),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700856 location_(location),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700857 closer_(closer),
858 header_(0),
859 string_ids_(0),
860 type_ids_(0),
861 field_ids_(0),
862 method_ids_(0),
863 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700864 class_defs_(0) {
865 CHECK(addr != NULL);
866 CHECK_GT(length, 0U);
867 CHECK(closer != NULL);
868 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700869
870 // Top-level initializer that calls other Init methods.
871 bool Init();
872
873 // Caches pointers into to the various file sections.
874 void InitMembers();
875
876 // Builds the index of descriptors to class definitions.
877 void InitIndex();
878
879 // Returns true if the byte string equals the magic value.
880 bool CheckMagic(const byte* magic);
881
882 // Returns true if the header magic is of the expected value.
883 bool IsMagicValid();
884
885 // The index of descriptors to class definitions.
Brian Carlstromf615a612011-07-23 12:50:34 -0700886 typedef std::map<const StringPiece, const DexFile::ClassDef*> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700887 Index index_;
888
889 // The base address of the memory mapping.
890 const byte* base_;
891
892 // The size of the underlying memory allocation in bytes.
893 size_t length_;
894
Brian Carlstroma663ea52011-08-19 23:33:41 -0700895 // Typically the dex file name when availble, alternatively some identifying string.
896 //
897 // The ClassLinker will use this to match DexFiles the boot class
898 // path to DexCache::GetLocation when loading from an image.
899 const std::string location_;
900
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700901 // Helper object to free the underlying allocation.
Elliott Hughes90a33692011-08-30 13:27:07 -0700902 UniquePtr<Closer> closer_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700903
904 // Points to the header section.
905 const Header* header_;
906
907 // Points to the base of the string identifier list.
908 const StringId* string_ids_;
909
910 // Points to the base of the type identifier list.
911 const TypeId* type_ids_;
912
913 // Points to the base of the field identifier list.
914 const FieldId* field_ids_;
915
916 // Points to the base of the method identifier list.
917 const MethodId* method_ids_;
918
919 // Points to the base of the prototype identifier list.
920 const ProtoId* proto_ids_;
921
922 // Points to the base of the class definition list.
923 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700924};
925
926} // namespace art
927
928#endif // ART_SRC_DEX_FILE_H_