Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_DEX_FILE_H_ |
| 4 | #define ART_SRC_DEX_FILE_H_ |
| 5 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 6 | #include <map> |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 7 | #include <string> |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 8 | #include <vector> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 9 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 10 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | #include "globals.h" |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame^] | 12 | #include "jni.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 13 | #include "leb128.h" |
| 14 | #include "logging.h" |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame^] | 15 | #include "mutex.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 16 | #include "stringpiece.h" |
| 17 | #include "strutil.h" |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 18 | #include "utils.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 19 | |
| 20 | namespace art { |
| 21 | |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 22 | union JValue; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 23 | class String; |
| 24 | class Method; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 26 | // TODO: move all of the macro functionality into the DexCache class. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 27 | class DexFile { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 28 | public: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 29 | static const byte kDexMagic[]; |
| 30 | static const byte kDexMagicVersion[]; |
| 31 | static const size_t kSha1DigestSize = 20; |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 32 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 33 | static const byte kEncodedValueTypeMask = 0x1f; // 0b11111 |
| 34 | static const byte kEncodedValueArgShift = 5; |
| 35 | |
| 36 | // The value of an invalid index. |
| 37 | static const uint32_t kDexNoIndex = 0xFFFFFFFF; |
| 38 | |
| 39 | enum ValueType { |
| 40 | kByte = 0x00, |
| 41 | kShort = 0x02, |
| 42 | kChar = 0x03, |
| 43 | kInt = 0x04, |
| 44 | kLong = 0x06, |
| 45 | kFloat = 0x10, |
| 46 | kDouble = 0x11, |
| 47 | kString = 0x17, |
| 48 | kType = 0x18, |
| 49 | kField = 0x19, |
| 50 | kMethod = 0x1a, |
| 51 | kEnum = 0x1b, |
| 52 | kArray = 0x1c, |
| 53 | kAnnotation = 0x1d, |
| 54 | kNull = 0x1e, |
| 55 | kBoolean = 0x1f |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 56 | }; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 57 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 58 | // Raw header_item. |
| 59 | struct Header { |
| 60 | uint8_t magic_[8]; |
| 61 | uint32_t checksum_; |
| 62 | uint8_t signature_[kSha1DigestSize]; |
| 63 | uint32_t file_size_; // length of entire file |
| 64 | uint32_t header_size_; // offset to start of next section |
| 65 | uint32_t endian_tag_; |
| 66 | uint32_t link_size_; |
| 67 | uint32_t link_off_; |
| 68 | uint32_t map_off_; |
| 69 | uint32_t string_ids_size_; |
| 70 | uint32_t string_ids_off_; |
| 71 | uint32_t type_ids_size_; |
| 72 | uint32_t type_ids_off_; |
| 73 | uint32_t proto_ids_size_; |
| 74 | uint32_t proto_ids_off_; |
| 75 | uint32_t field_ids_size_; |
| 76 | uint32_t field_ids_off_; |
| 77 | uint32_t method_ids_size_; |
| 78 | uint32_t method_ids_off_; |
| 79 | uint32_t class_defs_size_; |
| 80 | uint32_t class_defs_off_; |
| 81 | uint32_t data_size_; |
| 82 | uint32_t data_off_; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 83 | private: |
| 84 | DISALLOW_COPY_AND_ASSIGN(Header); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 85 | }; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 86 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 87 | // Raw string_id_item. |
| 88 | struct StringId { |
| 89 | uint32_t string_data_off_; // offset in bytes from the base address |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 90 | private: |
| 91 | DISALLOW_COPY_AND_ASSIGN(StringId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | // Raw type_id_item. |
| 95 | struct TypeId { |
| 96 | uint32_t descriptor_idx_; // index into string_ids |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 97 | private: |
| 98 | DISALLOW_COPY_AND_ASSIGN(TypeId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | // Raw field_id_item. |
| 102 | struct FieldId { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 103 | uint16_t class_idx_; // index into type_ids_ list for defining class |
| 104 | uint16_t type_idx_; // index into type_ids_ for field type |
| 105 | uint32_t name_idx_; // index into string_ids_ for field name |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 106 | private: |
| 107 | DISALLOW_COPY_AND_ASSIGN(FieldId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | // Raw method_id_item. |
| 111 | struct MethodId { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 112 | uint16_t class_idx_; // index into type_ids_ list for defining class |
| 113 | uint16_t proto_idx_; // index into proto_ids_ for method prototype |
| 114 | uint32_t name_idx_; // index into string_ids_ for method name |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 115 | private: |
| 116 | DISALLOW_COPY_AND_ASSIGN(MethodId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | // Raw proto_id_item. |
| 120 | struct ProtoId { |
| 121 | uint32_t shorty_idx_; // index into string_ids for shorty descriptor |
| 122 | uint32_t return_type_idx_; // index into type_ids list for return type |
| 123 | uint32_t parameters_off_; // file offset to type_list for parameter types |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 124 | private: |
| 125 | DISALLOW_COPY_AND_ASSIGN(ProtoId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | // Raw class_def_item. |
| 129 | struct ClassDef { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 130 | uint32_t class_idx_; // index into type_ids_ for this class |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 131 | uint32_t access_flags_; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 132 | uint32_t superclass_idx_; // index into type_ids_ for superclass |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 133 | uint32_t interfaces_off_; // file offset to TypeList |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 134 | 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] | 135 | uint32_t annotations_off_; // file offset to annotations_directory_item |
| 136 | uint32_t class_data_off_; // file offset to class_data_item |
| 137 | uint32_t static_values_off_; // file offset to EncodedArray |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 138 | private: |
| 139 | DISALLOW_COPY_AND_ASSIGN(ClassDef); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | // Raw type_item. |
| 143 | struct TypeItem { |
| 144 | uint16_t type_idx_; // index into type_ids section |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 145 | private: |
| 146 | DISALLOW_COPY_AND_ASSIGN(TypeItem); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | // Raw type_list. |
| 150 | class TypeList { |
| 151 | public: |
| 152 | uint32_t Size() const { |
| 153 | return size_; |
| 154 | } |
| 155 | |
| 156 | const TypeItem& GetTypeItem(uint32_t idx) const { |
| 157 | CHECK_LT(idx, this->size_); |
| 158 | return this->list_[idx]; |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | uint32_t size_; // size of the list, in entries |
| 163 | TypeItem list_[1]; // elements of the list |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 164 | DISALLOW_COPY_AND_ASSIGN(TypeList); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | class ParameterIterator { // TODO: stream |
| 168 | public: |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 169 | ParameterIterator(const DexFile& dex_file, const ProtoId& proto_id) |
| 170 | : dex_file_(dex_file), size_(0), pos_(0) { |
| 171 | type_list_ = dex_file_.GetProtoParameters(proto_id); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 172 | if (type_list_ != NULL) { |
| 173 | size_ = type_list_->Size(); |
| 174 | } |
| 175 | } |
| 176 | bool HasNext() const { return pos_ != size_; } |
| 177 | void Next() { ++pos_; } |
| 178 | const char* GetDescriptor() { |
| 179 | uint32_t type_idx = type_list_->GetTypeItem(pos_).type_idx_; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 180 | return dex_file_.dexStringByTypeIdx(type_idx); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 181 | } |
| 182 | private: |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 183 | const DexFile& dex_file_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 184 | const TypeList* type_list_; |
| 185 | uint32_t size_; |
| 186 | uint32_t pos_; |
| 187 | DISALLOW_IMPLICIT_CONSTRUCTORS(ParameterIterator); |
| 188 | }; |
| 189 | |
| 190 | ParameterIterator* GetParameterIterator(const ProtoId& proto_id) const { |
| 191 | return new ParameterIterator(*this, proto_id); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 194 | const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const { |
| 195 | return dexStringByTypeIdx(proto_id.return_type_idx_); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 198 | // Raw code_item. |
| 199 | struct CodeItem { |
| 200 | uint16_t registers_size_; |
| 201 | uint16_t ins_size_; |
| 202 | uint16_t outs_size_; |
| 203 | uint16_t tries_size_; |
| 204 | uint32_t debug_info_off_; // file offset to debug info stream |
| 205 | uint32_t insns_size_; // size of the insns array, in 2 byte code units |
| 206 | uint16_t insns_[1]; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 207 | private: |
| 208 | DISALLOW_COPY_AND_ASSIGN(CodeItem); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 211 | struct CatchHandlerItem { |
| 212 | uint32_t type_idx_; // type index of the caught exception type |
| 213 | uint32_t address_; // handler address |
| 214 | }; |
| 215 | |
Carl Shapiro | 2eaa968 | 2011-08-04 19:26:11 -0700 | [diff] [blame] | 216 | // Raw try_item. |
| 217 | struct TryItem { |
| 218 | uint32_t start_addr_; |
| 219 | uint16_t insn_count_; |
| 220 | uint16_t handler_off_; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 221 | private: |
| 222 | DISALLOW_COPY_AND_ASSIGN(TryItem); |
Carl Shapiro | 2eaa968 | 2011-08-04 19:26:11 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 225 | class CatchHandlerIterator { |
| 226 | public: |
| 227 | CatchHandlerIterator() { |
| 228 | remaining_count_ = -1; |
| 229 | catch_all_ = false; |
| 230 | } |
| 231 | |
| 232 | CatchHandlerIterator(const byte* handler_data) { |
| 233 | current_data_ = handler_data; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 234 | remaining_count_ = DecodeSignedLeb128(¤t_data_); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 235 | |
| 236 | // If remaining_count_ is non-positive, then it is the negative of |
| 237 | // the number of catch types, and the catches are followed by a |
| 238 | // catch-all handler. |
| 239 | if (remaining_count_ <= 0) { |
| 240 | catch_all_ = true; |
| 241 | remaining_count_ = -remaining_count_; |
| 242 | } else { |
| 243 | catch_all_ = false; |
| 244 | } |
| 245 | Next(); |
| 246 | } |
| 247 | |
Shih-wei Liao | fe909f2 | 2011-08-12 19:20:26 -0700 | [diff] [blame] | 248 | const CatchHandlerItem& Get() const { |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 249 | return handler_; |
| 250 | } |
| 251 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 252 | const byte* GetData() const { |
| 253 | return current_data_; |
| 254 | } |
| 255 | |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 256 | void Next() { |
| 257 | if (remaining_count_ > 0) { |
| 258 | handler_.type_idx_ = DecodeUnsignedLeb128(¤t_data_); |
| 259 | handler_.address_ = DecodeUnsignedLeb128(¤t_data_); |
| 260 | remaining_count_--; |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | if (catch_all_) { |
| 265 | handler_.type_idx_ = kDexNoIndex; |
| 266 | handler_.address_ = DecodeUnsignedLeb128(¤t_data_); |
| 267 | catch_all_ = false; |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | // no more handler |
| 272 | remaining_count_ = -1; |
| 273 | } |
| 274 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 275 | bool HasNext() const { |
Shih-wei Liao | 4e5c0b9 | 2011-08-11 22:50:08 -0700 | [diff] [blame] | 276 | return remaining_count_ == -1 && catch_all_ == false; |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | private: |
| 280 | CatchHandlerItem handler_; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 281 | const byte *current_data_; // the current handler in dex file. |
| 282 | int32_t remaining_count_; // number of handlers not read. |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 283 | bool catch_all_; // is there a handler that will catch all exceptions in case |
| 284 | // that all typed handler does not match. |
| 285 | }; |
| 286 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 287 | // Partially decoded form of class_data_item. |
| 288 | struct ClassDataHeader { |
| 289 | uint32_t static_fields_size_; // the number of static fields |
| 290 | uint32_t instance_fields_size_; // the number of instance fields |
| 291 | uint32_t direct_methods_size_; // the number of direct methods |
| 292 | uint32_t virtual_methods_size_; // the number of virtual methods |
| 293 | }; |
| 294 | |
| 295 | // Decoded form of encoded_field. |
| 296 | struct Field { |
| 297 | uint32_t field_idx_; // index into the field_ids list for the identity of this field |
| 298 | uint32_t access_flags_; // access flags for the field |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 299 | Field() {}; |
| 300 | private: |
| 301 | DISALLOW_COPY_AND_ASSIGN(Field); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | // Decoded form of encoded_method. |
| 305 | struct Method { |
| 306 | uint32_t method_idx_; |
| 307 | uint32_t access_flags_; |
| 308 | uint32_t code_off_; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 309 | Method() {}; |
| 310 | private: |
| 311 | DISALLOW_COPY_AND_ASSIGN(Method); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 314 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 315 | typedef std::vector<const DexFile*> ClassPath; |
| 316 | |
| 317 | // Search a collection of DexFiles for a descriptor |
| 318 | static ClassPathEntry FindInClassPath(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 319 | const ClassPath& class_path); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 320 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 321 | // Opens a collection of .dex files |
| 322 | static void OpenDexFiles(std::vector<const char*>& dex_filenames, |
| 323 | std::vector<const DexFile*>& dex_files, |
| 324 | const std::string& strip_location_prefix); |
| 325 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 326 | // Opens .dex file, guessing the format based on file extension |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 327 | static const DexFile* Open(const std::string& filename, |
| 328 | const std::string& strip_location_prefix); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 329 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 330 | // Opens a .dex file from the file system. |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 331 | static const DexFile* OpenFile(const std::string& filename, |
| 332 | const std::string& original_location, |
| 333 | const std::string& strip_location_prefix); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 334 | |
| 335 | // Opens a .jar, .zip, or .apk file from the file system. |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 336 | static const DexFile* OpenZip(const std::string& filename, |
| 337 | const std::string& strip_location_prefix); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 338 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 339 | // Opens a .dex file from a new allocated pointer. location is used |
| 340 | // to identify the source, for example "/system/framework/core.jar" |
| 341 | // or "contrived-test-42". When initializing a ClassLinker from an |
| 342 | // image, the location is used to match DexCaches the image to their |
| 343 | // corresponding DexFiles.N |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 344 | static const DexFile* OpenPtr(byte* ptr, size_t length, const std::string& location); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 345 | |
| 346 | // Closes a .dex file. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 347 | virtual ~DexFile(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 348 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 349 | const std::string& GetLocation() const { |
| 350 | return location_; |
| 351 | } |
| 352 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame^] | 353 | // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file. |
| 354 | // Used by managed code to implement annotations. |
| 355 | jobject GetDexObject(JNIEnv* env) const; |
| 356 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 357 | const Header& GetHeader() const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 358 | CHECK(header_ != NULL); |
| 359 | return *header_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 362 | // Looks up a class definition by its class descriptor. |
| 363 | const ClassDef* FindClassDef(const StringPiece& descriptor) const; |
| 364 | |
| 365 | // Returns the number of string identifiers in the .dex file. |
| 366 | size_t NumStringIds() const { |
| 367 | CHECK(header_ != NULL); |
| 368 | return header_->string_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 371 | // Returns the number of type identifiers in the .dex file. |
| 372 | size_t NumTypeIds() const { |
| 373 | CHECK(header_ != NULL); |
| 374 | return header_->type_ids_size_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 377 | // Returns the number of prototype identifiers in the .dex file. |
| 378 | size_t NumProtoIds() const { |
| 379 | CHECK(header_ != NULL); |
| 380 | return header_->proto_ids_size_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 383 | // Returns the number of field identifiers in the .dex file. |
| 384 | size_t NumFieldIds() const { |
| 385 | CHECK(header_ != NULL); |
| 386 | return header_->field_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 389 | // Returns the number of method identifiers in the .dex file. |
| 390 | size_t NumMethodIds() const { |
| 391 | CHECK(header_ != NULL); |
| 392 | return header_->method_ids_size_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 395 | // Returns the number of class definitions in the .dex file. |
| 396 | size_t NumClassDefs() const { |
| 397 | CHECK(header_ != NULL); |
| 398 | return header_->class_defs_size_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 401 | // Returns a pointer to the memory mapped class data. |
| 402 | // TODO: return a stream |
| 403 | const byte* GetClassData(const ClassDef& class_def) const { |
| 404 | if (class_def.class_data_off_ == 0) { |
| 405 | return NULL; |
| 406 | } else { |
| 407 | return base_ + class_def.class_data_off_; |
| 408 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 411 | // Decodes the header section from the class data bytes. |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 412 | ClassDataHeader ReadClassDataHeader(const byte** class_data) const { |
| 413 | CHECK(class_data != NULL); |
| 414 | ClassDataHeader header; |
| 415 | memset(&header, 0, sizeof(ClassDataHeader)); |
| 416 | if (*class_data != NULL) { |
| 417 | header.static_fields_size_ = DecodeUnsignedLeb128(class_data); |
| 418 | header.instance_fields_size_ = DecodeUnsignedLeb128(class_data); |
| 419 | header.direct_methods_size_ = DecodeUnsignedLeb128(class_data); |
| 420 | header.virtual_methods_size_ = DecodeUnsignedLeb128(class_data); |
| 421 | } |
| 422 | return header; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 425 | // Returns the class descriptor string of a class definition. |
| 426 | const char* GetClassDescriptor(const ClassDef& class_def) const { |
| 427 | return dexStringByTypeIdx(class_def.class_idx_); |
| 428 | } |
| 429 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 430 | // Returns the type descriptor string of a type id. |
| 431 | const char* GetTypeDescriptor(const TypeId& type_id) const { |
| 432 | return dexStringById(type_id.descriptor_idx_); |
| 433 | } |
| 434 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 435 | // Returns the class descriptor string of a field id. |
| 436 | const char* GetFieldClassDescriptor(const FieldId& field_id) const { |
| 437 | const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_); |
| 438 | return GetTypeDescriptor(type_id); |
| 439 | } |
| 440 | |
| 441 | // Returns the name of a field id. |
| 442 | const char* GetFieldName(const FieldId& field_id) const { |
| 443 | return dexStringById(field_id.name_idx_); |
| 444 | } |
| 445 | |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 446 | // Returns the class descriptor string of a method id. |
| 447 | const char* GetMethodClassDescriptor(const MethodId& method_id) const { |
| 448 | const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_); |
| 449 | return GetTypeDescriptor(type_id); |
| 450 | } |
| 451 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 452 | // Returns the prototype of a method id. |
| 453 | const char* GetMethodPrototype(const MethodId& method_id) const { |
| 454 | return dexStringById(method_id.proto_idx_); |
| 455 | } |
| 456 | |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 457 | // Returns the name of a method id. |
| 458 | const char* GetMethodName(const MethodId& method_id) const { |
| 459 | return dexStringById(method_id.name_idx_); |
| 460 | } |
| 461 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 462 | // Returns the StringId at the specified index. |
| 463 | const StringId& GetStringId(uint32_t idx) const { |
| 464 | CHECK_LT(idx, NumStringIds()); |
| 465 | return string_ids_[idx]; |
| 466 | } |
| 467 | |
| 468 | // Returns the TypeId at the specified index. |
| 469 | const TypeId& GetTypeId(uint32_t idx) const { |
| 470 | CHECK_LT(idx, NumTypeIds()); |
| 471 | return type_ids_[idx]; |
| 472 | } |
| 473 | |
| 474 | // Returns the FieldId at the specified index. |
| 475 | const FieldId& GetFieldId(uint32_t idx) const { |
| 476 | CHECK_LT(idx, NumFieldIds()); |
| 477 | return field_ids_[idx]; |
| 478 | } |
| 479 | |
| 480 | // Returns the MethodId at the specified index. |
| 481 | const MethodId& GetMethodId(uint32_t idx) const { |
| 482 | CHECK_LT(idx, NumMethodIds()); |
| 483 | return method_ids_[idx]; |
| 484 | } |
| 485 | |
| 486 | // Returns the ProtoId at the specified index. |
| 487 | const ProtoId& GetProtoId(uint32_t idx) const { |
| 488 | CHECK_LT(idx, NumProtoIds()); |
| 489 | return proto_ids_[idx]; |
| 490 | } |
| 491 | |
| 492 | // Returns the ClassDef at the specified index. |
| 493 | const ClassDef& GetClassDef(uint32_t idx) const { |
| 494 | CHECK_LT(idx, NumClassDefs()); |
| 495 | return class_defs_[idx]; |
| 496 | } |
| 497 | |
| 498 | const TypeList* GetInterfacesList(const ClassDef& class_def) const { |
| 499 | if (class_def.interfaces_off_ == 0) { |
| 500 | return NULL; |
| 501 | } else { |
| 502 | const byte* addr = base_ + class_def.interfaces_off_; |
| 503 | return reinterpret_cast<const TypeList*>(addr); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | const CodeItem* GetCodeItem(const Method& method) const { |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 508 | return GetCodeItem(method.code_off_); |
| 509 | } |
| 510 | |
| 511 | const CodeItem* GetCodeItem(const uint32_t code_off_) const { |
| 512 | if (code_off_ == 0) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 513 | return NULL; // native or abstract method |
| 514 | } else { |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 515 | const byte* addr = base_ + code_off_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 516 | return reinterpret_cast<const CodeItem*>(addr); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // Returns the short form method descriptor for the given prototype. |
| 521 | const char* GetShorty(uint32_t proto_idx) const { |
| 522 | const ProtoId& proto_id = GetProtoId(proto_idx); |
| 523 | return dexStringById(proto_id.shorty_idx_); |
| 524 | } |
| 525 | |
| 526 | const TypeList* GetProtoParameters(const ProtoId& proto_id) const { |
| 527 | if (proto_id.parameters_off_ == 0) { |
| 528 | return NULL; |
| 529 | } else { |
| 530 | const byte* addr = base_ + proto_id.parameters_off_; |
| 531 | return reinterpret_cast<const TypeList*>(addr); |
| 532 | } |
| 533 | } |
| 534 | |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 535 | std::string CreateMethodDescriptor(uint32_t proto_idx, int32_t* unicode_length) const; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 536 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 537 | const byte* GetEncodedArray(const ClassDef& class_def) const { |
| 538 | if (class_def.static_values_off_ == 0) { |
| 539 | return 0; |
| 540 | } else { |
| 541 | return base_ + class_def.static_values_off_; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | int32_t GetStringLength(const StringId& string_id) const { |
| 546 | const byte* ptr = base_ + string_id.string_data_off_; |
| 547 | return DecodeUnsignedLeb128(&ptr); |
| 548 | } |
| 549 | |
| 550 | ValueType ReadEncodedValue(const byte** encoded_value, JValue* value) const; |
| 551 | |
| 552 | // From libdex... |
| 553 | |
| 554 | // Returns a pointer to the UTF-8 string data referred to by the |
| 555 | // given string_id. |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 556 | const char* GetStringData(const StringId& string_id, int32_t* length) const { |
| 557 | CHECK(length != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 558 | const byte* ptr = base_ + string_id.string_data_off_; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 559 | *length = DecodeUnsignedLeb128(&ptr); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 560 | return reinterpret_cast<const char*>(ptr); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 563 | const char* GetStringData(const StringId& string_id) const { |
| 564 | int32_t length; |
| 565 | return GetStringData(string_id, &length); |
| 566 | } |
| 567 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 568 | // return the UTF-8 encoded string with the specified string_id index |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 569 | const char* dexStringById(uint32_t idx, int32_t* unicode_length) const { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 570 | if (idx == kDexNoIndex) { |
| 571 | *unicode_length = 0; |
| 572 | return NULL; |
| 573 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 574 | const StringId& string_id = GetStringId(idx); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 575 | return GetStringData(string_id, unicode_length); |
| 576 | } |
| 577 | |
| 578 | const char* dexStringById(uint32_t idx) const { |
| 579 | int32_t unicode_length; |
| 580 | return dexStringById(idx, &unicode_length); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 583 | String* dexArtStringById(int32_t idx) const; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 584 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 585 | // Get the descriptor string associated with a given type index. |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 586 | const char* dexStringByTypeIdx(uint32_t idx, int32_t* unicode_length) const { |
| 587 | const TypeId& type_id = GetTypeId(idx); |
| 588 | return dexStringById(type_id.descriptor_idx_, unicode_length); |
| 589 | } |
| 590 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 591 | const char* dexStringByTypeIdx(uint32_t idx) const { |
| 592 | const TypeId& type_id = GetTypeId(idx); |
| 593 | return dexStringById(type_id.descriptor_idx_); |
| 594 | } |
| 595 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 596 | String* dexArtStringByTypeIdx(int32_t idx) const { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 597 | const TypeId& type_id = GetTypeId(idx); |
| 598 | return dexArtStringById(type_id.descriptor_idx_); |
| 599 | } |
| 600 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 601 | // TODO: encoded_field is actually a stream of bytes |
| 602 | void dexReadClassDataField(const byte** encoded_field, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 603 | DexFile::Field* field, |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 604 | uint32_t* last_idx) const { |
| 605 | uint32_t idx = *last_idx + DecodeUnsignedLeb128(encoded_field); |
| 606 | field->access_flags_ = DecodeUnsignedLeb128(encoded_field); |
| 607 | field->field_idx_ = idx; |
| 608 | *last_idx = idx; |
| 609 | } |
| 610 | |
| 611 | // TODO: encoded_method is actually a stream of bytes |
| 612 | void dexReadClassDataMethod(const byte** encoded_method, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 613 | DexFile::Method* method, |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 614 | uint32_t* last_idx) const { |
| 615 | uint32_t idx = *last_idx + DecodeUnsignedLeb128(encoded_method); |
| 616 | method->access_flags_ = DecodeUnsignedLeb128(encoded_method); |
| 617 | method->code_off_ = DecodeUnsignedLeb128(encoded_method); |
| 618 | method->method_idx_ = idx; |
| 619 | *last_idx = idx; |
| 620 | } |
| 621 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 622 | static const TryItem* dexGetTryItems(const CodeItem& code_item, uint32_t offset) { |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 623 | const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_]; |
| 624 | return reinterpret_cast<const TryItem*> |
| 625 | (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset; |
| 626 | } |
| 627 | |
| 628 | // Get the base of the encoded data for the given DexCode. |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 629 | static const byte* dexGetCatchHandlerData(const CodeItem& code_item, uint32_t offset) { |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 630 | const byte* handler_data = reinterpret_cast<const byte*> |
| 631 | (dexGetTryItems(code_item, code_item.tries_size_)); |
| 632 | return handler_data + offset; |
| 633 | } |
| 634 | |
| 635 | // Find the handler associated with a given address, if any. |
| 636 | // Initializes the given iterator and returns true if a match is |
| 637 | // found. Returns end if there is no applicable handler. |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 638 | static CatchHandlerIterator dexFindCatchHandler(const CodeItem& code_item, uint32_t address) { |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 639 | CatchHandlerItem handler; |
| 640 | handler.address_ = -1; |
| 641 | int32_t offset = -1; |
| 642 | |
| 643 | // Short-circuit the overwhelmingly common cases. |
| 644 | switch (code_item.tries_size_) { |
| 645 | case 0: |
| 646 | break; |
| 647 | case 1: { |
| 648 | const TryItem* tries = dexGetTryItems(code_item, 0); |
| 649 | uint32_t start = tries->start_addr_; |
| 650 | if (address < start) |
| 651 | break; |
| 652 | |
| 653 | uint32_t end = start + tries->insn_count_; |
| 654 | if (address >= end) |
| 655 | break; |
| 656 | |
| 657 | offset = tries->handler_off_; |
| 658 | break; |
| 659 | } |
| 660 | default: |
| 661 | offset = dexFindCatchHandlerOffset0(code_item, code_item.tries_size_, address); |
| 662 | } |
| 663 | |
| 664 | if (offset >= 0) { |
| 665 | const byte* handler_data = dexGetCatchHandlerData(code_item, offset); |
| 666 | return CatchHandlerIterator(handler_data); |
| 667 | } |
| 668 | return CatchHandlerIterator(); |
| 669 | } |
| 670 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 671 | static int32_t dexFindCatchHandlerOffset0(const CodeItem &code_item, |
| 672 | int32_t tries_size, |
| 673 | uint32_t address) { |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 674 | // Note: Signed type is important for max and min. |
| 675 | int32_t min = 0; |
| 676 | int32_t max = tries_size - 1; |
| 677 | |
| 678 | while (max >= min) { |
| 679 | int32_t guess = (min + max) >> 1; |
| 680 | const TryItem* pTry = dexGetTryItems(code_item, guess); |
| 681 | uint32_t start = pTry->start_addr_; |
| 682 | |
| 683 | if (address < start) { |
| 684 | max = guess - 1; |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | uint32_t end = start + pTry->insn_count_; |
| 689 | if (address >= end) { |
| 690 | min = guess + 1; |
| 691 | continue; |
| 692 | } |
| 693 | |
| 694 | // We have a winner! |
| 695 | return (int32_t) pTry->handler_off_; |
| 696 | } |
| 697 | |
| 698 | // No match. |
| 699 | return -1; |
| 700 | } |
| 701 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 702 | // Get the pointer to the start of the debugging data |
| 703 | const byte* dexGetDebugInfoStream(const CodeItem* code_item) const { |
| 704 | if (code_item->debug_info_off_ == 0) { |
| 705 | return NULL; |
| 706 | } else { |
| 707 | return base_ + code_item->debug_info_off_; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | // Callback for "new position table entry". |
| 712 | // Returning true causes the decoder to stop early. |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 713 | typedef bool (*DexDebugNewPositionCb)(void* cnxt, uint32_t address, uint32_t line_num); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 714 | |
| 715 | // Callback for "new locals table entry". "signature" is an empty string |
| 716 | // if no signature is available for an entry. |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 717 | typedef void (*DexDebugNewLocalCb)(void* cnxt, uint16_t reg, |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 718 | uint32_t startAddress, |
| 719 | uint32_t endAddress, |
| 720 | const String* name, |
| 721 | const String* descriptor, |
| 722 | const String* signature); |
| 723 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 724 | static bool LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num) { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 725 | LineNumFromPcContext *context = (LineNumFromPcContext *)cnxt; |
| 726 | |
| 727 | // We know that this callback will be called in |
| 728 | // ascending address order, so keep going until we find |
| 729 | // a match or we've just gone past it. |
| 730 | if (address > context->address_) { |
| 731 | // The line number from the previous positions callback |
| 732 | // wil be the final result. |
| 733 | return true; |
| 734 | } else { |
| 735 | context->line_num_ = line_num; |
| 736 | return address == context->address_; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | |
| 741 | // Debug info opcodes and constants |
| 742 | enum { |
| 743 | DBG_END_SEQUENCE = 0x00, |
| 744 | DBG_ADVANCE_PC = 0x01, |
| 745 | DBG_ADVANCE_LINE = 0x02, |
| 746 | DBG_START_LOCAL = 0x03, |
| 747 | DBG_START_LOCAL_EXTENDED = 0x04, |
| 748 | DBG_END_LOCAL = 0x05, |
| 749 | DBG_RESTART_LOCAL = 0x06, |
| 750 | DBG_SET_PROLOGUE_END = 0x07, |
| 751 | DBG_SET_EPILOGUE_BEGIN = 0x08, |
| 752 | DBG_SET_FILE = 0x09, |
| 753 | DBG_FIRST_SPECIAL = 0x0a, |
| 754 | DBG_LINE_BASE = -4, |
| 755 | DBG_LINE_RANGE = 15, |
| 756 | }; |
| 757 | |
| 758 | struct LocalInfo { |
| 759 | LocalInfo() : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0), is_live_(false) {} |
| 760 | |
| 761 | // E.g., list |
| 762 | const String* name_; |
| 763 | |
| 764 | // E.g., Ljava/util/LinkedList; |
| 765 | const String* descriptor_; |
| 766 | |
| 767 | // E.g., java.util.LinkedList<java.lang.Integer> |
| 768 | const String* signature_; |
| 769 | |
| 770 | // PC location where the local is first defined. |
| 771 | uint16_t start_address_; |
| 772 | |
| 773 | // Is the local defined and live. |
| 774 | bool is_live_; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 775 | |
| 776 | private: |
| 777 | DISALLOW_COPY_AND_ASSIGN(LocalInfo); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 778 | }; |
| 779 | |
| 780 | struct LineNumFromPcContext { |
| 781 | LineNumFromPcContext(uint32_t address, uint32_t line_num) : |
| 782 | address_(address), line_num_(line_num) {} |
| 783 | uint32_t address_; |
| 784 | uint32_t line_num_; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 785 | private: |
| 786 | DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 787 | }; |
| 788 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 789 | void InvokeLocalCbIfLive(void* cnxt, int reg, uint32_t end_address, |
| 790 | LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 791 | if (local_cb != NULL && local_in_reg[reg].is_live_) { |
| 792 | local_cb(cnxt, reg, local_in_reg[reg].start_address_, end_address, |
| 793 | local_in_reg[reg].name_, local_in_reg[reg].descriptor_, |
| 794 | local_in_reg[reg].signature_); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // Determine the source file line number based on the program counter. |
| 799 | // "pc" is an offset, in 16-bit units, from the start of the method's code. |
| 800 | // |
| 801 | // Returns -1 if no match was found (possibly because the source files were |
| 802 | // compiled without "-g", so no line number information is present). |
| 803 | // Returns -2 for native methods (as expected in exception traces). |
| 804 | // |
| 805 | // This is used by runtime; therefore use art::Method not art::DexFile::Method. |
| 806 | int32_t GetLineNumFromPC(const art::Method* method, uint32_t rel_pc) const; |
| 807 | |
| 808 | void dexDecodeDebugInfo0(const CodeItem* code_item, const art::Method* method, |
| 809 | DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb, |
| 810 | void* cnxt, const byte* stream, LocalInfo* local_in_reg) const; |
| 811 | |
| 812 | void dexDecodeDebugInfo(const CodeItem* code_item, const art::Method *method, |
| 813 | DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb, |
| 814 | void* cnxt) const { |
| 815 | const byte* stream = dexGetDebugInfoStream(code_item); |
| 816 | LocalInfo local_in_reg[code_item->registers_size_]; |
| 817 | |
| 818 | if (stream != NULL) { |
| 819 | dexDecodeDebugInfo0(code_item, method, posCb, local_cb, cnxt, stream, local_in_reg); |
| 820 | } |
| 821 | for (int reg = 0; reg < code_item->registers_size_; reg++) { |
| 822 | InvokeLocalCbIfLive(cnxt, reg, code_item->insns_size_, local_in_reg, local_cb); |
| 823 | } |
| 824 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 825 | |
| 826 | // TODO: const reference |
| 827 | uint32_t dexGetIndexForClassDef(const ClassDef* class_def) const { |
| 828 | CHECK_GE(class_def, class_defs_); |
| 829 | CHECK_LT(class_def, class_defs_ + header_->class_defs_size_); |
| 830 | return class_def - class_defs_; |
| 831 | } |
| 832 | |
| 833 | const char* dexGetSourceFile(const ClassDef& class_def) const { |
| 834 | if (class_def.source_file_idx_ == 0xffffffff) { |
| 835 | return NULL; |
| 836 | } else { |
| 837 | return dexStringById(class_def.source_file_idx_); |
| 838 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 839 | } |
| 840 | |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 841 | void ChangePermissions(int prot) const; |
| 842 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 843 | private: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 844 | // Helper class to deallocate underlying storage. |
| 845 | class Closer { |
| 846 | public: |
| 847 | virtual ~Closer(); |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 848 | virtual void ChangePermissions(int prot) = 0; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 849 | }; |
| 850 | |
| 851 | // Helper class to deallocate mmap-backed .dex files. |
| 852 | class MmapCloser : public Closer { |
| 853 | public: |
| 854 | MmapCloser(void* addr, size_t length); |
| 855 | virtual ~MmapCloser(); |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 856 | virtual void ChangePermissions(int prot); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 857 | private: |
| 858 | void* addr_; |
| 859 | size_t length_; |
| 860 | }; |
| 861 | |
| 862 | // Helper class for deallocating new/delete-backed .dex files. |
| 863 | class PtrCloser : public Closer { |
| 864 | public: |
| 865 | PtrCloser(byte* addr); |
| 866 | virtual ~PtrCloser(); |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 867 | virtual void ChangePermissions(int prot); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 868 | private: |
| 869 | byte* addr_; |
| 870 | }; |
| 871 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 872 | // Opens a .dex file at the given address. |
| 873 | static const DexFile* Open(const byte* dex_file, |
| 874 | size_t length, |
| 875 | const std::string& location, |
| 876 | Closer* closer); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 877 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 878 | DexFile(const byte* addr, size_t length, const std::string& location, Closer* closer) |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 879 | : base_(addr), |
| 880 | length_(length), |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 881 | location_(location), |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 882 | closer_(closer), |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame^] | 883 | dex_object_lock_("a dex_object_lock_"), |
| 884 | dex_object_(NULL), |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 885 | header_(0), |
| 886 | string_ids_(0), |
| 887 | type_ids_(0), |
| 888 | field_ids_(0), |
| 889 | method_ids_(0), |
| 890 | proto_ids_(0), |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 891 | class_defs_(0) { |
| 892 | CHECK(addr != NULL); |
| 893 | CHECK_GT(length, 0U); |
| 894 | CHECK(closer != NULL); |
| 895 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 896 | |
| 897 | // Top-level initializer that calls other Init methods. |
| 898 | bool Init(); |
| 899 | |
| 900 | // Caches pointers into to the various file sections. |
| 901 | void InitMembers(); |
| 902 | |
| 903 | // Builds the index of descriptors to class definitions. |
| 904 | void InitIndex(); |
| 905 | |
| 906 | // Returns true if the byte string equals the magic value. |
| 907 | bool CheckMagic(const byte* magic); |
| 908 | |
| 909 | // Returns true if the header magic is of the expected value. |
| 910 | bool IsMagicValid(); |
| 911 | |
| 912 | // The index of descriptors to class definitions. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 913 | typedef std::map<const StringPiece, const DexFile::ClassDef*> Index; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 914 | Index index_; |
| 915 | |
| 916 | // The base address of the memory mapping. |
| 917 | const byte* base_; |
| 918 | |
| 919 | // The size of the underlying memory allocation in bytes. |
| 920 | size_t length_; |
| 921 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 922 | // Typically the dex file name when available, alternatively some identifying string. |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 923 | // |
| 924 | // The ClassLinker will use this to match DexFiles the boot class |
| 925 | // path to DexCache::GetLocation when loading from an image. |
| 926 | const std::string location_; |
| 927 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 928 | // Helper object to free the underlying allocation. |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 929 | UniquePtr<Closer> closer_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 930 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame^] | 931 | // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject. |
| 932 | mutable Mutex dex_object_lock_; |
| 933 | mutable jobject dex_object_; |
| 934 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 935 | // Points to the header section. |
| 936 | const Header* header_; |
| 937 | |
| 938 | // Points to the base of the string identifier list. |
| 939 | const StringId* string_ids_; |
| 940 | |
| 941 | // Points to the base of the type identifier list. |
| 942 | const TypeId* type_ids_; |
| 943 | |
| 944 | // Points to the base of the field identifier list. |
| 945 | const FieldId* field_ids_; |
| 946 | |
| 947 | // Points to the base of the method identifier list. |
| 948 | const MethodId* method_ids_; |
| 949 | |
| 950 | // Points to the base of the prototype identifier list. |
| 951 | const ProtoId* proto_ids_; |
| 952 | |
| 953 | // Points to the base of the class definition list. |
| 954 | const ClassDef* class_defs_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 955 | }; |
| 956 | |
| 957 | } // namespace art |
| 958 | |
| 959 | #endif // ART_SRC_DEX_FILE_H_ |