blob: aeb7e89efd1d3e7d862beeda07b0c59df66317a5 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_DEX_FILE_H_
18#define ART_SRC_DEX_FILE_H_
19
Elliott Hughes0c424cb2011-08-26 10:16:25 -070020#include <string>
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070021#include <vector>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070022
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "globals.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040024#include "jni.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070025#include "logging.h"
Brian Carlstrom33f741e2011-10-03 11:24:05 -070026#include "mem_map.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040027#include "mutex.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070028#include "safe_map.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070029#include "stringpiece.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070030#include "UniquePtr.h"
Shih-wei Liao2fb97532011-08-11 16:17:23 -070031#include "utils.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070032
33namespace art {
34
Brian Carlstroma6cc8932012-01-04 14:44:07 -080035class ZipArchive;
36
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070037// TODO: move all of the macro functionality into the DexCache class.
Brian Carlstromf615a612011-07-23 12:50:34 -070038class DexFile {
Carl Shapiro1fb86202011-06-27 17:43:13 -070039 public:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070040 static const byte kDexMagic[];
41 static const byte kDexMagicVersion[];
42 static const size_t kSha1DigestSize = 20;
jeffhao10037c82012-01-23 15:06:23 -080043 static const uint32_t kDexEndianConstant = 0x12345678;
Carl Shapiro80d4dde2011-06-28 16:24:07 -070044
Brian Carlstromb7bbba42011-10-13 14:58:47 -070045 // name of the DexFile entry within a zip archive
46 static const char* kClassesDex;
47
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070048 // The value of an invalid index.
49 static const uint32_t kDexNoIndex = 0xFFFFFFFF;
50
Ian Rogers0571d352011-11-03 19:51:38 -070051 // The value of an invalid index.
52 static const uint16_t kDexNoIndex16 = 0xFFFF;
Carl Shapiro1fb86202011-06-27 17:43:13 -070053
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070054 // Raw header_item.
55 struct Header {
56 uint8_t magic_[8];
Brian Carlstrom5b332c82012-02-01 15:02:31 -080057 uint32_t checksum_; // See also location_checksum_
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070058 uint8_t signature_[kSha1DigestSize];
jeffhaof6174e82012-01-31 16:14:17 -080059 uint32_t file_size_; // size of entire file
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070060 uint32_t header_size_; // offset to start of next section
61 uint32_t endian_tag_;
Ian Rogers0571d352011-11-03 19:51:38 -070062 uint32_t link_size_; // unused
63 uint32_t link_off_; // unused
64 uint32_t map_off_; // unused
65 uint32_t string_ids_size_; // number of StringIds
66 uint32_t string_ids_off_; // file offset of StringIds array
67 uint32_t type_ids_size_; // number of TypeIds, we don't support more than 65535
68 uint32_t type_ids_off_; // file offset of TypeIds array
69 uint32_t proto_ids_size_; // number of ProtoIds, we don't support more than 65535
70 uint32_t proto_ids_off_; // file offset of ProtoIds array
71 uint32_t field_ids_size_; // number of FieldIds
72 uint32_t field_ids_off_; // file offset of FieldIds array
73 uint32_t method_ids_size_; // number of MethodIds
74 uint32_t method_ids_off_; // file offset of MethodIds array
75 uint32_t class_defs_size_; // number of ClassDefs
76 uint32_t class_defs_off_; // file offset of ClassDef array
77 uint32_t data_size_; // unused
78 uint32_t data_off_; // unused
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070079 private:
80 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070081 };
Carl Shapiro1fb86202011-06-27 17:43:13 -070082
jeffhao10037c82012-01-23 15:06:23 -080083 // Map item type codes.
84 enum {
85 kDexTypeHeaderItem = 0x0000,
86 kDexTypeStringIdItem = 0x0001,
87 kDexTypeTypeIdItem = 0x0002,
88 kDexTypeProtoIdItem = 0x0003,
89 kDexTypeFieldIdItem = 0x0004,
90 kDexTypeMethodIdItem = 0x0005,
91 kDexTypeClassDefItem = 0x0006,
92 kDexTypeMapList = 0x1000,
93 kDexTypeTypeList = 0x1001,
94 kDexTypeAnnotationSetRefList = 0x1002,
95 kDexTypeAnnotationSetItem = 0x1003,
96 kDexTypeClassDataItem = 0x2000,
97 kDexTypeCodeItem = 0x2001,
98 kDexTypeStringDataItem = 0x2002,
99 kDexTypeDebugInfoItem = 0x2003,
100 kDexTypeAnnotationItem = 0x2004,
101 kDexTypeEncodedArrayItem = 0x2005,
102 kDexTypeAnnotationsDirectoryItem = 0x2006,
103 };
104
105 struct MapItem {
106 uint16_t type_;
107 uint16_t unused_;
108 uint32_t size_;
109 uint32_t offset_;
110 private:
111 DISALLOW_COPY_AND_ASSIGN(MapItem);
112 };
113
114 struct MapList {
115 uint32_t size_;
116 MapItem list_[1];
117 private:
118 DISALLOW_COPY_AND_ASSIGN(MapList);
119 };
120
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700121 // Raw string_id_item.
122 struct StringId {
123 uint32_t string_data_off_; // offset in bytes from the base address
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700124 private:
125 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700126 };
127
128 // Raw type_id_item.
129 struct TypeId {
130 uint32_t descriptor_idx_; // index into string_ids
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700131 private:
132 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700133 };
134
135 // Raw field_id_item.
136 struct FieldId {
Ian Rogers0571d352011-11-03 19:51:38 -0700137 uint16_t class_idx_; // index into type_ids_ array for defining class
138 uint16_t type_idx_; // index into type_ids_ array for field type
139 uint32_t name_idx_; // index into string_ids_ array for field name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700140 private:
141 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700142 };
143
144 // Raw method_id_item.
145 struct MethodId {
Ian Rogers0571d352011-11-03 19:51:38 -0700146 uint16_t class_idx_; // index into type_ids_ array for defining class
147 uint16_t proto_idx_; // index into proto_ids_ array for method prototype
148 uint32_t name_idx_; // index into string_ids_ array for method name
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700149 private:
150 DISALLOW_COPY_AND_ASSIGN(MethodId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700151 };
152
153 // Raw proto_id_item.
154 struct ProtoId {
Ian Rogers0571d352011-11-03 19:51:38 -0700155 uint32_t shorty_idx_; // index into string_ids array for shorty descriptor
156 uint16_t return_type_idx_; // index into type_ids array for return type
157 uint16_t pad_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700158 uint32_t parameters_off_; // file offset to type_list for parameter types
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700159 private:
160 DISALLOW_COPY_AND_ASSIGN(ProtoId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700161 };
162
163 // Raw class_def_item.
164 struct ClassDef {
Ian Rogers0571d352011-11-03 19:51:38 -0700165 uint16_t class_idx_; // index into type_ids_ array for this class
166 uint16_t pad1_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700167 uint32_t access_flags_;
Ian Rogers0571d352011-11-03 19:51:38 -0700168 uint16_t superclass_idx_; // index into type_ids_ array for superclass
169 uint16_t pad2_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700170 uint32_t interfaces_off_; // file offset to TypeList
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700171 uint32_t source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700172 uint32_t annotations_off_; // file offset to annotations_directory_item
173 uint32_t class_data_off_; // file offset to class_data_item
174 uint32_t static_values_off_; // file offset to EncodedArray
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700175 private:
176 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700177 };
178
179 // Raw type_item.
180 struct TypeItem {
181 uint16_t type_idx_; // index into type_ids section
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700182 private:
183 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700184 };
185
186 // Raw type_list.
187 class TypeList {
188 public:
189 uint32_t Size() const {
190 return size_;
191 }
192
193 const TypeItem& GetTypeItem(uint32_t idx) const {
194 CHECK_LT(idx, this->size_);
195 return this->list_[idx];
196 }
197
198 private:
199 uint32_t size_; // size of the list, in entries
200 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700201 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700202 };
203
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700204 // Raw code_item.
205 struct CodeItem {
206 uint16_t registers_size_;
207 uint16_t ins_size_;
208 uint16_t outs_size_;
209 uint16_t tries_size_;
210 uint32_t debug_info_off_; // file offset to debug info stream
Ian Rogersd81871c2011-10-03 13:57:23 -0700211 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700212 uint16_t insns_[1];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700213 private:
214 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700215 };
216
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700217 // Raw try_item.
218 struct TryItem {
219 uint32_t start_addr_;
220 uint16_t insn_count_;
221 uint16_t handler_off_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700222 private:
223 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700224 };
225
jeffhao10037c82012-01-23 15:06:23 -0800226 // Annotation constants.
227 enum {
228 kDexVisibilityBuild = 0x00, /* annotation visibility */
229 kDexVisibilityRuntime = 0x01,
230 kDexVisibilitySystem = 0x02,
231
232 kDexAnnotationByte = 0x00,
233 kDexAnnotationShort = 0x02,
234 kDexAnnotationChar = 0x03,
235 kDexAnnotationInt = 0x04,
236 kDexAnnotationLong = 0x06,
237 kDexAnnotationFloat = 0x10,
238 kDexAnnotationDouble = 0x11,
239 kDexAnnotationString = 0x17,
240 kDexAnnotationType = 0x18,
241 kDexAnnotationField = 0x19,
242 kDexAnnotationMethod = 0x1a,
243 kDexAnnotationEnum = 0x1b,
244 kDexAnnotationArray = 0x1c,
245 kDexAnnotationAnnotation = 0x1d,
246 kDexAnnotationNull = 0x1e,
247 kDexAnnotationBoolean = 0x1f,
248
249 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
250 kDexAnnotationValueArgShift = 5,
251 };
252
253 struct AnnotationsDirectoryItem {
254 uint32_t class_annotations_off_;
255 uint32_t fields_size_;
256 uint32_t methods_size_;
257 uint32_t parameters_size_;
258 private:
259 DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
260 };
261
262 struct FieldAnnotationsItem {
263 uint32_t field_idx_;
264 uint32_t annotations_off_;
265 private:
266 DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem);
267 };
268
269 struct MethodAnnotationsItem {
270 uint32_t method_idx_;
271 uint32_t annotations_off_;
272 private:
273 DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem);
274 };
275
276 struct ParameterAnnotationsItem {
277 uint32_t method_idx_;
278 uint32_t annotations_off_;
279 private:
280 DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem);
281 };
282
283 struct AnnotationSetRefItem {
284 uint32_t annotations_off_;
285 private:
286 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem);
287 };
288
289 struct AnnotationSetRefList {
290 uint32_t size_;
291 AnnotationSetRefItem list_[1];
292 private:
293 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList);
294 };
295
296 struct AnnotationSetItem {
297 uint32_t size_;
298 uint32_t entries_[1];
299 private:
300 DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
301 };
302
303 struct AnnotationItem {
304 uint8_t visibility_;
305 uint8_t annotation_[1];
306 private:
307 DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
308 };
309
Elliott Hughesaf778e62012-05-01 18:45:31 -0700310 struct PACKED Payload {
311 uint16_t ident; // kPackedSwitchSignature, kSparseSwitchSignature, or kArrayDataSignature.
312 uint16_t element_width;
313 uint32_t element_count;
314 uint8_t data[];
315 private:
316 DISALLOW_COPY_AND_ASSIGN(Payload);
317 };
318
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700319 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
320 typedef std::vector<const DexFile*> ClassPath;
321
322 // Search a collection of DexFiles for a descriptor
323 static ClassPathEntry FindInClassPath(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700324 const ClassPath& class_path);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700325
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800326 // Returns the checksum of a file for comparison with GetLocationChecksum().
327 // For .dex files, this is the header checksum.
328 // For zip files, this is the classes.dex zip entry CRC32 checksum.
329 // Return true if the checksum could be found, false otherwise.
330 static bool GetChecksum(const std::string& filename, uint32_t& checksum);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700331
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700332 // Opens .dex file, guessing the container format based on file extension
Brian Carlstrom16192862011-09-12 17:50:06 -0700333 static const DexFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800334 const std::string& location);
jeffhao262bf462011-10-20 18:36:32 -0700335
Brian Carlstrom89521892011-12-07 22:05:07 -0800336 // Opens .dex file, backed by existing memory
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800337 static const DexFile* Open(const uint8_t* base, size_t size,
338 const std::string& location, uint32_t location_checksum) {
339 return OpenMemory(base, size, location, location_checksum, NULL);
Brian Carlstrom89521892011-12-07 22:05:07 -0800340 }
341
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800342 // Opens .dex file from the classes.dex in a zip archive
343 static const DexFile* Open(const ZipArchive& zip_archive, const std::string& location);
344
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700345 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700346 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700347
Brian Carlstroma663ea52011-08-19 23:33:41 -0700348 const std::string& GetLocation() const {
349 return location_;
350 }
351
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800352 // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header.
353 // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex.
354 uint32_t GetLocationChecksum() const {
355 return location_checksum_;
356 }
357
Jesse Wilson6bf19152011-09-29 13:12:33 -0400358 // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file.
359 // Used by managed code to implement annotations.
360 jobject GetDexObject(JNIEnv* env) const;
361
Brian Carlstroma663ea52011-08-19 23:33:41 -0700362 const Header& GetHeader() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800363 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700364 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700365 }
366
Ian Rogers0571d352011-11-03 19:51:38 -0700367 // Decode the dex magic version
Ian Rogersd81871c2011-10-03 13:57:23 -0700368 uint32_t GetVersion() const;
369
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800370 // Returns true if the byte string points to the magic value.
371 static bool IsMagicValid(const byte* magic);
372
373 // Returns true if the byte string after the magic is the correct value.
374 static bool IsVersionValid(const byte* magic);
375
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700376 // Returns the number of string identifiers in the .dex file.
377 size_t NumStringIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800378 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700379 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380 }
381
Ian Rogers0571d352011-11-03 19:51:38 -0700382 // Returns the StringId at the specified index.
383 const StringId& GetStringId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800384 CHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700385 return string_ids_[idx];
386 }
387
388 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800389 CHECK_GE(&string_id, string_ids_) << GetLocation();
390 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700391 return &string_id - string_ids_;
392 }
393
394 int32_t GetStringLength(const StringId& string_id) const;
395
396 // Returns a pointer to the UTF-8 string data referred to by the given string_id.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800397 const char* GetStringDataAndLength(const StringId& string_id, uint32_t* length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700398
399 const char* GetStringData(const StringId& string_id) const {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800400 uint32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700401 return GetStringDataAndLength(string_id, &length);
402 }
403
404 // return the UTF-8 encoded string with the specified string_id index
Elliott Hughes45651fd2012-02-21 15:48:20 -0800405 const char* StringDataAndLengthByIdx(uint32_t idx, uint32_t* unicode_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700406 if (idx == kDexNoIndex) {
407 *unicode_length = 0;
408 return NULL;
409 }
410 const StringId& string_id = GetStringId(idx);
411 return GetStringDataAndLength(string_id, unicode_length);
412 }
413
414 const char* StringDataByIdx(uint32_t idx) const {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800415 uint32_t unicode_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700416 return StringDataAndLengthByIdx(idx, &unicode_length);
417 }
418
419 // Looks up a string id for a given string
420 const StringId* FindStringId(const std::string& string) const;
421
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700422 // Returns the number of type identifiers in the .dex file.
423 size_t NumTypeIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800424 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700425 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700426 }
427
Ian Rogers0571d352011-11-03 19:51:38 -0700428 // Returns the TypeId at the specified index.
429 const TypeId& GetTypeId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800430 CHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700431 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700432 }
433
Ian Rogers0571d352011-11-03 19:51:38 -0700434 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800435 CHECK_GE(&type_id, type_ids_) << GetLocation();
436 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700437 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800438 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700439 return static_cast<uint16_t>(result);
440 }
441
442 // Get the descriptor string associated with a given type index.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800443 const char* StringByTypeIdx(uint32_t idx, uint32_t* unicode_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700444 const TypeId& type_id = GetTypeId(idx);
445 return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length);
446 }
447
448 const char* StringByTypeIdx(uint32_t idx) const {
449 const TypeId& type_id = GetTypeId(idx);
450 return StringDataByIdx(type_id.descriptor_idx_);
451 }
452
453 // Returns the type descriptor string of a type id.
454 const char* GetTypeDescriptor(const TypeId& type_id) const {
455 return StringDataByIdx(type_id.descriptor_idx_);
456 }
457
458 // Looks up a type for the given string index
459 const TypeId* FindTypeId(uint32_t string_idx) const;
460
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700461 // Returns the number of field identifiers in the .dex file.
462 size_t NumFieldIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800463 CHECK(header_ != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700464 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700465 }
466
Ian Rogers0571d352011-11-03 19:51:38 -0700467 // Returns the FieldId at the specified index.
468 const FieldId& GetFieldId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800469 CHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700470 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700471 }
472
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800473 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800474 CHECK_GE(&field_id, field_ids_) << GetLocation();
475 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800476 return &field_id - field_ids_;
477 }
478
479 // Looks up a field by its declaring class, name and type
480 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
481 const DexFile::StringId& name,
482 const DexFile::TypeId& type) const;
483
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700484 // Returns the declaring class descriptor string of a field id.
485 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700486 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
487 return GetTypeDescriptor(type_id);
488 }
489
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700490 // Returns the class descriptor string of a field id.
491 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
492 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
493 return GetTypeDescriptor(type_id);
494 }
495
Brian Carlstromb9edb842011-08-28 16:31:06 -0700496 // Returns the name of a field id.
497 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700498 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700499 }
500
Ian Rogers0571d352011-11-03 19:51:38 -0700501 // Returns the number of method identifiers in the .dex file.
502 size_t NumMethodIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800503 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700504 return header_->method_ids_size_;
505 }
506
507 // Returns the MethodId at the specified index.
508 const MethodId& GetMethodId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800509 CHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700510 return method_ids_[idx];
511 }
512
513 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800514 CHECK_GE(&method_id, method_ids_) << GetLocation();
515 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700516 return &method_id - method_ids_;
517 }
518
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800519 // Looks up a method by its declaring class, name and proto_id
520 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
521 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700522 const DexFile::ProtoId& signature) const;
523
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700524 // Returns the declaring class descriptor string of a method id.
525 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700526 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
527 return GetTypeDescriptor(type_id);
528 }
529
jeffhao98eacac2011-09-14 16:11:53 -0700530 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700531 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
532 return GetProtoId(method_id.proto_idx_);
533 }
534
535 // Returns the signature of a method id.
536 const std::string GetMethodSignature(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700537 return CreateMethodSignature(method_id.proto_idx_, NULL);
jeffhao98eacac2011-09-14 16:11:53 -0700538 }
539
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700540 // Returns the name of a method id.
541 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700542 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700543 }
544
Ian Rogers0571d352011-11-03 19:51:38 -0700545 // Returns the shorty of a method id.
546 const char* GetMethodShorty(const MethodId& method_id) const {
547 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700548 }
Elliott Hughes45651fd2012-02-21 15:48:20 -0800549 const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800550 return StringDataAndLengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
551 }
Ian Rogers0571d352011-11-03 19:51:38 -0700552 // Returns the number of class definitions in the .dex file.
553 size_t NumClassDefs() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800554 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700555 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700556 }
557
558 // Returns the ClassDef at the specified index.
559 const ClassDef& GetClassDef(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800560 CHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700561 return class_defs_[idx];
562 }
563
Ian Rogers0571d352011-11-03 19:51:38 -0700564 uint32_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800565 CHECK_GE(&class_def, class_defs_) << GetLocation();
566 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700567 return &class_def - class_defs_;
568 }
569
570 // Returns the class descriptor string of a class definition.
571 const char* GetClassDescriptor(const ClassDef& class_def) const {
572 return StringByTypeIdx(class_def.class_idx_);
573 }
574
575 // Looks up a class definition by its class descriptor.
576 const ClassDef* FindClassDef(const StringPiece& descriptor) const;
577
578 // Looks up a class definition index by its class descriptor.
579 bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const;
580
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700581 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
582 if (class_def.interfaces_off_ == 0) {
583 return NULL;
584 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800585 const byte* addr = begin_ + class_def.interfaces_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700586 return reinterpret_cast<const TypeList*>(addr);
587 }
588 }
589
Ian Rogers0571d352011-11-03 19:51:38 -0700590 // Returns a pointer to the raw memory mapped class_data_item
591 const byte* GetClassData(const ClassDef& class_def) const {
592 if (class_def.class_data_off_ == 0) {
593 return NULL;
594 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800595 return begin_ + class_def.class_data_off_;
Ian Rogers0571d352011-11-03 19:51:38 -0700596 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700597 }
598
Ian Rogers0571d352011-11-03 19:51:38 -0700599 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800600 const CodeItem* GetCodeItem(const uint32_t code_off) const {
601 if (code_off == 0) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700602 return NULL; // native or abstract method
603 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800604 const byte* addr = begin_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700605 return reinterpret_cast<const CodeItem*>(addr);
606 }
607 }
608
Ian Rogers0571d352011-11-03 19:51:38 -0700609 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
610 return StringByTypeIdx(proto_id.return_type_idx_);
611 }
612
613 // Returns the number of prototype identifiers in the .dex file.
614 size_t NumProtoIds() const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800615 CHECK(header_ != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700616 return header_->proto_ids_size_;
617 }
618
619 // Returns the ProtoId at the specified index.
620 const ProtoId& GetProtoId(uint32_t idx) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800621 CHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700622 return proto_ids_[idx];
623 }
624
625 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800626 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
627 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700628 return &proto_id - proto_ids_;
629 }
630
631 // Looks up a proto id for a given return type and signature type list
632 const ProtoId* FindProtoId(uint16_t return_type_id,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800633 const std::vector<uint16_t>& signature_type_idxs_) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700634
635 // Given a signature place the type ids into the given vector, returns true on success
636 bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
637 const std::string& signature) const;
638
639 // Given a proto_idx decode the type list and return type into a method signature
640 std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const;
641
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700642 // Returns the short form method descriptor for the given prototype.
643 const char* GetShorty(uint32_t proto_idx) const {
644 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700645 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700646 }
647
648 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
649 if (proto_id.parameters_off_ == 0) {
650 return NULL;
651 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800652 const byte* addr = begin_ + proto_id.parameters_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700653 return reinterpret_cast<const TypeList*>(addr);
654 }
655 }
656
Ian Rogers0571d352011-11-03 19:51:38 -0700657 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700658 if (class_def.static_values_off_ == 0) {
659 return 0;
660 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800661 return begin_ + class_def.static_values_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700662 }
663 }
664
Ian Rogers0571d352011-11-03 19:51:38 -0700665 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700667 return reinterpret_cast<const TryItem*>
668 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
669 }
670
671 // Get the base of the encoded data for the given DexCode.
Ian Rogers0571d352011-11-03 19:51:38 -0700672 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
673 const byte* handler_data =
674 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700675 return handler_data + offset;
676 }
677
678 // Find the handler associated with a given address, if any.
679 // Initializes the given iterator and returns true if a match is
680 // found. Returns end if there is no applicable handler.
Ian Rogers0571d352011-11-03 19:51:38 -0700681 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
682 uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700683
Shih-wei Liao195487c2011-08-20 13:29:04 -0700684 // Get the pointer to the start of the debugging data
Ian Rogers0571d352011-11-03 19:51:38 -0700685 const byte* GetDebugInfoStream(const CodeItem* code_item) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700686 if (code_item->debug_info_off_ == 0) {
687 return NULL;
688 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800689 return begin_ + code_item->debug_info_off_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700690 }
691 }
692
693 // Callback for "new position table entry".
694 // Returning true causes the decoder to stop early.
Elliott Hughes2435a572012-02-17 16:07:41 -0800695 typedef bool (*DexDebugNewPositionCb)(void* context, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700696
697 // Callback for "new locals table entry". "signature" is an empty string
698 // if no signature is available for an entry.
Elliott Hughes2435a572012-02-17 16:07:41 -0800699 typedef void (*DexDebugNewLocalCb)(void* context, uint16_t reg,
Shih-wei Liao195487c2011-08-20 13:29:04 -0700700 uint32_t startAddress,
701 uint32_t endAddress,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700702 const char* name,
703 const char* descriptor,
704 const char* signature);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700705
Elliott Hughes2435a572012-02-17 16:07:41 -0800706 static bool LineNumForPcCb(void* context, uint32_t address, uint32_t line_num);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700707
708 // Debug info opcodes and constants
709 enum {
710 DBG_END_SEQUENCE = 0x00,
711 DBG_ADVANCE_PC = 0x01,
712 DBG_ADVANCE_LINE = 0x02,
713 DBG_START_LOCAL = 0x03,
714 DBG_START_LOCAL_EXTENDED = 0x04,
715 DBG_END_LOCAL = 0x05,
716 DBG_RESTART_LOCAL = 0x06,
717 DBG_SET_PROLOGUE_END = 0x07,
718 DBG_SET_EPILOGUE_BEGIN = 0x08,
719 DBG_SET_FILE = 0x09,
720 DBG_FIRST_SPECIAL = 0x0a,
721 DBG_LINE_BASE = -4,
722 DBG_LINE_RANGE = 15,
723 };
724
725 struct LocalInfo {
Ian Rogers0571d352011-11-03 19:51:38 -0700726 LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0),
727 is_live_(false) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -0700728
Ian Rogers0571d352011-11-03 19:51:38 -0700729 const char* name_; // E.g., list
730 const char* descriptor_; // E.g., Ljava/util/LinkedList;
731 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
732 uint16_t start_address_; // PC location where the local is first defined.
733 bool is_live_; // Is the local defined and live.
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700734
735 private:
736 DISALLOW_COPY_AND_ASSIGN(LocalInfo);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700737 };
738
739 struct LineNumFromPcContext {
740 LineNumFromPcContext(uint32_t address, uint32_t line_num) :
741 address_(address), line_num_(line_num) {}
742 uint32_t address_;
743 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700744 private:
745 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700746 };
747
Elliott Hughes2435a572012-02-17 16:07:41 -0800748 void InvokeLocalCbIfLive(void* context, int reg, uint32_t end_address,
Brian Carlstrom78128a62011-09-15 17:21:19 -0700749 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700750 if (local_cb != NULL && local_in_reg[reg].is_live_) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800751 local_cb(context, reg, local_in_reg[reg].start_address_, end_address,
Elliott Hughesdbb40792011-11-18 17:05:22 -0800752 local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
753 local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : "");
Shih-wei Liao195487c2011-08-20 13:29:04 -0700754 }
755 }
756
757 // Determine the source file line number based on the program counter.
758 // "pc" is an offset, in 16-bit units, from the start of the method's code.
759 //
760 // Returns -1 if no match was found (possibly because the source files were
761 // compiled without "-g", so no line number information is present).
762 // Returns -2 for native methods (as expected in exception traces).
763 //
764 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Ian Rogers0571d352011-11-03 19:51:38 -0700765 int32_t GetLineNumFromPC(const Method* method, uint32_t rel_pc) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700766
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800767 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800768 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
769 void* context) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700770
Ian Rogers0571d352011-11-03 19:51:38 -0700771 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700772 if (class_def.source_file_idx_ == 0xffffffff) {
773 return NULL;
774 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700775 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700776 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700777 }
778
jeffhaob4df5142011-09-19 20:25:32 -0700779 void ChangePermissions(int prot) const;
780
Carl Shapiro1fb86202011-06-27 17:43:13 -0700781 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700782
783 // Opens a .dex file
784 static const DexFile* OpenFile(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800785 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800786 bool verify);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700787
788 // Opens a dex file from within a .jar, .zip, or .apk file
789 static const DexFile* OpenZip(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800790 const std::string& location);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700791
Brian Carlstrom89521892011-12-07 22:05:07 -0800792 // Opens a .dex file at the given address backed by a MemMap
793 static const DexFile* OpenMemory(const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800794 uint32_t location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800795 MemMap* mem_map) {
Ian Rogers30fab402012-01-23 15:43:46 -0800796 return OpenMemory(mem_map->Begin(),
797 mem_map->Size(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800798 location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800799 location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800800 mem_map);
801 }
802
803 // Opens a .dex file at the given address, optionally backed by a MemMap
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700804 static const DexFile* OpenMemory(const byte* dex_file,
jeffhaof6174e82012-01-31 16:14:17 -0800805 size_t size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700806 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800807 uint32_t location_checksum,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700808 MemMap* mem_map);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700809
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800810 DexFile(const byte* base, size_t size,
811 const std::string& location, uint32_t location_checksum,
812 MemMap* mem_map)
Ian Rogers30fab402012-01-23 15:43:46 -0800813 : begin_(base),
jeffhaof6174e82012-01-31 16:14:17 -0800814 size_(size),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700815 location_(location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800816 location_checksum_(location_checksum),
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700817 mem_map_(mem_map),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400818 dex_object_lock_("a dex_object_lock_"),
819 dex_object_(NULL),
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700820 header_(0),
821 string_ids_(0),
822 type_ids_(0),
823 field_ids_(0),
824 method_ids_(0),
825 proto_ids_(0),
Brian Carlstroma663ea52011-08-19 23:33:41 -0700826 class_defs_(0) {
Ian Rogers30fab402012-01-23 15:43:46 -0800827 CHECK(begin_ != NULL) << GetLocation();
jeffhaof6174e82012-01-31 16:14:17 -0800828 CHECK_GT(size_, 0U) << GetLocation();
829 }
830
831 const byte* Begin() const {
832 return begin_;
833 }
834
835 size_t Size() const {
836 return size_;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700837 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700838
839 // Top-level initializer that calls other Init methods.
840 bool Init();
841
842 // Caches pointers into to the various file sections.
843 void InitMembers();
844
845 // Builds the index of descriptors to class definitions.
846 void InitIndex();
847
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800848 // Returns true if the header magic and version numbers are of the expected values.
jeffhao10037c82012-01-23 15:06:23 -0800849 bool CheckMagicAndVersion() const;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700850
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800851 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800852 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
853 void* context, const byte* stream, LocalInfo* local_in_reg) const;
Elliott Hughes03181a82011-11-17 17:22:21 -0800854
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800855 // The index of descriptors to class definition indexes (as opposed to type id indexes)
Elliott Hughesa0e18062012-04-13 15:59:59 -0700856 typedef SafeMap<const StringPiece, uint32_t> Index;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700857 Index index_;
858
859 // The base address of the memory mapping.
Ian Rogers30fab402012-01-23 15:43:46 -0800860 const byte* begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700861
862 // The size of the underlying memory allocation in bytes.
jeffhaof6174e82012-01-31 16:14:17 -0800863 size_t size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700864
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700865 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700866 //
867 // The ClassLinker will use this to match DexFiles the boot class
868 // path to DexCache::GetLocation when loading from an image.
869 const std::string location_;
870
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800871 const uint32_t location_checksum_;
872
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700873 // Manages the underlying memory allocation.
874 UniquePtr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700875
Jesse Wilson6bf19152011-09-29 13:12:33 -0400876 // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
877 mutable Mutex dex_object_lock_;
878 mutable jobject dex_object_;
879
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700880 // Points to the header section.
881 const Header* header_;
882
883 // Points to the base of the string identifier list.
884 const StringId* string_ids_;
885
886 // Points to the base of the type identifier list.
887 const TypeId* type_ids_;
888
889 // Points to the base of the field identifier list.
890 const FieldId* field_ids_;
891
892 // Points to the base of the method identifier list.
893 const MethodId* method_ids_;
894
895 // Points to the base of the prototype identifier list.
896 const ProtoId* proto_ids_;
897
898 // Points to the base of the class definition list.
899 const ClassDef* class_defs_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700900};
901
Ian Rogers0571d352011-11-03 19:51:38 -0700902// Iterate over a dex file's ProtoId's paramters
903class DexFileParameterIterator {
904 public:
905 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
906 : dex_file_(dex_file), size_(0), pos_(0) {
907 type_list_ = dex_file_.GetProtoParameters(proto_id);
908 if (type_list_ != NULL) {
909 size_ = type_list_->Size();
910 }
911 }
912 bool HasNext() const { return pos_ < size_; }
913 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800914 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -0700915 return type_list_->GetTypeItem(pos_).type_idx_;
916 }
917 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800918 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -0700919 }
920 private:
921 const DexFile& dex_file_;
922 const DexFile::TypeList* type_list_;
923 uint32_t size_;
924 uint32_t pos_;
925 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
926};
927
928// Iterate and decode class_data_item
929class ClassDataItemIterator {
930 public:
931 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
932 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
933 ReadClassDataHeader();
934 if (EndOfInstanceFieldsPos() > 0) {
935 ReadClassDataField();
936 } else if (EndOfVirtualMethodsPos() > 0) {
937 ReadClassDataMethod();
938 }
939 }
940 uint32_t NumStaticFields() const {
941 return header_.static_fields_size_;
942 }
943 uint32_t NumInstanceFields() const {
944 return header_.instance_fields_size_;
945 }
946 uint32_t NumDirectMethods() const {
947 return header_.direct_methods_size_;
948 }
949 uint32_t NumVirtualMethods() const {
950 return header_.virtual_methods_size_;
951 }
952 bool HasNextStaticField() const {
953 return pos_ < EndOfStaticFieldsPos();
954 }
955 bool HasNextInstanceField() const {
956 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
957 }
958 bool HasNextDirectMethod() const {
959 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
960 }
961 bool HasNextVirtualMethod() const {
962 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
963 }
964 bool HasNext() const {
965 return pos_ < EndOfVirtualMethodsPos();
966 }
967 void Next() {
968 pos_++;
969 if (pos_ < EndOfStaticFieldsPos()) {
970 last_idx_ = GetMemberIndex();
971 ReadClassDataField();
972 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
973 last_idx_ = 0; // transition to next array, reset last index
974 ReadClassDataField();
975 } else if (pos_ < EndOfInstanceFieldsPos()) {
976 last_idx_ = GetMemberIndex();
977 ReadClassDataField();
978 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
979 last_idx_ = 0; // transition to next array, reset last index
980 ReadClassDataMethod();
981 } else if (pos_ < EndOfDirectMethodsPos()) {
982 last_idx_ = GetMemberIndex();
983 ReadClassDataMethod();
984 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
985 last_idx_ = 0; // transition to next array, reset last index
986 ReadClassDataMethod();
987 } else if (pos_ < EndOfVirtualMethodsPos()) {
988 last_idx_ = GetMemberIndex();
989 ReadClassDataMethod();
990 } else {
991 DCHECK(!HasNext());
992 }
993 }
994 uint32_t GetMemberIndex() const {
995 if (pos_ < EndOfInstanceFieldsPos()) {
996 return last_idx_ + field_.field_idx_delta_;
997 } else {
998 CHECK_LT(pos_, EndOfVirtualMethodsPos());
999 return last_idx_ + method_.method_idx_delta_;
1000 }
1001 }
1002 uint32_t GetMemberAccessFlags() const {
1003 if (pos_ < EndOfInstanceFieldsPos()) {
1004 return field_.access_flags_;
1005 } else {
1006 CHECK_LT(pos_, EndOfVirtualMethodsPos());
1007 return method_.access_flags_;
1008 }
1009 }
1010 const DexFile::CodeItem* GetMethodCodeItem() const {
1011 return dex_file_.GetCodeItem(method_.code_off_);
1012 }
1013 uint32_t GetMethodCodeItemOffset() const {
1014 return method_.code_off_;
1015 }
jeffhao10037c82012-01-23 15:06:23 -08001016 const byte* EndDataPointer() const {
1017 CHECK(!HasNext());
1018 return ptr_pos_;
1019 }
Ian Rogers0571d352011-11-03 19:51:38 -07001020 private:
1021 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
1022 // header for a class_data_item
1023 struct ClassDataHeader {
1024 uint32_t static_fields_size_; // the number of static fields
1025 uint32_t instance_fields_size_; // the number of instance fields
1026 uint32_t direct_methods_size_; // the number of direct methods
1027 uint32_t virtual_methods_size_; // the number of virtual methods
1028 } header_;
1029
1030 // Read and decode header from a class_data_item stream into header
1031 void ReadClassDataHeader();
1032
1033 uint32_t EndOfStaticFieldsPos() const {
1034 return header_.static_fields_size_;
1035 }
1036 uint32_t EndOfInstanceFieldsPos() const {
1037 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
1038 }
1039 uint32_t EndOfDirectMethodsPos() const {
1040 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
1041 }
1042 uint32_t EndOfVirtualMethodsPos() const {
1043 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
1044 }
1045
1046 // A decoded version of the field of a class_data_item
1047 struct ClassDataField {
1048 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
1049 uint32_t access_flags_; // access flags for the field
1050 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
1051 private:
1052 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001053 };
1054 ClassDataField field_;
Ian Rogers0571d352011-11-03 19:51:38 -07001055
1056 // Read and decode a field from a class_data_item stream into field
1057 void ReadClassDataField();
1058
1059 // A decoded version of the method of a class_data_item
1060 struct ClassDataMethod {
1061 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
1062 uint32_t access_flags_;
1063 uint32_t code_off_;
1064 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
1065 private:
1066 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001067 };
1068 ClassDataMethod method_;
Ian Rogers0571d352011-11-03 19:51:38 -07001069
1070 // Read and decode a method from a class_data_item stream into method
1071 void ReadClassDataMethod();
1072
1073 const DexFile& dex_file_;
1074 size_t pos_; // integral number of items passed
1075 const byte* ptr_pos_; // pointer into stream of class_data_item
1076 uint32_t last_idx_; // last read field or method index to apply delta to
1077 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
1078};
1079
1080class ClassLinker;
1081class DexCache;
1082class Field;
1083
1084class EncodedStaticFieldValueIterator {
1085 public:
1086 EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache,
1087 ClassLinker* linker, const DexFile::ClassDef& class_def);
1088
1089 void ReadValueToField(Field* field) const;
1090
1091 bool HasNext() { return pos_ < array_size_; }
1092
1093 void Next();
1094 private:
1095 enum ValueType {
1096 kByte = 0x00,
1097 kShort = 0x02,
1098 kChar = 0x03,
1099 kInt = 0x04,
1100 kLong = 0x06,
1101 kFloat = 0x10,
1102 kDouble = 0x11,
1103 kString = 0x17,
1104 kType = 0x18,
1105 kField = 0x19,
1106 kMethod = 0x1a,
1107 kEnum = 0x1b,
1108 kArray = 0x1c,
1109 kAnnotation = 0x1d,
1110 kNull = 0x1e,
1111 kBoolean = 0x1f
1112 };
1113
1114 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111
1115 static const byte kEncodedValueArgShift = 5;
1116
1117 const DexFile& dex_file_;
1118 DexCache* dex_cache_; // dex cache to resolve literal objects
1119 ClassLinker* linker_; // linker to resolve literal objects
1120 size_t array_size_; // size of array
1121 size_t pos_; // current position
1122 const byte* ptr_; // pointer into encoded data array
1123 byte type_; // type of current encoded value
1124 jvalue jval_; // value of current encoded value
1125 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1126};
1127
1128class CatchHandlerIterator {
1129 public:
1130 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
Logan Chien736df022012-04-27 16:25:57 +08001131
1132 CatchHandlerIterator(const DexFile::CodeItem& code_item,
1133 const DexFile::TryItem& try_item);
1134
Ian Rogers0571d352011-11-03 19:51:38 -07001135 explicit CatchHandlerIterator(const byte* handler_data) {
1136 Init(handler_data);
1137 }
1138
1139 uint16_t GetHandlerTypeIndex() const {
1140 return handler_.type_idx_;
1141 }
1142 uint32_t GetHandlerAddress() const {
1143 return handler_.address_;
1144 }
1145 void Next();
1146 bool HasNext() const {
1147 return remaining_count_ != -1 || catch_all_;
1148 }
1149 // End of this set of catch blocks, convenience method to locate next set of catch blocks
1150 const byte* EndDataPointer() const {
1151 CHECK(!HasNext());
1152 return current_data_;
1153 }
1154 private:
Logan Chien736df022012-04-27 16:25:57 +08001155 void Init(const DexFile::CodeItem& code_item, int32_t offset);
Ian Rogers0571d352011-11-03 19:51:38 -07001156 void Init(const byte* handler_data);
1157
1158 struct CatchHandlerItem {
1159 uint16_t type_idx_; // type index of the caught exception type
1160 uint32_t address_; // handler address
1161 } handler_;
1162 const byte *current_data_; // the current handler in dex file.
1163 int32_t remaining_count_; // number of handlers not read.
1164 bool catch_all_; // is there a handler that will catch all exceptions in case
1165 // that all typed handler does not match.
1166};
1167
Carl Shapiro1fb86202011-06-27 17:43:13 -07001168} // namespace art
1169
1170#endif // ART_SRC_DEX_FILE_H_