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> |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 7 | #include <vector> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 8 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 9 | #include "globals.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 10 | #include "leb128.h" |
| 11 | #include "logging.h" |
| 12 | #include "scoped_ptr.h" |
| 13 | #include "stringpiece.h" |
| 14 | #include "strutil.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 15 | |
| 16 | namespace art { |
| 17 | |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 18 | union JValue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 19 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 20 | // TODO: move all of the macro functionality into the DexCache class. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 21 | class DexFile { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 22 | public: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 23 | static const byte kDexMagic[]; |
| 24 | static const byte kDexMagicVersion[]; |
| 25 | static const size_t kSha1DigestSize = 20; |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 26 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 27 | static const byte kEncodedValueTypeMask = 0x1f; // 0b11111 |
| 28 | static const byte kEncodedValueArgShift = 5; |
| 29 | |
| 30 | // The value of an invalid index. |
| 31 | static const uint32_t kDexNoIndex = 0xFFFFFFFF; |
| 32 | |
| 33 | enum ValueType { |
| 34 | kByte = 0x00, |
| 35 | kShort = 0x02, |
| 36 | kChar = 0x03, |
| 37 | kInt = 0x04, |
| 38 | kLong = 0x06, |
| 39 | kFloat = 0x10, |
| 40 | kDouble = 0x11, |
| 41 | kString = 0x17, |
| 42 | kType = 0x18, |
| 43 | kField = 0x19, |
| 44 | kMethod = 0x1a, |
| 45 | kEnum = 0x1b, |
| 46 | kArray = 0x1c, |
| 47 | kAnnotation = 0x1d, |
| 48 | kNull = 0x1e, |
| 49 | kBoolean = 0x1f |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 50 | }; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 51 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 52 | // Raw header_item. |
| 53 | struct Header { |
| 54 | uint8_t magic_[8]; |
| 55 | uint32_t checksum_; |
| 56 | uint8_t signature_[kSha1DigestSize]; |
| 57 | uint32_t file_size_; // length of entire file |
| 58 | uint32_t header_size_; // offset to start of next section |
| 59 | uint32_t endian_tag_; |
| 60 | uint32_t link_size_; |
| 61 | uint32_t link_off_; |
| 62 | uint32_t map_off_; |
| 63 | uint32_t string_ids_size_; |
| 64 | uint32_t string_ids_off_; |
| 65 | uint32_t type_ids_size_; |
| 66 | uint32_t type_ids_off_; |
| 67 | uint32_t proto_ids_size_; |
| 68 | uint32_t proto_ids_off_; |
| 69 | uint32_t field_ids_size_; |
| 70 | uint32_t field_ids_off_; |
| 71 | uint32_t method_ids_size_; |
| 72 | uint32_t method_ids_off_; |
| 73 | uint32_t class_defs_size_; |
| 74 | uint32_t class_defs_off_; |
| 75 | uint32_t data_size_; |
| 76 | uint32_t data_off_; |
| 77 | }; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 78 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 79 | // Raw string_id_item. |
| 80 | struct StringId { |
| 81 | uint32_t string_data_off_; // offset in bytes from the base address |
| 82 | }; |
| 83 | |
| 84 | // Raw type_id_item. |
| 85 | struct TypeId { |
| 86 | uint32_t descriptor_idx_; // index into string_ids |
| 87 | }; |
| 88 | |
| 89 | // Raw field_id_item. |
| 90 | struct FieldId { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 91 | uint16_t class_idx_; // index into type_ids_ list for defining class |
| 92 | uint16_t type_idx_; // index into type_ids_ for field type |
| 93 | uint32_t name_idx_; // index into string_ids_ for field name |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | // Raw method_id_item. |
| 97 | struct MethodId { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 98 | uint16_t class_idx_; // index into type_ids_ list for defining class |
| 99 | uint16_t proto_idx_; // index into proto_ids_ for method prototype |
| 100 | uint32_t name_idx_; // index into string_ids_ for method name |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | // Raw proto_id_item. |
| 104 | struct ProtoId { |
| 105 | uint32_t shorty_idx_; // index into string_ids for shorty descriptor |
| 106 | uint32_t return_type_idx_; // index into type_ids list for return type |
| 107 | uint32_t parameters_off_; // file offset to type_list for parameter types |
| 108 | }; |
| 109 | |
| 110 | // Raw class_def_item. |
| 111 | struct ClassDef { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 112 | uint32_t class_idx_; // index into type_ids_ for this class |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 113 | uint32_t access_flags_; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 114 | uint32_t superclass_idx_; // index into type_ids_ for superclass |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 115 | uint32_t interfaces_off_; // file offset to TypeList |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 116 | 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] | 117 | uint32_t annotations_off_; // file offset to annotations_directory_item |
| 118 | uint32_t class_data_off_; // file offset to class_data_item |
| 119 | uint32_t static_values_off_; // file offset to EncodedArray |
| 120 | }; |
| 121 | |
| 122 | // Raw type_item. |
| 123 | struct TypeItem { |
| 124 | uint16_t type_idx_; // index into type_ids section |
| 125 | }; |
| 126 | |
| 127 | // Raw type_list. |
| 128 | class TypeList { |
| 129 | public: |
| 130 | uint32_t Size() const { |
| 131 | return size_; |
| 132 | } |
| 133 | |
| 134 | const TypeItem& GetTypeItem(uint32_t idx) const { |
| 135 | CHECK_LT(idx, this->size_); |
| 136 | return this->list_[idx]; |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | uint32_t size_; // size of the list, in entries |
| 141 | TypeItem list_[1]; // elements of the list |
| 142 | }; |
| 143 | |
| 144 | class ParameterIterator { // TODO: stream |
| 145 | public: |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 146 | ParameterIterator(const DexFile& dex_file, const ProtoId& proto_id) |
| 147 | : dex_file_(dex_file), size_(0), pos_(0) { |
| 148 | type_list_ = dex_file_.GetProtoParameters(proto_id); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 149 | if (type_list_ != NULL) { |
| 150 | size_ = type_list_->Size(); |
| 151 | } |
| 152 | } |
| 153 | bool HasNext() const { return pos_ != size_; } |
| 154 | void Next() { ++pos_; } |
| 155 | const char* GetDescriptor() { |
| 156 | uint32_t type_idx = type_list_->GetTypeItem(pos_).type_idx_; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 157 | return dex_file_.dexStringByTypeIdx(type_idx); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 158 | } |
| 159 | private: |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 160 | const DexFile& dex_file_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 161 | const TypeList* type_list_; |
| 162 | uint32_t size_; |
| 163 | uint32_t pos_; |
| 164 | DISALLOW_IMPLICIT_CONSTRUCTORS(ParameterIterator); |
| 165 | }; |
| 166 | |
| 167 | ParameterIterator* GetParameterIterator(const ProtoId& proto_id) const { |
| 168 | return new ParameterIterator(*this, proto_id); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 171 | const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const { |
| 172 | return dexStringByTypeIdx(proto_id.return_type_idx_); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 175 | // Raw code_item. |
| 176 | struct CodeItem { |
| 177 | uint16_t registers_size_; |
| 178 | uint16_t ins_size_; |
| 179 | uint16_t outs_size_; |
| 180 | uint16_t tries_size_; |
| 181 | uint32_t debug_info_off_; // file offset to debug info stream |
| 182 | uint32_t insns_size_; // size of the insns array, in 2 byte code units |
| 183 | uint16_t insns_[1]; |
| 184 | }; |
| 185 | |
Carl Shapiro | 2eaa968 | 2011-08-04 19:26:11 -0700 | [diff] [blame^] | 186 | // Raw try_item. |
| 187 | struct TryItem { |
| 188 | uint32_t start_addr_; |
| 189 | uint16_t insn_count_; |
| 190 | uint16_t handler_off_; |
| 191 | }; |
| 192 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 193 | // Partially decoded form of class_data_item. |
| 194 | struct ClassDataHeader { |
| 195 | uint32_t static_fields_size_; // the number of static fields |
| 196 | uint32_t instance_fields_size_; // the number of instance fields |
| 197 | uint32_t direct_methods_size_; // the number of direct methods |
| 198 | uint32_t virtual_methods_size_; // the number of virtual methods |
| 199 | }; |
| 200 | |
| 201 | // Decoded form of encoded_field. |
| 202 | struct Field { |
| 203 | uint32_t field_idx_; // index into the field_ids list for the identity of this field |
| 204 | uint32_t access_flags_; // access flags for the field |
| 205 | }; |
| 206 | |
| 207 | // Decoded form of encoded_method. |
| 208 | struct Method { |
| 209 | uint32_t method_idx_; |
| 210 | uint32_t access_flags_; |
| 211 | uint32_t code_off_; |
| 212 | }; |
| 213 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 214 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 215 | typedef std::vector<const DexFile*> ClassPath; |
| 216 | |
| 217 | // Search a collection of DexFiles for a descriptor |
| 218 | static ClassPathEntry FindInClassPath(const StringPiece& descriptor, |
| 219 | ClassPath& class_path); |
| 220 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 221 | // Opens a .dex file from the file system. |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 222 | static DexFile* OpenFile(const std::string& filename); |
| 223 | |
| 224 | // Opens a .jar, .zip, or .apk file from the file system. |
| 225 | static DexFile* OpenZip(const std::string& filename); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 226 | |
| 227 | // Opens a .dex file from a new allocated pointer |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 228 | static DexFile* OpenPtr(byte* ptr, size_t length); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 229 | |
| 230 | // Closes a .dex file. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 231 | virtual ~DexFile(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 232 | |
| 233 | const Header& GetHeader() { |
| 234 | CHECK(header_ != NULL); |
| 235 | return *header_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 238 | // Looks up a class definition by its class descriptor. |
| 239 | const ClassDef* FindClassDef(const StringPiece& descriptor) const; |
| 240 | |
| 241 | // Returns the number of string identifiers in the .dex file. |
| 242 | size_t NumStringIds() const { |
| 243 | CHECK(header_ != NULL); |
| 244 | return header_->string_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 247 | // Returns the number of type identifiers in the .dex file. |
| 248 | size_t NumTypeIds() const { |
| 249 | CHECK(header_ != NULL); |
| 250 | return header_->type_ids_size_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 253 | // Returns the number of prototype identifiers in the .dex file. |
| 254 | size_t NumProtoIds() const { |
| 255 | CHECK(header_ != NULL); |
| 256 | return header_->proto_ids_size_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 259 | // Returns the number of field identifiers in the .dex file. |
| 260 | size_t NumFieldIds() const { |
| 261 | CHECK(header_ != NULL); |
| 262 | return header_->field_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 265 | // Returns the number of method identifiers in the .dex file. |
| 266 | size_t NumMethodIds() const { |
| 267 | CHECK(header_ != NULL); |
| 268 | return header_->method_ids_size_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 271 | // Returns the number of class definitions in the .dex file. |
| 272 | size_t NumClassDefs() const { |
| 273 | CHECK(header_ != NULL); |
| 274 | return header_->class_defs_size_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 277 | // Returns a pointer to the memory mapped class data. |
| 278 | // TODO: return a stream |
| 279 | const byte* GetClassData(const ClassDef& class_def) const { |
| 280 | if (class_def.class_data_off_ == 0) { |
| 281 | return NULL; |
| 282 | } else { |
| 283 | return base_ + class_def.class_data_off_; |
| 284 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 287 | // Decodes the header section from the class data bytes. |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 288 | ClassDataHeader ReadClassDataHeader(const byte** class_data) const { |
| 289 | CHECK(class_data != NULL); |
| 290 | ClassDataHeader header; |
| 291 | memset(&header, 0, sizeof(ClassDataHeader)); |
| 292 | if (*class_data != NULL) { |
| 293 | header.static_fields_size_ = DecodeUnsignedLeb128(class_data); |
| 294 | header.instance_fields_size_ = DecodeUnsignedLeb128(class_data); |
| 295 | header.direct_methods_size_ = DecodeUnsignedLeb128(class_data); |
| 296 | header.virtual_methods_size_ = DecodeUnsignedLeb128(class_data); |
| 297 | } |
| 298 | return header; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 301 | // Returns the class descriptor string of a class definition. |
| 302 | const char* GetClassDescriptor(const ClassDef& class_def) const { |
| 303 | return dexStringByTypeIdx(class_def.class_idx_); |
| 304 | } |
| 305 | |
| 306 | // Returns the StringId at the specified index. |
| 307 | const StringId& GetStringId(uint32_t idx) const { |
| 308 | CHECK_LT(idx, NumStringIds()); |
| 309 | return string_ids_[idx]; |
| 310 | } |
| 311 | |
| 312 | // Returns the TypeId at the specified index. |
| 313 | const TypeId& GetTypeId(uint32_t idx) const { |
| 314 | CHECK_LT(idx, NumTypeIds()); |
| 315 | return type_ids_[idx]; |
| 316 | } |
| 317 | |
| 318 | // Returns the FieldId at the specified index. |
| 319 | const FieldId& GetFieldId(uint32_t idx) const { |
| 320 | CHECK_LT(idx, NumFieldIds()); |
| 321 | return field_ids_[idx]; |
| 322 | } |
| 323 | |
| 324 | // Returns the MethodId at the specified index. |
| 325 | const MethodId& GetMethodId(uint32_t idx) const { |
| 326 | CHECK_LT(idx, NumMethodIds()); |
| 327 | return method_ids_[idx]; |
| 328 | } |
| 329 | |
| 330 | // Returns the ProtoId at the specified index. |
| 331 | const ProtoId& GetProtoId(uint32_t idx) const { |
| 332 | CHECK_LT(idx, NumProtoIds()); |
| 333 | return proto_ids_[idx]; |
| 334 | } |
| 335 | |
| 336 | // Returns the ClassDef at the specified index. |
| 337 | const ClassDef& GetClassDef(uint32_t idx) const { |
| 338 | CHECK_LT(idx, NumClassDefs()); |
| 339 | return class_defs_[idx]; |
| 340 | } |
| 341 | |
| 342 | const TypeList* GetInterfacesList(const ClassDef& class_def) const { |
| 343 | if (class_def.interfaces_off_ == 0) { |
| 344 | return NULL; |
| 345 | } else { |
| 346 | const byte* addr = base_ + class_def.interfaces_off_; |
| 347 | return reinterpret_cast<const TypeList*>(addr); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | const CodeItem* GetCodeItem(const Method& method) const { |
| 352 | if (method.code_off_ == 0) { |
| 353 | return NULL; // native or abstract method |
| 354 | } else { |
| 355 | const byte* addr = base_ + method.code_off_; |
| 356 | return reinterpret_cast<const CodeItem*>(addr); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // Returns the short form method descriptor for the given prototype. |
| 361 | const char* GetShorty(uint32_t proto_idx) const { |
| 362 | const ProtoId& proto_id = GetProtoId(proto_idx); |
| 363 | return dexStringById(proto_id.shorty_idx_); |
| 364 | } |
| 365 | |
| 366 | const TypeList* GetProtoParameters(const ProtoId& proto_id) const { |
| 367 | if (proto_id.parameters_off_ == 0) { |
| 368 | return NULL; |
| 369 | } else { |
| 370 | const byte* addr = base_ + proto_id.parameters_off_; |
| 371 | return reinterpret_cast<const TypeList*>(addr); |
| 372 | } |
| 373 | } |
| 374 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 375 | char* CreateMethodDescriptor(uint32_t proto_idx, |
| 376 | int32_t* unicode_length) const; |
| 377 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 378 | const byte* GetEncodedArray(const ClassDef& class_def) const { |
| 379 | if (class_def.static_values_off_ == 0) { |
| 380 | return 0; |
| 381 | } else { |
| 382 | return base_ + class_def.static_values_off_; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | int32_t GetStringLength(const StringId& string_id) const { |
| 387 | const byte* ptr = base_ + string_id.string_data_off_; |
| 388 | return DecodeUnsignedLeb128(&ptr); |
| 389 | } |
| 390 | |
| 391 | ValueType ReadEncodedValue(const byte** encoded_value, JValue* value) const; |
| 392 | |
| 393 | // From libdex... |
| 394 | |
| 395 | // Returns a pointer to the UTF-8 string data referred to by the |
| 396 | // given string_id. |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 397 | const char* GetStringData(const StringId& string_id, int32_t* length) const { |
| 398 | CHECK(length != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 399 | const byte* ptr = base_ + string_id.string_data_off_; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 400 | *length = DecodeUnsignedLeb128(&ptr); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 401 | return reinterpret_cast<const char*>(ptr); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 404 | const char* GetStringData(const StringId& string_id) const { |
| 405 | int32_t length; |
| 406 | return GetStringData(string_id, &length); |
| 407 | } |
| 408 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 409 | // return the UTF-8 encoded string with the specified string_id index |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 410 | const char* dexStringById(uint32_t idx, int32_t* unicode_length) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 411 | const StringId& string_id = GetStringId(idx); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 412 | return GetStringData(string_id, unicode_length); |
| 413 | } |
| 414 | |
| 415 | const char* dexStringById(uint32_t idx) const { |
| 416 | int32_t unicode_length; |
| 417 | return dexStringById(idx, &unicode_length); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // Get the descriptor string associated with a given type index. |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 421 | const char* dexStringByTypeIdx(uint32_t idx, int32_t* unicode_length) const { |
| 422 | const TypeId& type_id = GetTypeId(idx); |
| 423 | return dexStringById(type_id.descriptor_idx_, unicode_length); |
| 424 | } |
| 425 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 426 | const char* dexStringByTypeIdx(uint32_t idx) const { |
| 427 | const TypeId& type_id = GetTypeId(idx); |
| 428 | return dexStringById(type_id.descriptor_idx_); |
| 429 | } |
| 430 | |
| 431 | // TODO: encoded_field is actually a stream of bytes |
| 432 | void dexReadClassDataField(const byte** encoded_field, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 433 | DexFile::Field* field, |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 434 | uint32_t* last_idx) const { |
| 435 | uint32_t idx = *last_idx + DecodeUnsignedLeb128(encoded_field); |
| 436 | field->access_flags_ = DecodeUnsignedLeb128(encoded_field); |
| 437 | field->field_idx_ = idx; |
| 438 | *last_idx = idx; |
| 439 | } |
| 440 | |
| 441 | // TODO: encoded_method is actually a stream of bytes |
| 442 | void dexReadClassDataMethod(const byte** encoded_method, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 443 | DexFile::Method* method, |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 444 | uint32_t* last_idx) const { |
| 445 | uint32_t idx = *last_idx + DecodeUnsignedLeb128(encoded_method); |
| 446 | method->access_flags_ = DecodeUnsignedLeb128(encoded_method); |
| 447 | method->code_off_ = DecodeUnsignedLeb128(encoded_method); |
| 448 | method->method_idx_ = idx; |
| 449 | *last_idx = idx; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | // TODO: const reference |
| 454 | uint32_t dexGetIndexForClassDef(const ClassDef* class_def) const { |
| 455 | CHECK_GE(class_def, class_defs_); |
| 456 | CHECK_LT(class_def, class_defs_ + header_->class_defs_size_); |
| 457 | return class_def - class_defs_; |
| 458 | } |
| 459 | |
| 460 | const char* dexGetSourceFile(const ClassDef& class_def) const { |
| 461 | if (class_def.source_file_idx_ == 0xffffffff) { |
| 462 | return NULL; |
| 463 | } else { |
| 464 | return dexStringById(class_def.source_file_idx_); |
| 465 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 468 | private: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 469 | // Helper class to deallocate underlying storage. |
| 470 | class Closer { |
| 471 | public: |
| 472 | virtual ~Closer(); |
| 473 | }; |
| 474 | |
| 475 | // Helper class to deallocate mmap-backed .dex files. |
| 476 | class MmapCloser : public Closer { |
| 477 | public: |
| 478 | MmapCloser(void* addr, size_t length); |
| 479 | virtual ~MmapCloser(); |
| 480 | private: |
| 481 | void* addr_; |
| 482 | size_t length_; |
| 483 | }; |
| 484 | |
| 485 | // Helper class for deallocating new/delete-backed .dex files. |
| 486 | class PtrCloser : public Closer { |
| 487 | public: |
| 488 | PtrCloser(byte* addr); |
| 489 | virtual ~PtrCloser(); |
| 490 | private: |
| 491 | byte* addr_; |
| 492 | }; |
| 493 | |
| 494 | // Opens a .dex file at a the given address. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 495 | static DexFile* Open(const byte* dex_file, size_t length, Closer* closer); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 496 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 497 | DexFile(const byte* addr, size_t length, Closer* closer) |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 498 | : base_(addr), |
| 499 | length_(length), |
| 500 | closer_(closer), |
| 501 | header_(0), |
| 502 | string_ids_(0), |
| 503 | type_ids_(0), |
| 504 | field_ids_(0), |
| 505 | method_ids_(0), |
| 506 | proto_ids_(0), |
| 507 | class_defs_(0) {} |
| 508 | |
| 509 | // Top-level initializer that calls other Init methods. |
| 510 | bool Init(); |
| 511 | |
| 512 | // Caches pointers into to the various file sections. |
| 513 | void InitMembers(); |
| 514 | |
| 515 | // Builds the index of descriptors to class definitions. |
| 516 | void InitIndex(); |
| 517 | |
| 518 | // Returns true if the byte string equals the magic value. |
| 519 | bool CheckMagic(const byte* magic); |
| 520 | |
| 521 | // Returns true if the header magic is of the expected value. |
| 522 | bool IsMagicValid(); |
| 523 | |
| 524 | // The index of descriptors to class definitions. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 525 | typedef std::map<const StringPiece, const DexFile::ClassDef*> Index; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 526 | Index index_; |
| 527 | |
| 528 | // The base address of the memory mapping. |
| 529 | const byte* base_; |
| 530 | |
| 531 | // The size of the underlying memory allocation in bytes. |
| 532 | size_t length_; |
| 533 | |
| 534 | // Helper object to free the underlying allocation. |
| 535 | scoped_ptr<Closer> closer_; |
| 536 | |
| 537 | // Points to the header section. |
| 538 | const Header* header_; |
| 539 | |
| 540 | // Points to the base of the string identifier list. |
| 541 | const StringId* string_ids_; |
| 542 | |
| 543 | // Points to the base of the type identifier list. |
| 544 | const TypeId* type_ids_; |
| 545 | |
| 546 | // Points to the base of the field identifier list. |
| 547 | const FieldId* field_ids_; |
| 548 | |
| 549 | // Points to the base of the method identifier list. |
| 550 | const MethodId* method_ids_; |
| 551 | |
| 552 | // Points to the base of the prototype identifier list. |
| 553 | const ProtoId* proto_ids_; |
| 554 | |
| 555 | // Points to the base of the class definition list. |
| 556 | const ClassDef* class_defs_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | } // namespace art |
| 560 | |
| 561 | #endif // ART_SRC_DEX_FILE_H_ |