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