blob: 231826b7a8a50aa03aa635da7ace216c7df3c6f6 [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
Mathieu Chartier3e0c5172017-11-12 12:58:40 -080029Header* DexIrBuilder(const DexFile& dex_file, bool eagerly_assign_offsets) {
David Sehr853a8e12016-09-01 13:03:50 -070030 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_,
Mathieu Chartierf6e31472017-12-28 13:32:08 -080040 disk_header.data_off_,
41 dex_file.SupportsDefaultMethods());
Jeff Hao3ab96b42016-09-09 18:35:01 -070042 Collections& collections = header->GetCollections();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -080043 collections.SetEagerlyAssignOffsets(eagerly_assign_offsets);
David Sehr853a8e12016-09-01 13:03:50 -070044 // Walk the rest of the header fields.
45 // StringId table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070046 collections.SetStringIdsOffset(disk_header.string_ids_off_);
David Sehr853a8e12016-09-01 13:03:50 -070047 for (uint32_t i = 0; i < dex_file.NumStringIds(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070048 collections.CreateStringId(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070049 }
50 // TypeId table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070051 collections.SetTypeIdsOffset(disk_header.type_ids_off_);
David Sehr853a8e12016-09-01 13:03:50 -070052 for (uint32_t i = 0; i < dex_file.NumTypeIds(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070053 collections.CreateTypeId(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070054 }
55 // ProtoId table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070056 collections.SetProtoIdsOffset(disk_header.proto_ids_off_);
David Sehr853a8e12016-09-01 13:03:50 -070057 for (uint32_t i = 0; i < dex_file.NumProtoIds(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070058 collections.CreateProtoId(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070059 }
60 // FieldId table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070061 collections.SetFieldIdsOffset(disk_header.field_ids_off_);
David Sehr853a8e12016-09-01 13:03:50 -070062 for (uint32_t i = 0; i < dex_file.NumFieldIds(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070063 collections.CreateFieldId(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070064 }
65 // MethodId table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070066 collections.SetMethodIdsOffset(disk_header.method_ids_off_);
David Sehr853a8e12016-09-01 13:03:50 -070067 for (uint32_t i = 0; i < dex_file.NumMethodIds(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070068 collections.CreateMethodId(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070069 }
70 // ClassDef table.
Jeff Hao3ab96b42016-09-09 18:35:01 -070071 collections.SetClassDefsOffset(disk_header.class_defs_off_);
David Sehr853a8e12016-09-01 13:03:50 -070072 for (uint32_t i = 0; i < dex_file.NumClassDefs(); ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070073 collections.CreateClassDef(dex_file, i);
David Sehr853a8e12016-09-01 13:03:50 -070074 }
Jeff Haoa8621002016-10-04 18:13:44 +000075 // MapItem.
Jeff Haoea7c6292016-11-14 18:10:16 -080076 collections.SetMapListOffset(disk_header.map_off_);
Jeff Hao5daee902017-04-27 18:00:38 -070077 // CallSiteIds and MethodHandleItems.
78 collections.CreateCallSitesAndMethodHandles(dex_file);
David Sehrcdcfde72016-09-26 07:44:04 -070079 CheckAndSetRemainingOffsets(dex_file, &collections);
80
Mathieu Chartier3e0c5172017-11-12 12:58:40 -080081 // Sort the vectors by the map order (same order as the file).
82 collections.SortVectorsByMapOrder();
83
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -080084 // Load the link data if it exists.
85 collections.SetLinkData(std::vector<uint8_t>(
86 dex_file.Begin() + dex_file.GetHeader().link_off_,
87 dex_file.Begin() + dex_file.GetHeader().link_off_ + dex_file.GetHeader().link_size_));
88
David Sehr853a8e12016-09-01 13:03:50 -070089 return header;
90}
91
David Sehrcdcfde72016-09-26 07:44:04 -070092static void CheckAndSetRemainingOffsets(const DexFile& dex_file, Collections* collections) {
93 const DexFile::Header& disk_header = dex_file.GetHeader();
94 // Read MapItems and validate/set remaining offsets.
95 const DexFile::MapList* map =
96 reinterpret_cast<const DexFile::MapList*>(dex_file.Begin() + disk_header.map_off_);
97 const uint32_t count = map->size_;
98 for (uint32_t i = 0; i < count; ++i) {
99 const DexFile::MapItem* item = map->list_ + i;
100 switch (item->type_) {
101 case DexFile::kDexTypeHeaderItem:
102 CHECK_EQ(item->size_, 1u);
103 CHECK_EQ(item->offset_, 0u);
104 break;
105 case DexFile::kDexTypeStringIdItem:
106 CHECK_EQ(item->size_, collections->StringIdsSize());
107 CHECK_EQ(item->offset_, collections->StringIdsOffset());
108 break;
109 case DexFile::kDexTypeTypeIdItem:
110 CHECK_EQ(item->size_, collections->TypeIdsSize());
111 CHECK_EQ(item->offset_, collections->TypeIdsOffset());
112 break;
113 case DexFile::kDexTypeProtoIdItem:
114 CHECK_EQ(item->size_, collections->ProtoIdsSize());
115 CHECK_EQ(item->offset_, collections->ProtoIdsOffset());
116 break;
117 case DexFile::kDexTypeFieldIdItem:
118 CHECK_EQ(item->size_, collections->FieldIdsSize());
119 CHECK_EQ(item->offset_, collections->FieldIdsOffset());
120 break;
121 case DexFile::kDexTypeMethodIdItem:
122 CHECK_EQ(item->size_, collections->MethodIdsSize());
123 CHECK_EQ(item->offset_, collections->MethodIdsOffset());
124 break;
125 case DexFile::kDexTypeClassDefItem:
126 CHECK_EQ(item->size_, collections->ClassDefsSize());
127 CHECK_EQ(item->offset_, collections->ClassDefsOffset());
128 break;
Jeff Hao5daee902017-04-27 18:00:38 -0700129 case DexFile::kDexTypeCallSiteIdItem:
130 CHECK_EQ(item->size_, collections->CallSiteIdsSize());
131 CHECK_EQ(item->offset_, collections->CallSiteIdsOffset());
132 break;
133 case DexFile::kDexTypeMethodHandleItem:
134 CHECK_EQ(item->size_, collections->MethodHandleItemsSize());
135 CHECK_EQ(item->offset_, collections->MethodHandleItemsOffset());
136 break;
David Sehrcdcfde72016-09-26 07:44:04 -0700137 case DexFile::kDexTypeMapList:
138 CHECK_EQ(item->size_, 1u);
139 CHECK_EQ(item->offset_, disk_header.map_off_);
140 break;
141 case DexFile::kDexTypeTypeList:
142 collections->SetTypeListsOffset(item->offset_);
143 break;
144 case DexFile::kDexTypeAnnotationSetRefList:
145 collections->SetAnnotationSetRefListsOffset(item->offset_);
146 break;
147 case DexFile::kDexTypeAnnotationSetItem:
Jeff Haoa8621002016-10-04 18:13:44 +0000148 collections->SetAnnotationSetItemsOffset(item->offset_);
David Sehrcdcfde72016-09-26 07:44:04 -0700149 break;
150 case DexFile::kDexTypeClassDataItem:
151 collections->SetClassDatasOffset(item->offset_);
152 break;
153 case DexFile::kDexTypeCodeItem:
154 collections->SetCodeItemsOffset(item->offset_);
155 break;
156 case DexFile::kDexTypeStringDataItem:
157 collections->SetStringDatasOffset(item->offset_);
158 break;
159 case DexFile::kDexTypeDebugInfoItem:
Jeff Haoa8621002016-10-04 18:13:44 +0000160 collections->SetDebugInfoItemsOffset(item->offset_);
David Sehrcdcfde72016-09-26 07:44:04 -0700161 break;
162 case DexFile::kDexTypeAnnotationItem:
Jeff Haoa8621002016-10-04 18:13:44 +0000163 collections->SetAnnotationItemsOffset(item->offset_);
Mathieu Chartier24066ec2017-10-21 16:01:08 -0700164 collections->AddAnnotationsFromMapListSection(dex_file, item->offset_, item->size_);
David Sehrcdcfde72016-09-26 07:44:04 -0700165 break;
166 case DexFile::kDexTypeEncodedArrayItem:
Jeff Haoa8621002016-10-04 18:13:44 +0000167 collections->SetEncodedArrayItemsOffset(item->offset_);
David Sehrcdcfde72016-09-26 07:44:04 -0700168 break;
169 case DexFile::kDexTypeAnnotationsDirectoryItem:
Jeff Haoa8621002016-10-04 18:13:44 +0000170 collections->SetAnnotationsDirectoryItemsOffset(item->offset_);
David Sehrcdcfde72016-09-26 07:44:04 -0700171 break;
172 default:
173 LOG(ERROR) << "Unknown map list item type.";
174 }
175 }
176}
177
David Sehr853a8e12016-09-01 13:03:50 -0700178} // namespace dex_ir
179} // namespace art