Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -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 | * 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 Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 24 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 25 | #include "dex/code_item_accessors-inl.h" |
| 26 | #include "dex/dex_file_exception_helpers.h" |
| 27 | #include "dex/dex_instruction-inl.h" |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 28 | #include "dex_ir_builder.h" |
| 29 | |
| 30 | namespace art { |
| 31 | namespace dex_ir { |
| 32 | |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 33 | static uint32_t HeaderOffset(const dex_ir::Header* header ATTRIBUTE_UNUSED) { |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 37 | static uint32_t HeaderSize(const dex_ir::Header* header ATTRIBUTE_UNUSED) { |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 38 | // Size is in elements, so there is only one header. |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | // The description of each dex file section type. |
| 43 | struct 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 Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 48 | std::function<uint32_t(dex_ir::Header*)> size_fn; |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 49 | // A function that when applied to a collection object, gives the offset of the section. |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 50 | std::function<uint32_t(dex_ir::Header*)> offset_fn; |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
David Sehr | 7639cdc | 2017-04-15 10:06:21 -0700 | [diff] [blame] | 53 | static const FileSectionDescriptor kFileSectionDescriptors[] = { |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 54 | { |
| 55 | "Header", |
| 56 | DexFile::kDexTypeHeaderItem, |
| 57 | &HeaderSize, |
| 58 | &HeaderOffset, |
| 59 | }, { |
| 60 | "StringId", |
| 61 | DexFile::kDexTypeStringIdItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 62 | [](const dex_ir::Header* h) { return h->StringIds().Size(); }, |
| 63 | [](const dex_ir::Header* h) { return h->StringIds().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 64 | }, { |
| 65 | "TypeId", |
| 66 | DexFile::kDexTypeTypeIdItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 67 | [](const dex_ir::Header* h) { return h->TypeIds().Size(); }, |
| 68 | [](const dex_ir::Header* h) { return h->TypeIds().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 69 | }, { |
| 70 | "ProtoId", |
| 71 | DexFile::kDexTypeProtoIdItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 72 | [](const dex_ir::Header* h) { return h->ProtoIds().Size(); }, |
| 73 | [](const dex_ir::Header* h) { return h->ProtoIds().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 74 | }, { |
| 75 | "FieldId", |
| 76 | DexFile::kDexTypeFieldIdItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 77 | [](const dex_ir::Header* h) { return h->FieldIds().Size(); }, |
| 78 | [](const dex_ir::Header* h) { return h->FieldIds().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 79 | }, { |
| 80 | "MethodId", |
| 81 | DexFile::kDexTypeMethodIdItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 82 | [](const dex_ir::Header* h) { return h->MethodIds().Size(); }, |
| 83 | [](const dex_ir::Header* h) { return h->MethodIds().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 84 | }, { |
| 85 | "ClassDef", |
| 86 | DexFile::kDexTypeClassDefItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 87 | [](const dex_ir::Header* h) { return h->ClassDefs().Size(); }, |
| 88 | [](const dex_ir::Header* h) { return h->ClassDefs().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 89 | }, { |
Jeff Hao | 5daee90 | 2017-04-27 18:00:38 -0700 | [diff] [blame] | 90 | "CallSiteId", |
| 91 | DexFile::kDexTypeCallSiteIdItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 92 | [](const dex_ir::Header* h) { return h->CallSiteIds().Size(); }, |
| 93 | [](const dex_ir::Header* h) { return h->CallSiteIds().GetOffset(); } |
Jeff Hao | 5daee90 | 2017-04-27 18:00:38 -0700 | [diff] [blame] | 94 | }, { |
| 95 | "MethodHandle", |
| 96 | DexFile::kDexTypeMethodHandleItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 97 | [](const dex_ir::Header* h) { return h->MethodHandleItems().Size(); }, |
| 98 | [](const dex_ir::Header* h) { return h->MethodHandleItems().GetOffset(); } |
Jeff Hao | 5daee90 | 2017-04-27 18:00:38 -0700 | [diff] [blame] | 99 | }, { |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 100 | "StringData", |
| 101 | DexFile::kDexTypeStringDataItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 102 | [](const dex_ir::Header* h) { return h->StringDatas().Size(); }, |
| 103 | [](const dex_ir::Header* h) { return h->StringDatas().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 104 | }, { |
| 105 | "TypeList", |
| 106 | DexFile::kDexTypeTypeList, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 107 | [](const dex_ir::Header* h) { return h->TypeLists().Size(); }, |
| 108 | [](const dex_ir::Header* h) { return h->TypeLists().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 109 | }, { |
| 110 | "EncArr", |
| 111 | DexFile::kDexTypeEncodedArrayItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 112 | [](const dex_ir::Header* h) { return h->EncodedArrayItems().Size(); }, |
| 113 | [](const dex_ir::Header* h) { return h->EncodedArrayItems().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 114 | }, { |
| 115 | "Annotation", |
| 116 | DexFile::kDexTypeAnnotationItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 117 | [](const dex_ir::Header* h) { return h->AnnotationItems().Size(); }, |
| 118 | [](const dex_ir::Header* h) { return h->AnnotationItems().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 119 | }, { |
| 120 | "AnnoSet", |
| 121 | DexFile::kDexTypeAnnotationSetItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 122 | [](const dex_ir::Header* h) { return h->AnnotationSetItems().Size(); }, |
| 123 | [](const dex_ir::Header* h) { return h->AnnotationSetItems().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 124 | }, { |
| 125 | "AnnoSetRL", |
| 126 | DexFile::kDexTypeAnnotationSetRefList, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 127 | [](const dex_ir::Header* h) { return h->AnnotationSetRefLists().Size(); }, |
| 128 | [](const dex_ir::Header* h) { return h->AnnotationSetRefLists().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 129 | }, { |
| 130 | "AnnoDir", |
| 131 | DexFile::kDexTypeAnnotationsDirectoryItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 132 | [](const dex_ir::Header* h) { return h->AnnotationsDirectoryItems().Size(); }, |
| 133 | [](const dex_ir::Header* h) { return h->AnnotationsDirectoryItems().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 134 | }, { |
| 135 | "DebugInfo", |
| 136 | DexFile::kDexTypeDebugInfoItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 137 | [](const dex_ir::Header* h) { return h->DebugInfoItems().Size(); }, |
| 138 | [](const dex_ir::Header* h) { return h->DebugInfoItems().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 139 | }, { |
| 140 | "CodeItem", |
| 141 | DexFile::kDexTypeCodeItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 142 | [](const dex_ir::Header* h) { return h->CodeItems().Size(); }, |
| 143 | [](const dex_ir::Header* h) { return h->CodeItems().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 144 | }, { |
| 145 | "ClassData", |
| 146 | DexFile::kDexTypeClassDataItem, |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 147 | [](const dex_ir::Header* h) { return h->ClassDatas().Size(); }, |
| 148 | [](const dex_ir::Header* h) { return h->ClassDatas().GetOffset(); } |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 149 | } |
| 150 | }; |
| 151 | |
| 152 | std::vector<dex_ir::DexFileSection> GetSortedDexFileSections(dex_ir::Header* header, |
| 153 | dex_ir::SortDirection direction) { |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 154 | 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 Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 159 | s.size_fn(header), |
| 160 | s.offset_fn(header))); |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 161 | } |
| 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 Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 175 | } // namespace dex_ir |
| 176 | } // namespace art |