blob: 2f315a9b2038fc5f4e169cd5e775a31db72b9dc0 [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
Logan Chiendd361c92012-04-10 23:40:37 +0800758 int32_t GetLineNumFromPC(bool is_static, uint32_t method_idx,
759 const CodeItem* code_item, uint32_t rel_pc) const;
760
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800761 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800762 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
763 void* context) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700764
Ian Rogers0571d352011-11-03 19:51:38 -0700765 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700766 if (class_def.source_file_idx_ == 0xffffffff) {
767 return NULL;
768 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700769 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700770 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700771 }
772
jeffhaob4df5142011-09-19 20:25:32 -0700773 void ChangePermissions(int prot) const;
774
Carl Shapiro1fb86202011-06-27 17:43:13 -0700775 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700776
777 // Opens a .dex file
778 static const DexFile* OpenFile(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800779 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800780 bool verify);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700781
782 // Opens a dex file from within a .jar, .zip, or .apk file
783 static const DexFile* OpenZip(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800784 const std::string& location);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700785
Brian Carlstrom89521892011-12-07 22:05:07 -0800786 // Opens a .dex file at the given address backed by a MemMap
787 static const DexFile* OpenMemory(const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800788 uint32_t location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800789 MemMap* mem_map) {
Ian Rogers30fab402012-01-23 15:43:46 -0800790 return OpenMemory(mem_map->Begin(),
791 mem_map->Size(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800792 location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800793 location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800794 mem_map);
795 }
796
797 // Opens a .dex file at the given address, optionally backed by a MemMap
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700798 static const DexFile* OpenMemory(const byte* dex_file,
jeffhaof6174e82012-01-31 16:14:17 -0800799 size_t size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700800 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800801 uint32_t location_checksum,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700802 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700803
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800804 DexFile(const byte* base, size_t size,
805 const std::string& location, uint32_t location_checksum,
806 MemMap* mem_map)
Ian Rogers30fab402012-01-23 15:43:46 -0800807 : begin_(base),
jeffhaof6174e82012-01-31 16:14:17 -0800808 size_(size),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700809 location_(location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800810 location_checksum_(location_checksum),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700811 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400812 dex_object_lock_("a dex_object_lock_"),
813 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700814 header_(0),
815 string_ids_(0),
816 type_ids_(0),
817 field_ids_(0),
818 method_ids_(0),
819 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700820 class_defs_(0) {
Ian Rogers30fab402012-01-23 15:43:46 -0800821 CHECK(begin_ != NULL) << GetLocation();
jeffhaof6174e82012-01-31 16:14:17 -0800822 CHECK_GT(size_, 0U) << GetLocation();
823 }
824
825 const byte* Begin() const {
826 return begin_;
827 }
828
829 size_t Size() const {
830 return size_;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700831 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700832
833 // Top-level initializer that calls other Init methods.
834 bool Init();
835
836 // Caches pointers into to the various file sections.
837 void InitMembers();
838
839 // Builds the index of descriptors to class definitions.
840 void InitIndex();
841
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800842 // Returns true if the header magic and version numbers are of the expected values.
jeffhao10037c82012-01-23 15:06:23 -0800843 bool CheckMagicAndVersion() const;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700844
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800845 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800846 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
847 void* context, const byte* stream, LocalInfo* local_in_reg) const;
Elliott Hughes03181a82011-11-17 17:22:21 -0800848
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800849 // The index of descriptors to class definition indexes (as opposed to type id indexes)
Elliott Hughesa0e18062012-04-13 15:59:59 -0700850 typedef SafeMap<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700851 Index index_;
852
853 // The base address of the memory mapping.
Ian Rogers30fab402012-01-23 15:43:46 -0800854 const byte* begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700855
856 // The size of the underlying memory allocation in bytes.
jeffhaof6174e82012-01-31 16:14:17 -0800857 size_t size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700858
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700859 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700860 //
861 // The ClassLinker will use this to match DexFiles the boot class
862 // path to DexCache::GetLocation when loading from an image.
863 const std::string location_;
864
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800865 const uint32_t location_checksum_;
866
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700867 // Manages the underlying memory allocation.
868 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700869
Jesse Wilson6bf19152011-09-29 13:12:33 -0400870 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
871 mutable Mutex dex_object_lock_;
872 mutable jobject dex_object_;
873
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700874 // Points to the header section.
875 const Header* header_;
876
877 // Points to the base of the string identifier list.
878 const StringId* string_ids_;
879
880 // Points to the base of the type identifier list.
881 const TypeId* type_ids_;
882
883 // Points to the base of the field identifier list.
884 const FieldId* field_ids_;
885
886 // Points to the base of the method identifier list.
887 const MethodId* method_ids_;
888
889 // Points to the base of the prototype identifier list.
890 const ProtoId* proto_ids_;
891
892 // Points to the base of the class definition list.
893 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700894};
895
Ian Rogers0571d352011-11-03 19:51:38 -0700896// Iterate over a dex file's ProtoId's paramters
897class DexFileParameterIterator {
898 public:
899 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
900 : dex_file_(dex_file), size_(0), pos_(0) {
901 type_list_ = dex_file_.GetProtoParameters(proto_id);
902 if (type_list_ != NULL) {
903 size_ = type_list_->Size();
904 }
905 }
906 bool HasNext() const { return pos_ < size_; }
907 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800908 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -0700909 return type_list_->GetTypeItem(pos_).type_idx_;
910 }
911 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800912 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -0700913 }
914 private:
915 const DexFile& dex_file_;
916 const DexFile::TypeList* type_list_;
917 uint32_t size_;
918 uint32_t pos_;
919 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
920};
921
922// Iterate and decode class_data_item
923class ClassDataItemIterator {
924 public:
925 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
926 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
927 ReadClassDataHeader();
928 if (EndOfInstanceFieldsPos() > 0) {
929 ReadClassDataField();
930 } else if (EndOfVirtualMethodsPos() > 0) {
931 ReadClassDataMethod();
932 }
933 }
934 uint32_t NumStaticFields() const {
935 return header_.static_fields_size_;
936 }
937 uint32_t NumInstanceFields() const {
938 return header_.instance_fields_size_;
939 }
940 uint32_t NumDirectMethods() const {
941 return header_.direct_methods_size_;
942 }
943 uint32_t NumVirtualMethods() const {
944 return header_.virtual_methods_size_;
945 }
946 bool HasNextStaticField() const {
947 return pos_ < EndOfStaticFieldsPos();
948 }
949 bool HasNextInstanceField() const {
950 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
951 }
952 bool HasNextDirectMethod() const {
953 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
954 }
955 bool HasNextVirtualMethod() const {
956 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
957 }
958 bool HasNext() const {
959 return pos_ < EndOfVirtualMethodsPos();
960 }
961 void Next() {
962 pos_++;
963 if (pos_ < EndOfStaticFieldsPos()) {
964 last_idx_ = GetMemberIndex();
965 ReadClassDataField();
966 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
967 last_idx_ = 0; // transition to next array, reset last index
968 ReadClassDataField();
969 } else if (pos_ < EndOfInstanceFieldsPos()) {
970 last_idx_ = GetMemberIndex();
971 ReadClassDataField();
972 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
973 last_idx_ = 0; // transition to next array, reset last index
974 ReadClassDataMethod();
975 } else if (pos_ < EndOfDirectMethodsPos()) {
976 last_idx_ = GetMemberIndex();
977 ReadClassDataMethod();
978 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
979 last_idx_ = 0; // transition to next array, reset last index
980 ReadClassDataMethod();
981 } else if (pos_ < EndOfVirtualMethodsPos()) {
982 last_idx_ = GetMemberIndex();
983 ReadClassDataMethod();
984 } else {
985 DCHECK(!HasNext());
986 }
987 }
988 uint32_t GetMemberIndex() const {
989 if (pos_ < EndOfInstanceFieldsPos()) {
990 return last_idx_ + field_.field_idx_delta_;
991 } else {
992 CHECK_LT(pos_, EndOfVirtualMethodsPos());
993 return last_idx_ + method_.method_idx_delta_;
994 }
995 }
996 uint32_t GetMemberAccessFlags() const {
997 if (pos_ < EndOfInstanceFieldsPos()) {
998 return field_.access_flags_;
999 } else {
1000 CHECK_LT(pos_, EndOfVirtualMethodsPos());
1001 return method_.access_flags_;
1002 }
1003 }
1004 const DexFile::CodeItem* GetMethodCodeItem() const {
1005 return dex_file_.GetCodeItem(method_.code_off_);
1006 }
1007 uint32_t GetMethodCodeItemOffset() const {
1008 return method_.code_off_;
1009 }
jeffhao10037c82012-01-23 15:06:23 -08001010 const byte* EndDataPointer() const {
1011 CHECK(!HasNext());
1012 return ptr_pos_;
1013 }
Ian Rogers0571d352011-11-03 19:51:38 -07001014 private:
1015 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
1016 // header for a class_data_item
1017 struct ClassDataHeader {
1018 uint32_t static_fields_size_; // the number of static fields
1019 uint32_t instance_fields_size_; // the number of instance fields
1020 uint32_t direct_methods_size_; // the number of direct methods
1021 uint32_t virtual_methods_size_; // the number of virtual methods
1022 } header_;
1023
1024 // Read and decode header from a class_data_item stream into header
1025 void ReadClassDataHeader();
1026
1027 uint32_t EndOfStaticFieldsPos() const {
1028 return header_.static_fields_size_;
1029 }
1030 uint32_t EndOfInstanceFieldsPos() const {
1031 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
1032 }
1033 uint32_t EndOfDirectMethodsPos() const {
1034 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
1035 }
1036 uint32_t EndOfVirtualMethodsPos() const {
1037 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
1038 }
1039
1040 // A decoded version of the field of a class_data_item
1041 struct ClassDataField {
1042 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
1043 uint32_t access_flags_; // access flags for the field
1044 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
1045 private:
1046 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001047 };
1048 ClassDataField field_;
Ian Rogers0571d352011-11-03 19:51:38 -07001049
1050 // Read and decode a field from a class_data_item stream into field
1051 void ReadClassDataField();
1052
1053 // A decoded version of the method of a class_data_item
1054 struct ClassDataMethod {
1055 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
1056 uint32_t access_flags_;
1057 uint32_t code_off_;
1058 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
1059 private:
1060 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001061 };
1062 ClassDataMethod method_;
Ian Rogers0571d352011-11-03 19:51:38 -07001063
1064 // Read and decode a method from a class_data_item stream into method
1065 void ReadClassDataMethod();
1066
1067 const DexFile& dex_file_;
1068 size_t pos_; // integral number of items passed
1069 const byte* ptr_pos_; // pointer into stream of class_data_item
1070 uint32_t last_idx_; // last read field or method index to apply delta to
1071 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
1072};
1073
1074class ClassLinker;
1075class DexCache;
1076class Field;
1077
1078class EncodedStaticFieldValueIterator {
1079 public:
1080 EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache,
1081 ClassLinker* linker, const DexFile::ClassDef& class_def);
1082
1083 void ReadValueToField(Field* field) const;
1084
1085 bool HasNext() { return pos_ < array_size_; }
1086
1087 void Next();
1088 private:
1089 enum ValueType {
1090 kByte = 0x00,
1091 kShort = 0x02,
1092 kChar = 0x03,
1093 kInt = 0x04,
1094 kLong = 0x06,
1095 kFloat = 0x10,
1096 kDouble = 0x11,
1097 kString = 0x17,
1098 kType = 0x18,
1099 kField = 0x19,
1100 kMethod = 0x1a,
1101 kEnum = 0x1b,
1102 kArray = 0x1c,
1103 kAnnotation = 0x1d,
1104 kNull = 0x1e,
1105 kBoolean = 0x1f
1106 };
1107
1108 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
1109 static const byte kEncodedValueArgShift = 5;
1110
1111 const DexFile& dex_file_;
1112 DexCache* dex_cache_; // dex cache to resolve literal objects
1113 ClassLinker* linker_; // linker to resolve literal objects
1114 size_t array_size_; // size of array
1115 size_t pos_; // current position
1116 const byte* ptr_; // pointer into encoded data array
1117 byte type_; // type of current encoded value
1118 jvalue jval_; // value of current encoded value
1119 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1120};
1121
1122class CatchHandlerIterator {
1123 public:
1124 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
1125 explicit CatchHandlerIterator(const byte* handler_data) {
1126 Init(handler_data);
1127 }
1128
1129 uint16_t GetHandlerTypeIndex() const {
1130 return handler_.type_idx_;
1131 }
1132 uint32_t GetHandlerAddress() const {
1133 return handler_.address_;
1134 }
1135 void Next();
1136 bool HasNext() const {
1137 return remaining_count_ != -1 || catch_all_;
1138 }
1139 // End of this set of catch blocks, convenience method to locate next set of catch blocks
1140 const byte* EndDataPointer() const {
1141 CHECK(!HasNext());
1142 return current_data_;
1143 }
1144 private:
1145 void Init(const byte* handler_data);
1146
1147 struct CatchHandlerItem {
1148 uint16_t type_idx_; // type index of the caught exception type
1149 uint32_t address_; // handler address
1150 } handler_;
1151 const byte *current_data_; // the current handler in dex file.
1152 int32_t remaining_count_; // number of handlers not read.
1153 bool catch_all_; // is there a handler that will catch all exceptions in case
1154 // that all typed handler does not match.
1155};
1156
Carl Shapiro1fb86202011-06-27 17:43:13 -07001157} // namespace art
1158
1159#endif // ART_SRC_DEX_FILE_H_