blob: 62b99ab28b63d942f3f8139967fade664bdffcfb [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
179 static void OpenDexFiles(std::vector<const char*>& dex_filenames,
180 std::vector<const DexFile*>& dex_files,
181 const std::string& strip_location_prefix);
182
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700183 // Opens .dex file, guessing the container format based on file extension
Brian Carlstrom16192862011-09-12 17:50:06 -0700184 static const DexFile* Open(const std::string& filename,
185 const std::string& strip_location_prefix);
jeffhao262bf462011-10-20 18:36:32 -0700186
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700187 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700188 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700189
Brian Carlstroma663ea52011-08-19 23:33:41 -0700190 const std::string& GetLocation() const {
191 return location_;
192 }
193
Jesse Wilson6bf19152011-09-29 13:12:33 -0400194 // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file.
195 // Used by managed code to implement annotations.
196 jobject GetDexObject(JNIEnv* env) const;
197
Brian Carlstroma663ea52011-08-19 23:33:41 -0700198 const Header& GetHeader() const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700199 CHECK(header_ != NULL);
200 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700201 }
202
Ian Rogers0571d352011-11-03 19:51:38 -0700203 // Decode the dex magic version
Ian Rogersd81871c2011-10-03 13:57:23 -0700204 uint32_t GetVersion() const;
205
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700206 // Returns the number of string identifiers in the .dex file.
207 size_t NumStringIds() const {
208 CHECK(header_ != NULL);
209 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700210 }
211
Ian Rogers0571d352011-11-03 19:51:38 -0700212 // Returns the StringId at the specified index.
213 const StringId& GetStringId(uint32_t idx) const {
214 CHECK_LT(idx, NumStringIds());
215 return string_ids_[idx];
216 }
217
218 uint32_t GetIndexForStringId(const StringId& string_id) const {
219 CHECK_GE(&string_id, string_ids_);
220 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_);
221 return &string_id - string_ids_;
222 }
223
224 int32_t GetStringLength(const StringId& string_id) const;
225
226 // Returns a pointer to the UTF-8 string data referred to by the given string_id.
227 const char* GetStringDataAndLength(const StringId& string_id, int32_t* length) const;
228
229 const char* GetStringData(const StringId& string_id) const {
230 int32_t length;
231 return GetStringDataAndLength(string_id, &length);
232 }
233
234 // return the UTF-8 encoded string with the specified string_id index
235 const char* StringDataAndLengthByIdx(uint32_t idx, int32_t* unicode_length) const {
236 if (idx == kDexNoIndex) {
237 *unicode_length = 0;
238 return NULL;
239 }
240 const StringId& string_id = GetStringId(idx);
241 return GetStringDataAndLength(string_id, unicode_length);
242 }
243
244 const char* StringDataByIdx(uint32_t idx) const {
245 int32_t unicode_length;
246 return StringDataAndLengthByIdx(idx, &unicode_length);
247 }
248
249 // Looks up a string id for a given string
250 const StringId* FindStringId(const std::string& string) const;
251
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700252 // Returns the number of type identifiers in the .dex file.
253 size_t NumTypeIds() const {
254 CHECK(header_ != NULL);
255 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700256 }
257
Ian Rogers0571d352011-11-03 19:51:38 -0700258 // Returns the TypeId at the specified index.
259 const TypeId& GetTypeId(uint32_t idx) const {
260 CHECK_LT(idx, NumTypeIds());
261 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700262 }
263
Ian Rogers0571d352011-11-03 19:51:38 -0700264 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
265 CHECK_GE(&type_id, type_ids_);
266 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_);
267 size_t result = &type_id - type_ids_;
268 DCHECK(result < 65536);
269 return static_cast<uint16_t>(result);
270 }
271
272 // Get the descriptor string associated with a given type index.
273 const char* StringByTypeIdx(uint32_t idx, int32_t* unicode_length) const {
274 const TypeId& type_id = GetTypeId(idx);
275 return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length);
276 }
277
278 const char* StringByTypeIdx(uint32_t idx) const {
279 const TypeId& type_id = GetTypeId(idx);
280 return StringDataByIdx(type_id.descriptor_idx_);
281 }
282
283 // Returns the type descriptor string of a type id.
284 const char* GetTypeDescriptor(const TypeId& type_id) const {
285 return StringDataByIdx(type_id.descriptor_idx_);
286 }
287
288 // Looks up a type for the given string index
289 const TypeId* FindTypeId(uint32_t string_idx) const;
290
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700291 // Returns the number of field identifiers in the .dex file.
292 size_t NumFieldIds() const {
293 CHECK(header_ != NULL);
294 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700295 }
296
Ian Rogers0571d352011-11-03 19:51:38 -0700297 // Returns the FieldId at the specified index.
298 const FieldId& GetFieldId(uint32_t idx) const {
299 CHECK_LT(idx, NumFieldIds());
300 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700301 }
302
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700303 // Returns the declaring class descriptor string of a field id.
304 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700305 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
306 return GetTypeDescriptor(type_id);
307 }
308
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700309 // Returns the class descriptor string of a field id.
310 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
311 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
312 return GetTypeDescriptor(type_id);
313 }
314
Brian Carlstromb9edb842011-08-28 16:31:06 -0700315 // Returns the name of a field id.
316 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700317 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700318 }
319
Ian Rogers0571d352011-11-03 19:51:38 -0700320 // Returns the number of method identifiers in the .dex file.
321 size_t NumMethodIds() const {
322 CHECK(header_ != NULL);
323 return header_->method_ids_size_;
324 }
325
326 // Returns the MethodId at the specified index.
327 const MethodId& GetMethodId(uint32_t idx) const {
328 CHECK_LT(idx, NumMethodIds());
329 return method_ids_[idx];
330 }
331
332 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
333 CHECK_GE(&method_id, method_ids_);
334 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_);
335 return &method_id - method_ids_;
336 }
337
338 // Looks up a method by its class_dex, name and proto_id
339 const MethodId* FindMethodId(const DexFile::TypeId& klass, const DexFile::StringId& name,
340 const DexFile::ProtoId& signature) const;
341
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700342 // Returns the declaring class descriptor string of a method id.
343 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700344 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
345 return GetTypeDescriptor(type_id);
346 }
347
jeffhao98eacac2011-09-14 16:11:53 -0700348 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700349 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
350 return GetProtoId(method_id.proto_idx_);
351 }
352
353 // Returns the signature of a method id.
354 const std::string GetMethodSignature(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700355 return CreateMethodSignature(method_id.proto_idx_, NULL);
jeffhao98eacac2011-09-14 16:11:53 -0700356 }
357
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700358 // Returns the name of a method id.
359 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700360 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700361 }
362
Ian Rogers0571d352011-11-03 19:51:38 -0700363 // Returns the shorty of a method id.
364 const char* GetMethodShorty(const MethodId& method_id) const {
365 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700366 }
367
Ian Rogers0571d352011-11-03 19:51:38 -0700368 // Returns the number of class definitions in the .dex file.
369 size_t NumClassDefs() const {
370 CHECK(header_ != NULL);
371 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700372 }
373
374 // Returns the ClassDef at the specified index.
375 const ClassDef& GetClassDef(uint32_t idx) const {
376 CHECK_LT(idx, NumClassDefs());
377 return class_defs_[idx];
378 }
379
Ian Rogers0571d352011-11-03 19:51:38 -0700380 uint32_t GetIndexForClassDef(const ClassDef& class_def) const {
381 CHECK_GE(&class_def, class_defs_);
382 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_);
383 return &class_def - class_defs_;
384 }
385
386 // Returns the class descriptor string of a class definition.
387 const char* GetClassDescriptor(const ClassDef& class_def) const {
388 return StringByTypeIdx(class_def.class_idx_);
389 }
390
391 // Looks up a class definition by its class descriptor.
392 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
393
394 // Looks up a class definition index by its class descriptor.
395 bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const;
396
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700397 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
398 if (class_def.interfaces_off_ == 0) {
399 return NULL;
400 } else {
401 const byte* addr = base_ + class_def.interfaces_off_;
402 return reinterpret_cast<const TypeList*>(addr);
403 }
404 }
405
Ian Rogers0571d352011-11-03 19:51:38 -0700406 // Returns a pointer to the raw memory mapped class_data_item
407 const byte* GetClassData(const ClassDef& class_def) const {
408 if (class_def.class_data_off_ == 0) {
409 return NULL;
410 } else {
411 return base_ + class_def.class_data_off_;
412 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700413 }
414
Ian Rogers0571d352011-11-03 19:51:38 -0700415 //
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700416 const CodeItem* GetCodeItem(const uint32_t code_off_) const {
417 if (code_off_ == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700418 return NULL; // native or abstract method
419 } else {
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700420 const byte* addr = base_ + code_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700421 return reinterpret_cast<const CodeItem*>(addr);
422 }
423 }
424
Ian Rogers0571d352011-11-03 19:51:38 -0700425 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
426 return StringByTypeIdx(proto_id.return_type_idx_);
427 }
428
429 // Returns the number of prototype identifiers in the .dex file.
430 size_t NumProtoIds() const {
431 CHECK(header_ != NULL);
432 return header_->proto_ids_size_;
433 }
434
435 // Returns the ProtoId at the specified index.
436 const ProtoId& GetProtoId(uint32_t idx) const {
437 CHECK_LT(idx, NumProtoIds());
438 return proto_ids_[idx];
439 }
440
441 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
442 CHECK_GE(&proto_id, proto_ids_);
443 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_);
444 return &proto_id - proto_ids_;
445 }
446
447 // Looks up a proto id for a given return type and signature type list
448 const ProtoId* FindProtoId(uint16_t return_type_id,
449 const std::vector<uint16_t>& signature_type_ids_) const;
450
451 // Given a signature place the type ids into the given vector, returns true on success
452 bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
453 const std::string& signature) const;
454
455 // Given a proto_idx decode the type list and return type into a method signature
456 std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const;
457
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700458 // Returns the short form method descriptor for the given prototype.
459 const char* GetShorty(uint32_t proto_idx) const {
460 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700461 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700462 }
463
464 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
465 if (proto_id.parameters_off_ == 0) {
466 return NULL;
467 } else {
468 const byte* addr = base_ + proto_id.parameters_off_;
469 return reinterpret_cast<const TypeList*>(addr);
470 }
471 }
472
Ian Rogers0571d352011-11-03 19:51:38 -0700473 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700474 if (class_def.static_values_off_ == 0) {
475 return 0;
476 } else {
477 return base_ + class_def.static_values_off_;
478 }
479 }
480
Ian Rogers0571d352011-11-03 19:51:38 -0700481 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700482 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700483 return reinterpret_cast<const TryItem*>
484 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
485 }
486
487 // Get the base of the encoded data for the given DexCode.
Ian Rogers0571d352011-11-03 19:51:38 -0700488 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
489 const byte* handler_data =
490 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700491 return handler_data + offset;
492 }
493
494 // Find the handler associated with a given address, if any.
495 // Initializes the given iterator and returns true if a match is
496 // found. Returns end if there is no applicable handler.
Ian Rogers0571d352011-11-03 19:51:38 -0700497 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
498 uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700499
Shih-wei Liao195487c2011-08-20 13:29:04 -0700500 // Get the pointer to the start of the debugging data
Ian Rogers0571d352011-11-03 19:51:38 -0700501 const byte* GetDebugInfoStream(const CodeItem* code_item) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700502 if (code_item->debug_info_off_ == 0) {
503 return NULL;
504 } else {
505 return base_ + code_item->debug_info_off_;
506 }
507 }
508
509 // Callback for "new position table entry".
510 // Returning true causes the decoder to stop early.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700511 typedef bool (*DexDebugNewPositionCb)(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700512
513 // Callback for "new locals table entry". "signature" is an empty string
514 // if no signature is available for an entry.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700515 typedef void (*DexDebugNewLocalCb)(void* cnxt, uint16_t reg,
Shih-wei Liao195487c2011-08-20 13:29:04 -0700516 uint32_t startAddress,
517 uint32_t endAddress,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700518 const char* name,
519 const char* descriptor,
520 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700521
Ian Rogers0571d352011-11-03 19:51:38 -0700522 static bool LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700523
524 // Debug info opcodes and constants
525 enum {
526 DBG_END_SEQUENCE = 0x00,
527 DBG_ADVANCE_PC = 0x01,
528 DBG_ADVANCE_LINE = 0x02,
529 DBG_START_LOCAL = 0x03,
530 DBG_START_LOCAL_EXTENDED = 0x04,
531 DBG_END_LOCAL = 0x05,
532 DBG_RESTART_LOCAL = 0x06,
533 DBG_SET_PROLOGUE_END = 0x07,
534 DBG_SET_EPILOGUE_BEGIN = 0x08,
535 DBG_SET_FILE = 0x09,
536 DBG_FIRST_SPECIAL = 0x0a,
537 DBG_LINE_BASE = -4,
538 DBG_LINE_RANGE = 15,
539 };
540
541 struct LocalInfo {
Ian Rogers0571d352011-11-03 19:51:38 -0700542 LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0),
543 is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700544
Ian Rogers0571d352011-11-03 19:51:38 -0700545 const char* name_; // E.g., list
546 const char* descriptor_; // E.g., Ljava/util/LinkedList;
547 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
548 uint16_t start_address_; // PC location where the local is first defined.
549 bool is_live_; // Is the local defined and live.
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700550
551 private:
552 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700553 };
554
555 struct LineNumFromPcContext {
556 LineNumFromPcContext(uint32_t address, uint32_t line_num) :
557 address_(address), line_num_(line_num) {}
558 uint32_t address_;
559 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700560 private:
561 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700562 };
563
Brian Carlstrom78128a62011-09-15 17:21:19 -0700564 void InvokeLocalCbIfLive(void* cnxt, int reg, uint32_t end_address,
565 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700566 if (local_cb != NULL && local_in_reg[reg].is_live_) {
567 local_cb(cnxt, reg, local_in_reg[reg].start_address_, end_address,
568 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
569 local_in_reg[reg].signature_);
570 }
571 }
572
573 // Determine the source file line number based on the program counter.
574 // "pc" is an offset, in 16-bit units, from the start of the method's code.
575 //
576 // Returns -1 if no match was found (possibly because the source files were
577 // compiled without "-g", so no line number information is present).
578 // Returns -2 for native methods (as expected in exception traces).
579 //
580 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Ian Rogers0571d352011-11-03 19:51:38 -0700581 int32_t GetLineNumFromPC(const Method* method, uint32_t rel_pc) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700582
Ian Rogers0571d352011-11-03 19:51:38 -0700583 void DecodeDebugInfo0(const CodeItem* code_item, const Method* method,
584 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
585 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700586
Ian Rogers0571d352011-11-03 19:51:38 -0700587 void DecodeDebugInfo(const CodeItem* code_item, const Method* method,
588 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
589 void* cnxt) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700590
Ian Rogers0571d352011-11-03 19:51:38 -0700591 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700592 if (class_def.source_file_idx_ == 0xffffffff) {
593 return NULL;
594 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700595 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700596 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700597 }
598
jeffhaob4df5142011-09-19 20:25:32 -0700599 void ChangePermissions(int prot) const;
600
Carl Shapiro1fb86202011-06-27 17:43:13 -0700601 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700602
603 // Opens a .dex file
604 static const DexFile* OpenFile(const std::string& filename,
605 const std::string& original_location,
606 const std::string& strip_location_prefix);
607
608 // Opens a dex file from within a .jar, .zip, or .apk file
609 static const DexFile* OpenZip(const std::string& filename,
610 const std::string& strip_location_prefix);
611
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700612 // Opens a .dex file at the given address.
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700613 static const DexFile* OpenMemory(const byte* dex_file,
614 size_t length,
615 const std::string& location,
616 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700617
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700618 DexFile(const byte* addr, size_t length, const std::string& location, MemMap* mem_map)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700619 : base_(addr),
620 length_(length),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700621 location_(location),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700622 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400623 dex_object_lock_("a dex_object_lock_"),
624 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700625 header_(0),
626 string_ids_(0),
627 type_ids_(0),
628 field_ids_(0),
629 method_ids_(0),
630 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700631 class_defs_(0) {
632 CHECK(addr != NULL);
633 CHECK_GT(length, 0U);
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700634 CHECK(mem_map != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700635 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700636
637 // Top-level initializer that calls other Init methods.
638 bool Init();
639
640 // Caches pointers into to the various file sections.
641 void InitMembers();
642
643 // Builds the index of descriptors to class definitions.
644 void InitIndex();
645
646 // Returns true if the byte string equals the magic value.
647 bool CheckMagic(const byte* magic);
648
649 // Returns true if the header magic is of the expected value.
650 bool IsMagicValid();
651
Brian Carlstrome24fa612011-09-29 00:53:55 -0700652 // The index of descriptors to class definition indexes.
Ian Rogers0571d352011-11-03 19:51:38 -0700653 // TODO: given type_ids are sorted by string_id index, and string_ids are alphabetically, class
654 // lookup can be done with a binary search. Is the index necessary?
Brian Carlstrome24fa612011-09-29 00:53:55 -0700655 typedef std::map<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700656 Index index_;
657
658 // The base address of the memory mapping.
659 const byte* base_;
660
661 // The size of the underlying memory allocation in bytes.
662 size_t length_;
663
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700664 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700665 //
666 // The ClassLinker will use this to match DexFiles the boot class
667 // path to DexCache::GetLocation when loading from an image.
668 const std::string location_;
669
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700670 // Manages the underlying memory allocation.
671 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700672
Jesse Wilson6bf19152011-09-29 13:12:33 -0400673 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
674 mutable Mutex dex_object_lock_;
675 mutable jobject dex_object_;
676
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700677 // Points to the header section.
678 const Header* header_;
679
680 // Points to the base of the string identifier list.
681 const StringId* string_ids_;
682
683 // Points to the base of the type identifier list.
684 const TypeId* type_ids_;
685
686 // Points to the base of the field identifier list.
687 const FieldId* field_ids_;
688
689 // Points to the base of the method identifier list.
690 const MethodId* method_ids_;
691
692 // Points to the base of the prototype identifier list.
693 const ProtoId* proto_ids_;
694
695 // Points to the base of the class definition list.
696 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700697};
698
Ian Rogers0571d352011-11-03 19:51:38 -0700699// Iterate over a dex file's ProtoId's paramters
700class DexFileParameterIterator {
701 public:
702 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
703 : dex_file_(dex_file), size_(0), pos_(0) {
704 type_list_ = dex_file_.GetProtoParameters(proto_id);
705 if (type_list_ != NULL) {
706 size_ = type_list_->Size();
707 }
708 }
709 bool HasNext() const { return pos_ < size_; }
710 void Next() { ++pos_; }
711 uint16_t GetTypeId() {
712 return type_list_->GetTypeItem(pos_).type_idx_;
713 }
714 const char* GetDescriptor() {
715 return dex_file_.StringByTypeIdx(GetTypeId());
716 }
717 private:
718 const DexFile& dex_file_;
719 const DexFile::TypeList* type_list_;
720 uint32_t size_;
721 uint32_t pos_;
722 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
723};
724
725// Iterate and decode class_data_item
726class ClassDataItemIterator {
727 public:
728 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
729 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
730 ReadClassDataHeader();
731 if (EndOfInstanceFieldsPos() > 0) {
732 ReadClassDataField();
733 } else if (EndOfVirtualMethodsPos() > 0) {
734 ReadClassDataMethod();
735 }
736 }
737 uint32_t NumStaticFields() const {
738 return header_.static_fields_size_;
739 }
740 uint32_t NumInstanceFields() const {
741 return header_.instance_fields_size_;
742 }
743 uint32_t NumDirectMethods() const {
744 return header_.direct_methods_size_;
745 }
746 uint32_t NumVirtualMethods() const {
747 return header_.virtual_methods_size_;
748 }
749 bool HasNextStaticField() const {
750 return pos_ < EndOfStaticFieldsPos();
751 }
752 bool HasNextInstanceField() const {
753 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
754 }
755 bool HasNextDirectMethod() const {
756 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
757 }
758 bool HasNextVirtualMethod() const {
759 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
760 }
761 bool HasNext() const {
762 return pos_ < EndOfVirtualMethodsPos();
763 }
764 void Next() {
765 pos_++;
766 if (pos_ < EndOfStaticFieldsPos()) {
767 last_idx_ = GetMemberIndex();
768 ReadClassDataField();
769 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
770 last_idx_ = 0; // transition to next array, reset last index
771 ReadClassDataField();
772 } else if (pos_ < EndOfInstanceFieldsPos()) {
773 last_idx_ = GetMemberIndex();
774 ReadClassDataField();
775 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
776 last_idx_ = 0; // transition to next array, reset last index
777 ReadClassDataMethod();
778 } else if (pos_ < EndOfDirectMethodsPos()) {
779 last_idx_ = GetMemberIndex();
780 ReadClassDataMethod();
781 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
782 last_idx_ = 0; // transition to next array, reset last index
783 ReadClassDataMethod();
784 } else if (pos_ < EndOfVirtualMethodsPos()) {
785 last_idx_ = GetMemberIndex();
786 ReadClassDataMethod();
787 } else {
788 DCHECK(!HasNext());
789 }
790 }
791 uint32_t GetMemberIndex() const {
792 if (pos_ < EndOfInstanceFieldsPos()) {
793 return last_idx_ + field_.field_idx_delta_;
794 } else {
795 CHECK_LT(pos_, EndOfVirtualMethodsPos());
796 return last_idx_ + method_.method_idx_delta_;
797 }
798 }
799 uint32_t GetMemberAccessFlags() const {
800 if (pos_ < EndOfInstanceFieldsPos()) {
801 return field_.access_flags_;
802 } else {
803 CHECK_LT(pos_, EndOfVirtualMethodsPos());
804 return method_.access_flags_;
805 }
806 }
807 const DexFile::CodeItem* GetMethodCodeItem() const {
808 return dex_file_.GetCodeItem(method_.code_off_);
809 }
810 uint32_t GetMethodCodeItemOffset() const {
811 return method_.code_off_;
812 }
813 private:
814 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
815 // header for a class_data_item
816 struct ClassDataHeader {
817 uint32_t static_fields_size_; // the number of static fields
818 uint32_t instance_fields_size_; // the number of instance fields
819 uint32_t direct_methods_size_; // the number of direct methods
820 uint32_t virtual_methods_size_; // the number of virtual methods
821 } header_;
822
823 // Read and decode header from a class_data_item stream into header
824 void ReadClassDataHeader();
825
826 uint32_t EndOfStaticFieldsPos() const {
827 return header_.static_fields_size_;
828 }
829 uint32_t EndOfInstanceFieldsPos() const {
830 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
831 }
832 uint32_t EndOfDirectMethodsPos() const {
833 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
834 }
835 uint32_t EndOfVirtualMethodsPos() const {
836 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
837 }
838
839 // A decoded version of the field of a class_data_item
840 struct ClassDataField {
841 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
842 uint32_t access_flags_; // access flags for the field
843 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
844 private:
845 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
846 } field_;
847
848 // Read and decode a field from a class_data_item stream into field
849 void ReadClassDataField();
850
851 // A decoded version of the method of a class_data_item
852 struct ClassDataMethod {
853 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
854 uint32_t access_flags_;
855 uint32_t code_off_;
856 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
857 private:
858 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
859 } method_;
860
861 // Read and decode a method from a class_data_item stream into method
862 void ReadClassDataMethod();
863
864 const DexFile& dex_file_;
865 size_t pos_; // integral number of items passed
866 const byte* ptr_pos_; // pointer into stream of class_data_item
867 uint32_t last_idx_; // last read field or method index to apply delta to
868 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
869};
870
871class ClassLinker;
872class DexCache;
873class Field;
874
875class EncodedStaticFieldValueIterator {
876 public:
877 EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache,
878 ClassLinker* linker, const DexFile::ClassDef& class_def);
879
880 void ReadValueToField(Field* field) const;
881
882 bool HasNext() { return pos_ < array_size_; }
883
884 void Next();
885 private:
886 enum ValueType {
887 kByte = 0x00,
888 kShort = 0x02,
889 kChar = 0x03,
890 kInt = 0x04,
891 kLong = 0x06,
892 kFloat = 0x10,
893 kDouble = 0x11,
894 kString = 0x17,
895 kType = 0x18,
896 kField = 0x19,
897 kMethod = 0x1a,
898 kEnum = 0x1b,
899 kArray = 0x1c,
900 kAnnotation = 0x1d,
901 kNull = 0x1e,
902 kBoolean = 0x1f
903 };
904
905 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
906 static const byte kEncodedValueArgShift = 5;
907
908 const DexFile& dex_file_;
909 DexCache* dex_cache_; // dex cache to resolve literal objects
910 ClassLinker* linker_; // linker to resolve literal objects
911 size_t array_size_; // size of array
912 size_t pos_; // current position
913 const byte* ptr_; // pointer into encoded data array
914 byte type_; // type of current encoded value
915 jvalue jval_; // value of current encoded value
916 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
917};
918
919class CatchHandlerIterator {
920 public:
921 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
922 explicit CatchHandlerIterator(const byte* handler_data) {
923 Init(handler_data);
924 }
925
926 uint16_t GetHandlerTypeIndex() const {
927 return handler_.type_idx_;
928 }
929 uint32_t GetHandlerAddress() const {
930 return handler_.address_;
931 }
932 void Next();
933 bool HasNext() const {
934 return remaining_count_ != -1 || catch_all_;
935 }
936 // End of this set of catch blocks, convenience method to locate next set of catch blocks
937 const byte* EndDataPointer() const {
938 CHECK(!HasNext());
939 return current_data_;
940 }
941 private:
942 void Init(const byte* handler_data);
943
944 struct CatchHandlerItem {
945 uint16_t type_idx_; // type index of the caught exception type
946 uint32_t address_; // handler address
947 } handler_;
948 const byte *current_data_; // the current handler in dex file.
949 int32_t remaining_count_; // number of handlers not read.
950 bool catch_all_; // is there a handler that will catch all exceptions in case
951 // that all typed handler does not match.
952};
953
Carl Shapiro1fb86202011-06-27 17:43:13 -0700954} // namespace art
955
956#endif // ART_SRC_DEX_FILE_H_