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