blob: a62c28f5917f2545e18f9f285c68f04da6d3077e [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 Carlstroma6cc8932012-01-04 14:44:07 -080021class ZipArchive;
22
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070023// TODO: move all of the macro functionality into the DexCache class.
Brian Carlstromf615a612011-07-23 12:50:34 -070024class DexFile {
Carl Shapiro1fb86202011-06-27 17:43:13 -070025 public:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070026 static const byte kDexMagic[];
27 static const byte kDexMagicVersion[];
28 static const size_t kSha1DigestSize = 20;
Carl Shapiro80d4dde2011-06-28 16:24:07 -070029
Brian Carlstromb7bbba42011-10-13 14:58:47 -070030 // name of the DexFile entry within a zip archive
31 static const char* kClassesDex;
32
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070033 // The value of an invalid index.
34 static const uint32_t kDexNoIndex = 0xFFFFFFFF;
35
Ian Rogers0571d352011-11-03 19:51:38 -070036 // The value of an invalid index.
37 static const uint16_t kDexNoIndex16 = 0xFFFF;
Carl Shapiro1fb86202011-06-27 17:43:13 -070038
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070039 // Raw header_item.
40 struct Header {
41 uint8_t magic_[8];
42 uint32_t checksum_;
43 uint8_t signature_[kSha1DigestSize];
44 uint32_t file_size_; // length of entire file
45 uint32_t header_size_; // offset to start of next section
46 uint32_t endian_tag_;
Ian Rogers0571d352011-11-03 19:51:38 -070047 uint32_t link_size_; // unused
48 uint32_t link_off_; // unused
49 uint32_t map_off_; // unused
50 uint32_t string_ids_size_; // number of StringIds
51 uint32_t string_ids_off_; // file offset of StringIds array
52 uint32_t type_ids_size_; // number of TypeIds, we don't support more than 65535
53 uint32_t type_ids_off_; // file offset of TypeIds array
54 uint32_t proto_ids_size_; // number of ProtoIds, we don't support more than 65535
55 uint32_t proto_ids_off_; // file offset of ProtoIds array
56 uint32_t field_ids_size_; // number of FieldIds
57 uint32_t field_ids_off_; // file offset of FieldIds array
58 uint32_t method_ids_size_; // number of MethodIds
59 uint32_t method_ids_off_; // file offset of MethodIds array
60 uint32_t class_defs_size_; // number of ClassDefs
61 uint32_t class_defs_off_; // file offset of ClassDef array
62 uint32_t data_size_; // unused
63 uint32_t data_off_; // unused
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070064 private:
65 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070066 };
Carl Shapiro1fb86202011-06-27 17:43:13 -070067
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070068 // Raw string_id_item.
69 struct StringId {
70 uint32_t string_data_off_; // offset in bytes from the base address
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070071 private:
72 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070073 };
74
75 // Raw type_id_item.
76 struct TypeId {
77 uint32_t descriptor_idx_; // index into string_ids
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070078 private:
79 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070080 };
81
82 // Raw field_id_item.
83 struct FieldId {
Ian Rogers0571d352011-11-03 19:51:38 -070084 uint16_t class_idx_; // index into type_ids_ array for defining class
85 uint16_t type_idx_; // index into type_ids_ array for field type
86 uint32_t name_idx_; // index into string_ids_ array for field name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070087 private:
88 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070089 };
90
91 // Raw method_id_item.
92 struct MethodId {
Ian Rogers0571d352011-11-03 19:51:38 -070093 uint16_t class_idx_; // index into type_ids_ array for defining class
94 uint16_t proto_idx_; // index into proto_ids_ array for method prototype
95 uint32_t name_idx_; // index into string_ids_ array for method name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070096 private:
97 DISALLOW_COPY_AND_ASSIGN(MethodId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070098 };
99
100 // Raw proto_id_item.
101 struct ProtoId {
Ian Rogers0571d352011-11-03 19:51:38 -0700102 uint32_t shorty_idx_; // index into string_ids array for shorty descriptor
103 uint16_t return_type_idx_; // index into type_ids array for return type
104 uint16_t pad_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700105 uint32_t parameters_off_; // file offset to type_list for parameter types
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700106 private:
107 DISALLOW_COPY_AND_ASSIGN(ProtoId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700108 };
109
110 // Raw class_def_item.
111 struct ClassDef {
Ian Rogers0571d352011-11-03 19:51:38 -0700112 uint16_t class_idx_; // index into type_ids_ array for this class
113 uint16_t pad1_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700114 uint32_t access_flags_;
Ian Rogers0571d352011-11-03 19:51:38 -0700115 uint16_t superclass_idx_; // index into type_ids_ array for superclass
116 uint16_t pad2_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700117 uint32_t interfaces_off_; // file offset to TypeList
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700118 uint32_t source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700119 uint32_t annotations_off_; // file offset to annotations_directory_item
120 uint32_t class_data_off_; // file offset to class_data_item
121 uint32_t static_values_off_; // file offset to EncodedArray
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700122 private:
123 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700124 };
125
126 // Raw type_item.
127 struct TypeItem {
128 uint16_t type_idx_; // index into type_ids section
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700129 private:
130 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700131 };
132
133 // Raw type_list.
134 class TypeList {
135 public:
136 uint32_t Size() const {
137 return size_;
138 }
139
140 const TypeItem& GetTypeItem(uint32_t idx) const {
141 CHECK_LT(idx, this->size_);
142 return this->list_[idx];
143 }
144
145 private:
146 uint32_t size_; // size of the list, in entries
147 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700148 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700149 };
150
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700151 // Raw code_item.
152 struct CodeItem {
153 uint16_t registers_size_;
154 uint16_t ins_size_;
155 uint16_t outs_size_;
156 uint16_t tries_size_;
157 uint32_t debug_info_off_; // file offset to debug info stream
Ian Rogersd81871c2011-10-03 13:57:23 -0700158 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700159 uint16_t insns_[1];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700160 private:
161 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700162 };
163
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700164 // Raw try_item.
165 struct TryItem {
166 uint32_t start_addr_;
167 uint16_t insn_count_;
168 uint16_t handler_off_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700169 private:
170 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700171 };
172
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700173 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
174 typedef std::vector<const DexFile*> ClassPath;
175
176 // Search a collection of DexFiles for a descriptor
177 static ClassPathEntry FindInClassPath(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700178 const ClassPath& class_path);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700179
Brian Carlstrom78128a62011-09-15 17:21:19 -0700180 // Opens a collection of .dex files
Brian Carlstromae826982011-11-09 01:33:42 -0800181 static void OpenDexFiles(const std::vector<const char*>& dex_filenames,
Brian Carlstrom78128a62011-09-15 17:21:19 -0700182 std::vector<const DexFile*>& dex_files,
183 const std::string& strip_location_prefix);
184
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700185 // Opens .dex file, guessing the container format based on file extension
Brian Carlstrom16192862011-09-12 17:50:06 -0700186 static const DexFile* Open(const std::string& filename,
187 const std::string& strip_location_prefix);
jeffhao262bf462011-10-20 18:36:32 -0700188
Brian Carlstrom89521892011-12-07 22:05:07 -0800189 // Opens .dex file, backed by existing memory
190 static const DexFile* Open(const uint8_t* base, size_t length, const std::string& location) {
191 return OpenMemory(base, length, location, NULL);
192 }
193
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800194 // Opens .dex file from the classes.dex in a zip archive
195 static const DexFile* Open(const ZipArchive& zip_archive, const std::string& location);
196
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700197 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700198 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700199
Brian Carlstroma663ea52011-08-19 23:33:41 -0700200 const std::string& GetLocation() const {
201 return location_;
202 }
203
Jesse Wilson6bf19152011-09-29 13:12:33 -0400204 // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file.
205 // Used by managed code to implement annotations.
206 jobject GetDexObject(JNIEnv* env) const;
207
Brian Carlstroma663ea52011-08-19 23:33:41 -0700208 const Header& GetHeader() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800209 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700210 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700211 }
212
Ian Rogers0571d352011-11-03 19:51:38 -0700213 // Decode the dex magic version
Ian Rogersd81871c2011-10-03 13:57:23 -0700214 uint32_t GetVersion() const;
215
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800216 // Returns true if the byte string points to the magic value.
217 static bool IsMagicValid(const byte* magic);
218
219 // Returns true if the byte string after the magic is the correct value.
220 static bool IsVersionValid(const byte* magic);
221
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700222 // Returns the number of string identifiers in the .dex file.
223 size_t NumStringIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800224 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700225 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700226 }
227
Ian Rogers0571d352011-11-03 19:51:38 -0700228 // Returns the StringId at the specified index.
229 const StringId& GetStringId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800230 CHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700231 return string_ids_[idx];
232 }
233
234 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800235 CHECK_GE(&string_id, string_ids_) << GetLocation();
236 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700237 return &string_id - string_ids_;
238 }
239
240 int32_t GetStringLength(const StringId& string_id) const;
241
242 // Returns a pointer to the UTF-8 string data referred to by the given string_id.
243 const char* GetStringDataAndLength(const StringId& string_id, int32_t* length) const;
244
245 const char* GetStringData(const StringId& string_id) const {
246 int32_t length;
247 return GetStringDataAndLength(string_id, &length);
248 }
249
250 // return the UTF-8 encoded string with the specified string_id index
251 const char* StringDataAndLengthByIdx(uint32_t idx, int32_t* unicode_length) const {
252 if (idx == kDexNoIndex) {
253 *unicode_length = 0;
254 return NULL;
255 }
256 const StringId& string_id = GetStringId(idx);
257 return GetStringDataAndLength(string_id, unicode_length);
258 }
259
260 const char* StringDataByIdx(uint32_t idx) const {
261 int32_t unicode_length;
262 return StringDataAndLengthByIdx(idx, &unicode_length);
263 }
264
265 // Looks up a string id for a given string
266 const StringId* FindStringId(const std::string& string) const;
267
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700268 // Returns the number of type identifiers in the .dex file.
269 size_t NumTypeIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800270 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700271 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700272 }
273
Ian Rogers0571d352011-11-03 19:51:38 -0700274 // Returns the TypeId at the specified index.
275 const TypeId& GetTypeId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800276 CHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700277 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700278 }
279
Ian Rogers0571d352011-11-03 19:51:38 -0700280 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800281 CHECK_GE(&type_id, type_ids_) << GetLocation();
282 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700283 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800284 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700285 return static_cast<uint16_t>(result);
286 }
287
288 // Get the descriptor string associated with a given type index.
289 const char* StringByTypeIdx(uint32_t idx, int32_t* unicode_length) const {
290 const TypeId& type_id = GetTypeId(idx);
291 return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length);
292 }
293
294 const char* StringByTypeIdx(uint32_t idx) const {
295 const TypeId& type_id = GetTypeId(idx);
296 return StringDataByIdx(type_id.descriptor_idx_);
297 }
298
299 // Returns the type descriptor string of a type id.
300 const char* GetTypeDescriptor(const TypeId& type_id) const {
301 return StringDataByIdx(type_id.descriptor_idx_);
302 }
303
304 // Looks up a type for the given string index
305 const TypeId* FindTypeId(uint32_t string_idx) const;
306
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700307 // Returns the number of field identifiers in the .dex file.
308 size_t NumFieldIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800309 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700310 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700311 }
312
Ian Rogers0571d352011-11-03 19:51:38 -0700313 // Returns the FieldId at the specified index.
314 const FieldId& GetFieldId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800315 CHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700316 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700317 }
318
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800319 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800320 CHECK_GE(&field_id, field_ids_) << GetLocation();
321 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800322 return &field_id - field_ids_;
323 }
324
325 // Looks up a field by its declaring class, name and type
326 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
327 const DexFile::StringId& name,
328 const DexFile::TypeId& type) const;
329
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700330 // Returns the declaring class descriptor string of a field id.
331 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700332 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
333 return GetTypeDescriptor(type_id);
334 }
335
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700336 // Returns the class descriptor string of a field id.
337 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
338 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
339 return GetTypeDescriptor(type_id);
340 }
341
Brian Carlstromb9edb842011-08-28 16:31:06 -0700342 // Returns the name of a field id.
343 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700344 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700345 }
346
Ian Rogers0571d352011-11-03 19:51:38 -0700347 // Returns the number of method identifiers in the .dex file.
348 size_t NumMethodIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800349 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700350 return header_->method_ids_size_;
351 }
352
353 // Returns the MethodId at the specified index.
354 const MethodId& GetMethodId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800355 CHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700356 return method_ids_[idx];
357 }
358
359 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800360 CHECK_GE(&method_id, method_ids_) << GetLocation();
361 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700362 return &method_id - method_ids_;
363 }
364
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800365 // Looks up a method by its declaring class, name and proto_id
366 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
367 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700368 const DexFile::ProtoId& signature) const;
369
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700370 // Returns the declaring class descriptor string of a method id.
371 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700372 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
373 return GetTypeDescriptor(type_id);
374 }
375
jeffhao98eacac2011-09-14 16:11:53 -0700376 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700377 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
378 return GetProtoId(method_id.proto_idx_);
379 }
380
381 // Returns the signature of a method id.
382 const std::string GetMethodSignature(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700383 return CreateMethodSignature(method_id.proto_idx_, NULL);
jeffhao98eacac2011-09-14 16:11:53 -0700384 }
385
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700386 // Returns the name of a method id.
387 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700388 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700389 }
390
Ian Rogers0571d352011-11-03 19:51:38 -0700391 // Returns the shorty of a method id.
392 const char* GetMethodShorty(const MethodId& method_id) const {
393 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700394 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800395 const char* GetMethodShorty(const MethodId& method_id, int32_t* length) const {
396 return StringDataAndLengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
397 }
Ian Rogers0571d352011-11-03 19:51:38 -0700398 // Returns the number of class definitions in the .dex file.
399 size_t NumClassDefs() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800400 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700401 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700402 }
403
404 // Returns the ClassDef at the specified index.
405 const ClassDef& GetClassDef(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800406 CHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700407 return class_defs_[idx];
408 }
409
Ian Rogers0571d352011-11-03 19:51:38 -0700410 uint32_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800411 CHECK_GE(&class_def, class_defs_) << GetLocation();
412 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700413 return &class_def - class_defs_;
414 }
415
416 // Returns the class descriptor string of a class definition.
417 const char* GetClassDescriptor(const ClassDef& class_def) const {
418 return StringByTypeIdx(class_def.class_idx_);
419 }
420
421 // Looks up a class definition by its class descriptor.
422 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
423
424 // Looks up a class definition index by its class descriptor.
425 bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const;
426
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700427 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
428 if (class_def.interfaces_off_ == 0) {
429 return NULL;
430 } else {
431 const byte* addr = base_ + class_def.interfaces_off_;
432 return reinterpret_cast<const TypeList*>(addr);
433 }
434 }
435
Ian Rogers0571d352011-11-03 19:51:38 -0700436 // Returns a pointer to the raw memory mapped class_data_item
437 const byte* GetClassData(const ClassDef& class_def) const {
438 if (class_def.class_data_off_ == 0) {
439 return NULL;
440 } else {
441 return base_ + class_def.class_data_off_;
442 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700443 }
444
Ian Rogers0571d352011-11-03 19:51:38 -0700445 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446 const CodeItem* GetCodeItem(const uint32_t code_off) const {
447 if (code_off == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700448 return NULL; // native or abstract method
449 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800450 const byte* addr = base_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700451 return reinterpret_cast<const CodeItem*>(addr);
452 }
453 }
454
Ian Rogers0571d352011-11-03 19:51:38 -0700455 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
456 return StringByTypeIdx(proto_id.return_type_idx_);
457 }
458
459 // Returns the number of prototype identifiers in the .dex file.
460 size_t NumProtoIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800461 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700462 return header_->proto_ids_size_;
463 }
464
465 // Returns the ProtoId at the specified index.
466 const ProtoId& GetProtoId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800467 CHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700468 return proto_ids_[idx];
469 }
470
471 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800472 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
473 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700474 return &proto_id - proto_ids_;
475 }
476
477 // Looks up a proto id for a given return type and signature type list
478 const ProtoId* FindProtoId(uint16_t return_type_id,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800479 const std::vector<uint16_t>& signature_type_idxs_) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700480
481 // Given a signature place the type ids into the given vector, returns true on success
482 bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
483 const std::string& signature) const;
484
485 // Given a proto_idx decode the type list and return type into a method signature
486 std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const;
487
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700488 // Returns the short form method descriptor for the given prototype.
489 const char* GetShorty(uint32_t proto_idx) const {
490 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700491 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700492 }
493
494 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
495 if (proto_id.parameters_off_ == 0) {
496 return NULL;
497 } else {
498 const byte* addr = base_ + proto_id.parameters_off_;
499 return reinterpret_cast<const TypeList*>(addr);
500 }
501 }
502
Ian Rogers0571d352011-11-03 19:51:38 -0700503 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700504 if (class_def.static_values_off_ == 0) {
505 return 0;
506 } else {
507 return base_ + class_def.static_values_off_;
508 }
509 }
510
Ian Rogers0571d352011-11-03 19:51:38 -0700511 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700512 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700513 return reinterpret_cast<const TryItem*>
514 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
515 }
516
517 // Get the base of the encoded data for the given DexCode.
Ian Rogers0571d352011-11-03 19:51:38 -0700518 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
519 const byte* handler_data =
520 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700521 return handler_data + offset;
522 }
523
524 // Find the handler associated with a given address, if any.
525 // Initializes the given iterator and returns true if a match is
526 // found. Returns end if there is no applicable handler.
Ian Rogers0571d352011-11-03 19:51:38 -0700527 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
528 uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700529
Shih-wei Liao195487c2011-08-20 13:29:04 -0700530 // Get the pointer to the start of the debugging data
Ian Rogers0571d352011-11-03 19:51:38 -0700531 const byte* GetDebugInfoStream(const CodeItem* code_item) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700532 if (code_item->debug_info_off_ == 0) {
533 return NULL;
534 } else {
535 return base_ + code_item->debug_info_off_;
536 }
537 }
538
539 // Callback for "new position table entry".
540 // Returning true causes the decoder to stop early.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700541 typedef bool (*DexDebugNewPositionCb)(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700542
543 // Callback for "new locals table entry". "signature" is an empty string
544 // if no signature is available for an entry.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700545 typedef void (*DexDebugNewLocalCb)(void* cnxt, uint16_t reg,
Shih-wei Liao195487c2011-08-20 13:29:04 -0700546 uint32_t startAddress,
547 uint32_t endAddress,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700548 const char* name,
549 const char* descriptor,
550 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700551
Ian Rogers0571d352011-11-03 19:51:38 -0700552 static bool LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700553
554 // Debug info opcodes and constants
555 enum {
556 DBG_END_SEQUENCE = 0x00,
557 DBG_ADVANCE_PC = 0x01,
558 DBG_ADVANCE_LINE = 0x02,
559 DBG_START_LOCAL = 0x03,
560 DBG_START_LOCAL_EXTENDED = 0x04,
561 DBG_END_LOCAL = 0x05,
562 DBG_RESTART_LOCAL = 0x06,
563 DBG_SET_PROLOGUE_END = 0x07,
564 DBG_SET_EPILOGUE_BEGIN = 0x08,
565 DBG_SET_FILE = 0x09,
566 DBG_FIRST_SPECIAL = 0x0a,
567 DBG_LINE_BASE = -4,
568 DBG_LINE_RANGE = 15,
569 };
570
571 struct LocalInfo {
Ian Rogers0571d352011-11-03 19:51:38 -0700572 LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0),
573 is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700574
Ian Rogers0571d352011-11-03 19:51:38 -0700575 const char* name_; // E.g., list
576 const char* descriptor_; // E.g., Ljava/util/LinkedList;
577 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
578 uint16_t start_address_; // PC location where the local is first defined.
579 bool is_live_; // Is the local defined and live.
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700580
581 private:
582 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700583 };
584
585 struct LineNumFromPcContext {
586 LineNumFromPcContext(uint32_t address, uint32_t line_num) :
587 address_(address), line_num_(line_num) {}
588 uint32_t address_;
589 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700590 private:
591 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700592 };
593
Brian Carlstrom78128a62011-09-15 17:21:19 -0700594 void InvokeLocalCbIfLive(void* cnxt, int reg, uint32_t end_address,
595 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700596 if (local_cb != NULL && local_in_reg[reg].is_live_) {
597 local_cb(cnxt, reg, local_in_reg[reg].start_address_, end_address,
Elliott Hughesdbb40792011-11-18 17:05:22 -0800598 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
599 local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : "");
Shih-wei Liao195487c2011-08-20 13:29:04 -0700600 }
601 }
602
603 // Determine the source file line number based on the program counter.
604 // "pc" is an offset, in 16-bit units, from the start of the method's code.
605 //
606 // Returns -1 if no match was found (possibly because the source files were
607 // compiled without "-g", so no line number information is present).
608 // Returns -2 for native methods (as expected in exception traces).
609 //
610 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Ian Rogers0571d352011-11-03 19:51:38 -0700611 int32_t GetLineNumFromPC(const Method* method, uint32_t rel_pc) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700612
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800613 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700614 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
615 void* cnxt) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700616
Ian Rogers0571d352011-11-03 19:51:38 -0700617 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700618 if (class_def.source_file_idx_ == 0xffffffff) {
619 return NULL;
620 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700621 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700622 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700623 }
624
jeffhaob4df5142011-09-19 20:25:32 -0700625 void ChangePermissions(int prot) const;
626
Carl Shapiro1fb86202011-06-27 17:43:13 -0700627 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700628
629 // Opens a .dex file
630 static const DexFile* OpenFile(const std::string& filename,
631 const std::string& original_location,
632 const std::string& strip_location_prefix);
633
634 // Opens a dex file from within a .jar, .zip, or .apk file
635 static const DexFile* OpenZip(const std::string& filename,
636 const std::string& strip_location_prefix);
637
Brian Carlstrom89521892011-12-07 22:05:07 -0800638 // Opens a .dex file at the given address backed by a MemMap
639 static const DexFile* OpenMemory(const std::string& location,
640 MemMap* mem_map) {
641 return OpenMemory(mem_map->GetAddress(),
642 mem_map->GetLength(),
643 location,
644 mem_map);
645 }
646
647 // Opens a .dex file at the given address, optionally backed by a MemMap
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700648 static const DexFile* OpenMemory(const byte* dex_file,
649 size_t length,
650 const std::string& location,
651 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700652
Brian Carlstrom89521892011-12-07 22:05:07 -0800653 DexFile(const byte* base, size_t length, const std::string& location, MemMap* mem_map)
654 : base_(base),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700655 length_(length),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700656 location_(location),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700657 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400658 dex_object_lock_("a dex_object_lock_"),
659 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700660 header_(0),
661 string_ids_(0),
662 type_ids_(0),
663 field_ids_(0),
664 method_ids_(0),
665 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700666 class_defs_(0) {
Brian Carlstrom89521892011-12-07 22:05:07 -0800667 CHECK(base_ != NULL) << GetLocation();
668 CHECK_GT(length_, 0U) << GetLocation();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700669 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700670
671 // Top-level initializer that calls other Init methods.
672 bool Init();
673
674 // Caches pointers into to the various file sections.
675 void InitMembers();
676
677 // Builds the index of descriptors to class definitions.
678 void InitIndex();
679
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800680 // Returns true if the header magic and version numbers are of the expected values.
681 bool CheckMagicAndVersion();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700682
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800683 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes03181a82011-11-17 17:22:21 -0800684 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
685 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const;
686
687
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800688 // The index of descriptors to class definition indexes (as opposed to type id indexes)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700689 typedef std::map<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700690 Index index_;
691
692 // The base address of the memory mapping.
693 const byte* base_;
694
695 // The size of the underlying memory allocation in bytes.
696 size_t length_;
697
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700698 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700699 //
700 // The ClassLinker will use this to match DexFiles the boot class
701 // path to DexCache::GetLocation when loading from an image.
702 const std::string location_;
703
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700704 // Manages the underlying memory allocation.
705 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700706
Jesse Wilson6bf19152011-09-29 13:12:33 -0400707 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
708 mutable Mutex dex_object_lock_;
709 mutable jobject dex_object_;
710
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700711 // Points to the header section.
712 const Header* header_;
713
714 // Points to the base of the string identifier list.
715 const StringId* string_ids_;
716
717 // Points to the base of the type identifier list.
718 const TypeId* type_ids_;
719
720 // Points to the base of the field identifier list.
721 const FieldId* field_ids_;
722
723 // Points to the base of the method identifier list.
724 const MethodId* method_ids_;
725
726 // Points to the base of the prototype identifier list.
727 const ProtoId* proto_ids_;
728
729 // Points to the base of the class definition list.
730 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700731};
732
Ian Rogers0571d352011-11-03 19:51:38 -0700733// Iterate over a dex file's ProtoId's paramters
734class DexFileParameterIterator {
735 public:
736 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
737 : dex_file_(dex_file), size_(0), pos_(0) {
738 type_list_ = dex_file_.GetProtoParameters(proto_id);
739 if (type_list_ != NULL) {
740 size_ = type_list_->Size();
741 }
742 }
743 bool HasNext() const { return pos_ < size_; }
744 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800745 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -0700746 return type_list_->GetTypeItem(pos_).type_idx_;
747 }
748 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800749 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -0700750 }
751 private:
752 const DexFile& dex_file_;
753 const DexFile::TypeList* type_list_;
754 uint32_t size_;
755 uint32_t pos_;
756 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
757};
758
759// Iterate and decode class_data_item
760class ClassDataItemIterator {
761 public:
762 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
763 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
764 ReadClassDataHeader();
765 if (EndOfInstanceFieldsPos() > 0) {
766 ReadClassDataField();
767 } else if (EndOfVirtualMethodsPos() > 0) {
768 ReadClassDataMethod();
769 }
770 }
771 uint32_t NumStaticFields() const {
772 return header_.static_fields_size_;
773 }
774 uint32_t NumInstanceFields() const {
775 return header_.instance_fields_size_;
776 }
777 uint32_t NumDirectMethods() const {
778 return header_.direct_methods_size_;
779 }
780 uint32_t NumVirtualMethods() const {
781 return header_.virtual_methods_size_;
782 }
783 bool HasNextStaticField() const {
784 return pos_ < EndOfStaticFieldsPos();
785 }
786 bool HasNextInstanceField() const {
787 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
788 }
789 bool HasNextDirectMethod() const {
790 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
791 }
792 bool HasNextVirtualMethod() const {
793 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
794 }
795 bool HasNext() const {
796 return pos_ < EndOfVirtualMethodsPos();
797 }
798 void Next() {
799 pos_++;
800 if (pos_ < EndOfStaticFieldsPos()) {
801 last_idx_ = GetMemberIndex();
802 ReadClassDataField();
803 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
804 last_idx_ = 0; // transition to next array, reset last index
805 ReadClassDataField();
806 } else if (pos_ < EndOfInstanceFieldsPos()) {
807 last_idx_ = GetMemberIndex();
808 ReadClassDataField();
809 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
810 last_idx_ = 0; // transition to next array, reset last index
811 ReadClassDataMethod();
812 } else if (pos_ < EndOfDirectMethodsPos()) {
813 last_idx_ = GetMemberIndex();
814 ReadClassDataMethod();
815 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
816 last_idx_ = 0; // transition to next array, reset last index
817 ReadClassDataMethod();
818 } else if (pos_ < EndOfVirtualMethodsPos()) {
819 last_idx_ = GetMemberIndex();
820 ReadClassDataMethod();
821 } else {
822 DCHECK(!HasNext());
823 }
824 }
825 uint32_t GetMemberIndex() const {
826 if (pos_ < EndOfInstanceFieldsPos()) {
827 return last_idx_ + field_.field_idx_delta_;
828 } else {
829 CHECK_LT(pos_, EndOfVirtualMethodsPos());
830 return last_idx_ + method_.method_idx_delta_;
831 }
832 }
833 uint32_t GetMemberAccessFlags() const {
834 if (pos_ < EndOfInstanceFieldsPos()) {
835 return field_.access_flags_;
836 } else {
837 CHECK_LT(pos_, EndOfVirtualMethodsPos());
838 return method_.access_flags_;
839 }
840 }
841 const DexFile::CodeItem* GetMethodCodeItem() const {
842 return dex_file_.GetCodeItem(method_.code_off_);
843 }
844 uint32_t GetMethodCodeItemOffset() const {
845 return method_.code_off_;
846 }
847 private:
848 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
849 // header for a class_data_item
850 struct ClassDataHeader {
851 uint32_t static_fields_size_; // the number of static fields
852 uint32_t instance_fields_size_; // the number of instance fields
853 uint32_t direct_methods_size_; // the number of direct methods
854 uint32_t virtual_methods_size_; // the number of virtual methods
855 } header_;
856
857 // Read and decode header from a class_data_item stream into header
858 void ReadClassDataHeader();
859
860 uint32_t EndOfStaticFieldsPos() const {
861 return header_.static_fields_size_;
862 }
863 uint32_t EndOfInstanceFieldsPos() const {
864 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
865 }
866 uint32_t EndOfDirectMethodsPos() const {
867 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
868 }
869 uint32_t EndOfVirtualMethodsPos() const {
870 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
871 }
872
873 // A decoded version of the field of a class_data_item
874 struct ClassDataField {
875 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
876 uint32_t access_flags_; // access flags for the field
877 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
878 private:
879 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
880 } field_;
881
882 // Read and decode a field from a class_data_item stream into field
883 void ReadClassDataField();
884
885 // A decoded version of the method of a class_data_item
886 struct ClassDataMethod {
887 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
888 uint32_t access_flags_;
889 uint32_t code_off_;
890 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
891 private:
892 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
893 } method_;
894
895 // Read and decode a method from a class_data_item stream into method
896 void ReadClassDataMethod();
897
898 const DexFile& dex_file_;
899 size_t pos_; // integral number of items passed
900 const byte* ptr_pos_; // pointer into stream of class_data_item
901 uint32_t last_idx_; // last read field or method index to apply delta to
902 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
903};
904
905class ClassLinker;
906class DexCache;
907class Field;
908
909class EncodedStaticFieldValueIterator {
910 public:
911 EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache,
912 ClassLinker* linker, const DexFile::ClassDef& class_def);
913
914 void ReadValueToField(Field* field) const;
915
916 bool HasNext() { return pos_ < array_size_; }
917
918 void Next();
919 private:
920 enum ValueType {
921 kByte = 0x00,
922 kShort = 0x02,
923 kChar = 0x03,
924 kInt = 0x04,
925 kLong = 0x06,
926 kFloat = 0x10,
927 kDouble = 0x11,
928 kString = 0x17,
929 kType = 0x18,
930 kField = 0x19,
931 kMethod = 0x1a,
932 kEnum = 0x1b,
933 kArray = 0x1c,
934 kAnnotation = 0x1d,
935 kNull = 0x1e,
936 kBoolean = 0x1f
937 };
938
939 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
940 static const byte kEncodedValueArgShift = 5;
941
942 const DexFile& dex_file_;
943 DexCache* dex_cache_; // dex cache to resolve literal objects
944 ClassLinker* linker_; // linker to resolve literal objects
945 size_t array_size_; // size of array
946 size_t pos_; // current position
947 const byte* ptr_; // pointer into encoded data array
948 byte type_; // type of current encoded value
949 jvalue jval_; // value of current encoded value
950 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
951};
952
953class CatchHandlerIterator {
954 public:
955 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
956 explicit CatchHandlerIterator(const byte* handler_data) {
957 Init(handler_data);
958 }
959
960 uint16_t GetHandlerTypeIndex() const {
961 return handler_.type_idx_;
962 }
963 uint32_t GetHandlerAddress() const {
964 return handler_.address_;
965 }
966 void Next();
967 bool HasNext() const {
968 return remaining_count_ != -1 || catch_all_;
969 }
970 // End of this set of catch blocks, convenience method to locate next set of catch blocks
971 const byte* EndDataPointer() const {
972 CHECK(!HasNext());
973 return current_data_;
974 }
975 private:
976 void Init(const byte* handler_data);
977
978 struct CatchHandlerItem {
979 uint16_t type_idx_; // type index of the caught exception type
980 uint32_t address_; // handler address
981 } handler_;
982 const byte *current_data_; // the current handler in dex file.
983 int32_t remaining_count_; // number of handlers not read.
984 bool catch_all_; // is there a handler that will catch all exceptions in case
985 // that all typed handler does not match.
986};
987
Carl Shapiro1fb86202011-06-27 17:43:13 -0700988} // namespace art
989
990#endif // ART_SRC_DEX_FILE_H_