blob: 1356c8c7018d6c3445bceb5590edcabcb3cf3cba [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 "logging.h"
Brian Carlstrom33f741e2011-10-03 11:24:05 -070014#include "mem_map.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040015#include "mutex.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070016#include "stringpiece.h"
Shih-wei Liao2fb97532011-08-11 16:17:23 -070017#include "utils.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070018
19namespace art {
20
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070021// TODO: move all of the macro functionality into the DexCache class.
Brian Carlstromf615a612011-07-23 12:50:34 -070022class DexFile {
Carl Shapiro1fb86202011-06-27 17:43:13 -070023 public:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070024 static const byte kDexMagic[];
25 static const byte kDexMagicVersion[];
26 static const size_t kSha1DigestSize = 20;
Carl Shapiro80d4dde2011-06-28 16:24:07 -070027
Brian Carlstromb7bbba42011-10-13 14:58:47 -070028 // name of the DexFile entry within a zip archive
29 static const char* kClassesDex;
30
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070031 // The value of an invalid index.
32 static const uint32_t kDexNoIndex = 0xFFFFFFFF;
33
Ian Rogers0571d352011-11-03 19:51:38 -070034 // The value of an invalid index.
35 static const uint16_t kDexNoIndex16 = 0xFFFF;
Carl Shapiro1fb86202011-06-27 17:43:13 -070036
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070037 // Raw header_item.
38 struct Header {
39 uint8_t magic_[8];
40 uint32_t checksum_;
41 uint8_t signature_[kSha1DigestSize];
42 uint32_t file_size_; // length of entire file
43 uint32_t header_size_; // offset to start of next section
44 uint32_t endian_tag_;
Ian Rogers0571d352011-11-03 19:51:38 -070045 uint32_t link_size_; // unused
46 uint32_t link_off_; // unused
47 uint32_t map_off_; // unused
48 uint32_t string_ids_size_; // number of StringIds
49 uint32_t string_ids_off_; // file offset of StringIds array
50 uint32_t type_ids_size_; // number of TypeIds, we don't support more than 65535
51 uint32_t type_ids_off_; // file offset of TypeIds array
52 uint32_t proto_ids_size_; // number of ProtoIds, we don't support more than 65535
53 uint32_t proto_ids_off_; // file offset of ProtoIds array
54 uint32_t field_ids_size_; // number of FieldIds
55 uint32_t field_ids_off_; // file offset of FieldIds array
56 uint32_t method_ids_size_; // number of MethodIds
57 uint32_t method_ids_off_; // file offset of MethodIds array
58 uint32_t class_defs_size_; // number of ClassDefs
59 uint32_t class_defs_off_; // file offset of ClassDef array
60 uint32_t data_size_; // unused
61 uint32_t data_off_; // unused
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070062 private:
63 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070064 };
Carl Shapiro1fb86202011-06-27 17:43:13 -070065
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070066 // Raw string_id_item.
67 struct StringId {
68 uint32_t string_data_off_; // offset in bytes from the base address
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070069 private:
70 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070071 };
72
73 // Raw type_id_item.
74 struct TypeId {
75 uint32_t descriptor_idx_; // index into string_ids
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070076 private:
77 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070078 };
79
80 // Raw field_id_item.
81 struct FieldId {
Ian Rogers0571d352011-11-03 19:51:38 -070082 uint16_t class_idx_; // index into type_ids_ array for defining class
83 uint16_t type_idx_; // index into type_ids_ array for field type
84 uint32_t name_idx_; // index into string_ids_ array for field name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070085 private:
86 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070087 };
88
89 // Raw method_id_item.
90 struct MethodId {
Ian Rogers0571d352011-11-03 19:51:38 -070091 uint16_t class_idx_; // index into type_ids_ array for defining class
92 uint16_t proto_idx_; // index into proto_ids_ array for method prototype
93 uint32_t name_idx_; // index into string_ids_ array for method name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070094 private:
95 DISALLOW_COPY_AND_ASSIGN(MethodId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070096 };
97
98 // Raw proto_id_item.
99 struct ProtoId {
Ian Rogers0571d352011-11-03 19:51:38 -0700100 uint32_t shorty_idx_; // index into string_ids array for shorty descriptor
101 uint16_t return_type_idx_; // index into type_ids array for return type
102 uint16_t pad_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700103 uint32_t parameters_off_; // file offset to type_list for parameter types
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700104 private:
105 DISALLOW_COPY_AND_ASSIGN(ProtoId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700106 };
107
108 // Raw class_def_item.
109 struct ClassDef {
Ian Rogers0571d352011-11-03 19:51:38 -0700110 uint16_t class_idx_; // index into type_ids_ array for this class
111 uint16_t pad1_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700112 uint32_t access_flags_;
Ian Rogers0571d352011-11-03 19:51:38 -0700113 uint16_t superclass_idx_; // index into type_ids_ array for superclass
114 uint16_t pad2_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700115 uint32_t interfaces_off_; // file offset to TypeList
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700116 uint32_t source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700117 uint32_t annotations_off_; // file offset to annotations_directory_item
118 uint32_t class_data_off_; // file offset to class_data_item
119 uint32_t static_values_off_; // file offset to EncodedArray
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700120 private:
121 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700122 };
123
124 // Raw type_item.
125 struct TypeItem {
126 uint16_t type_idx_; // index into type_ids section
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700127 private:
128 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700129 };
130
131 // Raw type_list.
132 class TypeList {
133 public:
134 uint32_t Size() const {
135 return size_;
136 }
137
138 const TypeItem& GetTypeItem(uint32_t idx) const {
139 CHECK_LT(idx, this->size_);
140 return this->list_[idx];
141 }
142
143 private:
144 uint32_t size_; // size of the list, in entries
145 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700146 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700147 };
148
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700149 // Raw code_item.
150 struct CodeItem {
151 uint16_t registers_size_;
152 uint16_t ins_size_;
153 uint16_t outs_size_;
154 uint16_t tries_size_;
155 uint32_t debug_info_off_; // file offset to debug info stream
Ian Rogersd81871c2011-10-03 13:57:23 -0700156 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700157 uint16_t insns_[1];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700158 private:
159 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700160 };
161
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700162 // Raw try_item.
163 struct TryItem {
164 uint32_t start_addr_;
165 uint16_t insn_count_;
166 uint16_t handler_off_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700167 private:
168 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700169 };
170
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700171 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
172 typedef std::vector<const DexFile*> ClassPath;
173
174 // Search a collection of DexFiles for a descriptor
175 static ClassPathEntry FindInClassPath(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700176 const ClassPath& class_path);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700177
Brian Carlstrom78128a62011-09-15 17:21:19 -0700178 // Opens a collection of .dex files
Brian Carlstromae826982011-11-09 01:33:42 -0800179 static void OpenDexFiles(const std::vector<const char*>& dex_filenames,
Brian Carlstrom78128a62011-09-15 17:21:19 -0700180 std::vector<const DexFile*>& dex_files,
181 const std::string& strip_location_prefix);
182
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700183 // Opens .dex file, guessing the container format based on file extension
Brian Carlstrom16192862011-09-12 17:50:06 -0700184 static const DexFile* Open(const std::string& filename,
185 const std::string& strip_location_prefix);
jeffhao262bf462011-10-20 18:36:32 -0700186
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700187 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700188 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700189
Brian Carlstroma663ea52011-08-19 23:33:41 -0700190 const std::string& GetLocation() const {
191 return location_;
192 }
193
Jesse Wilson6bf19152011-09-29 13:12:33 -0400194 // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file.
195 // Used by managed code to implement annotations.
196 jobject GetDexObject(JNIEnv* env) const;
197
Brian Carlstroma663ea52011-08-19 23:33:41 -0700198 const Header& GetHeader() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800199 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700200 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700201 }
202
Ian Rogers0571d352011-11-03 19:51:38 -0700203 // Decode the dex magic version
Ian Rogersd81871c2011-10-03 13:57:23 -0700204 uint32_t GetVersion() const;
205
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700206 // Returns the number of string identifiers in the .dex file.
207 size_t NumStringIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800208 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700209 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700210 }
211
Ian Rogers0571d352011-11-03 19:51:38 -0700212 // Returns the StringId at the specified index.
213 const StringId& GetStringId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800214 CHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700215 return string_ids_[idx];
216 }
217
218 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800219 CHECK_GE(&string_id, string_ids_) << GetLocation();
220 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700221 return &string_id - string_ids_;
222 }
223
224 int32_t GetStringLength(const StringId& string_id) const;
225
226 // Returns a pointer to the UTF-8 string data referred to by the given string_id.
227 const char* GetStringDataAndLength(const StringId& string_id, int32_t* length) const;
228
229 const char* GetStringData(const StringId& string_id) const {
230 int32_t length;
231 return GetStringDataAndLength(string_id, &length);
232 }
233
234 // return the UTF-8 encoded string with the specified string_id index
235 const char* StringDataAndLengthByIdx(uint32_t idx, int32_t* unicode_length) const {
236 if (idx == kDexNoIndex) {
237 *unicode_length = 0;
238 return NULL;
239 }
240 const StringId& string_id = GetStringId(idx);
241 return GetStringDataAndLength(string_id, unicode_length);
242 }
243
244 const char* StringDataByIdx(uint32_t idx) const {
245 int32_t unicode_length;
246 return StringDataAndLengthByIdx(idx, &unicode_length);
247 }
248
249 // Looks up a string id for a given string
250 const StringId* FindStringId(const std::string& string) const;
251
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700252 // Returns the number of type identifiers in the .dex file.
253 size_t NumTypeIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800254 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700255 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700256 }
257
Ian Rogers0571d352011-11-03 19:51:38 -0700258 // Returns the TypeId at the specified index.
259 const TypeId& GetTypeId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800260 CHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700261 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700262 }
263
Ian Rogers0571d352011-11-03 19:51:38 -0700264 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800265 CHECK_GE(&type_id, type_ids_) << GetLocation();
266 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700267 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800268 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700269 return static_cast<uint16_t>(result);
270 }
271
272 // Get the descriptor string associated with a given type index.
273 const char* StringByTypeIdx(uint32_t idx, int32_t* unicode_length) const {
274 const TypeId& type_id = GetTypeId(idx);
275 return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length);
276 }
277
278 const char* StringByTypeIdx(uint32_t idx) const {
279 const TypeId& type_id = GetTypeId(idx);
280 return StringDataByIdx(type_id.descriptor_idx_);
281 }
282
283 // Returns the type descriptor string of a type id.
284 const char* GetTypeDescriptor(const TypeId& type_id) const {
285 return StringDataByIdx(type_id.descriptor_idx_);
286 }
287
288 // Looks up a type for the given string index
289 const TypeId* FindTypeId(uint32_t string_idx) const;
290
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700291 // Returns the number of field identifiers in the .dex file.
292 size_t NumFieldIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800293 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700294 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700295 }
296
Ian Rogers0571d352011-11-03 19:51:38 -0700297 // Returns the FieldId at the specified index.
298 const FieldId& GetFieldId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800299 CHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700300 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700301 }
302
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800303 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800304 CHECK_GE(&field_id, field_ids_) << GetLocation();
305 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800306 return &field_id - field_ids_;
307 }
308
309 // Looks up a field by its declaring class, name and type
310 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
311 const DexFile::StringId& name,
312 const DexFile::TypeId& type) const;
313
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700314 // Returns the declaring class descriptor string of a field id.
315 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700316 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
317 return GetTypeDescriptor(type_id);
318 }
319
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700320 // Returns the class descriptor string of a field id.
321 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
322 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
323 return GetTypeDescriptor(type_id);
324 }
325
Brian Carlstromb9edb842011-08-28 16:31:06 -0700326 // Returns the name of a field id.
327 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700328 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700329 }
330
Ian Rogers0571d352011-11-03 19:51:38 -0700331 // Returns the number of method identifiers in the .dex file.
332 size_t NumMethodIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800333 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700334 return header_->method_ids_size_;
335 }
336
337 // Returns the MethodId at the specified index.
338 const MethodId& GetMethodId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800339 CHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700340 return method_ids_[idx];
341 }
342
343 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800344 CHECK_GE(&method_id, method_ids_) << GetLocation();
345 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700346 return &method_id - method_ids_;
347 }
348
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800349 // Looks up a method by its declaring class, name and proto_id
350 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
351 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700352 const DexFile::ProtoId& signature) const;
353
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700354 // Returns the declaring class descriptor string of a method id.
355 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700356 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
357 return GetTypeDescriptor(type_id);
358 }
359
jeffhao98eacac2011-09-14 16:11:53 -0700360 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700361 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
362 return GetProtoId(method_id.proto_idx_);
363 }
364
365 // Returns the signature of a method id.
366 const std::string GetMethodSignature(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700367 return CreateMethodSignature(method_id.proto_idx_, NULL);
jeffhao98eacac2011-09-14 16:11:53 -0700368 }
369
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700370 // Returns the name of a method id.
371 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700372 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700373 }
374
Ian Rogers0571d352011-11-03 19:51:38 -0700375 // Returns the shorty of a method id.
376 const char* GetMethodShorty(const MethodId& method_id) const {
377 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700378 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800379 const char* GetMethodShorty(const MethodId& method_id, int32_t* length) const {
380 return StringDataAndLengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
381 }
Ian Rogers0571d352011-11-03 19:51:38 -0700382 // Returns the number of class definitions in the .dex file.
383 size_t NumClassDefs() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800384 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700385 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700386 }
387
388 // Returns the ClassDef at the specified index.
389 const ClassDef& GetClassDef(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800390 CHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700391 return class_defs_[idx];
392 }
393
Ian Rogers0571d352011-11-03 19:51:38 -0700394 uint32_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800395 CHECK_GE(&class_def, class_defs_) << GetLocation();
396 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700397 return &class_def - class_defs_;
398 }
399
400 // Returns the class descriptor string of a class definition.
401 const char* GetClassDescriptor(const ClassDef& class_def) const {
402 return StringByTypeIdx(class_def.class_idx_);
403 }
404
405 // Looks up a class definition by its class descriptor.
406 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
407
408 // Looks up a class definition index by its class descriptor.
409 bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const;
410
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700411 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
412 if (class_def.interfaces_off_ == 0) {
413 return NULL;
414 } else {
415 const byte* addr = base_ + class_def.interfaces_off_;
416 return reinterpret_cast<const TypeList*>(addr);
417 }
418 }
419
Ian Rogers0571d352011-11-03 19:51:38 -0700420 // Returns a pointer to the raw memory mapped class_data_item
421 const byte* GetClassData(const ClassDef& class_def) const {
422 if (class_def.class_data_off_ == 0) {
423 return NULL;
424 } else {
425 return base_ + class_def.class_data_off_;
426 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700427 }
428
Ian Rogers0571d352011-11-03 19:51:38 -0700429 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800430 const CodeItem* GetCodeItem(const uint32_t code_off) const {
431 if (code_off == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700432 return NULL; // native or abstract method
433 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800434 const byte* addr = base_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700435 return reinterpret_cast<const CodeItem*>(addr);
436 }
437 }
438
Ian Rogers0571d352011-11-03 19:51:38 -0700439 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
440 return StringByTypeIdx(proto_id.return_type_idx_);
441 }
442
443 // Returns the number of prototype identifiers in the .dex file.
444 size_t NumProtoIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800445 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700446 return header_->proto_ids_size_;
447 }
448
449 // Returns the ProtoId at the specified index.
450 const ProtoId& GetProtoId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800451 CHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700452 return proto_ids_[idx];
453 }
454
455 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800456 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
457 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700458 return &proto_id - proto_ids_;
459 }
460
461 // Looks up a proto id for a given return type and signature type list
462 const ProtoId* FindProtoId(uint16_t return_type_id,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800463 const std::vector<uint16_t>& signature_type_idxs_) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700464
465 // Given a signature place the type ids into the given vector, returns true on success
466 bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
467 const std::string& signature) const;
468
469 // Given a proto_idx decode the type list and return type into a method signature
470 std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const;
471
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700472 // Returns the short form method descriptor for the given prototype.
473 const char* GetShorty(uint32_t proto_idx) const {
474 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700475 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700476 }
477
478 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
479 if (proto_id.parameters_off_ == 0) {
480 return NULL;
481 } else {
482 const byte* addr = base_ + proto_id.parameters_off_;
483 return reinterpret_cast<const TypeList*>(addr);
484 }
485 }
486
Ian Rogers0571d352011-11-03 19:51:38 -0700487 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700488 if (class_def.static_values_off_ == 0) {
489 return 0;
490 } else {
491 return base_ + class_def.static_values_off_;
492 }
493 }
494
Ian Rogers0571d352011-11-03 19:51:38 -0700495 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700496 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700497 return reinterpret_cast<const TryItem*>
498 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
499 }
500
501 // Get the base of the encoded data for the given DexCode.
Ian Rogers0571d352011-11-03 19:51:38 -0700502 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
503 const byte* handler_data =
504 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700505 return handler_data + offset;
506 }
507
508 // Find the handler associated with a given address, if any.
509 // Initializes the given iterator and returns true if a match is
510 // found. Returns end if there is no applicable handler.
Ian Rogers0571d352011-11-03 19:51:38 -0700511 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
512 uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700513
Shih-wei Liao195487c2011-08-20 13:29:04 -0700514 // Get the pointer to the start of the debugging data
Ian Rogers0571d352011-11-03 19:51:38 -0700515 const byte* GetDebugInfoStream(const CodeItem* code_item) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700516 if (code_item->debug_info_off_ == 0) {
517 return NULL;
518 } else {
519 return base_ + code_item->debug_info_off_;
520 }
521 }
522
523 // Callback for "new position table entry".
524 // Returning true causes the decoder to stop early.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700525 typedef bool (*DexDebugNewPositionCb)(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700526
527 // Callback for "new locals table entry". "signature" is an empty string
528 // if no signature is available for an entry.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700529 typedef void (*DexDebugNewLocalCb)(void* cnxt, uint16_t reg,
Shih-wei Liao195487c2011-08-20 13:29:04 -0700530 uint32_t startAddress,
531 uint32_t endAddress,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700532 const char* name,
533 const char* descriptor,
534 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700535
Ian Rogers0571d352011-11-03 19:51:38 -0700536 static bool LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700537
538 // Debug info opcodes and constants
539 enum {
540 DBG_END_SEQUENCE = 0x00,
541 DBG_ADVANCE_PC = 0x01,
542 DBG_ADVANCE_LINE = 0x02,
543 DBG_START_LOCAL = 0x03,
544 DBG_START_LOCAL_EXTENDED = 0x04,
545 DBG_END_LOCAL = 0x05,
546 DBG_RESTART_LOCAL = 0x06,
547 DBG_SET_PROLOGUE_END = 0x07,
548 DBG_SET_EPILOGUE_BEGIN = 0x08,
549 DBG_SET_FILE = 0x09,
550 DBG_FIRST_SPECIAL = 0x0a,
551 DBG_LINE_BASE = -4,
552 DBG_LINE_RANGE = 15,
553 };
554
555 struct LocalInfo {
Ian Rogers0571d352011-11-03 19:51:38 -0700556 LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0),
557 is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700558
Ian Rogers0571d352011-11-03 19:51:38 -0700559 const char* name_; // E.g., list
560 const char* descriptor_; // E.g., Ljava/util/LinkedList;
561 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
562 uint16_t start_address_; // PC location where the local is first defined.
563 bool is_live_; // Is the local defined and live.
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700564
565 private:
566 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700567 };
568
569 struct LineNumFromPcContext {
570 LineNumFromPcContext(uint32_t address, uint32_t line_num) :
571 address_(address), line_num_(line_num) {}
572 uint32_t address_;
573 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700574 private:
575 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700576 };
577
Brian Carlstrom78128a62011-09-15 17:21:19 -0700578 void InvokeLocalCbIfLive(void* cnxt, int reg, uint32_t end_address,
579 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700580 if (local_cb != NULL && local_in_reg[reg].is_live_) {
581 local_cb(cnxt, reg, local_in_reg[reg].start_address_, end_address,
Elliott Hughesdbb40792011-11-18 17:05:22 -0800582 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
583 local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : "");
Shih-wei Liao195487c2011-08-20 13:29:04 -0700584 }
585 }
586
587 // Determine the source file line number based on the program counter.
588 // "pc" is an offset, in 16-bit units, from the start of the method's code.
589 //
590 // Returns -1 if no match was found (possibly because the source files were
591 // compiled without "-g", so no line number information is present).
592 // Returns -2 for native methods (as expected in exception traces).
593 //
594 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Ian Rogers0571d352011-11-03 19:51:38 -0700595 int32_t GetLineNumFromPC(const Method* method, uint32_t rel_pc) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700596
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800597 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700598 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
599 void* cnxt) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700600
Ian Rogers0571d352011-11-03 19:51:38 -0700601 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700602 if (class_def.source_file_idx_ == 0xffffffff) {
603 return NULL;
604 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700605 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700606 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700607 }
608
jeffhaob4df5142011-09-19 20:25:32 -0700609 void ChangePermissions(int prot) const;
610
Carl Shapiro1fb86202011-06-27 17:43:13 -0700611 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700612
613 // Opens a .dex file
614 static const DexFile* OpenFile(const std::string& filename,
615 const std::string& original_location,
616 const std::string& strip_location_prefix);
617
618 // Opens a dex file from within a .jar, .zip, or .apk file
619 static const DexFile* OpenZip(const std::string& filename,
620 const std::string& strip_location_prefix);
621
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700622 // Opens a .dex file at the given address.
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700623 static const DexFile* OpenMemory(const byte* dex_file,
624 size_t length,
625 const std::string& location,
626 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700627
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700628 DexFile(const byte* addr, size_t length, const std::string& location, MemMap* mem_map)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700629 : base_(addr),
630 length_(length),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700631 location_(location),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700632 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400633 dex_object_lock_("a dex_object_lock_"),
634 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700635 header_(0),
636 string_ids_(0),
637 type_ids_(0),
638 field_ids_(0),
639 method_ids_(0),
640 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700641 class_defs_(0) {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800642 CHECK(addr != NULL) << GetLocation();
643 CHECK_GT(length, 0U) << GetLocation();
644 CHECK(mem_map != NULL) << GetLocation();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700645 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700646
647 // Top-level initializer that calls other Init methods.
648 bool Init();
649
650 // Caches pointers into to the various file sections.
651 void InitMembers();
652
653 // Builds the index of descriptors to class definitions.
654 void InitIndex();
655
656 // Returns true if the byte string equals the magic value.
657 bool CheckMagic(const byte* magic);
658
659 // Returns true if the header magic is of the expected value.
660 bool IsMagicValid();
661
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800662 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes03181a82011-11-17 17:22:21 -0800663 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
664 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const;
665
666
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800667 // The index of descriptors to class definition indexes (as opposed to type id indexes)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700668 typedef std::map<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700669 Index index_;
670
671 // The base address of the memory mapping.
672 const byte* base_;
673
674 // The size of the underlying memory allocation in bytes.
675 size_t length_;
676
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700677 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700678 //
679 // The ClassLinker will use this to match DexFiles the boot class
680 // path to DexCache::GetLocation when loading from an image.
681 const std::string location_;
682
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700683 // Manages the underlying memory allocation.
684 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700685
Jesse Wilson6bf19152011-09-29 13:12:33 -0400686 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
687 mutable Mutex dex_object_lock_;
688 mutable jobject dex_object_;
689
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700690 // Points to the header section.
691 const Header* header_;
692
693 // Points to the base of the string identifier list.
694 const StringId* string_ids_;
695
696 // Points to the base of the type identifier list.
697 const TypeId* type_ids_;
698
699 // Points to the base of the field identifier list.
700 const FieldId* field_ids_;
701
702 // Points to the base of the method identifier list.
703 const MethodId* method_ids_;
704
705 // Points to the base of the prototype identifier list.
706 const ProtoId* proto_ids_;
707
708 // Points to the base of the class definition list.
709 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700710};
711
Ian Rogers0571d352011-11-03 19:51:38 -0700712// Iterate over a dex file's ProtoId's paramters
713class DexFileParameterIterator {
714 public:
715 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
716 : dex_file_(dex_file), size_(0), pos_(0) {
717 type_list_ = dex_file_.GetProtoParameters(proto_id);
718 if (type_list_ != NULL) {
719 size_ = type_list_->Size();
720 }
721 }
722 bool HasNext() const { return pos_ < size_; }
723 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800724 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -0700725 return type_list_->GetTypeItem(pos_).type_idx_;
726 }
727 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800728 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -0700729 }
730 private:
731 const DexFile& dex_file_;
732 const DexFile::TypeList* type_list_;
733 uint32_t size_;
734 uint32_t pos_;
735 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
736};
737
738// Iterate and decode class_data_item
739class ClassDataItemIterator {
740 public:
741 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
742 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
743 ReadClassDataHeader();
744 if (EndOfInstanceFieldsPos() > 0) {
745 ReadClassDataField();
746 } else if (EndOfVirtualMethodsPos() > 0) {
747 ReadClassDataMethod();
748 }
749 }
750 uint32_t NumStaticFields() const {
751 return header_.static_fields_size_;
752 }
753 uint32_t NumInstanceFields() const {
754 return header_.instance_fields_size_;
755 }
756 uint32_t NumDirectMethods() const {
757 return header_.direct_methods_size_;
758 }
759 uint32_t NumVirtualMethods() const {
760 return header_.virtual_methods_size_;
761 }
762 bool HasNextStaticField() const {
763 return pos_ < EndOfStaticFieldsPos();
764 }
765 bool HasNextInstanceField() const {
766 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
767 }
768 bool HasNextDirectMethod() const {
769 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
770 }
771 bool HasNextVirtualMethod() const {
772 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
773 }
774 bool HasNext() const {
775 return pos_ < EndOfVirtualMethodsPos();
776 }
777 void Next() {
778 pos_++;
779 if (pos_ < EndOfStaticFieldsPos()) {
780 last_idx_ = GetMemberIndex();
781 ReadClassDataField();
782 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
783 last_idx_ = 0; // transition to next array, reset last index
784 ReadClassDataField();
785 } else if (pos_ < EndOfInstanceFieldsPos()) {
786 last_idx_ = GetMemberIndex();
787 ReadClassDataField();
788 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
789 last_idx_ = 0; // transition to next array, reset last index
790 ReadClassDataMethod();
791 } else if (pos_ < EndOfDirectMethodsPos()) {
792 last_idx_ = GetMemberIndex();
793 ReadClassDataMethod();
794 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
795 last_idx_ = 0; // transition to next array, reset last index
796 ReadClassDataMethod();
797 } else if (pos_ < EndOfVirtualMethodsPos()) {
798 last_idx_ = GetMemberIndex();
799 ReadClassDataMethod();
800 } else {
801 DCHECK(!HasNext());
802 }
803 }
804 uint32_t GetMemberIndex() const {
805 if (pos_ < EndOfInstanceFieldsPos()) {
806 return last_idx_ + field_.field_idx_delta_;
807 } else {
808 CHECK_LT(pos_, EndOfVirtualMethodsPos());
809 return last_idx_ + method_.method_idx_delta_;
810 }
811 }
812 uint32_t GetMemberAccessFlags() const {
813 if (pos_ < EndOfInstanceFieldsPos()) {
814 return field_.access_flags_;
815 } else {
816 CHECK_LT(pos_, EndOfVirtualMethodsPos());
817 return method_.access_flags_;
818 }
819 }
820 const DexFile::CodeItem* GetMethodCodeItem() const {
821 return dex_file_.GetCodeItem(method_.code_off_);
822 }
823 uint32_t GetMethodCodeItemOffset() const {
824 return method_.code_off_;
825 }
826 private:
827 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
828 // header for a class_data_item
829 struct ClassDataHeader {
830 uint32_t static_fields_size_; // the number of static fields
831 uint32_t instance_fields_size_; // the number of instance fields
832 uint32_t direct_methods_size_; // the number of direct methods
833 uint32_t virtual_methods_size_; // the number of virtual methods
834 } header_;
835
836 // Read and decode header from a class_data_item stream into header
837 void ReadClassDataHeader();
838
839 uint32_t EndOfStaticFieldsPos() const {
840 return header_.static_fields_size_;
841 }
842 uint32_t EndOfInstanceFieldsPos() const {
843 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
844 }
845 uint32_t EndOfDirectMethodsPos() const {
846 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
847 }
848 uint32_t EndOfVirtualMethodsPos() const {
849 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
850 }
851
852 // A decoded version of the field of a class_data_item
853 struct ClassDataField {
854 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
855 uint32_t access_flags_; // access flags for the field
856 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
857 private:
858 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
859 } field_;
860
861 // Read and decode a field from a class_data_item stream into field
862 void ReadClassDataField();
863
864 // A decoded version of the method of a class_data_item
865 struct ClassDataMethod {
866 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
867 uint32_t access_flags_;
868 uint32_t code_off_;
869 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
870 private:
871 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
872 } method_;
873
874 // Read and decode a method from a class_data_item stream into method
875 void ReadClassDataMethod();
876
877 const DexFile& dex_file_;
878 size_t pos_; // integral number of items passed
879 const byte* ptr_pos_; // pointer into stream of class_data_item
880 uint32_t last_idx_; // last read field or method index to apply delta to
881 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
882};
883
884class ClassLinker;
885class DexCache;
886class Field;
887
888class EncodedStaticFieldValueIterator {
889 public:
890 EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache,
891 ClassLinker* linker, const DexFile::ClassDef& class_def);
892
893 void ReadValueToField(Field* field) const;
894
895 bool HasNext() { return pos_ < array_size_; }
896
897 void Next();
898 private:
899 enum ValueType {
900 kByte = 0x00,
901 kShort = 0x02,
902 kChar = 0x03,
903 kInt = 0x04,
904 kLong = 0x06,
905 kFloat = 0x10,
906 kDouble = 0x11,
907 kString = 0x17,
908 kType = 0x18,
909 kField = 0x19,
910 kMethod = 0x1a,
911 kEnum = 0x1b,
912 kArray = 0x1c,
913 kAnnotation = 0x1d,
914 kNull = 0x1e,
915 kBoolean = 0x1f
916 };
917
918 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
919 static const byte kEncodedValueArgShift = 5;
920
921 const DexFile& dex_file_;
922 DexCache* dex_cache_; // dex cache to resolve literal objects
923 ClassLinker* linker_; // linker to resolve literal objects
924 size_t array_size_; // size of array
925 size_t pos_; // current position
926 const byte* ptr_; // pointer into encoded data array
927 byte type_; // type of current encoded value
928 jvalue jval_; // value of current encoded value
929 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
930};
931
932class CatchHandlerIterator {
933 public:
934 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
935 explicit CatchHandlerIterator(const byte* handler_data) {
936 Init(handler_data);
937 }
938
939 uint16_t GetHandlerTypeIndex() const {
940 return handler_.type_idx_;
941 }
942 uint32_t GetHandlerAddress() const {
943 return handler_.address_;
944 }
945 void Next();
946 bool HasNext() const {
947 return remaining_count_ != -1 || catch_all_;
948 }
949 // End of this set of catch blocks, convenience method to locate next set of catch blocks
950 const byte* EndDataPointer() const {
951 CHECK(!HasNext());
952 return current_data_;
953 }
954 private:
955 void Init(const byte* handler_data);
956
957 struct CatchHandlerItem {
958 uint16_t type_idx_; // type index of the caught exception type
959 uint32_t address_; // handler address
960 } handler_;
961 const byte *current_data_; // the current handler in dex file.
962 int32_t remaining_count_; // number of handlers not read.
963 bool catch_all_; // is there a handler that will catch all exceptions in case
964 // that all typed handler does not match.
965};
966
Carl Shapiro1fb86202011-06-27 17:43:13 -0700967} // namespace art
968
969#endif // ART_SRC_DEX_FILE_H_