blob: 2794af646a59e7ed72f1b4c9b24db96c941fe837 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_DEX_FILE_H_
18#define ART_RUNTIME_DEX_FILE_H_
Carl Shapiro1fb86202011-06-27 17:43:13 -070019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Elliott Hughes0c424cb2011-08-26 10:16:25 -070021#include <string>
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070022#include <vector>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070023
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025#include "base/mutex.h" // For Locks::mutator_lock_.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "globals.h"
Ian Rogers08f753d2012-08-24 14:35:25 -070027#include "invoke_type.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040028#include "jni.h"
Ian Rogers08f753d2012-08-24 14:35:25 -070029#include "modifiers.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070030#include "safe_map.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070031
32namespace art {
33
Brian Carlstrome8104522013-10-15 21:56:36 -070034// TODO: remove dependencies on mirror classes, primarily by moving
35// EncodedStaticFieldValueIterator to its own file.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070037 class ArtField;
38 class ArtMethod;
39 class ClassLoader;
40 class DexCache;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041} // namespace mirror
42class ClassLinker;
Ian Rogers576ca0c2014-06-06 15:58:22 -070043class MemMap;
Ian Rogersd91d6d62013-09-25 20:26:14 -070044class Signature;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045template<class T> class Handle;
Ian Rogersfc0e94b2013-09-23 23:51:32 -070046class StringPiece;
Brian Carlstroma6cc8932012-01-04 14:44:07 -080047class ZipArchive;
48
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070049// TODO: move all of the macro functionality into the DexCache class.
Brian Carlstromf615a612011-07-23 12:50:34 -070050class DexFile {
Carl Shapiro1fb86202011-06-27 17:43:13 -070051 public:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070052 static const byte kDexMagic[];
53 static const byte kDexMagicVersion[];
54 static const size_t kSha1DigestSize = 20;
jeffhao10037c82012-01-23 15:06:23 -080055 static const uint32_t kDexEndianConstant = 0x12345678;
Carl Shapiro80d4dde2011-06-28 16:24:07 -070056
Brian Carlstromb7bbba42011-10-13 14:58:47 -070057 // name of the DexFile entry within a zip archive
58 static const char* kClassesDex;
59
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070060 // The value of an invalid index.
61 static const uint32_t kDexNoIndex = 0xFFFFFFFF;
62
Ian Rogers0571d352011-11-03 19:51:38 -070063 // The value of an invalid index.
64 static const uint16_t kDexNoIndex16 = 0xFFFF;
Carl Shapiro1fb86202011-06-27 17:43:13 -070065
Andreas Gampe833a4852014-05-21 18:46:59 -070066 // The separator charactor in MultiDex locations.
67 static constexpr char kMultiDexSeparator = ':';
68
69 // A string version of the previous. This is a define so that we can merge string literals in the
70 // preprocessor.
71 #define kMultiDexSeparatorString ":"
72
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070073 // Raw header_item.
74 struct Header {
75 uint8_t magic_[8];
Brian Carlstrom7934ac22013-07-26 10:54:15 -070076 uint32_t checksum_; // See also location_checksum_
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070077 uint8_t signature_[kSha1DigestSize];
jeffhaof6174e82012-01-31 16:14:17 -080078 uint32_t file_size_; // size of entire file
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070079 uint32_t header_size_; // offset to start of next section
80 uint32_t endian_tag_;
Ian Rogers0571d352011-11-03 19:51:38 -070081 uint32_t link_size_; // unused
82 uint32_t link_off_; // unused
83 uint32_t map_off_; // unused
84 uint32_t string_ids_size_; // number of StringIds
85 uint32_t string_ids_off_; // file offset of StringIds array
86 uint32_t type_ids_size_; // number of TypeIds, we don't support more than 65535
87 uint32_t type_ids_off_; // file offset of TypeIds array
88 uint32_t proto_ids_size_; // number of ProtoIds, we don't support more than 65535
89 uint32_t proto_ids_off_; // file offset of ProtoIds array
90 uint32_t field_ids_size_; // number of FieldIds
91 uint32_t field_ids_off_; // file offset of FieldIds array
92 uint32_t method_ids_size_; // number of MethodIds
93 uint32_t method_ids_off_; // file offset of MethodIds array
94 uint32_t class_defs_size_; // number of ClassDefs
95 uint32_t class_defs_off_; // file offset of ClassDef array
96 uint32_t data_size_; // unused
97 uint32_t data_off_; // unused
Elliott Hughesa21039c2012-06-21 12:09:25 -070098
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070099 private:
100 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700101 };
Carl Shapiro1fb86202011-06-27 17:43:13 -0700102
jeffhao10037c82012-01-23 15:06:23 -0800103 // Map item type codes.
104 enum {
105 kDexTypeHeaderItem = 0x0000,
106 kDexTypeStringIdItem = 0x0001,
107 kDexTypeTypeIdItem = 0x0002,
108 kDexTypeProtoIdItem = 0x0003,
109 kDexTypeFieldIdItem = 0x0004,
110 kDexTypeMethodIdItem = 0x0005,
111 kDexTypeClassDefItem = 0x0006,
112 kDexTypeMapList = 0x1000,
113 kDexTypeTypeList = 0x1001,
114 kDexTypeAnnotationSetRefList = 0x1002,
115 kDexTypeAnnotationSetItem = 0x1003,
116 kDexTypeClassDataItem = 0x2000,
117 kDexTypeCodeItem = 0x2001,
118 kDexTypeStringDataItem = 0x2002,
119 kDexTypeDebugInfoItem = 0x2003,
120 kDexTypeAnnotationItem = 0x2004,
121 kDexTypeEncodedArrayItem = 0x2005,
122 kDexTypeAnnotationsDirectoryItem = 0x2006,
123 };
124
125 struct MapItem {
126 uint16_t type_;
127 uint16_t unused_;
128 uint32_t size_;
129 uint32_t offset_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700130
jeffhao10037c82012-01-23 15:06:23 -0800131 private:
132 DISALLOW_COPY_AND_ASSIGN(MapItem);
133 };
134
135 struct MapList {
136 uint32_t size_;
137 MapItem list_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700138
jeffhao10037c82012-01-23 15:06:23 -0800139 private:
140 DISALLOW_COPY_AND_ASSIGN(MapList);
141 };
142
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700143 // Raw string_id_item.
144 struct StringId {
145 uint32_t string_data_off_; // offset in bytes from the base address
Elliott Hughesa21039c2012-06-21 12:09:25 -0700146
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700147 private:
148 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700149 };
150
151 // Raw type_id_item.
152 struct TypeId {
153 uint32_t descriptor_idx_; // index into string_ids
Elliott Hughesa21039c2012-06-21 12:09:25 -0700154
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700155 private:
156 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700157 };
158
159 // Raw field_id_item.
160 struct FieldId {
Ian Rogers0571d352011-11-03 19:51:38 -0700161 uint16_t class_idx_; // index into type_ids_ array for defining class
162 uint16_t type_idx_; // index into type_ids_ array for field type
163 uint32_t name_idx_; // index into string_ids_ array for field name
Elliott Hughesa21039c2012-06-21 12:09:25 -0700164
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700165 private:
166 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700167 };
168
169 // Raw method_id_item.
170 struct MethodId {
Ian Rogers0571d352011-11-03 19:51:38 -0700171 uint16_t class_idx_; // index into type_ids_ array for defining class
172 uint16_t proto_idx_; // index into proto_ids_ array for method prototype
173 uint32_t name_idx_; // index into string_ids_ array for method name
Elliott Hughesa21039c2012-06-21 12:09:25 -0700174
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700175 private:
176 DISALLOW_COPY_AND_ASSIGN(MethodId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700177 };
178
179 // Raw proto_id_item.
180 struct ProtoId {
Ian Rogers0571d352011-11-03 19:51:38 -0700181 uint32_t shorty_idx_; // index into string_ids array for shorty descriptor
182 uint16_t return_type_idx_; // index into type_ids array for return type
183 uint16_t pad_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700184 uint32_t parameters_off_; // file offset to type_list for parameter types
Elliott Hughesa21039c2012-06-21 12:09:25 -0700185
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700186 private:
187 DISALLOW_COPY_AND_ASSIGN(ProtoId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700188 };
189
190 // Raw class_def_item.
191 struct ClassDef {
Ian Rogers0571d352011-11-03 19:51:38 -0700192 uint16_t class_idx_; // index into type_ids_ array for this class
193 uint16_t pad1_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700194 uint32_t access_flags_;
Ian Rogers0571d352011-11-03 19:51:38 -0700195 uint16_t superclass_idx_; // index into type_ids_ array for superclass
196 uint16_t pad2_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700197 uint32_t interfaces_off_; // file offset to TypeList
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700198 uint32_t source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700199 uint32_t annotations_off_; // file offset to annotations_directory_item
200 uint32_t class_data_off_; // file offset to class_data_item
201 uint32_t static_values_off_; // file offset to EncodedArray
Elliott Hughesa21039c2012-06-21 12:09:25 -0700202
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700203 private:
204 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700205 };
206
207 // Raw type_item.
208 struct TypeItem {
209 uint16_t type_idx_; // index into type_ids section
Elliott Hughesa21039c2012-06-21 12:09:25 -0700210
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700211 private:
212 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700213 };
214
215 // Raw type_list.
216 class TypeList {
217 public:
218 uint32_t Size() const {
219 return size_;
220 }
221
222 const TypeItem& GetTypeItem(uint32_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200223 DCHECK_LT(idx, this->size_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700224 return this->list_[idx];
225 }
226
227 private:
228 uint32_t size_; // size of the list, in entries
229 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700230 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700231 };
232
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700233 // Raw code_item.
234 struct CodeItem {
235 uint16_t registers_size_;
236 uint16_t ins_size_;
237 uint16_t outs_size_;
238 uint16_t tries_size_;
239 uint32_t debug_info_off_; // file offset to debug info stream
Ian Rogersd81871c2011-10-03 13:57:23 -0700240 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700241 uint16_t insns_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700242
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700243 private:
244 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700245 };
246
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700247 // Raw try_item.
248 struct TryItem {
249 uint32_t start_addr_;
250 uint16_t insn_count_;
251 uint16_t handler_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700252
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700253 private:
254 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700255 };
256
jeffhao10037c82012-01-23 15:06:23 -0800257 // Annotation constants.
258 enum {
259 kDexVisibilityBuild = 0x00, /* annotation visibility */
260 kDexVisibilityRuntime = 0x01,
261 kDexVisibilitySystem = 0x02,
262
263 kDexAnnotationByte = 0x00,
264 kDexAnnotationShort = 0x02,
265 kDexAnnotationChar = 0x03,
266 kDexAnnotationInt = 0x04,
267 kDexAnnotationLong = 0x06,
268 kDexAnnotationFloat = 0x10,
269 kDexAnnotationDouble = 0x11,
270 kDexAnnotationString = 0x17,
271 kDexAnnotationType = 0x18,
272 kDexAnnotationField = 0x19,
273 kDexAnnotationMethod = 0x1a,
274 kDexAnnotationEnum = 0x1b,
275 kDexAnnotationArray = 0x1c,
276 kDexAnnotationAnnotation = 0x1d,
277 kDexAnnotationNull = 0x1e,
278 kDexAnnotationBoolean = 0x1f,
279
280 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
281 kDexAnnotationValueArgShift = 5,
282 };
283
284 struct AnnotationsDirectoryItem {
285 uint32_t class_annotations_off_;
286 uint32_t fields_size_;
287 uint32_t methods_size_;
288 uint32_t parameters_size_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700289
jeffhao10037c82012-01-23 15:06:23 -0800290 private:
291 DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
292 };
293
294 struct FieldAnnotationsItem {
295 uint32_t field_idx_;
296 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700297
jeffhao10037c82012-01-23 15:06:23 -0800298 private:
299 DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem);
300 };
301
302 struct MethodAnnotationsItem {
303 uint32_t method_idx_;
304 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700305
jeffhao10037c82012-01-23 15:06:23 -0800306 private:
307 DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem);
308 };
309
310 struct ParameterAnnotationsItem {
311 uint32_t method_idx_;
312 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700313
jeffhao10037c82012-01-23 15:06:23 -0800314 private:
315 DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem);
316 };
317
318 struct AnnotationSetRefItem {
319 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700320
jeffhao10037c82012-01-23 15:06:23 -0800321 private:
322 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem);
323 };
324
325 struct AnnotationSetRefList {
326 uint32_t size_;
327 AnnotationSetRefItem list_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700328
jeffhao10037c82012-01-23 15:06:23 -0800329 private:
330 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList);
331 };
332
333 struct AnnotationSetItem {
334 uint32_t size_;
335 uint32_t entries_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700336
jeffhao10037c82012-01-23 15:06:23 -0800337 private:
338 DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
339 };
340
341 struct AnnotationItem {
342 uint8_t visibility_;
343 uint8_t annotation_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700344
jeffhao10037c82012-01-23 15:06:23 -0800345 private:
346 DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
347 };
348
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700349 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
350 typedef std::vector<const DexFile*> ClassPath;
351
352 // Search a collection of DexFiles for a descriptor
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700353 static ClassPathEntry FindInClassPath(const char* descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700354 const ClassPath& class_path);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700355
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800356 // Returns the checksum of a file for comparison with GetLocationChecksum().
357 // For .dex files, this is the header checksum.
358 // For zip files, this is the classes.dex zip entry CRC32 checksum.
359 // Return true if the checksum could be found, false otherwise.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700360 static bool GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700361
Andreas Gampe833a4852014-05-21 18:46:59 -0700362 // Opens .dex files found in the container, guessing the container format based on file extension.
363 static bool Open(const char* filename, const char* location, std::string* error_msg,
364 std::vector<const DexFile*>* dex_files);
jeffhao262bf462011-10-20 18:36:32 -0700365
Brian Carlstrom89521892011-12-07 22:05:07 -0800366 // Opens .dex file, backed by existing memory
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800367 static const DexFile* Open(const uint8_t* base, size_t size,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700368 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700369 uint32_t location_checksum,
370 std::string* error_msg) {
371 return OpenMemory(base, size, location, location_checksum, NULL, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800372 }
373
Andreas Gampe833a4852014-05-21 18:46:59 -0700374 // Open all classesXXX.dex files from a zip archive.
375 static bool OpenFromZip(const ZipArchive& zip_archive, const std::string& location,
376 std::string* error_msg, std::vector<const DexFile*>* dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800377
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700378 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700379 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700380
Brian Carlstroma663ea52011-08-19 23:33:41 -0700381 const std::string& GetLocation() const {
382 return location_;
383 }
384
Andreas Gampecb8f9e82014-07-24 15:35:50 -0700385 // For normal dex files, location and base location coincide. If a dex file is part of a multidex
386 // archive, the base location is the name of the originating jar/apk, stripped of any internal
387 // classes*.dex path.
388 const std::string GetBaseLocation() const {
389 if (IsMultiDexLocation(location_.c_str())) {
390 std::pair<const char*, const char*> pair = SplitMultiDexLocation(location_.c_str());
391 std::string res(pair.first);
392 delete[] pair.first;
393 return res;
394 } else {
395 return location_;
396 }
397 }
398
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800399 // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header.
400 // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex.
401 uint32_t GetLocationChecksum() const {
402 return location_checksum_;
403 }
404
Brian Carlstroma663ea52011-08-19 23:33:41 -0700405 const Header& GetHeader() const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700406 DCHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700407 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700408 }
409
Ian Rogers0571d352011-11-03 19:51:38 -0700410 // Decode the dex magic version
Ian Rogersd81871c2011-10-03 13:57:23 -0700411 uint32_t GetVersion() const;
412
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800413 // Returns true if the byte string points to the magic value.
414 static bool IsMagicValid(const byte* magic);
415
416 // Returns true if the byte string after the magic is the correct value.
417 static bool IsVersionValid(const byte* magic);
418
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700419 // Returns the number of string identifiers in the .dex file.
420 size_t NumStringIds() const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700421 DCHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700422 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700423 }
424
Ian Rogers0571d352011-11-03 19:51:38 -0700425 // Returns the StringId at the specified index.
426 const StringId& GetStringId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700427 DCHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700428 return string_ids_[idx];
429 }
430
431 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800432 CHECK_GE(&string_id, string_ids_) << GetLocation();
433 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700434 return &string_id - string_ids_;
435 }
436
437 int32_t GetStringLength(const StringId& string_id) const;
438
Ian Rogersdfb325e2013-10-30 01:00:44 -0700439 // Returns a pointer to the UTF-8 string data referred to by the given string_id as well as the
440 // length of the string when decoded as a UTF-16 string. Note the UTF-16 length is not the same
441 // as the string length of the string data.
442 const char* GetStringDataAndUtf16Length(const StringId& string_id, uint32_t* utf16_length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700443
444 const char* GetStringData(const StringId& string_id) const {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700445 uint32_t ignored;
446 return GetStringDataAndUtf16Length(string_id, &ignored);
Ian Rogers0571d352011-11-03 19:51:38 -0700447 }
448
Ian Rogersdfb325e2013-10-30 01:00:44 -0700449 // Index version of GetStringDataAndUtf16Length.
450 const char* StringDataAndUtf16LengthByIdx(uint32_t idx, uint32_t* utf16_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700451 if (idx == kDexNoIndex) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700452 *utf16_length = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700453 return NULL;
454 }
455 const StringId& string_id = GetStringId(idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700456 return GetStringDataAndUtf16Length(string_id, utf16_length);
Ian Rogers0571d352011-11-03 19:51:38 -0700457 }
458
459 const char* StringDataByIdx(uint32_t idx) const {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800460 uint32_t unicode_length;
Ian Rogersdfb325e2013-10-30 01:00:44 -0700461 return StringDataAndUtf16LengthByIdx(idx, &unicode_length);
Ian Rogers0571d352011-11-03 19:51:38 -0700462 }
463
Ian Rogers637c65b2013-05-31 11:46:00 -0700464 // Looks up a string id for a given modified utf8 string.
465 const StringId* FindStringId(const char* string) const;
466
467 // Looks up a string id for a given utf16 string.
468 const StringId* FindStringId(const uint16_t* string) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700469
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700470 // Returns the number of type identifiers in the .dex file.
471 size_t NumTypeIds() const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700472 DCHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700473 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700474 }
475
Ian Rogers0571d352011-11-03 19:51:38 -0700476 // Returns the TypeId at the specified index.
477 const TypeId& GetTypeId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700478 DCHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700479 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700480 }
481
Ian Rogers0571d352011-11-03 19:51:38 -0700482 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800483 CHECK_GE(&type_id, type_ids_) << GetLocation();
484 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700485 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800486 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700487 return static_cast<uint16_t>(result);
488 }
489
490 // Get the descriptor string associated with a given type index.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800491 const char* StringByTypeIdx(uint32_t idx, uint32_t* unicode_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700492 const TypeId& type_id = GetTypeId(idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700493 return StringDataAndUtf16LengthByIdx(type_id.descriptor_idx_, unicode_length);
Ian Rogers0571d352011-11-03 19:51:38 -0700494 }
495
496 const char* StringByTypeIdx(uint32_t idx) const {
497 const TypeId& type_id = GetTypeId(idx);
498 return StringDataByIdx(type_id.descriptor_idx_);
499 }
500
501 // Returns the type descriptor string of a type id.
502 const char* GetTypeDescriptor(const TypeId& type_id) const {
503 return StringDataByIdx(type_id.descriptor_idx_);
504 }
505
506 // Looks up a type for the given string index
507 const TypeId* FindTypeId(uint32_t string_idx) const;
508
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700509 // Returns the number of field identifiers in the .dex file.
510 size_t NumFieldIds() const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700511 DCHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700512 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700513 }
514
Ian Rogers0571d352011-11-03 19:51:38 -0700515 // Returns the FieldId at the specified index.
516 const FieldId& GetFieldId(uint32_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200517 DCHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700518 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700519 }
520
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800521 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800522 CHECK_GE(&field_id, field_ids_) << GetLocation();
523 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800524 return &field_id - field_ids_;
525 }
526
527 // Looks up a field by its declaring class, name and type
528 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
529 const DexFile::StringId& name,
530 const DexFile::TypeId& type) const;
531
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700532 // Returns the declaring class descriptor string of a field id.
533 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700534 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
535 return GetTypeDescriptor(type_id);
536 }
537
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700538 // Returns the class descriptor string of a field id.
539 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
540 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
541 return GetTypeDescriptor(type_id);
542 }
543
Brian Carlstromb9edb842011-08-28 16:31:06 -0700544 // Returns the name of a field id.
545 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700546 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700547 }
548
Ian Rogers0571d352011-11-03 19:51:38 -0700549 // Returns the number of method identifiers in the .dex file.
550 size_t NumMethodIds() const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700551 DCHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700552 return header_->method_ids_size_;
553 }
554
555 // Returns the MethodId at the specified index.
556 const MethodId& GetMethodId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700557 DCHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700558 return method_ids_[idx];
559 }
560
561 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800562 CHECK_GE(&method_id, method_ids_) << GetLocation();
563 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700564 return &method_id - method_ids_;
565 }
566
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800567 // Looks up a method by its declaring class, name and proto_id
568 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
569 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700570 const DexFile::ProtoId& signature) const;
571
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700572 // Returns the declaring class descriptor string of a method id.
573 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700574 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
575 return GetTypeDescriptor(type_id);
576 }
577
jeffhao98eacac2011-09-14 16:11:53 -0700578 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700579 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
580 return GetProtoId(method_id.proto_idx_);
581 }
582
Ian Rogersd91d6d62013-09-25 20:26:14 -0700583 // Returns a representation of the signature of a method id.
584 const Signature GetMethodSignature(const MethodId& method_id) const;
jeffhao98eacac2011-09-14 16:11:53 -0700585
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700586 // Returns the name of a method id.
587 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700588 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700589 }
590
Ian Rogers0571d352011-11-03 19:51:38 -0700591 // Returns the shorty of a method id.
592 const char* GetMethodShorty(const MethodId& method_id) const {
593 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700594 }
Elliott Hughes45651fd2012-02-21 15:48:20 -0800595 const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const {
Ian Rogerscf5077a2013-10-31 12:37:54 -0700596 // Using the UTF16 length is safe here as shorties are guaranteed to be ASCII characters.
Ian Rogersdfb325e2013-10-30 01:00:44 -0700597 return StringDataAndUtf16LengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800598 }
Ian Rogers0571d352011-11-03 19:51:38 -0700599 // Returns the number of class definitions in the .dex file.
600 size_t NumClassDefs() const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700601 DCHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700602 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700603 }
604
605 // Returns the ClassDef at the specified index.
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700606 const ClassDef& GetClassDef(uint16_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200607 DCHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700608 return class_defs_[idx];
609 }
610
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700611 uint16_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800612 CHECK_GE(&class_def, class_defs_) << GetLocation();
613 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700614 return &class_def - class_defs_;
615 }
616
617 // Returns the class descriptor string of a class definition.
618 const char* GetClassDescriptor(const ClassDef& class_def) const {
619 return StringByTypeIdx(class_def.class_idx_);
620 }
621
622 // Looks up a class definition by its class descriptor.
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700623 const ClassDef* FindClassDef(const char* descriptor) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700624
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700625 // Looks up a class definition by its type index.
626 const ClassDef* FindClassDef(uint16_t type_idx) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700627
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700628 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
629 if (class_def.interfaces_off_ == 0) {
630 return NULL;
631 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800632 const byte* addr = begin_ + class_def.interfaces_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700633 return reinterpret_cast<const TypeList*>(addr);
634 }
635 }
636
Ian Rogers0571d352011-11-03 19:51:38 -0700637 // Returns a pointer to the raw memory mapped class_data_item
638 const byte* GetClassData(const ClassDef& class_def) const {
639 if (class_def.class_data_off_ == 0) {
640 return NULL;
641 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800642 return begin_ + class_def.class_data_off_;
Ian Rogers0571d352011-11-03 19:51:38 -0700643 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700644 }
645
Ian Rogers0571d352011-11-03 19:51:38 -0700646 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800647 const CodeItem* GetCodeItem(const uint32_t code_off) const {
648 if (code_off == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700649 return NULL; // native or abstract method
650 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800651 const byte* addr = begin_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700652 return reinterpret_cast<const CodeItem*>(addr);
653 }
654 }
655
Ian Rogers0571d352011-11-03 19:51:38 -0700656 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
657 return StringByTypeIdx(proto_id.return_type_idx_);
658 }
659
660 // Returns the number of prototype identifiers in the .dex file.
661 size_t NumProtoIds() const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700662 DCHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700663 return header_->proto_ids_size_;
664 }
665
666 // Returns the ProtoId at the specified index.
667 const ProtoId& GetProtoId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700668 DCHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700669 return proto_ids_[idx];
670 }
671
672 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800673 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
674 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700675 return &proto_id - proto_ids_;
676 }
677
678 // Looks up a proto id for a given return type and signature type list
Ian Rogersd91d6d62013-09-25 20:26:14 -0700679 const ProtoId* FindProtoId(uint16_t return_type_idx,
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000680 const uint16_t* signature_type_idxs, uint32_t signature_length) const;
681 const ProtoId* FindProtoId(uint16_t return_type_idx,
682 const std::vector<uint16_t>& signature_type_idxs) const {
683 return FindProtoId(return_type_idx, &signature_type_idxs[0], signature_type_idxs.size());
684 }
Ian Rogers0571d352011-11-03 19:51:38 -0700685
686 // Given a signature place the type ids into the given vector, returns true on success
Ian Rogersd91d6d62013-09-25 20:26:14 -0700687 bool CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx,
688 std::vector<uint16_t>* param_type_idxs) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700689
Ian Rogersd91d6d62013-09-25 20:26:14 -0700690 // Create a Signature from the given string signature or return Signature::NoSignature if not
691 // possible.
692 const Signature CreateSignature(const StringPiece& signature) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700693
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700694 // Returns the short form method descriptor for the given prototype.
695 const char* GetShorty(uint32_t proto_idx) const {
696 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700697 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700698 }
699
700 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
701 if (proto_id.parameters_off_ == 0) {
702 return NULL;
703 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800704 const byte* addr = begin_ + proto_id.parameters_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700705 return reinterpret_cast<const TypeList*>(addr);
706 }
707 }
708
Ian Rogers0571d352011-11-03 19:51:38 -0700709 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700710 if (class_def.static_values_off_ == 0) {
711 return 0;
712 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800713 return begin_ + class_def.static_values_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700714 }
715 }
716
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800717 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700718
719 // Get the base of the encoded data for the given DexCode.
Ian Rogers0571d352011-11-03 19:51:38 -0700720 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
721 const byte* handler_data =
722 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700723 return handler_data + offset;
724 }
725
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700726 // Find which try region is associated with the given address (ie dex pc). Returns -1 if none.
727 static int32_t FindTryItem(const CodeItem &code_item, uint32_t address);
728
729 // Find the handler offset associated with the given address (ie dex pc). Returns -1 if none.
730 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700731
Shih-wei Liao195487c2011-08-20 13:29:04 -0700732 // Get the pointer to the start of the debugging data
Ian Rogers0571d352011-11-03 19:51:38 -0700733 const byte* GetDebugInfoStream(const CodeItem* code_item) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700734 if (code_item->debug_info_off_ == 0) {
735 return NULL;
736 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800737 return begin_ + code_item->debug_info_off_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700738 }
739 }
740
741 // Callback for "new position table entry".
742 // Returning true causes the decoder to stop early.
Elliott Hughes2435a572012-02-17 16:07:41 -0800743 typedef bool (*DexDebugNewPositionCb)(void* context, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700744
745 // Callback for "new locals table entry". "signature" is an empty string
746 // if no signature is available for an entry.
Elliott Hughes2435a572012-02-17 16:07:41 -0800747 typedef void (*DexDebugNewLocalCb)(void* context, uint16_t reg,
Elliott Hughes24edeb52012-06-18 15:29:46 -0700748 uint32_t start_address,
749 uint32_t end_address,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700750 const char* name,
751 const char* descriptor,
752 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700753
Elliott Hughes2435a572012-02-17 16:07:41 -0800754 static bool LineNumForPcCb(void* context, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700755
756 // Debug info opcodes and constants
757 enum {
758 DBG_END_SEQUENCE = 0x00,
759 DBG_ADVANCE_PC = 0x01,
760 DBG_ADVANCE_LINE = 0x02,
761 DBG_START_LOCAL = 0x03,
762 DBG_START_LOCAL_EXTENDED = 0x04,
763 DBG_END_LOCAL = 0x05,
764 DBG_RESTART_LOCAL = 0x06,
765 DBG_SET_PROLOGUE_END = 0x07,
766 DBG_SET_EPILOGUE_BEGIN = 0x08,
767 DBG_SET_FILE = 0x09,
768 DBG_FIRST_SPECIAL = 0x0a,
769 DBG_LINE_BASE = -4,
770 DBG_LINE_RANGE = 15,
771 };
772
773 struct LocalInfo {
Ian Rogersca190662012-06-26 15:45:57 -0700774 LocalInfo()
775 : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0), is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700776
Ian Rogers0571d352011-11-03 19:51:38 -0700777 const char* name_; // E.g., list
778 const char* descriptor_; // E.g., Ljava/util/LinkedList;
779 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
780 uint16_t start_address_; // PC location where the local is first defined.
781 bool is_live_; // Is the local defined and live.
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700782
783 private:
784 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700785 };
786
787 struct LineNumFromPcContext {
Ian Rogersca190662012-06-26 15:45:57 -0700788 LineNumFromPcContext(uint32_t address, uint32_t line_num)
789 : address_(address), line_num_(line_num) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700790 uint32_t address_;
791 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700792 private:
793 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700794 };
795
Elliott Hughes2435a572012-02-17 16:07:41 -0800796 void InvokeLocalCbIfLive(void* context, int reg, uint32_t end_address,
Brian Carlstrom78128a62011-09-15 17:21:19 -0700797 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700798 if (local_cb != NULL && local_in_reg[reg].is_live_) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800799 local_cb(context, reg, local_in_reg[reg].start_address_, end_address,
Elliott Hughesdbb40792011-11-18 17:05:22 -0800800 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
801 local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : "");
Shih-wei Liao195487c2011-08-20 13:29:04 -0700802 }
803 }
804
805 // Determine the source file line number based on the program counter.
806 // "pc" is an offset, in 16-bit units, from the start of the method's code.
807 //
808 // Returns -1 if no match was found (possibly because the source files were
809 // compiled without "-g", so no line number information is present).
810 // Returns -2 for native methods (as expected in exception traces).
811 //
812 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800813 int32_t GetLineNumFromPC(mirror::ArtMethod* method, uint32_t rel_pc) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700814 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700815
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800816 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800817 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
818 void* context) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700819
Ian Rogers0571d352011-11-03 19:51:38 -0700820 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700821 if (class_def.source_file_idx_ == 0xffffffff) {
822 return NULL;
823 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700824 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700825 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700826 }
827
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800828 int GetPermissions() const;
Ian Rogers1c849e52012-06-28 14:00:33 -0700829
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200830 bool IsReadOnly() const;
831
Brian Carlstrome0948e12013-08-29 09:36:15 -0700832 bool EnableWrite() const;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200833
Brian Carlstrome0948e12013-08-29 09:36:15 -0700834 bool DisableWrite() const;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200835
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700836 const byte* Begin() const {
837 return begin_;
838 }
839
840 size_t Size() const {
841 return size_;
842 }
843
Calin Juravle4e1d5792014-07-15 23:56:47 +0100844 static std::string GetMultiDexClassesDexName(size_t number, const char* dex_location);
845
846 // Returns the canonical form of the given dex location.
847 //
848 // There are different flavors of "dex locations" as follows:
849 // the file name of a dex file:
850 // The actual file path that the dex file has on disk.
851 // dex_location:
852 // This acts as a key for the class linker to know which dex file to load.
853 // It may correspond to either an old odex file or a particular dex file
854 // inside an oat file. In the first case it will also match the file name
855 // of the dex file. In the second case (oat) it will include the file name
856 // and possibly some multidex annotation to uniquely identify it.
857 // canonical_dex_location:
858 // the dex_location where it's file name part has been made canonical.
859 static std::string GetDexCanonicalLocation(const char* dex_location);
860
Carl Shapiro1fb86202011-06-27 17:43:13 -0700861 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700862 // Opens a .dex file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700863 static const DexFile* OpenFile(int fd, const char* location, bool verify, std::string* error_msg);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700864
Andreas Gampe833a4852014-05-21 18:46:59 -0700865 // Opens dex files from within a .jar, .zip, or .apk file
866 static bool OpenZip(int fd, const std::string& location, std::string* error_msg,
867 std::vector<const DexFile*>* dex_files);
868
869 enum class ZipOpenErrorCode { // private
870 kNoError,
871 kEntryNotFound,
872 kExtractToMemoryError,
873 kDexFileError,
874 kMakeReadOnlyError,
875 kVerifyError
876 };
877
878 // Opens .dex file from the entry_name in a zip archive. error_code is undefined when non-nullptr
879 // return.
880 static const DexFile* Open(const ZipArchive& zip_archive, const char* entry_name,
881 const std::string& location, std::string* error_msg,
882 ZipOpenErrorCode* error_code);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700883
Brian Carlstrom89521892011-12-07 22:05:07 -0800884 // Opens a .dex file at the given address backed by a MemMap
885 static const DexFile* OpenMemory(const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800886 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700887 MemMap* mem_map,
888 std::string* error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800889
890 // Opens a .dex file at the given address, optionally backed by a MemMap
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700891 static const DexFile* OpenMemory(const byte* dex_file,
jeffhaof6174e82012-01-31 16:14:17 -0800892 size_t size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700893 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800894 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700895 MemMap* mem_map,
896 std::string* error_msg);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700897
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800898 DexFile(const byte* base, size_t size,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700899 const std::string& location,
900 uint32_t location_checksum,
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800901 MemMap* mem_map);
jeffhaof6174e82012-01-31 16:14:17 -0800902
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700903 // Top-level initializer that calls other Init methods.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700904 bool Init(std::string* error_msg);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700905
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800906 // Returns true if the header magic and version numbers are of the expected values.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700907 bool CheckMagicAndVersion(std::string* error_msg) const;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700908
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800910 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
911 void* context, const byte* stream, LocalInfo* local_in_reg) const;
Elliott Hughes03181a82011-11-17 17:22:21 -0800912
Andreas Gampe833a4852014-05-21 18:46:59 -0700913 // Check whether a location denotes a multidex dex file. This is a very simple check: returns
914 // whether the string contains the separator character.
915 static bool IsMultiDexLocation(const char* location);
916
917 // Splits a multidex location at the last separator character. The second component is a pointer
918 // to the character after the separator. The first is a copy of the substring up to the separator.
919 //
920 // Note: It's the caller's job to free the first component of the returned pair.
921 // Bug 15313523: gcc/libc++ don't allow a unique_ptr for the first component
922 static std::pair<const char*, const char*> SplitMultiDexLocation(const char* location);
923
924
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700925 // The base address of the memory mapping.
Ian Rogers62d6c772013-02-27 08:32:07 -0800926 const byte* const begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700927
928 // The size of the underlying memory allocation in bytes.
Ian Rogers62d6c772013-02-27 08:32:07 -0800929 const size_t size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700930
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700931 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700932 //
933 // The ClassLinker will use this to match DexFiles the boot class
934 // path to DexCache::GetLocation when loading from an image.
935 const std::string location_;
936
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800937 const uint32_t location_checksum_;
938
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700939 // Manages the underlying memory allocation.
Ian Rogers700a4022014-05-19 16:49:03 -0700940 std::unique_ptr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700941
942 // Points to the header section.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800943 const Header* const header_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700944
945 // Points to the base of the string identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800946 const StringId* const string_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700947
948 // Points to the base of the type identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800949 const TypeId* const type_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700950
951 // Points to the base of the field identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800952 const FieldId* const field_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700953
954 // Points to the base of the method identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800955 const MethodId* const method_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700956
957 // Points to the base of the prototype identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800958 const ProtoId* const proto_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700959
960 // Points to the base of the class definition list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800961 const ClassDef* const class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700962};
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800963std::ostream& operator<<(std::ostream& os, const DexFile& dex_file);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700964
Ian Rogers0571d352011-11-03 19:51:38 -0700965// Iterate over a dex file's ProtoId's paramters
966class DexFileParameterIterator {
967 public:
968 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
969 : dex_file_(dex_file), size_(0), pos_(0) {
970 type_list_ = dex_file_.GetProtoParameters(proto_id);
971 if (type_list_ != NULL) {
972 size_ = type_list_->Size();
973 }
974 }
975 bool HasNext() const { return pos_ < size_; }
976 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800977 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -0700978 return type_list_->GetTypeItem(pos_).type_idx_;
979 }
980 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800981 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -0700982 }
983 private:
984 const DexFile& dex_file_;
985 const DexFile::TypeList* type_list_;
986 uint32_t size_;
987 uint32_t pos_;
988 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
989};
990
Ian Rogersd91d6d62013-09-25 20:26:14 -0700991// Abstract the signature of a method.
992class Signature {
993 public:
994 std::string ToString() const;
995
996 static Signature NoSignature() {
997 return Signature();
998 }
999
Ian Rogersdfb325e2013-10-30 01:00:44 -07001000 bool operator==(const Signature& rhs) const;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001001 bool operator!=(const Signature& rhs) const {
1002 return !(*this == rhs);
1003 }
1004
Vladimir Markod9cffea2013-11-25 15:08:02 +00001005 bool operator==(const StringPiece& rhs) const;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001006
1007 private:
1008 Signature(const DexFile* dex, const DexFile::ProtoId& proto) : dex_file_(dex), proto_id_(&proto) {
1009 }
1010
1011 Signature() : dex_file_(nullptr), proto_id_(nullptr) {
1012 }
1013
1014 friend class DexFile;
1015
1016 const DexFile* const dex_file_;
1017 const DexFile::ProtoId* const proto_id_;
1018};
1019std::ostream& operator<<(std::ostream& os, const Signature& sig);
1020
Ian Rogers0571d352011-11-03 19:51:38 -07001021// Iterate and decode class_data_item
1022class ClassDataItemIterator {
1023 public:
1024 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
1025 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
1026 ReadClassDataHeader();
1027 if (EndOfInstanceFieldsPos() > 0) {
1028 ReadClassDataField();
1029 } else if (EndOfVirtualMethodsPos() > 0) {
1030 ReadClassDataMethod();
1031 }
1032 }
1033 uint32_t NumStaticFields() const {
1034 return header_.static_fields_size_;
1035 }
1036 uint32_t NumInstanceFields() const {
1037 return header_.instance_fields_size_;
1038 }
1039 uint32_t NumDirectMethods() const {
1040 return header_.direct_methods_size_;
1041 }
1042 uint32_t NumVirtualMethods() const {
1043 return header_.virtual_methods_size_;
1044 }
1045 bool HasNextStaticField() const {
1046 return pos_ < EndOfStaticFieldsPos();
1047 }
1048 bool HasNextInstanceField() const {
1049 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
1050 }
1051 bool HasNextDirectMethod() const {
1052 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
1053 }
1054 bool HasNextVirtualMethod() const {
1055 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
1056 }
1057 bool HasNext() const {
1058 return pos_ < EndOfVirtualMethodsPos();
1059 }
Ian Rogers637c65b2013-05-31 11:46:00 -07001060 inline void Next() {
Ian Rogers0571d352011-11-03 19:51:38 -07001061 pos_++;
1062 if (pos_ < EndOfStaticFieldsPos()) {
1063 last_idx_ = GetMemberIndex();
1064 ReadClassDataField();
1065 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
1066 last_idx_ = 0; // transition to next array, reset last index
1067 ReadClassDataField();
1068 } else if (pos_ < EndOfInstanceFieldsPos()) {
1069 last_idx_ = GetMemberIndex();
1070 ReadClassDataField();
1071 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
1072 last_idx_ = 0; // transition to next array, reset last index
1073 ReadClassDataMethod();
1074 } else if (pos_ < EndOfDirectMethodsPos()) {
1075 last_idx_ = GetMemberIndex();
1076 ReadClassDataMethod();
1077 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
1078 last_idx_ = 0; // transition to next array, reset last index
1079 ReadClassDataMethod();
1080 } else if (pos_ < EndOfVirtualMethodsPos()) {
1081 last_idx_ = GetMemberIndex();
1082 ReadClassDataMethod();
1083 } else {
1084 DCHECK(!HasNext());
1085 }
1086 }
1087 uint32_t GetMemberIndex() const {
1088 if (pos_ < EndOfInstanceFieldsPos()) {
1089 return last_idx_ + field_.field_idx_delta_;
1090 } else {
Sebastien Hertzb24bd992013-08-02 15:19:09 +02001091 DCHECK_LT(pos_, EndOfVirtualMethodsPos());
Ian Rogers0571d352011-11-03 19:51:38 -07001092 return last_idx_ + method_.method_idx_delta_;
1093 }
1094 }
1095 uint32_t GetMemberAccessFlags() const {
1096 if (pos_ < EndOfInstanceFieldsPos()) {
1097 return field_.access_flags_;
1098 } else {
Sebastien Hertzb24bd992013-08-02 15:19:09 +02001099 DCHECK_LT(pos_, EndOfVirtualMethodsPos());
Ian Rogers0571d352011-11-03 19:51:38 -07001100 return method_.access_flags_;
1101 }
1102 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001103 InvokeType GetMethodInvokeType(const DexFile::ClassDef& class_def) const {
1104 if (HasNextDirectMethod()) {
Brian Carlstromdf629502013-07-17 22:39:56 -07001105 if ((GetMemberAccessFlags() & kAccStatic) != 0) {
Ian Rogers08f753d2012-08-24 14:35:25 -07001106 return kStatic;
1107 } else {
1108 return kDirect;
1109 }
1110 } else {
Sebastien Hertzb24bd992013-08-02 15:19:09 +02001111 DCHECK_EQ(GetMemberAccessFlags() & kAccStatic, 0U);
Ian Rogers08f753d2012-08-24 14:35:25 -07001112 if ((class_def.access_flags_ & kAccInterface) != 0) {
1113 return kInterface;
1114 } else if ((GetMemberAccessFlags() & kAccConstructor) != 0) {
1115 return kSuper;
1116 } else {
1117 return kVirtual;
1118 }
1119 }
1120 }
Ian Rogers0571d352011-11-03 19:51:38 -07001121 const DexFile::CodeItem* GetMethodCodeItem() const {
1122 return dex_file_.GetCodeItem(method_.code_off_);
1123 }
1124 uint32_t GetMethodCodeItemOffset() const {
1125 return method_.code_off_;
1126 }
jeffhao10037c82012-01-23 15:06:23 -08001127 const byte* EndDataPointer() const {
1128 CHECK(!HasNext());
1129 return ptr_pos_;
1130 }
Elliott Hughesa21039c2012-06-21 12:09:25 -07001131
Ian Rogers0571d352011-11-03 19:51:38 -07001132 private:
1133 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
1134 // header for a class_data_item
1135 struct ClassDataHeader {
1136 uint32_t static_fields_size_; // the number of static fields
1137 uint32_t instance_fields_size_; // the number of instance fields
1138 uint32_t direct_methods_size_; // the number of direct methods
1139 uint32_t virtual_methods_size_; // the number of virtual methods
1140 } header_;
1141
1142 // Read and decode header from a class_data_item stream into header
1143 void ReadClassDataHeader();
1144
1145 uint32_t EndOfStaticFieldsPos() const {
1146 return header_.static_fields_size_;
1147 }
1148 uint32_t EndOfInstanceFieldsPos() const {
1149 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
1150 }
1151 uint32_t EndOfDirectMethodsPos() const {
1152 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
1153 }
1154 uint32_t EndOfVirtualMethodsPos() const {
1155 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
1156 }
1157
1158 // A decoded version of the field of a class_data_item
1159 struct ClassDataField {
1160 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
1161 uint32_t access_flags_; // access flags for the field
1162 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
Elliott Hughesa21039c2012-06-21 12:09:25 -07001163
Ian Rogers0571d352011-11-03 19:51:38 -07001164 private:
1165 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001166 };
1167 ClassDataField field_;
Ian Rogers0571d352011-11-03 19:51:38 -07001168
1169 // Read and decode a field from a class_data_item stream into field
1170 void ReadClassDataField();
1171
1172 // A decoded version of the method of a class_data_item
1173 struct ClassDataMethod {
1174 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
1175 uint32_t access_flags_;
1176 uint32_t code_off_;
1177 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
Elliott Hughesa21039c2012-06-21 12:09:25 -07001178
Ian Rogers0571d352011-11-03 19:51:38 -07001179 private:
1180 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001181 };
1182 ClassDataMethod method_;
Ian Rogers0571d352011-11-03 19:51:38 -07001183
1184 // Read and decode a method from a class_data_item stream into method
1185 void ReadClassDataMethod();
1186
1187 const DexFile& dex_file_;
1188 size_t pos_; // integral number of items passed
1189 const byte* ptr_pos_; // pointer into stream of class_data_item
1190 uint32_t last_idx_; // last read field or method index to apply delta to
1191 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
1192};
1193
Ian Rogers0571d352011-11-03 19:51:38 -07001194class EncodedStaticFieldValueIterator {
1195 public:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001196 EncodedStaticFieldValueIterator(const DexFile& dex_file, Handle<mirror::DexCache>* dex_cache,
1197 Handle<mirror::ClassLoader>* class_loader,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 ClassLinker* linker, const DexFile::ClassDef& class_def)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001200
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001201 template<bool kTransactionActive>
Brian Carlstromea46f952013-07-30 01:26:50 -07001202 void ReadValueToField(mirror::ArtField* field) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001203
1204 bool HasNext() { return pos_ < array_size_; }
1205
1206 void Next();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001207
Ian Rogers0571d352011-11-03 19:51:38 -07001208 enum ValueType {
1209 kByte = 0x00,
1210 kShort = 0x02,
1211 kChar = 0x03,
1212 kInt = 0x04,
1213 kLong = 0x06,
1214 kFloat = 0x10,
1215 kDouble = 0x11,
1216 kString = 0x17,
1217 kType = 0x18,
1218 kField = 0x19,
1219 kMethod = 0x1a,
1220 kEnum = 0x1b,
1221 kArray = 0x1c,
1222 kAnnotation = 0x1d,
1223 kNull = 0x1e,
1224 kBoolean = 0x1f
1225 };
1226
Brian Carlstrom88f36542012-10-16 23:24:21 -07001227 private:
Ian Rogers0571d352011-11-03 19:51:38 -07001228 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
1229 static const byte kEncodedValueArgShift = 5;
1230
1231 const DexFile& dex_file_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001232 Handle<mirror::DexCache>* const dex_cache_; // Dex cache to resolve literal objects.
1233 Handle<mirror::ClassLoader>* const class_loader_; // ClassLoader to resolve types.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001234 ClassLinker* linker_; // Linker to resolve literal objects.
1235 size_t array_size_; // Size of array.
1236 size_t pos_; // Current position.
1237 const byte* ptr_; // Pointer into encoded data array.
1238 ValueType type_; // Type of current encoded value.
1239 jvalue jval_; // Value of current encoded value.
Ian Rogers0571d352011-11-03 19:51:38 -07001240 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1241};
Brian Carlstrom88f36542012-10-16 23:24:21 -07001242std::ostream& operator<<(std::ostream& os, const EncodedStaticFieldValueIterator::ValueType& code);
Ian Rogers0571d352011-11-03 19:51:38 -07001243
1244class CatchHandlerIterator {
1245 public:
1246 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
Logan Chien736df022012-04-27 16:25:57 +08001247
1248 CatchHandlerIterator(const DexFile::CodeItem& code_item,
1249 const DexFile::TryItem& try_item);
1250
Ian Rogers0571d352011-11-03 19:51:38 -07001251 explicit CatchHandlerIterator(const byte* handler_data) {
1252 Init(handler_data);
1253 }
1254
1255 uint16_t GetHandlerTypeIndex() const {
1256 return handler_.type_idx_;
1257 }
1258 uint32_t GetHandlerAddress() const {
1259 return handler_.address_;
1260 }
1261 void Next();
1262 bool HasNext() const {
1263 return remaining_count_ != -1 || catch_all_;
1264 }
1265 // End of this set of catch blocks, convenience method to locate next set of catch blocks
1266 const byte* EndDataPointer() const {
1267 CHECK(!HasNext());
1268 return current_data_;
1269 }
Elliott Hughesa21039c2012-06-21 12:09:25 -07001270
Ian Rogers0571d352011-11-03 19:51:38 -07001271 private:
Logan Chien736df022012-04-27 16:25:57 +08001272 void Init(const DexFile::CodeItem& code_item, int32_t offset);
Ian Rogers0571d352011-11-03 19:51:38 -07001273 void Init(const byte* handler_data);
1274
1275 struct CatchHandlerItem {
1276 uint16_t type_idx_; // type index of the caught exception type
1277 uint32_t address_; // handler address
1278 } handler_;
1279 const byte *current_data_; // the current handler in dex file.
1280 int32_t remaining_count_; // number of handlers not read.
1281 bool catch_all_; // is there a handler that will catch all exceptions in case
1282 // that all typed handler does not match.
1283};
1284
Carl Shapiro1fb86202011-06-27 17:43:13 -07001285} // namespace art
1286
Brian Carlstromfc0e3212013-07-17 14:40:12 -07001287#endif // ART_RUNTIME_DEX_FILE_H_