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 | |
| 186 | // Partially decoded form of class_data_item. |
| 187 | struct ClassDataHeader { |
| 188 | uint32_t static_fields_size_; // the number of static fields |
| 189 | uint32_t instance_fields_size_; // the number of instance fields |
| 190 | uint32_t direct_methods_size_; // the number of direct methods |
| 191 | uint32_t virtual_methods_size_; // the number of virtual methods |
| 192 | }; |
| 193 | |
| 194 | // Decoded form of encoded_field. |
| 195 | struct Field { |
| 196 | uint32_t field_idx_; // index into the field_ids list for the identity of this field |
| 197 | uint32_t access_flags_; // access flags for the field |
| 198 | }; |
| 199 | |
| 200 | // Decoded form of encoded_method. |
| 201 | struct Method { |
| 202 | uint32_t method_idx_; |
| 203 | uint32_t access_flags_; |
| 204 | uint32_t code_off_; |
| 205 | }; |
| 206 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame^] | 207 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 208 | typedef std::vector<const DexFile*> ClassPath; |
| 209 | |
| 210 | // Search a collection of DexFiles for a descriptor |
| 211 | static ClassPathEntry FindInClassPath(const StringPiece& descriptor, |
| 212 | ClassPath& class_path); |
| 213 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 214 | // Opens a .dex file from the file system. |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 215 | static DexFile* OpenFile(const std::string& filename); |
| 216 | |
| 217 | // Opens a .jar, .zip, or .apk file from the file system. |
| 218 | static DexFile* OpenZip(const std::string& filename); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 219 | |
| 220 | // Opens a .dex file from a new allocated pointer |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 221 | static DexFile* OpenPtr(byte* ptr, size_t length); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 222 | |
| 223 | // Closes a .dex file. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 224 | virtual ~DexFile(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 225 | |
| 226 | const Header& GetHeader() { |
| 227 | CHECK(header_ != NULL); |
| 228 | return *header_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 231 | // Looks up a class definition by its class descriptor. |
| 232 | const ClassDef* FindClassDef(const StringPiece& descriptor) const; |
| 233 | |
| 234 | // Returns the number of string identifiers in the .dex file. |
| 235 | size_t NumStringIds() const { |
| 236 | CHECK(header_ != NULL); |
| 237 | return header_->string_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 240 | // Returns the number of type identifiers in the .dex file. |
| 241 | size_t NumTypeIds() const { |
| 242 | CHECK(header_ != NULL); |
| 243 | return header_->type_ids_size_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 246 | // Returns the number of prototype identifiers in the .dex file. |
| 247 | size_t NumProtoIds() const { |
| 248 | CHECK(header_ != NULL); |
| 249 | return header_->proto_ids_size_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 252 | // Returns the number of field identifiers in the .dex file. |
| 253 | size_t NumFieldIds() const { |
| 254 | CHECK(header_ != NULL); |
| 255 | return header_->field_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 258 | // Returns the number of method identifiers in the .dex file. |
| 259 | size_t NumMethodIds() const { |
| 260 | CHECK(header_ != NULL); |
| 261 | return header_->method_ids_size_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 264 | // Returns the number of class definitions in the .dex file. |
| 265 | size_t NumClassDefs() const { |
| 266 | CHECK(header_ != NULL); |
| 267 | return header_->class_defs_size_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 270 | // Returns a pointer to the memory mapped class data. |
| 271 | // TODO: return a stream |
| 272 | const byte* GetClassData(const ClassDef& class_def) const { |
| 273 | if (class_def.class_data_off_ == 0) { |
| 274 | return NULL; |
| 275 | } else { |
| 276 | return base_ + class_def.class_data_off_; |
| 277 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 280 | // Decodes the header section from the class data bytes. |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 281 | ClassDataHeader ReadClassDataHeader(const byte** class_data) const { |
| 282 | CHECK(class_data != NULL); |
| 283 | ClassDataHeader header; |
| 284 | memset(&header, 0, sizeof(ClassDataHeader)); |
| 285 | if (*class_data != NULL) { |
| 286 | header.static_fields_size_ = DecodeUnsignedLeb128(class_data); |
| 287 | header.instance_fields_size_ = DecodeUnsignedLeb128(class_data); |
| 288 | header.direct_methods_size_ = DecodeUnsignedLeb128(class_data); |
| 289 | header.virtual_methods_size_ = DecodeUnsignedLeb128(class_data); |
| 290 | } |
| 291 | return header; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 294 | // Returns the class descriptor string of a class definition. |
| 295 | const char* GetClassDescriptor(const ClassDef& class_def) const { |
| 296 | return dexStringByTypeIdx(class_def.class_idx_); |
| 297 | } |
| 298 | |
| 299 | // Returns the StringId at the specified index. |
| 300 | const StringId& GetStringId(uint32_t idx) const { |
| 301 | CHECK_LT(idx, NumStringIds()); |
| 302 | return string_ids_[idx]; |
| 303 | } |
| 304 | |
| 305 | // Returns the TypeId at the specified index. |
| 306 | const TypeId& GetTypeId(uint32_t idx) const { |
| 307 | CHECK_LT(idx, NumTypeIds()); |
| 308 | return type_ids_[idx]; |
| 309 | } |
| 310 | |
| 311 | // Returns the FieldId at the specified index. |
| 312 | const FieldId& GetFieldId(uint32_t idx) const { |
| 313 | CHECK_LT(idx, NumFieldIds()); |
| 314 | return field_ids_[idx]; |
| 315 | } |
| 316 | |
| 317 | // Returns the MethodId at the specified index. |
| 318 | const MethodId& GetMethodId(uint32_t idx) const { |
| 319 | CHECK_LT(idx, NumMethodIds()); |
| 320 | return method_ids_[idx]; |
| 321 | } |
| 322 | |
| 323 | // Returns the ProtoId at the specified index. |
| 324 | const ProtoId& GetProtoId(uint32_t idx) const { |
| 325 | CHECK_LT(idx, NumProtoIds()); |
| 326 | return proto_ids_[idx]; |
| 327 | } |
| 328 | |
| 329 | // Returns the ClassDef at the specified index. |
| 330 | const ClassDef& GetClassDef(uint32_t idx) const { |
| 331 | CHECK_LT(idx, NumClassDefs()); |
| 332 | return class_defs_[idx]; |
| 333 | } |
| 334 | |
| 335 | const TypeList* GetInterfacesList(const ClassDef& class_def) const { |
| 336 | if (class_def.interfaces_off_ == 0) { |
| 337 | return NULL; |
| 338 | } else { |
| 339 | const byte* addr = base_ + class_def.interfaces_off_; |
| 340 | return reinterpret_cast<const TypeList*>(addr); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | const CodeItem* GetCodeItem(const Method& method) const { |
| 345 | if (method.code_off_ == 0) { |
| 346 | return NULL; // native or abstract method |
| 347 | } else { |
| 348 | const byte* addr = base_ + method.code_off_; |
| 349 | return reinterpret_cast<const CodeItem*>(addr); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // Returns the short form method descriptor for the given prototype. |
| 354 | const char* GetShorty(uint32_t proto_idx) const { |
| 355 | const ProtoId& proto_id = GetProtoId(proto_idx); |
| 356 | return dexStringById(proto_id.shorty_idx_); |
| 357 | } |
| 358 | |
| 359 | const TypeList* GetProtoParameters(const ProtoId& proto_id) const { |
| 360 | if (proto_id.parameters_off_ == 0) { |
| 361 | return NULL; |
| 362 | } else { |
| 363 | const byte* addr = base_ + proto_id.parameters_off_; |
| 364 | return reinterpret_cast<const TypeList*>(addr); |
| 365 | } |
| 366 | } |
| 367 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 368 | char* CreateMethodDescriptor(uint32_t proto_idx, |
| 369 | int32_t* unicode_length) const; |
| 370 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 371 | const byte* GetEncodedArray(const ClassDef& class_def) const { |
| 372 | if (class_def.static_values_off_ == 0) { |
| 373 | return 0; |
| 374 | } else { |
| 375 | return base_ + class_def.static_values_off_; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | int32_t GetStringLength(const StringId& string_id) const { |
| 380 | const byte* ptr = base_ + string_id.string_data_off_; |
| 381 | return DecodeUnsignedLeb128(&ptr); |
| 382 | } |
| 383 | |
| 384 | ValueType ReadEncodedValue(const byte** encoded_value, JValue* value) const; |
| 385 | |
| 386 | // From libdex... |
| 387 | |
| 388 | // Returns a pointer to the UTF-8 string data referred to by the |
| 389 | // given string_id. |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 390 | const char* GetStringData(const StringId& string_id, int32_t* length) const { |
| 391 | CHECK(length != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 392 | const byte* ptr = base_ + string_id.string_data_off_; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 393 | *length = DecodeUnsignedLeb128(&ptr); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 394 | return reinterpret_cast<const char*>(ptr); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 397 | const char* GetStringData(const StringId& string_id) const { |
| 398 | int32_t length; |
| 399 | return GetStringData(string_id, &length); |
| 400 | } |
| 401 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 402 | // return the UTF-8 encoded string with the specified string_id index |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 403 | const char* dexStringById(uint32_t idx, int32_t* unicode_length) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 404 | const StringId& string_id = GetStringId(idx); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 405 | return GetStringData(string_id, unicode_length); |
| 406 | } |
| 407 | |
| 408 | const char* dexStringById(uint32_t idx) const { |
| 409 | int32_t unicode_length; |
| 410 | return dexStringById(idx, &unicode_length); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // Get the descriptor string associated with a given type index. |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 414 | const char* dexStringByTypeIdx(uint32_t idx, int32_t* unicode_length) const { |
| 415 | const TypeId& type_id = GetTypeId(idx); |
| 416 | return dexStringById(type_id.descriptor_idx_, unicode_length); |
| 417 | } |
| 418 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 419 | const char* dexStringByTypeIdx(uint32_t idx) const { |
| 420 | const TypeId& type_id = GetTypeId(idx); |
| 421 | return dexStringById(type_id.descriptor_idx_); |
| 422 | } |
| 423 | |
| 424 | // TODO: encoded_field is actually a stream of bytes |
| 425 | void dexReadClassDataField(const byte** encoded_field, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 426 | DexFile::Field* field, |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 427 | uint32_t* last_idx) const { |
| 428 | uint32_t idx = *last_idx + DecodeUnsignedLeb128(encoded_field); |
| 429 | field->access_flags_ = DecodeUnsignedLeb128(encoded_field); |
| 430 | field->field_idx_ = idx; |
| 431 | *last_idx = idx; |
| 432 | } |
| 433 | |
| 434 | // TODO: encoded_method is actually a stream of bytes |
| 435 | void dexReadClassDataMethod(const byte** encoded_method, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 436 | DexFile::Method* method, |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 437 | uint32_t* last_idx) const { |
| 438 | uint32_t idx = *last_idx + DecodeUnsignedLeb128(encoded_method); |
| 439 | method->access_flags_ = DecodeUnsignedLeb128(encoded_method); |
| 440 | method->code_off_ = DecodeUnsignedLeb128(encoded_method); |
| 441 | method->method_idx_ = idx; |
| 442 | *last_idx = idx; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | // TODO: const reference |
| 447 | uint32_t dexGetIndexForClassDef(const ClassDef* class_def) const { |
| 448 | CHECK_GE(class_def, class_defs_); |
| 449 | CHECK_LT(class_def, class_defs_ + header_->class_defs_size_); |
| 450 | return class_def - class_defs_; |
| 451 | } |
| 452 | |
| 453 | const char* dexGetSourceFile(const ClassDef& class_def) const { |
| 454 | if (class_def.source_file_idx_ == 0xffffffff) { |
| 455 | return NULL; |
| 456 | } else { |
| 457 | return dexStringById(class_def.source_file_idx_); |
| 458 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 461 | private: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 462 | // Helper class to deallocate underlying storage. |
| 463 | class Closer { |
| 464 | public: |
| 465 | virtual ~Closer(); |
| 466 | }; |
| 467 | |
| 468 | // Helper class to deallocate mmap-backed .dex files. |
| 469 | class MmapCloser : public Closer { |
| 470 | public: |
| 471 | MmapCloser(void* addr, size_t length); |
| 472 | virtual ~MmapCloser(); |
| 473 | private: |
| 474 | void* addr_; |
| 475 | size_t length_; |
| 476 | }; |
| 477 | |
| 478 | // Helper class for deallocating new/delete-backed .dex files. |
| 479 | class PtrCloser : public Closer { |
| 480 | public: |
| 481 | PtrCloser(byte* addr); |
| 482 | virtual ~PtrCloser(); |
| 483 | private: |
| 484 | byte* addr_; |
| 485 | }; |
| 486 | |
| 487 | // Opens a .dex file at a the given address. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 488 | static DexFile* Open(const byte* dex_file, size_t length, Closer* closer); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 489 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 490 | DexFile(const byte* addr, size_t length, Closer* closer) |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 491 | : base_(addr), |
| 492 | length_(length), |
| 493 | closer_(closer), |
| 494 | header_(0), |
| 495 | string_ids_(0), |
| 496 | type_ids_(0), |
| 497 | field_ids_(0), |
| 498 | method_ids_(0), |
| 499 | proto_ids_(0), |
| 500 | class_defs_(0) {} |
| 501 | |
| 502 | // Top-level initializer that calls other Init methods. |
| 503 | bool Init(); |
| 504 | |
| 505 | // Caches pointers into to the various file sections. |
| 506 | void InitMembers(); |
| 507 | |
| 508 | // Builds the index of descriptors to class definitions. |
| 509 | void InitIndex(); |
| 510 | |
| 511 | // Returns true if the byte string equals the magic value. |
| 512 | bool CheckMagic(const byte* magic); |
| 513 | |
| 514 | // Returns true if the header magic is of the expected value. |
| 515 | bool IsMagicValid(); |
| 516 | |
| 517 | // The index of descriptors to class definitions. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 518 | typedef std::map<const StringPiece, const DexFile::ClassDef*> Index; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 519 | Index index_; |
| 520 | |
| 521 | // The base address of the memory mapping. |
| 522 | const byte* base_; |
| 523 | |
| 524 | // The size of the underlying memory allocation in bytes. |
| 525 | size_t length_; |
| 526 | |
| 527 | // Helper object to free the underlying allocation. |
| 528 | scoped_ptr<Closer> closer_; |
| 529 | |
| 530 | // Points to the header section. |
| 531 | const Header* header_; |
| 532 | |
| 533 | // Points to the base of the string identifier list. |
| 534 | const StringId* string_ids_; |
| 535 | |
| 536 | // Points to the base of the type identifier list. |
| 537 | const TypeId* type_ids_; |
| 538 | |
| 539 | // Points to the base of the field identifier list. |
| 540 | const FieldId* field_ids_; |
| 541 | |
| 542 | // Points to the base of the method identifier list. |
| 543 | const MethodId* method_ids_; |
| 544 | |
| 545 | // Points to the base of the prototype identifier list. |
| 546 | const ProtoId* proto_ids_; |
| 547 | |
| 548 | // Points to the base of the class definition list. |
| 549 | const ClassDef* class_defs_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | } // namespace art |
| 553 | |
| 554 | #endif // ART_SRC_DEX_FILE_H_ |