Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | #include "compact_dex_writer.h" |
| 18 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
| 20 | #include "base/time_utils.h" |
| 21 | #include "dex/compact_dex_debug_info.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 22 | #include "dex/compact_dex_file.h" |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 23 | #include "dexlayout.h" |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 27 | CompactDexWriter::CompactDexWriter(DexLayout* dex_layout) |
| 28 | : DexWriter(dex_layout, /*compute_offsets*/ true) { |
| 29 | CHECK(GetCompactDexLevel() != CompactDexLevel::kCompactDexLevelNone); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 32 | CompactDexLevel CompactDexWriter::GetCompactDexLevel() const { |
| 33 | return dex_layout_->GetOptions().compact_dex_level_; |
| 34 | } |
| 35 | |
| 36 | CompactDexWriter::Container::Container(bool dedupe_code_items) |
| 37 | : code_item_dedupe_(dedupe_code_items, &data_section_) {} |
| 38 | |
| 39 | uint32_t CompactDexWriter::WriteDebugInfoOffsetTable(Stream* stream) { |
| 40 | const uint32_t start_offset = stream->Tell(); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 41 | const dex_ir::Collections& collections = header_->GetCollections(); |
| 42 | // Debug offsets for method indexes. 0 means no debug info. |
| 43 | std::vector<uint32_t> debug_info_offsets(collections.MethodIdsSize(), 0u); |
| 44 | |
| 45 | static constexpr InvokeType invoke_types[] = { |
| 46 | kDirect, |
| 47 | kVirtual |
| 48 | }; |
| 49 | |
| 50 | for (InvokeType invoke_type : invoke_types) { |
| 51 | for (const std::unique_ptr<dex_ir::ClassDef>& class_def : collections.ClassDefs()) { |
| 52 | // Skip classes that are not defined in this dex file. |
| 53 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 54 | if (class_data == nullptr) { |
| 55 | continue; |
| 56 | } |
| 57 | for (auto& method : *(invoke_type == InvokeType::kDirect |
| 58 | ? class_data->DirectMethods() |
| 59 | : class_data->VirtualMethods())) { |
| 60 | const dex_ir::MethodId* method_id = method->GetMethodId(); |
| 61 | dex_ir::CodeItem* code_item = method->GetCodeItem(); |
| 62 | if (code_item != nullptr && code_item->DebugInfo() != nullptr) { |
| 63 | const uint32_t debug_info_offset = code_item->DebugInfo()->GetOffset(); |
| 64 | const uint32_t method_idx = method_id->GetIndex(); |
| 65 | if (debug_info_offsets[method_idx] != 0u) { |
| 66 | CHECK_EQ(debug_info_offset, debug_info_offsets[method_idx]); |
| 67 | } |
| 68 | debug_info_offsets[method_idx] = debug_info_offset; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | std::vector<uint8_t> data; |
| 75 | debug_info_base_ = 0u; |
| 76 | debug_info_offsets_table_offset_ = 0u; |
| 77 | CompactDexDebugInfoOffsetTable::Build(debug_info_offsets, |
| 78 | &data, |
| 79 | &debug_info_base_, |
| 80 | &debug_info_offsets_table_offset_); |
| 81 | // Align the table and write it out. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 82 | stream->AlignTo(CompactDexDebugInfoOffsetTable::kAlignment); |
| 83 | debug_info_offsets_pos_ = stream->Tell(); |
| 84 | stream->Write(data.data(), data.size()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 85 | |
| 86 | // Verify that the whole table decodes as expected and measure average performance. |
| 87 | const bool kMeasureAndTestOutput = dex_layout_->GetOptions().verify_output_; |
| 88 | if (kMeasureAndTestOutput && !debug_info_offsets.empty()) { |
| 89 | uint64_t start_time = NanoTime(); |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 90 | stream->Begin(); |
| 91 | CompactDexDebugInfoOffsetTable::Accessor accessor(stream->Begin() + debug_info_offsets_pos_, |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 92 | debug_info_base_, |
| 93 | debug_info_offsets_table_offset_); |
| 94 | |
| 95 | for (size_t i = 0; i < debug_info_offsets.size(); ++i) { |
| 96 | CHECK_EQ(accessor.GetDebugInfoOffset(i), debug_info_offsets[i]); |
| 97 | } |
| 98 | uint64_t end_time = NanoTime(); |
| 99 | VLOG(dex) << "Average lookup time (ns) for debug info offsets: " |
| 100 | << (end_time - start_time) / debug_info_offsets.size(); |
| 101 | } |
| 102 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 103 | return stream->Tell() - start_offset; |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 106 | uint32_t CompactDexWriter::WriteCodeItem(Stream* stream, |
| 107 | dex_ir::CodeItem* code_item, |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 108 | bool reserve_only) { |
| 109 | DCHECK(code_item != nullptr); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 110 | DCHECK(!reserve_only) << "Not supported because of deduping."; |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 111 | const uint32_t start_offset = stream->Tell(); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 112 | |
| 113 | // Align to minimum requirements, additional alignment requirements are handled below after we |
| 114 | // know the preheader size. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 115 | stream->AlignTo(CompactDexFile::CodeItem::kAlignment); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 116 | |
| 117 | CompactDexFile::CodeItem disk_code_item; |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 118 | |
| 119 | uint16_t preheader_storage[CompactDexFile::CodeItem::kMaxPreHeaderSize] = {}; |
| 120 | uint16_t* preheader_end = preheader_storage + CompactDexFile::CodeItem::kMaxPreHeaderSize; |
| 121 | const uint16_t* preheader = disk_code_item.Create( |
| 122 | code_item->RegistersSize(), |
| 123 | code_item->InsSize(), |
| 124 | code_item->OutsSize(), |
| 125 | code_item->TriesSize(), |
| 126 | code_item->InsnsSize(), |
| 127 | preheader_end); |
| 128 | const size_t preheader_bytes = (preheader_end - preheader) * sizeof(preheader[0]); |
| 129 | |
| 130 | static constexpr size_t kPayloadInstructionRequiredAlignment = 4; |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 131 | const uint32_t current_code_item_start = stream->Tell() + preheader_bytes; |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 132 | if (!IsAlignedParam(current_code_item_start, kPayloadInstructionRequiredAlignment)) { |
| 133 | // If the preheader is going to make the code unaligned, consider adding 2 bytes of padding |
| 134 | // before if required. |
| 135 | for (const DexInstructionPcPair& instruction : code_item->Instructions()) { |
| 136 | const Instruction::Code opcode = instruction->Opcode(); |
| 137 | // Payload instructions possibly require special alignment for their data. |
| 138 | if (opcode == Instruction::FILL_ARRAY_DATA || |
| 139 | opcode == Instruction::PACKED_SWITCH || |
| 140 | opcode == Instruction::SPARSE_SWITCH) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 141 | stream->Skip( |
| 142 | RoundUp(current_code_item_start, kPayloadInstructionRequiredAlignment) - |
| 143 | current_code_item_start); |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 149 | const uint32_t data_start = stream->Tell(); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 150 | |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 151 | // Write preheader first. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 152 | stream->Write(reinterpret_cast<const uint8_t*>(preheader), preheader_bytes); |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 153 | // Registered offset is after the preheader. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 154 | ProcessOffset(stream, code_item); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 155 | // Avoid using sizeof so that we don't write the fake instruction array at the end of the code |
| 156 | // item. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 157 | stream->Write(&disk_code_item, OFFSETOF_MEMBER(CompactDexFile::CodeItem, insns_)); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 158 | // Write the instructions. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 159 | stream->Write(code_item->Insns(), code_item->InsnsSize() * sizeof(uint16_t)); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 160 | // Write the post instruction data. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 161 | WriteCodeItemPostInstructionData(stream, code_item, reserve_only); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 162 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 163 | if (compute_offsets_) { |
| 164 | // After having written, maybe dedupe the whole code item (excluding padding). |
| 165 | const uint32_t deduped_offset = code_item_dedupe_->Dedupe(data_start, |
| 166 | stream->Tell(), |
| 167 | code_item->GetOffset()); |
| 168 | if (deduped_offset != Deduper::kDidNotDedupe) { |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 169 | code_item->SetOffset(deduped_offset); |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 170 | stream->Clear(start_offset, stream->Tell() - start_offset); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 171 | // Undo the offset for all that we wrote since we deduped. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 172 | stream->Seek(start_offset); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 176 | return stream->Tell() - start_offset; |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 179 | |
| 180 | CompactDexWriter::Deduper::Deduper(bool enabled, DexContainer::Section* section) |
| 181 | : enabled_(enabled), |
| 182 | dedupe_map_(/*bucket_count*/ 32, |
| 183 | HashedMemoryRange::HashEqual(section), |
| 184 | HashedMemoryRange::HashEqual(section)) {} |
| 185 | |
| 186 | uint32_t CompactDexWriter::Deduper::Dedupe(uint32_t data_start, |
| 187 | uint32_t data_end, |
| 188 | uint32_t item_offset) { |
| 189 | if (!enabled_) { |
| 190 | return kDidNotDedupe; |
| 191 | } |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 192 | HashedMemoryRange range {data_start, data_end - data_start}; |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 193 | auto existing = dedupe_map_.emplace(range, item_offset); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 194 | if (!existing.second) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 195 | // Failed to insert means we deduped, return the existing item offset. |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 196 | return existing.first->second; |
| 197 | } |
| 198 | return kDidNotDedupe; |
| 199 | } |
| 200 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 201 | void CompactDexWriter::SortDebugInfosByMethodIndex() { |
| 202 | dex_ir::Collections& collections = header_->GetCollections(); |
| 203 | static constexpr InvokeType invoke_types[] = { |
| 204 | kDirect, |
| 205 | kVirtual |
| 206 | }; |
| 207 | std::map<const dex_ir::DebugInfoItem*, uint32_t> method_idx_map; |
| 208 | for (InvokeType invoke_type : invoke_types) { |
| 209 | for (std::unique_ptr<dex_ir::ClassDef>& class_def : collections.ClassDefs()) { |
| 210 | // Skip classes that are not defined in this dex file. |
| 211 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 212 | if (class_data == nullptr) { |
| 213 | continue; |
| 214 | } |
| 215 | for (auto& method : *(invoke_type == InvokeType::kDirect |
| 216 | ? class_data->DirectMethods() |
| 217 | : class_data->VirtualMethods())) { |
| 218 | const dex_ir::MethodId* method_id = method->GetMethodId(); |
| 219 | dex_ir::CodeItem* code_item = method->GetCodeItem(); |
| 220 | if (code_item != nullptr && code_item->DebugInfo() != nullptr) { |
| 221 | const dex_ir::DebugInfoItem* debug_item = code_item->DebugInfo(); |
| 222 | method_idx_map.insert(std::make_pair(debug_item, method_id->GetIndex())); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | std::sort(collections.DebugInfoItems().begin(), |
| 228 | collections.DebugInfoItems().end(), |
| 229 | [&](const std::unique_ptr<dex_ir::DebugInfoItem>& a, |
| 230 | const std::unique_ptr<dex_ir::DebugInfoItem>& b) { |
| 231 | auto it_a = method_idx_map.find(a.get()); |
| 232 | auto it_b = method_idx_map.find(b.get()); |
| 233 | uint32_t idx_a = it_a != method_idx_map.end() ? it_a->second : 0u; |
| 234 | uint32_t idx_b = it_b != method_idx_map.end() ? it_b->second : 0u; |
| 235 | return idx_a < idx_b; |
| 236 | }); |
| 237 | } |
| 238 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 239 | void CompactDexWriter::WriteHeader(Stream* stream) { |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 240 | CompactDexFile::Header header; |
| 241 | CompactDexFile::WriteMagic(&header.magic_[0]); |
| 242 | CompactDexFile::WriteCurrentVersion(&header.magic_[0]); |
| 243 | header.checksum_ = header_->Checksum(); |
| 244 | std::copy_n(header_->Signature(), DexFile::kSha1DigestSize, header.signature_); |
| 245 | header.file_size_ = header_->FileSize(); |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 246 | // Since we are not necessarily outputting the same format as the input, avoid using the stored |
| 247 | // header size. |
| 248 | header.header_size_ = GetHeaderSize(); |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 249 | header.endian_tag_ = header_->EndianTag(); |
| 250 | header.link_size_ = header_->LinkSize(); |
| 251 | header.link_off_ = header_->LinkOffset(); |
| 252 | const dex_ir::Collections& collections = header_->GetCollections(); |
| 253 | header.map_off_ = collections.MapListOffset(); |
| 254 | header.string_ids_size_ = collections.StringIdsSize(); |
| 255 | header.string_ids_off_ = collections.StringIdsOffset(); |
| 256 | header.type_ids_size_ = collections.TypeIdsSize(); |
| 257 | header.type_ids_off_ = collections.TypeIdsOffset(); |
| 258 | header.proto_ids_size_ = collections.ProtoIdsSize(); |
| 259 | header.proto_ids_off_ = collections.ProtoIdsOffset(); |
| 260 | header.field_ids_size_ = collections.FieldIdsSize(); |
| 261 | header.field_ids_off_ = collections.FieldIdsOffset(); |
| 262 | header.method_ids_size_ = collections.MethodIdsSize(); |
| 263 | header.method_ids_off_ = collections.MethodIdsOffset(); |
| 264 | header.class_defs_size_ = collections.ClassDefsSize(); |
| 265 | header.class_defs_off_ = collections.ClassDefsOffset(); |
| 266 | header.data_size_ = header_->DataSize(); |
| 267 | header.data_off_ = header_->DataOffset(); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 268 | |
| 269 | // Compact dex specific flags. |
| 270 | header.debug_info_offsets_pos_ = debug_info_offsets_pos_; |
| 271 | header.debug_info_offsets_table_offset_ = debug_info_offsets_table_offset_; |
| 272 | header.debug_info_base_ = debug_info_base_; |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 273 | header.feature_flags_ = 0u; |
| 274 | // In cases where apps are converted to cdex during install, maintain feature flags so that |
| 275 | // the verifier correctly verifies apps that aren't targetting default methods. |
| 276 | if (header_->SupportDefaultMethods()) { |
| 277 | header.feature_flags_ |= static_cast<uint32_t>(CompactDexFile::FeatureFlags::kDefaultMethods); |
| 278 | } |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 279 | stream->Seek(0); |
| 280 | stream->Overwrite(reinterpret_cast<uint8_t*>(&header), sizeof(header)); |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 283 | size_t CompactDexWriter::GetHeaderSize() const { |
| 284 | return sizeof(CompactDexFile::Header); |
| 285 | } |
| 286 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 287 | void CompactDexWriter::Write(DexContainer* output) { |
Mathieu Chartier | 9b302bf | 2018-01-25 13:08:08 -0800 | [diff] [blame^] | 288 | CHECK(compute_offsets_); |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 289 | CHECK(output->IsCompactDexContainer()); |
| 290 | Container* const container = down_cast<Container*>(output); |
| 291 | // For now, use the same stream for both data and metadata. |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 292 | Stream temp_main_stream(output->GetMainSection()); |
| 293 | Stream temp_data_stream(output->GetDataSection()); |
| 294 | Stream* main_stream = &temp_main_stream; |
| 295 | Stream* data_stream = &temp_data_stream; |
| 296 | |
| 297 | // We want offset 0 to be reserved for null, seek to the data section alignment or the end of the |
| 298 | // section. |
| 299 | data_stream->Seek(std::max( |
| 300 | static_cast<uint32_t>(output->GetDataSection()->Size()), |
| 301 | kDataSectionAlignment)); |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 302 | code_item_dedupe_ = &container->code_item_dedupe_; |
| 303 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 304 | // Starting offset is right after the header. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 305 | main_stream->Seek(GetHeaderSize()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 306 | |
| 307 | dex_ir::Collections& collection = header_->GetCollections(); |
| 308 | |
| 309 | // Based on: https://source.android.com/devices/tech/dalvik/dex-format |
| 310 | // Since the offsets may not be calculated already, the writing must be done in the correct order. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 311 | const uint32_t string_ids_offset = main_stream->Tell(); |
| 312 | WriteStringIds(main_stream, /*reserve_only*/ true); |
| 313 | WriteTypeIds(main_stream); |
| 314 | const uint32_t proto_ids_offset = main_stream->Tell(); |
| 315 | WriteProtoIds(main_stream, /*reserve_only*/ true); |
| 316 | WriteFieldIds(main_stream); |
| 317 | WriteMethodIds(main_stream); |
| 318 | const uint32_t class_defs_offset = main_stream->Tell(); |
| 319 | WriteClassDefs(main_stream, /*reserve_only*/ true); |
| 320 | const uint32_t call_site_ids_offset = main_stream->Tell(); |
| 321 | WriteCallSiteIds(main_stream, /*reserve_only*/ true); |
| 322 | WriteMethodHandles(main_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 323 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 324 | if (compute_offsets_) { |
| 325 | // Data section. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 326 | data_stream->AlignTo(kDataSectionAlignment); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Write code item first to minimize the space required for encoded methods. |
| 330 | // For cdex, the code items don't depend on the debug info. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 331 | WriteCodeItems(data_stream, /*reserve_only*/ false); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 332 | |
| 333 | // Sort the debug infos by method index order, this reduces size by ~0.1% by reducing the size of |
| 334 | // the debug info offset table. |
| 335 | SortDebugInfosByMethodIndex(); |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 336 | WriteDebugInfoItems(data_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 337 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 338 | WriteEncodedArrays(data_stream); |
| 339 | WriteAnnotations(data_stream); |
| 340 | WriteAnnotationSets(data_stream); |
| 341 | WriteAnnotationSetRefs(data_stream); |
| 342 | WriteAnnotationsDirectories(data_stream); |
| 343 | WriteTypeLists(data_stream); |
| 344 | WriteClassDatas(data_stream); |
| 345 | WriteStringDatas(data_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 346 | |
| 347 | // Write delayed id sections that depend on data sections. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 348 | { |
| 349 | Stream::ScopedSeek seek(main_stream, string_ids_offset); |
| 350 | WriteStringIds(main_stream, /*reserve_only*/ false); |
| 351 | } |
| 352 | { |
| 353 | Stream::ScopedSeek seek(main_stream, proto_ids_offset); |
| 354 | WriteProtoIds(main_stream, /*reserve_only*/ false); |
| 355 | } |
| 356 | { |
| 357 | Stream::ScopedSeek seek(main_stream, class_defs_offset); |
| 358 | WriteClassDefs(main_stream, /*reserve_only*/ false); |
| 359 | } |
| 360 | { |
| 361 | Stream::ScopedSeek seek(main_stream, call_site_ids_offset); |
| 362 | WriteCallSiteIds(main_stream, /*reserve_only*/ false); |
| 363 | } |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 364 | |
| 365 | // Write the map list. |
| 366 | if (compute_offsets_) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 367 | data_stream->AlignTo(SectionAlignment(DexFile::kDexTypeMapList)); |
| 368 | collection.SetMapListOffset(data_stream->Tell()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 369 | } else { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 370 | data_stream->Seek(collection.MapListOffset()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 371 | } |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 372 | |
| 373 | // Map items are included in the data section. |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 374 | GenerateAndWriteMapItems(data_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 375 | |
| 376 | // Write link data if it exists. |
| 377 | const std::vector<uint8_t>& link_data = collection.LinkData(); |
| 378 | if (link_data.size() > 0) { |
| 379 | CHECK_EQ(header_->LinkSize(), static_cast<uint32_t>(link_data.size())); |
| 380 | if (compute_offsets_) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 381 | header_->SetLinkOffset(data_stream->Tell()); |
| 382 | } else { |
| 383 | data_stream->Seek(header_->LinkOffset()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 384 | } |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 385 | data_stream->Write(&link_data[0], link_data.size()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // Write debug info offset table last to make dex file verifier happy. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 389 | WriteDebugInfoOffsetTable(data_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 390 | |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 391 | data_stream->AlignTo(kDataSectionAlignment); |
| 392 | if (compute_offsets_) { |
| 393 | header_->SetDataSize(data_stream->Tell()); |
| 394 | if (header_->DataSize() != 0) { |
| 395 | // Offset must be zero when the size is zero. |
| 396 | main_stream->AlignTo(kDataSectionAlignment); |
| 397 | // For now, default to saying the data is right after the main stream. |
| 398 | header_->SetDataOffset(main_stream->Tell()); |
| 399 | header_->SetDataOffset(0u); |
| 400 | } else { |
| 401 | header_->SetDataOffset(0u); |
| 402 | } |
| 403 | } |
| 404 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 405 | // Write header last. |
| 406 | if (compute_offsets_) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 407 | header_->SetFileSize(main_stream->Tell()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 408 | } |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 409 | WriteHeader(main_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 410 | |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 411 | // Trim sections to make sure they are sized properly. |
| 412 | output->GetMainSection()->Resize(header_->FileSize()); |
| 413 | output->GetDataSection()->Resize(data_stream->Tell()); |
| 414 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 415 | if (dex_layout_->GetOptions().update_checksum_) { |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 416 | // Compute the cdex section (also covers the used part of the data section). |
| 417 | header_->SetChecksum(CompactDexFile::CalculateChecksum(output->GetMainSection()->Begin(), |
| 418 | output->GetMainSection()->Size(), |
| 419 | output->GetDataSection()->Begin(), |
| 420 | output->GetDataSection()->Size())); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 421 | // Rewrite the header with the calculated checksum. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 422 | WriteHeader(main_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 423 | } |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | std::unique_ptr<DexContainer> CompactDexWriter::CreateDexContainer() const { |
| 427 | return std::unique_ptr<DexContainer>( |
| 428 | new CompactDexWriter::Container(dex_layout_->GetOptions().dedupe_code_items_)); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 431 | } // namespace art |