blob: 4c42b9b592de30c0ae6fb137599e8dd9bed602da [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_DEX_FILE_H_
18#define ART_SRC_DEX_FILE_H_
19
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070020#include <map>
Elliott Hughes0c424cb2011-08-26 10:16:25 -070021#include <string>
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070022#include <vector>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070023
Elliott Hughes90a33692011-08-30 13:27:07 -070024#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "globals.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040026#include "jni.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070027#include "logging.h"
Brian Carlstrom33f741e2011-10-03 11:24:05 -070028#include "mem_map.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040029#include "mutex.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070030#include "stringpiece.h"
Shih-wei Liao2fb97532011-08-11 16:17:23 -070031#include "utils.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070032
33namespace art {
34
Brian Carlstroma6cc8932012-01-04 14:44:07 -080035class ZipArchive;
36
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070037// TODO: move all of the macro functionality into the DexCache class.
Brian Carlstromf615a612011-07-23 12:50:34 -070038class DexFile {
Carl Shapiro1fb86202011-06-27 17:43:13 -070039 public:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070040 static const byte kDexMagic[];
41 static const byte kDexMagicVersion[];
42 static const size_t kSha1DigestSize = 20;
jeffhao10037c82012-01-23 15:06:23 -080043 static const uint32_t kDexEndianConstant = 0x12345678;
Carl Shapiro80d4dde2011-06-28 16:24:07 -070044
Brian Carlstromb7bbba42011-10-13 14:58:47 -070045 // name of the DexFile entry within a zip archive
46 static const char* kClassesDex;
47
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070048 // The value of an invalid index.
49 static const uint32_t kDexNoIndex = 0xFFFFFFFF;
50
Ian Rogers0571d352011-11-03 19:51:38 -070051 // The value of an invalid index.
52 static const uint16_t kDexNoIndex16 = 0xFFFF;
Carl Shapiro1fb86202011-06-27 17:43:13 -070053
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070054 // Raw header_item.
55 struct Header {
56 uint8_t magic_[8];
57 uint32_t checksum_;
58 uint8_t signature_[kSha1DigestSize];
59 uint32_t file_size_; // length of entire file
60 uint32_t header_size_; // offset to start of next section
61 uint32_t endian_tag_;
Ian Rogers0571d352011-11-03 19:51:38 -070062 uint32_t link_size_; // unused
63 uint32_t link_off_; // unused
64 uint32_t map_off_; // unused
65 uint32_t string_ids_size_; // number of StringIds
66 uint32_t string_ids_off_; // file offset of StringIds array
67 uint32_t type_ids_size_; // number of TypeIds, we don't support more than 65535
68 uint32_t type_ids_off_; // file offset of TypeIds array
69 uint32_t proto_ids_size_; // number of ProtoIds, we don't support more than 65535
70 uint32_t proto_ids_off_; // file offset of ProtoIds array
71 uint32_t field_ids_size_; // number of FieldIds
72 uint32_t field_ids_off_; // file offset of FieldIds array
73 uint32_t method_ids_size_; // number of MethodIds
74 uint32_t method_ids_off_; // file offset of MethodIds array
75 uint32_t class_defs_size_; // number of ClassDefs
76 uint32_t class_defs_off_; // file offset of ClassDef array
77 uint32_t data_size_; // unused
78 uint32_t data_off_; // unused
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070079 private:
80 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070081 };
Carl Shapiro1fb86202011-06-27 17:43:13 -070082
jeffhao10037c82012-01-23 15:06:23 -080083 // Map item type codes.
84 enum {
85 kDexTypeHeaderItem = 0x0000,
86 kDexTypeStringIdItem = 0x0001,
87 kDexTypeTypeIdItem = 0x0002,
88 kDexTypeProtoIdItem = 0x0003,
89 kDexTypeFieldIdItem = 0x0004,
90 kDexTypeMethodIdItem = 0x0005,
91 kDexTypeClassDefItem = 0x0006,
92 kDexTypeMapList = 0x1000,
93 kDexTypeTypeList = 0x1001,
94 kDexTypeAnnotationSetRefList = 0x1002,
95 kDexTypeAnnotationSetItem = 0x1003,
96 kDexTypeClassDataItem = 0x2000,
97 kDexTypeCodeItem = 0x2001,
98 kDexTypeStringDataItem = 0x2002,
99 kDexTypeDebugInfoItem = 0x2003,
100 kDexTypeAnnotationItem = 0x2004,
101 kDexTypeEncodedArrayItem = 0x2005,
102 kDexTypeAnnotationsDirectoryItem = 0x2006,
103 };
104
105 struct MapItem {
106 uint16_t type_;
107 uint16_t unused_;
108 uint32_t size_;
109 uint32_t offset_;
110 private:
111 DISALLOW_COPY_AND_ASSIGN(MapItem);
112 };
113
114 struct MapList {
115 uint32_t size_;
116 MapItem list_[1];
117 private:
118 DISALLOW_COPY_AND_ASSIGN(MapList);
119 };
120
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700121 // Raw string_id_item.
122 struct StringId {
123 uint32_t string_data_off_; // offset in bytes from the base address
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700124 private:
125 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700126 };
127
128 // Raw type_id_item.
129 struct TypeId {
130 uint32_t descriptor_idx_; // index into string_ids
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700131 private:
132 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700133 };
134
135 // Raw field_id_item.
136 struct FieldId {
Ian Rogers0571d352011-11-03 19:51:38 -0700137 uint16_t class_idx_; // index into type_ids_ array for defining class
138 uint16_t type_idx_; // index into type_ids_ array for field type
139 uint32_t name_idx_; // index into string_ids_ array for field name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700140 private:
141 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700142 };
143
144 // Raw method_id_item.
145 struct MethodId {
Ian Rogers0571d352011-11-03 19:51:38 -0700146 uint16_t class_idx_; // index into type_ids_ array for defining class
147 uint16_t proto_idx_; // index into proto_ids_ array for method prototype
148 uint32_t name_idx_; // index into string_ids_ array for method name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700149 private:
150 DISALLOW_COPY_AND_ASSIGN(MethodId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700151 };
152
153 // Raw proto_id_item.
154 struct ProtoId {
Ian Rogers0571d352011-11-03 19:51:38 -0700155 uint32_t shorty_idx_; // index into string_ids array for shorty descriptor
156 uint16_t return_type_idx_; // index into type_ids array for return type
157 uint16_t pad_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700158 uint32_t parameters_off_; // file offset to type_list for parameter types
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700159 private:
160 DISALLOW_COPY_AND_ASSIGN(ProtoId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700161 };
162
163 // Raw class_def_item.
164 struct ClassDef {
Ian Rogers0571d352011-11-03 19:51:38 -0700165 uint16_t class_idx_; // index into type_ids_ array for this class
166 uint16_t pad1_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700167 uint32_t access_flags_;
Ian Rogers0571d352011-11-03 19:51:38 -0700168 uint16_t superclass_idx_; // index into type_ids_ array for superclass
169 uint16_t pad2_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700170 uint32_t interfaces_off_; // file offset to TypeList
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700171 uint32_t source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700172 uint32_t annotations_off_; // file offset to annotations_directory_item
173 uint32_t class_data_off_; // file offset to class_data_item
174 uint32_t static_values_off_; // file offset to EncodedArray
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700175 private:
176 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700177 };
178
179 // Raw type_item.
180 struct TypeItem {
181 uint16_t type_idx_; // index into type_ids section
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700182 private:
183 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700184 };
185
186 // Raw type_list.
187 class TypeList {
188 public:
189 uint32_t Size() const {
190 return size_;
191 }
192
193 const TypeItem& GetTypeItem(uint32_t idx) const {
194 CHECK_LT(idx, this->size_);
195 return this->list_[idx];
196 }
197
198 private:
199 uint32_t size_; // size of the list, in entries
200 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700201 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700202 };
203
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700204 // Raw code_item.
205 struct CodeItem {
206 uint16_t registers_size_;
207 uint16_t ins_size_;
208 uint16_t outs_size_;
209 uint16_t tries_size_;
210 uint32_t debug_info_off_; // file offset to debug info stream
Ian Rogersd81871c2011-10-03 13:57:23 -0700211 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700212 uint16_t insns_[1];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700213 private:
214 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700215 };
216
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700217 // Raw try_item.
218 struct TryItem {
219 uint32_t start_addr_;
220 uint16_t insn_count_;
221 uint16_t handler_off_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700222 private:
223 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700224 };
225
jeffhao10037c82012-01-23 15:06:23 -0800226 // Annotation constants.
227 enum {
228 kDexVisibilityBuild = 0x00, /* annotation visibility */
229 kDexVisibilityRuntime = 0x01,
230 kDexVisibilitySystem = 0x02,
231
232 kDexAnnotationByte = 0x00,
233 kDexAnnotationShort = 0x02,
234 kDexAnnotationChar = 0x03,
235 kDexAnnotationInt = 0x04,
236 kDexAnnotationLong = 0x06,
237 kDexAnnotationFloat = 0x10,
238 kDexAnnotationDouble = 0x11,
239 kDexAnnotationString = 0x17,
240 kDexAnnotationType = 0x18,
241 kDexAnnotationField = 0x19,
242 kDexAnnotationMethod = 0x1a,
243 kDexAnnotationEnum = 0x1b,
244 kDexAnnotationArray = 0x1c,
245 kDexAnnotationAnnotation = 0x1d,
246 kDexAnnotationNull = 0x1e,
247 kDexAnnotationBoolean = 0x1f,
248
249 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
250 kDexAnnotationValueArgShift = 5,
251 };
252
253 struct AnnotationsDirectoryItem {
254 uint32_t class_annotations_off_;
255 uint32_t fields_size_;
256 uint32_t methods_size_;
257 uint32_t parameters_size_;
258 private:
259 DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
260 };
261
262 struct FieldAnnotationsItem {
263 uint32_t field_idx_;
264 uint32_t annotations_off_;
265 private:
266 DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem);
267 };
268
269 struct MethodAnnotationsItem {
270 uint32_t method_idx_;
271 uint32_t annotations_off_;
272 private:
273 DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem);
274 };
275
276 struct ParameterAnnotationsItem {
277 uint32_t method_idx_;
278 uint32_t annotations_off_;
279 private:
280 DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem);
281 };
282
283 struct AnnotationSetRefItem {
284 uint32_t annotations_off_;
285 private:
286 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem);
287 };
288
289 struct AnnotationSetRefList {
290 uint32_t size_;
291 AnnotationSetRefItem list_[1];
292 private:
293 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList);
294 };
295
296 struct AnnotationSetItem {
297 uint32_t size_;
298 uint32_t entries_[1];
299 private:
300 DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
301 };
302
303 struct AnnotationItem {
304 uint8_t visibility_;
305 uint8_t annotation_[1];
306 private:
307 DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
308 };
309
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700310 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
311 typedef std::vector<const DexFile*> ClassPath;
312
313 // Search a collection of DexFiles for a descriptor
314 static ClassPathEntry FindInClassPath(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700315 const ClassPath& class_path);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700316
Brian Carlstrom78128a62011-09-15 17:21:19 -0700317 // Opens a collection of .dex files
Brian Carlstromae826982011-11-09 01:33:42 -0800318 static void OpenDexFiles(const std::vector<const char*>& dex_filenames,
Brian Carlstrom78128a62011-09-15 17:21:19 -0700319 std::vector<const DexFile*>& dex_files,
320 const std::string& strip_location_prefix);
321
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700322 // Opens .dex file, guessing the container format based on file extension
Brian Carlstrom16192862011-09-12 17:50:06 -0700323 static const DexFile* Open(const std::string& filename,
324 const std::string& strip_location_prefix);
jeffhao262bf462011-10-20 18:36:32 -0700325
Brian Carlstrom89521892011-12-07 22:05:07 -0800326 // Opens .dex file, backed by existing memory
327 static const DexFile* Open(const uint8_t* base, size_t length, const std::string& location) {
328 return OpenMemory(base, length, location, NULL);
329 }
330
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800331 // Opens .dex file from the classes.dex in a zip archive
332 static const DexFile* Open(const ZipArchive& zip_archive, const std::string& location);
333
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700334 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700335 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700336
Brian Carlstroma663ea52011-08-19 23:33:41 -0700337 const std::string& GetLocation() const {
338 return location_;
339 }
340
Jesse Wilson6bf19152011-09-29 13:12:33 -0400341 // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file.
342 // Used by managed code to implement annotations.
343 jobject GetDexObject(JNIEnv* env) const;
344
Brian Carlstroma663ea52011-08-19 23:33:41 -0700345 const Header& GetHeader() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800346 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700347 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700348 }
349
Ian Rogers0571d352011-11-03 19:51:38 -0700350 // Decode the dex magic version
Ian Rogersd81871c2011-10-03 13:57:23 -0700351 uint32_t GetVersion() const;
352
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800353 // Returns true if the byte string points to the magic value.
354 static bool IsMagicValid(const byte* magic);
355
356 // Returns true if the byte string after the magic is the correct value.
357 static bool IsVersionValid(const byte* magic);
358
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700359 // Returns the number of string identifiers in the .dex file.
360 size_t NumStringIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800361 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700362 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700363 }
364
Ian Rogers0571d352011-11-03 19:51:38 -0700365 // Returns the StringId at the specified index.
366 const StringId& GetStringId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800367 CHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700368 return string_ids_[idx];
369 }
370
371 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800372 CHECK_GE(&string_id, string_ids_) << GetLocation();
373 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700374 return &string_id - string_ids_;
375 }
376
377 int32_t GetStringLength(const StringId& string_id) const;
378
379 // Returns a pointer to the UTF-8 string data referred to by the given string_id.
380 const char* GetStringDataAndLength(const StringId& string_id, int32_t* length) const;
381
382 const char* GetStringData(const StringId& string_id) const {
383 int32_t length;
384 return GetStringDataAndLength(string_id, &length);
385 }
386
387 // return the UTF-8 encoded string with the specified string_id index
388 const char* StringDataAndLengthByIdx(uint32_t idx, int32_t* unicode_length) const {
389 if (idx == kDexNoIndex) {
390 *unicode_length = 0;
391 return NULL;
392 }
393 const StringId& string_id = GetStringId(idx);
394 return GetStringDataAndLength(string_id, unicode_length);
395 }
396
397 const char* StringDataByIdx(uint32_t idx) const {
398 int32_t unicode_length;
399 return StringDataAndLengthByIdx(idx, &unicode_length);
400 }
401
402 // Looks up a string id for a given string
403 const StringId* FindStringId(const std::string& string) const;
404
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700405 // Returns the number of type identifiers in the .dex file.
406 size_t NumTypeIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800407 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700408 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700409 }
410
Ian Rogers0571d352011-11-03 19:51:38 -0700411 // Returns the TypeId at the specified index.
412 const TypeId& GetTypeId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800413 CHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700414 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700415 }
416
Ian Rogers0571d352011-11-03 19:51:38 -0700417 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800418 CHECK_GE(&type_id, type_ids_) << GetLocation();
419 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700420 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800421 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700422 return static_cast<uint16_t>(result);
423 }
424
425 // Get the descriptor string associated with a given type index.
426 const char* StringByTypeIdx(uint32_t idx, int32_t* unicode_length) const {
427 const TypeId& type_id = GetTypeId(idx);
428 return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length);
429 }
430
431 const char* StringByTypeIdx(uint32_t idx) const {
432 const TypeId& type_id = GetTypeId(idx);
433 return StringDataByIdx(type_id.descriptor_idx_);
434 }
435
436 // Returns the type descriptor string of a type id.
437 const char* GetTypeDescriptor(const TypeId& type_id) const {
438 return StringDataByIdx(type_id.descriptor_idx_);
439 }
440
441 // Looks up a type for the given string index
442 const TypeId* FindTypeId(uint32_t string_idx) const;
443
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700444 // Returns the number of field identifiers in the .dex file.
445 size_t NumFieldIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800446 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700447 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700448 }
449
Ian Rogers0571d352011-11-03 19:51:38 -0700450 // Returns the FieldId at the specified index.
451 const FieldId& GetFieldId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800452 CHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700453 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700454 }
455
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800456 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800457 CHECK_GE(&field_id, field_ids_) << GetLocation();
458 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800459 return &field_id - field_ids_;
460 }
461
462 // Looks up a field by its declaring class, name and type
463 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
464 const DexFile::StringId& name,
465 const DexFile::TypeId& type) const;
466
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700467 // Returns the declaring class descriptor string of a field id.
468 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700469 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
470 return GetTypeDescriptor(type_id);
471 }
472
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700473 // Returns the class descriptor string of a field id.
474 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
475 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
476 return GetTypeDescriptor(type_id);
477 }
478
Brian Carlstromb9edb842011-08-28 16:31:06 -0700479 // Returns the name of a field id.
480 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700481 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700482 }
483
Ian Rogers0571d352011-11-03 19:51:38 -0700484 // Returns the number of method identifiers in the .dex file.
485 size_t NumMethodIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800486 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700487 return header_->method_ids_size_;
488 }
489
490 // Returns the MethodId at the specified index.
491 const MethodId& GetMethodId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800492 CHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700493 return method_ids_[idx];
494 }
495
496 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800497 CHECK_GE(&method_id, method_ids_) << GetLocation();
498 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700499 return &method_id - method_ids_;
500 }
501
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800502 // Looks up a method by its declaring class, name and proto_id
503 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
504 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700505 const DexFile::ProtoId& signature) const;
506
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700507 // Returns the declaring class descriptor string of a method id.
508 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700509 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
510 return GetTypeDescriptor(type_id);
511 }
512
jeffhao98eacac2011-09-14 16:11:53 -0700513 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700514 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
515 return GetProtoId(method_id.proto_idx_);
516 }
517
518 // Returns the signature of a method id.
519 const std::string GetMethodSignature(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700520 return CreateMethodSignature(method_id.proto_idx_, NULL);
jeffhao98eacac2011-09-14 16:11:53 -0700521 }
522
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700523 // Returns the name of a method id.
524 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700525 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700526 }
527
Ian Rogers0571d352011-11-03 19:51:38 -0700528 // Returns the shorty of a method id.
529 const char* GetMethodShorty(const MethodId& method_id) const {
530 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700531 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800532 const char* GetMethodShorty(const MethodId& method_id, int32_t* length) const {
533 return StringDataAndLengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
534 }
Ian Rogers0571d352011-11-03 19:51:38 -0700535 // Returns the number of class definitions in the .dex file.
536 size_t NumClassDefs() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800537 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700538 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700539 }
540
541 // Returns the ClassDef at the specified index.
542 const ClassDef& GetClassDef(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800543 CHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700544 return class_defs_[idx];
545 }
546
Ian Rogers0571d352011-11-03 19:51:38 -0700547 uint32_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800548 CHECK_GE(&class_def, class_defs_) << GetLocation();
549 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700550 return &class_def - class_defs_;
551 }
552
553 // Returns the class descriptor string of a class definition.
554 const char* GetClassDescriptor(const ClassDef& class_def) const {
555 return StringByTypeIdx(class_def.class_idx_);
556 }
557
558 // Looks up a class definition by its class descriptor.
559 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
560
561 // Looks up a class definition index by its class descriptor.
562 bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const;
563
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700564 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
565 if (class_def.interfaces_off_ == 0) {
566 return NULL;
567 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800568 const byte* addr = begin_ + class_def.interfaces_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700569 return reinterpret_cast<const TypeList*>(addr);
570 }
571 }
572
Ian Rogers0571d352011-11-03 19:51:38 -0700573 // Returns a pointer to the raw memory mapped class_data_item
574 const byte* GetClassData(const ClassDef& class_def) const {
575 if (class_def.class_data_off_ == 0) {
576 return NULL;
577 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800578 return begin_ + class_def.class_data_off_;
Ian Rogers0571d352011-11-03 19:51:38 -0700579 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700580 }
581
Ian Rogers0571d352011-11-03 19:51:38 -0700582 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800583 const CodeItem* GetCodeItem(const uint32_t code_off) const {
584 if (code_off == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700585 return NULL; // native or abstract method
586 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800587 const byte* addr = begin_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700588 return reinterpret_cast<const CodeItem*>(addr);
589 }
590 }
591
Ian Rogers0571d352011-11-03 19:51:38 -0700592 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
593 return StringByTypeIdx(proto_id.return_type_idx_);
594 }
595
596 // Returns the number of prototype identifiers in the .dex file.
597 size_t NumProtoIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800598 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700599 return header_->proto_ids_size_;
600 }
601
602 // Returns the ProtoId at the specified index.
603 const ProtoId& GetProtoId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800604 CHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700605 return proto_ids_[idx];
606 }
607
608 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800609 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
610 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700611 return &proto_id - proto_ids_;
612 }
613
614 // Looks up a proto id for a given return type and signature type list
615 const ProtoId* FindProtoId(uint16_t return_type_id,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800616 const std::vector<uint16_t>& signature_type_idxs_) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700617
618 // Given a signature place the type ids into the given vector, returns true on success
619 bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
620 const std::string& signature) const;
621
622 // Given a proto_idx decode the type list and return type into a method signature
623 std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const;
624
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700625 // Returns the short form method descriptor for the given prototype.
626 const char* GetShorty(uint32_t proto_idx) const {
627 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700628 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700629 }
630
631 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
632 if (proto_id.parameters_off_ == 0) {
633 return NULL;
634 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800635 const byte* addr = begin_ + proto_id.parameters_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700636 return reinterpret_cast<const TypeList*>(addr);
637 }
638 }
639
Ian Rogers0571d352011-11-03 19:51:38 -0700640 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700641 if (class_def.static_values_off_ == 0) {
642 return 0;
643 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800644 return begin_ + class_def.static_values_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700645 }
646 }
647
Ian Rogers0571d352011-11-03 19:51:38 -0700648 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700650 return reinterpret_cast<const TryItem*>
651 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
652 }
653
654 // Get the base of the encoded data for the given DexCode.
Ian Rogers0571d352011-11-03 19:51:38 -0700655 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
656 const byte* handler_data =
657 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700658 return handler_data + offset;
659 }
660
661 // Find the handler associated with a given address, if any.
662 // Initializes the given iterator and returns true if a match is
663 // found. Returns end if there is no applicable handler.
Ian Rogers0571d352011-11-03 19:51:38 -0700664 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
665 uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700666
Shih-wei Liao195487c2011-08-20 13:29:04 -0700667 // Get the pointer to the start of the debugging data
Ian Rogers0571d352011-11-03 19:51:38 -0700668 const byte* GetDebugInfoStream(const CodeItem* code_item) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700669 if (code_item->debug_info_off_ == 0) {
670 return NULL;
671 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800672 return begin_ + code_item->debug_info_off_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700673 }
674 }
675
676 // Callback for "new position table entry".
677 // Returning true causes the decoder to stop early.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700678 typedef bool (*DexDebugNewPositionCb)(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700679
680 // Callback for "new locals table entry". "signature" is an empty string
681 // if no signature is available for an entry.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700682 typedef void (*DexDebugNewLocalCb)(void* cnxt, uint16_t reg,
Shih-wei Liao195487c2011-08-20 13:29:04 -0700683 uint32_t startAddress,
684 uint32_t endAddress,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700685 const char* name,
686 const char* descriptor,
687 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700688
Ian Rogers0571d352011-11-03 19:51:38 -0700689 static bool LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700690
691 // Debug info opcodes and constants
692 enum {
693 DBG_END_SEQUENCE = 0x00,
694 DBG_ADVANCE_PC = 0x01,
695 DBG_ADVANCE_LINE = 0x02,
696 DBG_START_LOCAL = 0x03,
697 DBG_START_LOCAL_EXTENDED = 0x04,
698 DBG_END_LOCAL = 0x05,
699 DBG_RESTART_LOCAL = 0x06,
700 DBG_SET_PROLOGUE_END = 0x07,
701 DBG_SET_EPILOGUE_BEGIN = 0x08,
702 DBG_SET_FILE = 0x09,
703 DBG_FIRST_SPECIAL = 0x0a,
704 DBG_LINE_BASE = -4,
705 DBG_LINE_RANGE = 15,
706 };
707
708 struct LocalInfo {
Ian Rogers0571d352011-11-03 19:51:38 -0700709 LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0),
710 is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700711
Ian Rogers0571d352011-11-03 19:51:38 -0700712 const char* name_; // E.g., list
713 const char* descriptor_; // E.g., Ljava/util/LinkedList;
714 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
715 uint16_t start_address_; // PC location where the local is first defined.
716 bool is_live_; // Is the local defined and live.
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700717
718 private:
719 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700720 };
721
722 struct LineNumFromPcContext {
723 LineNumFromPcContext(uint32_t address, uint32_t line_num) :
724 address_(address), line_num_(line_num) {}
725 uint32_t address_;
726 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700727 private:
728 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700729 };
730
Brian Carlstrom78128a62011-09-15 17:21:19 -0700731 void InvokeLocalCbIfLive(void* cnxt, int reg, uint32_t end_address,
732 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700733 if (local_cb != NULL && local_in_reg[reg].is_live_) {
734 local_cb(cnxt, reg, local_in_reg[reg].start_address_, end_address,
Elliott Hughesdbb40792011-11-18 17:05:22 -0800735 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
736 local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : "");
Shih-wei Liao195487c2011-08-20 13:29:04 -0700737 }
738 }
739
740 // Determine the source file line number based on the program counter.
741 // "pc" is an offset, in 16-bit units, from the start of the method's code.
742 //
743 // Returns -1 if no match was found (possibly because the source files were
744 // compiled without "-g", so no line number information is present).
745 // Returns -2 for native methods (as expected in exception traces).
746 //
747 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Ian Rogers0571d352011-11-03 19:51:38 -0700748 int32_t GetLineNumFromPC(const Method* method, uint32_t rel_pc) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700749
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800750 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700751 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
752 void* cnxt) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700753
Ian Rogers0571d352011-11-03 19:51:38 -0700754 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700755 if (class_def.source_file_idx_ == 0xffffffff) {
756 return NULL;
757 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700758 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700759 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700760 }
761
jeffhaob4df5142011-09-19 20:25:32 -0700762 void ChangePermissions(int prot) const;
763
Carl Shapiro1fb86202011-06-27 17:43:13 -0700764 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700765
766 // Opens a .dex file
767 static const DexFile* OpenFile(const std::string& filename,
768 const std::string& original_location,
769 const std::string& strip_location_prefix);
770
771 // Opens a dex file from within a .jar, .zip, or .apk file
772 static const DexFile* OpenZip(const std::string& filename,
773 const std::string& strip_location_prefix);
774
Brian Carlstrom89521892011-12-07 22:05:07 -0800775 // Opens a .dex file at the given address backed by a MemMap
776 static const DexFile* OpenMemory(const std::string& location,
777 MemMap* mem_map) {
Ian Rogers30fab402012-01-23 15:43:46 -0800778 return OpenMemory(mem_map->Begin(),
779 mem_map->Size(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800780 location,
781 mem_map);
782 }
783
784 // Opens a .dex file at the given address, optionally backed by a MemMap
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700785 static const DexFile* OpenMemory(const byte* dex_file,
786 size_t length,
787 const std::string& location,
788 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700789
Brian Carlstrom89521892011-12-07 22:05:07 -0800790 DexFile(const byte* base, size_t length, const std::string& location, MemMap* mem_map)
Ian Rogers30fab402012-01-23 15:43:46 -0800791 : begin_(base),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700792 length_(length),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700793 location_(location),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700794 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400795 dex_object_lock_("a dex_object_lock_"),
796 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700797 header_(0),
798 string_ids_(0),
799 type_ids_(0),
800 field_ids_(0),
801 method_ids_(0),
802 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700803 class_defs_(0) {
Ian Rogers30fab402012-01-23 15:43:46 -0800804 CHECK(begin_ != NULL) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800805 CHECK_GT(length_, 0U) << GetLocation();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700806 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700807
808 // Top-level initializer that calls other Init methods.
809 bool Init();
810
811 // Caches pointers into to the various file sections.
812 void InitMembers();
813
814 // Builds the index of descriptors to class definitions.
815 void InitIndex();
816
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800817 // Returns true if the header magic and version numbers are of the expected values.
jeffhao10037c82012-01-23 15:06:23 -0800818 bool CheckMagicAndVersion() const;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700819
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800820 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes03181a82011-11-17 17:22:21 -0800821 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
822 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const;
823
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800824 // The index of descriptors to class definition indexes (as opposed to type id indexes)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700825 typedef std::map<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700826 Index index_;
827
828 // The base address of the memory mapping.
Ian Rogers30fab402012-01-23 15:43:46 -0800829 const byte* begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700830
831 // The size of the underlying memory allocation in bytes.
832 size_t length_;
833
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700834 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700835 //
836 // The ClassLinker will use this to match DexFiles the boot class
837 // path to DexCache::GetLocation when loading from an image.
838 const std::string location_;
839
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700840 // Manages the underlying memory allocation.
841 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700842
Jesse Wilson6bf19152011-09-29 13:12:33 -0400843 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
844 mutable Mutex dex_object_lock_;
845 mutable jobject dex_object_;
846
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700847 // Points to the header section.
848 const Header* header_;
849
850 // Points to the base of the string identifier list.
851 const StringId* string_ids_;
852
853 // Points to the base of the type identifier list.
854 const TypeId* type_ids_;
855
856 // Points to the base of the field identifier list.
857 const FieldId* field_ids_;
858
859 // Points to the base of the method identifier list.
860 const MethodId* method_ids_;
861
862 // Points to the base of the prototype identifier list.
863 const ProtoId* proto_ids_;
864
865 // Points to the base of the class definition list.
866 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700867};
868
Ian Rogers0571d352011-11-03 19:51:38 -0700869// Iterate over a dex file's ProtoId's paramters
870class DexFileParameterIterator {
871 public:
872 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
873 : dex_file_(dex_file), size_(0), pos_(0) {
874 type_list_ = dex_file_.GetProtoParameters(proto_id);
875 if (type_list_ != NULL) {
876 size_ = type_list_->Size();
877 }
878 }
879 bool HasNext() const { return pos_ < size_; }
880 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800881 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -0700882 return type_list_->GetTypeItem(pos_).type_idx_;
883 }
884 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800885 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -0700886 }
887 private:
888 const DexFile& dex_file_;
889 const DexFile::TypeList* type_list_;
890 uint32_t size_;
891 uint32_t pos_;
892 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
893};
894
895// Iterate and decode class_data_item
896class ClassDataItemIterator {
897 public:
898 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
899 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
900 ReadClassDataHeader();
901 if (EndOfInstanceFieldsPos() > 0) {
902 ReadClassDataField();
903 } else if (EndOfVirtualMethodsPos() > 0) {
904 ReadClassDataMethod();
905 }
906 }
907 uint32_t NumStaticFields() const {
908 return header_.static_fields_size_;
909 }
910 uint32_t NumInstanceFields() const {
911 return header_.instance_fields_size_;
912 }
913 uint32_t NumDirectMethods() const {
914 return header_.direct_methods_size_;
915 }
916 uint32_t NumVirtualMethods() const {
917 return header_.virtual_methods_size_;
918 }
919 bool HasNextStaticField() const {
920 return pos_ < EndOfStaticFieldsPos();
921 }
922 bool HasNextInstanceField() const {
923 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
924 }
925 bool HasNextDirectMethod() const {
926 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
927 }
928 bool HasNextVirtualMethod() const {
929 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
930 }
931 bool HasNext() const {
932 return pos_ < EndOfVirtualMethodsPos();
933 }
934 void Next() {
935 pos_++;
936 if (pos_ < EndOfStaticFieldsPos()) {
937 last_idx_ = GetMemberIndex();
938 ReadClassDataField();
939 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
940 last_idx_ = 0; // transition to next array, reset last index
941 ReadClassDataField();
942 } else if (pos_ < EndOfInstanceFieldsPos()) {
943 last_idx_ = GetMemberIndex();
944 ReadClassDataField();
945 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
946 last_idx_ = 0; // transition to next array, reset last index
947 ReadClassDataMethod();
948 } else if (pos_ < EndOfDirectMethodsPos()) {
949 last_idx_ = GetMemberIndex();
950 ReadClassDataMethod();
951 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
952 last_idx_ = 0; // transition to next array, reset last index
953 ReadClassDataMethod();
954 } else if (pos_ < EndOfVirtualMethodsPos()) {
955 last_idx_ = GetMemberIndex();
956 ReadClassDataMethod();
957 } else {
958 DCHECK(!HasNext());
959 }
960 }
961 uint32_t GetMemberIndex() const {
962 if (pos_ < EndOfInstanceFieldsPos()) {
963 return last_idx_ + field_.field_idx_delta_;
964 } else {
965 CHECK_LT(pos_, EndOfVirtualMethodsPos());
966 return last_idx_ + method_.method_idx_delta_;
967 }
968 }
969 uint32_t GetMemberAccessFlags() const {
970 if (pos_ < EndOfInstanceFieldsPos()) {
971 return field_.access_flags_;
972 } else {
973 CHECK_LT(pos_, EndOfVirtualMethodsPos());
974 return method_.access_flags_;
975 }
976 }
977 const DexFile::CodeItem* GetMethodCodeItem() const {
978 return dex_file_.GetCodeItem(method_.code_off_);
979 }
980 uint32_t GetMethodCodeItemOffset() const {
981 return method_.code_off_;
982 }
jeffhao10037c82012-01-23 15:06:23 -0800983 const byte* EndDataPointer() const {
984 CHECK(!HasNext());
985 return ptr_pos_;
986 }
Ian Rogers0571d352011-11-03 19:51:38 -0700987 private:
988 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
989 // header for a class_data_item
990 struct ClassDataHeader {
991 uint32_t static_fields_size_; // the number of static fields
992 uint32_t instance_fields_size_; // the number of instance fields
993 uint32_t direct_methods_size_; // the number of direct methods
994 uint32_t virtual_methods_size_; // the number of virtual methods
995 } header_;
996
997 // Read and decode header from a class_data_item stream into header
998 void ReadClassDataHeader();
999
1000 uint32_t EndOfStaticFieldsPos() const {
1001 return header_.static_fields_size_;
1002 }
1003 uint32_t EndOfInstanceFieldsPos() const {
1004 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
1005 }
1006 uint32_t EndOfDirectMethodsPos() const {
1007 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
1008 }
1009 uint32_t EndOfVirtualMethodsPos() const {
1010 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
1011 }
1012
1013 // A decoded version of the field of a class_data_item
1014 struct ClassDataField {
1015 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
1016 uint32_t access_flags_; // access flags for the field
1017 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
1018 private:
1019 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
1020 } field_;
1021
1022 // Read and decode a field from a class_data_item stream into field
1023 void ReadClassDataField();
1024
1025 // A decoded version of the method of a class_data_item
1026 struct ClassDataMethod {
1027 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
1028 uint32_t access_flags_;
1029 uint32_t code_off_;
1030 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
1031 private:
1032 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
1033 } method_;
1034
1035 // Read and decode a method from a class_data_item stream into method
1036 void ReadClassDataMethod();
1037
1038 const DexFile& dex_file_;
1039 size_t pos_; // integral number of items passed
1040 const byte* ptr_pos_; // pointer into stream of class_data_item
1041 uint32_t last_idx_; // last read field or method index to apply delta to
1042 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
1043};
1044
1045class ClassLinker;
1046class DexCache;
1047class Field;
1048
1049class EncodedStaticFieldValueIterator {
1050 public:
1051 EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache,
1052 ClassLinker* linker, const DexFile::ClassDef& class_def);
1053
1054 void ReadValueToField(Field* field) const;
1055
1056 bool HasNext() { return pos_ < array_size_; }
1057
1058 void Next();
1059 private:
1060 enum ValueType {
1061 kByte = 0x00,
1062 kShort = 0x02,
1063 kChar = 0x03,
1064 kInt = 0x04,
1065 kLong = 0x06,
1066 kFloat = 0x10,
1067 kDouble = 0x11,
1068 kString = 0x17,
1069 kType = 0x18,
1070 kField = 0x19,
1071 kMethod = 0x1a,
1072 kEnum = 0x1b,
1073 kArray = 0x1c,
1074 kAnnotation = 0x1d,
1075 kNull = 0x1e,
1076 kBoolean = 0x1f
1077 };
1078
1079 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
1080 static const byte kEncodedValueArgShift = 5;
1081
1082 const DexFile& dex_file_;
1083 DexCache* dex_cache_; // dex cache to resolve literal objects
1084 ClassLinker* linker_; // linker to resolve literal objects
1085 size_t array_size_; // size of array
1086 size_t pos_; // current position
1087 const byte* ptr_; // pointer into encoded data array
1088 byte type_; // type of current encoded value
1089 jvalue jval_; // value of current encoded value
1090 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1091};
1092
1093class CatchHandlerIterator {
1094 public:
1095 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
1096 explicit CatchHandlerIterator(const byte* handler_data) {
1097 Init(handler_data);
1098 }
1099
1100 uint16_t GetHandlerTypeIndex() const {
1101 return handler_.type_idx_;
1102 }
1103 uint32_t GetHandlerAddress() const {
1104 return handler_.address_;
1105 }
1106 void Next();
1107 bool HasNext() const {
1108 return remaining_count_ != -1 || catch_all_;
1109 }
1110 // End of this set of catch blocks, convenience method to locate next set of catch blocks
1111 const byte* EndDataPointer() const {
1112 CHECK(!HasNext());
1113 return current_data_;
1114 }
1115 private:
1116 void Init(const byte* handler_data);
1117
1118 struct CatchHandlerItem {
1119 uint16_t type_idx_; // type index of the caught exception type
1120 uint32_t address_; // handler address
1121 } handler_;
1122 const byte *current_data_; // the current handler in dex file.
1123 int32_t remaining_count_; // number of handlers not read.
1124 bool catch_all_; // is there a handler that will catch all exceptions in case
1125 // that all typed handler does not match.
1126};
1127
Carl Shapiro1fb86202011-06-27 17:43:13 -07001128} // namespace art
1129
1130#endif // ART_SRC_DEX_FILE_H_