blob: f1f76a8eca072f596f0d7f3101f44a044c66951f [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
Elliott Hughesa21039c2012-06-21 12:09:25 -070079
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070080 private:
81 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070082 };
Carl Shapiro1fb86202011-06-27 17:43:13 -070083
jeffhao10037c82012-01-23 15:06:23 -080084 // Map item type codes.
85 enum {
86 kDexTypeHeaderItem = 0x0000,
87 kDexTypeStringIdItem = 0x0001,
88 kDexTypeTypeIdItem = 0x0002,
89 kDexTypeProtoIdItem = 0x0003,
90 kDexTypeFieldIdItem = 0x0004,
91 kDexTypeMethodIdItem = 0x0005,
92 kDexTypeClassDefItem = 0x0006,
93 kDexTypeMapList = 0x1000,
94 kDexTypeTypeList = 0x1001,
95 kDexTypeAnnotationSetRefList = 0x1002,
96 kDexTypeAnnotationSetItem = 0x1003,
97 kDexTypeClassDataItem = 0x2000,
98 kDexTypeCodeItem = 0x2001,
99 kDexTypeStringDataItem = 0x2002,
100 kDexTypeDebugInfoItem = 0x2003,
101 kDexTypeAnnotationItem = 0x2004,
102 kDexTypeEncodedArrayItem = 0x2005,
103 kDexTypeAnnotationsDirectoryItem = 0x2006,
104 };
105
106 struct MapItem {
107 uint16_t type_;
108 uint16_t unused_;
109 uint32_t size_;
110 uint32_t offset_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700111
jeffhao10037c82012-01-23 15:06:23 -0800112 private:
113 DISALLOW_COPY_AND_ASSIGN(MapItem);
114 };
115
116 struct MapList {
117 uint32_t size_;
118 MapItem list_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700119
jeffhao10037c82012-01-23 15:06:23 -0800120 private:
121 DISALLOW_COPY_AND_ASSIGN(MapList);
122 };
123
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700124 // Raw string_id_item.
125 struct StringId {
126 uint32_t string_data_off_; // offset in bytes from the base address
Elliott Hughesa21039c2012-06-21 12:09:25 -0700127
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700128 private:
129 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700130 };
131
132 // Raw type_id_item.
133 struct TypeId {
134 uint32_t descriptor_idx_; // index into string_ids
Elliott Hughesa21039c2012-06-21 12:09:25 -0700135
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700136 private:
137 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700138 };
139
140 // Raw field_id_item.
141 struct FieldId {
Ian Rogers0571d352011-11-03 19:51:38 -0700142 uint16_t class_idx_; // index into type_ids_ array for defining class
143 uint16_t type_idx_; // index into type_ids_ array for field type
144 uint32_t name_idx_; // index into string_ids_ array for field name
Elliott Hughesa21039c2012-06-21 12:09:25 -0700145
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700146 private:
147 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700148 };
149
150 // Raw method_id_item.
151 struct MethodId {
Ian Rogers0571d352011-11-03 19:51:38 -0700152 uint16_t class_idx_; // index into type_ids_ array for defining class
153 uint16_t proto_idx_; // index into proto_ids_ array for method prototype
154 uint32_t name_idx_; // index into string_ids_ array for method name
Elliott Hughesa21039c2012-06-21 12:09:25 -0700155
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700156 private:
157 DISALLOW_COPY_AND_ASSIGN(MethodId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700158 };
159
160 // Raw proto_id_item.
161 struct ProtoId {
Ian Rogers0571d352011-11-03 19:51:38 -0700162 uint32_t shorty_idx_; // index into string_ids array for shorty descriptor
163 uint16_t return_type_idx_; // index into type_ids array for return type
164 uint16_t pad_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700165 uint32_t parameters_off_; // file offset to type_list for parameter types
Elliott Hughesa21039c2012-06-21 12:09:25 -0700166
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700167 private:
168 DISALLOW_COPY_AND_ASSIGN(ProtoId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700169 };
170
171 // Raw class_def_item.
172 struct ClassDef {
Ian Rogers0571d352011-11-03 19:51:38 -0700173 uint16_t class_idx_; // index into type_ids_ array for this class
174 uint16_t pad1_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700175 uint32_t access_flags_;
Ian Rogers0571d352011-11-03 19:51:38 -0700176 uint16_t superclass_idx_; // index into type_ids_ array for superclass
177 uint16_t pad2_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700178 uint32_t interfaces_off_; // file offset to TypeList
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700179 uint32_t source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700180 uint32_t annotations_off_; // file offset to annotations_directory_item
181 uint32_t class_data_off_; // file offset to class_data_item
182 uint32_t static_values_off_; // file offset to EncodedArray
Elliott Hughesa21039c2012-06-21 12:09:25 -0700183
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700184 private:
185 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700186 };
187
188 // Raw type_item.
189 struct TypeItem {
190 uint16_t type_idx_; // index into type_ids section
Elliott Hughesa21039c2012-06-21 12:09:25 -0700191
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700192 private:
193 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700194 };
195
196 // Raw type_list.
197 class TypeList {
198 public:
199 uint32_t Size() const {
200 return size_;
201 }
202
203 const TypeItem& GetTypeItem(uint32_t idx) const {
204 CHECK_LT(idx, this->size_);
205 return this->list_[idx];
206 }
207
208 private:
209 uint32_t size_; // size of the list, in entries
210 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700211 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700212 };
213
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700214 // Raw code_item.
215 struct CodeItem {
216 uint16_t registers_size_;
217 uint16_t ins_size_;
218 uint16_t outs_size_;
219 uint16_t tries_size_;
220 uint32_t debug_info_off_; // file offset to debug info stream
Ian Rogersd81871c2011-10-03 13:57:23 -0700221 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700222 uint16_t insns_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700223
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700224 private:
225 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700226 };
227
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700228 // Raw try_item.
229 struct TryItem {
230 uint32_t start_addr_;
231 uint16_t insn_count_;
232 uint16_t handler_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700233
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700234 private:
235 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700236 };
237
jeffhao10037c82012-01-23 15:06:23 -0800238 // Annotation constants.
239 enum {
240 kDexVisibilityBuild = 0x00, /* annotation visibility */
241 kDexVisibilityRuntime = 0x01,
242 kDexVisibilitySystem = 0x02,
243
244 kDexAnnotationByte = 0x00,
245 kDexAnnotationShort = 0x02,
246 kDexAnnotationChar = 0x03,
247 kDexAnnotationInt = 0x04,
248 kDexAnnotationLong = 0x06,
249 kDexAnnotationFloat = 0x10,
250 kDexAnnotationDouble = 0x11,
251 kDexAnnotationString = 0x17,
252 kDexAnnotationType = 0x18,
253 kDexAnnotationField = 0x19,
254 kDexAnnotationMethod = 0x1a,
255 kDexAnnotationEnum = 0x1b,
256 kDexAnnotationArray = 0x1c,
257 kDexAnnotationAnnotation = 0x1d,
258 kDexAnnotationNull = 0x1e,
259 kDexAnnotationBoolean = 0x1f,
260
261 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
262 kDexAnnotationValueArgShift = 5,
263 };
264
265 struct AnnotationsDirectoryItem {
266 uint32_t class_annotations_off_;
267 uint32_t fields_size_;
268 uint32_t methods_size_;
269 uint32_t parameters_size_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700270
jeffhao10037c82012-01-23 15:06:23 -0800271 private:
272 DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
273 };
274
275 struct FieldAnnotationsItem {
276 uint32_t field_idx_;
277 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700278
jeffhao10037c82012-01-23 15:06:23 -0800279 private:
280 DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem);
281 };
282
283 struct MethodAnnotationsItem {
284 uint32_t method_idx_;
285 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700286
jeffhao10037c82012-01-23 15:06:23 -0800287 private:
288 DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem);
289 };
290
291 struct ParameterAnnotationsItem {
292 uint32_t method_idx_;
293 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700294
jeffhao10037c82012-01-23 15:06:23 -0800295 private:
296 DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem);
297 };
298
299 struct AnnotationSetRefItem {
300 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700301
jeffhao10037c82012-01-23 15:06:23 -0800302 private:
303 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem);
304 };
305
306 struct AnnotationSetRefList {
307 uint32_t size_;
308 AnnotationSetRefItem list_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700309
jeffhao10037c82012-01-23 15:06:23 -0800310 private:
311 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList);
312 };
313
314 struct AnnotationSetItem {
315 uint32_t size_;
316 uint32_t entries_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700317
jeffhao10037c82012-01-23 15:06:23 -0800318 private:
319 DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
320 };
321
322 struct AnnotationItem {
323 uint8_t visibility_;
324 uint8_t annotation_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700325
jeffhao10037c82012-01-23 15:06:23 -0800326 private:
327 DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
328 };
329
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700330 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
331 typedef std::vector<const DexFile*> ClassPath;
332
333 // Search a collection of DexFiles for a descriptor
334 static ClassPathEntry FindInClassPath(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700335 const ClassPath& class_path);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700336
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800337 // Returns the checksum of a file for comparison with GetLocationChecksum().
338 // For .dex files, this is the header checksum.
339 // For zip files, this is the classes.dex zip entry CRC32 checksum.
340 // Return true if the checksum could be found, false otherwise.
341 static bool GetChecksum(const std::string& filename, uint32_t& checksum);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700342
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700343 // Opens .dex file, guessing the container format based on file extension
Brian Carlstrom16192862011-09-12 17:50:06 -0700344 static const DexFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800345 const std::string& location);
jeffhao262bf462011-10-20 18:36:32 -0700346
Brian Carlstrom89521892011-12-07 22:05:07 -0800347 // Opens .dex file, backed by existing memory
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800348 static const DexFile* Open(const uint8_t* base, size_t size,
349 const std::string& location, uint32_t location_checksum) {
350 return OpenMemory(base, size, location, location_checksum, NULL);
Brian Carlstrom89521892011-12-07 22:05:07 -0800351 }
352
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800353 // Opens .dex file from the classes.dex in a zip archive
354 static const DexFile* Open(const ZipArchive& zip_archive, const std::string& location);
355
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700356 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700357 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700358
Brian Carlstroma663ea52011-08-19 23:33:41 -0700359 const std::string& GetLocation() const {
360 return location_;
361 }
362
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800363 // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header.
364 // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex.
365 uint32_t GetLocationChecksum() const {
366 return location_checksum_;
367 }
368
Jesse Wilson6bf19152011-09-29 13:12:33 -0400369 // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file.
370 // Used by managed code to implement annotations.
371 jobject GetDexObject(JNIEnv* env) const;
372
Brian Carlstroma663ea52011-08-19 23:33:41 -0700373 const Header& GetHeader() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800374 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700375 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700376 }
377
Ian Rogers0571d352011-11-03 19:51:38 -0700378 // Decode the dex magic version
Ian Rogersd81871c2011-10-03 13:57:23 -0700379 uint32_t GetVersion() const;
380
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800381 // Returns true if the byte string points to the magic value.
382 static bool IsMagicValid(const byte* magic);
383
384 // Returns true if the byte string after the magic is the correct value.
385 static bool IsVersionValid(const byte* magic);
386
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700387 // Returns the number of string identifiers in the .dex file.
388 size_t NumStringIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800389 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700390 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700391 }
392
Ian Rogers0571d352011-11-03 19:51:38 -0700393 // Returns the StringId at the specified index.
394 const StringId& GetStringId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800395 CHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700396 return string_ids_[idx];
397 }
398
399 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800400 CHECK_GE(&string_id, string_ids_) << GetLocation();
401 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700402 return &string_id - string_ids_;
403 }
404
405 int32_t GetStringLength(const StringId& string_id) const;
406
407 // Returns a pointer to the UTF-8 string data referred to by the given string_id.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800408 const char* GetStringDataAndLength(const StringId& string_id, uint32_t* length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700409
410 const char* GetStringData(const StringId& string_id) const {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800411 uint32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700412 return GetStringDataAndLength(string_id, &length);
413 }
414
415 // return the UTF-8 encoded string with the specified string_id index
Elliott Hughes45651fd2012-02-21 15:48:20 -0800416 const char* StringDataAndLengthByIdx(uint32_t idx, uint32_t* unicode_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700417 if (idx == kDexNoIndex) {
418 *unicode_length = 0;
419 return NULL;
420 }
421 const StringId& string_id = GetStringId(idx);
422 return GetStringDataAndLength(string_id, unicode_length);
423 }
424
425 const char* StringDataByIdx(uint32_t idx) const {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800426 uint32_t unicode_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700427 return StringDataAndLengthByIdx(idx, &unicode_length);
428 }
429
430 // Looks up a string id for a given string
431 const StringId* FindStringId(const std::string& string) const;
432
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700433 // Returns the number of type identifiers in the .dex file.
434 size_t NumTypeIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800435 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700436 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700437 }
438
Ian Rogers0571d352011-11-03 19:51:38 -0700439 // Returns the TypeId at the specified index.
440 const TypeId& GetTypeId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800441 CHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700442 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700443 }
444
Ian Rogers0571d352011-11-03 19:51:38 -0700445 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800446 CHECK_GE(&type_id, type_ids_) << GetLocation();
447 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700448 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800449 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700450 return static_cast<uint16_t>(result);
451 }
452
453 // Get the descriptor string associated with a given type index.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800454 const char* StringByTypeIdx(uint32_t idx, uint32_t* unicode_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700455 const TypeId& type_id = GetTypeId(idx);
456 return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length);
457 }
458
459 const char* StringByTypeIdx(uint32_t idx) const {
460 const TypeId& type_id = GetTypeId(idx);
461 return StringDataByIdx(type_id.descriptor_idx_);
462 }
463
464 // Returns the type descriptor string of a type id.
465 const char* GetTypeDescriptor(const TypeId& type_id) const {
466 return StringDataByIdx(type_id.descriptor_idx_);
467 }
468
469 // Looks up a type for the given string index
470 const TypeId* FindTypeId(uint32_t string_idx) const;
471
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700472 // Returns the number of field identifiers in the .dex file.
473 size_t NumFieldIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800474 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700475 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700476 }
477
Ian Rogers0571d352011-11-03 19:51:38 -0700478 // Returns the FieldId at the specified index.
479 const FieldId& GetFieldId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800480 CHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700481 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700482 }
483
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800484 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800485 CHECK_GE(&field_id, field_ids_) << GetLocation();
486 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800487 return &field_id - field_ids_;
488 }
489
490 // Looks up a field by its declaring class, name and type
491 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
492 const DexFile::StringId& name,
493 const DexFile::TypeId& type) const;
494
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700495 // Returns the declaring class descriptor string of a field id.
496 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700497 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
498 return GetTypeDescriptor(type_id);
499 }
500
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700501 // Returns the class descriptor string of a field id.
502 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
503 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
504 return GetTypeDescriptor(type_id);
505 }
506
Brian Carlstromb9edb842011-08-28 16:31:06 -0700507 // Returns the name of a field id.
508 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700509 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700510 }
511
Ian Rogers0571d352011-11-03 19:51:38 -0700512 // Returns the number of method identifiers in the .dex file.
513 size_t NumMethodIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800514 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700515 return header_->method_ids_size_;
516 }
517
518 // Returns the MethodId at the specified index.
519 const MethodId& GetMethodId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800520 CHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700521 return method_ids_[idx];
522 }
523
524 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800525 CHECK_GE(&method_id, method_ids_) << GetLocation();
526 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700527 return &method_id - method_ids_;
528 }
529
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800530 // Looks up a method by its declaring class, name and proto_id
531 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
532 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700533 const DexFile::ProtoId& signature) const;
534
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700535 // Returns the declaring class descriptor string of a method id.
536 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700537 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
538 return GetTypeDescriptor(type_id);
539 }
540
jeffhao98eacac2011-09-14 16:11:53 -0700541 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700542 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
543 return GetProtoId(method_id.proto_idx_);
544 }
545
546 // Returns the signature of a method id.
547 const std::string GetMethodSignature(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700548 return CreateMethodSignature(method_id.proto_idx_, NULL);
jeffhao98eacac2011-09-14 16:11:53 -0700549 }
550
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700551 // Returns the name of a method id.
552 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700553 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700554 }
555
Ian Rogers0571d352011-11-03 19:51:38 -0700556 // Returns the shorty of a method id.
557 const char* GetMethodShorty(const MethodId& method_id) const {
558 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700559 }
Elliott Hughes45651fd2012-02-21 15:48:20 -0800560 const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800561 return StringDataAndLengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
562 }
Ian Rogers0571d352011-11-03 19:51:38 -0700563 // Returns the number of class definitions in the .dex file.
564 size_t NumClassDefs() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800565 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700566 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700567 }
568
569 // Returns the ClassDef at the specified index.
570 const ClassDef& GetClassDef(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800571 CHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700572 return class_defs_[idx];
573 }
574
Ian Rogers0571d352011-11-03 19:51:38 -0700575 uint32_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800576 CHECK_GE(&class_def, class_defs_) << GetLocation();
577 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700578 return &class_def - class_defs_;
579 }
580
581 // Returns the class descriptor string of a class definition.
582 const char* GetClassDescriptor(const ClassDef& class_def) const {
583 return StringByTypeIdx(class_def.class_idx_);
584 }
585
586 // Looks up a class definition by its class descriptor.
587 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
588
589 // Looks up a class definition index by its class descriptor.
590 bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const;
591
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700592 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
593 if (class_def.interfaces_off_ == 0) {
594 return NULL;
595 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800596 const byte* addr = begin_ + class_def.interfaces_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700597 return reinterpret_cast<const TypeList*>(addr);
598 }
599 }
600
Ian Rogers0571d352011-11-03 19:51:38 -0700601 // Returns a pointer to the raw memory mapped class_data_item
602 const byte* GetClassData(const ClassDef& class_def) const {
603 if (class_def.class_data_off_ == 0) {
604 return NULL;
605 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800606 return begin_ + class_def.class_data_off_;
Ian Rogers0571d352011-11-03 19:51:38 -0700607 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700608 }
609
Ian Rogers0571d352011-11-03 19:51:38 -0700610 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800611 const CodeItem* GetCodeItem(const uint32_t code_off) const {
612 if (code_off == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700613 return NULL; // native or abstract method
614 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800615 const byte* addr = begin_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700616 return reinterpret_cast<const CodeItem*>(addr);
617 }
618 }
619
Ian Rogers0571d352011-11-03 19:51:38 -0700620 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
621 return StringByTypeIdx(proto_id.return_type_idx_);
622 }
623
624 // Returns the number of prototype identifiers in the .dex file.
625 size_t NumProtoIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800626 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700627 return header_->proto_ids_size_;
628 }
629
630 // Returns the ProtoId at the specified index.
631 const ProtoId& GetProtoId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800632 CHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700633 return proto_ids_[idx];
634 }
635
636 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800637 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
638 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700639 return &proto_id - proto_ids_;
640 }
641
642 // Looks up a proto id for a given return type and signature type list
643 const ProtoId* FindProtoId(uint16_t return_type_id,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800644 const std::vector<uint16_t>& signature_type_idxs_) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700645
646 // Given a signature place the type ids into the given vector, returns true on success
647 bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
648 const std::string& signature) const;
649
650 // Given a proto_idx decode the type list and return type into a method signature
651 std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const;
652
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700653 // Returns the short form method descriptor for the given prototype.
654 const char* GetShorty(uint32_t proto_idx) const {
655 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700656 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700657 }
658
659 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
660 if (proto_id.parameters_off_ == 0) {
661 return NULL;
662 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800663 const byte* addr = begin_ + proto_id.parameters_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700664 return reinterpret_cast<const TypeList*>(addr);
665 }
666 }
667
Ian Rogers0571d352011-11-03 19:51:38 -0700668 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700669 if (class_def.static_values_off_ == 0) {
670 return 0;
671 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800672 return begin_ + class_def.static_values_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700673 }
674 }
675
Ian Rogers0571d352011-11-03 19:51:38 -0700676 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700678 return reinterpret_cast<const TryItem*>
679 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
680 }
681
682 // Get the base of the encoded data for the given DexCode.
Ian Rogers0571d352011-11-03 19:51:38 -0700683 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
684 const byte* handler_data =
685 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700686 return handler_data + offset;
687 }
688
689 // Find the handler associated with a given address, if any.
690 // Initializes the given iterator and returns true if a match is
691 // found. Returns end if there is no applicable handler.
Ian Rogers0571d352011-11-03 19:51:38 -0700692 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
693 uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700694
Shih-wei Liao195487c2011-08-20 13:29:04 -0700695 // Get the pointer to the start of the debugging data
Ian Rogers0571d352011-11-03 19:51:38 -0700696 const byte* GetDebugInfoStream(const CodeItem* code_item) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700697 if (code_item->debug_info_off_ == 0) {
698 return NULL;
699 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800700 return begin_ + code_item->debug_info_off_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700701 }
702 }
703
704 // Callback for "new position table entry".
705 // Returning true causes the decoder to stop early.
Elliott Hughes2435a572012-02-17 16:07:41 -0800706 typedef bool (*DexDebugNewPositionCb)(void* context, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700707
708 // Callback for "new locals table entry". "signature" is an empty string
709 // if no signature is available for an entry.
Elliott Hughes2435a572012-02-17 16:07:41 -0800710 typedef void (*DexDebugNewLocalCb)(void* context, uint16_t reg,
Elliott Hughes24edeb52012-06-18 15:29:46 -0700711 uint32_t start_address,
712 uint32_t end_address,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700713 const char* name,
714 const char* descriptor,
715 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700716
Elliott Hughes2435a572012-02-17 16:07:41 -0800717 static bool LineNumForPcCb(void* context, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700718
719 // Debug info opcodes and constants
720 enum {
721 DBG_END_SEQUENCE = 0x00,
722 DBG_ADVANCE_PC = 0x01,
723 DBG_ADVANCE_LINE = 0x02,
724 DBG_START_LOCAL = 0x03,
725 DBG_START_LOCAL_EXTENDED = 0x04,
726 DBG_END_LOCAL = 0x05,
727 DBG_RESTART_LOCAL = 0x06,
728 DBG_SET_PROLOGUE_END = 0x07,
729 DBG_SET_EPILOGUE_BEGIN = 0x08,
730 DBG_SET_FILE = 0x09,
731 DBG_FIRST_SPECIAL = 0x0a,
732 DBG_LINE_BASE = -4,
733 DBG_LINE_RANGE = 15,
734 };
735
736 struct LocalInfo {
Ian Rogersca190662012-06-26 15:45:57 -0700737 LocalInfo()
738 : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0), is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700739
Ian Rogers0571d352011-11-03 19:51:38 -0700740 const char* name_; // E.g., list
741 const char* descriptor_; // E.g., Ljava/util/LinkedList;
742 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
743 uint16_t start_address_; // PC location where the local is first defined.
744 bool is_live_; // Is the local defined and live.
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700745
746 private:
747 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700748 };
749
750 struct LineNumFromPcContext {
Ian Rogersca190662012-06-26 15:45:57 -0700751 LineNumFromPcContext(uint32_t address, uint32_t line_num)
752 : address_(address), line_num_(line_num) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700753 uint32_t address_;
754 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700755 private:
756 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700757 };
758
Elliott Hughes2435a572012-02-17 16:07:41 -0800759 void InvokeLocalCbIfLive(void* context, int reg, uint32_t end_address,
Brian Carlstrom78128a62011-09-15 17:21:19 -0700760 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700761 if (local_cb != NULL && local_in_reg[reg].is_live_) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800762 local_cb(context, reg, local_in_reg[reg].start_address_, end_address,
Elliott Hughesdbb40792011-11-18 17:05:22 -0800763 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
764 local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : "");
Shih-wei Liao195487c2011-08-20 13:29:04 -0700765 }
766 }
767
768 // Determine the source file line number based on the program counter.
769 // "pc" is an offset, in 16-bit units, from the start of the method's code.
770 //
771 // Returns -1 if no match was found (possibly because the source files were
772 // compiled without "-g", so no line number information is present).
773 // Returns -2 for native methods (as expected in exception traces).
774 //
775 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Ian Rogers0571d352011-11-03 19:51:38 -0700776 int32_t GetLineNumFromPC(const Method* method, uint32_t rel_pc) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700777
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800778 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800779 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
780 void* context) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700781
Ian Rogers0571d352011-11-03 19:51:38 -0700782 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700783 if (class_def.source_file_idx_ == 0xffffffff) {
784 return NULL;
785 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700786 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700787 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700788 }
789
jeffhaob4df5142011-09-19 20:25:32 -0700790 void ChangePermissions(int prot) const;
791
Ian Rogers1c849e52012-06-28 14:00:33 -0700792 int GetPermissions() const {
793 if (mem_map_.get() == NULL) {
794 return 0;
795 } else {
796 return mem_map_->GetProtect();
797 }
798 }
799
Carl Shapiro1fb86202011-06-27 17:43:13 -0700800 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700801 // Opens a .dex file
802 static const DexFile* OpenFile(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800803 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800804 bool verify);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700805
806 // Opens a dex file from within a .jar, .zip, or .apk file
807 static const DexFile* OpenZip(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800808 const std::string& location);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700809
Brian Carlstrom89521892011-12-07 22:05:07 -0800810 // Opens a .dex file at the given address backed by a MemMap
811 static const DexFile* OpenMemory(const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800812 uint32_t location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800813 MemMap* mem_map) {
Ian Rogers30fab402012-01-23 15:43:46 -0800814 return OpenMemory(mem_map->Begin(),
815 mem_map->Size(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800816 location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800817 location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800818 mem_map);
819 }
820
821 // Opens a .dex file at the given address, optionally backed by a MemMap
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700822 static const DexFile* OpenMemory(const byte* dex_file,
jeffhaof6174e82012-01-31 16:14:17 -0800823 size_t size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700824 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800825 uint32_t location_checksum,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700826 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700827
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800828 DexFile(const byte* base, size_t size,
829 const std::string& location, uint32_t location_checksum,
830 MemMap* mem_map)
Ian Rogers30fab402012-01-23 15:43:46 -0800831 : begin_(base),
jeffhaof6174e82012-01-31 16:14:17 -0800832 size_(size),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700833 location_(location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800834 location_checksum_(location_checksum),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700835 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400836 dex_object_lock_("a dex_object_lock_"),
837 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700838 header_(0),
839 string_ids_(0),
840 type_ids_(0),
841 field_ids_(0),
842 method_ids_(0),
843 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700844 class_defs_(0) {
Ian Rogers30fab402012-01-23 15:43:46 -0800845 CHECK(begin_ != NULL) << GetLocation();
jeffhaof6174e82012-01-31 16:14:17 -0800846 CHECK_GT(size_, 0U) << GetLocation();
847 }
848
849 const byte* Begin() const {
850 return begin_;
851 }
852
853 size_t Size() const {
854 return size_;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700855 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700856
857 // Top-level initializer that calls other Init methods.
858 bool Init();
859
860 // Caches pointers into to the various file sections.
861 void InitMembers();
862
863 // Builds the index of descriptors to class definitions.
864 void InitIndex();
865
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800866 // Returns true if the header magic and version numbers are of the expected values.
jeffhao10037c82012-01-23 15:06:23 -0800867 bool CheckMagicAndVersion() const;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700868
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800869 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800870 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
871 void* context, const byte* stream, LocalInfo* local_in_reg) const;
Elliott Hughes03181a82011-11-17 17:22:21 -0800872
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800873 // The index of descriptors to class definition indexes (as opposed to type id indexes)
Elliott Hughesa0e18062012-04-13 15:59:59 -0700874 typedef SafeMap<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700875 Index index_;
876
877 // The base address of the memory mapping.
Ian Rogers30fab402012-01-23 15:43:46 -0800878 const byte* begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700879
880 // The size of the underlying memory allocation in bytes.
jeffhaof6174e82012-01-31 16:14:17 -0800881 size_t size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700882
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700883 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700884 //
885 // The ClassLinker will use this to match DexFiles the boot class
886 // path to DexCache::GetLocation when loading from an image.
887 const std::string location_;
888
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800889 const uint32_t location_checksum_;
890
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700891 // Manages the underlying memory allocation.
892 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700893
Jesse Wilson6bf19152011-09-29 13:12:33 -0400894 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
895 mutable Mutex dex_object_lock_;
Elliott Hughesf8349362012-06-18 15:00:06 -0700896 mutable jobject dex_object_ GUARDED_BY(dex_object_lock_);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400897
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700898 // Points to the header section.
899 const Header* header_;
900
901 // Points to the base of the string identifier list.
902 const StringId* string_ids_;
903
904 // Points to the base of the type identifier list.
905 const TypeId* type_ids_;
906
907 // Points to the base of the field identifier list.
908 const FieldId* field_ids_;
909
910 // Points to the base of the method identifier list.
911 const MethodId* method_ids_;
912
913 // Points to the base of the prototype identifier list.
914 const ProtoId* proto_ids_;
915
916 // Points to the base of the class definition list.
917 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700918};
919
Ian Rogers0571d352011-11-03 19:51:38 -0700920// Iterate over a dex file's ProtoId's paramters
921class DexFileParameterIterator {
922 public:
923 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
924 : dex_file_(dex_file), size_(0), pos_(0) {
925 type_list_ = dex_file_.GetProtoParameters(proto_id);
926 if (type_list_ != NULL) {
927 size_ = type_list_->Size();
928 }
929 }
930 bool HasNext() const { return pos_ < size_; }
931 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800932 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -0700933 return type_list_->GetTypeItem(pos_).type_idx_;
934 }
935 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800936 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -0700937 }
938 private:
939 const DexFile& dex_file_;
940 const DexFile::TypeList* type_list_;
941 uint32_t size_;
942 uint32_t pos_;
943 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
944};
945
946// Iterate and decode class_data_item
947class ClassDataItemIterator {
948 public:
949 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
950 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
951 ReadClassDataHeader();
952 if (EndOfInstanceFieldsPos() > 0) {
953 ReadClassDataField();
954 } else if (EndOfVirtualMethodsPos() > 0) {
955 ReadClassDataMethod();
956 }
957 }
958 uint32_t NumStaticFields() const {
959 return header_.static_fields_size_;
960 }
961 uint32_t NumInstanceFields() const {
962 return header_.instance_fields_size_;
963 }
964 uint32_t NumDirectMethods() const {
965 return header_.direct_methods_size_;
966 }
967 uint32_t NumVirtualMethods() const {
968 return header_.virtual_methods_size_;
969 }
970 bool HasNextStaticField() const {
971 return pos_ < EndOfStaticFieldsPos();
972 }
973 bool HasNextInstanceField() const {
974 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
975 }
976 bool HasNextDirectMethod() const {
977 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
978 }
979 bool HasNextVirtualMethod() const {
980 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
981 }
982 bool HasNext() const {
983 return pos_ < EndOfVirtualMethodsPos();
984 }
985 void Next() {
986 pos_++;
987 if (pos_ < EndOfStaticFieldsPos()) {
988 last_idx_ = GetMemberIndex();
989 ReadClassDataField();
990 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
991 last_idx_ = 0; // transition to next array, reset last index
992 ReadClassDataField();
993 } else if (pos_ < EndOfInstanceFieldsPos()) {
994 last_idx_ = GetMemberIndex();
995 ReadClassDataField();
996 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
997 last_idx_ = 0; // transition to next array, reset last index
998 ReadClassDataMethod();
999 } else if (pos_ < EndOfDirectMethodsPos()) {
1000 last_idx_ = GetMemberIndex();
1001 ReadClassDataMethod();
1002 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
1003 last_idx_ = 0; // transition to next array, reset last index
1004 ReadClassDataMethod();
1005 } else if (pos_ < EndOfVirtualMethodsPos()) {
1006 last_idx_ = GetMemberIndex();
1007 ReadClassDataMethod();
1008 } else {
1009 DCHECK(!HasNext());
1010 }
1011 }
1012 uint32_t GetMemberIndex() const {
1013 if (pos_ < EndOfInstanceFieldsPos()) {
1014 return last_idx_ + field_.field_idx_delta_;
1015 } else {
1016 CHECK_LT(pos_, EndOfVirtualMethodsPos());
1017 return last_idx_ + method_.method_idx_delta_;
1018 }
1019 }
1020 uint32_t GetMemberAccessFlags() const {
1021 if (pos_ < EndOfInstanceFieldsPos()) {
1022 return field_.access_flags_;
1023 } else {
1024 CHECK_LT(pos_, EndOfVirtualMethodsPos());
1025 return method_.access_flags_;
1026 }
1027 }
1028 const DexFile::CodeItem* GetMethodCodeItem() const {
1029 return dex_file_.GetCodeItem(method_.code_off_);
1030 }
1031 uint32_t GetMethodCodeItemOffset() const {
1032 return method_.code_off_;
1033 }
jeffhao10037c82012-01-23 15:06:23 -08001034 const byte* EndDataPointer() const {
1035 CHECK(!HasNext());
1036 return ptr_pos_;
1037 }
Elliott Hughesa21039c2012-06-21 12:09:25 -07001038
Ian Rogers0571d352011-11-03 19:51:38 -07001039 private:
1040 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
1041 // header for a class_data_item
1042 struct ClassDataHeader {
1043 uint32_t static_fields_size_; // the number of static fields
1044 uint32_t instance_fields_size_; // the number of instance fields
1045 uint32_t direct_methods_size_; // the number of direct methods
1046 uint32_t virtual_methods_size_; // the number of virtual methods
1047 } header_;
1048
1049 // Read and decode header from a class_data_item stream into header
1050 void ReadClassDataHeader();
1051
1052 uint32_t EndOfStaticFieldsPos() const {
1053 return header_.static_fields_size_;
1054 }
1055 uint32_t EndOfInstanceFieldsPos() const {
1056 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
1057 }
1058 uint32_t EndOfDirectMethodsPos() const {
1059 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
1060 }
1061 uint32_t EndOfVirtualMethodsPos() const {
1062 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
1063 }
1064
1065 // A decoded version of the field of a class_data_item
1066 struct ClassDataField {
1067 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
1068 uint32_t access_flags_; // access flags for the field
1069 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
Elliott Hughesa21039c2012-06-21 12:09:25 -07001070
Ian Rogers0571d352011-11-03 19:51:38 -07001071 private:
1072 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001073 };
1074 ClassDataField field_;
Ian Rogers0571d352011-11-03 19:51:38 -07001075
1076 // Read and decode a field from a class_data_item stream into field
1077 void ReadClassDataField();
1078
1079 // A decoded version of the method of a class_data_item
1080 struct ClassDataMethod {
1081 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
1082 uint32_t access_flags_;
1083 uint32_t code_off_;
1084 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
Elliott Hughesa21039c2012-06-21 12:09:25 -07001085
Ian Rogers0571d352011-11-03 19:51:38 -07001086 private:
1087 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001088 };
1089 ClassDataMethod method_;
Ian Rogers0571d352011-11-03 19:51:38 -07001090
1091 // Read and decode a method from a class_data_item stream into method
1092 void ReadClassDataMethod();
1093
1094 const DexFile& dex_file_;
1095 size_t pos_; // integral number of items passed
1096 const byte* ptr_pos_; // pointer into stream of class_data_item
1097 uint32_t last_idx_; // last read field or method index to apply delta to
1098 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
1099};
1100
1101class ClassLinker;
1102class DexCache;
1103class Field;
1104
1105class EncodedStaticFieldValueIterator {
1106 public:
1107 EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache,
1108 ClassLinker* linker, const DexFile::ClassDef& class_def);
1109
1110 void ReadValueToField(Field* field) const;
1111
1112 bool HasNext() { return pos_ < array_size_; }
1113
1114 void Next();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001115
Ian Rogers0571d352011-11-03 19:51:38 -07001116 private:
1117 enum ValueType {
1118 kByte = 0x00,
1119 kShort = 0x02,
1120 kChar = 0x03,
1121 kInt = 0x04,
1122 kLong = 0x06,
1123 kFloat = 0x10,
1124 kDouble = 0x11,
1125 kString = 0x17,
1126 kType = 0x18,
1127 kField = 0x19,
1128 kMethod = 0x1a,
1129 kEnum = 0x1b,
1130 kArray = 0x1c,
1131 kAnnotation = 0x1d,
1132 kNull = 0x1e,
1133 kBoolean = 0x1f
1134 };
1135
1136 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
1137 static const byte kEncodedValueArgShift = 5;
1138
1139 const DexFile& dex_file_;
1140 DexCache* dex_cache_; // dex cache to resolve literal objects
1141 ClassLinker* linker_; // linker to resolve literal objects
1142 size_t array_size_; // size of array
1143 size_t pos_; // current position
1144 const byte* ptr_; // pointer into encoded data array
1145 byte type_; // type of current encoded value
1146 jvalue jval_; // value of current encoded value
1147 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1148};
1149
1150class CatchHandlerIterator {
1151 public:
1152 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
Logan Chien736df022012-04-27 16:25:57 +08001153
1154 CatchHandlerIterator(const DexFile::CodeItem& code_item,
1155 const DexFile::TryItem& try_item);
1156
Ian Rogers0571d352011-11-03 19:51:38 -07001157 explicit CatchHandlerIterator(const byte* handler_data) {
1158 Init(handler_data);
1159 }
1160
1161 uint16_t GetHandlerTypeIndex() const {
1162 return handler_.type_idx_;
1163 }
1164 uint32_t GetHandlerAddress() const {
1165 return handler_.address_;
1166 }
1167 void Next();
1168 bool HasNext() const {
1169 return remaining_count_ != -1 || catch_all_;
1170 }
1171 // End of this set of catch blocks, convenience method to locate next set of catch blocks
1172 const byte* EndDataPointer() const {
1173 CHECK(!HasNext());
1174 return current_data_;
1175 }
Elliott Hughesa21039c2012-06-21 12:09:25 -07001176
Ian Rogers0571d352011-11-03 19:51:38 -07001177 private:
Logan Chien736df022012-04-27 16:25:57 +08001178 void Init(const DexFile::CodeItem& code_item, int32_t offset);
Ian Rogers0571d352011-11-03 19:51:38 -07001179 void Init(const byte* handler_data);
1180
1181 struct CatchHandlerItem {
1182 uint16_t type_idx_; // type index of the caught exception type
1183 uint32_t address_; // handler address
1184 } handler_;
1185 const byte *current_data_; // the current handler in dex file.
1186 int32_t remaining_count_; // number of handlers not read.
1187 bool catch_all_; // is there a handler that will catch all exceptions in case
1188 // that all typed handler does not match.
1189};
1190
Carl Shapiro1fb86202011-06-27 17:43:13 -07001191} // namespace art
1192
1193#endif // ART_SRC_DEX_FILE_H_