blob: eead13f69a9f838cf585f77ef585e3f897309e66 [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;
210 for (std::unique_ptr<dex_ir::FieldItem>& field : *fields) {
211 uint32_t index = field->GetFieldId()->GetIndex();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800212 stream->WriteUleb128(index - prev_index);
213 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;
220 for (std::unique_ptr<dex_ir::MethodItem>& 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);
224 stream->WriteUleb128(method->GetAccessFlags());
225 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();
Jeff Haoea7c6292016-11-14 18:10:16 -0800234 for (std::unique_ptr<dex_ir::StringId>& string_id : header_->GetCollections().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()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800244 header_->GetCollections().SetStringIdsOffset(start);
245 }
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();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800259 for (std::unique_ptr<dex_ir::StringData>& string_data : header_->GetCollections().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()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800263 header_->GetCollections().SetStringDatasOffset(start);
264 }
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();
Jeff Haoea7c6292016-11-14 18:10:16 -0800270 for (std::unique_ptr<dex_ir::TypeId>& type_id : header_->GetCollections().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()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800277 header_->GetCollections().SetTypeIdsOffset(start);
278 }
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();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800285 for (std::unique_ptr<dex_ir::TypeList>& type_list : header_->GetCollections().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()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800296 header_->GetCollections().SetTypeListsOffset(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();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800303 for (std::unique_ptr<dex_ir::ProtoId>& proto_id : header_->GetCollections().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()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800316 header_->GetCollections().SetProtoIdsOffset(start);
317 }
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();
Jeff Haoea7c6292016-11-14 18:10:16 -0800323 for (std::unique_ptr<dex_ir::FieldId>& field_id : header_->GetCollections().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()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800333 header_->GetCollections().SetFieldIdsOffset(start);
334 }
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();
Jeff Haoea7c6292016-11-14 18:10:16 -0800340 for (std::unique_ptr<dex_ir::MethodId>& method_id : header_->GetCollections().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()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800350 header_->GetCollections().SetMethodIdsOffset(start);
351 }
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();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800356 for (std::unique_ptr<dex_ir::EncodedArrayItem>& encoded_array :
357 header_->GetCollections().EncodedArrayItems()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800358 stream->AlignTo(SectionAlignment(DexFile::kDexTypeEncodedArrayItem));
359 ProcessOffset(stream, encoded_array.get());
360 WriteEncodedArray(stream, encoded_array->GetEncodedValues());
Jeff Haoa8621002016-10-04 18:13:44 +0000361 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800362 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800363 header_->GetCollections().SetEncodedArrayItemsOffset(start);
364 }
Jeff Haoa8621002016-10-04 18:13:44 +0000365}
366
Mathieu Chartier5e496142018-01-27 13:11:14 -0800367void DexWriter::WriteAnnotations(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000368 uint8_t visibility[1];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800369 const uint32_t start = stream->Tell();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800370 for (std::unique_ptr<dex_ir::AnnotationItem>& annotation :
371 header_->GetCollections().AnnotationItems()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800372 stream->AlignTo(SectionAlignment(DexFile::kDexTypeAnnotationItem));
Jeff Haoa8621002016-10-04 18:13:44 +0000373 visibility[0] = annotation->GetVisibility();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800374 ProcessOffset(stream, annotation.get());
375 stream->Write(visibility, sizeof(uint8_t));
376 WriteEncodedAnnotation(stream, annotation->GetAnnotation());
Jeff Haoa8621002016-10-04 18:13:44 +0000377 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800378 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800379 header_->GetCollections().SetAnnotationItemsOffset(start);
380 }
Jeff Haoa8621002016-10-04 18:13:44 +0000381}
382
Mathieu Chartier5e496142018-01-27 13:11:14 -0800383void DexWriter::WriteAnnotationSets(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000384 uint32_t size[1];
385 uint32_t annotation_off[1];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800386 const uint32_t start = stream->Tell();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800387 for (std::unique_ptr<dex_ir::AnnotationSetItem>& annotation_set :
388 header_->GetCollections().AnnotationSetItems()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800389 stream->AlignTo(SectionAlignment(DexFile::kDexTypeAnnotationSetItem));
Jeff Haoa8621002016-10-04 18:13:44 +0000390 size[0] = annotation_set->GetItems()->size();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800391 ProcessOffset(stream, annotation_set.get());
392 stream->Write(size, sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000393 for (dex_ir::AnnotationItem* annotation : *annotation_set->GetItems()) {
394 annotation_off[0] = annotation->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800395 stream->Write(annotation_off, sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000396 }
397 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800398 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800399 header_->GetCollections().SetAnnotationSetItemsOffset(start);
400 }
Jeff Haoa8621002016-10-04 18:13:44 +0000401}
402
Mathieu Chartier5e496142018-01-27 13:11:14 -0800403void DexWriter::WriteAnnotationSetRefs(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000404 uint32_t size[1];
405 uint32_t annotations_off[1];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800406 const uint32_t start = stream->Tell();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800407 for (std::unique_ptr<dex_ir::AnnotationSetRefList>& annotation_set_ref :
408 header_->GetCollections().AnnotationSetRefLists()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800409 stream->AlignTo(SectionAlignment(DexFile::kDexTypeAnnotationSetRefList));
Jeff Haoa8621002016-10-04 18:13:44 +0000410 size[0] = annotation_set_ref->GetItems()->size();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800411 ProcessOffset(stream, annotation_set_ref.get());
412 stream->Write(size, sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000413 for (dex_ir::AnnotationSetItem* annotation_set : *annotation_set_ref->GetItems()) {
414 annotations_off[0] = annotation_set == nullptr ? 0 : annotation_set->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800415 stream->Write(annotations_off, sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000416 }
417 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800418 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800419 header_->GetCollections().SetAnnotationSetRefListsOffset(start);
420 }
Jeff Haoa8621002016-10-04 18:13:44 +0000421}
422
Mathieu Chartier5e496142018-01-27 13:11:14 -0800423void DexWriter::WriteAnnotationsDirectories(Stream* stream) {
Jeff Haoa8621002016-10-04 18:13:44 +0000424 uint32_t directory_buffer[4];
425 uint32_t annotation_buffer[2];
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800426 const uint32_t start = stream->Tell();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800427 for (std::unique_ptr<dex_ir::AnnotationsDirectoryItem>& annotations_directory :
428 header_->GetCollections().AnnotationsDirectoryItems()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800429 stream->AlignTo(SectionAlignment(DexFile::kDexTypeAnnotationsDirectoryItem));
430 ProcessOffset(stream, annotations_directory.get());
Jeff Haoa8621002016-10-04 18:13:44 +0000431 directory_buffer[0] = annotations_directory->GetClassAnnotation() == nullptr ? 0 :
432 annotations_directory->GetClassAnnotation()->GetOffset();
433 directory_buffer[1] = annotations_directory->GetFieldAnnotations() == nullptr ? 0 :
434 annotations_directory->GetFieldAnnotations()->size();
435 directory_buffer[2] = annotations_directory->GetMethodAnnotations() == nullptr ? 0 :
436 annotations_directory->GetMethodAnnotations()->size();
437 directory_buffer[3] = annotations_directory->GetParameterAnnotations() == nullptr ? 0 :
438 annotations_directory->GetParameterAnnotations()->size();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800439 stream->Write(directory_buffer, 4 * sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000440 if (annotations_directory->GetFieldAnnotations() != nullptr) {
441 for (std::unique_ptr<dex_ir::FieldAnnotation>& field :
442 *annotations_directory->GetFieldAnnotations()) {
443 annotation_buffer[0] = field->GetFieldId()->GetIndex();
444 annotation_buffer[1] = field->GetAnnotationSetItem()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800445 stream->Write(annotation_buffer, 2 * sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000446 }
447 }
448 if (annotations_directory->GetMethodAnnotations() != nullptr) {
449 for (std::unique_ptr<dex_ir::MethodAnnotation>& method :
450 *annotations_directory->GetMethodAnnotations()) {
451 annotation_buffer[0] = method->GetMethodId()->GetIndex();
452 annotation_buffer[1] = method->GetAnnotationSetItem()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800453 stream->Write(annotation_buffer, 2 * sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000454 }
455 }
456 if (annotations_directory->GetParameterAnnotations() != nullptr) {
457 for (std::unique_ptr<dex_ir::ParameterAnnotation>& parameter :
458 *annotations_directory->GetParameterAnnotations()) {
459 annotation_buffer[0] = parameter->GetMethodId()->GetIndex();
460 annotation_buffer[1] = parameter->GetAnnotations()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800461 stream->Write(annotation_buffer, 2 * sizeof(uint32_t));
Jeff Haoa8621002016-10-04 18:13:44 +0000462 }
463 }
464 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800465 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800466 header_->GetCollections().SetAnnotationsDirectoryItemsOffset(start);
Jeff Haoa8621002016-10-04 18:13:44 +0000467 }
468}
469
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800470void DexWriter::WriteDebugInfoItem(Stream* stream, dex_ir::DebugInfoItem* debug_info) {
471 stream->AlignTo(SectionAlignment(DexFile::kDexTypeDebugInfoItem));
472 ProcessOffset(stream, debug_info);
473 stream->Write(debug_info->GetDebugInfo(), debug_info->GetDebugInfoSize());
474}
475
Mathieu Chartier5e496142018-01-27 13:11:14 -0800476void DexWriter::WriteDebugInfoItems(Stream* stream) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800477 const uint32_t start = stream->Tell();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800478 for (std::unique_ptr<dex_ir::DebugInfoItem>& debug_info :
479 header_->GetCollections().DebugInfoItems()) {
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800480 WriteDebugInfoItem(stream, debug_info.get());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800481 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800482 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800483 header_->GetCollections().SetDebugInfoItemsOffset(start);
484 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800485}
486
Mathieu Chartier5e496142018-01-27 13:11:14 -0800487void DexWriter::WriteCodeItemPostInstructionData(Stream* stream,
488 dex_ir::CodeItem* code_item,
489 bool reserve_only) {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800490 if (code_item->TriesSize() != 0) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800491 stream->AlignTo(DexFile::TryItem::kAlignment);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800492 // Write try items.
493 for (std::unique_ptr<const dex_ir::TryItem>& try_item : *code_item->Tries()) {
494 DexFile::TryItem disk_try_item;
495 if (!reserve_only) {
496 disk_try_item.start_addr_ = try_item->StartAddr();
497 disk_try_item.insn_count_ = try_item->InsnCount();
498 disk_try_item.handler_off_ = try_item->GetHandlers()->GetListOffset();
499 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800500 stream->Write(&disk_try_item, sizeof(disk_try_item));
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800501 }
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800502 // Leave offset pointing to the end of the try items.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800503 const size_t offset = stream->Tell();
504 size_t max_offset = offset + stream->WriteUleb128(code_item->Handlers()->size());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800505 for (std::unique_ptr<const dex_ir::CatchHandler>& handlers : *code_item->Handlers()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800506 stream->Seek(offset + handlers->GetListOffset());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800507 uint32_t size = handlers->HasCatchAll() ? (handlers->GetHandlers()->size() - 1) * -1 :
508 handlers->GetHandlers()->size();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800509 stream->WriteSleb128(size);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800510 for (std::unique_ptr<const dex_ir::TypeAddrPair>& handler : *handlers->GetHandlers()) {
511 if (handler->GetTypeId() != nullptr) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800512 stream->WriteUleb128(handler->GetTypeId()->GetIndex());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800513 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800514 stream->WriteUleb128(handler->GetAddress());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800515 }
516 // TODO: Clean this up to write the handlers in address order.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800517 max_offset = std::max(max_offset, stream->Tell());
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800518 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800519 stream->Seek(max_offset);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800520 }
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800521}
522
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800523void DexWriter::WriteCodeItem(Stream* stream,
524 dex_ir::CodeItem* code_item,
525 bool reserve_only) {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800526 DCHECK(code_item != nullptr);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800527 const uint32_t start_offset = stream->Tell();
528 stream->AlignTo(SectionAlignment(DexFile::kDexTypeCodeItem));
529 ProcessOffset(stream, code_item);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800530
531 StandardDexFile::CodeItem disk_code_item;
532 if (!reserve_only) {
533 disk_code_item.registers_size_ = code_item->RegistersSize();
534 disk_code_item.ins_size_ = code_item->InsSize();
535 disk_code_item.outs_size_ = code_item->OutsSize();
536 disk_code_item.tries_size_ = code_item->TriesSize();
537 disk_code_item.debug_info_off_ = code_item->DebugInfo() == nullptr
538 ? 0
539 : code_item->DebugInfo()->GetOffset();
540 disk_code_item.insns_size_in_code_units_ = code_item->InsnsSize();
541 }
542 // Avoid using sizeof so that we don't write the fake instruction array at the end of the code
543 // item.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800544 stream->Write(&disk_code_item, OFFSETOF_MEMBER(StandardDexFile::CodeItem, insns_));
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800545 // Write the instructions.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800546 stream->Write(code_item->Insns(), code_item->InsnsSize() * sizeof(uint16_t));
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800547 // Write the post instruction data.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800548 WriteCodeItemPostInstructionData(stream, code_item, reserve_only);
549 if (reserve_only) {
550 stream->Clear(start_offset, stream->Tell() - start_offset);
551 }
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800552}
553
Mathieu Chartier5e496142018-01-27 13:11:14 -0800554void DexWriter::WriteCodeItems(Stream* stream, bool reserve_only) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800555 DexLayoutSection* code_section = nullptr;
556 if (!reserve_only && dex_layout_ != nullptr) {
557 code_section = &dex_layout_->GetSections().sections_[static_cast<size_t>(
558 DexLayoutSections::SectionType::kSectionTypeCode)];
559 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800560 const uint32_t start = stream->Tell();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800561 for (auto& code_item : header_->GetCollections().CodeItems()) {
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800562 uint32_t start_offset = stream->Tell();
563 WriteCodeItem(stream, code_item.get(), reserve_only);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800564 // Only add the section hotness info once.
565 if (!reserve_only && code_section != nullptr) {
566 auto it = dex_layout_->LayoutHotnessInfo().code_item_layout_.find(code_item.get());
567 if (it != dex_layout_->LayoutHotnessInfo().code_item_layout_.end()) {
568 code_section->parts_[static_cast<size_t>(it->second)].CombineSection(
Mathieu Chartierb81ecad2018-01-23 22:08:26 -0800569 start_offset,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800570 stream->Tell());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800571 }
572 }
Jeff Haoa8621002016-10-04 18:13:44 +0000573 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800574
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800575 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800576 header_->GetCollections().SetCodeItemsOffset(start);
577 }
Jeff Haoa8621002016-10-04 18:13:44 +0000578}
579
Mathieu Chartier5e496142018-01-27 13:11:14 -0800580void DexWriter::WriteClassDefs(Stream* stream, bool reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800581 const uint32_t start = stream->Tell();
Jeff Haoa8621002016-10-04 18:13:44 +0000582 uint32_t class_def_buffer[8];
Jeff Haoea7c6292016-11-14 18:10:16 -0800583 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800584 stream->AlignTo(SectionAlignment(DexFile::kDexTypeClassDefItem));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800585 if (reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800586 stream->Skip(class_def->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800587 } else {
588 class_def_buffer[0] = class_def->ClassType()->GetIndex();
589 class_def_buffer[1] = class_def->GetAccessFlags();
590 class_def_buffer[2] = class_def->Superclass() == nullptr ? dex::kDexNoIndex :
591 class_def->Superclass()->GetIndex();
592 class_def_buffer[3] = class_def->InterfacesOffset();
593 class_def_buffer[4] = class_def->SourceFile() == nullptr ? dex::kDexNoIndex :
594 class_def->SourceFile()->GetIndex();
595 class_def_buffer[5] = class_def->Annotations() == nullptr ? 0 :
596 class_def->Annotations()->GetOffset();
597 class_def_buffer[6] = class_def->GetClassData() == nullptr ? 0 :
598 class_def->GetClassData()->GetOffset();
599 class_def_buffer[7] = class_def->StaticValues() == nullptr ? 0 :
600 class_def->StaticValues()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800601 stream->Write(class_def_buffer, class_def->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800602 }
Jeff Haoa8621002016-10-04 18:13:44 +0000603 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800604 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800605 header_->GetCollections().SetClassDefsOffset(start);
606 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800607}
Jeff Haoa8621002016-10-04 18:13:44 +0000608
Mathieu Chartier5e496142018-01-27 13:11:14 -0800609void DexWriter::WriteClassDatas(Stream* stream) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800610 const uint32_t start = stream->Tell();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800611 for (const std::unique_ptr<dex_ir::ClassData>& class_data :
612 header_->GetCollections().ClassDatas()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800613 stream->AlignTo(SectionAlignment(DexFile::kDexTypeClassDataItem));
614 ProcessOffset(stream, class_data.get());
615 stream->WriteUleb128(class_data->StaticFields()->size());
616 stream->WriteUleb128(class_data->InstanceFields()->size());
617 stream->WriteUleb128(class_data->DirectMethods()->size());
618 stream->WriteUleb128(class_data->VirtualMethods()->size());
619 WriteEncodedFields(stream, class_data->StaticFields());
620 WriteEncodedFields(stream, class_data->InstanceFields());
621 WriteEncodedMethods(stream, class_data->DirectMethods());
622 WriteEncodedMethods(stream, class_data->VirtualMethods());
Jeff Haoa8621002016-10-04 18:13:44 +0000623 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800624 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800625 header_->GetCollections().SetClassDatasOffset(start);
626 }
Jeff Haoa8621002016-10-04 18:13:44 +0000627}
628
Mathieu Chartier5e496142018-01-27 13:11:14 -0800629void DexWriter::WriteCallSiteIds(Stream* stream, bool reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800630 const uint32_t start = stream->Tell();
Jeff Hao5daee902017-04-27 18:00:38 -0700631 uint32_t call_site_off[1];
632 for (std::unique_ptr<dex_ir::CallSiteId>& call_site_id :
633 header_->GetCollections().CallSiteIds()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800634 stream->AlignTo(SectionAlignment(DexFile::kDexTypeCallSiteIdItem));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800635 if (reserve_only) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800636 stream->Skip(call_site_id->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800637 } else {
638 call_site_off[0] = call_site_id->CallSiteItem()->GetOffset();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800639 stream->Write(call_site_off, call_site_id->GetSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800640 }
Jeff Hao5daee902017-04-27 18:00:38 -0700641 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800642 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800643 header_->GetCollections().SetCallSiteIdsOffset(start);
644 }
Jeff Hao5daee902017-04-27 18:00:38 -0700645}
646
Mathieu Chartier5e496142018-01-27 13:11:14 -0800647void DexWriter::WriteMethodHandles(Stream* stream) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800648 const uint32_t start = stream->Tell();
Jeff Hao5daee902017-04-27 18:00:38 -0700649 uint16_t method_handle_buff[4];
650 for (std::unique_ptr<dex_ir::MethodHandleItem>& method_handle :
651 header_->GetCollections().MethodHandleItems()) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800652 stream->AlignTo(SectionAlignment(DexFile::kDexTypeMethodHandleItem));
Jeff Hao5daee902017-04-27 18:00:38 -0700653 method_handle_buff[0] = static_cast<uint16_t>(method_handle->GetMethodHandleType());
654 method_handle_buff[1] = 0; // unused.
655 method_handle_buff[2] = method_handle->GetFieldOrMethodId()->GetIndex();
656 method_handle_buff[3] = 0; // unused.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800657 stream->Write(method_handle_buff, method_handle->GetSize());
Jeff Hao5daee902017-04-27 18:00:38 -0700658 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800659 if (compute_offsets_ && start != stream->Tell()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800660 header_->GetCollections().SetMethodHandleItemsOffset(start);
661 }
Jeff Hao5daee902017-04-27 18:00:38 -0700662}
663
Mathieu Chartier5e496142018-01-27 13:11:14 -0800664void DexWriter::WriteMapItems(Stream* stream, MapItemQueue* queue) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800665 // All the sections should already have been added.
Mathieu Chartier9b302bf2018-01-25 13:08:08 -0800666 const uint32_t map_list_size = queue->size();
Mathieu Chartier9b302bf2018-01-25 13:08:08 -0800667 stream->Write(&map_list_size, sizeof(map_list_size));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800668 while (!queue->empty()) {
Mathieu Chartier9b302bf2018-01-25 13:08:08 -0800669 const MapItem& item = queue->top();
670 DexFile::MapItem map_item;
671 map_item.type_ = item.type_;
672 map_item.size_ = item.size_;
673 map_item.offset_ = item.offset_;
674 map_item.unused_ = 0u;
675 stream->Write(&map_item, sizeof(map_item));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800676 queue->pop();
Jeff Haoa8621002016-10-04 18:13:44 +0000677 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800678}
679
Mathieu Chartier5e496142018-01-27 13:11:14 -0800680void DexWriter::GenerateAndWriteMapItems(Stream* stream) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800681 dex_ir::Collections& collection = header_->GetCollections();
682 MapItemQueue queue;
683
684 // Header and index section.
685 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeHeaderItem, 1, 0));
686 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeStringIdItem,
687 collection.StringIdsSize(),
688 collection.StringIdsOffset()));
689 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeTypeIdItem,
690 collection.TypeIdsSize(),
691 collection.TypeIdsOffset()));
692 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeProtoIdItem,
693 collection.ProtoIdsSize(),
694 collection.ProtoIdsOffset()));
695 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeFieldIdItem,
696 collection.FieldIdsSize(),
697 collection.FieldIdsOffset()));
698 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeMethodIdItem,
699 collection.MethodIdsSize(),
700 collection.MethodIdsOffset()));
701 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeClassDefItem,
702 collection.ClassDefsSize(),
703 collection.ClassDefsOffset()));
704 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeCallSiteIdItem,
705 collection.CallSiteIdsSize(),
706 collection.CallSiteIdsOffset()));
707 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeMethodHandleItem,
708 collection.MethodHandleItemsSize(),
709 collection.MethodHandleItemsOffset()));
710 // Data section.
711 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeMapList, 1, collection.MapListOffset()));
712 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeTypeList,
713 collection.TypeListsSize(),
714 collection.TypeListsOffset()));
715 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeAnnotationSetRefList,
716 collection.AnnotationSetRefListsSize(),
717 collection.AnnotationSetRefListsOffset()));
718 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeAnnotationSetItem,
719 collection.AnnotationSetItemsSize(),
720 collection.AnnotationSetItemsOffset()));
721 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeClassDataItem,
722 collection.ClassDatasSize(),
723 collection.ClassDatasOffset()));
724 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeCodeItem,
725 collection.CodeItemsSize(),
726 collection.CodeItemsOffset()));
727 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeStringDataItem,
728 collection.StringDatasSize(),
729 collection.StringDatasOffset()));
730 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeDebugInfoItem,
731 collection.DebugInfoItemsSize(),
732 collection.DebugInfoItemsOffset()));
733 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeAnnotationItem,
734 collection.AnnotationItemsSize(),
735 collection.AnnotationItemsOffset()));
736 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeEncodedArrayItem,
737 collection.EncodedArrayItemsSize(),
738 collection.EncodedArrayItemsOffset()));
739 queue.AddIfNotEmpty(MapItem(DexFile::kDexTypeAnnotationsDirectoryItem,
740 collection.AnnotationsDirectoryItemsSize(),
741 collection.AnnotationsDirectoryItemsOffset()));
Mathieu Chartier5e496142018-01-27 13:11:14 -0800742 WriteMapItems(stream, &queue);
Jeff Haoa8621002016-10-04 18:13:44 +0000743}
744
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800745void DexWriter::WriteHeader(Stream* stream) {
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700746 StandardDexFile::Header header;
Mathieu Chartier21cf2582018-01-08 17:09:48 -0800747 if (CompactDexFile::IsMagicValid(header_->Magic())) {
748 StandardDexFile::WriteMagic(header.magic_);
749 // TODO: Should we write older versions based on the feature flags?
750 StandardDexFile::WriteCurrentVersion(header.magic_);
751 } else {
752 // Standard dex -> standard dex, just reuse the same header.
753 static constexpr size_t kMagicAndVersionLen =
754 StandardDexFile::kDexMagicSize + StandardDexFile::kDexVersionLen;
755 std::copy_n(header_->Magic(), kMagicAndVersionLen, header.magic_);
756 }
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700757 header.checksum_ = header_->Checksum();
758 std::copy_n(header_->Signature(), DexFile::kSha1DigestSize, header.signature_);
759 header.file_size_ = header_->FileSize();
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800760 header.header_size_ = GetHeaderSize();
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700761 header.endian_tag_ = header_->EndianTag();
762 header.link_size_ = header_->LinkSize();
763 header.link_off_ = header_->LinkOffset();
764 const dex_ir::Collections& collections = header_->GetCollections();
765 header.map_off_ = collections.MapListOffset();
766 header.string_ids_size_ = collections.StringIdsSize();
767 header.string_ids_off_ = collections.StringIdsOffset();
768 header.type_ids_size_ = collections.TypeIdsSize();
769 header.type_ids_off_ = collections.TypeIdsOffset();
770 header.proto_ids_size_ = collections.ProtoIdsSize();
771 header.proto_ids_off_ = collections.ProtoIdsOffset();
772 header.field_ids_size_ = collections.FieldIdsSize();
773 header.field_ids_off_ = collections.FieldIdsOffset();
774 header.method_ids_size_ = collections.MethodIdsSize();
775 header.method_ids_off_ = collections.MethodIdsOffset();
776 header.class_defs_size_ = collections.ClassDefsSize();
777 header.class_defs_off_ = collections.ClassDefsOffset();
778 header.data_size_ = header_->DataSize();
779 header.data_off_ = header_->DataOffset();
780
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800781 CHECK_EQ(sizeof(header), GetHeaderSize());
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700782 static_assert(sizeof(header) == 0x70, "Size doesn't match dex spec");
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800783 stream->Seek(0);
784 stream->Overwrite(reinterpret_cast<uint8_t*>(&header), sizeof(header));
Jeff Haoa8621002016-10-04 18:13:44 +0000785}
786
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800787size_t DexWriter::GetHeaderSize() const {
788 return sizeof(StandardDexFile::Header);
789}
790
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800791bool DexWriter::Write(DexContainer* output, std::string* error_msg) {
792 DCHECK(error_msg != nullptr);
793
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800794 Stream stream_storage(output->GetMainSection());
795 Stream* stream = &stream_storage;
796
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800797 // Starting offset is right after the header.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800798 stream->Seek(GetHeaderSize());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800799
800 dex_ir::Collections& collection = header_->GetCollections();
801
802 // Based on: https://source.android.com/devices/tech/dalvik/dex-format
803 // Since the offsets may not be calculated already, the writing must be done in the correct order.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800804 const uint32_t string_ids_offset = stream->Tell();
805 WriteStringIds(stream, /*reserve_only*/ true);
806 WriteTypeIds(stream);
807 const uint32_t proto_ids_offset = stream->Tell();
808 WriteProtoIds(stream, /*reserve_only*/ true);
809 WriteFieldIds(stream);
810 WriteMethodIds(stream);
811 const uint32_t class_defs_offset = stream->Tell();
812 WriteClassDefs(stream, /*reserve_only*/ true);
813 const uint32_t call_site_ids_offset = stream->Tell();
814 WriteCallSiteIds(stream, /*reserve_only*/ true);
815 WriteMethodHandles(stream);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800816
817 uint32_t data_offset_ = 0u;
818 if (compute_offsets_) {
819 // Data section.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800820 stream->AlignTo(kDataSectionAlignment);
821 data_offset_ = stream->Tell();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800822 }
823
824 // Write code item first to minimize the space required for encoded methods.
825 // Reserve code item space since we need the debug offsets to actually write them.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800826 const uint32_t code_items_offset = stream->Tell();
827 WriteCodeItems(stream, /*reserve_only*/ true);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800828 // Write debug info section.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800829 WriteDebugInfoItems(stream);
830 {
831 // Actually write code items since debug info offsets are calculated now.
832 Stream::ScopedSeek seek(stream, code_items_offset);
833 WriteCodeItems(stream, /*reserve_only*/ false);
834 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800835
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800836 WriteEncodedArrays(stream);
837 WriteAnnotations(stream);
838 WriteAnnotationSets(stream);
839 WriteAnnotationSetRefs(stream);
840 WriteAnnotationsDirectories(stream);
841 WriteTypeLists(stream);
842 WriteClassDatas(stream);
843 WriteStringDatas(stream);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800844
845 // Write delayed id sections that depend on data sections.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800846 {
847 Stream::ScopedSeek seek(stream, string_ids_offset);
848 WriteStringIds(stream, /*reserve_only*/ false);
849 }
850 {
851 Stream::ScopedSeek seek(stream, proto_ids_offset);
852 WriteProtoIds(stream, /*reserve_only*/ false);
853 }
854 {
855 Stream::ScopedSeek seek(stream, class_defs_offset);
856 WriteClassDefs(stream, /*reserve_only*/ false);
857 }
858 {
859 Stream::ScopedSeek seek(stream, call_site_ids_offset);
860 WriteCallSiteIds(stream, /*reserve_only*/ false);
861 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800862
863 // Write the map list.
864 if (compute_offsets_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800865 stream->AlignTo(SectionAlignment(DexFile::kDexTypeMapList));
866 collection.SetMapListOffset(stream->Tell());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800867 } else {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800868 stream->Seek(collection.MapListOffset());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800869 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800870 GenerateAndWriteMapItems(stream);
871 stream->AlignTo(kDataSectionAlignment);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800872
873 // Map items are included in the data section.
874 if (compute_offsets_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800875 header_->SetDataSize(stream->Tell() - data_offset_);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800876 if (header_->DataSize() != 0) {
877 // Offset must be zero when the size is zero.
878 header_->SetDataOffset(data_offset_);
879 } else {
880 header_->SetDataOffset(0u);
881 }
882 }
883
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800884 // Write link data if it exists.
885 const std::vector<uint8_t>& link_data = collection.LinkData();
886 if (link_data.size() > 0) {
887 CHECK_EQ(header_->LinkSize(), static_cast<uint32_t>(link_data.size()));
888 if (compute_offsets_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800889 header_->SetLinkOffset(stream->Tell());
890 } else {
891 stream->Seek(header_->LinkOffset());
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800892 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800893 stream->Write(&link_data[0], link_data.size());
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800894 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800895
896 // Write header last.
897 if (compute_offsets_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800898 header_->SetFileSize(stream->Tell());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800899 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800900 WriteHeader(stream);
Mathieu Chartier2c4b0842017-12-13 11:49:51 -0800901
902 if (dex_layout_->GetOptions().update_checksum_) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800903 header_->SetChecksum(DexFile::CalculateChecksum(stream->Begin(), header_->FileSize()));
Mathieu Chartier2c4b0842017-12-13 11:49:51 -0800904 // Rewrite the header with the calculated checksum.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800905 WriteHeader(stream);
Mathieu Chartier2c4b0842017-12-13 11:49:51 -0800906 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800907
908 // Trim the map to make it sized as large as the dex file.
909 output->GetMainSection()->Resize(header_->FileSize());
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800910 return true;
Jeff Haoa8621002016-10-04 18:13:44 +0000911}
912
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800913bool DexWriter::Output(DexLayout* dex_layout,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800914 std::unique_ptr<DexContainer>* container,
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800915 bool compute_offsets,
916 std::string* error_msg) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800917 CHECK(dex_layout != nullptr);
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700918 std::unique_ptr<DexWriter> writer;
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800919 if (dex_layout->GetOptions().compact_dex_level_ != CompactDexLevel::kCompactDexLevelNone) {
920 CHECK(compute_offsets) << "Compact dex requires computing offsets";
921 writer.reset(new CompactDexWriter(dex_layout));
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700922 } else {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800923 writer.reset(new DexWriter(dex_layout, compute_offsets));
Mathieu Chartierf95a75e2017-11-03 15:25:52 -0700924 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800925 DCHECK(container != nullptr);
926 if (*container == nullptr) {
927 *container = writer->CreateDexContainer();
928 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800929 return writer->Write(container->get(), error_msg);
Jeff Haoa8621002016-10-04 18:13:44 +0000930}
931
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800932void MapItemQueue::AddIfNotEmpty(const MapItem& item) {
933 if (item.size_ != 0) {
934 push(item);
935 }
936}
937
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800938void DexWriter::ProcessOffset(Stream* stream, dex_ir::Item* item) {
939 if (compute_offsets_) {
940 item->SetOffset(stream->Tell());
941 } else {
942 // Not computing offsets, just use the one in the item.
943 stream->Seek(item->GetOffset());
944 }
945}
946
947std::unique_ptr<DexContainer> DexWriter::CreateDexContainer() const {
948 return std::unique_ptr<DexContainer>(new DexWriter::Container);
949}
950
Jeff Haoa8621002016-10-04 18:13:44 +0000951} // namespace art