blob: 29ce08700f4e09de5c8a1fc1b3e4febd1901c895 [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
Elliott Hughes0c424cb2011-08-26 10:16:25 -070020#include <string>
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070021#include <vector>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070022
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "globals.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040024#include "jni.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070025#include "logging.h"
Brian Carlstrom33f741e2011-10-03 11:24:05 -070026#include "mem_map.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040027#include "mutex.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070028#include "safe_map.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070029#include "stringpiece.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070030#include "UniquePtr.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];
Brian Carlstrom5b332c82012-02-01 15:02:31 -080057 uint32_t checksum_; // See also location_checksum_
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070058 uint8_t signature_[kSha1DigestSize];
jeffhaof6174e82012-01-31 16:14:17 -080059 uint32_t file_size_; // size of entire file
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070060 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 Carlstrom5b332c82012-02-01 15:02:31 -0800317 // Returns the checksum of a file for comparison with GetLocationChecksum().
318 // For .dex files, this is the header checksum.
319 // For zip files, this is the classes.dex zip entry CRC32 checksum.
320 // Return true if the checksum could be found, false otherwise.
321 static bool GetChecksum(const std::string& filename, uint32_t& checksum);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700322
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700323 // Opens .dex file, guessing the container format based on file extension
Brian Carlstrom16192862011-09-12 17:50:06 -0700324 static const DexFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800325 const std::string& location);
jeffhao262bf462011-10-20 18:36:32 -0700326
Brian Carlstrom89521892011-12-07 22:05:07 -0800327 // Opens .dex file, backed by existing memory
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800328 static const DexFile* Open(const uint8_t* base, size_t size,
329 const std::string& location, uint32_t location_checksum) {
330 return OpenMemory(base, size, location, location_checksum, NULL);
Brian Carlstrom89521892011-12-07 22:05:07 -0800331 }
332
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800333 // Opens .dex file from the classes.dex in a zip archive
334 static const DexFile* Open(const ZipArchive& zip_archive, const std::string& location);
335
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700336 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700337 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700338
Brian Carlstroma663ea52011-08-19 23:33:41 -0700339 const std::string& GetLocation() const {
340 return location_;
341 }
342
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800343 // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header.
344 // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex.
345 uint32_t GetLocationChecksum() const {
346 return location_checksum_;
347 }
348
Jesse Wilson6bf19152011-09-29 13:12:33 -0400349 // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file.
350 // Used by managed code to implement annotations.
351 jobject GetDexObject(JNIEnv* env) const;
352
Brian Carlstroma663ea52011-08-19 23:33:41 -0700353 const Header& GetHeader() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800354 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700355 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700356 }
357
Ian Rogers0571d352011-11-03 19:51:38 -0700358 // Decode the dex magic version
Ian Rogersd81871c2011-10-03 13:57:23 -0700359 uint32_t GetVersion() const;
360
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800361 // Returns true if the byte string points to the magic value.
362 static bool IsMagicValid(const byte* magic);
363
364 // Returns true if the byte string after the magic is the correct value.
365 static bool IsVersionValid(const byte* magic);
366
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700367 // Returns the number of string identifiers in the .dex file.
368 size_t NumStringIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800369 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700370 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700371 }
372
Ian Rogers0571d352011-11-03 19:51:38 -0700373 // Returns the StringId at the specified index.
374 const StringId& GetStringId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800375 CHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700376 return string_ids_[idx];
377 }
378
379 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800380 CHECK_GE(&string_id, string_ids_) << GetLocation();
381 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700382 return &string_id - string_ids_;
383 }
384
385 int32_t GetStringLength(const StringId& string_id) const;
386
387 // Returns a pointer to the UTF-8 string data referred to by the given string_id.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800388 const char* GetStringDataAndLength(const StringId& string_id, uint32_t* length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700389
390 const char* GetStringData(const StringId& string_id) const {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800391 uint32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700392 return GetStringDataAndLength(string_id, &length);
393 }
394
395 // return the UTF-8 encoded string with the specified string_id index
Elliott Hughes45651fd2012-02-21 15:48:20 -0800396 const char* StringDataAndLengthByIdx(uint32_t idx, uint32_t* unicode_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700397 if (idx == kDexNoIndex) {
398 *unicode_length = 0;
399 return NULL;
400 }
401 const StringId& string_id = GetStringId(idx);
402 return GetStringDataAndLength(string_id, unicode_length);
403 }
404
405 const char* StringDataByIdx(uint32_t idx) const {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800406 uint32_t unicode_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700407 return StringDataAndLengthByIdx(idx, &unicode_length);
408 }
409
410 // Looks up a string id for a given string
411 const StringId* FindStringId(const std::string& string) const;
412
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700413 // Returns the number of type identifiers in the .dex file.
414 size_t NumTypeIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800415 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700416 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700417 }
418
Ian Rogers0571d352011-11-03 19:51:38 -0700419 // Returns the TypeId at the specified index.
420 const TypeId& GetTypeId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800421 CHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700422 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700423 }
424
Ian Rogers0571d352011-11-03 19:51:38 -0700425 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800426 CHECK_GE(&type_id, type_ids_) << GetLocation();
427 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700428 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800429 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700430 return static_cast<uint16_t>(result);
431 }
432
433 // Get the descriptor string associated with a given type index.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800434 const char* StringByTypeIdx(uint32_t idx, uint32_t* unicode_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700435 const TypeId& type_id = GetTypeId(idx);
436 return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length);
437 }
438
439 const char* StringByTypeIdx(uint32_t idx) const {
440 const TypeId& type_id = GetTypeId(idx);
441 return StringDataByIdx(type_id.descriptor_idx_);
442 }
443
444 // Returns the type descriptor string of a type id.
445 const char* GetTypeDescriptor(const TypeId& type_id) const {
446 return StringDataByIdx(type_id.descriptor_idx_);
447 }
448
449 // Looks up a type for the given string index
450 const TypeId* FindTypeId(uint32_t string_idx) const;
451
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700452 // Returns the number of field identifiers in the .dex file.
453 size_t NumFieldIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800454 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700455 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700456 }
457
Ian Rogers0571d352011-11-03 19:51:38 -0700458 // Returns the FieldId at the specified index.
459 const FieldId& GetFieldId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800460 CHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700461 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700462 }
463
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800464 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800465 CHECK_GE(&field_id, field_ids_) << GetLocation();
466 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800467 return &field_id - field_ids_;
468 }
469
470 // Looks up a field by its declaring class, name and type
471 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
472 const DexFile::StringId& name,
473 const DexFile::TypeId& type) const;
474
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700475 // Returns the declaring class descriptor string of a field id.
476 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700477 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
478 return GetTypeDescriptor(type_id);
479 }
480
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700481 // Returns the class descriptor string of a field id.
482 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
483 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
484 return GetTypeDescriptor(type_id);
485 }
486
Brian Carlstromb9edb842011-08-28 16:31:06 -0700487 // Returns the name of a field id.
488 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700489 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700490 }
491
Ian Rogers0571d352011-11-03 19:51:38 -0700492 // Returns the number of method identifiers in the .dex file.
493 size_t NumMethodIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800494 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700495 return header_->method_ids_size_;
496 }
497
498 // Returns the MethodId at the specified index.
499 const MethodId& GetMethodId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800500 CHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700501 return method_ids_[idx];
502 }
503
504 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800505 CHECK_GE(&method_id, method_ids_) << GetLocation();
506 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700507 return &method_id - method_ids_;
508 }
509
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800510 // Looks up a method by its declaring class, name and proto_id
511 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
512 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700513 const DexFile::ProtoId& signature) const;
514
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700515 // Returns the declaring class descriptor string of a method id.
516 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700517 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
518 return GetTypeDescriptor(type_id);
519 }
520
jeffhao98eacac2011-09-14 16:11:53 -0700521 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700522 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
523 return GetProtoId(method_id.proto_idx_);
524 }
525
526 // Returns the signature of a method id.
527 const std::string GetMethodSignature(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700528 return CreateMethodSignature(method_id.proto_idx_, NULL);
jeffhao98eacac2011-09-14 16:11:53 -0700529 }
530
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700531 // Returns the name of a method id.
532 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700533 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700534 }
535
Ian Rogers0571d352011-11-03 19:51:38 -0700536 // Returns the shorty of a method id.
537 const char* GetMethodShorty(const MethodId& method_id) const {
538 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700539 }
Elliott Hughes45651fd2012-02-21 15:48:20 -0800540 const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800541 return StringDataAndLengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
542 }
Ian Rogers0571d352011-11-03 19:51:38 -0700543 // Returns the number of class definitions in the .dex file.
544 size_t NumClassDefs() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800545 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700546 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700547 }
548
549 // Returns the ClassDef at the specified index.
550 const ClassDef& GetClassDef(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800551 CHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700552 return class_defs_[idx];
553 }
554
Ian Rogers0571d352011-11-03 19:51:38 -0700555 uint32_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800556 CHECK_GE(&class_def, class_defs_) << GetLocation();
557 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700558 return &class_def - class_defs_;
559 }
560
561 // Returns the class descriptor string of a class definition.
562 const char* GetClassDescriptor(const ClassDef& class_def) const {
563 return StringByTypeIdx(class_def.class_idx_);
564 }
565
566 // Looks up a class definition by its class descriptor.
567 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
568
569 // Looks up a class definition index by its class descriptor.
570 bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const;
571
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700572 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
573 if (class_def.interfaces_off_ == 0) {
574 return NULL;
575 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800576 const byte* addr = begin_ + class_def.interfaces_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700577 return reinterpret_cast<const TypeList*>(addr);
578 }
579 }
580
Ian Rogers0571d352011-11-03 19:51:38 -0700581 // Returns a pointer to the raw memory mapped class_data_item
582 const byte* GetClassData(const ClassDef& class_def) const {
583 if (class_def.class_data_off_ == 0) {
584 return NULL;
585 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800586 return begin_ + class_def.class_data_off_;
Ian Rogers0571d352011-11-03 19:51:38 -0700587 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700588 }
589
Ian Rogers0571d352011-11-03 19:51:38 -0700590 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800591 const CodeItem* GetCodeItem(const uint32_t code_off) const {
592 if (code_off == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700593 return NULL; // native or abstract method
594 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800595 const byte* addr = begin_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700596 return reinterpret_cast<const CodeItem*>(addr);
597 }
598 }
599
Ian Rogers0571d352011-11-03 19:51:38 -0700600 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
601 return StringByTypeIdx(proto_id.return_type_idx_);
602 }
603
604 // Returns the number of prototype identifiers in the .dex file.
605 size_t NumProtoIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800606 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700607 return header_->proto_ids_size_;
608 }
609
610 // Returns the ProtoId at the specified index.
611 const ProtoId& GetProtoId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800612 CHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700613 return proto_ids_[idx];
614 }
615
616 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800617 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
618 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700619 return &proto_id - proto_ids_;
620 }
621
622 // Looks up a proto id for a given return type and signature type list
623 const ProtoId* FindProtoId(uint16_t return_type_id,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800624 const std::vector<uint16_t>& signature_type_idxs_) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700625
626 // Given a signature place the type ids into the given vector, returns true on success
627 bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
628 const std::string& signature) const;
629
630 // Given a proto_idx decode the type list and return type into a method signature
631 std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const;
632
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700633 // Returns the short form method descriptor for the given prototype.
634 const char* GetShorty(uint32_t proto_idx) const {
635 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700636 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700637 }
638
639 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
640 if (proto_id.parameters_off_ == 0) {
641 return NULL;
642 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800643 const byte* addr = begin_ + proto_id.parameters_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700644 return reinterpret_cast<const TypeList*>(addr);
645 }
646 }
647
Ian Rogers0571d352011-11-03 19:51:38 -0700648 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700649 if (class_def.static_values_off_ == 0) {
650 return 0;
651 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800652 return begin_ + class_def.static_values_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700653 }
654 }
655
Ian Rogers0571d352011-11-03 19:51:38 -0700656 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700658 return reinterpret_cast<const TryItem*>
659 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
660 }
661
662 // Get the base of the encoded data for the given DexCode.
Ian Rogers0571d352011-11-03 19:51:38 -0700663 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
664 const byte* handler_data =
665 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700666 return handler_data + offset;
667 }
668
669 // Find the handler associated with a given address, if any.
670 // Initializes the given iterator and returns true if a match is
671 // found. Returns end if there is no applicable handler.
Ian Rogers0571d352011-11-03 19:51:38 -0700672 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
673 uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700674
Shih-wei Liao195487c2011-08-20 13:29:04 -0700675 // Get the pointer to the start of the debugging data
Ian Rogers0571d352011-11-03 19:51:38 -0700676 const byte* GetDebugInfoStream(const CodeItem* code_item) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700677 if (code_item->debug_info_off_ == 0) {
678 return NULL;
679 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800680 return begin_ + code_item->debug_info_off_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700681 }
682 }
683
684 // Callback for "new position table entry".
685 // Returning true causes the decoder to stop early.
Elliott Hughes2435a572012-02-17 16:07:41 -0800686 typedef bool (*DexDebugNewPositionCb)(void* context, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700687
688 // Callback for "new locals table entry". "signature" is an empty string
689 // if no signature is available for an entry.
Elliott Hughes2435a572012-02-17 16:07:41 -0800690 typedef void (*DexDebugNewLocalCb)(void* context, uint16_t reg,
Shih-wei Liao195487c2011-08-20 13:29:04 -0700691 uint32_t startAddress,
692 uint32_t endAddress,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700693 const char* name,
694 const char* descriptor,
695 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700696
Elliott Hughes2435a572012-02-17 16:07:41 -0800697 static bool LineNumForPcCb(void* context, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700698
699 // Debug info opcodes and constants
700 enum {
701 DBG_END_SEQUENCE = 0x00,
702 DBG_ADVANCE_PC = 0x01,
703 DBG_ADVANCE_LINE = 0x02,
704 DBG_START_LOCAL = 0x03,
705 DBG_START_LOCAL_EXTENDED = 0x04,
706 DBG_END_LOCAL = 0x05,
707 DBG_RESTART_LOCAL = 0x06,
708 DBG_SET_PROLOGUE_END = 0x07,
709 DBG_SET_EPILOGUE_BEGIN = 0x08,
710 DBG_SET_FILE = 0x09,
711 DBG_FIRST_SPECIAL = 0x0a,
712 DBG_LINE_BASE = -4,
713 DBG_LINE_RANGE = 15,
714 };
715
716 struct LocalInfo {
Ian Rogers0571d352011-11-03 19:51:38 -0700717 LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0),
718 is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700719
Ian Rogers0571d352011-11-03 19:51:38 -0700720 const char* name_; // E.g., list
721 const char* descriptor_; // E.g., Ljava/util/LinkedList;
722 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
723 uint16_t start_address_; // PC location where the local is first defined.
724 bool is_live_; // Is the local defined and live.
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700725
726 private:
727 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700728 };
729
730 struct LineNumFromPcContext {
731 LineNumFromPcContext(uint32_t address, uint32_t line_num) :
732 address_(address), line_num_(line_num) {}
733 uint32_t address_;
734 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700735 private:
736 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700737 };
738
Elliott Hughes2435a572012-02-17 16:07:41 -0800739 void InvokeLocalCbIfLive(void* context, int reg, uint32_t end_address,
Brian Carlstrom78128a62011-09-15 17:21:19 -0700740 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700741 if (local_cb != NULL && local_in_reg[reg].is_live_) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800742 local_cb(context, reg, local_in_reg[reg].start_address_, end_address,
Elliott Hughesdbb40792011-11-18 17:05:22 -0800743 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
744 local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : "");
Shih-wei Liao195487c2011-08-20 13:29:04 -0700745 }
746 }
747
748 // Determine the source file line number based on the program counter.
749 // "pc" is an offset, in 16-bit units, from the start of the method's code.
750 //
751 // Returns -1 if no match was found (possibly because the source files were
752 // compiled without "-g", so no line number information is present).
753 // Returns -2 for native methods (as expected in exception traces).
754 //
755 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Ian Rogers0571d352011-11-03 19:51:38 -0700756 int32_t GetLineNumFromPC(const Method* method, uint32_t rel_pc) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700757
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800758 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800759 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
760 void* context) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700761
Ian Rogers0571d352011-11-03 19:51:38 -0700762 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700763 if (class_def.source_file_idx_ == 0xffffffff) {
764 return NULL;
765 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700766 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700767 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700768 }
769
jeffhaob4df5142011-09-19 20:25:32 -0700770 void ChangePermissions(int prot) const;
771
Carl Shapiro1fb86202011-06-27 17:43:13 -0700772 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700773
774 // Opens a .dex file
775 static const DexFile* OpenFile(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800776 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800777 bool verify);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700778
779 // Opens a dex file from within a .jar, .zip, or .apk file
780 static const DexFile* OpenZip(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800781 const std::string& location);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700782
Brian Carlstrom89521892011-12-07 22:05:07 -0800783 // Opens a .dex file at the given address backed by a MemMap
784 static const DexFile* OpenMemory(const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800785 uint32_t location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800786 MemMap* mem_map) {
Ian Rogers30fab402012-01-23 15:43:46 -0800787 return OpenMemory(mem_map->Begin(),
788 mem_map->Size(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800789 location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800790 location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800791 mem_map);
792 }
793
794 // Opens a .dex file at the given address, optionally backed by a MemMap
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700795 static const DexFile* OpenMemory(const byte* dex_file,
jeffhaof6174e82012-01-31 16:14:17 -0800796 size_t size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700797 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800798 uint32_t location_checksum,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700799 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700800
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800801 DexFile(const byte* base, size_t size,
802 const std::string& location, uint32_t location_checksum,
803 MemMap* mem_map)
Ian Rogers30fab402012-01-23 15:43:46 -0800804 : begin_(base),
jeffhaof6174e82012-01-31 16:14:17 -0800805 size_(size),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700806 location_(location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800807 location_checksum_(location_checksum),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700808 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400809 dex_object_lock_("a dex_object_lock_"),
810 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700811 header_(0),
812 string_ids_(0),
813 type_ids_(0),
814 field_ids_(0),
815 method_ids_(0),
816 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700817 class_defs_(0) {
Ian Rogers30fab402012-01-23 15:43:46 -0800818 CHECK(begin_ != NULL) << GetLocation();
jeffhaof6174e82012-01-31 16:14:17 -0800819 CHECK_GT(size_, 0U) << GetLocation();
820 }
821
822 const byte* Begin() const {
823 return begin_;
824 }
825
826 size_t Size() const {
827 return size_;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700828 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700829
830 // Top-level initializer that calls other Init methods.
831 bool Init();
832
833 // Caches pointers into to the various file sections.
834 void InitMembers();
835
836 // Builds the index of descriptors to class definitions.
837 void InitIndex();
838
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800839 // Returns true if the header magic and version numbers are of the expected values.
jeffhao10037c82012-01-23 15:06:23 -0800840 bool CheckMagicAndVersion() const;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700841
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800842 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800843 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
844 void* context, const byte* stream, LocalInfo* local_in_reg) const;
Elliott Hughes03181a82011-11-17 17:22:21 -0800845
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800846 // The index of descriptors to class definition indexes (as opposed to type id indexes)
Elliott Hughesa0e18062012-04-13 15:59:59 -0700847 typedef SafeMap<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700848 Index index_;
849
850 // The base address of the memory mapping.
Ian Rogers30fab402012-01-23 15:43:46 -0800851 const byte* begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700852
853 // The size of the underlying memory allocation in bytes.
jeffhaof6174e82012-01-31 16:14:17 -0800854 size_t size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700855
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700856 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700857 //
858 // The ClassLinker will use this to match DexFiles the boot class
859 // path to DexCache::GetLocation when loading from an image.
860 const std::string location_;
861
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800862 const uint32_t location_checksum_;
863
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700864 // Manages the underlying memory allocation.
865 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700866
Jesse Wilson6bf19152011-09-29 13:12:33 -0400867 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
868 mutable Mutex dex_object_lock_;
869 mutable jobject dex_object_;
870
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700871 // Points to the header section.
872 const Header* header_;
873
874 // Points to the base of the string identifier list.
875 const StringId* string_ids_;
876
877 // Points to the base of the type identifier list.
878 const TypeId* type_ids_;
879
880 // Points to the base of the field identifier list.
881 const FieldId* field_ids_;
882
883 // Points to the base of the method identifier list.
884 const MethodId* method_ids_;
885
886 // Points to the base of the prototype identifier list.
887 const ProtoId* proto_ids_;
888
889 // Points to the base of the class definition list.
890 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700891};
892
Ian Rogers0571d352011-11-03 19:51:38 -0700893// Iterate over a dex file's ProtoId's paramters
894class DexFileParameterIterator {
895 public:
896 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
897 : dex_file_(dex_file), size_(0), pos_(0) {
898 type_list_ = dex_file_.GetProtoParameters(proto_id);
899 if (type_list_ != NULL) {
900 size_ = type_list_->Size();
901 }
902 }
903 bool HasNext() const { return pos_ < size_; }
904 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800905 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -0700906 return type_list_->GetTypeItem(pos_).type_idx_;
907 }
908 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -0700910 }
911 private:
912 const DexFile& dex_file_;
913 const DexFile::TypeList* type_list_;
914 uint32_t size_;
915 uint32_t pos_;
916 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
917};
918
919// Iterate and decode class_data_item
920class ClassDataItemIterator {
921 public:
922 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
923 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
924 ReadClassDataHeader();
925 if (EndOfInstanceFieldsPos() > 0) {
926 ReadClassDataField();
927 } else if (EndOfVirtualMethodsPos() > 0) {
928 ReadClassDataMethod();
929 }
930 }
931 uint32_t NumStaticFields() const {
932 return header_.static_fields_size_;
933 }
934 uint32_t NumInstanceFields() const {
935 return header_.instance_fields_size_;
936 }
937 uint32_t NumDirectMethods() const {
938 return header_.direct_methods_size_;
939 }
940 uint32_t NumVirtualMethods() const {
941 return header_.virtual_methods_size_;
942 }
943 bool HasNextStaticField() const {
944 return pos_ < EndOfStaticFieldsPos();
945 }
946 bool HasNextInstanceField() const {
947 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
948 }
949 bool HasNextDirectMethod() const {
950 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
951 }
952 bool HasNextVirtualMethod() const {
953 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
954 }
955 bool HasNext() const {
956 return pos_ < EndOfVirtualMethodsPos();
957 }
958 void Next() {
959 pos_++;
960 if (pos_ < EndOfStaticFieldsPos()) {
961 last_idx_ = GetMemberIndex();
962 ReadClassDataField();
963 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
964 last_idx_ = 0; // transition to next array, reset last index
965 ReadClassDataField();
966 } else if (pos_ < EndOfInstanceFieldsPos()) {
967 last_idx_ = GetMemberIndex();
968 ReadClassDataField();
969 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
970 last_idx_ = 0; // transition to next array, reset last index
971 ReadClassDataMethod();
972 } else if (pos_ < EndOfDirectMethodsPos()) {
973 last_idx_ = GetMemberIndex();
974 ReadClassDataMethod();
975 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
976 last_idx_ = 0; // transition to next array, reset last index
977 ReadClassDataMethod();
978 } else if (pos_ < EndOfVirtualMethodsPos()) {
979 last_idx_ = GetMemberIndex();
980 ReadClassDataMethod();
981 } else {
982 DCHECK(!HasNext());
983 }
984 }
985 uint32_t GetMemberIndex() const {
986 if (pos_ < EndOfInstanceFieldsPos()) {
987 return last_idx_ + field_.field_idx_delta_;
988 } else {
989 CHECK_LT(pos_, EndOfVirtualMethodsPos());
990 return last_idx_ + method_.method_idx_delta_;
991 }
992 }
993 uint32_t GetMemberAccessFlags() const {
994 if (pos_ < EndOfInstanceFieldsPos()) {
995 return field_.access_flags_;
996 } else {
997 CHECK_LT(pos_, EndOfVirtualMethodsPos());
998 return method_.access_flags_;
999 }
1000 }
1001 const DexFile::CodeItem* GetMethodCodeItem() const {
1002 return dex_file_.GetCodeItem(method_.code_off_);
1003 }
1004 uint32_t GetMethodCodeItemOffset() const {
1005 return method_.code_off_;
1006 }
jeffhao10037c82012-01-23 15:06:23 -08001007 const byte* EndDataPointer() const {
1008 CHECK(!HasNext());
1009 return ptr_pos_;
1010 }
Ian Rogers0571d352011-11-03 19:51:38 -07001011 private:
1012 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
1013 // header for a class_data_item
1014 struct ClassDataHeader {
1015 uint32_t static_fields_size_; // the number of static fields
1016 uint32_t instance_fields_size_; // the number of instance fields
1017 uint32_t direct_methods_size_; // the number of direct methods
1018 uint32_t virtual_methods_size_; // the number of virtual methods
1019 } header_;
1020
1021 // Read and decode header from a class_data_item stream into header
1022 void ReadClassDataHeader();
1023
1024 uint32_t EndOfStaticFieldsPos() const {
1025 return header_.static_fields_size_;
1026 }
1027 uint32_t EndOfInstanceFieldsPos() const {
1028 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
1029 }
1030 uint32_t EndOfDirectMethodsPos() const {
1031 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
1032 }
1033 uint32_t EndOfVirtualMethodsPos() const {
1034 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
1035 }
1036
1037 // A decoded version of the field of a class_data_item
1038 struct ClassDataField {
1039 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
1040 uint32_t access_flags_; // access flags for the field
1041 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
1042 private:
1043 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001044 };
1045 ClassDataField field_;
Ian Rogers0571d352011-11-03 19:51:38 -07001046
1047 // Read and decode a field from a class_data_item stream into field
1048 void ReadClassDataField();
1049
1050 // A decoded version of the method of a class_data_item
1051 struct ClassDataMethod {
1052 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
1053 uint32_t access_flags_;
1054 uint32_t code_off_;
1055 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
1056 private:
1057 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001058 };
1059 ClassDataMethod method_;
Ian Rogers0571d352011-11-03 19:51:38 -07001060
1061 // Read and decode a method from a class_data_item stream into method
1062 void ReadClassDataMethod();
1063
1064 const DexFile& dex_file_;
1065 size_t pos_; // integral number of items passed
1066 const byte* ptr_pos_; // pointer into stream of class_data_item
1067 uint32_t last_idx_; // last read field or method index to apply delta to
1068 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
1069};
1070
1071class ClassLinker;
1072class DexCache;
1073class Field;
1074
1075class EncodedStaticFieldValueIterator {
1076 public:
1077 EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache,
1078 ClassLinker* linker, const DexFile::ClassDef& class_def);
1079
1080 void ReadValueToField(Field* field) const;
1081
1082 bool HasNext() { return pos_ < array_size_; }
1083
1084 void Next();
1085 private:
1086 enum ValueType {
1087 kByte = 0x00,
1088 kShort = 0x02,
1089 kChar = 0x03,
1090 kInt = 0x04,
1091 kLong = 0x06,
1092 kFloat = 0x10,
1093 kDouble = 0x11,
1094 kString = 0x17,
1095 kType = 0x18,
1096 kField = 0x19,
1097 kMethod = 0x1a,
1098 kEnum = 0x1b,
1099 kArray = 0x1c,
1100 kAnnotation = 0x1d,
1101 kNull = 0x1e,
1102 kBoolean = 0x1f
1103 };
1104
1105 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
1106 static const byte kEncodedValueArgShift = 5;
1107
1108 const DexFile& dex_file_;
1109 DexCache* dex_cache_; // dex cache to resolve literal objects
1110 ClassLinker* linker_; // linker to resolve literal objects
1111 size_t array_size_; // size of array
1112 size_t pos_; // current position
1113 const byte* ptr_; // pointer into encoded data array
1114 byte type_; // type of current encoded value
1115 jvalue jval_; // value of current encoded value
1116 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1117};
1118
1119class CatchHandlerIterator {
1120 public:
1121 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
Logan Chien736df022012-04-27 16:25:57 +08001122
1123 CatchHandlerIterator(const DexFile::CodeItem& code_item,
1124 const DexFile::TryItem& try_item);
1125
Ian Rogers0571d352011-11-03 19:51:38 -07001126 explicit CatchHandlerIterator(const byte* handler_data) {
1127 Init(handler_data);
1128 }
1129
1130 uint16_t GetHandlerTypeIndex() const {
1131 return handler_.type_idx_;
1132 }
1133 uint32_t GetHandlerAddress() const {
1134 return handler_.address_;
1135 }
1136 void Next();
1137 bool HasNext() const {
1138 return remaining_count_ != -1 || catch_all_;
1139 }
1140 // End of this set of catch blocks, convenience method to locate next set of catch blocks
1141 const byte* EndDataPointer() const {
1142 CHECK(!HasNext());
1143 return current_data_;
1144 }
1145 private:
Logan Chien736df022012-04-27 16:25:57 +08001146 void Init(const DexFile::CodeItem& code_item, int32_t offset);
Ian Rogers0571d352011-11-03 19:51:38 -07001147 void Init(const byte* handler_data);
1148
1149 struct CatchHandlerItem {
1150 uint16_t type_idx_; // type index of the caught exception type
1151 uint32_t address_; // handler address
1152 } handler_;
1153 const byte *current_data_; // the current handler in dex file.
1154 int32_t remaining_count_; // number of handlers not read.
1155 bool catch_all_; // is there a handler that will catch all exceptions in case
1156 // that all typed handler does not match.
1157};
1158
Carl Shapiro1fb86202011-06-27 17:43:13 -07001159} // namespace art
1160
1161#endif // ART_SRC_DEX_FILE_H_