blob: a0363b088b123a00e69bd0014c1c4878060af984 [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 Carlstrom89521892011-12-07 22:05:07 -0800187 // Opens .dex file, backed by existing memory
188 static const DexFile* Open(const uint8_t* base, size_t length, const std::string& location) {
189 return OpenMemory(base, length, location, NULL);
190 }
191
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700192 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700193 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700194
Brian Carlstroma663ea52011-08-19 23:33:41 -0700195 const std::string& GetLocation() const {
196 return location_;
197 }
198
Jesse Wilson6bf19152011-09-29 13:12:33 -0400199 // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file.
200 // Used by managed code to implement annotations.
201 jobject GetDexObject(JNIEnv* env) const;
202
Brian Carlstroma663ea52011-08-19 23:33:41 -0700203 const Header& GetHeader() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800204 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700205 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700206 }
207
Ian Rogers0571d352011-11-03 19:51:38 -0700208 // Decode the dex magic version
Ian Rogersd81871c2011-10-03 13:57:23 -0700209 uint32_t GetVersion() const;
210
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700211 // Returns the number of string identifiers in the .dex file.
212 size_t NumStringIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800213 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700214 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700215 }
216
Ian Rogers0571d352011-11-03 19:51:38 -0700217 // Returns the StringId at the specified index.
218 const StringId& GetStringId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800219 CHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700220 return string_ids_[idx];
221 }
222
223 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800224 CHECK_GE(&string_id, string_ids_) << GetLocation();
225 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700226 return &string_id - string_ids_;
227 }
228
229 int32_t GetStringLength(const StringId& string_id) const;
230
231 // Returns a pointer to the UTF-8 string data referred to by the given string_id.
232 const char* GetStringDataAndLength(const StringId& string_id, int32_t* length) const;
233
234 const char* GetStringData(const StringId& string_id) const {
235 int32_t length;
236 return GetStringDataAndLength(string_id, &length);
237 }
238
239 // return the UTF-8 encoded string with the specified string_id index
240 const char* StringDataAndLengthByIdx(uint32_t idx, int32_t* unicode_length) const {
241 if (idx == kDexNoIndex) {
242 *unicode_length = 0;
243 return NULL;
244 }
245 const StringId& string_id = GetStringId(idx);
246 return GetStringDataAndLength(string_id, unicode_length);
247 }
248
249 const char* StringDataByIdx(uint32_t idx) const {
250 int32_t unicode_length;
251 return StringDataAndLengthByIdx(idx, &unicode_length);
252 }
253
254 // Looks up a string id for a given string
255 const StringId* FindStringId(const std::string& string) const;
256
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700257 // Returns the number of type identifiers in the .dex file.
258 size_t NumTypeIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800259 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700260 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700261 }
262
Ian Rogers0571d352011-11-03 19:51:38 -0700263 // Returns the TypeId at the specified index.
264 const TypeId& GetTypeId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800265 CHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700266 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700267 }
268
Ian Rogers0571d352011-11-03 19:51:38 -0700269 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800270 CHECK_GE(&type_id, type_ids_) << GetLocation();
271 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700272 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800273 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700274 return static_cast<uint16_t>(result);
275 }
276
277 // Get the descriptor string associated with a given type index.
278 const char* StringByTypeIdx(uint32_t idx, int32_t* unicode_length) const {
279 const TypeId& type_id = GetTypeId(idx);
280 return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length);
281 }
282
283 const char* StringByTypeIdx(uint32_t idx) const {
284 const TypeId& type_id = GetTypeId(idx);
285 return StringDataByIdx(type_id.descriptor_idx_);
286 }
287
288 // Returns the type descriptor string of a type id.
289 const char* GetTypeDescriptor(const TypeId& type_id) const {
290 return StringDataByIdx(type_id.descriptor_idx_);
291 }
292
293 // Looks up a type for the given string index
294 const TypeId* FindTypeId(uint32_t string_idx) const;
295
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700296 // Returns the number of field identifiers in the .dex file.
297 size_t NumFieldIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800298 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700299 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700300 }
301
Ian Rogers0571d352011-11-03 19:51:38 -0700302 // Returns the FieldId at the specified index.
303 const FieldId& GetFieldId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800304 CHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700305 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700306 }
307
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800308 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800309 CHECK_GE(&field_id, field_ids_) << GetLocation();
310 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800311 return &field_id - field_ids_;
312 }
313
314 // Looks up a field by its declaring class, name and type
315 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
316 const DexFile::StringId& name,
317 const DexFile::TypeId& type) const;
318
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700319 // Returns the declaring class descriptor string of a field id.
320 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700321 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
322 return GetTypeDescriptor(type_id);
323 }
324
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700325 // Returns the class descriptor string of a field id.
326 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
327 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
328 return GetTypeDescriptor(type_id);
329 }
330
Brian Carlstromb9edb842011-08-28 16:31:06 -0700331 // Returns the name of a field id.
332 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700333 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700334 }
335
Ian Rogers0571d352011-11-03 19:51:38 -0700336 // Returns the number of method identifiers in the .dex file.
337 size_t NumMethodIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800338 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700339 return header_->method_ids_size_;
340 }
341
342 // Returns the MethodId at the specified index.
343 const MethodId& GetMethodId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800344 CHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700345 return method_ids_[idx];
346 }
347
348 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800349 CHECK_GE(&method_id, method_ids_) << GetLocation();
350 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700351 return &method_id - method_ids_;
352 }
353
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800354 // Looks up a method by its declaring class, name and proto_id
355 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
356 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700357 const DexFile::ProtoId& signature) const;
358
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700359 // Returns the declaring class descriptor string of a method id.
360 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700361 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
362 return GetTypeDescriptor(type_id);
363 }
364
jeffhao98eacac2011-09-14 16:11:53 -0700365 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700366 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
367 return GetProtoId(method_id.proto_idx_);
368 }
369
370 // Returns the signature of a method id.
371 const std::string GetMethodSignature(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700372 return CreateMethodSignature(method_id.proto_idx_, NULL);
jeffhao98eacac2011-09-14 16:11:53 -0700373 }
374
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700375 // Returns the name of a method id.
376 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700377 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700378 }
379
Ian Rogers0571d352011-11-03 19:51:38 -0700380 // Returns the shorty of a method id.
381 const char* GetMethodShorty(const MethodId& method_id) const {
382 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700383 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800384 const char* GetMethodShorty(const MethodId& method_id, int32_t* length) const {
385 return StringDataAndLengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
386 }
Ian Rogers0571d352011-11-03 19:51:38 -0700387 // Returns the number of class definitions in the .dex file.
388 size_t NumClassDefs() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800389 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700390 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700391 }
392
393 // Returns the ClassDef at the specified index.
394 const ClassDef& GetClassDef(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800395 CHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700396 return class_defs_[idx];
397 }
398
Ian Rogers0571d352011-11-03 19:51:38 -0700399 uint32_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800400 CHECK_GE(&class_def, class_defs_) << GetLocation();
401 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700402 return &class_def - class_defs_;
403 }
404
405 // Returns the class descriptor string of a class definition.
406 const char* GetClassDescriptor(const ClassDef& class_def) const {
407 return StringByTypeIdx(class_def.class_idx_);
408 }
409
410 // Looks up a class definition by its class descriptor.
411 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
412
413 // Looks up a class definition index by its class descriptor.
414 bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const;
415
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700416 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
417 if (class_def.interfaces_off_ == 0) {
418 return NULL;
419 } else {
420 const byte* addr = base_ + class_def.interfaces_off_;
421 return reinterpret_cast<const TypeList*>(addr);
422 }
423 }
424
Ian Rogers0571d352011-11-03 19:51:38 -0700425 // Returns a pointer to the raw memory mapped class_data_item
426 const byte* GetClassData(const ClassDef& class_def) const {
427 if (class_def.class_data_off_ == 0) {
428 return NULL;
429 } else {
430 return base_ + class_def.class_data_off_;
431 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700432 }
433
Ian Rogers0571d352011-11-03 19:51:38 -0700434 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800435 const CodeItem* GetCodeItem(const uint32_t code_off) const {
436 if (code_off == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700437 return NULL; // native or abstract method
438 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800439 const byte* addr = base_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700440 return reinterpret_cast<const CodeItem*>(addr);
441 }
442 }
443
Ian Rogers0571d352011-11-03 19:51:38 -0700444 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
445 return StringByTypeIdx(proto_id.return_type_idx_);
446 }
447
448 // Returns the number of prototype identifiers in the .dex file.
449 size_t NumProtoIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800450 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700451 return header_->proto_ids_size_;
452 }
453
454 // Returns the ProtoId at the specified index.
455 const ProtoId& GetProtoId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800456 CHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700457 return proto_ids_[idx];
458 }
459
460 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800461 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
462 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700463 return &proto_id - proto_ids_;
464 }
465
466 // Looks up a proto id for a given return type and signature type list
467 const ProtoId* FindProtoId(uint16_t return_type_id,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800468 const std::vector<uint16_t>& signature_type_idxs_) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700469
470 // Given a signature place the type ids into the given vector, returns true on success
471 bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
472 const std::string& signature) const;
473
474 // Given a proto_idx decode the type list and return type into a method signature
475 std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const;
476
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700477 // Returns the short form method descriptor for the given prototype.
478 const char* GetShorty(uint32_t proto_idx) const {
479 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700480 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700481 }
482
483 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
484 if (proto_id.parameters_off_ == 0) {
485 return NULL;
486 } else {
487 const byte* addr = base_ + proto_id.parameters_off_;
488 return reinterpret_cast<const TypeList*>(addr);
489 }
490 }
491
Ian Rogers0571d352011-11-03 19:51:38 -0700492 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700493 if (class_def.static_values_off_ == 0) {
494 return 0;
495 } else {
496 return base_ + class_def.static_values_off_;
497 }
498 }
499
Ian Rogers0571d352011-11-03 19:51:38 -0700500 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700501 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700502 return reinterpret_cast<const TryItem*>
503 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
504 }
505
506 // Get the base of the encoded data for the given DexCode.
Ian Rogers0571d352011-11-03 19:51:38 -0700507 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
508 const byte* handler_data =
509 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700510 return handler_data + offset;
511 }
512
513 // Find the handler associated with a given address, if any.
514 // Initializes the given iterator and returns true if a match is
515 // found. Returns end if there is no applicable handler.
Ian Rogers0571d352011-11-03 19:51:38 -0700516 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
517 uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700518
Shih-wei Liao195487c2011-08-20 13:29:04 -0700519 // Get the pointer to the start of the debugging data
Ian Rogers0571d352011-11-03 19:51:38 -0700520 const byte* GetDebugInfoStream(const CodeItem* code_item) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700521 if (code_item->debug_info_off_ == 0) {
522 return NULL;
523 } else {
524 return base_ + code_item->debug_info_off_;
525 }
526 }
527
528 // Callback for "new position table entry".
529 // Returning true causes the decoder to stop early.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700530 typedef bool (*DexDebugNewPositionCb)(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700531
532 // Callback for "new locals table entry". "signature" is an empty string
533 // if no signature is available for an entry.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700534 typedef void (*DexDebugNewLocalCb)(void* cnxt, uint16_t reg,
Shih-wei Liao195487c2011-08-20 13:29:04 -0700535 uint32_t startAddress,
536 uint32_t endAddress,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700537 const char* name,
538 const char* descriptor,
539 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700540
Ian Rogers0571d352011-11-03 19:51:38 -0700541 static bool LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700542
543 // Debug info opcodes and constants
544 enum {
545 DBG_END_SEQUENCE = 0x00,
546 DBG_ADVANCE_PC = 0x01,
547 DBG_ADVANCE_LINE = 0x02,
548 DBG_START_LOCAL = 0x03,
549 DBG_START_LOCAL_EXTENDED = 0x04,
550 DBG_END_LOCAL = 0x05,
551 DBG_RESTART_LOCAL = 0x06,
552 DBG_SET_PROLOGUE_END = 0x07,
553 DBG_SET_EPILOGUE_BEGIN = 0x08,
554 DBG_SET_FILE = 0x09,
555 DBG_FIRST_SPECIAL = 0x0a,
556 DBG_LINE_BASE = -4,
557 DBG_LINE_RANGE = 15,
558 };
559
560 struct LocalInfo {
Ian Rogers0571d352011-11-03 19:51:38 -0700561 LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0),
562 is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700563
Ian Rogers0571d352011-11-03 19:51:38 -0700564 const char* name_; // E.g., list
565 const char* descriptor_; // E.g., Ljava/util/LinkedList;
566 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
567 uint16_t start_address_; // PC location where the local is first defined.
568 bool is_live_; // Is the local defined and live.
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700569
570 private:
571 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700572 };
573
574 struct LineNumFromPcContext {
575 LineNumFromPcContext(uint32_t address, uint32_t line_num) :
576 address_(address), line_num_(line_num) {}
577 uint32_t address_;
578 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700579 private:
580 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700581 };
582
Brian Carlstrom78128a62011-09-15 17:21:19 -0700583 void InvokeLocalCbIfLive(void* cnxt, int reg, uint32_t end_address,
584 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700585 if (local_cb != NULL && local_in_reg[reg].is_live_) {
586 local_cb(cnxt, reg, local_in_reg[reg].start_address_, end_address,
Elliott Hughesdbb40792011-11-18 17:05:22 -0800587 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
588 local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : "");
Shih-wei Liao195487c2011-08-20 13:29:04 -0700589 }
590 }
591
592 // Determine the source file line number based on the program counter.
593 // "pc" is an offset, in 16-bit units, from the start of the method's code.
594 //
595 // Returns -1 if no match was found (possibly because the source files were
596 // compiled without "-g", so no line number information is present).
597 // Returns -2 for native methods (as expected in exception traces).
598 //
599 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Ian Rogers0571d352011-11-03 19:51:38 -0700600 int32_t GetLineNumFromPC(const Method* method, uint32_t rel_pc) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700601
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800602 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700603 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
604 void* cnxt) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700605
Ian Rogers0571d352011-11-03 19:51:38 -0700606 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700607 if (class_def.source_file_idx_ == 0xffffffff) {
608 return NULL;
609 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700610 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700611 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700612 }
613
jeffhaob4df5142011-09-19 20:25:32 -0700614 void ChangePermissions(int prot) const;
615
Carl Shapiro1fb86202011-06-27 17:43:13 -0700616 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700617
618 // Opens a .dex file
619 static const DexFile* OpenFile(const std::string& filename,
620 const std::string& original_location,
621 const std::string& strip_location_prefix);
622
623 // Opens a dex file from within a .jar, .zip, or .apk file
624 static const DexFile* OpenZip(const std::string& filename,
625 const std::string& strip_location_prefix);
626
Brian Carlstrom89521892011-12-07 22:05:07 -0800627 // Opens a .dex file at the given address backed by a MemMap
628 static const DexFile* OpenMemory(const std::string& location,
629 MemMap* mem_map) {
630 return OpenMemory(mem_map->GetAddress(),
631 mem_map->GetLength(),
632 location,
633 mem_map);
634 }
635
636 // Opens a .dex file at the given address, optionally backed by a MemMap
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700637 static const DexFile* OpenMemory(const byte* dex_file,
638 size_t length,
639 const std::string& location,
640 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700641
Brian Carlstrom89521892011-12-07 22:05:07 -0800642 DexFile(const byte* base, size_t length, const std::string& location, MemMap* mem_map)
643 : base_(base),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700644 length_(length),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700645 location_(location),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700646 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400647 dex_object_lock_("a dex_object_lock_"),
648 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700649 header_(0),
650 string_ids_(0),
651 type_ids_(0),
652 field_ids_(0),
653 method_ids_(0),
654 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700655 class_defs_(0) {
Brian Carlstrom89521892011-12-07 22:05:07 -0800656 CHECK(base_ != NULL) << GetLocation();
657 CHECK_GT(length_, 0U) << GetLocation();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700658 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700659
660 // Top-level initializer that calls other Init methods.
661 bool Init();
662
663 // Caches pointers into to the various file sections.
664 void InitMembers();
665
666 // Builds the index of descriptors to class definitions.
667 void InitIndex();
668
669 // Returns true if the byte string equals the magic value.
670 bool CheckMagic(const byte* magic);
671
672 // Returns true if the header magic is of the expected value.
673 bool IsMagicValid();
674
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800675 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes03181a82011-11-17 17:22:21 -0800676 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
677 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const;
678
679
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800680 // The index of descriptors to class definition indexes (as opposed to type id indexes)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700681 typedef std::map<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700682 Index index_;
683
684 // The base address of the memory mapping.
685 const byte* base_;
686
687 // The size of the underlying memory allocation in bytes.
688 size_t length_;
689
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700690 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700691 //
692 // The ClassLinker will use this to match DexFiles the boot class
693 // path to DexCache::GetLocation when loading from an image.
694 const std::string location_;
695
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700696 // Manages the underlying memory allocation.
697 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700698
Jesse Wilson6bf19152011-09-29 13:12:33 -0400699 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
700 mutable Mutex dex_object_lock_;
701 mutable jobject dex_object_;
702
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700703 // Points to the header section.
704 const Header* header_;
705
706 // Points to the base of the string identifier list.
707 const StringId* string_ids_;
708
709 // Points to the base of the type identifier list.
710 const TypeId* type_ids_;
711
712 // Points to the base of the field identifier list.
713 const FieldId* field_ids_;
714
715 // Points to the base of the method identifier list.
716 const MethodId* method_ids_;
717
718 // Points to the base of the prototype identifier list.
719 const ProtoId* proto_ids_;
720
721 // Points to the base of the class definition list.
722 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700723};
724
Ian Rogers0571d352011-11-03 19:51:38 -0700725// Iterate over a dex file's ProtoId's paramters
726class DexFileParameterIterator {
727 public:
728 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
729 : dex_file_(dex_file), size_(0), pos_(0) {
730 type_list_ = dex_file_.GetProtoParameters(proto_id);
731 if (type_list_ != NULL) {
732 size_ = type_list_->Size();
733 }
734 }
735 bool HasNext() const { return pos_ < size_; }
736 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800737 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -0700738 return type_list_->GetTypeItem(pos_).type_idx_;
739 }
740 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800741 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -0700742 }
743 private:
744 const DexFile& dex_file_;
745 const DexFile::TypeList* type_list_;
746 uint32_t size_;
747 uint32_t pos_;
748 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
749};
750
751// Iterate and decode class_data_item
752class ClassDataItemIterator {
753 public:
754 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
755 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
756 ReadClassDataHeader();
757 if (EndOfInstanceFieldsPos() > 0) {
758 ReadClassDataField();
759 } else if (EndOfVirtualMethodsPos() > 0) {
760 ReadClassDataMethod();
761 }
762 }
763 uint32_t NumStaticFields() const {
764 return header_.static_fields_size_;
765 }
766 uint32_t NumInstanceFields() const {
767 return header_.instance_fields_size_;
768 }
769 uint32_t NumDirectMethods() const {
770 return header_.direct_methods_size_;
771 }
772 uint32_t NumVirtualMethods() const {
773 return header_.virtual_methods_size_;
774 }
775 bool HasNextStaticField() const {
776 return pos_ < EndOfStaticFieldsPos();
777 }
778 bool HasNextInstanceField() const {
779 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
780 }
781 bool HasNextDirectMethod() const {
782 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
783 }
784 bool HasNextVirtualMethod() const {
785 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
786 }
787 bool HasNext() const {
788 return pos_ < EndOfVirtualMethodsPos();
789 }
790 void Next() {
791 pos_++;
792 if (pos_ < EndOfStaticFieldsPos()) {
793 last_idx_ = GetMemberIndex();
794 ReadClassDataField();
795 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
796 last_idx_ = 0; // transition to next array, reset last index
797 ReadClassDataField();
798 } else if (pos_ < EndOfInstanceFieldsPos()) {
799 last_idx_ = GetMemberIndex();
800 ReadClassDataField();
801 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
802 last_idx_ = 0; // transition to next array, reset last index
803 ReadClassDataMethod();
804 } else if (pos_ < EndOfDirectMethodsPos()) {
805 last_idx_ = GetMemberIndex();
806 ReadClassDataMethod();
807 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
808 last_idx_ = 0; // transition to next array, reset last index
809 ReadClassDataMethod();
810 } else if (pos_ < EndOfVirtualMethodsPos()) {
811 last_idx_ = GetMemberIndex();
812 ReadClassDataMethod();
813 } else {
814 DCHECK(!HasNext());
815 }
816 }
817 uint32_t GetMemberIndex() const {
818 if (pos_ < EndOfInstanceFieldsPos()) {
819 return last_idx_ + field_.field_idx_delta_;
820 } else {
821 CHECK_LT(pos_, EndOfVirtualMethodsPos());
822 return last_idx_ + method_.method_idx_delta_;
823 }
824 }
825 uint32_t GetMemberAccessFlags() const {
826 if (pos_ < EndOfInstanceFieldsPos()) {
827 return field_.access_flags_;
828 } else {
829 CHECK_LT(pos_, EndOfVirtualMethodsPos());
830 return method_.access_flags_;
831 }
832 }
833 const DexFile::CodeItem* GetMethodCodeItem() const {
834 return dex_file_.GetCodeItem(method_.code_off_);
835 }
836 uint32_t GetMethodCodeItemOffset() const {
837 return method_.code_off_;
838 }
839 private:
840 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
841 // header for a class_data_item
842 struct ClassDataHeader {
843 uint32_t static_fields_size_; // the number of static fields
844 uint32_t instance_fields_size_; // the number of instance fields
845 uint32_t direct_methods_size_; // the number of direct methods
846 uint32_t virtual_methods_size_; // the number of virtual methods
847 } header_;
848
849 // Read and decode header from a class_data_item stream into header
850 void ReadClassDataHeader();
851
852 uint32_t EndOfStaticFieldsPos() const {
853 return header_.static_fields_size_;
854 }
855 uint32_t EndOfInstanceFieldsPos() const {
856 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
857 }
858 uint32_t EndOfDirectMethodsPos() const {
859 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
860 }
861 uint32_t EndOfVirtualMethodsPos() const {
862 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
863 }
864
865 // A decoded version of the field of a class_data_item
866 struct ClassDataField {
867 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
868 uint32_t access_flags_; // access flags for the field
869 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
870 private:
871 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
872 } field_;
873
874 // Read and decode a field from a class_data_item stream into field
875 void ReadClassDataField();
876
877 // A decoded version of the method of a class_data_item
878 struct ClassDataMethod {
879 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
880 uint32_t access_flags_;
881 uint32_t code_off_;
882 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
883 private:
884 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
885 } method_;
886
887 // Read and decode a method from a class_data_item stream into method
888 void ReadClassDataMethod();
889
890 const DexFile& dex_file_;
891 size_t pos_; // integral number of items passed
892 const byte* ptr_pos_; // pointer into stream of class_data_item
893 uint32_t last_idx_; // last read field or method index to apply delta to
894 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
895};
896
897class ClassLinker;
898class DexCache;
899class Field;
900
901class EncodedStaticFieldValueIterator {
902 public:
903 EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache,
904 ClassLinker* linker, const DexFile::ClassDef& class_def);
905
906 void ReadValueToField(Field* field) const;
907
908 bool HasNext() { return pos_ < array_size_; }
909
910 void Next();
911 private:
912 enum ValueType {
913 kByte = 0x00,
914 kShort = 0x02,
915 kChar = 0x03,
916 kInt = 0x04,
917 kLong = 0x06,
918 kFloat = 0x10,
919 kDouble = 0x11,
920 kString = 0x17,
921 kType = 0x18,
922 kField = 0x19,
923 kMethod = 0x1a,
924 kEnum = 0x1b,
925 kArray = 0x1c,
926 kAnnotation = 0x1d,
927 kNull = 0x1e,
928 kBoolean = 0x1f
929 };
930
931 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
932 static const byte kEncodedValueArgShift = 5;
933
934 const DexFile& dex_file_;
935 DexCache* dex_cache_; // dex cache to resolve literal objects
936 ClassLinker* linker_; // linker to resolve literal objects
937 size_t array_size_; // size of array
938 size_t pos_; // current position
939 const byte* ptr_; // pointer into encoded data array
940 byte type_; // type of current encoded value
941 jvalue jval_; // value of current encoded value
942 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
943};
944
945class CatchHandlerIterator {
946 public:
947 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
948 explicit CatchHandlerIterator(const byte* handler_data) {
949 Init(handler_data);
950 }
951
952 uint16_t GetHandlerTypeIndex() const {
953 return handler_.type_idx_;
954 }
955 uint32_t GetHandlerAddress() const {
956 return handler_.address_;
957 }
958 void Next();
959 bool HasNext() const {
960 return remaining_count_ != -1 || catch_all_;
961 }
962 // End of this set of catch blocks, convenience method to locate next set of catch blocks
963 const byte* EndDataPointer() const {
964 CHECK(!HasNext());
965 return current_data_;
966 }
967 private:
968 void Init(const byte* handler_data);
969
970 struct CatchHandlerItem {
971 uint16_t type_idx_; // type index of the caught exception type
972 uint32_t address_; // handler address
973 } handler_;
974 const byte *current_data_; // the current handler in dex file.
975 int32_t remaining_count_; // number of handlers not read.
976 bool catch_all_; // is there a handler that will catch all exceptions in case
977 // that all typed handler does not match.
978};
979
Carl Shapiro1fb86202011-06-27 17:43:13 -0700980} // namespace art
981
982#endif // ART_SRC_DEX_FILE_H_