blob: 268abe4415d5ce836cd7c6cf92acb5192787459f [file] [log] [blame]
Jeff Haoa8621002016-10-04 18:13:44 +00001/*
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.
Jeff Haoa8621002016-10-04 18:13:44 +000015 */
16
Andreas Gampee2abbc62017-09-15 11:59:26 -070017#include "dex_writer.h"
18
Jeff Haoa8621002016-10-04 18:13:44 +000019#include <stdint.h>
20
Jeff Haoa8621002016-10-04 18:13:44 +000021#include <vector>
22
Mathieu Chartierf95a75e2017-11-03 15:25:52 -070023#include "compact_dex_writer.h"
David Sehr9e734c72018-01-04 17:56:19 -080024#include "dex/compact_dex_file.h"
25#include "dex/dex_file_layout.h"
26#include "dex/dex_file_types.h"
27#include "dex/standard_dex_file.h"
David Sehr0225f8e2018-01-31 08:52:24 +000028#include "dex/utf.h"
Mathieu Chartier3e0c5172017-11-12 12:58:40 -080029#include "dexlayout.h"
Jeff Haoa8621002016-10-04 18:13:44 +000030
31namespace art {
32
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080033constexpr uint32_t DexWriter::kDataSectionAlignment;
34
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080035static size_t EncodeIntValue(int32_t value, uint8_t* buffer) {
Jeff Haoa8621002016-10-04 18:13:44 +000036 size_t length = 0;
37 if (value >= 0) {
38 while (value > 0x7f) {
39 buffer[length++] = static_cast<uint8_t>(value);
40 value >>= 8;
41 }
42 } else {
43 while (value < -0x80) {
44 buffer[length++] = static_cast<uint8_t>(value);
45 value >>= 8;
46 }
47 }
48 buffer[length++] = static_cast<uint8_t>(value);
49 return length;
50}
51
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080052static size_t EncodeUIntValue(uint32_t value, uint8_t* buffer) {
Jeff Haoa8621002016-10-04 18:13:44 +000053 size_t length = 0;
54 do {
55 buffer[length++] = static_cast<uint8_t>(value);
56 value >>= 8;
57 } while (value != 0);
58 return length;
59}
60
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080061static size_t EncodeLongValue(int64_t value, uint8_t* buffer) {
Jeff Haoa8621002016-10-04 18:13:44 +000062 size_t length = 0;
63 if (value >= 0) {
64 while (value > 0x7f) {
65 buffer[length++] = static_cast<uint8_t>(value);
66 value >>= 8;
67 }
68 } else {
69 while (value < -0x80) {
70 buffer[length++] = static_cast<uint8_t>(value);
71 value >>= 8;
72 }
73 }
74 buffer[length++] = static_cast<uint8_t>(value);
75 return length;
76}
77
78union FloatUnion {
79 float f_;
80 uint32_t i_;
81};
82
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080083static size_t EncodeFloatValue(float value, uint8_t* buffer) {
Jeff Haoa8621002016-10-04 18:13:44 +000084 FloatUnion float_union;
85 float_union.f_ = value;
86 uint32_t int_value = float_union.i_;
87 size_t index = 3;
88 do {
89 buffer[index--] = int_value >> 24;
90 int_value <<= 8;
91 } while (int_value != 0);
92 return 3 - index;
93}
94
95union DoubleUnion {
96 double d_;
97 uint64_t l_;
98};
99
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800100static size_t EncodeDoubleValue(double value, uint8_t* buffer) {
Jeff Haoa8621002016-10-04 18:13:44 +0000101 DoubleUnion double_union;
102 double_union.d_ = value;
103 uint64_t long_value = double_union.l_;
104 size_t index = 7;
105 do {
106 buffer[index--] = long_value >> 56;
107 long_value <<= 8;
108 } while (long_value != 0);
109 return 7 - index;
110}
111
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800112DexWriter::DexWriter(DexLayout* dex_layout, bool compute_offsets)
113 : header_(dex_layout->GetHeader()),
114 dex_layout_(dex_layout),
115 compute_offsets_(compute_offsets) {}
Jeff Haoa8621002016-10-04 18:13:44 +0000116
Mathieu Chartier5e496142018-01-27 13:11:14 -0800117void DexWriter::WriteEncodedValue(Stream* stream, dex_ir::EncodedValue* encoded_value) {
Jeff Haoa8621002016-10-04 18:13:44 +0000118 size_t start = 0;
119 size_t length;
120 uint8_t buffer[8];
121 int8_t type = encoded_value->Type();
122 switch (type) {
123 case DexFile::kDexAnnotationByte:
124 length = EncodeIntValue(encoded_value->GetByte(), buffer);
125 break;
126 case DexFile::kDexAnnotationShort:
127 length = EncodeIntValue(encoded_value->GetShort(), buffer);
128 break;
129 case DexFile::kDexAnnotationChar:
130 length = EncodeUIntValue(encoded_value->GetChar(), buffer);
131 break;
132 case DexFile::kDexAnnotationInt:
133 length = EncodeIntValue(encoded_value->GetInt(), buffer);
134 break;
135 case DexFile::kDexAnnotationLong:
136 length = EncodeLongValue(encoded_value->GetLong(), buffer);
137 break;
138 case DexFile::kDexAnnotationFloat:
139 length = EncodeFloatValue(encoded_value->GetFloat(), buffer);
140 start = 4 - length;
141 break;
142 case DexFile::kDexAnnotationDouble:
143 length = EncodeDoubleValue(encoded_value->GetDouble(), buffer);
144 start = 8 - length;
145 break;
Jeff Hao5daee902017-04-27 18:00:38 -0700146 case DexFile::kDexAnnotationMethodType:
147 length = EncodeUIntValue(encoded_value->GetProtoId()->GetIndex(), buffer);
148 break;
149 case DexFile::kDexAnnotationMethodHandle:
150 length = EncodeUIntValue(encoded_value->GetMethodHandle()->GetIndex(), buffer);
151 break;
Jeff Haoa8621002016-10-04 18:13:44 +0000152 case DexFile::kDexAnnotationString:
153 length = EncodeUIntValue(encoded_value->GetStringId()->GetIndex(), buffer);
154 break;
155 case DexFile::kDexAnnotationType:
156 length = EncodeUIntValue(encoded_value->GetTypeId()->GetIndex(), buffer);
157 break;
158 case DexFile::kDexAnnotationField:
159 case DexFile::kDexAnnotationEnum:
160 length = EncodeUIntValue(encoded_value->GetFieldId()->GetIndex(), buffer);
161 break;
162 case DexFile::kDexAnnotationMethod:
163 length = EncodeUIntValue(encoded_value->GetMethodId()->GetIndex(), buffer);
164 break;
165 case DexFile::kDexAnnotationArray:
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800166 WriteEncodedValueHeader(stream, type, 0);
167 WriteEncodedArray(stream, encoded_value->GetEncodedArray()->GetEncodedValues());
Mathieu Chartier5e496142018-01-27 13:11:14 -0800168 return;
Jeff Haoa8621002016-10-04 18:13:44 +0000169 case DexFile::kDexAnnotationAnnotation:
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800170 WriteEncodedValueHeader(stream, type, 0);
171 WriteEncodedAnnotation(stream, encoded_value->GetEncodedAnnotation());
Mathieu Chartier5e496142018-01-27 13:11:14 -0800172 return;
Jeff Haoa8621002016-10-04 18:13:44 +0000173 case DexFile::kDexAnnotationNull:
Mathieu Chartier5e496142018-01-27 13:11:14 -0800174 WriteEncodedValueHeader(stream, type, 0);
175 return;
Jeff Haoa8621002016-10-04 18:13:44 +0000176 case DexFile::kDexAnnotationBoolean:
Mathieu Chartier5e496142018-01-27 13:11:14 -0800177 WriteEncodedValueHeader(stream, type, encoded_value->GetBoolean() ? 1 : 0);
178 return;
Jeff Haoa8621002016-10-04 18:13:44 +0000179 default:
Mathieu Chartier5e496142018-01-27 13:11:14 -0800180 return;
Jeff Haoa8621002016-10-04 18:13:44 +0000181 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800182 WriteEncodedValueHeader(stream, type, length - 1);
183 stream->Write(buffer + start, length);
Jeff Haoa8621002016-10-04 18:13:44 +0000184}
185
Mathieu Chartier5e496142018-01-27 13:11:14 -0800186void DexWriter::WriteEncodedValueHeader(Stream* stream, int8_t value_type, size_t value_arg) {
Jeff Haoa8621002016-10-04 18:13:44 +0000187 uint8_t buffer[1] = { static_cast<uint8_t>((value_arg << 5) | value_type) };
Mathieu Chartier5e496142018-01-27 13:11:14 -0800188 stream->Write(buffer, sizeof(uint8_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000189}
190
Mathieu Chartier5e496142018-01-27 13:11:14 -0800191void DexWriter::WriteEncodedArray(Stream* stream, dex_ir::EncodedValueVector* values) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800192 stream->WriteUleb128(values->size());
Jeff Haoa8621002016-10-04 18:13:44 +0000193 for (std::unique_ptr<dex_ir::EncodedValue>& value : *values) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800194 WriteEncodedValue(stream, value.get());
Jeff Haoa8621002016-10-04 18:13:44 +0000195 }
Jeff Haoa8621002016-10-04 18:13:44 +0000196}
197
Mathieu Chartier5e496142018-01-27 13:11:14 -0800198void DexWriter::WriteEncodedAnnotation(Stream* stream, dex_ir::EncodedAnnotation* annotation) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800199 stream->WriteUleb128(annotation->GetType()->GetIndex());
200 stream->WriteUleb128(annotation->GetAnnotationElements()->size());
Jeff Haoa8621002016-10-04 18:13:44 +0000201 for (std::unique_ptr<dex_ir::AnnotationElement>& annotation_element :
202 *annotation->GetAnnotationElements()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800203 stream->WriteUleb128(annotation_element->GetName()->GetIndex());
204 WriteEncodedValue(stream, annotation_element->GetValue());
Jeff Haoa8621002016-10-04 18:13:44 +0000205 }
Jeff Haoa8621002016-10-04 18:13:44 +0000206}
207
Mathieu Chartier5e496142018-01-27 13:11:14 -0800208void DexWriter::WriteEncodedFields(Stream* stream, dex_ir::FieldItemVector* fields) {
Jeff Haoa8621002016-10-04 18:13:44 +0000209 uint32_t prev_index = 0;
David Sehrd83437c2018-06-11 14:06:23 -0700210 for (auto& field : *fields) {
211 uint32_t index = field.GetFieldId()->GetIndex();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800212 stream->WriteUleb128(index - prev_index);
David Sehrd83437c2018-06-11 14:06:23 -0700213 stream->WriteUleb128(field.GetAccessFlags());
Jeff Haoa8621002016-10-04 18:13:44 +0000214 prev_index = index;
215 }
Jeff Haoa8621002016-10-04 18:13:44 +0000216}
217
Mathieu Chartier5e496142018-01-27 13:11:14 -0800218void DexWriter::WriteEncodedMethods(Stream* stream, dex_ir::MethodItemVector* methods) {
Jeff Haoa8621002016-10-04 18:13:44 +0000219 uint32_t prev_index = 0;
David Sehrd83437c2018-06-11 14:06:23 -0700220 for (auto& method : *methods) {
221 uint32_t index = method.GetMethodId()->GetIndex();
222 uint32_t code_off = method.GetCodeItem() == nullptr ? 0 : method.GetCodeItem()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800223 stream->WriteUleb128(index - prev_index);
David Sehrd83437c2018-06-11 14:06:23 -0700224 stream->WriteUleb128(method.GetAccessFlags());
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800225 stream->WriteUleb128(code_off);
Jeff Haoa8621002016-10-04 18:13:44 +0000226 prev_index = index;
227 }
Jeff Haoa8621002016-10-04 18:13:44 +0000228}
229
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800230// TODO: Refactor this to remove duplicated boiler plate. One way to do this is adding
231// function that takes a CollectionVector<T> and uses overloading.
Mathieu Chartier5e496142018-01-27 13:11:14 -0800232void DexWriter::WriteStringIds(Stream* stream, bool reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800233 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700234 for (auto& string_id : header_->StringIds()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800235 stream->AlignTo(SectionAlignment(DexFile::kDexTypeStringIdItem));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800236 if (reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800237 stream->Skip(string_id->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800238 } else {
239 uint32_t string_data_off = string_id->DataItem()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800240 stream->Write(&string_data_off, string_id->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800241 }
Jeff Haoa8621002016-10-04 18:13:44 +0000242 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800243 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700244 header_->StringIds().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800245 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800246}
Jeff Haoa8621002016-10-04 18:13:44 +0000247
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800248void DexWriter::WriteStringData(Stream* stream, dex_ir::StringData* string_data) {
249 ProcessOffset(stream, string_data);
250 stream->AlignTo(SectionAlignment(DexFile::kDexTypeStringDataItem));
251 stream->WriteUleb128(CountModifiedUtf8Chars(string_data->Data()));
252 stream->Write(string_data->Data(), strlen(string_data->Data()));
253 // Skip null terminator (already zeroed out, no need to write).
254 stream->Skip(1);
255}
256
Mathieu Chartier5e496142018-01-27 13:11:14 -0800257void DexWriter::WriteStringDatas(Stream* stream) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800258 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700259 for (auto& string_data : header_->StringDatas()) {
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800260 WriteStringData(stream, string_data.get());
Jeff Haoa8621002016-10-04 18:13:44 +0000261 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800262 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700263 header_->StringDatas().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800264 }
Jeff Haoa8621002016-10-04 18:13:44 +0000265}
266
Mathieu Chartier5e496142018-01-27 13:11:14 -0800267void DexWriter::WriteTypeIds(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000268 uint32_t descriptor_idx[1];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800269 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700270 for (auto& type_id : header_->TypeIds()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800271 stream->AlignTo(SectionAlignment(DexFile::kDexTypeTypeIdItem));
272 ProcessOffset(stream, type_id.get());
Jeff Haoa8621002016-10-04 18:13:44 +0000273 descriptor_idx[0] = type_id->GetStringId()->GetIndex();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800274 stream->Write(descriptor_idx, type_id->GetSize());
Jeff Haoa8621002016-10-04 18:13:44 +0000275 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800276 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700277 header_->TypeIds().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800278 }
Jeff Haoa8621002016-10-04 18:13:44 +0000279}
280
Mathieu Chartier5e496142018-01-27 13:11:14 -0800281void DexWriter::WriteTypeLists(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000282 uint32_t size[1];
283 uint16_t list[1];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800284 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700285 for (auto& type_list : header_->TypeLists()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800286 stream->AlignTo(SectionAlignment(DexFile::kDexTypeTypeList));
Jeff Haoa8621002016-10-04 18:13:44 +0000287 size[0] = type_list->GetTypeList()->size();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800288 ProcessOffset(stream, type_list.get());
289 stream->Write(size, sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000290 for (const dex_ir::TypeId* type_id : *type_list->GetTypeList()) {
291 list[0] = type_id->GetIndex();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800292 stream->Write(list, sizeof(uint16_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000293 }
294 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800295 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700296 header_->TypeLists().SetOffset(start);
Jeff Haoa8621002016-10-04 18:13:44 +0000297 }
298}
299
Mathieu Chartier5e496142018-01-27 13:11:14 -0800300void DexWriter::WriteProtoIds(Stream* stream, bool reserve_only) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800301 uint32_t buffer[3];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800302 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700303 for (auto& proto_id : header_->ProtoIds()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800304 stream->AlignTo(SectionAlignment(DexFile::kDexTypeProtoIdItem));
305 ProcessOffset(stream, proto_id.get());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800306 if (reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800307 stream->Skip(proto_id->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800308 } else {
309 buffer[0] = proto_id->Shorty()->GetIndex();
310 buffer[1] = proto_id->ReturnType()->GetIndex();
311 buffer[2] = proto_id->Parameters() == nullptr ? 0 : proto_id->Parameters()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800312 stream->Write(buffer, proto_id->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800313 }
314 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800315 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700316 header_->ProtoIds().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800317 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800318}
319
Mathieu Chartier5e496142018-01-27 13:11:14 -0800320void DexWriter::WriteFieldIds(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000321 uint16_t buffer[4];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800322 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700323 for (auto& field_id : header_->FieldIds()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800324 stream->AlignTo(SectionAlignment(DexFile::kDexTypeFieldIdItem));
325 ProcessOffset(stream, field_id.get());
Jeff Haoa8621002016-10-04 18:13:44 +0000326 buffer[0] = field_id->Class()->GetIndex();
327 buffer[1] = field_id->Type()->GetIndex();
328 buffer[2] = field_id->Name()->GetIndex();
329 buffer[3] = field_id->Name()->GetIndex() >> 16;
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800330 stream->Write(buffer, field_id->GetSize());
Jeff Haoa8621002016-10-04 18:13:44 +0000331 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800332 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700333 header_->FieldIds().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800334 }
Jeff Haoa8621002016-10-04 18:13:44 +0000335}
336
Mathieu Chartier5e496142018-01-27 13:11:14 -0800337void DexWriter::WriteMethodIds(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000338 uint16_t buffer[4];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800339 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700340 for (auto& method_id : header_->MethodIds()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800341 stream->AlignTo(SectionAlignment(DexFile::kDexTypeMethodIdItem));
342 ProcessOffset(stream, method_id.get());
Jeff Haoa8621002016-10-04 18:13:44 +0000343 buffer[0] = method_id->Class()->GetIndex();
344 buffer[1] = method_id->Proto()->GetIndex();
345 buffer[2] = method_id->Name()->GetIndex();
346 buffer[3] = method_id->Name()->GetIndex() >> 16;
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800347 stream->Write(buffer, method_id->GetSize());
Jeff Haoa8621002016-10-04 18:13:44 +0000348 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800349 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700350 header_->MethodIds().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800351 }
Jeff Haoa8621002016-10-04 18:13:44 +0000352}
353
Mathieu Chartier5e496142018-01-27 13:11:14 -0800354void DexWriter::WriteEncodedArrays(Stream* stream) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800355 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700356 for (auto& encoded_array : header_->EncodedArrayItems()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800357 stream->AlignTo(SectionAlignment(DexFile::kDexTypeEncodedArrayItem));
358 ProcessOffset(stream, encoded_array.get());
359 WriteEncodedArray(stream, encoded_array->GetEncodedValues());
Jeff Haoa8621002016-10-04 18:13:44 +0000360 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800361 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700362 header_->EncodedArrayItems().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800363 }
Jeff Haoa8621002016-10-04 18:13:44 +0000364}
365
Mathieu Chartier5e496142018-01-27 13:11:14 -0800366void DexWriter::WriteAnnotations(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000367 uint8_t visibility[1];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800368 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700369 for (auto& annotation : header_->AnnotationItems()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800370 stream->AlignTo(SectionAlignment(DexFile::kDexTypeAnnotationItem));
Jeff Haoa8621002016-10-04 18:13:44 +0000371 visibility[0] = annotation->GetVisibility();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800372 ProcessOffset(stream, annotation.get());
373 stream->Write(visibility, sizeof(uint8_t));
374 WriteEncodedAnnotation(stream, annotation->GetAnnotation());
Jeff Haoa8621002016-10-04 18:13:44 +0000375 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800376 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700377 header_->AnnotationItems().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800378 }
Jeff Haoa8621002016-10-04 18:13:44 +0000379}
380
Mathieu Chartier5e496142018-01-27 13:11:14 -0800381void DexWriter::WriteAnnotationSets(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000382 uint32_t size[1];
383 uint32_t annotation_off[1];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800384 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700385 for (auto& annotation_set : header_->AnnotationSetItems()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800386 stream->AlignTo(SectionAlignment(DexFile::kDexTypeAnnotationSetItem));
Jeff Haoa8621002016-10-04 18:13:44 +0000387 size[0] = annotation_set->GetItems()->size();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800388 ProcessOffset(stream, annotation_set.get());
389 stream->Write(size, sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000390 for (dex_ir::AnnotationItem* annotation : *annotation_set->GetItems()) {
391 annotation_off[0] = annotation->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800392 stream->Write(annotation_off, sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000393 }
394 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800395 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700396 header_->AnnotationSetItems().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800397 }
Jeff Haoa8621002016-10-04 18:13:44 +0000398}
399
Mathieu Chartier5e496142018-01-27 13:11:14 -0800400void DexWriter::WriteAnnotationSetRefs(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000401 uint32_t size[1];
402 uint32_t annotations_off[1];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800403 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700404 for (auto& annotation_set_ref : header_->AnnotationSetRefLists()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800405 stream->AlignTo(SectionAlignment(DexFile::kDexTypeAnnotationSetRefList));
Jeff Haoa8621002016-10-04 18:13:44 +0000406 size[0] = annotation_set_ref->GetItems()->size();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800407 ProcessOffset(stream, annotation_set_ref.get());
408 stream->Write(size, sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000409 for (dex_ir::AnnotationSetItem* annotation_set : *annotation_set_ref->GetItems()) {
410 annotations_off[0] = annotation_set == nullptr ? 0 : annotation_set->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800411 stream->Write(annotations_off, sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000412 }
413 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800414 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700415 header_->AnnotationSetRefLists().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800416 }
Jeff Haoa8621002016-10-04 18:13:44 +0000417}
418
Mathieu Chartier5e496142018-01-27 13:11:14 -0800419void DexWriter::WriteAnnotationsDirectories(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000420 uint32_t directory_buffer[4];
421 uint32_t annotation_buffer[2];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800422 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700423 for (auto& annotations_directory : header_->AnnotationsDirectoryItems()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800424 stream->AlignTo(SectionAlignment(DexFile::kDexTypeAnnotationsDirectoryItem));
425 ProcessOffset(stream, annotations_directory.get());
Jeff Haoa8621002016-10-04 18:13:44 +0000426 directory_buffer[0] = annotations_directory->GetClassAnnotation() == nullptr ? 0 :
427 annotations_directory->GetClassAnnotation()->GetOffset();
428 directory_buffer[1] = annotations_directory->GetFieldAnnotations() == nullptr ? 0 :
429 annotations_directory->GetFieldAnnotations()->size();
430 directory_buffer[2] = annotations_directory->GetMethodAnnotations() == nullptr ? 0 :
431 annotations_directory->GetMethodAnnotations()->size();
432 directory_buffer[3] = annotations_directory->GetParameterAnnotations() == nullptr ? 0 :
433 annotations_directory->GetParameterAnnotations()->size();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800434 stream->Write(directory_buffer, 4 * sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000435 if (annotations_directory->GetFieldAnnotations() != nullptr) {
436 for (std::unique_ptr<dex_ir::FieldAnnotation>& field :
437 *annotations_directory->GetFieldAnnotations()) {
438 annotation_buffer[0] = field->GetFieldId()->GetIndex();
439 annotation_buffer[1] = field->GetAnnotationSetItem()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800440 stream->Write(annotation_buffer, 2 * sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000441 }
442 }
443 if (annotations_directory->GetMethodAnnotations() != nullptr) {
444 for (std::unique_ptr<dex_ir::MethodAnnotation>& method :
445 *annotations_directory->GetMethodAnnotations()) {
446 annotation_buffer[0] = method->GetMethodId()->GetIndex();
447 annotation_buffer[1] = method->GetAnnotationSetItem()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800448 stream->Write(annotation_buffer, 2 * sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000449 }
450 }
451 if (annotations_directory->GetParameterAnnotations() != nullptr) {
452 for (std::unique_ptr<dex_ir::ParameterAnnotation>& parameter :
453 *annotations_directory->GetParameterAnnotations()) {
454 annotation_buffer[0] = parameter->GetMethodId()->GetIndex();
455 annotation_buffer[1] = parameter->GetAnnotations()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800456 stream->Write(annotation_buffer, 2 * sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000457 }
458 }
459 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800460 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700461 header_->AnnotationsDirectoryItems().SetOffset(start);
Jeff Haoa8621002016-10-04 18:13:44 +0000462 }
463}
464
David Brazdil20c765f2018-10-27 21:45:15 +0000465void DexWriter::WriteHiddenapiClassData(Stream* stream) {
466 if (header_->HiddenapiClassDatas().Empty()) {
467 return;
468 }
469 DCHECK_EQ(header_->HiddenapiClassDatas().Size(), header_->ClassDefs().Size());
470
471 stream->AlignTo(SectionAlignment(DexFile::kDexTypeHiddenapiClassData));
David Brazdil17834802019-01-21 19:46:46 +0000472 ProcessOffset(stream, &header_->HiddenapiClassDatas());
David Brazdil20c765f2018-10-27 21:45:15 +0000473 const uint32_t start = stream->Tell();
474
475 // Compute offsets for each class def and write the header.
476 // data_header[0]: total size of the section
477 // data_header[i + 1]: offset of class def[i] from the beginning of the section,
478 // or zero if no data
479 std::vector<uint32_t> data_header(header_->ClassDefs().Size() + 1, 0);
480 data_header[0] = sizeof(uint32_t) * (header_->ClassDefs().Size() + 1);
481 for (uint32_t i = 0; i < header_->ClassDefs().Size(); ++i) {
482 uint32_t item_size = header_->HiddenapiClassDatas()[i]->ItemSize();
483 data_header[i + 1] = item_size == 0u ? 0 : data_header[0];
484 data_header[0] += item_size;
485 }
486 stream->Write(data_header.data(), sizeof(uint32_t) * data_header.size());
487
488 // Write class data streams.
489 for (uint32_t i = 0; i < header_->ClassDefs().Size(); ++i) {
490 dex_ir::ClassDef* class_def = header_->ClassDefs()[i];
491 const auto& item = header_->HiddenapiClassDatas()[i];
492 DCHECK(item->GetClassDef() == class_def);
493
494 if (data_header[i + 1] != 0u) {
495 dex_ir::ClassData* class_data = class_def->GetClassData();
496 DCHECK(class_data != nullptr);
497 DCHECK_EQ(data_header[i + 1], stream->Tell() - start);
498 for (const dex_ir::FieldItem& field : *class_data->StaticFields()) {
499 stream->WriteUleb128(item->GetFlags(&field));
500 }
501 for (const dex_ir::FieldItem& field : *class_data->InstanceFields()) {
502 stream->WriteUleb128(item->GetFlags(&field));
503 }
504 for (const dex_ir::MethodItem& method : *class_data->DirectMethods()) {
505 stream->WriteUleb128(item->GetFlags(&method));
506 }
507 for (const dex_ir::MethodItem& method : *class_data->VirtualMethods()) {
508 stream->WriteUleb128(item->GetFlags(&method));
509 }
510 }
511 }
512 DCHECK_EQ(stream->Tell() - start, data_header[0]);
513
514 if (compute_offsets_ && start != stream->Tell()) {
515 header_->HiddenapiClassDatas().SetOffset(start);
516 }
517}
518
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800519void DexWriter::WriteDebugInfoItem(Stream* stream, dex_ir::DebugInfoItem* debug_info) {
520 stream->AlignTo(SectionAlignment(DexFile::kDexTypeDebugInfoItem));
521 ProcessOffset(stream, debug_info);
522 stream->Write(debug_info->GetDebugInfo(), debug_info->GetDebugInfoSize());
523}
524
Mathieu Chartier5e496142018-01-27 13:11:14 -0800525void DexWriter::WriteDebugInfoItems(Stream* stream) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800526 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700527 for (auto& debug_info : header_->DebugInfoItems()) {
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800528 WriteDebugInfoItem(stream, debug_info.get());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800529 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800530 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700531 header_->DebugInfoItems().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800532 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800533}
534
Mathieu Chartier5e496142018-01-27 13:11:14 -0800535void DexWriter::WriteCodeItemPostInstructionData(Stream* stream,
536 dex_ir::CodeItem* code_item,
537 bool reserve_only) {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800538 if (code_item->TriesSize() != 0) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800539 stream->AlignTo(dex::TryItem::kAlignment);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800540 // Write try items.
541 for (std::unique_ptr<const dex_ir::TryItem>& try_item : *code_item->Tries()) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800542 dex::TryItem disk_try_item;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800543 if (!reserve_only) {
544 disk_try_item.start_addr_ = try_item->StartAddr();
545 disk_try_item.insn_count_ = try_item->InsnCount();
546 disk_try_item.handler_off_ = try_item->GetHandlers()->GetListOffset();
547 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800548 stream->Write(&disk_try_item, sizeof(disk_try_item));
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800549 }
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800550 // Leave offset pointing to the end of the try items.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800551 const size_t offset = stream->Tell();
552 size_t max_offset = offset + stream->WriteUleb128(code_item->Handlers()->size());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800553 for (std::unique_ptr<const dex_ir::CatchHandler>& handlers : *code_item->Handlers()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800554 stream->Seek(offset + handlers->GetListOffset());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800555 uint32_t size = handlers->HasCatchAll() ? (handlers->GetHandlers()->size() - 1) * -1 :
556 handlers->GetHandlers()->size();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800557 stream->WriteSleb128(size);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800558 for (std::unique_ptr<const dex_ir::TypeAddrPair>& handler : *handlers->GetHandlers()) {
559 if (handler->GetTypeId() != nullptr) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800560 stream->WriteUleb128(handler->GetTypeId()->GetIndex());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800561 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800562 stream->WriteUleb128(handler->GetAddress());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800563 }
564 // TODO: Clean this up to write the handlers in address order.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800565 max_offset = std::max(max_offset, stream->Tell());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800566 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800567 stream->Seek(max_offset);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800568 }
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800569}
570
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800571void DexWriter::WriteCodeItem(Stream* stream,
572 dex_ir::CodeItem* code_item,
573 bool reserve_only) {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800574 DCHECK(code_item != nullptr);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800575 const uint32_t start_offset = stream->Tell();
576 stream->AlignTo(SectionAlignment(DexFile::kDexTypeCodeItem));
577 ProcessOffset(stream, code_item);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800578
579 StandardDexFile::CodeItem disk_code_item;
580 if (!reserve_only) {
581 disk_code_item.registers_size_ = code_item->RegistersSize();
582 disk_code_item.ins_size_ = code_item->InsSize();
583 disk_code_item.outs_size_ = code_item->OutsSize();
584 disk_code_item.tries_size_ = code_item->TriesSize();
585 disk_code_item.debug_info_off_ = code_item->DebugInfo() == nullptr
586 ? 0
587 : code_item->DebugInfo()->GetOffset();
588 disk_code_item.insns_size_in_code_units_ = code_item->InsnsSize();
589 }
590 // Avoid using sizeof so that we don't write the fake instruction array at the end of the code
591 // item.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800592 stream->Write(&disk_code_item, OFFSETOF_MEMBER(StandardDexFile::CodeItem, insns_));
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800593 // Write the instructions.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800594 stream->Write(code_item->Insns(), code_item->InsnsSize() * sizeof(uint16_t));
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800595 // Write the post instruction data.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800596 WriteCodeItemPostInstructionData(stream, code_item, reserve_only);
597 if (reserve_only) {
598 stream->Clear(start_offset, stream->Tell() - start_offset);
599 }
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800600}
601
Mathieu Chartier5e496142018-01-27 13:11:14 -0800602void DexWriter::WriteCodeItems(Stream* stream, bool reserve_only) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800603 DexLayoutSection* code_section = nullptr;
604 if (!reserve_only && dex_layout_ != nullptr) {
605 code_section = &dex_layout_->GetSections().sections_[static_cast<size_t>(
606 DexLayoutSections::SectionType::kSectionTypeCode)];
607 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800608 const uint32_t start = stream->Tell();
David Sehr2b5a38f2018-06-14 15:13:04 -0700609 for (auto& code_item : header_->CodeItems()) {
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800610 uint32_t start_offset = stream->Tell();
611 WriteCodeItem(stream, code_item.get(), reserve_only);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800612 // Only add the section hotness info once.
613 if (!reserve_only && code_section != nullptr) {
614 auto it = dex_layout_->LayoutHotnessInfo().code_item_layout_.find(code_item.get());
615 if (it != dex_layout_->LayoutHotnessInfo().code_item_layout_.end()) {
616 code_section->parts_[static_cast<size_t>(it->second)].CombineSection(
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800617 start_offset,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800618 stream->Tell());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800619 }
620 }
Jeff Haoa8621002016-10-04 18:13:44 +0000621 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800622
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800623 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700624 header_->CodeItems().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800625 }
Jeff Haoa8621002016-10-04 18:13:44 +0000626}
627
Mathieu Chartier5e496142018-01-27 13:11:14 -0800628void DexWriter::WriteClassDefs(Stream* stream, bool reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800629 const uint32_t start = stream->Tell();
Jeff Haoa8621002016-10-04 18:13:44 +0000630 uint32_t class_def_buffer[8];
David Sehr2b5a38f2018-06-14 15:13:04 -0700631 for (auto& class_def : header_->ClassDefs()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800632 stream->AlignTo(SectionAlignment(DexFile::kDexTypeClassDefItem));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800633 if (reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800634 stream->Skip(class_def->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800635 } else {
636 class_def_buffer[0] = class_def->ClassType()->GetIndex();
637 class_def_buffer[1] = class_def->GetAccessFlags();
638 class_def_buffer[2] = class_def->Superclass() == nullptr ? dex::kDexNoIndex :
639 class_def->Superclass()->GetIndex();
640 class_def_buffer[3] = class_def->InterfacesOffset();
641 class_def_buffer[4] = class_def->SourceFile() == nullptr ? dex::kDexNoIndex :
642 class_def->SourceFile()->GetIndex();
643 class_def_buffer[5] = class_def->Annotations() == nullptr ? 0 :
644 class_def->Annotations()->GetOffset();
645 class_def_buffer[6] = class_def->GetClassData() == nullptr ? 0 :
646 class_def->GetClassData()->GetOffset();
647 class_def_buffer[7] = class_def->StaticValues() == nullptr ? 0 :
648 class_def->StaticValues()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800649 stream->Write(class_def_buffer, class_def->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800650 }
Jeff Haoa8621002016-10-04 18:13:44 +0000651 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800652 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700653 header_->ClassDefs().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800654 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800655}
Jeff Haoa8621002016-10-04 18:13:44 +0000656
Mathieu Chartier5e496142018-01-27 13:11:14 -0800657void DexWriter::WriteClassDatas(Stream* stream) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800658 const uint32_t start = stream->Tell();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800659 for (const std::unique_ptr<dex_ir::ClassData>& class_data :
David Sehr2b5a38f2018-06-14 15:13:04 -0700660 header_->ClassDatas()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800661 stream->AlignTo(SectionAlignment(DexFile::kDexTypeClassDataItem));
662 ProcessOffset(stream, class_data.get());
663 stream->WriteUleb128(class_data->StaticFields()->size());
664 stream->WriteUleb128(class_data->InstanceFields()->size());
665 stream->WriteUleb128(class_data->DirectMethods()->size());
666 stream->WriteUleb128(class_data->VirtualMethods()->size());
667 WriteEncodedFields(stream, class_data->StaticFields());
668 WriteEncodedFields(stream, class_data->InstanceFields());
669 WriteEncodedMethods(stream, class_data->DirectMethods());
670 WriteEncodedMethods(stream, class_data->VirtualMethods());
Jeff Haoa8621002016-10-04 18:13:44 +0000671 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800672 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700673 header_->ClassDatas().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800674 }
Jeff Haoa8621002016-10-04 18:13:44 +0000675}
676
Mathieu Chartier5e496142018-01-27 13:11:14 -0800677void DexWriter::WriteCallSiteIds(Stream* stream, bool reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800678 const uint32_t start = stream->Tell();
Jeff Hao5daee902017-04-27 18:00:38 -0700679 uint32_t call_site_off[1];
David Sehr2b5a38f2018-06-14 15:13:04 -0700680 for (auto& call_site_id : header_->CallSiteIds()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800681 stream->AlignTo(SectionAlignment(DexFile::kDexTypeCallSiteIdItem));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800682 if (reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800683 stream->Skip(call_site_id->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800684 } else {
685 call_site_off[0] = call_site_id->CallSiteItem()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800686 stream->Write(call_site_off, call_site_id->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800687 }
Jeff Hao5daee902017-04-27 18:00:38 -0700688 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800689 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700690 header_->CallSiteIds().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800691 }
Jeff Hao5daee902017-04-27 18:00:38 -0700692}
693
Mathieu Chartier5e496142018-01-27 13:11:14 -0800694void DexWriter::WriteMethodHandles(Stream* stream) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800695 const uint32_t start = stream->Tell();
Jeff Hao5daee902017-04-27 18:00:38 -0700696 uint16_t method_handle_buff[4];
David Sehr2b5a38f2018-06-14 15:13:04 -0700697 for (auto& method_handle : header_->MethodHandleItems()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800698 stream->AlignTo(SectionAlignment(DexFile::kDexTypeMethodHandleItem));
Jeff Hao5daee902017-04-27 18:00:38 -0700699 method_handle_buff[0] = static_cast<uint16_t>(method_handle->GetMethodHandleType());
700 method_handle_buff[1] = 0; // unused.
701 method_handle_buff[2] = method_handle->GetFieldOrMethodId()->GetIndex();
702 method_handle_buff[3] = 0; // unused.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800703 stream->Write(method_handle_buff, method_handle->GetSize());
Jeff Hao5daee902017-04-27 18:00:38 -0700704 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800705 if (compute_offsets_ && start != stream->Tell()) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700706 header_->MethodHandleItems().SetOffset(start);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800707 }
Jeff Hao5daee902017-04-27 18:00:38 -0700708}
709
Mathieu Chartier5e496142018-01-27 13:11:14 -0800710void DexWriter::WriteMapItems(Stream* stream, MapItemQueue* queue) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800711 // All the sections should already have been added.
Mathieu Chartier9b302bf2018-01-25 13:08:08 -0800712 const uint32_t map_list_size = queue->size();
Mathieu Chartier9b302bf2018-01-25 13:08:08 -0800713 stream->Write(&map_list_size, sizeof(map_list_size));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800714 while (!queue->empty()) {
Mathieu Chartier9b302bf2018-01-25 13:08:08 -0800715 const MapItem& item = queue->top();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800716 dex::MapItem map_item;
Mathieu Chartier9b302bf2018-01-25 13:08:08 -0800717 map_item.type_ = item.type_;
718 map_item.size_ = item.size_;
719 map_item.offset_ = item.offset_;
720 map_item.unused_ = 0u;
721 stream->Write(&map_item, sizeof(map_item));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800722 queue->pop();
Jeff Haoa8621002016-10-04 18:13:44 +0000723 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800724}
725
Mathieu Chartier5e496142018-01-27 13:11:14 -0800726void DexWriter::GenerateAndWriteMapItems(Stream* stream) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800727 MapItemQueue queue;
728
729 // Header and index section.
730 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeHeaderItem, 1, 0));
731 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeStringIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700732 header_->StringIds().Size(),
733 header_->StringIds().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800734 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeTypeIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700735 header_->TypeIds().Size(),
736 header_->TypeIds().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800737 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeProtoIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700738 header_->ProtoIds().Size(),
739 header_->ProtoIds().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800740 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeFieldIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700741 header_->FieldIds().Size(),
742 header_->FieldIds().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800743 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeMethodIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700744 header_->MethodIds().Size(),
745 header_->MethodIds().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800746 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeClassDefItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700747 header_->ClassDefs().Size(),
748 header_->ClassDefs().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800749 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeCallSiteIdItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700750 header_->CallSiteIds().Size(),
751 header_->CallSiteIds().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800752 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeMethodHandleItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700753 header_->MethodHandleItems().Size(),
754 header_->MethodHandleItems().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800755 // Data section.
David Sehr2b5a38f2018-06-14 15:13:04 -0700756 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeMapList, 1, header_->MapListOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800757 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeTypeList,
David Sehr2b5a38f2018-06-14 15:13:04 -0700758 header_->TypeLists().Size(),
759 header_->TypeLists().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800760 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeAnnotationSetRefList,
David Sehr2b5a38f2018-06-14 15:13:04 -0700761 header_->AnnotationSetRefLists().Size(),
762 header_->AnnotationSetRefLists().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800763 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeAnnotationSetItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700764 header_->AnnotationSetItems().Size(),
765 header_->AnnotationSetItems().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800766 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeClassDataItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700767 header_->ClassDatas().Size(),
768 header_->ClassDatas().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800769 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeCodeItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700770 header_->CodeItems().Size(),
771 header_->CodeItems().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800772 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeStringDataItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700773 header_->StringDatas().Size(),
774 header_->StringDatas().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800775 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeDebugInfoItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700776 header_->DebugInfoItems().Size(),
777 header_->DebugInfoItems().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800778 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeAnnotationItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700779 header_->AnnotationItems().Size(),
780 header_->AnnotationItems().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800781 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeEncodedArrayItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700782 header_->EncodedArrayItems().Size(),
783 header_->EncodedArrayItems().GetOffset()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800784 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeAnnotationsDirectoryItem,
David Sehr2b5a38f2018-06-14 15:13:04 -0700785 header_->AnnotationsDirectoryItems().Size(),
786 header_->AnnotationsDirectoryItems().GetOffset()));
David Brazdil20c765f2018-10-27 21:45:15 +0000787 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeHiddenapiClassData,
788 header_->HiddenapiClassDatas().Empty() ? 0u : 1u,
789 header_->HiddenapiClassDatas().GetOffset()));
Mathieu Chartier5e496142018-01-27 13:11:14 -0800790 WriteMapItems(stream, &queue);
Jeff Haoa8621002016-10-04 18:13:44 +0000791}
792
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800793void DexWriter::WriteHeader(Stream* stream) {
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700794 StandardDexFile::Header header;
Mathieu Chartier21cf2582018-01-08 17:09:48 -0800795 if (CompactDexFile::IsMagicValid(header_->Magic())) {
796 StandardDexFile::WriteMagic(header.magic_);
797 // TODO: Should we write older versions based on the feature flags?
798 StandardDexFile::WriteCurrentVersion(header.magic_);
799 } else {
800 // Standard dex -> standard dex, just reuse the same header.
801 static constexpr size_t kMagicAndVersionLen =
802 StandardDexFile::kDexMagicSize + StandardDexFile::kDexVersionLen;
803 std::copy_n(header_->Magic(), kMagicAndVersionLen, header.magic_);
804 }
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700805 header.checksum_ = header_->Checksum();
806 std::copy_n(header_->Signature(), DexFile::kSha1DigestSize, header.signature_);
807 header.file_size_ = header_->FileSize();
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800808 header.header_size_ = GetHeaderSize();
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700809 header.endian_tag_ = header_->EndianTag();
810 header.link_size_ = header_->LinkSize();
811 header.link_off_ = header_->LinkOffset();
David Sehr2b5a38f2018-06-14 15:13:04 -0700812 header.map_off_ = header_->MapListOffset();
813 header.string_ids_size_ = header_->StringIds().Size();
814 header.string_ids_off_ = header_->StringIds().GetOffset();
815 header.type_ids_size_ = header_->TypeIds().Size();
816 header.type_ids_off_ = header_->TypeIds().GetOffset();
817 header.proto_ids_size_ = header_->ProtoIds().Size();
818 header.proto_ids_off_ = header_->ProtoIds().GetOffset();
819 header.field_ids_size_ = header_->FieldIds().Size();
820 header.field_ids_off_ = header_->FieldIds().GetOffset();
821 header.method_ids_size_ = header_->MethodIds().Size();
822 header.method_ids_off_ = header_->MethodIds().GetOffset();
823 header.class_defs_size_ = header_->ClassDefs().Size();
824 header.class_defs_off_ = header_->ClassDefs().GetOffset();
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700825 header.data_size_ = header_->DataSize();
826 header.data_off_ = header_->DataOffset();
827
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800828 CHECK_EQ(sizeof(header), GetHeaderSize());
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700829 static_assert(sizeof(header) == 0x70, "Size doesn't match dex spec");
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800830 stream->Seek(0);
831 stream->Overwrite(reinterpret_cast<uint8_t*>(&header), sizeof(header));
Jeff Haoa8621002016-10-04 18:13:44 +0000832}
833
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800834size_t DexWriter::GetHeaderSize() const {
835 return sizeof(StandardDexFile::Header);
836}
837
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800838bool DexWriter::Write(DexContainer* output, std::string* error_msg) {
839 DCHECK(error_msg != nullptr);
840
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800841 Stream stream_storage(output->GetMainSection());
842 Stream* stream = &stream_storage;
843
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800844 // Starting offset is right after the header.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800845 stream->Seek(GetHeaderSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800846
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800847 // Based on: https://source.android.com/devices/tech/dalvik/dex-format
848 // Since the offsets may not be calculated already, the writing must be done in the correct order.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800849 const uint32_t string_ids_offset = stream->Tell();
Andreas Gampe9b031f72018-10-04 11:03:34 -0700850 WriteStringIds(stream, /*reserve_only=*/ true);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800851 WriteTypeIds(stream);
852 const uint32_t proto_ids_offset = stream->Tell();
Andreas Gampe9b031f72018-10-04 11:03:34 -0700853 WriteProtoIds(stream, /*reserve_only=*/ true);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800854 WriteFieldIds(stream);
855 WriteMethodIds(stream);
856 const uint32_t class_defs_offset = stream->Tell();
Andreas Gampe9b031f72018-10-04 11:03:34 -0700857 WriteClassDefs(stream, /*reserve_only=*/ true);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800858 const uint32_t call_site_ids_offset = stream->Tell();
Andreas Gampe9b031f72018-10-04 11:03:34 -0700859 WriteCallSiteIds(stream, /*reserve_only=*/ true);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800860 WriteMethodHandles(stream);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800861
862 uint32_t data_offset_ = 0u;
863 if (compute_offsets_) {
864 // Data section.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800865 stream->AlignTo(kDataSectionAlignment);
866 data_offset_ = stream->Tell();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800867 }
868
869 // Write code item first to minimize the space required for encoded methods.
870 // Reserve code item space since we need the debug offsets to actually write them.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800871 const uint32_t code_items_offset = stream->Tell();
Andreas Gampe9b031f72018-10-04 11:03:34 -0700872 WriteCodeItems(stream, /*reserve_only=*/ true);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800873 // Write debug info section.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800874 WriteDebugInfoItems(stream);
875 {
876 // Actually write code items since debug info offsets are calculated now.
877 Stream::ScopedSeek seek(stream, code_items_offset);
Andreas Gampe9b031f72018-10-04 11:03:34 -0700878 WriteCodeItems(stream, /*reserve_only=*/ false);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800879 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800880
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800881 WriteEncodedArrays(stream);
882 WriteAnnotations(stream);
883 WriteAnnotationSets(stream);
884 WriteAnnotationSetRefs(stream);
885 WriteAnnotationsDirectories(stream);
886 WriteTypeLists(stream);
887 WriteClassDatas(stream);
888 WriteStringDatas(stream);
David Brazdil20c765f2018-10-27 21:45:15 +0000889 WriteHiddenapiClassData(stream);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800890
891 // Write delayed id sections that depend on data sections.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800892 {
893 Stream::ScopedSeek seek(stream, string_ids_offset);
Andreas Gampe9b031f72018-10-04 11:03:34 -0700894 WriteStringIds(stream, /*reserve_only=*/ false);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800895 }
896 {
897 Stream::ScopedSeek seek(stream, proto_ids_offset);
Andreas Gampe9b031f72018-10-04 11:03:34 -0700898 WriteProtoIds(stream, /*reserve_only=*/ false);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800899 }
900 {
901 Stream::ScopedSeek seek(stream, class_defs_offset);
Andreas Gampe9b031f72018-10-04 11:03:34 -0700902 WriteClassDefs(stream, /*reserve_only=*/ false);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800903 }
904 {
905 Stream::ScopedSeek seek(stream, call_site_ids_offset);
Andreas Gampe9b031f72018-10-04 11:03:34 -0700906 WriteCallSiteIds(stream, /*reserve_only=*/ false);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800907 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800908
909 // Write the map list.
910 if (compute_offsets_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800911 stream->AlignTo(SectionAlignment(DexFile::kDexTypeMapList));
David Sehr2b5a38f2018-06-14 15:13:04 -0700912 header_->SetMapListOffset(stream->Tell());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800913 } else {
David Sehr2b5a38f2018-06-14 15:13:04 -0700914 stream->Seek(header_->MapListOffset());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800915 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800916 GenerateAndWriteMapItems(stream);
917 stream->AlignTo(kDataSectionAlignment);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800918
919 // Map items are included in the data section.
920 if (compute_offsets_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800921 header_->SetDataSize(stream->Tell() - data_offset_);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800922 if (header_->DataSize() != 0) {
923 // Offset must be zero when the size is zero.
924 header_->SetDataOffset(data_offset_);
925 } else {
926 header_->SetDataOffset(0u);
927 }
928 }
929
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800930 // Write link data if it exists.
David Sehr2b5a38f2018-06-14 15:13:04 -0700931 const std::vector<uint8_t>& link_data = header_->LinkData();
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800932 if (link_data.size() > 0) {
933 CHECK_EQ(header_->LinkSize(), static_cast<uint32_t>(link_data.size()));
934 if (compute_offsets_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800935 header_->SetLinkOffset(stream->Tell());
936 } else {
937 stream->Seek(header_->LinkOffset());
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800938 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800939 stream->Write(&link_data[0], link_data.size());
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800940 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800941
942 // Write header last.
943 if (compute_offsets_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800944 header_->SetFileSize(stream->Tell());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800945 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800946 WriteHeader(stream);
Mathieu Chartier2c4b0842017-12-13 11:49:51 -0800947
948 if (dex_layout_->GetOptions().update_checksum_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800949 header_->SetChecksum(DexFile::CalculateChecksum(stream->Begin(), header_->FileSize()));
Mathieu Chartier2c4b0842017-12-13 11:49:51 -0800950 // Rewrite the header with the calculated checksum.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800951 WriteHeader(stream);
Mathieu Chartier2c4b0842017-12-13 11:49:51 -0800952 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800953
954 // Trim the map to make it sized as large as the dex file.
955 output->GetMainSection()->Resize(header_->FileSize());
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800956 return true;
Jeff Haoa8621002016-10-04 18:13:44 +0000957}
958
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800959bool DexWriter::Output(DexLayout* dex_layout,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800960 std::unique_ptr<DexContainer>* container,
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800961 bool compute_offsets,
962 std::string* error_msg) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800963 CHECK(dex_layout != nullptr);
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700964 std::unique_ptr<DexWriter> writer;
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800965 if (dex_layout->GetOptions().compact_dex_level_ != CompactDexLevel::kCompactDexLevelNone) {
966 CHECK(compute_offsets) << "Compact dex requires computing offsets";
967 writer.reset(new CompactDexWriter(dex_layout));
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700968 } else {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800969 writer.reset(new DexWriter(dex_layout, compute_offsets));
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700970 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800971 DCHECK(container != nullptr);
972 if (*container == nullptr) {
973 *container = writer->CreateDexContainer();
974 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800975 return writer->Write(container->get(), error_msg);
Jeff Haoa8621002016-10-04 18:13:44 +0000976}
977
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800978void MapItemQueue::AddIfNotEmpty(const MapItem& item) {
979 if (item.size_ != 0) {
980 push(item);
981 }
982}
983
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800984void DexWriter::ProcessOffset(Stream* stream, dex_ir::Item* item) {
985 if (compute_offsets_) {
986 item->SetOffset(stream->Tell());
987 } else {
988 // Not computing offsets, just use the one in the item.
989 stream->Seek(item->GetOffset());
990 }
991}
992
David Brazdil17834802019-01-21 19:46:46 +0000993void DexWriter::ProcessOffset(Stream* stream, dex_ir::CollectionBase* item) {
994 if (compute_offsets_) {
995 item->SetOffset(stream->Tell());
996 } else {
997 // Not computing offsets, just use the one in the item.
998 stream->Seek(item->GetOffset());
999 }
1000}
1001
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001002std::unique_ptr<DexContainer> DexWriter::CreateDexContainer() const {
1003 return std::unique_ptr<DexContainer>(new DexWriter::Container);
1004}
1005
Jeff Haoa8621002016-10-04 18:13:44 +00001006} // namespace art