blob: 2ca4308a23b167a255a95ccecd310a95d51d4a07 [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 Carlstrom7e49dca2011-07-22 18:07:34 -0700216 // Returns the number of string identifiers in the .dex file.
217 size_t NumStringIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800218 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700219 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700220 }
221
Ian Rogers0571d352011-11-03 19:51:38 -0700222 // Returns the StringId at the specified index.
223 const StringId& GetStringId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800224 CHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700225 return string_ids_[idx];
226 }
227
228 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800229 CHECK_GE(&string_id, string_ids_) << GetLocation();
230 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700231 return &string_id - string_ids_;
232 }
233
234 int32_t GetStringLength(const StringId& string_id) const;
235
236 // Returns a pointer to the UTF-8 string data referred to by the given string_id.
237 const char* GetStringDataAndLength(const StringId& string_id, int32_t* length) const;
238
239 const char* GetStringData(const StringId& string_id) const {
240 int32_t length;
241 return GetStringDataAndLength(string_id, &length);
242 }
243
244 // return the UTF-8 encoded string with the specified string_id index
245 const char* StringDataAndLengthByIdx(uint32_t idx, int32_t* unicode_length) const {
246 if (idx == kDexNoIndex) {
247 *unicode_length = 0;
248 return NULL;
249 }
250 const StringId& string_id = GetStringId(idx);
251 return GetStringDataAndLength(string_id, unicode_length);
252 }
253
254 const char* StringDataByIdx(uint32_t idx) const {
255 int32_t unicode_length;
256 return StringDataAndLengthByIdx(idx, &unicode_length);
257 }
258
259 // Looks up a string id for a given string
260 const StringId* FindStringId(const std::string& string) const;
261
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700262 // Returns the number of type identifiers in the .dex file.
263 size_t NumTypeIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800264 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700265 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700266 }
267
Ian Rogers0571d352011-11-03 19:51:38 -0700268 // Returns the TypeId at the specified index.
269 const TypeId& GetTypeId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800270 CHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700271 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700272 }
273
Ian Rogers0571d352011-11-03 19:51:38 -0700274 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800275 CHECK_GE(&type_id, type_ids_) << GetLocation();
276 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700277 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800278 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700279 return static_cast<uint16_t>(result);
280 }
281
282 // Get the descriptor string associated with a given type index.
283 const char* StringByTypeIdx(uint32_t idx, int32_t* unicode_length) const {
284 const TypeId& type_id = GetTypeId(idx);
285 return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length);
286 }
287
288 const char* StringByTypeIdx(uint32_t idx) const {
289 const TypeId& type_id = GetTypeId(idx);
290 return StringDataByIdx(type_id.descriptor_idx_);
291 }
292
293 // Returns the type descriptor string of a type id.
294 const char* GetTypeDescriptor(const TypeId& type_id) const {
295 return StringDataByIdx(type_id.descriptor_idx_);
296 }
297
298 // Looks up a type for the given string index
299 const TypeId* FindTypeId(uint32_t string_idx) const;
300
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700301 // Returns the number of field identifiers in the .dex file.
302 size_t NumFieldIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800303 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700304 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700305 }
306
Ian Rogers0571d352011-11-03 19:51:38 -0700307 // Returns the FieldId at the specified index.
308 const FieldId& GetFieldId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800309 CHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700310 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700311 }
312
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800313 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800314 CHECK_GE(&field_id, field_ids_) << GetLocation();
315 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800316 return &field_id - field_ids_;
317 }
318
319 // Looks up a field by its declaring class, name and type
320 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
321 const DexFile::StringId& name,
322 const DexFile::TypeId& type) const;
323
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700324 // Returns the declaring class descriptor string of a field id.
325 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700326 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
327 return GetTypeDescriptor(type_id);
328 }
329
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700330 // Returns the class descriptor string of a field id.
331 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
332 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
333 return GetTypeDescriptor(type_id);
334 }
335
Brian Carlstromb9edb842011-08-28 16:31:06 -0700336 // Returns the name of a field id.
337 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700338 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700339 }
340
Ian Rogers0571d352011-11-03 19:51:38 -0700341 // Returns the number of method identifiers in the .dex file.
342 size_t NumMethodIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800343 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700344 return header_->method_ids_size_;
345 }
346
347 // Returns the MethodId at the specified index.
348 const MethodId& GetMethodId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800349 CHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700350 return method_ids_[idx];
351 }
352
353 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800354 CHECK_GE(&method_id, method_ids_) << GetLocation();
355 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700356 return &method_id - method_ids_;
357 }
358
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800359 // Looks up a method by its declaring class, name and proto_id
360 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
361 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700362 const DexFile::ProtoId& signature) const;
363
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700364 // Returns the declaring class descriptor string of a method id.
365 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700366 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
367 return GetTypeDescriptor(type_id);
368 }
369
jeffhao98eacac2011-09-14 16:11:53 -0700370 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700371 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
372 return GetProtoId(method_id.proto_idx_);
373 }
374
375 // Returns the signature of a method id.
376 const std::string GetMethodSignature(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700377 return CreateMethodSignature(method_id.proto_idx_, NULL);
jeffhao98eacac2011-09-14 16:11:53 -0700378 }
379
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700380 // Returns the name of a method id.
381 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700382 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700383 }
384
Ian Rogers0571d352011-11-03 19:51:38 -0700385 // Returns the shorty of a method id.
386 const char* GetMethodShorty(const MethodId& method_id) const {
387 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700388 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800389 const char* GetMethodShorty(const MethodId& method_id, int32_t* length) const {
390 return StringDataAndLengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
391 }
Ian Rogers0571d352011-11-03 19:51:38 -0700392 // Returns the number of class definitions in the .dex file.
393 size_t NumClassDefs() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800394 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700395 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700396 }
397
398 // Returns the ClassDef at the specified index.
399 const ClassDef& GetClassDef(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800400 CHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700401 return class_defs_[idx];
402 }
403
Ian Rogers0571d352011-11-03 19:51:38 -0700404 uint32_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800405 CHECK_GE(&class_def, class_defs_) << GetLocation();
406 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700407 return &class_def - class_defs_;
408 }
409
410 // Returns the class descriptor string of a class definition.
411 const char* GetClassDescriptor(const ClassDef& class_def) const {
412 return StringByTypeIdx(class_def.class_idx_);
413 }
414
415 // Looks up a class definition by its class descriptor.
416 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
417
418 // Looks up a class definition index by its class descriptor.
419 bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const;
420
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700421 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
422 if (class_def.interfaces_off_ == 0) {
423 return NULL;
424 } else {
425 const byte* addr = base_ + class_def.interfaces_off_;
426 return reinterpret_cast<const TypeList*>(addr);
427 }
428 }
429
Ian Rogers0571d352011-11-03 19:51:38 -0700430 // Returns a pointer to the raw memory mapped class_data_item
431 const byte* GetClassData(const ClassDef& class_def) const {
432 if (class_def.class_data_off_ == 0) {
433 return NULL;
434 } else {
435 return base_ + class_def.class_data_off_;
436 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700437 }
438
Ian Rogers0571d352011-11-03 19:51:38 -0700439 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 const CodeItem* GetCodeItem(const uint32_t code_off) const {
441 if (code_off == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700442 return NULL; // native or abstract method
443 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800444 const byte* addr = base_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700445 return reinterpret_cast<const CodeItem*>(addr);
446 }
447 }
448
Ian Rogers0571d352011-11-03 19:51:38 -0700449 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
450 return StringByTypeIdx(proto_id.return_type_idx_);
451 }
452
453 // Returns the number of prototype identifiers in the .dex file.
454 size_t NumProtoIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800455 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700456 return header_->proto_ids_size_;
457 }
458
459 // Returns the ProtoId at the specified index.
460 const ProtoId& GetProtoId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800461 CHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700462 return proto_ids_[idx];
463 }
464
465 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800466 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
467 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700468 return &proto_id - proto_ids_;
469 }
470
471 // Looks up a proto id for a given return type and signature type list
472 const ProtoId* FindProtoId(uint16_t return_type_id,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800473 const std::vector<uint16_t>& signature_type_idxs_) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700474
475 // Given a signature place the type ids into the given vector, returns true on success
476 bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
477 const std::string& signature) const;
478
479 // Given a proto_idx decode the type list and return type into a method signature
480 std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const;
481
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700482 // Returns the short form method descriptor for the given prototype.
483 const char* GetShorty(uint32_t proto_idx) const {
484 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700485 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700486 }
487
488 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
489 if (proto_id.parameters_off_ == 0) {
490 return NULL;
491 } else {
492 const byte* addr = base_ + proto_id.parameters_off_;
493 return reinterpret_cast<const TypeList*>(addr);
494 }
495 }
496
Ian Rogers0571d352011-11-03 19:51:38 -0700497 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700498 if (class_def.static_values_off_ == 0) {
499 return 0;
500 } else {
501 return base_ + class_def.static_values_off_;
502 }
503 }
504
Ian Rogers0571d352011-11-03 19:51:38 -0700505 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700506 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700507 return reinterpret_cast<const TryItem*>
508 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
509 }
510
511 // Get the base of the encoded data for the given DexCode.
Ian Rogers0571d352011-11-03 19:51:38 -0700512 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
513 const byte* handler_data =
514 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700515 return handler_data + offset;
516 }
517
518 // Find the handler associated with a given address, if any.
519 // Initializes the given iterator and returns true if a match is
520 // found. Returns end if there is no applicable handler.
Ian Rogers0571d352011-11-03 19:51:38 -0700521 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
522 uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700523
Shih-wei Liao195487c2011-08-20 13:29:04 -0700524 // Get the pointer to the start of the debugging data
Ian Rogers0571d352011-11-03 19:51:38 -0700525 const byte* GetDebugInfoStream(const CodeItem* code_item) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700526 if (code_item->debug_info_off_ == 0) {
527 return NULL;
528 } else {
529 return base_ + code_item->debug_info_off_;
530 }
531 }
532
533 // Callback for "new position table entry".
534 // Returning true causes the decoder to stop early.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700535 typedef bool (*DexDebugNewPositionCb)(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700536
537 // Callback for "new locals table entry". "signature" is an empty string
538 // if no signature is available for an entry.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700539 typedef void (*DexDebugNewLocalCb)(void* cnxt, uint16_t reg,
Shih-wei Liao195487c2011-08-20 13:29:04 -0700540 uint32_t startAddress,
541 uint32_t endAddress,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700542 const char* name,
543 const char* descriptor,
544 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700545
Ian Rogers0571d352011-11-03 19:51:38 -0700546 static bool LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700547
548 // Debug info opcodes and constants
549 enum {
550 DBG_END_SEQUENCE = 0x00,
551 DBG_ADVANCE_PC = 0x01,
552 DBG_ADVANCE_LINE = 0x02,
553 DBG_START_LOCAL = 0x03,
554 DBG_START_LOCAL_EXTENDED = 0x04,
555 DBG_END_LOCAL = 0x05,
556 DBG_RESTART_LOCAL = 0x06,
557 DBG_SET_PROLOGUE_END = 0x07,
558 DBG_SET_EPILOGUE_BEGIN = 0x08,
559 DBG_SET_FILE = 0x09,
560 DBG_FIRST_SPECIAL = 0x0a,
561 DBG_LINE_BASE = -4,
562 DBG_LINE_RANGE = 15,
563 };
564
565 struct LocalInfo {
Ian Rogers0571d352011-11-03 19:51:38 -0700566 LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0),
567 is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700568
Ian Rogers0571d352011-11-03 19:51:38 -0700569 const char* name_; // E.g., list
570 const char* descriptor_; // E.g., Ljava/util/LinkedList;
571 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
572 uint16_t start_address_; // PC location where the local is first defined.
573 bool is_live_; // Is the local defined and live.
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700574
575 private:
576 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700577 };
578
579 struct LineNumFromPcContext {
580 LineNumFromPcContext(uint32_t address, uint32_t line_num) :
581 address_(address), line_num_(line_num) {}
582 uint32_t address_;
583 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700584 private:
585 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700586 };
587
Brian Carlstrom78128a62011-09-15 17:21:19 -0700588 void InvokeLocalCbIfLive(void* cnxt, int reg, uint32_t end_address,
589 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700590 if (local_cb != NULL && local_in_reg[reg].is_live_) {
591 local_cb(cnxt, reg, local_in_reg[reg].start_address_, end_address,
Elliott Hughesdbb40792011-11-18 17:05:22 -0800592 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
593 local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : "");
Shih-wei Liao195487c2011-08-20 13:29:04 -0700594 }
595 }
596
597 // Determine the source file line number based on the program counter.
598 // "pc" is an offset, in 16-bit units, from the start of the method's code.
599 //
600 // Returns -1 if no match was found (possibly because the source files were
601 // compiled without "-g", so no line number information is present).
602 // Returns -2 for native methods (as expected in exception traces).
603 //
604 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Ian Rogers0571d352011-11-03 19:51:38 -0700605 int32_t GetLineNumFromPC(const Method* method, uint32_t rel_pc) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700606
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800607 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700608 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
609 void* cnxt) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700610
Ian Rogers0571d352011-11-03 19:51:38 -0700611 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700612 if (class_def.source_file_idx_ == 0xffffffff) {
613 return NULL;
614 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700615 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700616 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700617 }
618
jeffhaob4df5142011-09-19 20:25:32 -0700619 void ChangePermissions(int prot) const;
620
Carl Shapiro1fb86202011-06-27 17:43:13 -0700621 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700622
623 // Opens a .dex file
624 static const DexFile* OpenFile(const std::string& filename,
625 const std::string& original_location,
626 const std::string& strip_location_prefix);
627
628 // Opens a dex file from within a .jar, .zip, or .apk file
629 static const DexFile* OpenZip(const std::string& filename,
630 const std::string& strip_location_prefix);
631
Brian Carlstrom89521892011-12-07 22:05:07 -0800632 // Opens a .dex file at the given address backed by a MemMap
633 static const DexFile* OpenMemory(const std::string& location,
634 MemMap* mem_map) {
635 return OpenMemory(mem_map->GetAddress(),
636 mem_map->GetLength(),
637 location,
638 mem_map);
639 }
640
641 // Opens a .dex file at the given address, optionally backed by a MemMap
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700642 static const DexFile* OpenMemory(const byte* dex_file,
643 size_t length,
644 const std::string& location,
645 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700646
Brian Carlstrom89521892011-12-07 22:05:07 -0800647 DexFile(const byte* base, size_t length, const std::string& location, MemMap* mem_map)
648 : base_(base),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700649 length_(length),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700650 location_(location),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700651 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400652 dex_object_lock_("a dex_object_lock_"),
653 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700654 header_(0),
655 string_ids_(0),
656 type_ids_(0),
657 field_ids_(0),
658 method_ids_(0),
659 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700660 class_defs_(0) {
Brian Carlstrom89521892011-12-07 22:05:07 -0800661 CHECK(base_ != NULL) << GetLocation();
662 CHECK_GT(length_, 0U) << GetLocation();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700663 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700664
665 // Top-level initializer that calls other Init methods.
666 bool Init();
667
668 // Caches pointers into to the various file sections.
669 void InitMembers();
670
671 // Builds the index of descriptors to class definitions.
672 void InitIndex();
673
674 // Returns true if the byte string equals the magic value.
675 bool CheckMagic(const byte* magic);
676
677 // Returns true if the header magic is of the expected value.
678 bool IsMagicValid();
679
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800680 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes03181a82011-11-17 17:22:21 -0800681 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
682 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const;
683
684
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800685 // The index of descriptors to class definition indexes (as opposed to type id indexes)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700686 typedef std::map<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700687 Index index_;
688
689 // The base address of the memory mapping.
690 const byte* base_;
691
692 // The size of the underlying memory allocation in bytes.
693 size_t length_;
694
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700695 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700696 //
697 // The ClassLinker will use this to match DexFiles the boot class
698 // path to DexCache::GetLocation when loading from an image.
699 const std::string location_;
700
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700701 // Manages the underlying memory allocation.
702 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700703
Jesse Wilson6bf19152011-09-29 13:12:33 -0400704 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
705 mutable Mutex dex_object_lock_;
706 mutable jobject dex_object_;
707
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700708 // Points to the header section.
709 const Header* header_;
710
711 // Points to the base of the string identifier list.
712 const StringId* string_ids_;
713
714 // Points to the base of the type identifier list.
715 const TypeId* type_ids_;
716
717 // Points to the base of the field identifier list.
718 const FieldId* field_ids_;
719
720 // Points to the base of the method identifier list.
721 const MethodId* method_ids_;
722
723 // Points to the base of the prototype identifier list.
724 const ProtoId* proto_ids_;
725
726 // Points to the base of the class definition list.
727 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700728};
729
Ian Rogers0571d352011-11-03 19:51:38 -0700730// Iterate over a dex file's ProtoId's paramters
731class DexFileParameterIterator {
732 public:
733 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
734 : dex_file_(dex_file), size_(0), pos_(0) {
735 type_list_ = dex_file_.GetProtoParameters(proto_id);
736 if (type_list_ != NULL) {
737 size_ = type_list_->Size();
738 }
739 }
740 bool HasNext() const { return pos_ < size_; }
741 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800742 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -0700743 return type_list_->GetTypeItem(pos_).type_idx_;
744 }
745 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800746 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -0700747 }
748 private:
749 const DexFile& dex_file_;
750 const DexFile::TypeList* type_list_;
751 uint32_t size_;
752 uint32_t pos_;
753 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
754};
755
756// Iterate and decode class_data_item
757class ClassDataItemIterator {
758 public:
759 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
760 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
761 ReadClassDataHeader();
762 if (EndOfInstanceFieldsPos() > 0) {
763 ReadClassDataField();
764 } else if (EndOfVirtualMethodsPos() > 0) {
765 ReadClassDataMethod();
766 }
767 }
768 uint32_t NumStaticFields() const {
769 return header_.static_fields_size_;
770 }
771 uint32_t NumInstanceFields() const {
772 return header_.instance_fields_size_;
773 }
774 uint32_t NumDirectMethods() const {
775 return header_.direct_methods_size_;
776 }
777 uint32_t NumVirtualMethods() const {
778 return header_.virtual_methods_size_;
779 }
780 bool HasNextStaticField() const {
781 return pos_ < EndOfStaticFieldsPos();
782 }
783 bool HasNextInstanceField() const {
784 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
785 }
786 bool HasNextDirectMethod() const {
787 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
788 }
789 bool HasNextVirtualMethod() const {
790 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
791 }
792 bool HasNext() const {
793 return pos_ < EndOfVirtualMethodsPos();
794 }
795 void Next() {
796 pos_++;
797 if (pos_ < EndOfStaticFieldsPos()) {
798 last_idx_ = GetMemberIndex();
799 ReadClassDataField();
800 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
801 last_idx_ = 0; // transition to next array, reset last index
802 ReadClassDataField();
803 } else if (pos_ < EndOfInstanceFieldsPos()) {
804 last_idx_ = GetMemberIndex();
805 ReadClassDataField();
806 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
807 last_idx_ = 0; // transition to next array, reset last index
808 ReadClassDataMethod();
809 } else if (pos_ < EndOfDirectMethodsPos()) {
810 last_idx_ = GetMemberIndex();
811 ReadClassDataMethod();
812 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
813 last_idx_ = 0; // transition to next array, reset last index
814 ReadClassDataMethod();
815 } else if (pos_ < EndOfVirtualMethodsPos()) {
816 last_idx_ = GetMemberIndex();
817 ReadClassDataMethod();
818 } else {
819 DCHECK(!HasNext());
820 }
821 }
822 uint32_t GetMemberIndex() const {
823 if (pos_ < EndOfInstanceFieldsPos()) {
824 return last_idx_ + field_.field_idx_delta_;
825 } else {
826 CHECK_LT(pos_, EndOfVirtualMethodsPos());
827 return last_idx_ + method_.method_idx_delta_;
828 }
829 }
830 uint32_t GetMemberAccessFlags() const {
831 if (pos_ < EndOfInstanceFieldsPos()) {
832 return field_.access_flags_;
833 } else {
834 CHECK_LT(pos_, EndOfVirtualMethodsPos());
835 return method_.access_flags_;
836 }
837 }
838 const DexFile::CodeItem* GetMethodCodeItem() const {
839 return dex_file_.GetCodeItem(method_.code_off_);
840 }
841 uint32_t GetMethodCodeItemOffset() const {
842 return method_.code_off_;
843 }
844 private:
845 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
846 // header for a class_data_item
847 struct ClassDataHeader {
848 uint32_t static_fields_size_; // the number of static fields
849 uint32_t instance_fields_size_; // the number of instance fields
850 uint32_t direct_methods_size_; // the number of direct methods
851 uint32_t virtual_methods_size_; // the number of virtual methods
852 } header_;
853
854 // Read and decode header from a class_data_item stream into header
855 void ReadClassDataHeader();
856
857 uint32_t EndOfStaticFieldsPos() const {
858 return header_.static_fields_size_;
859 }
860 uint32_t EndOfInstanceFieldsPos() const {
861 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
862 }
863 uint32_t EndOfDirectMethodsPos() const {
864 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
865 }
866 uint32_t EndOfVirtualMethodsPos() const {
867 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
868 }
869
870 // A decoded version of the field of a class_data_item
871 struct ClassDataField {
872 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
873 uint32_t access_flags_; // access flags for the field
874 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
875 private:
876 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
877 } field_;
878
879 // Read and decode a field from a class_data_item stream into field
880 void ReadClassDataField();
881
882 // A decoded version of the method of a class_data_item
883 struct ClassDataMethod {
884 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
885 uint32_t access_flags_;
886 uint32_t code_off_;
887 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
888 private:
889 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
890 } method_;
891
892 // Read and decode a method from a class_data_item stream into method
893 void ReadClassDataMethod();
894
895 const DexFile& dex_file_;
896 size_t pos_; // integral number of items passed
897 const byte* ptr_pos_; // pointer into stream of class_data_item
898 uint32_t last_idx_; // last read field or method index to apply delta to
899 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
900};
901
902class ClassLinker;
903class DexCache;
904class Field;
905
906class EncodedStaticFieldValueIterator {
907 public:
908 EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache,
909 ClassLinker* linker, const DexFile::ClassDef& class_def);
910
911 void ReadValueToField(Field* field) const;
912
913 bool HasNext() { return pos_ < array_size_; }
914
915 void Next();
916 private:
917 enum ValueType {
918 kByte = 0x00,
919 kShort = 0x02,
920 kChar = 0x03,
921 kInt = 0x04,
922 kLong = 0x06,
923 kFloat = 0x10,
924 kDouble = 0x11,
925 kString = 0x17,
926 kType = 0x18,
927 kField = 0x19,
928 kMethod = 0x1a,
929 kEnum = 0x1b,
930 kArray = 0x1c,
931 kAnnotation = 0x1d,
932 kNull = 0x1e,
933 kBoolean = 0x1f
934 };
935
936 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
937 static const byte kEncodedValueArgShift = 5;
938
939 const DexFile& dex_file_;
940 DexCache* dex_cache_; // dex cache to resolve literal objects
941 ClassLinker* linker_; // linker to resolve literal objects
942 size_t array_size_; // size of array
943 size_t pos_; // current position
944 const byte* ptr_; // pointer into encoded data array
945 byte type_; // type of current encoded value
946 jvalue jval_; // value of current encoded value
947 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
948};
949
950class CatchHandlerIterator {
951 public:
952 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
953 explicit CatchHandlerIterator(const byte* handler_data) {
954 Init(handler_data);
955 }
956
957 uint16_t GetHandlerTypeIndex() const {
958 return handler_.type_idx_;
959 }
960 uint32_t GetHandlerAddress() const {
961 return handler_.address_;
962 }
963 void Next();
964 bool HasNext() const {
965 return remaining_count_ != -1 || catch_all_;
966 }
967 // End of this set of catch blocks, convenience method to locate next set of catch blocks
968 const byte* EndDataPointer() const {
969 CHECK(!HasNext());
970 return current_data_;
971 }
972 private:
973 void Init(const byte* handler_data);
974
975 struct CatchHandlerItem {
976 uint16_t type_idx_; // type index of the caught exception type
977 uint32_t address_; // handler address
978 } handler_;
979 const byte *current_data_; // the current handler in dex file.
980 int32_t remaining_count_; // number of handlers not read.
981 bool catch_all_; // is there a handler that will catch all exceptions in case
982 // that all typed handler does not match.
983};
984
Carl Shapiro1fb86202011-06-27 17:43:13 -0700985} // namespace art
986
987#endif // ART_SRC_DEX_FILE_H_