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