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 | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 27 | Header* DexIrBuilder(const DexFile& dex_file) { |
| 28 | const DexFile::Header& disk_header = dex_file.GetHeader(); |
| 29 | Header* header = new Header(disk_header.magic_, |
| 30 | disk_header.checksum_, |
| 31 | disk_header.signature_, |
| 32 | disk_header.endian_tag_, |
| 33 | disk_header.file_size_, |
| 34 | disk_header.header_size_, |
| 35 | disk_header.link_size_, |
| 36 | disk_header.link_off_, |
| 37 | disk_header.data_size_, |
| 38 | disk_header.data_off_); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 39 | Collections& collections = header->GetCollections(); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 40 | // Walk the rest of the header fields. |
| 41 | // StringId table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 42 | collections.SetStringIdsOffset(disk_header.string_ids_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 43 | for (uint32_t i = 0; i < dex_file.NumStringIds(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 44 | collections.CreateStringId(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 45 | } |
| 46 | // TypeId table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 47 | collections.SetTypeIdsOffset(disk_header.type_ids_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 48 | for (uint32_t i = 0; i < dex_file.NumTypeIds(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 49 | collections.CreateTypeId(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 50 | } |
| 51 | // ProtoId table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 52 | collections.SetProtoIdsOffset(disk_header.proto_ids_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 53 | for (uint32_t i = 0; i < dex_file.NumProtoIds(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 54 | collections.CreateProtoId(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 55 | } |
| 56 | // FieldId table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 57 | collections.SetFieldIdsOffset(disk_header.field_ids_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 58 | for (uint32_t i = 0; i < dex_file.NumFieldIds(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 59 | collections.CreateFieldId(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 60 | } |
| 61 | // MethodId table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 62 | collections.SetMethodIdsOffset(disk_header.method_ids_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 63 | for (uint32_t i = 0; i < dex_file.NumMethodIds(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 64 | collections.CreateMethodId(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 65 | } |
| 66 | // ClassDef table. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 67 | collections.SetClassDefsOffset(disk_header.class_defs_off_); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 68 | for (uint32_t i = 0; i < dex_file.NumClassDefs(); ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame^] | 69 | collections.CreateClassDef(dex_file, i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return header; |
| 73 | } |
| 74 | |
| 75 | } // namespace dex_ir |
| 76 | } // namespace art |