Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_DEX_FILE_H_ |
| 18 | #define ART_SRC_DEX_FILE_H_ |
| 19 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 20 | #include <map> |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 21 | #include <string> |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 22 | #include <vector> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 24 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | #include "globals.h" |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 26 | #include "jni.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 27 | #include "logging.h" |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 28 | #include "mem_map.h" |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 29 | #include "mutex.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 30 | #include "stringpiece.h" |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 31 | #include "utils.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 35 | class ZipArchive; |
| 36 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 37 | // TODO: move all of the macro functionality into the DexCache class. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 38 | class DexFile { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 39 | public: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 40 | static const byte kDexMagic[]; |
| 41 | static const byte kDexMagicVersion[]; |
| 42 | static const size_t kSha1DigestSize = 20; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 43 | static const uint32_t kDexEndianConstant = 0x12345678; |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 44 | |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 45 | // name of the DexFile entry within a zip archive |
| 46 | static const char* kClassesDex; |
| 47 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 48 | // The value of an invalid index. |
| 49 | static const uint32_t kDexNoIndex = 0xFFFFFFFF; |
| 50 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 51 | // The value of an invalid index. |
| 52 | static const uint16_t kDexNoIndex16 = 0xFFFF; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 53 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 54 | // Raw header_item. |
| 55 | struct Header { |
| 56 | uint8_t magic_[8]; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 57 | uint32_t checksum_; // See also location_checksum_ |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 58 | uint8_t signature_[kSha1DigestSize]; |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 59 | uint32_t file_size_; // size of entire file |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 60 | uint32_t header_size_; // offset to start of next section |
| 61 | uint32_t endian_tag_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 62 | 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 Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 79 | private: |
| 80 | DISALLOW_COPY_AND_ASSIGN(Header); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 81 | }; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 82 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 83 | // 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 Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 121 | // Raw string_id_item. |
| 122 | struct StringId { |
| 123 | uint32_t string_data_off_; // offset in bytes from the base address |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 124 | private: |
| 125 | DISALLOW_COPY_AND_ASSIGN(StringId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | // Raw type_id_item. |
| 129 | struct TypeId { |
| 130 | uint32_t descriptor_idx_; // index into string_ids |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 131 | private: |
| 132 | DISALLOW_COPY_AND_ASSIGN(TypeId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | // Raw field_id_item. |
| 136 | struct FieldId { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 137 | 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 Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 140 | private: |
| 141 | DISALLOW_COPY_AND_ASSIGN(FieldId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | // Raw method_id_item. |
| 145 | struct MethodId { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 146 | 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 Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 149 | private: |
| 150 | DISALLOW_COPY_AND_ASSIGN(MethodId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | // Raw proto_id_item. |
| 154 | struct ProtoId { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 155 | 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 Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 158 | uint32_t parameters_off_; // file offset to type_list for parameter types |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 159 | private: |
| 160 | DISALLOW_COPY_AND_ASSIGN(ProtoId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | // Raw class_def_item. |
| 164 | struct ClassDef { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 165 | uint16_t class_idx_; // index into type_ids_ array for this class |
| 166 | uint16_t pad1_; // padding = 0 |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 167 | uint32_t access_flags_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 168 | uint16_t superclass_idx_; // index into type_ids_ array for superclass |
| 169 | uint16_t pad2_; // padding = 0 |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 170 | uint32_t interfaces_off_; // file offset to TypeList |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 171 | uint32_t source_file_idx_; // index into string_ids_ for source file name |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 172 | 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 Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 175 | private: |
| 176 | DISALLOW_COPY_AND_ASSIGN(ClassDef); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | // Raw type_item. |
| 180 | struct TypeItem { |
| 181 | uint16_t type_idx_; // index into type_ids section |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 182 | private: |
| 183 | DISALLOW_COPY_AND_ASSIGN(TypeItem); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 184 | }; |
| 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 Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 201 | DISALLOW_COPY_AND_ASSIGN(TypeList); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 204 | // 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 Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 211 | uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 212 | uint16_t insns_[1]; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 213 | private: |
| 214 | DISALLOW_COPY_AND_ASSIGN(CodeItem); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
Carl Shapiro | 2eaa968 | 2011-08-04 19:26:11 -0700 | [diff] [blame] | 217 | // Raw try_item. |
| 218 | struct TryItem { |
| 219 | uint32_t start_addr_; |
| 220 | uint16_t insn_count_; |
| 221 | uint16_t handler_off_; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 222 | private: |
| 223 | DISALLOW_COPY_AND_ASSIGN(TryItem); |
Carl Shapiro | 2eaa968 | 2011-08-04 19:26:11 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 226 | // Annotation constants. |
| 227 | enum { |
| 228 | kDexVisibilityBuild = 0x00, /* annotation visibility */ |
| 229 | kDexVisibilityRuntime = 0x01, |
| 230 | kDexVisibilitySystem = 0x02, |
| 231 | |
| 232 | kDexAnnotationByte = 0x00, |
| 233 | kDexAnnotationShort = 0x02, |
| 234 | kDexAnnotationChar = 0x03, |
| 235 | kDexAnnotationInt = 0x04, |
| 236 | kDexAnnotationLong = 0x06, |
| 237 | kDexAnnotationFloat = 0x10, |
| 238 | kDexAnnotationDouble = 0x11, |
| 239 | kDexAnnotationString = 0x17, |
| 240 | kDexAnnotationType = 0x18, |
| 241 | kDexAnnotationField = 0x19, |
| 242 | kDexAnnotationMethod = 0x1a, |
| 243 | kDexAnnotationEnum = 0x1b, |
| 244 | kDexAnnotationArray = 0x1c, |
| 245 | kDexAnnotationAnnotation = 0x1d, |
| 246 | kDexAnnotationNull = 0x1e, |
| 247 | kDexAnnotationBoolean = 0x1f, |
| 248 | |
| 249 | kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */ |
| 250 | kDexAnnotationValueArgShift = 5, |
| 251 | }; |
| 252 | |
| 253 | struct AnnotationsDirectoryItem { |
| 254 | uint32_t class_annotations_off_; |
| 255 | uint32_t fields_size_; |
| 256 | uint32_t methods_size_; |
| 257 | uint32_t parameters_size_; |
| 258 | private: |
| 259 | DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem); |
| 260 | }; |
| 261 | |
| 262 | struct FieldAnnotationsItem { |
| 263 | uint32_t field_idx_; |
| 264 | uint32_t annotations_off_; |
| 265 | private: |
| 266 | DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem); |
| 267 | }; |
| 268 | |
| 269 | struct MethodAnnotationsItem { |
| 270 | uint32_t method_idx_; |
| 271 | uint32_t annotations_off_; |
| 272 | private: |
| 273 | DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem); |
| 274 | }; |
| 275 | |
| 276 | struct ParameterAnnotationsItem { |
| 277 | uint32_t method_idx_; |
| 278 | uint32_t annotations_off_; |
| 279 | private: |
| 280 | DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem); |
| 281 | }; |
| 282 | |
| 283 | struct AnnotationSetRefItem { |
| 284 | uint32_t annotations_off_; |
| 285 | private: |
| 286 | DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem); |
| 287 | }; |
| 288 | |
| 289 | struct AnnotationSetRefList { |
| 290 | uint32_t size_; |
| 291 | AnnotationSetRefItem list_[1]; |
| 292 | private: |
| 293 | DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList); |
| 294 | }; |
| 295 | |
| 296 | struct AnnotationSetItem { |
| 297 | uint32_t size_; |
| 298 | uint32_t entries_[1]; |
| 299 | private: |
| 300 | DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem); |
| 301 | }; |
| 302 | |
| 303 | struct AnnotationItem { |
| 304 | uint8_t visibility_; |
| 305 | uint8_t annotation_[1]; |
| 306 | private: |
| 307 | DISALLOW_COPY_AND_ASSIGN(AnnotationItem); |
| 308 | }; |
| 309 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 310 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 311 | typedef std::vector<const DexFile*> ClassPath; |
| 312 | |
| 313 | // Search a collection of DexFiles for a descriptor |
| 314 | static ClassPathEntry FindInClassPath(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 315 | const ClassPath& class_path); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 316 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 317 | // Returns the checksum of a file for comparison with GetLocationChecksum(). |
| 318 | // For .dex files, this is the header checksum. |
| 319 | // For zip files, this is the classes.dex zip entry CRC32 checksum. |
| 320 | // Return true if the checksum could be found, false otherwise. |
| 321 | static bool GetChecksum(const std::string& filename, uint32_t& checksum); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 322 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 323 | // Opens .dex file, guessing the container format based on file extension |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 324 | static const DexFile* Open(const std::string& filename, |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 325 | const std::string& location); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 326 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 327 | // Opens .dex file, backed by existing memory |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 328 | static const DexFile* Open(const uint8_t* base, size_t size, |
| 329 | const std::string& location, uint32_t location_checksum) { |
| 330 | return OpenMemory(base, size, location, location_checksum, NULL); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 333 | // Opens .dex file from the classes.dex in a zip archive |
| 334 | static const DexFile* Open(const ZipArchive& zip_archive, const std::string& location); |
| 335 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 336 | // Closes a .dex file. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 337 | virtual ~DexFile(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 338 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 339 | const std::string& GetLocation() const { |
| 340 | return location_; |
| 341 | } |
| 342 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 343 | // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header. |
| 344 | // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex. |
| 345 | uint32_t GetLocationChecksum() const { |
| 346 | return location_checksum_; |
| 347 | } |
| 348 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 349 | // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file. |
| 350 | // Used by managed code to implement annotations. |
| 351 | jobject GetDexObject(JNIEnv* env) const; |
| 352 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 353 | const Header& GetHeader() const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 354 | CHECK(header_ != NULL) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 355 | return *header_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 358 | // Decode the dex magic version |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 359 | uint32_t GetVersion() const; |
| 360 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 361 | // Returns true if the byte string points to the magic value. |
| 362 | static bool IsMagicValid(const byte* magic); |
| 363 | |
| 364 | // Returns true if the byte string after the magic is the correct value. |
| 365 | static bool IsVersionValid(const byte* magic); |
| 366 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 367 | // Returns the number of string identifiers in the .dex file. |
| 368 | size_t NumStringIds() const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 369 | CHECK(header_ != NULL) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 370 | return header_->string_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 373 | // Returns the StringId at the specified index. |
| 374 | const StringId& GetStringId(uint32_t idx) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 375 | CHECK_LT(idx, NumStringIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 376 | return string_ids_[idx]; |
| 377 | } |
| 378 | |
| 379 | uint32_t GetIndexForStringId(const StringId& string_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 380 | CHECK_GE(&string_id, string_ids_) << GetLocation(); |
| 381 | CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 382 | return &string_id - string_ids_; |
| 383 | } |
| 384 | |
| 385 | int32_t GetStringLength(const StringId& string_id) const; |
| 386 | |
| 387 | // Returns a pointer to the UTF-8 string data referred to by the given string_id. |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 388 | const char* GetStringDataAndLength(const StringId& string_id, uint32_t* length) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 389 | |
| 390 | const char* GetStringData(const StringId& string_id) const { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 391 | uint32_t length; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 392 | return GetStringDataAndLength(string_id, &length); |
| 393 | } |
| 394 | |
| 395 | // return the UTF-8 encoded string with the specified string_id index |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 396 | const char* StringDataAndLengthByIdx(uint32_t idx, uint32_t* unicode_length) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 397 | if (idx == kDexNoIndex) { |
| 398 | *unicode_length = 0; |
| 399 | return NULL; |
| 400 | } |
| 401 | const StringId& string_id = GetStringId(idx); |
| 402 | return GetStringDataAndLength(string_id, unicode_length); |
| 403 | } |
| 404 | |
| 405 | const char* StringDataByIdx(uint32_t idx) const { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 406 | uint32_t unicode_length; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 407 | return StringDataAndLengthByIdx(idx, &unicode_length); |
| 408 | } |
| 409 | |
| 410 | // Looks up a string id for a given string |
| 411 | const StringId* FindStringId(const std::string& string) const; |
| 412 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 413 | // Returns the number of type identifiers in the .dex file. |
| 414 | size_t NumTypeIds() const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 415 | CHECK(header_ != NULL) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 416 | return header_->type_ids_size_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 419 | // Returns the TypeId at the specified index. |
| 420 | const TypeId& GetTypeId(uint32_t idx) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 421 | CHECK_LT(idx, NumTypeIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 422 | return type_ids_[idx]; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 425 | uint16_t GetIndexForTypeId(const TypeId& type_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 426 | CHECK_GE(&type_id, type_ids_) << GetLocation(); |
| 427 | CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 428 | size_t result = &type_id - type_ids_; |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 429 | DCHECK_LT(result, 65536U) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 430 | return static_cast<uint16_t>(result); |
| 431 | } |
| 432 | |
| 433 | // Get the descriptor string associated with a given type index. |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 434 | const char* StringByTypeIdx(uint32_t idx, uint32_t* unicode_length) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 435 | const TypeId& type_id = GetTypeId(idx); |
| 436 | return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length); |
| 437 | } |
| 438 | |
| 439 | const char* StringByTypeIdx(uint32_t idx) const { |
| 440 | const TypeId& type_id = GetTypeId(idx); |
| 441 | return StringDataByIdx(type_id.descriptor_idx_); |
| 442 | } |
| 443 | |
| 444 | // Returns the type descriptor string of a type id. |
| 445 | const char* GetTypeDescriptor(const TypeId& type_id) const { |
| 446 | return StringDataByIdx(type_id.descriptor_idx_); |
| 447 | } |
| 448 | |
| 449 | // Looks up a type for the given string index |
| 450 | const TypeId* FindTypeId(uint32_t string_idx) const; |
| 451 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 452 | // Returns the number of field identifiers in the .dex file. |
| 453 | size_t NumFieldIds() const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 454 | CHECK(header_ != NULL) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 455 | return header_->field_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 458 | // Returns the FieldId at the specified index. |
| 459 | const FieldId& GetFieldId(uint32_t idx) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 460 | CHECK_LT(idx, NumFieldIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 461 | return field_ids_[idx]; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 464 | uint32_t GetIndexForFieldId(const FieldId& field_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 465 | CHECK_GE(&field_id, field_ids_) << GetLocation(); |
| 466 | CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation(); |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 467 | return &field_id - field_ids_; |
| 468 | } |
| 469 | |
| 470 | // Looks up a field by its declaring class, name and type |
| 471 | const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass, |
| 472 | const DexFile::StringId& name, |
| 473 | const DexFile::TypeId& type) const; |
| 474 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 475 | // Returns the declaring class descriptor string of a field id. |
| 476 | const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const { |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 477 | const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_); |
| 478 | return GetTypeDescriptor(type_id); |
| 479 | } |
| 480 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 481 | // Returns the class descriptor string of a field id. |
| 482 | const char* GetFieldTypeDescriptor(const FieldId& field_id) const { |
| 483 | const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_); |
| 484 | return GetTypeDescriptor(type_id); |
| 485 | } |
| 486 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 487 | // Returns the name of a field id. |
| 488 | const char* GetFieldName(const FieldId& field_id) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 489 | return StringDataByIdx(field_id.name_idx_); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 492 | // Returns the number of method identifiers in the .dex file. |
| 493 | size_t NumMethodIds() const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 494 | CHECK(header_ != NULL) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 495 | return header_->method_ids_size_; |
| 496 | } |
| 497 | |
| 498 | // Returns the MethodId at the specified index. |
| 499 | const MethodId& GetMethodId(uint32_t idx) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 500 | CHECK_LT(idx, NumMethodIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 501 | return method_ids_[idx]; |
| 502 | } |
| 503 | |
| 504 | uint32_t GetIndexForMethodId(const MethodId& method_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 505 | CHECK_GE(&method_id, method_ids_) << GetLocation(); |
| 506 | CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 507 | return &method_id - method_ids_; |
| 508 | } |
| 509 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 510 | // Looks up a method by its declaring class, name and proto_id |
| 511 | const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass, |
| 512 | const DexFile::StringId& name, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 513 | const DexFile::ProtoId& signature) const; |
| 514 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 515 | // Returns the declaring class descriptor string of a method id. |
| 516 | const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const { |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 517 | const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_); |
| 518 | return GetTypeDescriptor(type_id); |
| 519 | } |
| 520 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 521 | // Returns the prototype of a method id. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 522 | const ProtoId& GetMethodPrototype(const MethodId& method_id) const { |
| 523 | return GetProtoId(method_id.proto_idx_); |
| 524 | } |
| 525 | |
| 526 | // Returns the signature of a method id. |
| 527 | const std::string GetMethodSignature(const MethodId& method_id) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 528 | return CreateMethodSignature(method_id.proto_idx_, NULL); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 531 | // Returns the name of a method id. |
| 532 | const char* GetMethodName(const MethodId& method_id) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 533 | return StringDataByIdx(method_id.name_idx_); |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 536 | // Returns the shorty of a method id. |
| 537 | const char* GetMethodShorty(const MethodId& method_id) const { |
| 538 | return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 539 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 540 | const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 541 | return StringDataAndLengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length); |
| 542 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 543 | // Returns the number of class definitions in the .dex file. |
| 544 | size_t NumClassDefs() const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 545 | CHECK(header_ != NULL) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 546 | return header_->class_defs_size_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | // Returns the ClassDef at the specified index. |
| 550 | const ClassDef& GetClassDef(uint32_t idx) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 551 | CHECK_LT(idx, NumClassDefs()) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 552 | return class_defs_[idx]; |
| 553 | } |
| 554 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 555 | uint32_t GetIndexForClassDef(const ClassDef& class_def) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 556 | CHECK_GE(&class_def, class_defs_) << GetLocation(); |
| 557 | CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 558 | return &class_def - class_defs_; |
| 559 | } |
| 560 | |
| 561 | // Returns the class descriptor string of a class definition. |
| 562 | const char* GetClassDescriptor(const ClassDef& class_def) const { |
| 563 | return StringByTypeIdx(class_def.class_idx_); |
| 564 | } |
| 565 | |
| 566 | // Looks up a class definition by its class descriptor. |
| 567 | const ClassDef* FindClassDef(const StringPiece& descriptor) const; |
| 568 | |
| 569 | // Looks up a class definition index by its class descriptor. |
| 570 | bool FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const; |
| 571 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 572 | const TypeList* GetInterfacesList(const ClassDef& class_def) const { |
| 573 | if (class_def.interfaces_off_ == 0) { |
| 574 | return NULL; |
| 575 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 576 | const byte* addr = begin_ + class_def.interfaces_off_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 577 | return reinterpret_cast<const TypeList*>(addr); |
| 578 | } |
| 579 | } |
| 580 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 581 | // Returns a pointer to the raw memory mapped class_data_item |
| 582 | const byte* GetClassData(const ClassDef& class_def) const { |
| 583 | if (class_def.class_data_off_ == 0) { |
| 584 | return NULL; |
| 585 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 586 | return begin_ + class_def.class_data_off_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 587 | } |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 590 | // |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 591 | const CodeItem* GetCodeItem(const uint32_t code_off) const { |
| 592 | if (code_off == 0) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 593 | return NULL; // native or abstract method |
| 594 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 595 | const byte* addr = begin_ + code_off; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 596 | return reinterpret_cast<const CodeItem*>(addr); |
| 597 | } |
| 598 | } |
| 599 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 600 | const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const { |
| 601 | return StringByTypeIdx(proto_id.return_type_idx_); |
| 602 | } |
| 603 | |
| 604 | // Returns the number of prototype identifiers in the .dex file. |
| 605 | size_t NumProtoIds() const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 606 | CHECK(header_ != NULL) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 607 | return header_->proto_ids_size_; |
| 608 | } |
| 609 | |
| 610 | // Returns the ProtoId at the specified index. |
| 611 | const ProtoId& GetProtoId(uint32_t idx) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 612 | CHECK_LT(idx, NumProtoIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 613 | return proto_ids_[idx]; |
| 614 | } |
| 615 | |
| 616 | uint16_t GetIndexForProtoId(const ProtoId& proto_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 617 | CHECK_GE(&proto_id, proto_ids_) << GetLocation(); |
| 618 | CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 619 | return &proto_id - proto_ids_; |
| 620 | } |
| 621 | |
| 622 | // Looks up a proto id for a given return type and signature type list |
| 623 | const ProtoId* FindProtoId(uint16_t return_type_id, |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 624 | const std::vector<uint16_t>& signature_type_idxs_) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 625 | |
| 626 | // Given a signature place the type ids into the given vector, returns true on success |
| 627 | bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs, |
| 628 | const std::string& signature) const; |
| 629 | |
| 630 | // Given a proto_idx decode the type list and return type into a method signature |
| 631 | std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const; |
| 632 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 633 | // Returns the short form method descriptor for the given prototype. |
| 634 | const char* GetShorty(uint32_t proto_idx) const { |
| 635 | const ProtoId& proto_id = GetProtoId(proto_idx); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 636 | return StringDataByIdx(proto_id.shorty_idx_); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | const TypeList* GetProtoParameters(const ProtoId& proto_id) const { |
| 640 | if (proto_id.parameters_off_ == 0) { |
| 641 | return NULL; |
| 642 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 643 | const byte* addr = begin_ + proto_id.parameters_off_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 644 | return reinterpret_cast<const TypeList*>(addr); |
| 645 | } |
| 646 | } |
| 647 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 648 | const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 649 | if (class_def.static_values_off_ == 0) { |
| 650 | return 0; |
| 651 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 652 | return begin_ + class_def.static_values_off_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 656 | static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 657 | const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_]; |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 658 | return reinterpret_cast<const TryItem*> |
| 659 | (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset; |
| 660 | } |
| 661 | |
| 662 | // Get the base of the encoded data for the given DexCode. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 663 | static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) { |
| 664 | const byte* handler_data = |
| 665 | reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 666 | return handler_data + offset; |
| 667 | } |
| 668 | |
| 669 | // Find the handler associated with a given address, if any. |
| 670 | // Initializes the given iterator and returns true if a match is |
| 671 | // found. Returns end if there is no applicable handler. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 672 | static int32_t FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size, |
| 673 | uint32_t address); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 674 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 675 | // Get the pointer to the start of the debugging data |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 676 | const byte* GetDebugInfoStream(const CodeItem* code_item) const { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 677 | if (code_item->debug_info_off_ == 0) { |
| 678 | return NULL; |
| 679 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 680 | return begin_ + code_item->debug_info_off_; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
| 684 | // Callback for "new position table entry". |
| 685 | // Returning true causes the decoder to stop early. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 686 | typedef bool (*DexDebugNewPositionCb)(void* context, uint32_t address, uint32_t line_num); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 687 | |
| 688 | // Callback for "new locals table entry". "signature" is an empty string |
| 689 | // if no signature is available for an entry. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 690 | typedef void (*DexDebugNewLocalCb)(void* context, uint16_t reg, |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 691 | uint32_t startAddress, |
| 692 | uint32_t endAddress, |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 693 | const char* name, |
| 694 | const char* descriptor, |
| 695 | const char* signature); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 696 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 697 | static bool LineNumForPcCb(void* context, uint32_t address, uint32_t line_num); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 698 | |
| 699 | // Debug info opcodes and constants |
| 700 | enum { |
| 701 | DBG_END_SEQUENCE = 0x00, |
| 702 | DBG_ADVANCE_PC = 0x01, |
| 703 | DBG_ADVANCE_LINE = 0x02, |
| 704 | DBG_START_LOCAL = 0x03, |
| 705 | DBG_START_LOCAL_EXTENDED = 0x04, |
| 706 | DBG_END_LOCAL = 0x05, |
| 707 | DBG_RESTART_LOCAL = 0x06, |
| 708 | DBG_SET_PROLOGUE_END = 0x07, |
| 709 | DBG_SET_EPILOGUE_BEGIN = 0x08, |
| 710 | DBG_SET_FILE = 0x09, |
| 711 | DBG_FIRST_SPECIAL = 0x0a, |
| 712 | DBG_LINE_BASE = -4, |
| 713 | DBG_LINE_RANGE = 15, |
| 714 | }; |
| 715 | |
| 716 | struct LocalInfo { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 717 | LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0), |
| 718 | is_live_(false) {} |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 719 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 720 | const char* name_; // E.g., list |
| 721 | const char* descriptor_; // E.g., Ljava/util/LinkedList; |
| 722 | const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer> |
| 723 | uint16_t start_address_; // PC location where the local is first defined. |
| 724 | bool is_live_; // Is the local defined and live. |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 725 | |
| 726 | private: |
| 727 | DISALLOW_COPY_AND_ASSIGN(LocalInfo); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 728 | }; |
| 729 | |
| 730 | struct LineNumFromPcContext { |
| 731 | LineNumFromPcContext(uint32_t address, uint32_t line_num) : |
| 732 | address_(address), line_num_(line_num) {} |
| 733 | uint32_t address_; |
| 734 | uint32_t line_num_; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 735 | private: |
| 736 | DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 737 | }; |
| 738 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 739 | void InvokeLocalCbIfLive(void* context, int reg, uint32_t end_address, |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 740 | LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 741 | if (local_cb != NULL && local_in_reg[reg].is_live_) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 742 | local_cb(context, reg, local_in_reg[reg].start_address_, end_address, |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 743 | local_in_reg[reg].name_, local_in_reg[reg].descriptor_, |
| 744 | local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : ""); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
| 748 | // Determine the source file line number based on the program counter. |
| 749 | // "pc" is an offset, in 16-bit units, from the start of the method's code. |
| 750 | // |
| 751 | // Returns -1 if no match was found (possibly because the source files were |
| 752 | // compiled without "-g", so no line number information is present). |
| 753 | // Returns -2 for native methods (as expected in exception traces). |
| 754 | // |
| 755 | // This is used by runtime; therefore use art::Method not art::DexFile::Method. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 756 | int32_t GetLineNumFromPC(const Method* method, uint32_t rel_pc) const; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 757 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 758 | void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx, |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 759 | DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb, |
| 760 | void* context) const; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 761 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 762 | const char* GetSourceFile(const ClassDef& class_def) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 763 | if (class_def.source_file_idx_ == 0xffffffff) { |
| 764 | return NULL; |
| 765 | } else { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 766 | return StringDataByIdx(class_def.source_file_idx_); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 767 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 768 | } |
| 769 | |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 770 | void ChangePermissions(int prot) const; |
| 771 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 772 | private: |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 773 | |
| 774 | // Opens a .dex file |
| 775 | static const DexFile* OpenFile(const std::string& filename, |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 776 | const std::string& location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 777 | bool verify); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 778 | |
| 779 | // Opens a dex file from within a .jar, .zip, or .apk file |
| 780 | static const DexFile* OpenZip(const std::string& filename, |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 781 | const std::string& location); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 782 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 783 | // Opens a .dex file at the given address backed by a MemMap |
| 784 | static const DexFile* OpenMemory(const std::string& location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 785 | uint32_t location_checksum, |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 786 | MemMap* mem_map) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 787 | return OpenMemory(mem_map->Begin(), |
| 788 | mem_map->Size(), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 789 | location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 790 | location_checksum, |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 791 | mem_map); |
| 792 | } |
| 793 | |
| 794 | // Opens a .dex file at the given address, optionally backed by a MemMap |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 795 | static const DexFile* OpenMemory(const byte* dex_file, |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 796 | size_t size, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 797 | const std::string& location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 798 | uint32_t location_checksum, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 799 | MemMap* mem_map); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 800 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 801 | DexFile(const byte* base, size_t size, |
| 802 | const std::string& location, uint32_t location_checksum, |
| 803 | MemMap* mem_map) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 804 | : begin_(base), |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 805 | size_(size), |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 806 | location_(location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 807 | location_checksum_(location_checksum), |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 808 | mem_map_(mem_map), |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 809 | dex_object_lock_("a dex_object_lock_"), |
| 810 | dex_object_(NULL), |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 811 | header_(0), |
| 812 | string_ids_(0), |
| 813 | type_ids_(0), |
| 814 | field_ids_(0), |
| 815 | method_ids_(0), |
| 816 | proto_ids_(0), |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 817 | class_defs_(0) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 818 | CHECK(begin_ != NULL) << GetLocation(); |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 819 | CHECK_GT(size_, 0U) << GetLocation(); |
| 820 | } |
| 821 | |
| 822 | const byte* Begin() const { |
| 823 | return begin_; |
| 824 | } |
| 825 | |
| 826 | size_t Size() const { |
| 827 | return size_; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 828 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 829 | |
| 830 | // Top-level initializer that calls other Init methods. |
| 831 | bool Init(); |
| 832 | |
| 833 | // Caches pointers into to the various file sections. |
| 834 | void InitMembers(); |
| 835 | |
| 836 | // Builds the index of descriptors to class definitions. |
| 837 | void InitIndex(); |
| 838 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 839 | // Returns true if the header magic and version numbers are of the expected values. |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 840 | bool CheckMagicAndVersion() const; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 841 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 842 | void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx, |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 843 | DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb, |
| 844 | void* context, const byte* stream, LocalInfo* local_in_reg) const; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 845 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 846 | // The index of descriptors to class definition indexes (as opposed to type id indexes) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 847 | typedef std::map<const StringPiece, uint32_t> Index; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 848 | Index index_; |
| 849 | |
| 850 | // The base address of the memory mapping. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 851 | const byte* begin_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 852 | |
| 853 | // The size of the underlying memory allocation in bytes. |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 854 | size_t size_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 855 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 856 | // Typically the dex file name when available, alternatively some identifying string. |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 857 | // |
| 858 | // The ClassLinker will use this to match DexFiles the boot class |
| 859 | // path to DexCache::GetLocation when loading from an image. |
| 860 | const std::string location_; |
| 861 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 862 | const uint32_t location_checksum_; |
| 863 | |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 864 | // Manages the underlying memory allocation. |
| 865 | UniquePtr<MemMap> mem_map_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 866 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 867 | // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject. |
| 868 | mutable Mutex dex_object_lock_; |
| 869 | mutable jobject dex_object_; |
| 870 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 871 | // Points to the header section. |
| 872 | const Header* header_; |
| 873 | |
| 874 | // Points to the base of the string identifier list. |
| 875 | const StringId* string_ids_; |
| 876 | |
| 877 | // Points to the base of the type identifier list. |
| 878 | const TypeId* type_ids_; |
| 879 | |
| 880 | // Points to the base of the field identifier list. |
| 881 | const FieldId* field_ids_; |
| 882 | |
| 883 | // Points to the base of the method identifier list. |
| 884 | const MethodId* method_ids_; |
| 885 | |
| 886 | // Points to the base of the prototype identifier list. |
| 887 | const ProtoId* proto_ids_; |
| 888 | |
| 889 | // Points to the base of the class definition list. |
| 890 | const ClassDef* class_defs_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 891 | }; |
| 892 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 893 | // Iterate over a dex file's ProtoId's paramters |
| 894 | class DexFileParameterIterator { |
| 895 | public: |
| 896 | DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id) |
| 897 | : dex_file_(dex_file), size_(0), pos_(0) { |
| 898 | type_list_ = dex_file_.GetProtoParameters(proto_id); |
| 899 | if (type_list_ != NULL) { |
| 900 | size_ = type_list_->Size(); |
| 901 | } |
| 902 | } |
| 903 | bool HasNext() const { return pos_ < size_; } |
| 904 | void Next() { ++pos_; } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 905 | uint16_t GetTypeIdx() { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 906 | return type_list_->GetTypeItem(pos_).type_idx_; |
| 907 | } |
| 908 | const char* GetDescriptor() { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 909 | return dex_file_.StringByTypeIdx(GetTypeIdx()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 910 | } |
| 911 | private: |
| 912 | const DexFile& dex_file_; |
| 913 | const DexFile::TypeList* type_list_; |
| 914 | uint32_t size_; |
| 915 | uint32_t pos_; |
| 916 | DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator); |
| 917 | }; |
| 918 | |
| 919 | // Iterate and decode class_data_item |
| 920 | class ClassDataItemIterator { |
| 921 | public: |
| 922 | ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item) |
| 923 | : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) { |
| 924 | ReadClassDataHeader(); |
| 925 | if (EndOfInstanceFieldsPos() > 0) { |
| 926 | ReadClassDataField(); |
| 927 | } else if (EndOfVirtualMethodsPos() > 0) { |
| 928 | ReadClassDataMethod(); |
| 929 | } |
| 930 | } |
| 931 | uint32_t NumStaticFields() const { |
| 932 | return header_.static_fields_size_; |
| 933 | } |
| 934 | uint32_t NumInstanceFields() const { |
| 935 | return header_.instance_fields_size_; |
| 936 | } |
| 937 | uint32_t NumDirectMethods() const { |
| 938 | return header_.direct_methods_size_; |
| 939 | } |
| 940 | uint32_t NumVirtualMethods() const { |
| 941 | return header_.virtual_methods_size_; |
| 942 | } |
| 943 | bool HasNextStaticField() const { |
| 944 | return pos_ < EndOfStaticFieldsPos(); |
| 945 | } |
| 946 | bool HasNextInstanceField() const { |
| 947 | return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos(); |
| 948 | } |
| 949 | bool HasNextDirectMethod() const { |
| 950 | return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos(); |
| 951 | } |
| 952 | bool HasNextVirtualMethod() const { |
| 953 | return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos(); |
| 954 | } |
| 955 | bool HasNext() const { |
| 956 | return pos_ < EndOfVirtualMethodsPos(); |
| 957 | } |
| 958 | void Next() { |
| 959 | pos_++; |
| 960 | if (pos_ < EndOfStaticFieldsPos()) { |
| 961 | last_idx_ = GetMemberIndex(); |
| 962 | ReadClassDataField(); |
| 963 | } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) { |
| 964 | last_idx_ = 0; // transition to next array, reset last index |
| 965 | ReadClassDataField(); |
| 966 | } else if (pos_ < EndOfInstanceFieldsPos()) { |
| 967 | last_idx_ = GetMemberIndex(); |
| 968 | ReadClassDataField(); |
| 969 | } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) { |
| 970 | last_idx_ = 0; // transition to next array, reset last index |
| 971 | ReadClassDataMethod(); |
| 972 | } else if (pos_ < EndOfDirectMethodsPos()) { |
| 973 | last_idx_ = GetMemberIndex(); |
| 974 | ReadClassDataMethod(); |
| 975 | } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) { |
| 976 | last_idx_ = 0; // transition to next array, reset last index |
| 977 | ReadClassDataMethod(); |
| 978 | } else if (pos_ < EndOfVirtualMethodsPos()) { |
| 979 | last_idx_ = GetMemberIndex(); |
| 980 | ReadClassDataMethod(); |
| 981 | } else { |
| 982 | DCHECK(!HasNext()); |
| 983 | } |
| 984 | } |
| 985 | uint32_t GetMemberIndex() const { |
| 986 | if (pos_ < EndOfInstanceFieldsPos()) { |
| 987 | return last_idx_ + field_.field_idx_delta_; |
| 988 | } else { |
| 989 | CHECK_LT(pos_, EndOfVirtualMethodsPos()); |
| 990 | return last_idx_ + method_.method_idx_delta_; |
| 991 | } |
| 992 | } |
| 993 | uint32_t GetMemberAccessFlags() const { |
| 994 | if (pos_ < EndOfInstanceFieldsPos()) { |
| 995 | return field_.access_flags_; |
| 996 | } else { |
| 997 | CHECK_LT(pos_, EndOfVirtualMethodsPos()); |
| 998 | return method_.access_flags_; |
| 999 | } |
| 1000 | } |
| 1001 | const DexFile::CodeItem* GetMethodCodeItem() const { |
| 1002 | return dex_file_.GetCodeItem(method_.code_off_); |
| 1003 | } |
| 1004 | uint32_t GetMethodCodeItemOffset() const { |
| 1005 | return method_.code_off_; |
| 1006 | } |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1007 | const byte* EndDataPointer() const { |
| 1008 | CHECK(!HasNext()); |
| 1009 | return ptr_pos_; |
| 1010 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1011 | private: |
| 1012 | // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the |
| 1013 | // header for a class_data_item |
| 1014 | struct ClassDataHeader { |
| 1015 | uint32_t static_fields_size_; // the number of static fields |
| 1016 | uint32_t instance_fields_size_; // the number of instance fields |
| 1017 | uint32_t direct_methods_size_; // the number of direct methods |
| 1018 | uint32_t virtual_methods_size_; // the number of virtual methods |
| 1019 | } header_; |
| 1020 | |
| 1021 | // Read and decode header from a class_data_item stream into header |
| 1022 | void ReadClassDataHeader(); |
| 1023 | |
| 1024 | uint32_t EndOfStaticFieldsPos() const { |
| 1025 | return header_.static_fields_size_; |
| 1026 | } |
| 1027 | uint32_t EndOfInstanceFieldsPos() const { |
| 1028 | return EndOfStaticFieldsPos() + header_.instance_fields_size_; |
| 1029 | } |
| 1030 | uint32_t EndOfDirectMethodsPos() const { |
| 1031 | return EndOfInstanceFieldsPos() + header_.direct_methods_size_; |
| 1032 | } |
| 1033 | uint32_t EndOfVirtualMethodsPos() const { |
| 1034 | return EndOfDirectMethodsPos() + header_.virtual_methods_size_; |
| 1035 | } |
| 1036 | |
| 1037 | // A decoded version of the field of a class_data_item |
| 1038 | struct ClassDataField { |
| 1039 | uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId |
| 1040 | uint32_t access_flags_; // access flags for the field |
| 1041 | ClassDataField() : field_idx_delta_(0), access_flags_(0) {} |
| 1042 | private: |
| 1043 | DISALLOW_COPY_AND_ASSIGN(ClassDataField); |
| 1044 | } field_; |
| 1045 | |
| 1046 | // Read and decode a field from a class_data_item stream into field |
| 1047 | void ReadClassDataField(); |
| 1048 | |
| 1049 | // A decoded version of the method of a class_data_item |
| 1050 | struct ClassDataMethod { |
| 1051 | uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId |
| 1052 | uint32_t access_flags_; |
| 1053 | uint32_t code_off_; |
| 1054 | ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {} |
| 1055 | private: |
| 1056 | DISALLOW_COPY_AND_ASSIGN(ClassDataMethod); |
| 1057 | } method_; |
| 1058 | |
| 1059 | // Read and decode a method from a class_data_item stream into method |
| 1060 | void ReadClassDataMethod(); |
| 1061 | |
| 1062 | const DexFile& dex_file_; |
| 1063 | size_t pos_; // integral number of items passed |
| 1064 | const byte* ptr_pos_; // pointer into stream of class_data_item |
| 1065 | uint32_t last_idx_; // last read field or method index to apply delta to |
| 1066 | DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator); |
| 1067 | }; |
| 1068 | |
| 1069 | class ClassLinker; |
| 1070 | class DexCache; |
| 1071 | class Field; |
| 1072 | |
| 1073 | class EncodedStaticFieldValueIterator { |
| 1074 | public: |
| 1075 | EncodedStaticFieldValueIterator(const DexFile& dex_file, DexCache* dex_cache, |
| 1076 | ClassLinker* linker, const DexFile::ClassDef& class_def); |
| 1077 | |
| 1078 | void ReadValueToField(Field* field) const; |
| 1079 | |
| 1080 | bool HasNext() { return pos_ < array_size_; } |
| 1081 | |
| 1082 | void Next(); |
| 1083 | private: |
| 1084 | enum ValueType { |
| 1085 | kByte = 0x00, |
| 1086 | kShort = 0x02, |
| 1087 | kChar = 0x03, |
| 1088 | kInt = 0x04, |
| 1089 | kLong = 0x06, |
| 1090 | kFloat = 0x10, |
| 1091 | kDouble = 0x11, |
| 1092 | kString = 0x17, |
| 1093 | kType = 0x18, |
| 1094 | kField = 0x19, |
| 1095 | kMethod = 0x1a, |
| 1096 | kEnum = 0x1b, |
| 1097 | kArray = 0x1c, |
| 1098 | kAnnotation = 0x1d, |
| 1099 | kNull = 0x1e, |
| 1100 | kBoolean = 0x1f |
| 1101 | }; |
| 1102 | |
| 1103 | static const byte kEncodedValueTypeMask = 0x1f; // 0b11111 |
| 1104 | static const byte kEncodedValueArgShift = 5; |
| 1105 | |
| 1106 | const DexFile& dex_file_; |
| 1107 | DexCache* dex_cache_; // dex cache to resolve literal objects |
| 1108 | ClassLinker* linker_; // linker to resolve literal objects |
| 1109 | size_t array_size_; // size of array |
| 1110 | size_t pos_; // current position |
| 1111 | const byte* ptr_; // pointer into encoded data array |
| 1112 | byte type_; // type of current encoded value |
| 1113 | jvalue jval_; // value of current encoded value |
| 1114 | DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator); |
| 1115 | }; |
| 1116 | |
| 1117 | class CatchHandlerIterator { |
| 1118 | public: |
| 1119 | CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address); |
| 1120 | explicit CatchHandlerIterator(const byte* handler_data) { |
| 1121 | Init(handler_data); |
| 1122 | } |
| 1123 | |
| 1124 | uint16_t GetHandlerTypeIndex() const { |
| 1125 | return handler_.type_idx_; |
| 1126 | } |
| 1127 | uint32_t GetHandlerAddress() const { |
| 1128 | return handler_.address_; |
| 1129 | } |
| 1130 | void Next(); |
| 1131 | bool HasNext() const { |
| 1132 | return remaining_count_ != -1 || catch_all_; |
| 1133 | } |
| 1134 | // End of this set of catch blocks, convenience method to locate next set of catch blocks |
| 1135 | const byte* EndDataPointer() const { |
| 1136 | CHECK(!HasNext()); |
| 1137 | return current_data_; |
| 1138 | } |
| 1139 | private: |
| 1140 | void Init(const byte* handler_data); |
| 1141 | |
| 1142 | struct CatchHandlerItem { |
| 1143 | uint16_t type_idx_; // type index of the caught exception type |
| 1144 | uint32_t address_; // handler address |
| 1145 | } handler_; |
| 1146 | const byte *current_data_; // the current handler in dex file. |
| 1147 | int32_t remaining_count_; // number of handlers not read. |
| 1148 | bool catch_all_; // is there a handler that will catch all exceptions in case |
| 1149 | // that all typed handler does not match. |
| 1150 | }; |
| 1151 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1152 | } // namespace art |
| 1153 | |
| 1154 | #endif // ART_SRC_DEX_FILE_H_ |