blob: bc909c3b565ea3a7a1e9aa2fdf48305c3899e93f [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"
24#include "dex_ir_builder.h"
25
26namespace art {
27namespace dex_ir {
28
29static uint64_t ReadVarWidth(const uint8_t** data, uint8_t length, bool sign_extend) {
30 uint64_t value = 0;
31 for (uint32_t i = 0; i <= length; i++) {
32 value |= static_cast<uint64_t>(*(*data)++) << (i * 8);
33 }
34 if (sign_extend) {
35 int shift = (7 - length) * 8;
36 return (static_cast<int64_t>(value) << shift) >> shift;
37 }
38 return value;
39}
40
41static bool GetPositionsCb(void* context, const DexFile::PositionInfo& entry) {
42 DebugInfoItem* debug_info = reinterpret_cast<DebugInfoItem*>(context);
43 PositionInfoVector& positions = debug_info->GetPositionInfo();
44 positions.push_back(std::unique_ptr<PositionInfo>(new PositionInfo(entry.address_, entry.line_)));
45 return false;
46}
47
48static void GetLocalsCb(void* context, const DexFile::LocalInfo& entry) {
49 DebugInfoItem* debug_info = reinterpret_cast<DebugInfoItem*>(context);
50 LocalInfoVector& locals = debug_info->GetLocalInfo();
51 const char* name = entry.name_ != nullptr ? entry.name_ : "(null)";
52 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
53 locals.push_back(std::unique_ptr<LocalInfo>(
54 new LocalInfo(name, entry.descriptor_, signature, entry.start_address_,
55 entry.end_address_, entry.reg_)));
56}
57
Jeff Haoa8621002016-10-04 18:13:44 +000058static uint32_t GetDebugInfoStreamSize(const uint8_t* debug_info_stream) {
59 const uint8_t* stream = debug_info_stream;
60 DecodeUnsignedLeb128(&stream); // line_start
61 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
62 for (uint32_t i = 0; i < parameters_size; ++i) {
63 DecodeUnsignedLeb128P1(&stream); // Parameter name.
64 }
65
66 for (;;) {
67 uint8_t opcode = *stream++;
68 switch (opcode) {
69 case DexFile::DBG_END_SEQUENCE:
70 return stream - debug_info_stream; // end of stream.
71 case DexFile::DBG_ADVANCE_PC:
72 DecodeUnsignedLeb128(&stream); // addr_diff
73 break;
74 case DexFile::DBG_ADVANCE_LINE:
75 DecodeSignedLeb128(&stream); // line_diff
76 break;
77 case DexFile::DBG_START_LOCAL:
78 DecodeUnsignedLeb128(&stream); // register_num
79 DecodeUnsignedLeb128P1(&stream); // name_idx
80 DecodeUnsignedLeb128P1(&stream); // type_idx
81 break;
82 case DexFile::DBG_START_LOCAL_EXTENDED:
83 DecodeUnsignedLeb128(&stream); // register_num
84 DecodeUnsignedLeb128P1(&stream); // name_idx
85 DecodeUnsignedLeb128P1(&stream); // type_idx
86 DecodeUnsignedLeb128P1(&stream); // sig_idx
87 break;
88 case DexFile::DBG_END_LOCAL:
89 case DexFile::DBG_RESTART_LOCAL:
90 DecodeUnsignedLeb128(&stream); // register_num
91 break;
92 case DexFile::DBG_SET_PROLOGUE_END:
93 case DexFile::DBG_SET_EPILOGUE_BEGIN:
94 break;
95 case DexFile::DBG_SET_FILE: {
96 DecodeUnsignedLeb128P1(&stream); // name_idx
97 break;
98 }
99 default: {
100 break;
101 }
102 }
103 }
104}
105
Jeff Hao3ab96b42016-09-09 18:35:01 -0700106EncodedValue* Collections::ReadEncodedValue(const uint8_t** data) {
107 const uint8_t encoded_value = *(*data)++;
108 const uint8_t type = encoded_value & 0x1f;
109 EncodedValue* item = new EncodedValue(type);
110 ReadEncodedValue(data, type, encoded_value >> 5, item);
111 return item;
112}
113
114EncodedValue* Collections::ReadEncodedValue(const uint8_t** data, uint8_t type, uint8_t length) {
115 EncodedValue* item = new EncodedValue(type);
116 ReadEncodedValue(data, type, length, item);
117 return item;
118}
119
120void Collections::ReadEncodedValue(
121 const uint8_t** data, uint8_t type, uint8_t length, EncodedValue* item) {
122 switch (type) {
123 case DexFile::kDexAnnotationByte:
124 item->SetByte(static_cast<int8_t>(ReadVarWidth(data, length, false)));
125 break;
126 case DexFile::kDexAnnotationShort:
127 item->SetShort(static_cast<int16_t>(ReadVarWidth(data, length, true)));
128 break;
129 case DexFile::kDexAnnotationChar:
130 item->SetChar(static_cast<uint16_t>(ReadVarWidth(data, length, false)));
131 break;
132 case DexFile::kDexAnnotationInt:
133 item->SetInt(static_cast<int32_t>(ReadVarWidth(data, length, true)));
134 break;
135 case DexFile::kDexAnnotationLong:
136 item->SetLong(static_cast<int64_t>(ReadVarWidth(data, length, true)));
137 break;
138 case DexFile::kDexAnnotationFloat: {
139 // Fill on right.
140 union {
141 float f;
142 uint32_t data;
143 } conv;
144 conv.data = static_cast<uint32_t>(ReadVarWidth(data, length, false)) << (3 - length) * 8;
145 item->SetFloat(conv.f);
146 break;
147 }
148 case DexFile::kDexAnnotationDouble: {
149 // Fill on right.
150 union {
151 double d;
152 uint64_t data;
153 } conv;
154 conv.data = ReadVarWidth(data, length, false) << (7 - length) * 8;
155 item->SetDouble(conv.d);
156 break;
157 }
158 case DexFile::kDexAnnotationString: {
159 const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
160 item->SetStringId(GetStringId(string_index));
161 break;
162 }
163 case DexFile::kDexAnnotationType: {
164 const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
165 item->SetTypeId(GetTypeId(string_index));
166 break;
167 }
168 case DexFile::kDexAnnotationField:
169 case DexFile::kDexAnnotationEnum: {
170 const uint32_t field_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
171 item->SetFieldId(GetFieldId(field_index));
172 break;
173 }
174 case DexFile::kDexAnnotationMethod: {
175 const uint32_t method_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
176 item->SetMethodId(GetMethodId(method_index));
177 break;
178 }
179 case DexFile::kDexAnnotationArray: {
180 EncodedValueVector* values = new EncodedValueVector();
181 const uint32_t size = DecodeUnsignedLeb128(data);
182 // Decode all elements.
183 for (uint32_t i = 0; i < size; i++) {
184 values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(data)));
185 }
186 item->SetEncodedArray(new EncodedArrayItem(values));
187 break;
188 }
189 case DexFile::kDexAnnotationAnnotation: {
190 AnnotationElementVector* elements = new AnnotationElementVector();
191 const uint32_t type_idx = DecodeUnsignedLeb128(data);
192 const uint32_t size = DecodeUnsignedLeb128(data);
193 // Decode all name=value pairs.
194 for (uint32_t i = 0; i < size; i++) {
195 const uint32_t name_index = DecodeUnsignedLeb128(data);
196 elements->push_back(std::unique_ptr<AnnotationElement>(
197 new AnnotationElement(GetStringId(name_index), ReadEncodedValue(data))));
198 }
199 item->SetEncodedAnnotation(new EncodedAnnotation(GetTypeId(type_idx), elements));
200 break;
201 }
202 case DexFile::kDexAnnotationNull:
203 break;
204 case DexFile::kDexAnnotationBoolean:
205 item->SetBoolean(length != 0);
206 break;
207 default:
208 break;
209 }
210}
211
212void Collections::CreateStringId(const DexFile& dex_file, uint32_t i) {
213 const DexFile::StringId& disk_string_id = dex_file.GetStringId(i);
214 StringData* string_data = new StringData(dex_file.GetStringData(disk_string_id));
215 string_datas_.AddItem(string_data, disk_string_id.string_data_off_);
216
217 StringId* string_id = new StringId(string_data);
218 string_ids_.AddIndexedItem(string_id, StringIdsOffset() + i * StringId::ItemSize(), i);
219}
220
221void Collections::CreateTypeId(const DexFile& dex_file, uint32_t i) {
222 const DexFile::TypeId& disk_type_id = dex_file.GetTypeId(i);
223 TypeId* type_id = new TypeId(GetStringId(disk_type_id.descriptor_idx_));
224 type_ids_.AddIndexedItem(type_id, TypeIdsOffset() + i * TypeId::ItemSize(), i);
225}
226
227void Collections::CreateProtoId(const DexFile& dex_file, uint32_t i) {
228 const DexFile::ProtoId& disk_proto_id = dex_file.GetProtoId(i);
229 const DexFile::TypeList* type_list = dex_file.GetProtoParameters(disk_proto_id);
Jeff Haoa8621002016-10-04 18:13:44 +0000230 TypeList* parameter_type_list = CreateTypeList(type_list, disk_proto_id.parameters_off_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700231
232 ProtoId* proto_id = new ProtoId(GetStringId(disk_proto_id.shorty_idx_),
233 GetTypeId(disk_proto_id.return_type_idx_),
234 parameter_type_list);
235 proto_ids_.AddIndexedItem(proto_id, ProtoIdsOffset() + i * ProtoId::ItemSize(), i);
236}
237
238void Collections::CreateFieldId(const DexFile& dex_file, uint32_t i) {
239 const DexFile::FieldId& disk_field_id = dex_file.GetFieldId(i);
240 FieldId* field_id = new FieldId(GetTypeId(disk_field_id.class_idx_),
241 GetTypeId(disk_field_id.type_idx_),
242 GetStringId(disk_field_id.name_idx_));
243 field_ids_.AddIndexedItem(field_id, FieldIdsOffset() + i * FieldId::ItemSize(), i);
244}
245
246void Collections::CreateMethodId(const DexFile& dex_file, uint32_t i) {
247 const DexFile::MethodId& disk_method_id = dex_file.GetMethodId(i);
248 MethodId* method_id = new MethodId(GetTypeId(disk_method_id.class_idx_),
249 GetProtoId(disk_method_id.proto_idx_),
250 GetStringId(disk_method_id.name_idx_));
251 method_ids_.AddIndexedItem(method_id, MethodIdsOffset() + i * MethodId::ItemSize(), i);
252}
253
254void Collections::CreateClassDef(const DexFile& dex_file, uint32_t i) {
255 const DexFile::ClassDef& disk_class_def = dex_file.GetClassDef(i);
256 const TypeId* class_type = GetTypeId(disk_class_def.class_idx_);
257 uint32_t access_flags = disk_class_def.access_flags_;
258 const TypeId* superclass = GetTypeIdOrNullPtr(disk_class_def.superclass_idx_);
259
260 const DexFile::TypeList* type_list = dex_file.GetInterfacesList(disk_class_def);
Jeff Haoa8621002016-10-04 18:13:44 +0000261 TypeList* interfaces_type_list = CreateTypeList(type_list, disk_class_def.interfaces_off_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700262
263 const StringId* source_file = GetStringIdOrNullPtr(disk_class_def.source_file_idx_);
264 // Annotations.
265 AnnotationsDirectoryItem* annotations = nullptr;
266 const DexFile::AnnotationsDirectoryItem* disk_annotations_directory_item =
267 dex_file.GetAnnotationsDirectory(disk_class_def);
268 if (disk_annotations_directory_item != nullptr) {
269 annotations = CreateAnnotationsDirectoryItem(
270 dex_file, disk_annotations_directory_item, disk_class_def.annotations_off_);
271 }
272 // Static field initializers.
273 const uint8_t* static_data = dex_file.GetEncodedStaticFieldValuesArray(disk_class_def);
274 EncodedArrayItem* static_values =
275 CreateEncodedArrayItem(static_data, disk_class_def.static_values_off_);
276 ClassData* class_data = CreateClassData(
277 dex_file, dex_file.GetClassData(disk_class_def), disk_class_def.class_data_off_);
278 ClassDef* class_def = new ClassDef(class_type, access_flags, superclass, interfaces_type_list,
279 source_file, annotations, static_values, class_data);
280 class_defs_.AddIndexedItem(class_def, ClassDefsOffset() + i * ClassDef::ItemSize(), i);
281}
282
Jeff Haoa8621002016-10-04 18:13:44 +0000283TypeList* Collections::CreateTypeList(const DexFile::TypeList* dex_type_list, uint32_t offset) {
284 if (dex_type_list == nullptr) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700285 return nullptr;
286 }
287 // TODO: Create more efficient lookup for existing type lists.
288 for (std::unique_ptr<TypeList>& type_list : TypeLists()) {
289 if (type_list->GetOffset() == offset) {
290 return type_list.get();
291 }
292 }
293 TypeIdVector* type_vector = new TypeIdVector();
Jeff Haoa8621002016-10-04 18:13:44 +0000294 uint32_t size = dex_type_list->Size();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700295 for (uint32_t index = 0; index < size; ++index) {
296 type_vector->push_back(GetTypeId(dex_type_list->GetTypeItem(index).type_idx_));
297 }
298 TypeList* new_type_list = new TypeList(type_vector);
299 type_lists_.AddItem(new_type_list, offset);
300 return new_type_list;
301}
302
303EncodedArrayItem* Collections::CreateEncodedArrayItem(const uint8_t* static_data, uint32_t offset) {
304 if (static_data == nullptr) {
305 return nullptr;
306 }
Jeff Haoa8621002016-10-04 18:13:44 +0000307 for (std::unique_ptr<EncodedArrayItem>& existing_array_item : EncodedArrayItems()) {
308 if (existing_array_item->GetOffset() == offset) {
309 return existing_array_item.get();
310 }
311 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700312 uint32_t size = DecodeUnsignedLeb128(&static_data);
313 EncodedValueVector* values = new EncodedValueVector();
314 for (uint32_t i = 0; i < size; ++i) {
315 values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(&static_data)));
316 }
317 // TODO: Calculate the size of the encoded array.
318 EncodedArrayItem* encoded_array_item = new EncodedArrayItem(values);
319 encoded_array_items_.AddItem(encoded_array_item, offset);
320 return encoded_array_item;
321}
322
323AnnotationItem* Collections::CreateAnnotationItem(const DexFile::AnnotationItem* annotation,
324 uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000325 for (std::unique_ptr<AnnotationItem>& existing_annotation_item : AnnotationItems()) {
326 if (existing_annotation_item->GetOffset() == offset) {
327 return existing_annotation_item.get();
328 }
329 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700330 uint8_t visibility = annotation->visibility_;
331 const uint8_t* annotation_data = annotation->annotation_;
332 EncodedValue* encoded_value =
333 ReadEncodedValue(&annotation_data, DexFile::kDexAnnotationAnnotation, 0);
334 // TODO: Calculate the size of the annotation.
335 AnnotationItem* annotation_item =
336 new AnnotationItem(visibility, encoded_value->ReleaseEncodedAnnotation());
337 annotation_items_.AddItem(annotation_item, offset);
338 return annotation_item;
339}
340
341
342AnnotationSetItem* Collections::CreateAnnotationSetItem(const DexFile& dex_file,
343 const DexFile::AnnotationSetItem& disk_annotations_item, uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000344 if (disk_annotations_item.size_ == 0 && offset == 0) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700345 return nullptr;
346 }
Jeff Haoa8621002016-10-04 18:13:44 +0000347 for (std::unique_ptr<AnnotationSetItem>& existing_anno_set_item : AnnotationSetItems()) {
348 if (existing_anno_set_item->GetOffset() == offset) {
349 return existing_anno_set_item.get();
350 }
351 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700352 std::vector<AnnotationItem*>* items = new std::vector<AnnotationItem*>();
353 for (uint32_t i = 0; i < disk_annotations_item.size_; ++i) {
354 const DexFile::AnnotationItem* annotation =
355 dex_file.GetAnnotationItem(&disk_annotations_item, i);
356 if (annotation == nullptr) {
357 continue;
358 }
359 AnnotationItem* annotation_item =
360 CreateAnnotationItem(annotation, disk_annotations_item.entries_[i]);
361 items->push_back(annotation_item);
362 }
363 AnnotationSetItem* annotation_set_item = new AnnotationSetItem(items);
364 annotation_set_items_.AddItem(annotation_set_item, offset);
365 return annotation_set_item;
366}
367
368AnnotationsDirectoryItem* Collections::CreateAnnotationsDirectoryItem(const DexFile& dex_file,
369 const DexFile::AnnotationsDirectoryItem* disk_annotations_item, uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000370 for (std::unique_ptr<AnnotationsDirectoryItem>& anno_dir_item : AnnotationsDirectoryItems()) {
371 if (anno_dir_item->GetOffset() == offset) {
372 return anno_dir_item.get();
373 }
374 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700375 const DexFile::AnnotationSetItem* class_set_item =
376 dex_file.GetClassAnnotationSet(disk_annotations_item);
377 AnnotationSetItem* class_annotation = nullptr;
378 if (class_set_item != nullptr) {
379 uint32_t offset = disk_annotations_item->class_annotations_off_;
380 class_annotation = CreateAnnotationSetItem(dex_file, *class_set_item, offset);
381 }
382 const DexFile::FieldAnnotationsItem* fields =
383 dex_file.GetFieldAnnotations(disk_annotations_item);
384 FieldAnnotationVector* field_annotations = nullptr;
385 if (fields != nullptr) {
386 field_annotations = new FieldAnnotationVector();
387 for (uint32_t i = 0; i < disk_annotations_item->fields_size_; ++i) {
388 FieldId* field_id = GetFieldId(fields[i].field_idx_);
389 const DexFile::AnnotationSetItem* field_set_item =
390 dex_file.GetFieldAnnotationSetItem(fields[i]);
391 uint32_t annotation_set_offset = fields[i].annotations_off_;
392 AnnotationSetItem* annotation_set_item =
393 CreateAnnotationSetItem(dex_file, *field_set_item, annotation_set_offset);
394 field_annotations->push_back(std::unique_ptr<FieldAnnotation>(
395 new FieldAnnotation(field_id, annotation_set_item)));
396 }
397 }
398 const DexFile::MethodAnnotationsItem* methods =
399 dex_file.GetMethodAnnotations(disk_annotations_item);
400 MethodAnnotationVector* method_annotations = nullptr;
401 if (methods != nullptr) {
402 method_annotations = new MethodAnnotationVector();
403 for (uint32_t i = 0; i < disk_annotations_item->methods_size_; ++i) {
404 MethodId* method_id = GetMethodId(methods[i].method_idx_);
405 const DexFile::AnnotationSetItem* method_set_item =
406 dex_file.GetMethodAnnotationSetItem(methods[i]);
407 uint32_t annotation_set_offset = methods[i].annotations_off_;
408 AnnotationSetItem* annotation_set_item =
409 CreateAnnotationSetItem(dex_file, *method_set_item, annotation_set_offset);
410 method_annotations->push_back(std::unique_ptr<MethodAnnotation>(
411 new MethodAnnotation(method_id, annotation_set_item)));
412 }
413 }
414 const DexFile::ParameterAnnotationsItem* parameters =
415 dex_file.GetParameterAnnotations(disk_annotations_item);
416 ParameterAnnotationVector* parameter_annotations = nullptr;
417 if (parameters != nullptr) {
418 parameter_annotations = new ParameterAnnotationVector();
419 for (uint32_t i = 0; i < disk_annotations_item->parameters_size_; ++i) {
420 MethodId* method_id = GetMethodId(parameters[i].method_idx_);
421 const DexFile::AnnotationSetRefList* list =
422 dex_file.GetParameterAnnotationSetRefList(&parameters[i]);
423 parameter_annotations->push_back(std::unique_ptr<ParameterAnnotation>(
424 GenerateParameterAnnotation(dex_file, method_id, list, parameters[i].annotations_off_)));
425 }
426 }
427 // TODO: Calculate the size of the annotations directory.
428 AnnotationsDirectoryItem* annotations_directory_item = new AnnotationsDirectoryItem(
429 class_annotation, field_annotations, method_annotations, parameter_annotations);
430 annotations_directory_items_.AddItem(annotations_directory_item, offset);
431 return annotations_directory_item;
432}
433
434ParameterAnnotation* Collections::GenerateParameterAnnotation(
435 const DexFile& dex_file, MethodId* method_id,
436 const DexFile::AnnotationSetRefList* annotation_set_ref_list, uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000437 AnnotationSetRefList* set_ref_list = nullptr;
438 for (std::unique_ptr<AnnotationSetRefList>& existing_set_ref_list : AnnotationSetRefLists()) {
439 if (existing_set_ref_list->GetOffset() == offset) {
440 set_ref_list = existing_set_ref_list.get();
441 break;
442 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700443 }
Jeff Haoa8621002016-10-04 18:13:44 +0000444 if (set_ref_list == nullptr) {
445 std::vector<AnnotationSetItem*>* annotations = new std::vector<AnnotationSetItem*>();
446 for (uint32_t i = 0; i < annotation_set_ref_list->size_; ++i) {
447 const DexFile::AnnotationSetItem* annotation_set_item =
448 dex_file.GetSetRefItemItem(&annotation_set_ref_list->list_[i]);
449 uint32_t set_offset = annotation_set_ref_list->list_[i].annotations_off_;
450 annotations->push_back(CreateAnnotationSetItem(dex_file, *annotation_set_item, set_offset));
451 }
452 set_ref_list = new AnnotationSetRefList(annotations);
453 annotation_set_ref_lists_.AddItem(set_ref_list, offset);
454 }
455 return new ParameterAnnotation(method_id, set_ref_list);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700456}
457
458CodeItem* Collections::CreateCodeItem(const DexFile& dex_file,
459 const DexFile::CodeItem& disk_code_item, uint32_t offset) {
460 uint16_t registers_size = disk_code_item.registers_size_;
461 uint16_t ins_size = disk_code_item.ins_size_;
462 uint16_t outs_size = disk_code_item.outs_size_;
463 uint32_t tries_size = disk_code_item.tries_size_;
464
465 // TODO: Calculate the size of the debug info.
466 const uint8_t* debug_info_stream = dex_file.GetDebugInfoStream(&disk_code_item);
467 DebugInfoItem* debug_info = nullptr;
468 if (debug_info_stream != nullptr) {
Jeff Haoa8621002016-10-04 18:13:44 +0000469 uint32_t debug_info_size = GetDebugInfoStreamSize(debug_info_stream);
470 uint8_t* debug_info_buffer = new uint8_t[debug_info_size];
471 memcpy(debug_info_buffer, debug_info_stream, debug_info_size);
472 debug_info = new DebugInfoItem(debug_info_size, debug_info_buffer);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700473 debug_info_items_.AddItem(debug_info, disk_code_item.debug_info_off_);
474 }
475
476 uint32_t insns_size = disk_code_item.insns_size_in_code_units_;
477 uint16_t* insns = new uint16_t[insns_size];
478 memcpy(insns, disk_code_item.insns_, insns_size * sizeof(uint16_t));
479
480 TryItemVector* tries = nullptr;
Jeff Haoa8621002016-10-04 18:13:44 +0000481 CatchHandlerVector* handler_list = nullptr;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700482 if (tries_size > 0) {
483 tries = new TryItemVector();
Jeff Haoa8621002016-10-04 18:13:44 +0000484 handler_list = new CatchHandlerVector();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700485 for (uint32_t i = 0; i < tries_size; ++i) {
486 const DexFile::TryItem* disk_try_item = dex_file.GetTryItems(disk_code_item, i);
487 uint32_t start_addr = disk_try_item->start_addr_;
488 uint16_t insn_count = disk_try_item->insn_count_;
Jeff Haoa8621002016-10-04 18:13:44 +0000489 uint16_t handler_off = disk_try_item->handler_off_;
490 const CatchHandler* handlers = nullptr;
491 for (std::unique_ptr<const CatchHandler>& existing_handlers : *handler_list) {
492 if (handler_off == existing_handlers->GetListOffset()) {
493 handlers = existing_handlers.get();
494 }
495 }
496 if (handlers == nullptr) {
497 bool catch_all = false;
498 TypeAddrPairVector* addr_pairs = new TypeAddrPairVector();
499 for (CatchHandlerIterator it(disk_code_item, *disk_try_item); it.HasNext(); it.Next()) {
500 const uint16_t type_index = it.GetHandlerTypeIndex();
501 const TypeId* type_id = GetTypeIdOrNullPtr(type_index);
502 catch_all |= type_id == nullptr;
503 addr_pairs->push_back(std::unique_ptr<const TypeAddrPair>(
504 new TypeAddrPair(type_id, it.GetHandlerAddress())));
505 }
506 handlers = new CatchHandler(catch_all, handler_off, addr_pairs);
507 handler_list->push_back(std::unique_ptr<const CatchHandler>(handlers));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700508 }
509 TryItem* try_item = new TryItem(start_addr, insn_count, handlers);
510 tries->push_back(std::unique_ptr<const TryItem>(try_item));
511 }
512 }
513 // TODO: Calculate the size of the code item.
Jeff Haoa8621002016-10-04 18:13:44 +0000514 CodeItem* code_item = new CodeItem(
515 registers_size, ins_size, outs_size, debug_info, insns_size, insns, tries, handler_list);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700516 code_items_.AddItem(code_item, offset);
517 return code_item;
518}
519
520MethodItem* Collections::GenerateMethodItem(const DexFile& dex_file, ClassDataItemIterator& cdii) {
521 MethodId* method_item = GetMethodId(cdii.GetMemberIndex());
522 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
523 const DexFile::CodeItem* disk_code_item = cdii.GetMethodCodeItem();
524 CodeItem* code_item = nullptr;
525 DebugInfoItem* debug_info = nullptr;
526 if (disk_code_item != nullptr) {
527 code_item = CreateCodeItem(dex_file, *disk_code_item, cdii.GetMethodCodeItemOffset());
528 debug_info = code_item->DebugInfo();
529 }
530 if (debug_info != nullptr) {
531 bool is_static = (access_flags & kAccStatic) != 0;
532 dex_file.DecodeDebugLocalInfo(
533 disk_code_item, is_static, cdii.GetMemberIndex(), GetLocalsCb, debug_info);
534 dex_file.DecodeDebugPositionInfo(disk_code_item, GetPositionsCb, debug_info);
535 }
536 return new MethodItem(access_flags, method_item, code_item);
537}
538
539ClassData* Collections::CreateClassData(
540 const DexFile& dex_file, const uint8_t* encoded_data, uint32_t offset) {
541 // Read the fields and methods defined by the class, resolving the circular reference from those
542 // to classes by setting class at the same time.
543 ClassData* class_data = nullptr;
544 if (encoded_data != nullptr) {
545 ClassDataItemIterator cdii(dex_file, encoded_data);
546 // Static fields.
547 FieldItemVector* static_fields = new FieldItemVector();
548 for (uint32_t i = 0; cdii.HasNextStaticField(); i++, cdii.Next()) {
549 FieldId* field_item = GetFieldId(cdii.GetMemberIndex());
550 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
551 static_fields->push_back(std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item)));
552 }
553 // Instance fields.
554 FieldItemVector* instance_fields = new FieldItemVector();
555 for (uint32_t i = 0; cdii.HasNextInstanceField(); i++, cdii.Next()) {
556 FieldId* field_item = GetFieldId(cdii.GetMemberIndex());
557 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
558 instance_fields->push_back(
559 std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item)));
560 }
561 // Direct methods.
562 MethodItemVector* direct_methods = new MethodItemVector();
563 for (uint32_t i = 0; cdii.HasNextDirectMethod(); i++, cdii.Next()) {
564 direct_methods->push_back(
565 std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii)));
566 }
567 // Virtual methods.
568 MethodItemVector* virtual_methods = new MethodItemVector();
569 for (uint32_t i = 0; cdii.HasNextVirtualMethod(); i++, cdii.Next()) {
570 virtual_methods->push_back(
571 std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii)));
572 }
573 // TODO: Calculate the size of the class data.
574 class_data = new ClassData(static_fields, instance_fields, direct_methods, virtual_methods);
575 class_datas_.AddItem(class_data, offset);
576 }
577 return class_data;
578}
579
Jeff Hao3ab96b42016-09-09 18:35:01 -0700580} // namespace dex_ir
581} // namespace art