blob: 3077ae6cf627fb082b4a921e55f0a08d5500e6d2 [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"
Jesse Wilson6bf19152011-09-29 13:12:33 -040012#include "jni.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070013#include "leb128.h"
14#include "logging.h"
Brian Carlstrom33f741e2011-10-03 11:24:05 -070015#include "mem_map.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040016#include "mutex.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070017#include "stringpiece.h"
18#include "strutil.h"
Shih-wei Liao2fb97532011-08-11 16:17:23 -070019#include "utils.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070020
21namespace art {
22
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070023union JValue;
Shih-wei Liao195487c2011-08-20 13:29:04 -070024class String;
25class Method;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070026
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070027// TODO: move all of the macro functionality into the DexCache class.
Brian Carlstromf615a612011-07-23 12:50:34 -070028class DexFile {
Carl Shapiro1fb86202011-06-27 17:43:13 -070029 public:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070030 static const byte kDexMagic[];
31 static const byte kDexMagicVersion[];
32 static const size_t kSha1DigestSize = 20;
Carl Shapiro80d4dde2011-06-28 16:24:07 -070033
Brian Carlstromb7bbba42011-10-13 14:58:47 -070034 // name of the DexFile entry within a zip archive
35 static const char* kClassesDex;
36
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070037 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
38 static const byte kEncodedValueArgShift = 5;
39
40 // The value of an invalid index.
41 static const uint32_t kDexNoIndex = 0xFFFFFFFF;
42
43 enum ValueType {
44 kByte = 0x00,
45 kShort = 0x02,
46 kChar = 0x03,
47 kInt = 0x04,
48 kLong = 0x06,
49 kFloat = 0x10,
50 kDouble = 0x11,
51 kString = 0x17,
52 kType = 0x18,
53 kField = 0x19,
54 kMethod = 0x1a,
55 kEnum = 0x1b,
56 kArray = 0x1c,
57 kAnnotation = 0x1d,
58 kNull = 0x1e,
59 kBoolean = 0x1f
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070060 };
Carl Shapiro1fb86202011-06-27 17:43:13 -070061
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070062 // Raw header_item.
63 struct Header {
64 uint8_t magic_[8];
65 uint32_t checksum_;
66 uint8_t signature_[kSha1DigestSize];
67 uint32_t file_size_; // length of entire file
68 uint32_t header_size_; // offset to start of next section
69 uint32_t endian_tag_;
70 uint32_t link_size_;
71 uint32_t link_off_;
72 uint32_t map_off_;
73 uint32_t string_ids_size_;
74 uint32_t string_ids_off_;
75 uint32_t type_ids_size_;
76 uint32_t type_ids_off_;
77 uint32_t proto_ids_size_;
78 uint32_t proto_ids_off_;
79 uint32_t field_ids_size_;
80 uint32_t field_ids_off_;
81 uint32_t method_ids_size_;
82 uint32_t method_ids_off_;
83 uint32_t class_defs_size_;
84 uint32_t class_defs_off_;
85 uint32_t data_size_;
86 uint32_t data_off_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070087 private:
88 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070089 };
Carl Shapiro1fb86202011-06-27 17:43:13 -070090
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070091 // Raw string_id_item.
92 struct StringId {
93 uint32_t string_data_off_; // offset in bytes from the base address
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070094 private:
95 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070096 };
97
98 // Raw type_id_item.
99 struct TypeId {
100 uint32_t descriptor_idx_; // index into string_ids
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700101 private:
102 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700103 };
104
105 // Raw field_id_item.
106 struct FieldId {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700107 uint16_t class_idx_; // index into type_ids_ list for defining class
108 uint16_t type_idx_; // index into type_ids_ for field type
109 uint32_t name_idx_; // index into string_ids_ for field name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700110 private:
111 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700112 };
113
114 // Raw method_id_item.
115 struct MethodId {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700116 uint16_t class_idx_; // index into type_ids_ list for defining class
117 uint16_t proto_idx_; // index into proto_ids_ for method prototype
118 uint32_t name_idx_; // index into string_ids_ for method name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700119 private:
120 DISALLOW_COPY_AND_ASSIGN(MethodId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700121 };
122
123 // Raw proto_id_item.
124 struct ProtoId {
125 uint32_t shorty_idx_; // index into string_ids for shorty descriptor
126 uint32_t return_type_idx_; // index into type_ids list for return type
127 uint32_t parameters_off_; // file offset to type_list for parameter types
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700128 private:
129 DISALLOW_COPY_AND_ASSIGN(ProtoId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700130 };
131
132 // Raw class_def_item.
133 struct ClassDef {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700134 uint32_t class_idx_; // index into type_ids_ for this class
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700135 uint32_t access_flags_;
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700136 uint32_t superclass_idx_; // index into type_ids_ for superclass
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700137 uint32_t interfaces_off_; // file offset to TypeList
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700138 uint32_t source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700139 uint32_t annotations_off_; // file offset to annotations_directory_item
140 uint32_t class_data_off_; // file offset to class_data_item
141 uint32_t static_values_off_; // file offset to EncodedArray
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700142 private:
143 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700144 };
145
146 // Raw type_item.
147 struct TypeItem {
148 uint16_t type_idx_; // index into type_ids section
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700149 private:
150 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700151 };
152
153 // Raw type_list.
154 class TypeList {
155 public:
156 uint32_t Size() const {
157 return size_;
158 }
159
160 const TypeItem& GetTypeItem(uint32_t idx) const {
161 CHECK_LT(idx, this->size_);
162 return this->list_[idx];
163 }
164
165 private:
166 uint32_t size_; // size of the list, in entries
167 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700168 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700169 };
170
171 class ParameterIterator { // TODO: stream
172 public:
Brian Carlstromf615a612011-07-23 12:50:34 -0700173 ParameterIterator(const DexFile& dex_file, const ProtoId& proto_id)
174 : dex_file_(dex_file), size_(0), pos_(0) {
175 type_list_ = dex_file_.GetProtoParameters(proto_id);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700176 if (type_list_ != NULL) {
177 size_ = type_list_->Size();
178 }
179 }
180 bool HasNext() const { return pos_ != size_; }
181 void Next() { ++pos_; }
182 const char* GetDescriptor() {
183 uint32_t type_idx = type_list_->GetTypeItem(pos_).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700184 return dex_file_.dexStringByTypeIdx(type_idx);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700185 }
186 private:
Brian Carlstromf615a612011-07-23 12:50:34 -0700187 const DexFile& dex_file_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700188 const TypeList* type_list_;
189 uint32_t size_;
190 uint32_t pos_;
191 DISALLOW_IMPLICIT_CONSTRUCTORS(ParameterIterator);
192 };
193
194 ParameterIterator* GetParameterIterator(const ProtoId& proto_id) const {
195 return new ParameterIterator(*this, proto_id);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700196 }
197
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700198 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
199 return dexStringByTypeIdx(proto_id.return_type_idx_);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700200 }
201
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700202 // Raw code_item.
203 struct CodeItem {
204 uint16_t registers_size_;
205 uint16_t ins_size_;
206 uint16_t outs_size_;
207 uint16_t tries_size_;
208 uint32_t debug_info_off_; // file offset to debug info stream
209 uint32_t insns_size_; // size of the insns array, in 2 byte code units
210 uint16_t insns_[1];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700211 private:
212 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700213 };
214
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700215 struct CatchHandlerItem {
216 uint32_t type_idx_; // type index of the caught exception type
217 uint32_t address_; // handler address
218 };
219
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700220 // Raw try_item.
221 struct TryItem {
222 uint32_t start_addr_;
223 uint16_t insn_count_;
224 uint16_t handler_off_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700225 private:
226 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700227 };
228
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700229 class CatchHandlerIterator {
230 public:
231 CatchHandlerIterator() {
232 remaining_count_ = -1;
233 catch_all_ = false;
234 }
235
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700236 explicit CatchHandlerIterator(const byte* handler_data) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700237 current_data_ = handler_data;
jeffhaoba5ebb92011-08-25 17:24:37 -0700238 remaining_count_ = DecodeSignedLeb128(&current_data_);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700239
240 // If remaining_count_ is non-positive, then it is the negative of
241 // the number of catch types, and the catches are followed by a
242 // catch-all handler.
243 if (remaining_count_ <= 0) {
244 catch_all_ = true;
245 remaining_count_ = -remaining_count_;
246 } else {
247 catch_all_ = false;
248 }
249 Next();
250 }
251
Shih-wei Liaofe909f22011-08-12 19:20:26 -0700252 const CatchHandlerItem& Get() const {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700253 return handler_;
254 }
255
jeffhaoba5ebb92011-08-25 17:24:37 -0700256 const byte* GetData() const {
257 return current_data_;
258 }
259
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700260 void Next() {
261 if (remaining_count_ > 0) {
262 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
263 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
264 remaining_count_--;
265 return;
266 }
267
268 if (catch_all_) {
269 handler_.type_idx_ = kDexNoIndex;
270 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
271 catch_all_ = false;
272 return;
273 }
274
275 // no more handler
276 remaining_count_ = -1;
277 }
278
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700279 bool HasNext() const {
Shih-wei Liao4e5c0b92011-08-11 22:50:08 -0700280 return remaining_count_ == -1 && catch_all_ == false;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700281 }
282
283 private:
284 CatchHandlerItem handler_;
Ian Rogersbdb03912011-09-14 00:55:44 -0700285 const byte *current_data_; // the current handler in dex file.
286 int32_t remaining_count_; // number of handlers not read.
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700287 bool catch_all_; // is there a handler that will catch all exceptions in case
288 // that all typed handler does not match.
289 };
290
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700291 // Partially decoded form of class_data_item.
292 struct ClassDataHeader {
293 uint32_t static_fields_size_; // the number of static fields
294 uint32_t instance_fields_size_; // the number of instance fields
295 uint32_t direct_methods_size_; // the number of direct methods
296 uint32_t virtual_methods_size_; // the number of virtual methods
297 };
298
299 // Decoded form of encoded_field.
300 struct Field {
301 uint32_t field_idx_; // index into the field_ids list for the identity of this field
302 uint32_t access_flags_; // access flags for the field
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700303 Field() {}
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700304 private:
305 DISALLOW_COPY_AND_ASSIGN(Field);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700306 };
307
308 // Decoded form of encoded_method.
309 struct Method {
310 uint32_t method_idx_;
311 uint32_t access_flags_;
312 uint32_t code_off_;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700313 Method() {}
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700314 private:
315 DISALLOW_COPY_AND_ASSIGN(Method);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700316 };
317
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700318 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
319 typedef std::vector<const DexFile*> ClassPath;
320
321 // Search a collection of DexFiles for a descriptor
322 static ClassPathEntry FindInClassPath(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700323 const ClassPath& class_path);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700324
Brian Carlstrom78128a62011-09-15 17:21:19 -0700325 // Opens a collection of .dex files
326 static void OpenDexFiles(std::vector<const char*>& dex_filenames,
327 std::vector<const DexFile*>& dex_files,
328 const std::string& strip_location_prefix);
329
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700330 // Opens .dex file, guessing the container format based on file extension
Brian Carlstrom16192862011-09-12 17:50:06 -0700331 static const DexFile* Open(const std::string& filename,
332 const std::string& strip_location_prefix);
jeffhao262bf462011-10-20 18:36:32 -0700333
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700334 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700335 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700336
Brian Carlstroma663ea52011-08-19 23:33:41 -0700337 const std::string& GetLocation() const {
338 return location_;
339 }
340
Jesse Wilson6bf19152011-09-29 13:12:33 -0400341 // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file.
342 // Used by managed code to implement annotations.
343 jobject GetDexObject(JNIEnv* env) const;
344
Brian Carlstroma663ea52011-08-19 23:33:41 -0700345 const Header& GetHeader() const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700346 CHECK(header_ != NULL);
347 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700348 }
349
Brian Carlstrome24fa612011-09-29 00:53:55 -0700350 // Looks up a class definition index by its class descriptor.
351 bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const;
352
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700353 // Looks up a class definition by its class descriptor.
354 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
355
356 // Returns the number of string identifiers in the .dex file.
357 size_t NumStringIds() const {
358 CHECK(header_ != NULL);
359 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700360 }
361
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700362 // Returns the number of type identifiers in the .dex file.
363 size_t NumTypeIds() const {
364 CHECK(header_ != NULL);
365 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700366 }
367
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700368 // Returns the number of prototype identifiers in the .dex file.
369 size_t NumProtoIds() const {
370 CHECK(header_ != NULL);
371 return header_->proto_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700372 }
373
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700374 // Returns the number of field identifiers in the .dex file.
375 size_t NumFieldIds() const {
376 CHECK(header_ != NULL);
377 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700378 }
379
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700380 // Returns the number of method identifiers in the .dex file.
381 size_t NumMethodIds() const {
382 CHECK(header_ != NULL);
383 return header_->method_ids_size_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700384 }
385
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700386 // Returns the number of class definitions in the .dex file.
387 size_t NumClassDefs() const {
388 CHECK(header_ != NULL);
389 return header_->class_defs_size_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700390 }
391
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700392 // Returns a pointer to the memory mapped class data.
393 // TODO: return a stream
394 const byte* GetClassData(const ClassDef& class_def) const {
395 if (class_def.class_data_off_ == 0) {
396 return NULL;
397 } else {
398 return base_ + class_def.class_data_off_;
399 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700400 }
401
Brian Carlstromf615a612011-07-23 12:50:34 -0700402 // Decodes the header section from the class data bytes.
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700403 ClassDataHeader ReadClassDataHeader(const byte** class_data) const {
404 CHECK(class_data != NULL);
405 ClassDataHeader header;
406 memset(&header, 0, sizeof(ClassDataHeader));
407 if (*class_data != NULL) {
408 header.static_fields_size_ = DecodeUnsignedLeb128(class_data);
409 header.instance_fields_size_ = DecodeUnsignedLeb128(class_data);
410 header.direct_methods_size_ = DecodeUnsignedLeb128(class_data);
411 header.virtual_methods_size_ = DecodeUnsignedLeb128(class_data);
412 }
413 return header;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700414 }
415
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700416 // Returns the class descriptor string of a class definition.
417 const char* GetClassDescriptor(const ClassDef& class_def) const {
418 return dexStringByTypeIdx(class_def.class_idx_);
419 }
420
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700421 // Returns the type descriptor string of a type id.
422 const char* GetTypeDescriptor(const TypeId& type_id) const {
423 return dexStringById(type_id.descriptor_idx_);
424 }
425
Brian Carlstromb9edb842011-08-28 16:31:06 -0700426 // Returns the class descriptor string of a field id.
427 const char* GetFieldClassDescriptor(const FieldId& field_id) const {
428 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
429 return GetTypeDescriptor(type_id);
430 }
431
432 // Returns the name of a field id.
433 const char* GetFieldName(const FieldId& field_id) const {
434 return dexStringById(field_id.name_idx_);
435 }
436
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700437 // Returns the class descriptor string of a method id.
438 const char* GetMethodClassDescriptor(const MethodId& method_id) const {
439 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
440 return GetTypeDescriptor(type_id);
441 }
442
jeffhao98eacac2011-09-14 16:11:53 -0700443 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700444 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
445 return GetProtoId(method_id.proto_idx_);
446 }
447
448 // Returns the signature of a method id.
449 const std::string GetMethodSignature(const MethodId& method_id) const {
450 return CreateMethodDescriptor(method_id.proto_idx_, NULL);
jeffhao98eacac2011-09-14 16:11:53 -0700451 }
452
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700453 // Returns the name of a method id.
454 const char* GetMethodName(const MethodId& method_id) const {
455 return dexStringById(method_id.name_idx_);
456 }
457
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700458 // Returns the StringId at the specified index.
459 const StringId& GetStringId(uint32_t idx) const {
460 CHECK_LT(idx, NumStringIds());
461 return string_ids_[idx];
462 }
463
464 // Returns the TypeId at the specified index.
465 const TypeId& GetTypeId(uint32_t idx) const {
466 CHECK_LT(idx, NumTypeIds());
467 return type_ids_[idx];
468 }
469
470 // Returns the FieldId at the specified index.
471 const FieldId& GetFieldId(uint32_t idx) const {
472 CHECK_LT(idx, NumFieldIds());
473 return field_ids_[idx];
474 }
475
476 // Returns the MethodId at the specified index.
477 const MethodId& GetMethodId(uint32_t idx) const {
478 CHECK_LT(idx, NumMethodIds());
479 return method_ids_[idx];
480 }
481
482 // Returns the ProtoId at the specified index.
483 const ProtoId& GetProtoId(uint32_t idx) const {
484 CHECK_LT(idx, NumProtoIds());
485 return proto_ids_[idx];
486 }
487
488 // Returns the ClassDef at the specified index.
489 const ClassDef& GetClassDef(uint32_t idx) const {
490 CHECK_LT(idx, NumClassDefs());
491 return class_defs_[idx];
492 }
493
494 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
495 if (class_def.interfaces_off_ == 0) {
496 return NULL;
497 } else {
498 const byte* addr = base_ + class_def.interfaces_off_;
499 return reinterpret_cast<const TypeList*>(addr);
500 }
501 }
502
503 const CodeItem* GetCodeItem(const Method& method) const {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700504 return GetCodeItem(method.code_off_);
505 }
506
507 const CodeItem* GetCodeItem(const uint32_t code_off_) const {
508 if (code_off_ == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700509 return NULL; // native or abstract method
510 } else {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700511 const byte* addr = base_ + code_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700512 return reinterpret_cast<const CodeItem*>(addr);
513 }
514 }
515
516 // Returns the short form method descriptor for the given prototype.
517 const char* GetShorty(uint32_t proto_idx) const {
518 const ProtoId& proto_id = GetProtoId(proto_idx);
519 return dexStringById(proto_id.shorty_idx_);
520 }
521
522 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
523 if (proto_id.parameters_off_ == 0) {
524 return NULL;
525 } else {
526 const byte* addr = base_ + proto_id.parameters_off_;
527 return reinterpret_cast<const TypeList*>(addr);
528 }
529 }
530
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700531 std::string CreateMethodDescriptor(uint32_t proto_idx, int32_t* unicode_length) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700532
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700533 const byte* GetEncodedArray(const ClassDef& class_def) const {
534 if (class_def.static_values_off_ == 0) {
535 return 0;
536 } else {
537 return base_ + class_def.static_values_off_;
538 }
539 }
540
541 int32_t GetStringLength(const StringId& string_id) const {
542 const byte* ptr = base_ + string_id.string_data_off_;
543 return DecodeUnsignedLeb128(&ptr);
544 }
545
546 ValueType ReadEncodedValue(const byte** encoded_value, JValue* value) const;
547
548 // From libdex...
549
550 // Returns a pointer to the UTF-8 string data referred to by the
551 // given string_id.
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700552 const char* GetStringData(const StringId& string_id, int32_t* length) const {
553 CHECK(length != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700554 const byte* ptr = base_ + string_id.string_data_off_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700555 *length = DecodeUnsignedLeb128(&ptr);
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700556 return reinterpret_cast<const char*>(ptr);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700557 }
558
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700559 const char* GetStringData(const StringId& string_id) const {
560 int32_t length;
561 return GetStringData(string_id, &length);
562 }
563
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700564 // return the UTF-8 encoded string with the specified string_id index
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700565 const char* dexStringById(uint32_t idx, int32_t* unicode_length) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700566 if (idx == kDexNoIndex) {
567 *unicode_length = 0;
568 return NULL;
569 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700570 const StringId& string_id = GetStringId(idx);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700571 return GetStringData(string_id, unicode_length);
572 }
573
574 const char* dexStringById(uint32_t idx) const {
575 int32_t unicode_length;
576 return dexStringById(idx, &unicode_length);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700577 }
578
579 // Get the descriptor string associated with a given type index.
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700580 const char* dexStringByTypeIdx(uint32_t idx, int32_t* unicode_length) const {
581 const TypeId& type_id = GetTypeId(idx);
582 return dexStringById(type_id.descriptor_idx_, unicode_length);
583 }
584
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700585 const char* dexStringByTypeIdx(uint32_t idx) const {
586 const TypeId& type_id = GetTypeId(idx);
587 return dexStringById(type_id.descriptor_idx_);
588 }
589
590 // TODO: encoded_field is actually a stream of bytes
591 void dexReadClassDataField(const byte** encoded_field,
Brian Carlstromf615a612011-07-23 12:50:34 -0700592 DexFile::Field* field,
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700593 uint32_t* last_idx) const {
594 uint32_t idx = *last_idx + DecodeUnsignedLeb128(encoded_field);
595 field->access_flags_ = DecodeUnsignedLeb128(encoded_field);
596 field->field_idx_ = idx;
597 *last_idx = idx;
598 }
599
600 // TODO: encoded_method is actually a stream of bytes
601 void dexReadClassDataMethod(const byte** encoded_method,
Brian Carlstromf615a612011-07-23 12:50:34 -0700602 DexFile::Method* method,
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700603 uint32_t* last_idx) const {
604 uint32_t idx = *last_idx + DecodeUnsignedLeb128(encoded_method);
605 method->access_flags_ = DecodeUnsignedLeb128(encoded_method);
606 method->code_off_ = DecodeUnsignedLeb128(encoded_method);
607 method->method_idx_ = idx;
608 *last_idx = idx;
609 }
610
jeffhaoba5ebb92011-08-25 17:24:37 -0700611 static const TryItem* dexGetTryItems(const CodeItem& code_item, uint32_t offset) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700612 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_];
613 return reinterpret_cast<const TryItem*>
614 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
615 }
616
617 // Get the base of the encoded data for the given DexCode.
jeffhaoba5ebb92011-08-25 17:24:37 -0700618 static const byte* dexGetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700619 const byte* handler_data = reinterpret_cast<const byte*>
620 (dexGetTryItems(code_item, code_item.tries_size_));
621 return handler_data + offset;
622 }
623
624 // Find the handler associated with a given address, if any.
625 // Initializes the given iterator and returns true if a match is
626 // found. Returns end if there is no applicable handler.
jeffhaoba5ebb92011-08-25 17:24:37 -0700627 static CatchHandlerIterator dexFindCatchHandler(const CodeItem& code_item, uint32_t address) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700628 CatchHandlerItem handler;
629 handler.address_ = -1;
630 int32_t offset = -1;
631
632 // Short-circuit the overwhelmingly common cases.
633 switch (code_item.tries_size_) {
634 case 0:
635 break;
636 case 1: {
637 const TryItem* tries = dexGetTryItems(code_item, 0);
638 uint32_t start = tries->start_addr_;
639 if (address < start)
640 break;
641
642 uint32_t end = start + tries->insn_count_;
643 if (address >= end)
644 break;
645
646 offset = tries->handler_off_;
647 break;
648 }
649 default:
650 offset = dexFindCatchHandlerOffset0(code_item, code_item.tries_size_, address);
651 }
652
653 if (offset >= 0) {
654 const byte* handler_data = dexGetCatchHandlerData(code_item, offset);
655 return CatchHandlerIterator(handler_data);
656 }
657 return CatchHandlerIterator();
658 }
659
jeffhaoba5ebb92011-08-25 17:24:37 -0700660 static int32_t dexFindCatchHandlerOffset0(const CodeItem &code_item,
661 int32_t tries_size,
662 uint32_t address) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700663 // Note: Signed type is important for max and min.
664 int32_t min = 0;
665 int32_t max = tries_size - 1;
666
667 while (max >= min) {
668 int32_t guess = (min + max) >> 1;
669 const TryItem* pTry = dexGetTryItems(code_item, guess);
670 uint32_t start = pTry->start_addr_;
671
672 if (address < start) {
673 max = guess - 1;
674 continue;
675 }
676
677 uint32_t end = start + pTry->insn_count_;
678 if (address >= end) {
679 min = guess + 1;
680 continue;
681 }
682
683 // We have a winner!
684 return (int32_t) pTry->handler_off_;
685 }
686
687 // No match.
688 return -1;
689 }
690
Shih-wei Liao195487c2011-08-20 13:29:04 -0700691 // Get the pointer to the start of the debugging data
692 const byte* dexGetDebugInfoStream(const CodeItem* code_item) const {
693 if (code_item->debug_info_off_ == 0) {
694 return NULL;
695 } else {
696 return base_ + code_item->debug_info_off_;
697 }
698 }
699
700 // Callback for "new position table entry".
701 // Returning true causes the decoder to stop early.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700702 typedef bool (*DexDebugNewPositionCb)(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700703
704 // Callback for "new locals table entry". "signature" is an empty string
705 // if no signature is available for an entry.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700706 typedef void (*DexDebugNewLocalCb)(void* cnxt, uint16_t reg,
Shih-wei Liao195487c2011-08-20 13:29:04 -0700707 uint32_t startAddress,
708 uint32_t endAddress,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700709 const char* name,
710 const char* descriptor,
711 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700712
Brian Carlstrom78128a62011-09-15 17:21:19 -0700713 static bool LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num) {
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700714 LineNumFromPcContext* context = (LineNumFromPcContext*) cnxt;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700715
716 // We know that this callback will be called in
717 // ascending address order, so keep going until we find
718 // a match or we've just gone past it.
719 if (address > context->address_) {
720 // The line number from the previous positions callback
721 // wil be the final result.
722 return true;
723 } else {
724 context->line_num_ = line_num;
725 return address == context->address_;
726 }
727 }
728
729
730 // Debug info opcodes and constants
731 enum {
732 DBG_END_SEQUENCE = 0x00,
733 DBG_ADVANCE_PC = 0x01,
734 DBG_ADVANCE_LINE = 0x02,
735 DBG_START_LOCAL = 0x03,
736 DBG_START_LOCAL_EXTENDED = 0x04,
737 DBG_END_LOCAL = 0x05,
738 DBG_RESTART_LOCAL = 0x06,
739 DBG_SET_PROLOGUE_END = 0x07,
740 DBG_SET_EPILOGUE_BEGIN = 0x08,
741 DBG_SET_FILE = 0x09,
742 DBG_FIRST_SPECIAL = 0x0a,
743 DBG_LINE_BASE = -4,
744 DBG_LINE_RANGE = 15,
745 };
746
747 struct LocalInfo {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700748 LocalInfo() : name_(NULL), start_address_(0), is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700749
750 // E.g., list
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700751 const char* name_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700752
753 // E.g., Ljava/util/LinkedList;
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700754 const char* descriptor_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700755
756 // E.g., java.util.LinkedList<java.lang.Integer>
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700757 const char* signature_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700758
759 // PC location where the local is first defined.
760 uint16_t start_address_;
761
762 // Is the local defined and live.
763 bool is_live_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700764
765 private:
766 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700767 };
768
769 struct LineNumFromPcContext {
770 LineNumFromPcContext(uint32_t address, uint32_t line_num) :
771 address_(address), line_num_(line_num) {}
772 uint32_t address_;
773 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700774 private:
775 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700776 };
777
Brian Carlstrom78128a62011-09-15 17:21:19 -0700778 void InvokeLocalCbIfLive(void* cnxt, int reg, uint32_t end_address,
779 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700780 if (local_cb != NULL && local_in_reg[reg].is_live_) {
781 local_cb(cnxt, reg, local_in_reg[reg].start_address_, end_address,
782 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
783 local_in_reg[reg].signature_);
784 }
785 }
786
787 // Determine the source file line number based on the program counter.
788 // "pc" is an offset, in 16-bit units, from the start of the method's code.
789 //
790 // Returns -1 if no match was found (possibly because the source files were
791 // compiled without "-g", so no line number information is present).
792 // Returns -2 for native methods (as expected in exception traces).
793 //
794 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
795 int32_t GetLineNumFromPC(const art::Method* method, uint32_t rel_pc) const;
796
797 void dexDecodeDebugInfo0(const CodeItem* code_item, const art::Method* method,
798 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
799 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const;
800
Elliott Hughes30646832011-10-13 16:59:46 -0700801 void dexDecodeDebugInfo(const CodeItem* code_item, const art::Method* method,
Shih-wei Liao195487c2011-08-20 13:29:04 -0700802 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
803 void* cnxt) const {
804 const byte* stream = dexGetDebugInfoStream(code_item);
805 LocalInfo local_in_reg[code_item->registers_size_];
806
807 if (stream != NULL) {
808 dexDecodeDebugInfo0(code_item, method, posCb, local_cb, cnxt, stream, local_in_reg);
809 }
810 for (int reg = 0; reg < code_item->registers_size_; reg++) {
811 InvokeLocalCbIfLive(cnxt, reg, code_item->insns_size_, local_in_reg, local_cb);
812 }
813 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700814
815 // TODO: const reference
816 uint32_t dexGetIndexForClassDef(const ClassDef* class_def) const {
817 CHECK_GE(class_def, class_defs_);
818 CHECK_LT(class_def, class_defs_ + header_->class_defs_size_);
819 return class_def - class_defs_;
820 }
821
822 const char* dexGetSourceFile(const ClassDef& class_def) const {
823 if (class_def.source_file_idx_ == 0xffffffff) {
824 return NULL;
825 } else {
826 return dexStringById(class_def.source_file_idx_);
827 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700828 }
829
jeffhaob4df5142011-09-19 20:25:32 -0700830 void ChangePermissions(int prot) const;
831
Carl Shapiro1fb86202011-06-27 17:43:13 -0700832 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700833
834 // Opens a .dex file
835 static const DexFile* OpenFile(const std::string& filename,
836 const std::string& original_location,
837 const std::string& strip_location_prefix);
838
839 // Opens a dex file from within a .jar, .zip, or .apk file
840 static const DexFile* OpenZip(const std::string& filename,
841 const std::string& strip_location_prefix);
842
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700843 // Opens a .dex file at the given address.
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700844 static const DexFile* OpenMemory(const byte* dex_file,
845 size_t length,
846 const std::string& location,
847 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700848
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700849 DexFile(const byte* addr, size_t length, const std::string& location, MemMap* mem_map)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700850 : base_(addr),
851 length_(length),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700852 location_(location),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700853 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400854 dex_object_lock_("a dex_object_lock_"),
855 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700856 header_(0),
857 string_ids_(0),
858 type_ids_(0),
859 field_ids_(0),
860 method_ids_(0),
861 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700862 class_defs_(0) {
863 CHECK(addr != NULL);
864 CHECK_GT(length, 0U);
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700865 CHECK(mem_map != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700866 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700867
868 // Top-level initializer that calls other Init methods.
869 bool Init();
870
871 // Caches pointers into to the various file sections.
872 void InitMembers();
873
874 // Builds the index of descriptors to class definitions.
875 void InitIndex();
876
877 // Returns true if the byte string equals the magic value.
878 bool CheckMagic(const byte* magic);
879
880 // Returns true if the header magic is of the expected value.
881 bool IsMagicValid();
882
Brian Carlstrome24fa612011-09-29 00:53:55 -0700883 // The index of descriptors to class definition indexes.
884 typedef std::map<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700885 Index index_;
886
887 // The base address of the memory mapping.
888 const byte* base_;
889
890 // The size of the underlying memory allocation in bytes.
891 size_t length_;
892
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700893 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700894 //
895 // The ClassLinker will use this to match DexFiles the boot class
896 // path to DexCache::GetLocation when loading from an image.
897 const std::string location_;
898
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700899 // Manages the underlying memory allocation.
900 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700901
Jesse Wilson6bf19152011-09-29 13:12:33 -0400902 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
903 mutable Mutex dex_object_lock_;
904 mutable jobject dex_object_;
905
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700906 // Points to the header section.
907 const Header* header_;
908
909 // Points to the base of the string identifier list.
910 const StringId* string_ids_;
911
912 // Points to the base of the type identifier list.
913 const TypeId* type_ids_;
914
915 // Points to the base of the field identifier list.
916 const FieldId* field_ids_;
917
918 // Points to the base of the method identifier list.
919 const MethodId* method_ids_;
920
921 // Points to the base of the prototype identifier list.
922 const ProtoId* proto_ids_;
923
924 // Points to the base of the class definition list.
925 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700926};
927
928} // namespace art
929
930#endif // ART_SRC_DEX_FILE_H_