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