blob: 599f48b3b698bb34f45038c787b07bfa3c6223d5 [file] [log] [blame]
David Sehr853a8e12016-09-01 13:03:50 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Header file of an in-memory representation of DEX files.
17 */
18
19#include <stdint.h>
20#include <vector>
21
22#include "dex_ir_builder.h"
23
24namespace art {
25namespace dex_ir {
26
David Sehrcdcfde72016-09-26 07:44:04 -070027static void CheckAndSetRemainingOffsets(const DexFile& dex_file, Collections* collections);
28
David Sehr853a8e12016-09-01 13:03:50 -070029Header* DexIrBuilder(const DexFile& dex_file) {
30 const DexFile::Header& disk_header = dex_file.GetHeader();
31 Header* header = new Header(disk_header.magic_,
32 disk_header.checksum_,
33 disk_header.signature_,
34 disk_header.endian_tag_,
35 disk_header.file_size_,
36 disk_header.header_size_,
37 disk_header.link_size_,
38 disk_header.link_off_,
39 disk_header.data_size_,
40 disk_header.data_off_);
Jeff Hao3ab96b42016-09-09 18:35:01 -070041 Collections& collections = header->GetCollections();
David Sehr853a8e12016-09-01 13:03:50 -070042 // Walk the rest of the header fields.
43 // StringId table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070044 collections.SetStringIdsOffset(disk_header.string_ids_off_);
David Sehr853a8e12016-09-01 13:03:50 -070045 for (uint32_t i = 0; i < dex_file.NumStringIds(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070046 collections.CreateStringId(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070047 }
48 // TypeId table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070049 collections.SetTypeIdsOffset(disk_header.type_ids_off_);
David Sehr853a8e12016-09-01 13:03:50 -070050 for (uint32_t i = 0; i < dex_file.NumTypeIds(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070051 collections.CreateTypeId(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070052 }
53 // ProtoId table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070054 collections.SetProtoIdsOffset(disk_header.proto_ids_off_);
David Sehr853a8e12016-09-01 13:03:50 -070055 for (uint32_t i = 0; i < dex_file.NumProtoIds(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070056 collections.CreateProtoId(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070057 }
58 // FieldId table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070059 collections.SetFieldIdsOffset(disk_header.field_ids_off_);
David Sehr853a8e12016-09-01 13:03:50 -070060 for (uint32_t i = 0; i < dex_file.NumFieldIds(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070061 collections.CreateFieldId(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070062 }
63 // MethodId table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070064 collections.SetMethodIdsOffset(disk_header.method_ids_off_);
David Sehr853a8e12016-09-01 13:03:50 -070065 for (uint32_t i = 0; i < dex_file.NumMethodIds(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070066 collections.CreateMethodId(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070067 }
68 // ClassDef table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070069 collections.SetClassDefsOffset(disk_header.class_defs_off_);
David Sehr853a8e12016-09-01 13:03:50 -070070 for (uint32_t i = 0; i < dex_file.NumClassDefs(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070071 collections.CreateClassDef(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070072 }
David Sehr853a8e12016-09-01 13:03:50 -070073
David Sehrcdcfde72016-09-26 07:44:04 -070074 CheckAndSetRemainingOffsets(dex_file, &collections);
75
David Sehr853a8e12016-09-01 13:03:50 -070076 return header;
77}
78
David Sehrcdcfde72016-09-26 07:44:04 -070079static void CheckAndSetRemainingOffsets(const DexFile& dex_file, Collections* collections) {
80 const DexFile::Header& disk_header = dex_file.GetHeader();
81 // Read MapItems and validate/set remaining offsets.
82 const DexFile::MapList* map =
83 reinterpret_cast<const DexFile::MapList*>(dex_file.Begin() + disk_header.map_off_);
84 const uint32_t count = map->size_;
85 for (uint32_t i = 0; i < count; ++i) {
86 const DexFile::MapItem* item = map->list_ + i;
87 switch (item->type_) {
88 case DexFile::kDexTypeHeaderItem:
89 CHECK_EQ(item->size_, 1u);
90 CHECK_EQ(item->offset_, 0u);
91 break;
92 case DexFile::kDexTypeStringIdItem:
93 CHECK_EQ(item->size_, collections->StringIdsSize());
94 CHECK_EQ(item->offset_, collections->StringIdsOffset());
95 break;
96 case DexFile::kDexTypeTypeIdItem:
97 CHECK_EQ(item->size_, collections->TypeIdsSize());
98 CHECK_EQ(item->offset_, collections->TypeIdsOffset());
99 break;
100 case DexFile::kDexTypeProtoIdItem:
101 CHECK_EQ(item->size_, collections->ProtoIdsSize());
102 CHECK_EQ(item->offset_, collections->ProtoIdsOffset());
103 break;
104 case DexFile::kDexTypeFieldIdItem:
105 CHECK_EQ(item->size_, collections->FieldIdsSize());
106 CHECK_EQ(item->offset_, collections->FieldIdsOffset());
107 break;
108 case DexFile::kDexTypeMethodIdItem:
109 CHECK_EQ(item->size_, collections->MethodIdsSize());
110 CHECK_EQ(item->offset_, collections->MethodIdsOffset());
111 break;
112 case DexFile::kDexTypeClassDefItem:
113 CHECK_EQ(item->size_, collections->ClassDefsSize());
114 CHECK_EQ(item->offset_, collections->ClassDefsOffset());
115 break;
116 case DexFile::kDexTypeMapList:
117 CHECK_EQ(item->size_, 1u);
118 CHECK_EQ(item->offset_, disk_header.map_off_);
119 break;
120 case DexFile::kDexTypeTypeList:
121 collections->SetTypeListsOffset(item->offset_);
122 break;
123 case DexFile::kDexTypeAnnotationSetRefList:
124 collections->SetAnnotationSetRefListsOffset(item->offset_);
125 break;
126 case DexFile::kDexTypeAnnotationSetItem:
127 collections->SetAnnotationSetOffset(item->offset_);
128 break;
129 case DexFile::kDexTypeClassDataItem:
130 collections->SetClassDatasOffset(item->offset_);
131 break;
132 case DexFile::kDexTypeCodeItem:
133 collections->SetCodeItemsOffset(item->offset_);
134 break;
135 case DexFile::kDexTypeStringDataItem:
136 collections->SetStringDatasOffset(item->offset_);
137 break;
138 case DexFile::kDexTypeDebugInfoItem:
139 collections->SetDebugInfoOffset(item->offset_);
140 break;
141 case DexFile::kDexTypeAnnotationItem:
142 collections->SetAnnotationOffset(item->offset_);
143 break;
144 case DexFile::kDexTypeEncodedArrayItem:
145 collections->SetEncodedArrayOffset(item->offset_);
146 break;
147 case DexFile::kDexTypeAnnotationsDirectoryItem:
148 collections->SetAnnotationsDirectoryOffset(item->offset_);
149 break;
150 default:
151 LOG(ERROR) << "Unknown map list item type.";
152 }
153 }
154}
155
David Sehr853a8e12016-09-01 13:03:50 -0700156} // namespace dex_ir
157} // namespace art