blob: 0f07f23a88db83197dff69e06a6e18d233a079a4 [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
42static bool GetPositionsCb(void* context, const DexFile::PositionInfo& entry) {
43 DebugInfoItem* debug_info = reinterpret_cast<DebugInfoItem*>(context);
44 PositionInfoVector& positions = debug_info->GetPositionInfo();
45 positions.push_back(std::unique_ptr<PositionInfo>(new PositionInfo(entry.address_, entry.line_)));
46 return false;
47}
48
49static void GetLocalsCb(void* context, const DexFile::LocalInfo& entry) {
50 DebugInfoItem* debug_info = reinterpret_cast<DebugInfoItem*>(context);
51 LocalInfoVector& locals = debug_info->GetLocalInfo();
52 const char* name = entry.name_ != nullptr ? entry.name_ : "(null)";
53 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
54 locals.push_back(std::unique_ptr<LocalInfo>(
55 new LocalInfo(name, entry.descriptor_, signature, entry.start_address_,
56 entry.end_address_, entry.reg_)));
57}
58
Jeff Haoa8621002016-10-04 18:13:44 +000059static uint32_t GetDebugInfoStreamSize(const uint8_t* debug_info_stream) {
60 const uint8_t* stream = debug_info_stream;
61 DecodeUnsignedLeb128(&stream); // line_start
62 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
63 for (uint32_t i = 0; i < parameters_size; ++i) {
64 DecodeUnsignedLeb128P1(&stream); // Parameter name.
65 }
66
67 for (;;) {
68 uint8_t opcode = *stream++;
69 switch (opcode) {
70 case DexFile::DBG_END_SEQUENCE:
71 return stream - debug_info_stream; // end of stream.
72 case DexFile::DBG_ADVANCE_PC:
73 DecodeUnsignedLeb128(&stream); // addr_diff
74 break;
75 case DexFile::DBG_ADVANCE_LINE:
76 DecodeSignedLeb128(&stream); // line_diff
77 break;
78 case DexFile::DBG_START_LOCAL:
79 DecodeUnsignedLeb128(&stream); // register_num
80 DecodeUnsignedLeb128P1(&stream); // name_idx
81 DecodeUnsignedLeb128P1(&stream); // type_idx
82 break;
83 case DexFile::DBG_START_LOCAL_EXTENDED:
84 DecodeUnsignedLeb128(&stream); // register_num
85 DecodeUnsignedLeb128P1(&stream); // name_idx
86 DecodeUnsignedLeb128P1(&stream); // type_idx
87 DecodeUnsignedLeb128P1(&stream); // sig_idx
88 break;
89 case DexFile::DBG_END_LOCAL:
90 case DexFile::DBG_RESTART_LOCAL:
91 DecodeUnsignedLeb128(&stream); // register_num
92 break;
93 case DexFile::DBG_SET_PROLOGUE_END:
94 case DexFile::DBG_SET_EPILOGUE_BEGIN:
95 break;
96 case DexFile::DBG_SET_FILE: {
97 DecodeUnsignedLeb128P1(&stream); // name_idx
98 break;
99 }
100 default: {
101 break;
102 }
103 }
104 }
105}
106
David Sehrd1e44e22016-10-06 17:09:32 -0700107static bool GetIdFromInstruction(Collections& collections,
108 const Instruction* dec_insn,
109 std::vector<TypeId*>* type_ids,
110 std::vector<StringId*>* string_ids,
111 std::vector<MethodId*>* method_ids,
112 std::vector<FieldId*>* field_ids) {
113 // Determine index and width of the string.
114 uint32_t index = 0;
115 switch (Instruction::FormatOf(dec_insn->Opcode())) {
116 // SOME NOT SUPPORTED:
117 // case Instruction::k20bc:
118 case Instruction::k21c:
119 case Instruction::k35c:
120 // case Instruction::k35ms:
121 case Instruction::k3rc:
122 // case Instruction::k3rms:
123 // case Instruction::k35mi:
124 // case Instruction::k3rmi:
125 index = dec_insn->VRegB();
126 break;
127 case Instruction::k31c:
128 index = dec_insn->VRegB();
129 break;
130 case Instruction::k22c:
131 // case Instruction::k22cs:
132 index = dec_insn->VRegC();
133 break;
134 default:
135 break;
136 } // switch
137
138 // Determine index type, and add reference to the appropriate collection.
139 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
140 case Instruction::kIndexTypeRef:
141 if (index < collections.TypeIdsSize()) {
142 type_ids->push_back(collections.GetTypeId(index));
143 return true;
144 }
145 break;
146 case Instruction::kIndexStringRef:
147 if (index < collections.StringIdsSize()) {
148 string_ids->push_back(collections.GetStringId(index));
149 return true;
150 }
151 break;
152 case Instruction::kIndexMethodRef:
153 if (index < collections.MethodIdsSize()) {
154 method_ids->push_back(collections.GetMethodId(index));
155 return true;
156 }
157 break;
158 case Instruction::kIndexFieldRef:
159 if (index < collections.FieldIdsSize()) {
160 field_ids->push_back(collections.GetFieldId(index));
161 return true;
162 }
163 break;
164 case Instruction::kIndexUnknown:
165 case Instruction::kIndexNone:
166 case Instruction::kIndexVtableOffset:
167 case Instruction::kIndexFieldOffset:
168 default:
169 break;
170 } // switch
171 return false;
172}
173
174/*
175 * Get all the types, strings, methods, and fields referred to from bytecode.
176 */
177static bool GetIdsFromByteCode(Collections& collections,
178 const CodeItem* code,
179 std::vector<TypeId*>* type_ids,
180 std::vector<StringId*>* string_ids,
181 std::vector<MethodId*>* method_ids,
182 std::vector<FieldId*>* field_ids) {
183 bool has_id = false;
184 // Iterate over all instructions.
185 const uint16_t* insns = code->Insns();
186 for (uint32_t insn_idx = 0; insn_idx < code->InsnsSize();) {
187 const Instruction* instruction = Instruction::At(&insns[insn_idx]);
188 const uint32_t insn_width = instruction->SizeInCodeUnits();
189 if (insn_width == 0) {
190 break;
191 }
192 has_id |= GetIdFromInstruction(collections,
193 instruction,
194 type_ids,
195 string_ids,
196 method_ids,
197 field_ids);
198 insn_idx += insn_width;
199 } // for
200 return has_id;
201}
202
Jeff Hao3ab96b42016-09-09 18:35:01 -0700203EncodedValue* Collections::ReadEncodedValue(const uint8_t** data) {
204 const uint8_t encoded_value = *(*data)++;
205 const uint8_t type = encoded_value & 0x1f;
206 EncodedValue* item = new EncodedValue(type);
207 ReadEncodedValue(data, type, encoded_value >> 5, item);
208 return item;
209}
210
211EncodedValue* Collections::ReadEncodedValue(const uint8_t** data, uint8_t type, uint8_t length) {
212 EncodedValue* item = new EncodedValue(type);
213 ReadEncodedValue(data, type, length, item);
214 return item;
215}
216
217void Collections::ReadEncodedValue(
218 const uint8_t** data, uint8_t type, uint8_t length, EncodedValue* item) {
219 switch (type) {
220 case DexFile::kDexAnnotationByte:
221 item->SetByte(static_cast<int8_t>(ReadVarWidth(data, length, false)));
222 break;
223 case DexFile::kDexAnnotationShort:
224 item->SetShort(static_cast<int16_t>(ReadVarWidth(data, length, true)));
225 break;
226 case DexFile::kDexAnnotationChar:
227 item->SetChar(static_cast<uint16_t>(ReadVarWidth(data, length, false)));
228 break;
229 case DexFile::kDexAnnotationInt:
230 item->SetInt(static_cast<int32_t>(ReadVarWidth(data, length, true)));
231 break;
232 case DexFile::kDexAnnotationLong:
233 item->SetLong(static_cast<int64_t>(ReadVarWidth(data, length, true)));
234 break;
235 case DexFile::kDexAnnotationFloat: {
236 // Fill on right.
237 union {
238 float f;
239 uint32_t data;
240 } conv;
241 conv.data = static_cast<uint32_t>(ReadVarWidth(data, length, false)) << (3 - length) * 8;
242 item->SetFloat(conv.f);
243 break;
244 }
245 case DexFile::kDexAnnotationDouble: {
246 // Fill on right.
247 union {
248 double d;
249 uint64_t data;
250 } conv;
251 conv.data = ReadVarWidth(data, length, false) << (7 - length) * 8;
252 item->SetDouble(conv.d);
253 break;
254 }
255 case DexFile::kDexAnnotationString: {
256 const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
257 item->SetStringId(GetStringId(string_index));
258 break;
259 }
260 case DexFile::kDexAnnotationType: {
261 const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
262 item->SetTypeId(GetTypeId(string_index));
263 break;
264 }
265 case DexFile::kDexAnnotationField:
266 case DexFile::kDexAnnotationEnum: {
267 const uint32_t field_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
268 item->SetFieldId(GetFieldId(field_index));
269 break;
270 }
271 case DexFile::kDexAnnotationMethod: {
272 const uint32_t method_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
273 item->SetMethodId(GetMethodId(method_index));
274 break;
275 }
276 case DexFile::kDexAnnotationArray: {
277 EncodedValueVector* values = new EncodedValueVector();
278 const uint32_t size = DecodeUnsignedLeb128(data);
279 // Decode all elements.
280 for (uint32_t i = 0; i < size; i++) {
281 values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(data)));
282 }
283 item->SetEncodedArray(new EncodedArrayItem(values));
284 break;
285 }
286 case DexFile::kDexAnnotationAnnotation: {
287 AnnotationElementVector* elements = new AnnotationElementVector();
288 const uint32_t type_idx = DecodeUnsignedLeb128(data);
289 const uint32_t size = DecodeUnsignedLeb128(data);
290 // Decode all name=value pairs.
291 for (uint32_t i = 0; i < size; i++) {
292 const uint32_t name_index = DecodeUnsignedLeb128(data);
293 elements->push_back(std::unique_ptr<AnnotationElement>(
294 new AnnotationElement(GetStringId(name_index), ReadEncodedValue(data))));
295 }
296 item->SetEncodedAnnotation(new EncodedAnnotation(GetTypeId(type_idx), elements));
297 break;
298 }
299 case DexFile::kDexAnnotationNull:
300 break;
301 case DexFile::kDexAnnotationBoolean:
302 item->SetBoolean(length != 0);
303 break;
304 default:
305 break;
306 }
307}
308
309void Collections::CreateStringId(const DexFile& dex_file, uint32_t i) {
310 const DexFile::StringId& disk_string_id = dex_file.GetStringId(i);
311 StringData* string_data = new StringData(dex_file.GetStringData(disk_string_id));
312 string_datas_.AddItem(string_data, disk_string_id.string_data_off_);
313
314 StringId* string_id = new StringId(string_data);
315 string_ids_.AddIndexedItem(string_id, StringIdsOffset() + i * StringId::ItemSize(), i);
316}
317
318void Collections::CreateTypeId(const DexFile& dex_file, uint32_t i) {
319 const DexFile::TypeId& disk_type_id = dex_file.GetTypeId(i);
320 TypeId* type_id = new TypeId(GetStringId(disk_type_id.descriptor_idx_));
321 type_ids_.AddIndexedItem(type_id, TypeIdsOffset() + i * TypeId::ItemSize(), i);
322}
323
324void Collections::CreateProtoId(const DexFile& dex_file, uint32_t i) {
325 const DexFile::ProtoId& disk_proto_id = dex_file.GetProtoId(i);
326 const DexFile::TypeList* type_list = dex_file.GetProtoParameters(disk_proto_id);
Jeff Haoa8621002016-10-04 18:13:44 +0000327 TypeList* parameter_type_list = CreateTypeList(type_list, disk_proto_id.parameters_off_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700328
329 ProtoId* proto_id = new ProtoId(GetStringId(disk_proto_id.shorty_idx_),
330 GetTypeId(disk_proto_id.return_type_idx_),
331 parameter_type_list);
332 proto_ids_.AddIndexedItem(proto_id, ProtoIdsOffset() + i * ProtoId::ItemSize(), i);
333}
334
335void Collections::CreateFieldId(const DexFile& dex_file, uint32_t i) {
336 const DexFile::FieldId& disk_field_id = dex_file.GetFieldId(i);
337 FieldId* field_id = new FieldId(GetTypeId(disk_field_id.class_idx_),
338 GetTypeId(disk_field_id.type_idx_),
339 GetStringId(disk_field_id.name_idx_));
340 field_ids_.AddIndexedItem(field_id, FieldIdsOffset() + i * FieldId::ItemSize(), i);
341}
342
343void Collections::CreateMethodId(const DexFile& dex_file, uint32_t i) {
344 const DexFile::MethodId& disk_method_id = dex_file.GetMethodId(i);
345 MethodId* method_id = new MethodId(GetTypeId(disk_method_id.class_idx_),
346 GetProtoId(disk_method_id.proto_idx_),
347 GetStringId(disk_method_id.name_idx_));
348 method_ids_.AddIndexedItem(method_id, MethodIdsOffset() + i * MethodId::ItemSize(), i);
349}
350
351void Collections::CreateClassDef(const DexFile& dex_file, uint32_t i) {
352 const DexFile::ClassDef& disk_class_def = dex_file.GetClassDef(i);
353 const TypeId* class_type = GetTypeId(disk_class_def.class_idx_);
354 uint32_t access_flags = disk_class_def.access_flags_;
355 const TypeId* superclass = GetTypeIdOrNullPtr(disk_class_def.superclass_idx_);
356
357 const DexFile::TypeList* type_list = dex_file.GetInterfacesList(disk_class_def);
Jeff Haoa8621002016-10-04 18:13:44 +0000358 TypeList* interfaces_type_list = CreateTypeList(type_list, disk_class_def.interfaces_off_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700359
360 const StringId* source_file = GetStringIdOrNullPtr(disk_class_def.source_file_idx_);
361 // Annotations.
362 AnnotationsDirectoryItem* annotations = nullptr;
363 const DexFile::AnnotationsDirectoryItem* disk_annotations_directory_item =
364 dex_file.GetAnnotationsDirectory(disk_class_def);
365 if (disk_annotations_directory_item != nullptr) {
366 annotations = CreateAnnotationsDirectoryItem(
367 dex_file, disk_annotations_directory_item, disk_class_def.annotations_off_);
368 }
369 // Static field initializers.
370 const uint8_t* static_data = dex_file.GetEncodedStaticFieldValuesArray(disk_class_def);
371 EncodedArrayItem* static_values =
372 CreateEncodedArrayItem(static_data, disk_class_def.static_values_off_);
373 ClassData* class_data = CreateClassData(
374 dex_file, dex_file.GetClassData(disk_class_def), disk_class_def.class_data_off_);
375 ClassDef* class_def = new ClassDef(class_type, access_flags, superclass, interfaces_type_list,
376 source_file, annotations, static_values, class_data);
377 class_defs_.AddIndexedItem(class_def, ClassDefsOffset() + i * ClassDef::ItemSize(), i);
378}
379
Jeff Haoa8621002016-10-04 18:13:44 +0000380TypeList* Collections::CreateTypeList(const DexFile::TypeList* dex_type_list, uint32_t offset) {
381 if (dex_type_list == nullptr) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700382 return nullptr;
383 }
384 // TODO: Create more efficient lookup for existing type lists.
385 for (std::unique_ptr<TypeList>& type_list : TypeLists()) {
386 if (type_list->GetOffset() == offset) {
387 return type_list.get();
388 }
389 }
390 TypeIdVector* type_vector = new TypeIdVector();
Jeff Haoa8621002016-10-04 18:13:44 +0000391 uint32_t size = dex_type_list->Size();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700392 for (uint32_t index = 0; index < size; ++index) {
393 type_vector->push_back(GetTypeId(dex_type_list->GetTypeItem(index).type_idx_));
394 }
395 TypeList* new_type_list = new TypeList(type_vector);
396 type_lists_.AddItem(new_type_list, offset);
397 return new_type_list;
398}
399
400EncodedArrayItem* Collections::CreateEncodedArrayItem(const uint8_t* static_data, uint32_t offset) {
401 if (static_data == nullptr) {
402 return nullptr;
403 }
Jeff Haoa8621002016-10-04 18:13:44 +0000404 for (std::unique_ptr<EncodedArrayItem>& existing_array_item : EncodedArrayItems()) {
405 if (existing_array_item->GetOffset() == offset) {
406 return existing_array_item.get();
407 }
408 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700409 uint32_t size = DecodeUnsignedLeb128(&static_data);
410 EncodedValueVector* values = new EncodedValueVector();
411 for (uint32_t i = 0; i < size; ++i) {
412 values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(&static_data)));
413 }
414 // TODO: Calculate the size of the encoded array.
415 EncodedArrayItem* encoded_array_item = new EncodedArrayItem(values);
416 encoded_array_items_.AddItem(encoded_array_item, offset);
417 return encoded_array_item;
418}
419
420AnnotationItem* Collections::CreateAnnotationItem(const DexFile::AnnotationItem* annotation,
421 uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000422 for (std::unique_ptr<AnnotationItem>& existing_annotation_item : AnnotationItems()) {
423 if (existing_annotation_item->GetOffset() == offset) {
424 return existing_annotation_item.get();
425 }
426 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700427 uint8_t visibility = annotation->visibility_;
428 const uint8_t* annotation_data = annotation->annotation_;
429 EncodedValue* encoded_value =
430 ReadEncodedValue(&annotation_data, DexFile::kDexAnnotationAnnotation, 0);
431 // TODO: Calculate the size of the annotation.
432 AnnotationItem* annotation_item =
433 new AnnotationItem(visibility, encoded_value->ReleaseEncodedAnnotation());
434 annotation_items_.AddItem(annotation_item, offset);
435 return annotation_item;
436}
437
438
439AnnotationSetItem* Collections::CreateAnnotationSetItem(const DexFile& dex_file,
440 const DexFile::AnnotationSetItem& disk_annotations_item, uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000441 if (disk_annotations_item.size_ == 0 && offset == 0) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700442 return nullptr;
443 }
Jeff Haoa8621002016-10-04 18:13:44 +0000444 for (std::unique_ptr<AnnotationSetItem>& existing_anno_set_item : AnnotationSetItems()) {
445 if (existing_anno_set_item->GetOffset() == offset) {
446 return existing_anno_set_item.get();
447 }
448 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700449 std::vector<AnnotationItem*>* items = new std::vector<AnnotationItem*>();
450 for (uint32_t i = 0; i < disk_annotations_item.size_; ++i) {
451 const DexFile::AnnotationItem* annotation =
452 dex_file.GetAnnotationItem(&disk_annotations_item, i);
453 if (annotation == nullptr) {
454 continue;
455 }
456 AnnotationItem* annotation_item =
457 CreateAnnotationItem(annotation, disk_annotations_item.entries_[i]);
458 items->push_back(annotation_item);
459 }
460 AnnotationSetItem* annotation_set_item = new AnnotationSetItem(items);
461 annotation_set_items_.AddItem(annotation_set_item, offset);
462 return annotation_set_item;
463}
464
465AnnotationsDirectoryItem* Collections::CreateAnnotationsDirectoryItem(const DexFile& dex_file,
466 const DexFile::AnnotationsDirectoryItem* disk_annotations_item, uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000467 for (std::unique_ptr<AnnotationsDirectoryItem>& anno_dir_item : AnnotationsDirectoryItems()) {
468 if (anno_dir_item->GetOffset() == offset) {
469 return anno_dir_item.get();
470 }
471 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700472 const DexFile::AnnotationSetItem* class_set_item =
473 dex_file.GetClassAnnotationSet(disk_annotations_item);
474 AnnotationSetItem* class_annotation = nullptr;
475 if (class_set_item != nullptr) {
476 uint32_t offset = disk_annotations_item->class_annotations_off_;
477 class_annotation = CreateAnnotationSetItem(dex_file, *class_set_item, offset);
478 }
479 const DexFile::FieldAnnotationsItem* fields =
480 dex_file.GetFieldAnnotations(disk_annotations_item);
481 FieldAnnotationVector* field_annotations = nullptr;
482 if (fields != nullptr) {
483 field_annotations = new FieldAnnotationVector();
484 for (uint32_t i = 0; i < disk_annotations_item->fields_size_; ++i) {
485 FieldId* field_id = GetFieldId(fields[i].field_idx_);
486 const DexFile::AnnotationSetItem* field_set_item =
487 dex_file.GetFieldAnnotationSetItem(fields[i]);
488 uint32_t annotation_set_offset = fields[i].annotations_off_;
489 AnnotationSetItem* annotation_set_item =
490 CreateAnnotationSetItem(dex_file, *field_set_item, annotation_set_offset);
491 field_annotations->push_back(std::unique_ptr<FieldAnnotation>(
492 new FieldAnnotation(field_id, annotation_set_item)));
493 }
494 }
495 const DexFile::MethodAnnotationsItem* methods =
496 dex_file.GetMethodAnnotations(disk_annotations_item);
497 MethodAnnotationVector* method_annotations = nullptr;
498 if (methods != nullptr) {
499 method_annotations = new MethodAnnotationVector();
500 for (uint32_t i = 0; i < disk_annotations_item->methods_size_; ++i) {
501 MethodId* method_id = GetMethodId(methods[i].method_idx_);
502 const DexFile::AnnotationSetItem* method_set_item =
503 dex_file.GetMethodAnnotationSetItem(methods[i]);
504 uint32_t annotation_set_offset = methods[i].annotations_off_;
505 AnnotationSetItem* annotation_set_item =
506 CreateAnnotationSetItem(dex_file, *method_set_item, annotation_set_offset);
507 method_annotations->push_back(std::unique_ptr<MethodAnnotation>(
508 new MethodAnnotation(method_id, annotation_set_item)));
509 }
510 }
511 const DexFile::ParameterAnnotationsItem* parameters =
512 dex_file.GetParameterAnnotations(disk_annotations_item);
513 ParameterAnnotationVector* parameter_annotations = nullptr;
514 if (parameters != nullptr) {
515 parameter_annotations = new ParameterAnnotationVector();
516 for (uint32_t i = 0; i < disk_annotations_item->parameters_size_; ++i) {
517 MethodId* method_id = GetMethodId(parameters[i].method_idx_);
518 const DexFile::AnnotationSetRefList* list =
519 dex_file.GetParameterAnnotationSetRefList(&parameters[i]);
520 parameter_annotations->push_back(std::unique_ptr<ParameterAnnotation>(
521 GenerateParameterAnnotation(dex_file, method_id, list, parameters[i].annotations_off_)));
522 }
523 }
524 // TODO: Calculate the size of the annotations directory.
525 AnnotationsDirectoryItem* annotations_directory_item = new AnnotationsDirectoryItem(
526 class_annotation, field_annotations, method_annotations, parameter_annotations);
527 annotations_directory_items_.AddItem(annotations_directory_item, offset);
528 return annotations_directory_item;
529}
530
531ParameterAnnotation* Collections::GenerateParameterAnnotation(
532 const DexFile& dex_file, MethodId* method_id,
533 const DexFile::AnnotationSetRefList* annotation_set_ref_list, uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000534 AnnotationSetRefList* set_ref_list = nullptr;
535 for (std::unique_ptr<AnnotationSetRefList>& existing_set_ref_list : AnnotationSetRefLists()) {
536 if (existing_set_ref_list->GetOffset() == offset) {
537 set_ref_list = existing_set_ref_list.get();
538 break;
539 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700540 }
Jeff Haoa8621002016-10-04 18:13:44 +0000541 if (set_ref_list == nullptr) {
542 std::vector<AnnotationSetItem*>* annotations = new std::vector<AnnotationSetItem*>();
543 for (uint32_t i = 0; i < annotation_set_ref_list->size_; ++i) {
544 const DexFile::AnnotationSetItem* annotation_set_item =
545 dex_file.GetSetRefItemItem(&annotation_set_ref_list->list_[i]);
546 uint32_t set_offset = annotation_set_ref_list->list_[i].annotations_off_;
547 annotations->push_back(CreateAnnotationSetItem(dex_file, *annotation_set_item, set_offset));
548 }
549 set_ref_list = new AnnotationSetRefList(annotations);
550 annotation_set_ref_lists_.AddItem(set_ref_list, offset);
551 }
552 return new ParameterAnnotation(method_id, set_ref_list);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700553}
554
555CodeItem* Collections::CreateCodeItem(const DexFile& dex_file,
556 const DexFile::CodeItem& disk_code_item, uint32_t offset) {
557 uint16_t registers_size = disk_code_item.registers_size_;
558 uint16_t ins_size = disk_code_item.ins_size_;
559 uint16_t outs_size = disk_code_item.outs_size_;
560 uint32_t tries_size = disk_code_item.tries_size_;
561
562 // TODO: Calculate the size of the debug info.
563 const uint8_t* debug_info_stream = dex_file.GetDebugInfoStream(&disk_code_item);
564 DebugInfoItem* debug_info = nullptr;
565 if (debug_info_stream != nullptr) {
Jeff Haoa8621002016-10-04 18:13:44 +0000566 uint32_t debug_info_size = GetDebugInfoStreamSize(debug_info_stream);
567 uint8_t* debug_info_buffer = new uint8_t[debug_info_size];
568 memcpy(debug_info_buffer, debug_info_stream, debug_info_size);
569 debug_info = new DebugInfoItem(debug_info_size, debug_info_buffer);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700570 debug_info_items_.AddItem(debug_info, disk_code_item.debug_info_off_);
571 }
572
573 uint32_t insns_size = disk_code_item.insns_size_in_code_units_;
574 uint16_t* insns = new uint16_t[insns_size];
575 memcpy(insns, disk_code_item.insns_, insns_size * sizeof(uint16_t));
576
577 TryItemVector* tries = nullptr;
Jeff Haoa8621002016-10-04 18:13:44 +0000578 CatchHandlerVector* handler_list = nullptr;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700579 if (tries_size > 0) {
580 tries = new TryItemVector();
Jeff Haoa8621002016-10-04 18:13:44 +0000581 handler_list = new CatchHandlerVector();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700582 for (uint32_t i = 0; i < tries_size; ++i) {
583 const DexFile::TryItem* disk_try_item = dex_file.GetTryItems(disk_code_item, i);
584 uint32_t start_addr = disk_try_item->start_addr_;
585 uint16_t insn_count = disk_try_item->insn_count_;
Jeff Haoa8621002016-10-04 18:13:44 +0000586 uint16_t handler_off = disk_try_item->handler_off_;
587 const CatchHandler* handlers = nullptr;
588 for (std::unique_ptr<const CatchHandler>& existing_handlers : *handler_list) {
589 if (handler_off == existing_handlers->GetListOffset()) {
590 handlers = existing_handlers.get();
591 }
592 }
593 if (handlers == nullptr) {
594 bool catch_all = false;
595 TypeAddrPairVector* addr_pairs = new TypeAddrPairVector();
596 for (CatchHandlerIterator it(disk_code_item, *disk_try_item); it.HasNext(); it.Next()) {
597 const uint16_t type_index = it.GetHandlerTypeIndex();
598 const TypeId* type_id = GetTypeIdOrNullPtr(type_index);
599 catch_all |= type_id == nullptr;
600 addr_pairs->push_back(std::unique_ptr<const TypeAddrPair>(
601 new TypeAddrPair(type_id, it.GetHandlerAddress())));
602 }
603 handlers = new CatchHandler(catch_all, handler_off, addr_pairs);
604 handler_list->push_back(std::unique_ptr<const CatchHandler>(handlers));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700605 }
606 TryItem* try_item = new TryItem(start_addr, insn_count, handlers);
607 tries->push_back(std::unique_ptr<const TryItem>(try_item));
608 }
609 }
610 // TODO: Calculate the size of the code item.
Jeff Haoa8621002016-10-04 18:13:44 +0000611 CodeItem* code_item = new CodeItem(
612 registers_size, ins_size, outs_size, debug_info, insns_size, insns, tries, handler_list);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700613 code_items_.AddItem(code_item, offset);
David Sehrd1e44e22016-10-06 17:09:32 -0700614 // Add "fixup" references to types, strings, methods, and fields.
615 // This is temporary, as we will probably want more detailed parsing of the
616 // instructions here.
617 std::unique_ptr<std::vector<TypeId*>> type_ids(new std::vector<TypeId*>());
618 std::unique_ptr<std::vector<StringId*>> string_ids(new std::vector<StringId*>());
619 std::unique_ptr<std::vector<MethodId*>> method_ids(new std::vector<MethodId*>());
620 std::unique_ptr<std::vector<FieldId*>> field_ids(new std::vector<FieldId*>());
621 if (GetIdsFromByteCode(*this,
622 code_item,
623 type_ids.get(),
624 string_ids.get(),
625 method_ids.get(),
626 field_ids.get())) {
627 CodeFixups* fixups = new CodeFixups(type_ids.release(),
628 string_ids.release(),
629 method_ids.release(),
630 field_ids.release());
631 code_item->SetCodeFixups(fixups);
632 }
633
Jeff Hao3ab96b42016-09-09 18:35:01 -0700634 return code_item;
635}
636
637MethodItem* Collections::GenerateMethodItem(const DexFile& dex_file, ClassDataItemIterator& cdii) {
638 MethodId* method_item = GetMethodId(cdii.GetMemberIndex());
639 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
640 const DexFile::CodeItem* disk_code_item = cdii.GetMethodCodeItem();
641 CodeItem* code_item = nullptr;
642 DebugInfoItem* debug_info = nullptr;
643 if (disk_code_item != nullptr) {
644 code_item = CreateCodeItem(dex_file, *disk_code_item, cdii.GetMethodCodeItemOffset());
645 debug_info = code_item->DebugInfo();
646 }
647 if (debug_info != nullptr) {
648 bool is_static = (access_flags & kAccStatic) != 0;
649 dex_file.DecodeDebugLocalInfo(
650 disk_code_item, is_static, cdii.GetMemberIndex(), GetLocalsCb, debug_info);
651 dex_file.DecodeDebugPositionInfo(disk_code_item, GetPositionsCb, debug_info);
652 }
653 return new MethodItem(access_flags, method_item, code_item);
654}
655
656ClassData* Collections::CreateClassData(
657 const DexFile& dex_file, const uint8_t* encoded_data, uint32_t offset) {
658 // Read the fields and methods defined by the class, resolving the circular reference from those
659 // to classes by setting class at the same time.
660 ClassData* class_data = nullptr;
661 if (encoded_data != nullptr) {
662 ClassDataItemIterator cdii(dex_file, encoded_data);
663 // Static fields.
664 FieldItemVector* static_fields = new FieldItemVector();
665 for (uint32_t i = 0; cdii.HasNextStaticField(); i++, cdii.Next()) {
666 FieldId* field_item = GetFieldId(cdii.GetMemberIndex());
667 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
668 static_fields->push_back(std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item)));
669 }
670 // Instance fields.
671 FieldItemVector* instance_fields = new FieldItemVector();
672 for (uint32_t i = 0; cdii.HasNextInstanceField(); i++, cdii.Next()) {
673 FieldId* field_item = GetFieldId(cdii.GetMemberIndex());
674 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
675 instance_fields->push_back(
676 std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item)));
677 }
678 // Direct methods.
679 MethodItemVector* direct_methods = new MethodItemVector();
680 for (uint32_t i = 0; cdii.HasNextDirectMethod(); i++, cdii.Next()) {
681 direct_methods->push_back(
682 std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii)));
683 }
684 // Virtual methods.
685 MethodItemVector* virtual_methods = new MethodItemVector();
686 for (uint32_t i = 0; cdii.HasNextVirtualMethod(); i++, cdii.Next()) {
687 virtual_methods->push_back(
688 std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii)));
689 }
690 // TODO: Calculate the size of the class data.
691 class_data = new ClassData(static_fields, instance_fields, direct_methods, virtual_methods);
692 class_datas_.AddItem(class_data, offset);
693 }
694 return class_data;
695}
696
Jeff Hao3ab96b42016-09-09 18:35:01 -0700697} // namespace dex_ir
698} // namespace art