blob: 23c3a5ca937461a8b6e70eadc4e406024888272e [file] [log] [blame]
Jeff Hao3ab96b42016-09-09 18:35:01 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Implementation file of the dexlayout utility.
17 *
18 * This is a tool to read dex files into an internal representation,
19 * reorganize the representation, and emit dex files with a better
20 * file layout.
21 */
22
23#include "dex_ir.h"
David Sehrd1e44e22016-10-06 17:09:32 -070024#include "dex_instruction-inl.h"
Jeff Hao3ab96b42016-09-09 18:35:01 -070025#include "dex_ir_builder.h"
26
27namespace art {
28namespace dex_ir {
29
30static uint64_t ReadVarWidth(const uint8_t** data, uint8_t length, bool sign_extend) {
31 uint64_t value = 0;
32 for (uint32_t i = 0; i <= length; i++) {
33 value |= static_cast<uint64_t>(*(*data)++) << (i * 8);
34 }
35 if (sign_extend) {
36 int shift = (7 - length) * 8;
37 return (static_cast<int64_t>(value) << shift) >> shift;
38 }
39 return value;
40}
41
Jeff Haoa8621002016-10-04 18:13:44 +000042static uint32_t GetDebugInfoStreamSize(const uint8_t* debug_info_stream) {
43 const uint8_t* stream = debug_info_stream;
44 DecodeUnsignedLeb128(&stream); // line_start
45 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
46 for (uint32_t i = 0; i < parameters_size; ++i) {
47 DecodeUnsignedLeb128P1(&stream); // Parameter name.
48 }
49
50 for (;;) {
51 uint8_t opcode = *stream++;
52 switch (opcode) {
53 case DexFile::DBG_END_SEQUENCE:
54 return stream - debug_info_stream; // end of stream.
55 case DexFile::DBG_ADVANCE_PC:
56 DecodeUnsignedLeb128(&stream); // addr_diff
57 break;
58 case DexFile::DBG_ADVANCE_LINE:
59 DecodeSignedLeb128(&stream); // line_diff
60 break;
61 case DexFile::DBG_START_LOCAL:
62 DecodeUnsignedLeb128(&stream); // register_num
63 DecodeUnsignedLeb128P1(&stream); // name_idx
64 DecodeUnsignedLeb128P1(&stream); // type_idx
65 break;
66 case DexFile::DBG_START_LOCAL_EXTENDED:
67 DecodeUnsignedLeb128(&stream); // register_num
68 DecodeUnsignedLeb128P1(&stream); // name_idx
69 DecodeUnsignedLeb128P1(&stream); // type_idx
70 DecodeUnsignedLeb128P1(&stream); // sig_idx
71 break;
72 case DexFile::DBG_END_LOCAL:
73 case DexFile::DBG_RESTART_LOCAL:
74 DecodeUnsignedLeb128(&stream); // register_num
75 break;
76 case DexFile::DBG_SET_PROLOGUE_END:
77 case DexFile::DBG_SET_EPILOGUE_BEGIN:
78 break;
79 case DexFile::DBG_SET_FILE: {
80 DecodeUnsignedLeb128P1(&stream); // name_idx
81 break;
82 }
83 default: {
84 break;
85 }
86 }
87 }
88}
89
David Sehrd1e44e22016-10-06 17:09:32 -070090static bool GetIdFromInstruction(Collections& collections,
91 const Instruction* dec_insn,
92 std::vector<TypeId*>* type_ids,
93 std::vector<StringId*>* string_ids,
94 std::vector<MethodId*>* method_ids,
95 std::vector<FieldId*>* field_ids) {
96 // Determine index and width of the string.
97 uint32_t index = 0;
98 switch (Instruction::FormatOf(dec_insn->Opcode())) {
99 // SOME NOT SUPPORTED:
100 // case Instruction::k20bc:
101 case Instruction::k21c:
102 case Instruction::k35c:
103 // case Instruction::k35ms:
104 case Instruction::k3rc:
105 // case Instruction::k3rms:
106 // case Instruction::k35mi:
107 // case Instruction::k3rmi:
Orion Hodsonb34bb192016-10-18 17:02:58 +0100108 case Instruction::k45cc:
109 case Instruction::k4rcc:
David Sehrd1e44e22016-10-06 17:09:32 -0700110 index = dec_insn->VRegB();
111 break;
112 case Instruction::k31c:
113 index = dec_insn->VRegB();
114 break;
115 case Instruction::k22c:
116 // case Instruction::k22cs:
117 index = dec_insn->VRegC();
118 break;
119 default:
120 break;
121 } // switch
122
123 // Determine index type, and add reference to the appropriate collection.
124 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
125 case Instruction::kIndexTypeRef:
126 if (index < collections.TypeIdsSize()) {
127 type_ids->push_back(collections.GetTypeId(index));
128 return true;
129 }
130 break;
131 case Instruction::kIndexStringRef:
132 if (index < collections.StringIdsSize()) {
133 string_ids->push_back(collections.GetStringId(index));
134 return true;
135 }
136 break;
137 case Instruction::kIndexMethodRef:
Orion Hodsonb34bb192016-10-18 17:02:58 +0100138 case Instruction::kIndexMethodAndProtoRef:
David Sehrd1e44e22016-10-06 17:09:32 -0700139 if (index < collections.MethodIdsSize()) {
140 method_ids->push_back(collections.GetMethodId(index));
141 return true;
142 }
143 break;
144 case Instruction::kIndexFieldRef:
145 if (index < collections.FieldIdsSize()) {
146 field_ids->push_back(collections.GetFieldId(index));
147 return true;
148 }
149 break;
150 case Instruction::kIndexUnknown:
151 case Instruction::kIndexNone:
152 case Instruction::kIndexVtableOffset:
153 case Instruction::kIndexFieldOffset:
154 default:
155 break;
156 } // switch
157 return false;
158}
159
160/*
161 * Get all the types, strings, methods, and fields referred to from bytecode.
162 */
163static bool GetIdsFromByteCode(Collections& collections,
164 const CodeItem* code,
165 std::vector<TypeId*>* type_ids,
166 std::vector<StringId*>* string_ids,
167 std::vector<MethodId*>* method_ids,
168 std::vector<FieldId*>* field_ids) {
169 bool has_id = false;
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700170 IterationRange<DexInstructionIterator> instructions = code->Instructions();
171 SafeDexInstructionIterator it(instructions.begin(), instructions.end());
172 for (; !it.IsErrorState() && it < instructions.end(); ++it) {
173 // In case the instruction goes past the end of the code item, make sure to not process it.
174 SafeDexInstructionIterator next = it;
175 ++next;
176 if (next.IsErrorState() || next > instructions.end()) {
177 break;
178 }
David Sehrd1e44e22016-10-06 17:09:32 -0700179 has_id |= GetIdFromInstruction(collections,
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700180 it.Inst(),
David Sehrd1e44e22016-10-06 17:09:32 -0700181 type_ids,
182 string_ids,
183 method_ids,
184 field_ids);
David Sehrd1e44e22016-10-06 17:09:32 -0700185 } // for
186 return has_id;
187}
188
Jeff Hao3ab96b42016-09-09 18:35:01 -0700189EncodedValue* Collections::ReadEncodedValue(const uint8_t** data) {
190 const uint8_t encoded_value = *(*data)++;
191 const uint8_t type = encoded_value & 0x1f;
192 EncodedValue* item = new EncodedValue(type);
193 ReadEncodedValue(data, type, encoded_value >> 5, item);
194 return item;
195}
196
197EncodedValue* Collections::ReadEncodedValue(const uint8_t** data, uint8_t type, uint8_t length) {
198 EncodedValue* item = new EncodedValue(type);
199 ReadEncodedValue(data, type, length, item);
200 return item;
201}
202
203void Collections::ReadEncodedValue(
204 const uint8_t** data, uint8_t type, uint8_t length, EncodedValue* item) {
205 switch (type) {
206 case DexFile::kDexAnnotationByte:
207 item->SetByte(static_cast<int8_t>(ReadVarWidth(data, length, false)));
208 break;
209 case DexFile::kDexAnnotationShort:
210 item->SetShort(static_cast<int16_t>(ReadVarWidth(data, length, true)));
211 break;
212 case DexFile::kDexAnnotationChar:
213 item->SetChar(static_cast<uint16_t>(ReadVarWidth(data, length, false)));
214 break;
215 case DexFile::kDexAnnotationInt:
216 item->SetInt(static_cast<int32_t>(ReadVarWidth(data, length, true)));
217 break;
218 case DexFile::kDexAnnotationLong:
219 item->SetLong(static_cast<int64_t>(ReadVarWidth(data, length, true)));
220 break;
221 case DexFile::kDexAnnotationFloat: {
222 // Fill on right.
223 union {
224 float f;
225 uint32_t data;
226 } conv;
227 conv.data = static_cast<uint32_t>(ReadVarWidth(data, length, false)) << (3 - length) * 8;
228 item->SetFloat(conv.f);
229 break;
230 }
231 case DexFile::kDexAnnotationDouble: {
232 // Fill on right.
233 union {
234 double d;
235 uint64_t data;
236 } conv;
237 conv.data = ReadVarWidth(data, length, false) << (7 - length) * 8;
238 item->SetDouble(conv.d);
239 break;
240 }
Jeff Hao5daee902017-04-27 18:00:38 -0700241 case DexFile::kDexAnnotationMethodType: {
242 const uint32_t proto_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
243 item->SetProtoId(GetProtoId(proto_index));
244 break;
245 }
246 case DexFile::kDexAnnotationMethodHandle: {
247 const uint32_t method_handle_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
248 item->SetMethodHandle(GetMethodHandle(method_handle_index));
249 break;
250 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700251 case DexFile::kDexAnnotationString: {
252 const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
253 item->SetStringId(GetStringId(string_index));
254 break;
255 }
256 case DexFile::kDexAnnotationType: {
257 const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
258 item->SetTypeId(GetTypeId(string_index));
259 break;
260 }
261 case DexFile::kDexAnnotationField:
262 case DexFile::kDexAnnotationEnum: {
263 const uint32_t field_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
264 item->SetFieldId(GetFieldId(field_index));
265 break;
266 }
267 case DexFile::kDexAnnotationMethod: {
268 const uint32_t method_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
269 item->SetMethodId(GetMethodId(method_index));
270 break;
271 }
272 case DexFile::kDexAnnotationArray: {
273 EncodedValueVector* values = new EncodedValueVector();
274 const uint32_t size = DecodeUnsignedLeb128(data);
275 // Decode all elements.
276 for (uint32_t i = 0; i < size; i++) {
277 values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(data)));
278 }
279 item->SetEncodedArray(new EncodedArrayItem(values));
280 break;
281 }
282 case DexFile::kDexAnnotationAnnotation: {
283 AnnotationElementVector* elements = new AnnotationElementVector();
284 const uint32_t type_idx = DecodeUnsignedLeb128(data);
285 const uint32_t size = DecodeUnsignedLeb128(data);
286 // Decode all name=value pairs.
287 for (uint32_t i = 0; i < size; i++) {
288 const uint32_t name_index = DecodeUnsignedLeb128(data);
289 elements->push_back(std::unique_ptr<AnnotationElement>(
290 new AnnotationElement(GetStringId(name_index), ReadEncodedValue(data))));
291 }
292 item->SetEncodedAnnotation(new EncodedAnnotation(GetTypeId(type_idx), elements));
293 break;
294 }
295 case DexFile::kDexAnnotationNull:
296 break;
297 case DexFile::kDexAnnotationBoolean:
298 item->SetBoolean(length != 0);
299 break;
300 default:
301 break;
302 }
303}
304
305void Collections::CreateStringId(const DexFile& dex_file, uint32_t i) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800306 const DexFile::StringId& disk_string_id = dex_file.GetStringId(dex::StringIndex(i));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700307 StringData* string_data = new StringData(dex_file.GetStringData(disk_string_id));
308 string_datas_.AddItem(string_data, disk_string_id.string_data_off_);
309
310 StringId* string_id = new StringId(string_data);
311 string_ids_.AddIndexedItem(string_id, StringIdsOffset() + i * StringId::ItemSize(), i);
312}
313
314void Collections::CreateTypeId(const DexFile& dex_file, uint32_t i) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800315 const DexFile::TypeId& disk_type_id = dex_file.GetTypeId(dex::TypeIndex(i));
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800316 TypeId* type_id = new TypeId(GetStringId(disk_type_id.descriptor_idx_.index_));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700317 type_ids_.AddIndexedItem(type_id, TypeIdsOffset() + i * TypeId::ItemSize(), i);
318}
319
320void Collections::CreateProtoId(const DexFile& dex_file, uint32_t i) {
321 const DexFile::ProtoId& disk_proto_id = dex_file.GetProtoId(i);
322 const DexFile::TypeList* type_list = dex_file.GetProtoParameters(disk_proto_id);
Jeff Haoa8621002016-10-04 18:13:44 +0000323 TypeList* parameter_type_list = CreateTypeList(type_list, disk_proto_id.parameters_off_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700324
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800325 ProtoId* proto_id = new ProtoId(GetStringId(disk_proto_id.shorty_idx_.index_),
Andreas Gampea5b09a62016-11-17 15:21:22 -0800326 GetTypeId(disk_proto_id.return_type_idx_.index_),
Jeff Hao3ab96b42016-09-09 18:35:01 -0700327 parameter_type_list);
328 proto_ids_.AddIndexedItem(proto_id, ProtoIdsOffset() + i * ProtoId::ItemSize(), i);
329}
330
331void Collections::CreateFieldId(const DexFile& dex_file, uint32_t i) {
332 const DexFile::FieldId& disk_field_id = dex_file.GetFieldId(i);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800333 FieldId* field_id = new FieldId(GetTypeId(disk_field_id.class_idx_.index_),
334 GetTypeId(disk_field_id.type_idx_.index_),
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800335 GetStringId(disk_field_id.name_idx_.index_));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700336 field_ids_.AddIndexedItem(field_id, FieldIdsOffset() + i * FieldId::ItemSize(), i);
337}
338
339void Collections::CreateMethodId(const DexFile& dex_file, uint32_t i) {
340 const DexFile::MethodId& disk_method_id = dex_file.GetMethodId(i);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800341 MethodId* method_id = new MethodId(GetTypeId(disk_method_id.class_idx_.index_),
Jeff Hao3ab96b42016-09-09 18:35:01 -0700342 GetProtoId(disk_method_id.proto_idx_),
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800343 GetStringId(disk_method_id.name_idx_.index_));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700344 method_ids_.AddIndexedItem(method_id, MethodIdsOffset() + i * MethodId::ItemSize(), i);
345}
346
347void Collections::CreateClassDef(const DexFile& dex_file, uint32_t i) {
348 const DexFile::ClassDef& disk_class_def = dex_file.GetClassDef(i);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800349 const TypeId* class_type = GetTypeId(disk_class_def.class_idx_.index_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700350 uint32_t access_flags = disk_class_def.access_flags_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800351 const TypeId* superclass = GetTypeIdOrNullPtr(disk_class_def.superclass_idx_.index_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700352
353 const DexFile::TypeList* type_list = dex_file.GetInterfacesList(disk_class_def);
Jeff Haoa8621002016-10-04 18:13:44 +0000354 TypeList* interfaces_type_list = CreateTypeList(type_list, disk_class_def.interfaces_off_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700355
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800356 const StringId* source_file = GetStringIdOrNullPtr(disk_class_def.source_file_idx_.index_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700357 // Annotations.
358 AnnotationsDirectoryItem* annotations = nullptr;
359 const DexFile::AnnotationsDirectoryItem* disk_annotations_directory_item =
360 dex_file.GetAnnotationsDirectory(disk_class_def);
361 if (disk_annotations_directory_item != nullptr) {
362 annotations = CreateAnnotationsDirectoryItem(
363 dex_file, disk_annotations_directory_item, disk_class_def.annotations_off_);
364 }
365 // Static field initializers.
366 const uint8_t* static_data = dex_file.GetEncodedStaticFieldValuesArray(disk_class_def);
367 EncodedArrayItem* static_values =
368 CreateEncodedArrayItem(static_data, disk_class_def.static_values_off_);
369 ClassData* class_data = CreateClassData(
370 dex_file, dex_file.GetClassData(disk_class_def), disk_class_def.class_data_off_);
371 ClassDef* class_def = new ClassDef(class_type, access_flags, superclass, interfaces_type_list,
372 source_file, annotations, static_values, class_data);
373 class_defs_.AddIndexedItem(class_def, ClassDefsOffset() + i * ClassDef::ItemSize(), i);
374}
375
Jeff Haoa8621002016-10-04 18:13:44 +0000376TypeList* Collections::CreateTypeList(const DexFile::TypeList* dex_type_list, uint32_t offset) {
377 if (dex_type_list == nullptr) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700378 return nullptr;
379 }
Jeff Haoea7c6292016-11-14 18:10:16 -0800380 auto found_type_list = TypeLists().find(offset);
381 if (found_type_list != TypeLists().end()) {
382 return found_type_list->second.get();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700383 }
384 TypeIdVector* type_vector = new TypeIdVector();
Jeff Haoa8621002016-10-04 18:13:44 +0000385 uint32_t size = dex_type_list->Size();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700386 for (uint32_t index = 0; index < size; ++index) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800387 type_vector->push_back(GetTypeId(dex_type_list->GetTypeItem(index).type_idx_.index_));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700388 }
389 TypeList* new_type_list = new TypeList(type_vector);
390 type_lists_.AddItem(new_type_list, offset);
391 return new_type_list;
392}
393
394EncodedArrayItem* Collections::CreateEncodedArrayItem(const uint8_t* static_data, uint32_t offset) {
395 if (static_data == nullptr) {
396 return nullptr;
397 }
Jeff Haoea7c6292016-11-14 18:10:16 -0800398 auto found_encoded_array_item = EncodedArrayItems().find(offset);
399 if (found_encoded_array_item != EncodedArrayItems().end()) {
400 return found_encoded_array_item->second.get();
Jeff Haoa8621002016-10-04 18:13:44 +0000401 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700402 uint32_t size = DecodeUnsignedLeb128(&static_data);
403 EncodedValueVector* values = new EncodedValueVector();
404 for (uint32_t i = 0; i < size; ++i) {
405 values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(&static_data)));
406 }
407 // TODO: Calculate the size of the encoded array.
408 EncodedArrayItem* encoded_array_item = new EncodedArrayItem(values);
409 encoded_array_items_.AddItem(encoded_array_item, offset);
410 return encoded_array_item;
411}
412
Mathieu Chartier24066ec2017-10-21 16:01:08 -0700413void Collections::AddAnnotationsFromMapListSection(const DexFile& dex_file,
414 uint32_t start_offset,
415 uint32_t count) {
416 uint32_t current_offset = start_offset;
417 for (size_t i = 0; i < count; ++i) {
418 // Annotation that we didn't process already, add it to the set.
419 const DexFile::AnnotationItem* annotation = dex_file.GetAnnotationItemAtOffset(current_offset);
420 AnnotationItem* annotation_item = CreateAnnotationItem(dex_file, annotation);
421 DCHECK(annotation_item != nullptr);
422 current_offset += annotation_item->GetSize();
423 }
424}
425
426AnnotationItem* Collections::CreateAnnotationItem(const DexFile& dex_file,
427 const DexFile::AnnotationItem* annotation) {
428 const uint8_t* const start_data = reinterpret_cast<const uint8_t*>(annotation);
429 const uint32_t offset = start_data - dex_file.Begin();
Jeff Haoea7c6292016-11-14 18:10:16 -0800430 auto found_annotation_item = AnnotationItems().find(offset);
431 if (found_annotation_item != AnnotationItems().end()) {
432 return found_annotation_item->second.get();
Jeff Haoa8621002016-10-04 18:13:44 +0000433 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700434 uint8_t visibility = annotation->visibility_;
435 const uint8_t* annotation_data = annotation->annotation_;
Andreas Gampe2ae2dda2017-04-26 22:01:01 -0700436 std::unique_ptr<EncodedValue> encoded_value(
437 ReadEncodedValue(&annotation_data, DexFile::kDexAnnotationAnnotation, 0));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700438 AnnotationItem* annotation_item =
439 new AnnotationItem(visibility, encoded_value->ReleaseEncodedAnnotation());
Mathieu Chartier24066ec2017-10-21 16:01:08 -0700440 annotation_item->SetOffset(offset);
441 annotation_item->SetSize(annotation_data - start_data);
442 annotation_items_.AddItem(annotation_item, annotation_item->GetOffset());
Jeff Hao3ab96b42016-09-09 18:35:01 -0700443 return annotation_item;
444}
445
446
447AnnotationSetItem* Collections::CreateAnnotationSetItem(const DexFile& dex_file,
Jeff Haobe9b44b2017-02-16 13:34:38 -0800448 const DexFile::AnnotationSetItem* disk_annotations_item, uint32_t offset) {
449 if (disk_annotations_item == nullptr || (disk_annotations_item->size_ == 0 && offset == 0)) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700450 return nullptr;
451 }
Jeff Haoea7c6292016-11-14 18:10:16 -0800452 auto found_anno_set_item = AnnotationSetItems().find(offset);
453 if (found_anno_set_item != AnnotationSetItems().end()) {
454 return found_anno_set_item->second.get();
Jeff Haoa8621002016-10-04 18:13:44 +0000455 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700456 std::vector<AnnotationItem*>* items = new std::vector<AnnotationItem*>();
Jeff Haobe9b44b2017-02-16 13:34:38 -0800457 for (uint32_t i = 0; i < disk_annotations_item->size_; ++i) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700458 const DexFile::AnnotationItem* annotation =
Jeff Haobe9b44b2017-02-16 13:34:38 -0800459 dex_file.GetAnnotationItem(disk_annotations_item, i);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700460 if (annotation == nullptr) {
461 continue;
462 }
Mathieu Chartier24066ec2017-10-21 16:01:08 -0700463 AnnotationItem* annotation_item = CreateAnnotationItem(dex_file, annotation);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700464 items->push_back(annotation_item);
465 }
466 AnnotationSetItem* annotation_set_item = new AnnotationSetItem(items);
467 annotation_set_items_.AddItem(annotation_set_item, offset);
468 return annotation_set_item;
469}
470
471AnnotationsDirectoryItem* Collections::CreateAnnotationsDirectoryItem(const DexFile& dex_file,
472 const DexFile::AnnotationsDirectoryItem* disk_annotations_item, uint32_t offset) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800473 auto found_anno_dir_item = AnnotationsDirectoryItems().find(offset);
474 if (found_anno_dir_item != AnnotationsDirectoryItems().end()) {
475 return found_anno_dir_item->second.get();
Jeff Haoa8621002016-10-04 18:13:44 +0000476 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700477 const DexFile::AnnotationSetItem* class_set_item =
478 dex_file.GetClassAnnotationSet(disk_annotations_item);
479 AnnotationSetItem* class_annotation = nullptr;
480 if (class_set_item != nullptr) {
David Sehr7639cdc2017-04-15 10:06:21 -0700481 uint32_t item_offset = disk_annotations_item->class_annotations_off_;
482 class_annotation = CreateAnnotationSetItem(dex_file, class_set_item, item_offset);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700483 }
484 const DexFile::FieldAnnotationsItem* fields =
485 dex_file.GetFieldAnnotations(disk_annotations_item);
486 FieldAnnotationVector* field_annotations = nullptr;
487 if (fields != nullptr) {
488 field_annotations = new FieldAnnotationVector();
489 for (uint32_t i = 0; i < disk_annotations_item->fields_size_; ++i) {
490 FieldId* field_id = GetFieldId(fields[i].field_idx_);
491 const DexFile::AnnotationSetItem* field_set_item =
492 dex_file.GetFieldAnnotationSetItem(fields[i]);
493 uint32_t annotation_set_offset = fields[i].annotations_off_;
494 AnnotationSetItem* annotation_set_item =
Jeff Haobe9b44b2017-02-16 13:34:38 -0800495 CreateAnnotationSetItem(dex_file, field_set_item, annotation_set_offset);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700496 field_annotations->push_back(std::unique_ptr<FieldAnnotation>(
497 new FieldAnnotation(field_id, annotation_set_item)));
498 }
499 }
500 const DexFile::MethodAnnotationsItem* methods =
501 dex_file.GetMethodAnnotations(disk_annotations_item);
502 MethodAnnotationVector* method_annotations = nullptr;
503 if (methods != nullptr) {
504 method_annotations = new MethodAnnotationVector();
505 for (uint32_t i = 0; i < disk_annotations_item->methods_size_; ++i) {
506 MethodId* method_id = GetMethodId(methods[i].method_idx_);
507 const DexFile::AnnotationSetItem* method_set_item =
508 dex_file.GetMethodAnnotationSetItem(methods[i]);
509 uint32_t annotation_set_offset = methods[i].annotations_off_;
510 AnnotationSetItem* annotation_set_item =
Jeff Haobe9b44b2017-02-16 13:34:38 -0800511 CreateAnnotationSetItem(dex_file, method_set_item, annotation_set_offset);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700512 method_annotations->push_back(std::unique_ptr<MethodAnnotation>(
513 new MethodAnnotation(method_id, annotation_set_item)));
514 }
515 }
516 const DexFile::ParameterAnnotationsItem* parameters =
517 dex_file.GetParameterAnnotations(disk_annotations_item);
518 ParameterAnnotationVector* parameter_annotations = nullptr;
519 if (parameters != nullptr) {
520 parameter_annotations = new ParameterAnnotationVector();
521 for (uint32_t i = 0; i < disk_annotations_item->parameters_size_; ++i) {
522 MethodId* method_id = GetMethodId(parameters[i].method_idx_);
523 const DexFile::AnnotationSetRefList* list =
524 dex_file.GetParameterAnnotationSetRefList(&parameters[i]);
525 parameter_annotations->push_back(std::unique_ptr<ParameterAnnotation>(
526 GenerateParameterAnnotation(dex_file, method_id, list, parameters[i].annotations_off_)));
527 }
528 }
529 // TODO: Calculate the size of the annotations directory.
530 AnnotationsDirectoryItem* annotations_directory_item = new AnnotationsDirectoryItem(
531 class_annotation, field_annotations, method_annotations, parameter_annotations);
532 annotations_directory_items_.AddItem(annotations_directory_item, offset);
533 return annotations_directory_item;
534}
535
536ParameterAnnotation* Collections::GenerateParameterAnnotation(
537 const DexFile& dex_file, MethodId* method_id,
538 const DexFile::AnnotationSetRefList* annotation_set_ref_list, uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000539 AnnotationSetRefList* set_ref_list = nullptr;
Jeff Haoea7c6292016-11-14 18:10:16 -0800540 auto found_set_ref_list = AnnotationSetRefLists().find(offset);
541 if (found_set_ref_list != AnnotationSetRefLists().end()) {
542 set_ref_list = found_set_ref_list->second.get();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700543 }
Jeff Haoa8621002016-10-04 18:13:44 +0000544 if (set_ref_list == nullptr) {
545 std::vector<AnnotationSetItem*>* annotations = new std::vector<AnnotationSetItem*>();
546 for (uint32_t i = 0; i < annotation_set_ref_list->size_; ++i) {
547 const DexFile::AnnotationSetItem* annotation_set_item =
548 dex_file.GetSetRefItemItem(&annotation_set_ref_list->list_[i]);
549 uint32_t set_offset = annotation_set_ref_list->list_[i].annotations_off_;
Jeff Haobe9b44b2017-02-16 13:34:38 -0800550 annotations->push_back(CreateAnnotationSetItem(dex_file, annotation_set_item, set_offset));
Jeff Haoa8621002016-10-04 18:13:44 +0000551 }
552 set_ref_list = new AnnotationSetRefList(annotations);
553 annotation_set_ref_lists_.AddItem(set_ref_list, offset);
554 }
555 return new ParameterAnnotation(method_id, set_ref_list);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700556}
557
558CodeItem* Collections::CreateCodeItem(const DexFile& dex_file,
559 const DexFile::CodeItem& disk_code_item, uint32_t offset) {
560 uint16_t registers_size = disk_code_item.registers_size_;
561 uint16_t ins_size = disk_code_item.ins_size_;
562 uint16_t outs_size = disk_code_item.outs_size_;
563 uint32_t tries_size = disk_code_item.tries_size_;
564
565 // TODO: Calculate the size of the debug info.
566 const uint8_t* debug_info_stream = dex_file.GetDebugInfoStream(&disk_code_item);
567 DebugInfoItem* debug_info = nullptr;
568 if (debug_info_stream != nullptr) {
Mathieu Chartiera2973d72017-02-14 17:12:20 -0800569 debug_info = debug_info_items_.GetExistingObject(disk_code_item.debug_info_off_);
570 if (debug_info == nullptr) {
571 uint32_t debug_info_size = GetDebugInfoStreamSize(debug_info_stream);
572 uint8_t* debug_info_buffer = new uint8_t[debug_info_size];
573 memcpy(debug_info_buffer, debug_info_stream, debug_info_size);
574 debug_info = new DebugInfoItem(debug_info_size, debug_info_buffer);
575 debug_info_items_.AddItem(debug_info, disk_code_item.debug_info_off_);
576 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700577 }
578
579 uint32_t insns_size = disk_code_item.insns_size_in_code_units_;
580 uint16_t* insns = new uint16_t[insns_size];
581 memcpy(insns, disk_code_item.insns_, insns_size * sizeof(uint16_t));
582
583 TryItemVector* tries = nullptr;
Jeff Haoa8621002016-10-04 18:13:44 +0000584 CatchHandlerVector* handler_list = nullptr;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700585 if (tries_size > 0) {
586 tries = new TryItemVector();
Jeff Haoa8621002016-10-04 18:13:44 +0000587 handler_list = new CatchHandlerVector();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700588 for (uint32_t i = 0; i < tries_size; ++i) {
589 const DexFile::TryItem* disk_try_item = dex_file.GetTryItems(disk_code_item, i);
590 uint32_t start_addr = disk_try_item->start_addr_;
591 uint16_t insn_count = disk_try_item->insn_count_;
Jeff Haoa8621002016-10-04 18:13:44 +0000592 uint16_t handler_off = disk_try_item->handler_off_;
593 const CatchHandler* handlers = nullptr;
594 for (std::unique_ptr<const CatchHandler>& existing_handlers : *handler_list) {
595 if (handler_off == existing_handlers->GetListOffset()) {
596 handlers = existing_handlers.get();
Jeff Hao44652a32017-02-22 14:20:41 -0800597 break;
Jeff Haoa8621002016-10-04 18:13:44 +0000598 }
599 }
600 if (handlers == nullptr) {
601 bool catch_all = false;
602 TypeAddrPairVector* addr_pairs = new TypeAddrPairVector();
603 for (CatchHandlerIterator it(disk_code_item, *disk_try_item); it.HasNext(); it.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800604 const dex::TypeIndex type_index = it.GetHandlerTypeIndex();
605 const TypeId* type_id = GetTypeIdOrNullPtr(type_index.index_);
Jeff Haoa8621002016-10-04 18:13:44 +0000606 catch_all |= type_id == nullptr;
607 addr_pairs->push_back(std::unique_ptr<const TypeAddrPair>(
608 new TypeAddrPair(type_id, it.GetHandlerAddress())));
609 }
610 handlers = new CatchHandler(catch_all, handler_off, addr_pairs);
611 handler_list->push_back(std::unique_ptr<const CatchHandler>(handlers));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700612 }
613 TryItem* try_item = new TryItem(start_addr, insn_count, handlers);
614 tries->push_back(std::unique_ptr<const TryItem>(try_item));
615 }
Jeff Hao44652a32017-02-22 14:20:41 -0800616 // Manually walk catch handlers list and add any missing handlers unreferenced by try items.
617 const uint8_t* handlers_base = DexFile::GetCatchHandlerData(disk_code_item, 0);
618 const uint8_t* handlers_data = handlers_base;
619 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_data);
620 while (handlers_size > handler_list->size()) {
621 bool already_added = false;
622 uint16_t handler_off = handlers_data - handlers_base;
623 for (std::unique_ptr<const CatchHandler>& existing_handlers : *handler_list) {
624 if (handler_off == existing_handlers->GetListOffset()) {
625 already_added = true;
626 break;
627 }
628 }
629 int32_t size = DecodeSignedLeb128(&handlers_data);
Jeff Haoac462712017-03-02 10:59:43 -0800630 bool has_catch_all = size <= 0;
Jeff Hao44652a32017-02-22 14:20:41 -0800631 if (has_catch_all) {
632 size = -size;
633 }
Jeff Haoe17f5892017-02-23 16:14:04 -0800634 if (already_added) {
Jeff Hao44652a32017-02-22 14:20:41 -0800635 for (int32_t i = 0; i < size; i++) {
636 DecodeUnsignedLeb128(&handlers_data);
637 DecodeUnsignedLeb128(&handlers_data);
638 }
639 if (has_catch_all) {
640 DecodeUnsignedLeb128(&handlers_data);
641 }
642 continue;
643 }
644 TypeAddrPairVector* addr_pairs = new TypeAddrPairVector();
645 for (int32_t i = 0; i < size; i++) {
646 const TypeId* type_id = GetTypeIdOrNullPtr(DecodeUnsignedLeb128(&handlers_data));
647 uint32_t addr = DecodeUnsignedLeb128(&handlers_data);
648 addr_pairs->push_back(
649 std::unique_ptr<const TypeAddrPair>(new TypeAddrPair(type_id, addr)));
650 }
651 if (has_catch_all) {
652 uint32_t addr = DecodeUnsignedLeb128(&handlers_data);
653 addr_pairs->push_back(
654 std::unique_ptr<const TypeAddrPair>(new TypeAddrPair(nullptr, addr)));
655 }
656 const CatchHandler* handler = new CatchHandler(has_catch_all, handler_off, addr_pairs);
657 handler_list->push_back(std::unique_ptr<const CatchHandler>(handler));
658 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700659 }
Jeff Hao44652a32017-02-22 14:20:41 -0800660
Bharadwaj Kalandhabhatta043c9082017-06-06 17:14:12 -0700661 uint32_t size = DexFile::GetCodeItemSize(disk_code_item);
Jeff Haoa8621002016-10-04 18:13:44 +0000662 CodeItem* code_item = new CodeItem(
663 registers_size, ins_size, outs_size, debug_info, insns_size, insns, tries, handler_list);
Jeff Haoea7c6292016-11-14 18:10:16 -0800664 code_item->SetSize(size);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700665 code_items_.AddItem(code_item, offset);
David Sehrd1e44e22016-10-06 17:09:32 -0700666 // Add "fixup" references to types, strings, methods, and fields.
667 // This is temporary, as we will probably want more detailed parsing of the
668 // instructions here.
669 std::unique_ptr<std::vector<TypeId*>> type_ids(new std::vector<TypeId*>());
670 std::unique_ptr<std::vector<StringId*>> string_ids(new std::vector<StringId*>());
671 std::unique_ptr<std::vector<MethodId*>> method_ids(new std::vector<MethodId*>());
672 std::unique_ptr<std::vector<FieldId*>> field_ids(new std::vector<FieldId*>());
673 if (GetIdsFromByteCode(*this,
674 code_item,
675 type_ids.get(),
676 string_ids.get(),
677 method_ids.get(),
678 field_ids.get())) {
679 CodeFixups* fixups = new CodeFixups(type_ids.release(),
680 string_ids.release(),
681 method_ids.release(),
682 field_ids.release());
683 code_item->SetCodeFixups(fixups);
684 }
685
Jeff Hao3ab96b42016-09-09 18:35:01 -0700686 return code_item;
687}
688
689MethodItem* Collections::GenerateMethodItem(const DexFile& dex_file, ClassDataItemIterator& cdii) {
Jeff Hao9804e9e2017-06-15 14:04:51 -0700690 MethodId* method_id = GetMethodId(cdii.GetMemberIndex());
Jeff Hao3ab96b42016-09-09 18:35:01 -0700691 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
692 const DexFile::CodeItem* disk_code_item = cdii.GetMethodCodeItem();
Jeff Hao9804e9e2017-06-15 14:04:51 -0700693 CodeItem* code_item = code_items_.GetExistingObject(cdii.GetMethodCodeItemOffset());
Jeff Hao3ab96b42016-09-09 18:35:01 -0700694 DebugInfoItem* debug_info = nullptr;
695 if (disk_code_item != nullptr) {
Jeff Haod212d5b2017-04-26 12:09:06 -0700696 if (code_item == nullptr) {
697 code_item = CreateCodeItem(dex_file, *disk_code_item, cdii.GetMethodCodeItemOffset());
698 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700699 debug_info = code_item->DebugInfo();
700 }
Jeff Hao9804e9e2017-06-15 14:04:51 -0700701 return new MethodItem(access_flags, method_id, code_item);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700702}
703
704ClassData* Collections::CreateClassData(
705 const DexFile& dex_file, const uint8_t* encoded_data, uint32_t offset) {
706 // Read the fields and methods defined by the class, resolving the circular reference from those
707 // to classes by setting class at the same time.
Mathieu Chartiera2973d72017-02-14 17:12:20 -0800708 ClassData* class_data = class_datas_.GetExistingObject(offset);
709 if (class_data == nullptr && encoded_data != nullptr) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700710 ClassDataItemIterator cdii(dex_file, encoded_data);
711 // Static fields.
712 FieldItemVector* static_fields = new FieldItemVector();
Jeff Hao9804e9e2017-06-15 14:04:51 -0700713 for (; cdii.HasNextStaticField(); cdii.Next()) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700714 FieldId* field_item = GetFieldId(cdii.GetMemberIndex());
715 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
716 static_fields->push_back(std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item)));
717 }
718 // Instance fields.
719 FieldItemVector* instance_fields = new FieldItemVector();
Jeff Hao9804e9e2017-06-15 14:04:51 -0700720 for (; cdii.HasNextInstanceField(); cdii.Next()) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700721 FieldId* field_item = GetFieldId(cdii.GetMemberIndex());
722 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
723 instance_fields->push_back(
724 std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item)));
725 }
726 // Direct methods.
727 MethodItemVector* direct_methods = new MethodItemVector();
Jeff Hao9804e9e2017-06-15 14:04:51 -0700728 for (; cdii.HasNextDirectMethod(); cdii.Next()) {
729 direct_methods->push_back(std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii)));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700730 }
731 // Virtual methods.
732 MethodItemVector* virtual_methods = new MethodItemVector();
Jeff Hao9804e9e2017-06-15 14:04:51 -0700733 for (; cdii.HasNextVirtualMethod(); cdii.Next()) {
734 virtual_methods->push_back(std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii)));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700735 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700736 class_data = new ClassData(static_fields, instance_fields, direct_methods, virtual_methods);
Jeff Haoea7c6292016-11-14 18:10:16 -0800737 class_data->SetSize(cdii.EndDataPointer() - encoded_data);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700738 class_datas_.AddItem(class_data, offset);
739 }
740 return class_data;
741}
742
Jeff Hao5daee902017-04-27 18:00:38 -0700743void Collections::CreateCallSitesAndMethodHandles(const DexFile& dex_file) {
744 // Iterate through the map list and set the offset of the CallSiteIds and MethodHandleItems.
745 const DexFile::MapList* map =
746 reinterpret_cast<const DexFile::MapList*>(dex_file.Begin() + MapListOffset());
747 for (uint32_t i = 0; i < map->size_; ++i) {
748 const DexFile::MapItem* item = map->list_ + i;
749 switch (item->type_) {
750 case DexFile::kDexTypeCallSiteIdItem:
751 SetCallSiteIdsOffset(item->offset_);
752 break;
753 case DexFile::kDexTypeMethodHandleItem:
754 SetMethodHandleItemsOffset(item->offset_);
755 break;
756 default:
757 break;
758 }
759 }
760 // Populate MethodHandleItems first (CallSiteIds may depend on them).
761 for (uint32_t i = 0; i < dex_file.NumMethodHandles(); i++) {
762 CreateMethodHandleItem(dex_file, i);
763 }
764 // Populate CallSiteIds.
765 for (uint32_t i = 0; i < dex_file.NumCallSiteIds(); i++) {
766 CreateCallSiteId(dex_file, i);
767 }
768}
769
770void Collections::CreateCallSiteId(const DexFile& dex_file, uint32_t i) {
771 const DexFile::CallSiteIdItem& disk_call_site_id = dex_file.GetCallSiteId(i);
772 const uint8_t* disk_call_item_ptr = dex_file.Begin() + disk_call_site_id.data_off_;
773 EncodedArrayItem* call_site_item =
774 CreateEncodedArrayItem(disk_call_item_ptr, disk_call_site_id.data_off_);
775
776 CallSiteId* call_site_id = new CallSiteId(call_site_item);
777 call_site_ids_.AddIndexedItem(call_site_id, CallSiteIdsOffset() + i * CallSiteId::ItemSize(), i);
778}
779
780void Collections::CreateMethodHandleItem(const DexFile& dex_file, uint32_t i) {
781 const DexFile::MethodHandleItem& disk_method_handle = dex_file.GetMethodHandle(i);
782 uint16_t index = disk_method_handle.field_or_method_idx_;
783 DexFile::MethodHandleType type =
784 static_cast<DexFile::MethodHandleType>(disk_method_handle.method_handle_type_);
785 bool is_invoke = type == DexFile::MethodHandleType::kInvokeStatic ||
786 type == DexFile::MethodHandleType::kInvokeInstance ||
Orion Hodson631827d2017-04-10 14:53:47 +0100787 type == DexFile::MethodHandleType::kInvokeConstructor ||
788 type == DexFile::MethodHandleType::kInvokeDirect ||
789 type == DexFile::MethodHandleType::kInvokeInterface;
790 static_assert(DexFile::MethodHandleType::kLast == DexFile::MethodHandleType::kInvokeInterface,
Jeff Hao5daee902017-04-27 18:00:38 -0700791 "Unexpected method handle types.");
792 IndexedItem* field_or_method_id;
793 if (is_invoke) {
794 field_or_method_id = GetMethodId(index);
795 } else {
796 field_or_method_id = GetFieldId(index);
797 }
798 MethodHandleItem* method_handle = new MethodHandleItem(type, field_or_method_id);
799 method_handle_items_.AddIndexedItem(
800 method_handle, MethodHandleItemsOffset() + i * MethodHandleItem::ItemSize(), i);
801}
802
David Sehr9037a3a2017-03-30 17:50:24 -0700803static uint32_t HeaderOffset(const dex_ir::Collections& collections ATTRIBUTE_UNUSED) {
804 return 0;
805}
806
807static uint32_t HeaderSize(const dex_ir::Collections& collections ATTRIBUTE_UNUSED) {
808 // Size is in elements, so there is only one header.
809 return 1;
810}
811
812// The description of each dex file section type.
813struct FileSectionDescriptor {
814 public:
815 std::string name;
816 uint16_t type;
817 // A function that when applied to a collection object, gives the size of the section.
818 std::function<uint32_t(const dex_ir::Collections&)> size_fn;
819 // A function that when applied to a collection object, gives the offset of the section.
820 std::function<uint32_t(const dex_ir::Collections&)> offset_fn;
821};
822
David Sehr7639cdc2017-04-15 10:06:21 -0700823static const FileSectionDescriptor kFileSectionDescriptors[] = {
David Sehr9037a3a2017-03-30 17:50:24 -0700824 {
825 "Header",
826 DexFile::kDexTypeHeaderItem,
827 &HeaderSize,
828 &HeaderOffset,
829 }, {
830 "StringId",
831 DexFile::kDexTypeStringIdItem,
832 &dex_ir::Collections::StringIdsSize,
833 &dex_ir::Collections::StringIdsOffset
834 }, {
835 "TypeId",
836 DexFile::kDexTypeTypeIdItem,
837 &dex_ir::Collections::TypeIdsSize,
838 &dex_ir::Collections::TypeIdsOffset
839 }, {
840 "ProtoId",
841 DexFile::kDexTypeProtoIdItem,
842 &dex_ir::Collections::ProtoIdsSize,
843 &dex_ir::Collections::ProtoIdsOffset
844 }, {
845 "FieldId",
846 DexFile::kDexTypeFieldIdItem,
847 &dex_ir::Collections::FieldIdsSize,
848 &dex_ir::Collections::FieldIdsOffset
849 }, {
850 "MethodId",
851 DexFile::kDexTypeMethodIdItem,
852 &dex_ir::Collections::MethodIdsSize,
853 &dex_ir::Collections::MethodIdsOffset
854 }, {
855 "ClassDef",
856 DexFile::kDexTypeClassDefItem,
857 &dex_ir::Collections::ClassDefsSize,
858 &dex_ir::Collections::ClassDefsOffset
859 }, {
Jeff Hao5daee902017-04-27 18:00:38 -0700860 "CallSiteId",
861 DexFile::kDexTypeCallSiteIdItem,
862 &dex_ir::Collections::CallSiteIdsSize,
863 &dex_ir::Collections::CallSiteIdsOffset
864 }, {
865 "MethodHandle",
866 DexFile::kDexTypeMethodHandleItem,
867 &dex_ir::Collections::MethodHandleItemsSize,
868 &dex_ir::Collections::MethodHandleItemsOffset
869 }, {
David Sehr9037a3a2017-03-30 17:50:24 -0700870 "StringData",
871 DexFile::kDexTypeStringDataItem,
872 &dex_ir::Collections::StringDatasSize,
873 &dex_ir::Collections::StringDatasOffset
874 }, {
875 "TypeList",
876 DexFile::kDexTypeTypeList,
877 &dex_ir::Collections::TypeListsSize,
878 &dex_ir::Collections::TypeListsOffset
879 }, {
880 "EncArr",
881 DexFile::kDexTypeEncodedArrayItem,
882 &dex_ir::Collections::EncodedArrayItemsSize,
883 &dex_ir::Collections::EncodedArrayItemsOffset
884 }, {
885 "Annotation",
886 DexFile::kDexTypeAnnotationItem,
887 &dex_ir::Collections::AnnotationItemsSize,
888 &dex_ir::Collections::AnnotationItemsOffset
889 }, {
890 "AnnoSet",
891 DexFile::kDexTypeAnnotationSetItem,
892 &dex_ir::Collections::AnnotationSetItemsSize,
893 &dex_ir::Collections::AnnotationSetItemsOffset
894 }, {
895 "AnnoSetRL",
896 DexFile::kDexTypeAnnotationSetRefList,
897 &dex_ir::Collections::AnnotationSetRefListsSize,
898 &dex_ir::Collections::AnnotationSetRefListsOffset
899 }, {
900 "AnnoDir",
901 DexFile::kDexTypeAnnotationsDirectoryItem,
902 &dex_ir::Collections::AnnotationsDirectoryItemsSize,
903 &dex_ir::Collections::AnnotationsDirectoryItemsOffset
904 }, {
905 "DebugInfo",
906 DexFile::kDexTypeDebugInfoItem,
907 &dex_ir::Collections::DebugInfoItemsSize,
908 &dex_ir::Collections::DebugInfoItemsOffset
909 }, {
910 "CodeItem",
911 DexFile::kDexTypeCodeItem,
912 &dex_ir::Collections::CodeItemsSize,
913 &dex_ir::Collections::CodeItemsOffset
914 }, {
915 "ClassData",
916 DexFile::kDexTypeClassDataItem,
917 &dex_ir::Collections::ClassDatasSize,
918 &dex_ir::Collections::ClassDatasOffset
919 }
920};
921
922std::vector<dex_ir::DexFileSection> GetSortedDexFileSections(dex_ir::Header* header,
923 dex_ir::SortDirection direction) {
924 const dex_ir::Collections& collections = header->GetCollections();
925 std::vector<dex_ir::DexFileSection> sorted_sections;
926 // Build the table that will map from offset to color
927 for (const FileSectionDescriptor& s : kFileSectionDescriptors) {
928 sorted_sections.push_back(dex_ir::DexFileSection(s.name,
929 s.type,
930 s.size_fn(collections),
931 s.offset_fn(collections)));
932 }
933 // Sort by offset.
934 std::sort(sorted_sections.begin(),
935 sorted_sections.end(),
936 [=](dex_ir::DexFileSection& a, dex_ir::DexFileSection& b) {
937 if (direction == SortDirection::kSortDescending) {
938 return a.offset > b.offset;
939 } else {
940 return a.offset < b.offset;
941 }
942 });
943 return sorted_sections;
944}
945
Jeff Hao3ab96b42016-09-09 18:35:01 -0700946} // namespace dex_ir
947} // namespace art