blob: 3917847ea743fec9bbb1521057c5b47aab74c542 [file] [log] [blame]
Jeff Hao3ab96b42016-09-09 18:35:01 -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 * Implementation file of the dexlayout utility.
17 *
18 * This is a tool to read dex files into an internal representation,
19 * reorganize the representation, and emit dex files with a better
20 * file layout.
21 */
22
23#include "dex_ir.h"
Mathieu Chartier808c7a52017-12-15 11:19:33 -080024
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/code_item_accessors-inl.h"
26#include "dex/dex_file_exception_helpers.h"
27#include "dex/dex_instruction-inl.h"
Jeff Hao3ab96b42016-09-09 18:35:01 -070028#include "dex_ir_builder.h"
29
30namespace art {
31namespace dex_ir {
32
David Sehr2b5a38f2018-06-14 15:13:04 -070033static uint32_t HeaderOffset(const dex_ir::Header* header ATTRIBUTE_UNUSED) {
David Sehr9037a3a2017-03-30 17:50:24 -070034 return 0;
35}
36
David Sehr2b5a38f2018-06-14 15:13:04 -070037static uint32_t HeaderSize(const dex_ir::Header* header ATTRIBUTE_UNUSED) {
David Sehr9037a3a2017-03-30 17:50:24 -070038 // Size is in elements, so there is only one header.
39 return 1;
40}
41
42// The description of each dex file section type.
43struct FileSectionDescriptor {
44 public:
45 std::string name;
46 uint16_t type;
47 // A function that when applied to a collection object, gives the size of the section.
David Sehr2b5a38f2018-06-14 15:13:04 -070048 std::function<uint32_t(dex_ir::Header*)> size_fn;
David Sehr9037a3a2017-03-30 17:50:24 -070049 // A function that when applied to a collection object, gives the offset of the section.
David Sehr2b5a38f2018-06-14 15:13:04 -070050 std::function<uint32_t(dex_ir::Header*)> offset_fn;
David Sehr9037a3a2017-03-30 17:50:24 -070051};
52
David Sehr7639cdc2017-04-15 10:06:21 -070053static const FileSectionDescriptor kFileSectionDescriptors[] = {
David Sehr9037a3a2017-03-30 17:50:24 -070054 {
55 "Header",
56 DexFile::kDexTypeHeaderItem,
57 &HeaderSize,
58 &HeaderOffset,
59 }, {
60 "StringId",
61 DexFile::kDexTypeStringIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -070062 [](const dex_ir::Header* h) { return h->StringIds().Size(); },
63 [](const dex_ir::Header* h) { return h->StringIds().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -070064 }, {
65 "TypeId",
66 DexFile::kDexTypeTypeIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -070067 [](const dex_ir::Header* h) { return h->TypeIds().Size(); },
68 [](const dex_ir::Header* h) { return h->TypeIds().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -070069 }, {
70 "ProtoId",
71 DexFile::kDexTypeProtoIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -070072 [](const dex_ir::Header* h) { return h->ProtoIds().Size(); },
73 [](const dex_ir::Header* h) { return h->ProtoIds().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -070074 }, {
75 "FieldId",
76 DexFile::kDexTypeFieldIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -070077 [](const dex_ir::Header* h) { return h->FieldIds().Size(); },
78 [](const dex_ir::Header* h) { return h->FieldIds().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -070079 }, {
80 "MethodId",
81 DexFile::kDexTypeMethodIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -070082 [](const dex_ir::Header* h) { return h->MethodIds().Size(); },
83 [](const dex_ir::Header* h) { return h->MethodIds().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -070084 }, {
85 "ClassDef",
86 DexFile::kDexTypeClassDefItem,
David Sehr2b5a38f2018-06-14 15:13:04 -070087 [](const dex_ir::Header* h) { return h->ClassDefs().Size(); },
88 [](const dex_ir::Header* h) { return h->ClassDefs().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -070089 }, {
Jeff Hao5daee902017-04-27 18:00:38 -070090 "CallSiteId",
91 DexFile::kDexTypeCallSiteIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -070092 [](const dex_ir::Header* h) { return h->CallSiteIds().Size(); },
93 [](const dex_ir::Header* h) { return h->CallSiteIds().GetOffset(); }
Jeff Hao5daee902017-04-27 18:00:38 -070094 }, {
95 "MethodHandle",
96 DexFile::kDexTypeMethodHandleItem,
David Sehr2b5a38f2018-06-14 15:13:04 -070097 [](const dex_ir::Header* h) { return h->MethodHandleItems().Size(); },
98 [](const dex_ir::Header* h) { return h->MethodHandleItems().GetOffset(); }
Jeff Hao5daee902017-04-27 18:00:38 -070099 }, {
David Sehr9037a3a2017-03-30 17:50:24 -0700100 "StringData",
101 DexFile::kDexTypeStringDataItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700102 [](const dex_ir::Header* h) { return h->StringDatas().Size(); },
103 [](const dex_ir::Header* h) { return h->StringDatas().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -0700104 }, {
105 "TypeList",
106 DexFile::kDexTypeTypeList,
David Sehr2b5a38f2018-06-14 15:13:04 -0700107 [](const dex_ir::Header* h) { return h->TypeLists().Size(); },
108 [](const dex_ir::Header* h) { return h->TypeLists().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -0700109 }, {
110 "EncArr",
111 DexFile::kDexTypeEncodedArrayItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700112 [](const dex_ir::Header* h) { return h->EncodedArrayItems().Size(); },
113 [](const dex_ir::Header* h) { return h->EncodedArrayItems().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -0700114 }, {
115 "Annotation",
116 DexFile::kDexTypeAnnotationItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700117 [](const dex_ir::Header* h) { return h->AnnotationItems().Size(); },
118 [](const dex_ir::Header* h) { return h->AnnotationItems().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -0700119 }, {
120 "AnnoSet",
121 DexFile::kDexTypeAnnotationSetItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700122 [](const dex_ir::Header* h) { return h->AnnotationSetItems().Size(); },
123 [](const dex_ir::Header* h) { return h->AnnotationSetItems().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -0700124 }, {
125 "AnnoSetRL",
126 DexFile::kDexTypeAnnotationSetRefList,
David Sehr2b5a38f2018-06-14 15:13:04 -0700127 [](const dex_ir::Header* h) { return h->AnnotationSetRefLists().Size(); },
128 [](const dex_ir::Header* h) { return h->AnnotationSetRefLists().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -0700129 }, {
130 "AnnoDir",
131 DexFile::kDexTypeAnnotationsDirectoryItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700132 [](const dex_ir::Header* h) { return h->AnnotationsDirectoryItems().Size(); },
133 [](const dex_ir::Header* h) { return h->AnnotationsDirectoryItems().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -0700134 }, {
135 "DebugInfo",
136 DexFile::kDexTypeDebugInfoItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700137 [](const dex_ir::Header* h) { return h->DebugInfoItems().Size(); },
138 [](const dex_ir::Header* h) { return h->DebugInfoItems().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -0700139 }, {
140 "CodeItem",
141 DexFile::kDexTypeCodeItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700142 [](const dex_ir::Header* h) { return h->CodeItems().Size(); },
143 [](const dex_ir::Header* h) { return h->CodeItems().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -0700144 }, {
145 "ClassData",
146 DexFile::kDexTypeClassDataItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700147 [](const dex_ir::Header* h) { return h->ClassDatas().Size(); },
148 [](const dex_ir::Header* h) { return h->ClassDatas().GetOffset(); }
David Sehr9037a3a2017-03-30 17:50:24 -0700149 }
150};
151
152std::vector<dex_ir::DexFileSection> GetSortedDexFileSections(dex_ir::Header* header,
153 dex_ir::SortDirection direction) {
David Sehr9037a3a2017-03-30 17:50:24 -0700154 std::vector<dex_ir::DexFileSection> sorted_sections;
155 // Build the table that will map from offset to color
156 for (const FileSectionDescriptor& s : kFileSectionDescriptors) {
157 sorted_sections.push_back(dex_ir::DexFileSection(s.name,
158 s.type,
David Sehr2b5a38f2018-06-14 15:13:04 -0700159 s.size_fn(header),
160 s.offset_fn(header)));
David Sehr9037a3a2017-03-30 17:50:24 -0700161 }
162 // Sort by offset.
163 std::sort(sorted_sections.begin(),
164 sorted_sections.end(),
165 [=](dex_ir::DexFileSection& a, dex_ir::DexFileSection& b) {
166 if (direction == SortDirection::kSortDescending) {
167 return a.offset > b.offset;
168 } else {
169 return a.offset < b.offset;
170 }
171 });
172 return sorted_sections;
173}
174
Jeff Hao3ab96b42016-09-09 18:35:01 -0700175} // namespace dex_ir
176} // namespace art