blob: 0580f16b9f88e57b3d7feec88efaa23735354127 [file] [log] [blame]
Mathieu Chartierf95a75e2017-11-03 15:25:52 -07001/*
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 Chartier8892c6b2018-01-09 15:10:17 -080019#include "base/logging.h"
20#include "base/time_utils.h"
21#include "dex/compact_dex_debug_info.h"
David Sehr9e734c72018-01-04 17:56:19 -080022#include "dex/compact_dex_file.h"
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080023#include "dexlayout.h"
Mathieu Chartierf95a75e2017-11-03 15:25:52 -070024
25namespace art {
26
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080027CompactDexWriter::CompactDexWriter(DexLayout* dex_layout)
28 : DexWriter(dex_layout, /*compute_offsets*/ true) {
29 CHECK(GetCompactDexLevel() != CompactDexLevel::kCompactDexLevelNone);
Mathieu Chartier66c9df12018-01-16 14:45:20 -080030}
31
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080032CompactDexLevel CompactDexWriter::GetCompactDexLevel() const {
33 return dex_layout_->GetOptions().compact_dex_level_;
34}
35
36CompactDexWriter::Container::Container(bool dedupe_code_items)
37 : code_item_dedupe_(dedupe_code_items, &data_section_) {}
38
39uint32_t CompactDexWriter::WriteDebugInfoOffsetTable(Stream* stream) {
40 const uint32_t start_offset = stream->Tell();
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080041 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 Chartiere6b6ff82018-01-19 18:58:34 -080082 stream->AlignTo(CompactDexDebugInfoOffsetTable::kAlignment);
83 debug_info_offsets_pos_ = stream->Tell();
84 stream->Write(data.data(), data.size());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080085
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 Chartiere6b6ff82018-01-19 18:58:34 -080090 stream->Begin();
91 CompactDexDebugInfoOffsetTable::Accessor accessor(stream->Begin() + debug_info_offsets_pos_,
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080092 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 Chartiere6b6ff82018-01-19 18:58:34 -0800103 return stream->Tell() - start_offset;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800104}
105
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800106uint32_t CompactDexWriter::WriteCodeItem(Stream* stream,
107 dex_ir::CodeItem* code_item,
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800108 bool reserve_only) {
109 DCHECK(code_item != nullptr);
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800110 DCHECK(!reserve_only) << "Not supported because of deduping.";
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800111 const uint32_t start_offset = stream->Tell();
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800112
113 // Align to minimum requirements, additional alignment requirements are handled below after we
114 // know the preheader size.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800115 stream->AlignTo(CompactDexFile::CodeItem::kAlignment);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800116
117 CompactDexFile::CodeItem disk_code_item;
Mathieu Chartier8740c662018-01-11 14:50:02 -0800118
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 Chartiere6b6ff82018-01-19 18:58:34 -0800131 const uint32_t current_code_item_start = stream->Tell() + preheader_bytes;
Mathieu Chartier8740c662018-01-11 14:50:02 -0800132 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 Chartiere6b6ff82018-01-19 18:58:34 -0800141 stream->Skip(
142 RoundUp(current_code_item_start, kPayloadInstructionRequiredAlignment) -
143 current_code_item_start);
Mathieu Chartier8740c662018-01-11 14:50:02 -0800144 break;
145 }
146 }
147 }
148
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800149 const uint32_t data_start = stream->Tell();
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800150
Mathieu Chartier8740c662018-01-11 14:50:02 -0800151 // Write preheader first.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800152 stream->Write(reinterpret_cast<const uint8_t*>(preheader), preheader_bytes);
Mathieu Chartier8740c662018-01-11 14:50:02 -0800153 // Registered offset is after the preheader.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800154 ProcessOffset(stream, code_item);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800155 // Avoid using sizeof so that we don't write the fake instruction array at the end of the code
156 // item.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800157 stream->Write(&disk_code_item, OFFSETOF_MEMBER(CompactDexFile::CodeItem, insns_));
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800158 // Write the instructions.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800159 stream->Write(code_item->Insns(), code_item->InsnsSize() * sizeof(uint16_t));
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800160 // Write the post instruction data.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800161 WriteCodeItemPostInstructionData(stream, code_item, reserve_only);
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800162
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800163 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 Chartier66c9df12018-01-16 14:45:20 -0800169 code_item->SetOffset(deduped_offset);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800170 stream->Clear(start_offset, stream->Tell() - start_offset);
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800171 // Undo the offset for all that we wrote since we deduped.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800172 stream->Seek(start_offset);
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800173 }
174 }
175
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800176 return stream->Tell() - start_offset;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800177}
178
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800179
180CompactDexWriter::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
186uint32_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 Chartier66c9df12018-01-16 14:45:20 -0800192 HashedMemoryRange range {data_start, data_end - data_start};
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800193 auto existing = dedupe_map_.emplace(range, item_offset);
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800194 if (!existing.second) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800195 // Failed to insert means we deduped, return the existing item offset.
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800196 return existing.first->second;
197 }
198 return kDidNotDedupe;
199}
200
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800201void 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 Chartiere6b6ff82018-01-19 18:58:34 -0800239void CompactDexWriter::WriteHeader(Stream* stream) {
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700240 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 Chartierf6e31472017-12-28 13:32:08 -0800246 // 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 Chartierf95a75e2017-11-03 15:25:52 -0700249 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 Chartier8892c6b2018-01-09 15:10:17 -0800268
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 Chartierf6e31472017-12-28 13:32:08 -0800273 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 Chartiere6b6ff82018-01-19 18:58:34 -0800279 stream->Seek(0);
280 stream->Overwrite(reinterpret_cast<uint8_t*>(&header), sizeof(header));
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700281}
282
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800283size_t CompactDexWriter::GetHeaderSize() const {
284 return sizeof(CompactDexFile::Header);
285}
286
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800287void CompactDexWriter::Write(DexContainer* output) {
Mathieu Chartier9b302bf2018-01-25 13:08:08 -0800288 CHECK(compute_offsets_);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800289 CHECK(output->IsCompactDexContainer());
290 Container* const container = down_cast<Container*>(output);
291 // For now, use the same stream for both data and metadata.
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800292 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 Chartiere6b6ff82018-01-19 18:58:34 -0800302 code_item_dedupe_ = &container->code_item_dedupe_;
303
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800304 // Starting offset is right after the header.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800305 main_stream->Seek(GetHeaderSize());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800306
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 Chartiere6b6ff82018-01-19 18:58:34 -0800311 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 Chartier8892c6b2018-01-09 15:10:17 -0800323
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800324 if (compute_offsets_) {
325 // Data section.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800326 data_stream->AlignTo(kDataSectionAlignment);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800327 }
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 Chartiere6b6ff82018-01-19 18:58:34 -0800331 WriteCodeItems(data_stream, /*reserve_only*/ false);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800332
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 Chartiere6b6ff82018-01-19 18:58:34 -0800336 WriteDebugInfoItems(data_stream);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800337
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800338 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 Chartier8892c6b2018-01-09 15:10:17 -0800346
347 // Write delayed id sections that depend on data sections.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800348 {
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 Chartier8892c6b2018-01-09 15:10:17 -0800364
365 // Write the map list.
366 if (compute_offsets_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800367 data_stream->AlignTo(SectionAlignment(DexFile::kDexTypeMapList));
368 collection.SetMapListOffset(data_stream->Tell());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800369 } else {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800370 data_stream->Seek(collection.MapListOffset());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800371 }
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800372
373 // Map items are included in the data section.
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800374 GenerateAndWriteMapItems(data_stream);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800375
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 Chartiere6b6ff82018-01-19 18:58:34 -0800381 header_->SetLinkOffset(data_stream->Tell());
382 } else {
383 data_stream->Seek(header_->LinkOffset());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800384 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800385 data_stream->Write(&link_data[0], link_data.size());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800386 }
387
388 // Write debug info offset table last to make dex file verifier happy.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800389 WriteDebugInfoOffsetTable(data_stream);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800390
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800391 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 Chartier8892c6b2018-01-09 15:10:17 -0800405 // Write header last.
406 if (compute_offsets_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800407 header_->SetFileSize(main_stream->Tell());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800408 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800409 WriteHeader(main_stream);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800410
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800411 // Trim sections to make sure they are sized properly.
412 output->GetMainSection()->Resize(header_->FileSize());
413 output->GetDataSection()->Resize(data_stream->Tell());
414
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800415 if (dex_layout_->GetOptions().update_checksum_) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800416 // 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 Chartier8892c6b2018-01-09 15:10:17 -0800421 // Rewrite the header with the calculated checksum.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800422 WriteHeader(main_stream);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800423 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800424}
425
426std::unique_ptr<DexContainer> CompactDexWriter::CreateDexContainer() const {
427 return std::unique_ptr<DexContainer>(
428 new CompactDexWriter::Container(dex_layout_->GetOptions().dedupe_code_items_));
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800429}
430
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700431} // namespace art