David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | namespace art { |
| 25 | namespace dex_ir { |
| 26 | |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 27 | static void CheckAndSetRemainingOffsets(const DexFile& dex_file, Collections* collections); |
| 28 | |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 29 | Header* DexIrBuilder(const DexFile& dex_file, bool eagerly_assign_offsets) { |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 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_, |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame^] | 40 | disk_header.data_off_, |
| 41 | dex_file.SupportsDefaultMethods()); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 42 | Collections& collections = header->GetCollections(); |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 43 | collections.SetEagerlyAssignOffsets(eagerly_assign_offsets); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 44 | // Walk the rest of the header fields. |
| 45 | // StringId table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 46 | collections.SetStringIdsOffset(disk_header.string_ids_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 47 | for (uint32_t i = 0; i < dex_file.NumStringIds(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 48 | collections.CreateStringId(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 49 | } |
| 50 | // TypeId table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 51 | collections.SetTypeIdsOffset(disk_header.type_ids_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 52 | for (uint32_t i = 0; i < dex_file.NumTypeIds(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 53 | collections.CreateTypeId(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 54 | } |
| 55 | // ProtoId table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 56 | collections.SetProtoIdsOffset(disk_header.proto_ids_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 57 | for (uint32_t i = 0; i < dex_file.NumProtoIds(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 58 | collections.CreateProtoId(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 59 | } |
| 60 | // FieldId table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 61 | collections.SetFieldIdsOffset(disk_header.field_ids_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 62 | for (uint32_t i = 0; i < dex_file.NumFieldIds(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 63 | collections.CreateFieldId(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 64 | } |
| 65 | // MethodId table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 66 | collections.SetMethodIdsOffset(disk_header.method_ids_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 67 | for (uint32_t i = 0; i < dex_file.NumMethodIds(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 68 | collections.CreateMethodId(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 69 | } |
| 70 | // ClassDef table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 71 | collections.SetClassDefsOffset(disk_header.class_defs_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 72 | for (uint32_t i = 0; i < dex_file.NumClassDefs(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 73 | collections.CreateClassDef(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 74 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 75 | // MapItem. |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 76 | collections.SetMapListOffset(disk_header.map_off_); |
Jeff Hao | 5daee90 | 2017-04-27 18:00:38 -0700 | [diff] [blame] | 77 | // CallSiteIds and MethodHandleItems. |
| 78 | collections.CreateCallSitesAndMethodHandles(dex_file); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 79 | CheckAndSetRemainingOffsets(dex_file, &collections); |
| 80 | |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 81 | // Sort the vectors by the map order (same order as the file). |
| 82 | collections.SortVectorsByMapOrder(); |
| 83 | |
Mathieu Chartier | 2f36d2f | 2017-11-20 15:45:25 -0800 | [diff] [blame] | 84 | // 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 Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 89 | return header; |
| 90 | } |
| 91 | |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 92 | static 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 Hao | 5daee90 | 2017-04-27 18:00:38 -0700 | [diff] [blame] | 129 | 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 Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 137 | 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 Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 148 | collections->SetAnnotationSetItemsOffset(item->offset_); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 149 | 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 Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 160 | collections->SetDebugInfoItemsOffset(item->offset_); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 161 | break; |
| 162 | case DexFile::kDexTypeAnnotationItem: |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 163 | collections->SetAnnotationItemsOffset(item->offset_); |
Mathieu Chartier | 24066ec | 2017-10-21 16:01:08 -0700 | [diff] [blame] | 164 | collections->AddAnnotationsFromMapListSection(dex_file, item->offset_, item->size_); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 165 | break; |
| 166 | case DexFile::kDexTypeEncodedArrayItem: |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 167 | collections->SetEncodedArrayItemsOffset(item->offset_); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 168 | break; |
| 169 | case DexFile::kDexTypeAnnotationsDirectoryItem: |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 170 | collections->SetAnnotationsDirectoryItemsOffset(item->offset_); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 171 | break; |
| 172 | default: |
| 173 | LOG(ERROR) << "Unknown map list item type."; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 178 | } // namespace dex_ir |
| 179 | } // namespace art |