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