blob: 1add6bfedea3a85810d011c00611754cffbe0fe9 [file] [log] [blame]
David Sehr7629f602016-08-07 16:01:51 -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 "dexlayout.h"
24
25#include <inttypes.h>
26#include <stdio.h>
27
28#include <iostream>
29#include <memory>
30#include <sstream>
31#include <vector>
32
Andreas Gampe46ee31b2016-12-14 10:11:49 -080033#include "android-base/stringprintf.h"
34
David Sehr853a8e12016-09-01 13:03:50 -070035#include "dex_ir_builder.h"
David Sehr7629f602016-08-07 16:01:51 -070036#include "dex_file-inl.h"
37#include "dex_instruction-inl.h"
David Sehrcdcfde72016-09-26 07:44:04 -070038#include "dex_visualize.h"
Jeff Haoa8621002016-10-04 18:13:44 +000039#include "dex_writer.h"
Calin Juravle33083d62017-01-18 15:29:12 -080040#include "jit/profile_compilation_info.h"
Jeff Haoea7c6292016-11-14 18:10:16 -080041#include "mem_map.h"
Nicolas Geoffrayfd1a6c22016-10-04 11:01:17 +000042#include "os.h"
David Sehr7629f602016-08-07 16:01:51 -070043#include "utils.h"
44
45namespace art {
46
Andreas Gampe46ee31b2016-12-14 10:11:49 -080047using android::base::StringPrintf;
48
David Sehr7629f602016-08-07 16:01:51 -070049/*
David Sehr7629f602016-08-07 16:01:51 -070050 * Flags for use with createAccessFlagStr().
51 */
52enum AccessFor {
53 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
54};
55const int kNumFlags = 18;
56
57/*
58 * Gets 2 little-endian bytes.
59 */
60static inline uint16_t Get2LE(unsigned char const* src) {
61 return src[0] | (src[1] << 8);
62}
63
64/*
Jeff Haoc3acfc52016-08-29 14:18:26 -070065 * Converts a type descriptor to human-readable "dotted" form. For
66 * example, "Ljava/lang/String;" becomes "java.lang.String", and
67 * "[I" becomes "int[]". Also converts '$' to '.', which means this
68 * form can't be converted back to a descriptor.
69 */
70static std::string DescriptorToDotWrapper(const char* descriptor) {
71 std::string result = DescriptorToDot(descriptor);
72 size_t found = result.find('$');
73 while (found != std::string::npos) {
74 result[found] = '.';
75 found = result.find('$', found);
76 }
77 return result;
78}
79
80/*
David Sehr7629f602016-08-07 16:01:51 -070081 * Converts the class name portion of a type descriptor to human-readable
82 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
83 */
84static std::string DescriptorClassToDot(const char* str) {
85 std::string descriptor(str);
86 // Reduce to just the class name prefix.
87 size_t last_slash = descriptor.rfind('/');
88 if (last_slash == std::string::npos) {
89 last_slash = 0;
90 }
91 // Start past the '/' or 'L'.
92 last_slash++;
93
94 // Copy class name over, trimming trailing ';'.
95 size_t size = descriptor.size() - 1 - last_slash;
96 std::string result(descriptor.substr(last_slash, size));
97
98 // Replace '$' with '.'.
99 size_t dollar_sign = result.find('$');
100 while (dollar_sign != std::string::npos) {
101 result[dollar_sign] = '.';
102 dollar_sign = result.find('$', dollar_sign);
103 }
104
105 return result;
106}
107
108/*
109 * Returns string representing the boolean value.
110 */
111static const char* StrBool(bool val) {
112 return val ? "true" : "false";
113}
114
115/*
116 * Returns a quoted string representing the boolean value.
117 */
118static const char* QuotedBool(bool val) {
119 return val ? "\"true\"" : "\"false\"";
120}
121
122/*
123 * Returns a quoted string representing the access flags.
124 */
125static const char* QuotedVisibility(uint32_t access_flags) {
126 if (access_flags & kAccPublic) {
127 return "\"public\"";
128 } else if (access_flags & kAccProtected) {
129 return "\"protected\"";
130 } else if (access_flags & kAccPrivate) {
131 return "\"private\"";
132 } else {
133 return "\"package\"";
134 }
135}
136
137/*
138 * Counts the number of '1' bits in a word.
139 */
140static int CountOnes(uint32_t val) {
141 val = val - ((val >> 1) & 0x55555555);
142 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
143 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
144}
145
146/*
147 * Creates a new string with human-readable access flags.
148 *
149 * In the base language the access_flags fields are type uint16_t; in Dalvik they're uint32_t.
150 */
151static char* CreateAccessFlagStr(uint32_t flags, AccessFor for_what) {
152 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
153 {
154 "PUBLIC", /* 0x00001 */
155 "PRIVATE", /* 0x00002 */
156 "PROTECTED", /* 0x00004 */
157 "STATIC", /* 0x00008 */
158 "FINAL", /* 0x00010 */
159 "?", /* 0x00020 */
160 "?", /* 0x00040 */
161 "?", /* 0x00080 */
162 "?", /* 0x00100 */
163 "INTERFACE", /* 0x00200 */
164 "ABSTRACT", /* 0x00400 */
165 "?", /* 0x00800 */
166 "SYNTHETIC", /* 0x01000 */
167 "ANNOTATION", /* 0x02000 */
168 "ENUM", /* 0x04000 */
169 "?", /* 0x08000 */
170 "VERIFIED", /* 0x10000 */
171 "OPTIMIZED", /* 0x20000 */
172 }, {
173 "PUBLIC", /* 0x00001 */
174 "PRIVATE", /* 0x00002 */
175 "PROTECTED", /* 0x00004 */
176 "STATIC", /* 0x00008 */
177 "FINAL", /* 0x00010 */
178 "SYNCHRONIZED", /* 0x00020 */
179 "BRIDGE", /* 0x00040 */
180 "VARARGS", /* 0x00080 */
181 "NATIVE", /* 0x00100 */
182 "?", /* 0x00200 */
183 "ABSTRACT", /* 0x00400 */
184 "STRICT", /* 0x00800 */
185 "SYNTHETIC", /* 0x01000 */
186 "?", /* 0x02000 */
187 "?", /* 0x04000 */
188 "MIRANDA", /* 0x08000 */
189 "CONSTRUCTOR", /* 0x10000 */
190 "DECLARED_SYNCHRONIZED", /* 0x20000 */
191 }, {
192 "PUBLIC", /* 0x00001 */
193 "PRIVATE", /* 0x00002 */
194 "PROTECTED", /* 0x00004 */
195 "STATIC", /* 0x00008 */
196 "FINAL", /* 0x00010 */
197 "?", /* 0x00020 */
198 "VOLATILE", /* 0x00040 */
199 "TRANSIENT", /* 0x00080 */
200 "?", /* 0x00100 */
201 "?", /* 0x00200 */
202 "?", /* 0x00400 */
203 "?", /* 0x00800 */
204 "SYNTHETIC", /* 0x01000 */
205 "?", /* 0x02000 */
206 "ENUM", /* 0x04000 */
207 "?", /* 0x08000 */
208 "?", /* 0x10000 */
209 "?", /* 0x20000 */
210 },
211 };
212
213 // Allocate enough storage to hold the expected number of strings,
214 // plus a space between each. We over-allocate, using the longest
215 // string above as the base metric.
216 const int kLongest = 21; // The strlen of longest string above.
217 const int count = CountOnes(flags);
218 char* str;
219 char* cp;
220 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
221
222 for (int i = 0; i < kNumFlags; i++) {
223 if (flags & 0x01) {
224 const char* accessStr = kAccessStrings[for_what][i];
225 const int len = strlen(accessStr);
226 if (cp != str) {
227 *cp++ = ' ';
228 }
229 memcpy(cp, accessStr, len);
230 cp += len;
231 }
232 flags >>= 1;
233 } // for
234
235 *cp = '\0';
236 return str;
237}
238
239static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) {
240 if (proto == nullptr) {
241 return "<no signature>";
242 }
243
David Sehr7629f602016-08-07 16:01:51 -0700244 std::string result("(");
Jeff Haoa8621002016-10-04 18:13:44 +0000245 const dex_ir::TypeList* type_list = proto->Parameters();
246 if (type_list != nullptr) {
247 for (const dex_ir::TypeId* type_id : *type_list->GetTypeList()) {
248 result += type_id->GetStringId()->Data();
249 }
David Sehr7629f602016-08-07 16:01:51 -0700250 }
251 result += ")";
252 result += proto->ReturnType()->GetStringId()->Data();
253 return result;
254}
255
256/*
257 * Copies character data from "data" to "out", converting non-ASCII values
258 * to fprintf format chars or an ASCII filler ('.' or '?').
259 *
260 * The output buffer must be able to hold (2*len)+1 bytes. The result is
261 * NULL-terminated.
262 */
263static void Asciify(char* out, const unsigned char* data, size_t len) {
264 while (len--) {
265 if (*data < 0x20) {
266 // Could do more here, but we don't need them yet.
267 switch (*data) {
268 case '\0':
269 *out++ = '\\';
270 *out++ = '0';
271 break;
272 case '\n':
273 *out++ = '\\';
274 *out++ = 'n';
275 break;
276 default:
277 *out++ = '.';
278 break;
279 } // switch
280 } else if (*data >= 0x80) {
281 *out++ = '?';
282 } else {
283 *out++ = *data;
284 }
285 data++;
286 } // while
287 *out = '\0';
288}
289
290/*
291 * Dumps a string value with some escape characters.
292 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800293static void DumpEscapedString(const char* p, FILE* out_file) {
294 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700295 for (; *p; p++) {
296 switch (*p) {
297 case '\\':
Jeff Haoea7c6292016-11-14 18:10:16 -0800298 fputs("\\\\", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700299 break;
300 case '\"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800301 fputs("\\\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700302 break;
303 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800304 fputs("\\t", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700305 break;
306 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800307 fputs("\\n", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700308 break;
309 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800310 fputs("\\r", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700311 break;
312 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800313 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700314 } // switch
315 } // for
Jeff Haoea7c6292016-11-14 18:10:16 -0800316 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700317}
318
319/*
320 * Dumps a string as an XML attribute value.
321 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800322static void DumpXmlAttribute(const char* p, FILE* out_file) {
David Sehr7629f602016-08-07 16:01:51 -0700323 for (; *p; p++) {
324 switch (*p) {
325 case '&':
Jeff Haoea7c6292016-11-14 18:10:16 -0800326 fputs("&amp;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700327 break;
328 case '<':
Jeff Haoea7c6292016-11-14 18:10:16 -0800329 fputs("&lt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700330 break;
331 case '>':
Jeff Haoea7c6292016-11-14 18:10:16 -0800332 fputs("&gt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700333 break;
334 case '"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800335 fputs("&quot;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700336 break;
337 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800338 fputs("&#x9;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700339 break;
340 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800341 fputs("&#xA;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700342 break;
343 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800344 fputs("&#xD;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700345 break;
346 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800347 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700348 } // switch
349 } // for
350}
351
David Sehr7629f602016-08-07 16:01:51 -0700352/*
353 * Helper for dumpInstruction(), which builds the string
354 * representation for the index in the given instruction.
355 * Returns a pointer to a buffer of sufficient size.
356 */
357static std::unique_ptr<char[]> IndexString(dex_ir::Header* header,
358 const Instruction* dec_insn,
359 size_t buf_size) {
360 std::unique_ptr<char[]> buf(new char[buf_size]);
361 // Determine index and width of the string.
362 uint32_t index = 0;
Jeff Haoea7c6292016-11-14 18:10:16 -0800363 uint32_t secondary_index = DexFile::kDexNoIndex;
David Sehr7629f602016-08-07 16:01:51 -0700364 uint32_t width = 4;
365 switch (Instruction::FormatOf(dec_insn->Opcode())) {
366 // SOME NOT SUPPORTED:
367 // case Instruction::k20bc:
368 case Instruction::k21c:
369 case Instruction::k35c:
370 // case Instruction::k35ms:
371 case Instruction::k3rc:
372 // case Instruction::k3rms:
373 // case Instruction::k35mi:
374 // case Instruction::k3rmi:
375 index = dec_insn->VRegB();
376 width = 4;
377 break;
378 case Instruction::k31c:
379 index = dec_insn->VRegB();
380 width = 8;
381 break;
382 case Instruction::k22c:
383 // case Instruction::k22cs:
384 index = dec_insn->VRegC();
385 width = 4;
386 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100387 case Instruction::k45cc:
388 case Instruction::k4rcc:
389 index = dec_insn->VRegB();
390 secondary_index = dec_insn->VRegH();
391 width = 4;
David Sehr7629f602016-08-07 16:01:51 -0700392 default:
393 break;
394 } // switch
395
396 // Determine index type.
397 size_t outSize = 0;
398 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
399 case Instruction::kIndexUnknown:
400 // This function should never get called for this type, but do
401 // something sensible here, just to help with debugging.
402 outSize = snprintf(buf.get(), buf_size, "<unknown-index>");
403 break;
404 case Instruction::kIndexNone:
405 // This function should never get called for this type, but do
406 // something sensible here, just to help with debugging.
407 outSize = snprintf(buf.get(), buf_size, "<no-index>");
408 break;
409 case Instruction::kIndexTypeRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700410 if (index < header->GetCollections().TypeIdsSize()) {
411 const char* tp = header->GetCollections().GetTypeId(index)->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -0700412 outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index);
413 } else {
414 outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index);
415 }
416 break;
417 case Instruction::kIndexStringRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700418 if (index < header->GetCollections().StringIdsSize()) {
419 const char* st = header->GetCollections().GetStringId(index)->Data();
David Sehr7629f602016-08-07 16:01:51 -0700420 outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index);
421 } else {
422 outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index);
423 }
424 break;
425 case Instruction::kIndexMethodRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700426 if (index < header->GetCollections().MethodIdsSize()) {
427 dex_ir::MethodId* method_id = header->GetCollections().GetMethodId(index);
David Sehr7629f602016-08-07 16:01:51 -0700428 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -0700429 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -0700430 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
431 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x",
David Sehr72359222016-09-07 13:04:01 -0700432 back_descriptor, name, type_descriptor.c_str(), width, index);
David Sehr7629f602016-08-07 16:01:51 -0700433 } else {
434 outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index);
435 }
436 break;
437 case Instruction::kIndexFieldRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700438 if (index < header->GetCollections().FieldIdsSize()) {
439 dex_ir::FieldId* field_id = header->GetCollections().GetFieldId(index);
David Sehr7629f602016-08-07 16:01:51 -0700440 const char* name = field_id->Name()->Data();
441 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
442 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
443 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x",
444 back_descriptor, name, type_descriptor, width, index);
445 } else {
446 outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index);
447 }
448 break;
449 case Instruction::kIndexVtableOffset:
450 outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x",
451 width, index, width, index);
452 break;
453 case Instruction::kIndexFieldOffset:
454 outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index);
455 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100456 case Instruction::kIndexMethodAndProtoRef: {
457 std::string method("<method?>");
458 std::string proto("<proto?>");
459 if (index < header->GetCollections().MethodIdsSize()) {
460 dex_ir::MethodId* method_id = header->GetCollections().GetMethodId(index);
461 const char* name = method_id->Name()->Data();
462 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
463 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
464 method = StringPrintf("%s.%s:%s", back_descriptor, name, type_descriptor.c_str());
465 }
466 if (secondary_index < header->GetCollections().ProtoIdsSize()) {
467 dex_ir::ProtoId* proto_id = header->GetCollections().GetProtoId(secondary_index);
468 proto = GetSignatureForProtoId(proto_id);
469 }
470 outSize = snprintf(buf.get(), buf_size, "%s, %s // method@%0*x, proto@%0*x",
471 method.c_str(), proto.c_str(), width, index, width, secondary_index);
Jeff Haoea7c6292016-11-14 18:10:16 -0800472 }
473 break;
474 // SOME NOT SUPPORTED:
475 // case Instruction::kIndexVaries:
476 // case Instruction::kIndexInlineMethod:
David Sehr7629f602016-08-07 16:01:51 -0700477 default:
478 outSize = snprintf(buf.get(), buf_size, "<?>");
479 break;
480 } // switch
481
482 // Determine success of string construction.
483 if (outSize >= buf_size) {
484 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
485 // doesn't count/ the '\0' as part of its returned size, so we add explicit
486 // space for it here.
487 return IndexString(header, dec_insn, outSize + 1);
488 }
489 return buf;
490}
491
492/*
Jeff Haoea7c6292016-11-14 18:10:16 -0800493 * Dumps encoded annotation.
494 */
495void DexLayout::DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation) {
496 fputs(annotation->GetType()->GetStringId()->Data(), out_file_);
497 // Display all name=value pairs.
498 for (auto& subannotation : *annotation->GetAnnotationElements()) {
499 fputc(' ', out_file_);
500 fputs(subannotation->GetName()->Data(), out_file_);
501 fputc('=', out_file_);
502 DumpEncodedValue(subannotation->GetValue());
503 }
504}
505/*
506 * Dumps encoded value.
507 */
508void DexLayout::DumpEncodedValue(const dex_ir::EncodedValue* data) {
509 switch (data->Type()) {
510 case DexFile::kDexAnnotationByte:
511 fprintf(out_file_, "%" PRId8, data->GetByte());
512 break;
513 case DexFile::kDexAnnotationShort:
514 fprintf(out_file_, "%" PRId16, data->GetShort());
515 break;
516 case DexFile::kDexAnnotationChar:
517 fprintf(out_file_, "%" PRIu16, data->GetChar());
518 break;
519 case DexFile::kDexAnnotationInt:
520 fprintf(out_file_, "%" PRId32, data->GetInt());
521 break;
522 case DexFile::kDexAnnotationLong:
523 fprintf(out_file_, "%" PRId64, data->GetLong());
524 break;
525 case DexFile::kDexAnnotationFloat: {
526 fprintf(out_file_, "%g", data->GetFloat());
527 break;
528 }
529 case DexFile::kDexAnnotationDouble: {
530 fprintf(out_file_, "%g", data->GetDouble());
531 break;
532 }
533 case DexFile::kDexAnnotationString: {
534 dex_ir::StringId* string_id = data->GetStringId();
535 if (options_.output_format_ == kOutputPlain) {
536 DumpEscapedString(string_id->Data(), out_file_);
537 } else {
538 DumpXmlAttribute(string_id->Data(), out_file_);
539 }
540 break;
541 }
542 case DexFile::kDexAnnotationType: {
543 dex_ir::TypeId* type_id = data->GetTypeId();
544 fputs(type_id->GetStringId()->Data(), out_file_);
545 break;
546 }
547 case DexFile::kDexAnnotationField:
548 case DexFile::kDexAnnotationEnum: {
549 dex_ir::FieldId* field_id = data->GetFieldId();
550 fputs(field_id->Name()->Data(), out_file_);
551 break;
552 }
553 case DexFile::kDexAnnotationMethod: {
554 dex_ir::MethodId* method_id = data->GetMethodId();
555 fputs(method_id->Name()->Data(), out_file_);
556 break;
557 }
558 case DexFile::kDexAnnotationArray: {
559 fputc('{', out_file_);
560 // Display all elements.
561 for (auto& value : *data->GetEncodedArray()->GetEncodedValues()) {
562 fputc(' ', out_file_);
563 DumpEncodedValue(value.get());
564 }
565 fputs(" }", out_file_);
566 break;
567 }
568 case DexFile::kDexAnnotationAnnotation: {
569 DumpEncodedAnnotation(data->GetEncodedAnnotation());
570 break;
571 }
572 case DexFile::kDexAnnotationNull:
573 fputs("null", out_file_);
574 break;
575 case DexFile::kDexAnnotationBoolean:
576 fputs(StrBool(data->GetBoolean()), out_file_);
577 break;
578 default:
579 fputs("????", out_file_);
580 break;
581 } // switch
582}
583
584/*
585 * Dumps the file header.
586 */
587void DexLayout::DumpFileHeader() {
588 char sanitized[8 * 2 + 1];
589 dex_ir::Collections& collections = header_->GetCollections();
590 fprintf(out_file_, "DEX file header:\n");
591 Asciify(sanitized, header_->Magic(), 8);
592 fprintf(out_file_, "magic : '%s'\n", sanitized);
593 fprintf(out_file_, "checksum : %08x\n", header_->Checksum());
594 fprintf(out_file_, "signature : %02x%02x...%02x%02x\n",
595 header_->Signature()[0], header_->Signature()[1],
596 header_->Signature()[DexFile::kSha1DigestSize - 2],
597 header_->Signature()[DexFile::kSha1DigestSize - 1]);
598 fprintf(out_file_, "file_size : %d\n", header_->FileSize());
599 fprintf(out_file_, "header_size : %d\n", header_->HeaderSize());
600 fprintf(out_file_, "link_size : %d\n", header_->LinkSize());
601 fprintf(out_file_, "link_off : %d (0x%06x)\n",
602 header_->LinkOffset(), header_->LinkOffset());
603 fprintf(out_file_, "string_ids_size : %d\n", collections.StringIdsSize());
604 fprintf(out_file_, "string_ids_off : %d (0x%06x)\n",
605 collections.StringIdsOffset(), collections.StringIdsOffset());
606 fprintf(out_file_, "type_ids_size : %d\n", collections.TypeIdsSize());
607 fprintf(out_file_, "type_ids_off : %d (0x%06x)\n",
608 collections.TypeIdsOffset(), collections.TypeIdsOffset());
609 fprintf(out_file_, "proto_ids_size : %d\n", collections.ProtoIdsSize());
610 fprintf(out_file_, "proto_ids_off : %d (0x%06x)\n",
611 collections.ProtoIdsOffset(), collections.ProtoIdsOffset());
612 fprintf(out_file_, "field_ids_size : %d\n", collections.FieldIdsSize());
613 fprintf(out_file_, "field_ids_off : %d (0x%06x)\n",
614 collections.FieldIdsOffset(), collections.FieldIdsOffset());
615 fprintf(out_file_, "method_ids_size : %d\n", collections.MethodIdsSize());
616 fprintf(out_file_, "method_ids_off : %d (0x%06x)\n",
617 collections.MethodIdsOffset(), collections.MethodIdsOffset());
618 fprintf(out_file_, "class_defs_size : %d\n", collections.ClassDefsSize());
619 fprintf(out_file_, "class_defs_off : %d (0x%06x)\n",
620 collections.ClassDefsOffset(), collections.ClassDefsOffset());
621 fprintf(out_file_, "data_size : %d\n", header_->DataSize());
622 fprintf(out_file_, "data_off : %d (0x%06x)\n\n",
623 header_->DataOffset(), header_->DataOffset());
624}
625
626/*
627 * Dumps a class_def_item.
628 */
629void DexLayout::DumpClassDef(int idx) {
630 // General class information.
631 dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx);
632 fprintf(out_file_, "Class #%d header:\n", idx);
633 fprintf(out_file_, "class_idx : %d\n", class_def->ClassType()->GetIndex());
634 fprintf(out_file_, "access_flags : %d (0x%04x)\n",
635 class_def->GetAccessFlags(), class_def->GetAccessFlags());
636 uint32_t superclass_idx = class_def->Superclass() == nullptr ?
637 DexFile::kDexNoIndex16 : class_def->Superclass()->GetIndex();
638 fprintf(out_file_, "superclass_idx : %d\n", superclass_idx);
639 fprintf(out_file_, "interfaces_off : %d (0x%06x)\n",
640 class_def->InterfacesOffset(), class_def->InterfacesOffset());
641 uint32_t source_file_offset = 0xffffffffU;
642 if (class_def->SourceFile() != nullptr) {
643 source_file_offset = class_def->SourceFile()->GetIndex();
644 }
645 fprintf(out_file_, "source_file_idx : %d\n", source_file_offset);
646 uint32_t annotations_offset = 0;
647 if (class_def->Annotations() != nullptr) {
648 annotations_offset = class_def->Annotations()->GetOffset();
649 }
650 fprintf(out_file_, "annotations_off : %d (0x%06x)\n",
651 annotations_offset, annotations_offset);
652 if (class_def->GetClassData() == nullptr) {
653 fprintf(out_file_, "class_data_off : %d (0x%06x)\n", 0, 0);
654 } else {
655 fprintf(out_file_, "class_data_off : %d (0x%06x)\n",
656 class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset());
657 }
658
659 // Fields and methods.
660 dex_ir::ClassData* class_data = class_def->GetClassData();
661 if (class_data != nullptr && class_data->StaticFields() != nullptr) {
662 fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields()->size());
663 } else {
664 fprintf(out_file_, "static_fields_size : 0\n");
665 }
666 if (class_data != nullptr && class_data->InstanceFields() != nullptr) {
667 fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields()->size());
668 } else {
669 fprintf(out_file_, "instance_fields_size: 0\n");
670 }
671 if (class_data != nullptr && class_data->DirectMethods() != nullptr) {
672 fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods()->size());
673 } else {
674 fprintf(out_file_, "direct_methods_size : 0\n");
675 }
676 if (class_data != nullptr && class_data->VirtualMethods() != nullptr) {
677 fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods()->size());
678 } else {
679 fprintf(out_file_, "virtual_methods_size: 0\n");
680 }
681 fprintf(out_file_, "\n");
682}
683
684/**
685 * Dumps an annotation set item.
686 */
687void DexLayout::DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) {
688 if (set_item == nullptr || set_item->GetItems()->size() == 0) {
689 fputs(" empty-annotation-set\n", out_file_);
690 return;
691 }
692 for (dex_ir::AnnotationItem* annotation : *set_item->GetItems()) {
693 if (annotation == nullptr) {
694 continue;
695 }
696 fputs(" ", out_file_);
697 switch (annotation->GetVisibility()) {
698 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break;
699 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break;
700 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break;
701 default: fputs("VISIBILITY_UNKNOWN ", out_file_); break;
702 } // switch
703 DumpEncodedAnnotation(annotation->GetAnnotation());
704 fputc('\n', out_file_);
705 }
706}
707
708/*
709 * Dumps class annotations.
710 */
711void DexLayout::DumpClassAnnotations(int idx) {
712 dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx);
713 dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations();
714 if (annotations_directory == nullptr) {
715 return; // none
716 }
717
718 fprintf(out_file_, "Class #%d annotations:\n", idx);
719
720 dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation();
721 dex_ir::FieldAnnotationVector* fields = annotations_directory->GetFieldAnnotations();
722 dex_ir::MethodAnnotationVector* methods = annotations_directory->GetMethodAnnotations();
723 dex_ir::ParameterAnnotationVector* parameters = annotations_directory->GetParameterAnnotations();
724
725 // Annotations on the class itself.
726 if (class_set_item != nullptr) {
727 fprintf(out_file_, "Annotations on class\n");
728 DumpAnnotationSetItem(class_set_item);
729 }
730
731 // Annotations on fields.
732 if (fields != nullptr) {
733 for (auto& field : *fields) {
734 const dex_ir::FieldId* field_id = field->GetFieldId();
735 const uint32_t field_idx = field_id->GetIndex();
736 const char* field_name = field_id->Name()->Data();
737 fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name);
738 DumpAnnotationSetItem(field->GetAnnotationSetItem());
739 }
740 }
741
742 // Annotations on methods.
743 if (methods != nullptr) {
744 for (auto& method : *methods) {
745 const dex_ir::MethodId* method_id = method->GetMethodId();
746 const uint32_t method_idx = method_id->GetIndex();
747 const char* method_name = method_id->Name()->Data();
748 fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name);
749 DumpAnnotationSetItem(method->GetAnnotationSetItem());
750 }
751 }
752
753 // Annotations on method parameters.
754 if (parameters != nullptr) {
755 for (auto& parameter : *parameters) {
756 const dex_ir::MethodId* method_id = parameter->GetMethodId();
757 const uint32_t method_idx = method_id->GetIndex();
758 const char* method_name = method_id->Name()->Data();
759 fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
760 uint32_t j = 0;
761 for (dex_ir::AnnotationSetItem* annotation : *parameter->GetAnnotations()->GetItems()) {
762 fprintf(out_file_, "#%u\n", j);
763 DumpAnnotationSetItem(annotation);
764 ++j;
765 }
766 }
767 }
768
769 fputc('\n', out_file_);
770}
771
772/*
773 * Dumps an interface that a class declares to implement.
774 */
775void DexLayout::DumpInterface(const dex_ir::TypeId* type_item, int i) {
776 const char* interface_name = type_item->GetStringId()->Data();
777 if (options_.output_format_ == kOutputPlain) {
778 fprintf(out_file_, " #%d : '%s'\n", i, interface_name);
779 } else {
780 std::string dot(DescriptorToDotWrapper(interface_name));
781 fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str());
782 }
783}
784
785/*
786 * Dumps the catches table associated with the code.
787 */
788void DexLayout::DumpCatches(const dex_ir::CodeItem* code) {
789 const uint16_t tries_size = code->TriesSize();
790
791 // No catch table.
792 if (tries_size == 0) {
793 fprintf(out_file_, " catches : (none)\n");
794 return;
795 }
796
797 // Dump all table entries.
798 fprintf(out_file_, " catches : %d\n", tries_size);
799 std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries();
800 for (uint32_t i = 0; i < tries_size; i++) {
801 const dex_ir::TryItem* try_item = (*tries)[i].get();
802 const uint32_t start = try_item->StartAddr();
803 const uint32_t end = start + try_item->InsnCount();
804 fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end);
805 for (auto& handler : *try_item->GetHandlers()->GetHandlers()) {
806 const dex_ir::TypeId* type_id = handler->GetTypeId();
807 const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data();
808 fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress());
809 } // for
810 } // for
811}
812
813/*
814 * Dumps all positions table entries associated with the code.
815 */
816void DexLayout::DumpPositionInfo(const dex_ir::CodeItem* code) {
817 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
818 if (debug_info == nullptr) {
819 return;
820 }
821 std::vector<std::unique_ptr<dex_ir::PositionInfo>>& positions = debug_info->GetPositionInfo();
822 for (size_t i = 0; i < positions.size(); ++i) {
823 fprintf(out_file_, " 0x%04x line=%d\n", positions[i]->address_, positions[i]->line_);
824 }
825}
826
827/*
828 * Dumps all locals table entries associated with the code.
829 */
830void DexLayout::DumpLocalInfo(const dex_ir::CodeItem* code) {
831 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
832 if (debug_info == nullptr) {
833 return;
834 }
835 std::vector<std::unique_ptr<dex_ir::LocalInfo>>& locals = debug_info->GetLocalInfo();
836 for (size_t i = 0; i < locals.size(); ++i) {
837 dex_ir::LocalInfo* entry = locals[i].get();
838 fprintf(out_file_, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
839 entry->start_address_, entry->end_address_, entry->reg_,
840 entry->name_.c_str(), entry->descriptor_.c_str(), entry->signature_.c_str());
841 }
842}
843
844/*
David Sehr7629f602016-08-07 16:01:51 -0700845 * Dumps a single instruction.
846 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800847void DexLayout::DumpInstruction(const dex_ir::CodeItem* code,
848 uint32_t code_offset,
849 uint32_t insn_idx,
850 uint32_t insn_width,
851 const Instruction* dec_insn) {
David Sehr7629f602016-08-07 16:01:51 -0700852 // Address of instruction (expressed as byte offset).
853 fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2);
854
855 // Dump (part of) raw bytes.
856 const uint16_t* insns = code->Insns();
857 for (uint32_t i = 0; i < 8; i++) {
858 if (i < insn_width) {
859 if (i == 7) {
860 fprintf(out_file_, " ... ");
861 } else {
862 // Print 16-bit value in little-endian order.
863 const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i];
864 fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]);
865 }
866 } else {
867 fputs(" ", out_file_);
868 }
869 } // for
870
871 // Dump pseudo-instruction or opcode.
872 if (dec_insn->Opcode() == Instruction::NOP) {
873 const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]);
874 if (instr == Instruction::kPackedSwitchSignature) {
875 fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width);
876 } else if (instr == Instruction::kSparseSwitchSignature) {
877 fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width);
878 } else if (instr == Instruction::kArrayDataSignature) {
879 fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width);
880 } else {
881 fprintf(out_file_, "|%04x: nop // spacer", insn_idx);
882 }
883 } else {
884 fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name());
885 }
886
887 // Set up additional argument.
888 std::unique_ptr<char[]> index_buf;
889 if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800890 index_buf = IndexString(header_, dec_insn, 200);
David Sehr7629f602016-08-07 16:01:51 -0700891 }
892
893 // Dump the instruction.
894 //
895 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
896 //
897 switch (Instruction::FormatOf(dec_insn->Opcode())) {
898 case Instruction::k10x: // op
899 break;
900 case Instruction::k12x: // op vA, vB
901 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
902 break;
903 case Instruction::k11n: // op vA, #+B
904 fprintf(out_file_, " v%d, #int %d // #%x",
905 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB());
906 break;
907 case Instruction::k11x: // op vAA
908 fprintf(out_file_, " v%d", dec_insn->VRegA());
909 break;
910 case Instruction::k10t: // op +AA
911 case Instruction::k20t: { // op +AAAA
912 const int32_t targ = (int32_t) dec_insn->VRegA();
913 fprintf(out_file_, " %04x // %c%04x",
914 insn_idx + targ,
915 (targ < 0) ? '-' : '+',
916 (targ < 0) ? -targ : targ);
917 break;
918 }
919 case Instruction::k22x: // op vAA, vBBBB
920 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
921 break;
922 case Instruction::k21t: { // op vAA, +BBBB
923 const int32_t targ = (int32_t) dec_insn->VRegB();
924 fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(),
925 insn_idx + targ,
926 (targ < 0) ? '-' : '+',
927 (targ < 0) ? -targ : targ);
928 break;
929 }
930 case Instruction::k21s: // op vAA, #+BBBB
931 fprintf(out_file_, " v%d, #int %d // #%x",
932 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB());
933 break;
934 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
935 // The printed format varies a bit based on the actual opcode.
936 if (dec_insn->Opcode() == Instruction::CONST_HIGH16) {
937 const int32_t value = dec_insn->VRegB() << 16;
938 fprintf(out_file_, " v%d, #int %d // #%x",
939 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
940 } else {
941 const int64_t value = ((int64_t) dec_insn->VRegB()) << 48;
942 fprintf(out_file_, " v%d, #long %" PRId64 " // #%x",
943 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
944 }
945 break;
946 case Instruction::k21c: // op vAA, thing@BBBB
947 case Instruction::k31c: // op vAA, thing@BBBBBBBB
948 fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get());
949 break;
950 case Instruction::k23x: // op vAA, vBB, vCC
951 fprintf(out_file_, " v%d, v%d, v%d",
952 dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC());
953 break;
954 case Instruction::k22b: // op vAA, vBB, #+CC
955 fprintf(out_file_, " v%d, v%d, #int %d // #%02x",
956 dec_insn->VRegA(), dec_insn->VRegB(),
957 (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC());
958 break;
959 case Instruction::k22t: { // op vA, vB, +CCCC
960 const int32_t targ = (int32_t) dec_insn->VRegC();
961 fprintf(out_file_, " v%d, v%d, %04x // %c%04x",
962 dec_insn->VRegA(), dec_insn->VRegB(),
963 insn_idx + targ,
964 (targ < 0) ? '-' : '+',
965 (targ < 0) ? -targ : targ);
966 break;
967 }
968 case Instruction::k22s: // op vA, vB, #+CCCC
969 fprintf(out_file_, " v%d, v%d, #int %d // #%04x",
970 dec_insn->VRegA(), dec_insn->VRegB(),
971 (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC());
972 break;
973 case Instruction::k22c: // op vA, vB, thing@CCCC
974 // NOT SUPPORTED:
975 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
976 fprintf(out_file_, " v%d, v%d, %s",
977 dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get());
978 break;
979 case Instruction::k30t:
980 fprintf(out_file_, " #%08x", dec_insn->VRegA());
981 break;
982 case Instruction::k31i: { // op vAA, #+BBBBBBBB
983 // This is often, but not always, a float.
984 union {
985 float f;
986 uint32_t i;
987 } conv;
988 conv.i = dec_insn->VRegB();
989 fprintf(out_file_, " v%d, #float %g // #%08x",
990 dec_insn->VRegA(), conv.f, dec_insn->VRegB());
991 break;
992 }
993 case Instruction::k31t: // op vAA, offset +BBBBBBBB
994 fprintf(out_file_, " v%d, %08x // +%08x",
995 dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB());
996 break;
997 case Instruction::k32x: // op vAAAA, vBBBB
998 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
999 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +01001000 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
1001 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -07001002 // NOT SUPPORTED:
1003 // case Instruction::k35ms: // [opt] invoke-virtual+super
1004 // case Instruction::k35mi: // [opt] inline invoke
1005 uint32_t arg[Instruction::kMaxVarArgRegs];
1006 dec_insn->GetVarArgs(arg);
1007 fputs(" {", out_file_);
1008 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
1009 if (i == 0) {
1010 fprintf(out_file_, "v%d", arg[i]);
1011 } else {
1012 fprintf(out_file_, ", v%d", arg[i]);
1013 }
1014 } // for
1015 fprintf(out_file_, "}, %s", index_buf.get());
1016 break;
1017 }
Orion Hodsonb34bb192016-10-18 17:02:58 +01001018 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
1019 case Instruction::k4rcc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -07001020 // NOT SUPPORTED:
1021 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
1022 // case Instruction::k3rmi: // [opt] execute-inline/range
1023 {
1024 // This doesn't match the "dx" output when some of the args are
1025 // 64-bit values -- dx only shows the first register.
1026 fputs(" {", out_file_);
1027 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
1028 if (i == 0) {
1029 fprintf(out_file_, "v%d", dec_insn->VRegC() + i);
1030 } else {
1031 fprintf(out_file_, ", v%d", dec_insn->VRegC() + i);
1032 }
1033 } // for
1034 fprintf(out_file_, "}, %s", index_buf.get());
1035 }
1036 break;
1037 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1038 // This is often, but not always, a double.
1039 union {
1040 double d;
1041 uint64_t j;
1042 } conv;
1043 conv.j = dec_insn->WideVRegB();
1044 fprintf(out_file_, " v%d, #double %g // #%016" PRIx64,
1045 dec_insn->VRegA(), conv.d, dec_insn->WideVRegB());
1046 break;
1047 }
1048 // NOT SUPPORTED:
1049 // case Instruction::k00x: // unknown op or breakpoint
1050 // break;
1051 default:
1052 fprintf(out_file_, " ???");
1053 break;
1054 } // switch
1055
1056 fputc('\n', out_file_);
1057}
1058
1059/*
1060 * Dumps a bytecode disassembly.
1061 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001062void DexLayout::DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) {
1063 dex_ir::MethodId* method_id = header_->GetCollections().GetMethodId(idx);
David Sehr7629f602016-08-07 16:01:51 -07001064 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -07001065 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001066 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1067
1068 // Generate header.
Jeff Haoc3acfc52016-08-29 14:18:26 -07001069 std::string dot(DescriptorToDotWrapper(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001070 fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n",
David Sehr72359222016-09-07 13:04:01 -07001071 code_offset, code_offset, dot.c_str(), name, type_descriptor.c_str());
David Sehr7629f602016-08-07 16:01:51 -07001072
1073 // Iterate over all instructions.
1074 const uint16_t* insns = code->Insns();
1075 for (uint32_t insn_idx = 0; insn_idx < code->InsnsSize();) {
1076 const Instruction* instruction = Instruction::At(&insns[insn_idx]);
1077 const uint32_t insn_width = instruction->SizeInCodeUnits();
1078 if (insn_width == 0) {
1079 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insn_idx);
1080 break;
1081 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001082 DumpInstruction(code, code_offset, insn_idx, insn_width, instruction);
David Sehr7629f602016-08-07 16:01:51 -07001083 insn_idx += insn_width;
1084 } // for
1085}
1086
1087/*
1088 * Dumps code of a method.
1089 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001090void DexLayout::DumpCode(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) {
David Sehr7629f602016-08-07 16:01:51 -07001091 fprintf(out_file_, " registers : %d\n", code->RegistersSize());
1092 fprintf(out_file_, " ins : %d\n", code->InsSize());
1093 fprintf(out_file_, " outs : %d\n", code->OutsSize());
1094 fprintf(out_file_, " insns size : %d 16-bit code units\n",
1095 code->InsnsSize());
1096
1097 // Bytecode disassembly, if requested.
1098 if (options_.disassemble_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001099 DumpBytecodes(idx, code, code_offset);
David Sehr7629f602016-08-07 16:01:51 -07001100 }
1101
1102 // Try-catch blocks.
1103 DumpCatches(code);
1104
1105 // Positions and locals table in the debug info.
1106 fprintf(out_file_, " positions : \n");
1107 DumpPositionInfo(code);
1108 fprintf(out_file_, " locals : \n");
1109 DumpLocalInfo(code);
1110}
1111
1112/*
1113 * Dumps a method.
1114 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001115void DexLayout::DumpMethod(uint32_t idx, uint32_t flags, const dex_ir::CodeItem* code, int i) {
David Sehr7629f602016-08-07 16:01:51 -07001116 // Bail for anything private if export only requested.
1117 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1118 return;
1119 }
1120
Jeff Haoea7c6292016-11-14 18:10:16 -08001121 dex_ir::MethodId* method_id = header_->GetCollections().GetMethodId(idx);
David Sehr7629f602016-08-07 16:01:51 -07001122 const char* name = method_id->Name()->Data();
1123 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1124 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1125 char* access_str = CreateAccessFlagStr(flags, kAccessForMethod);
1126
1127 if (options_.output_format_ == kOutputPlain) {
1128 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1129 fprintf(out_file_, " name : '%s'\n", name);
1130 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1131 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1132 if (code == nullptr) {
1133 fprintf(out_file_, " code : (none)\n");
1134 } else {
1135 fprintf(out_file_, " code -\n");
Jeff Haoea7c6292016-11-14 18:10:16 -08001136 DumpCode(idx, code, code->GetOffset());
David Sehr7629f602016-08-07 16:01:51 -07001137 }
1138 if (options_.disassemble_) {
1139 fputc('\n', out_file_);
1140 }
1141 } else if (options_.output_format_ == kOutputXml) {
1142 const bool constructor = (name[0] == '<');
1143
1144 // Method name and prototype.
1145 if (constructor) {
1146 std::string dot(DescriptorClassToDot(back_descriptor));
1147 fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str());
Jeff Haoc3acfc52016-08-29 14:18:26 -07001148 dot = DescriptorToDotWrapper(back_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001149 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1150 } else {
1151 fprintf(out_file_, "<method name=\"%s\"\n", name);
1152 const char* return_type = strrchr(type_descriptor, ')');
1153 if (return_type == nullptr) {
1154 fprintf(stderr, "bad method type descriptor '%s'\n", type_descriptor);
1155 goto bail;
1156 }
Jeff Haoc3acfc52016-08-29 14:18:26 -07001157 std::string dot(DescriptorToDotWrapper(return_type + 1));
David Sehr7629f602016-08-07 16:01:51 -07001158 fprintf(out_file_, " return=\"%s\"\n", dot.c_str());
1159 fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0));
1160 fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0));
1161 fprintf(out_file_, " synchronized=%s\n", QuotedBool(
1162 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1163 }
1164
1165 // Additional method flags.
1166 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1167 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1168 // The "deprecated=" not knowable w/o parsing annotations.
1169 fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags));
1170
1171 // Parameters.
1172 if (type_descriptor[0] != '(') {
1173 fprintf(stderr, "ERROR: bad descriptor '%s'\n", type_descriptor);
1174 goto bail;
1175 }
1176 char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1));
1177 const char* base = type_descriptor + 1;
1178 int arg_num = 0;
1179 while (*base != ')') {
1180 char* cp = tmp_buf;
1181 while (*base == '[') {
1182 *cp++ = *base++;
1183 }
1184 if (*base == 'L') {
1185 // Copy through ';'.
1186 do {
1187 *cp = *base++;
1188 } while (*cp++ != ';');
1189 } else {
1190 // Primitive char, copy it.
1191 if (strchr("ZBCSIFJD", *base) == nullptr) {
1192 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
1193 break; // while
1194 }
1195 *cp++ = *base++;
1196 }
1197 // Null terminate and display.
1198 *cp++ = '\0';
Jeff Haoc3acfc52016-08-29 14:18:26 -07001199 std::string dot(DescriptorToDotWrapper(tmp_buf));
David Sehr7629f602016-08-07 16:01:51 -07001200 fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n"
1201 "</parameter>\n", arg_num++, dot.c_str());
1202 } // while
1203 free(tmp_buf);
1204 if (constructor) {
1205 fprintf(out_file_, "</constructor>\n");
1206 } else {
1207 fprintf(out_file_, "</method>\n");
1208 }
1209 }
1210
1211 bail:
1212 free(type_descriptor);
1213 free(access_str);
1214}
1215
1216/*
1217 * Dumps a static (class) field.
1218 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001219void DexLayout::DumpSField(uint32_t idx, uint32_t flags, int i, dex_ir::EncodedValue* init) {
David Sehr7629f602016-08-07 16:01:51 -07001220 // Bail for anything private if export only requested.
1221 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1222 return;
1223 }
1224
Jeff Haoea7c6292016-11-14 18:10:16 -08001225 dex_ir::FieldId* field_id = header_->GetCollections().GetFieldId(idx);
David Sehr7629f602016-08-07 16:01:51 -07001226 const char* name = field_id->Name()->Data();
1227 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
1228 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
1229 char* access_str = CreateAccessFlagStr(flags, kAccessForField);
1230
1231 if (options_.output_format_ == kOutputPlain) {
1232 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1233 fprintf(out_file_, " name : '%s'\n", name);
1234 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1235 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1236 if (init != nullptr) {
1237 fputs(" value : ", out_file_);
1238 DumpEncodedValue(init);
1239 fputs("\n", out_file_);
1240 }
1241 } else if (options_.output_format_ == kOutputXml) {
1242 fprintf(out_file_, "<field name=\"%s\"\n", name);
Jeff Haoc3acfc52016-08-29 14:18:26 -07001243 std::string dot(DescriptorToDotWrapper(type_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001244 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1245 fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0));
1246 fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0));
1247 // The "value=" is not knowable w/o parsing annotations.
1248 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1249 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1250 // The "deprecated=" is not knowable w/o parsing annotations.
1251 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags));
1252 if (init != nullptr) {
1253 fputs(" value=\"", out_file_);
1254 DumpEncodedValue(init);
1255 fputs("\"\n", out_file_);
1256 }
1257 fputs(">\n</field>\n", out_file_);
1258 }
1259
1260 free(access_str);
1261}
1262
1263/*
1264 * Dumps an instance field.
1265 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001266void DexLayout::DumpIField(uint32_t idx, uint32_t flags, int i) {
1267 DumpSField(idx, flags, i, nullptr);
David Sehr7629f602016-08-07 16:01:51 -07001268}
1269
1270/*
David Sehr7629f602016-08-07 16:01:51 -07001271 * Dumps the class.
1272 *
1273 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1274 *
1275 * If "*last_package" is nullptr or does not match the current class' package,
1276 * the value will be replaced with a newly-allocated string.
1277 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001278void DexLayout::DumpClass(int idx, char** last_package) {
1279 dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx);
David Sehr7629f602016-08-07 16:01:51 -07001280 // Omitting non-public class.
1281 if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) {
1282 return;
1283 }
1284
1285 if (options_.show_section_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001286 DumpClassDef(idx);
David Sehr7629f602016-08-07 16:01:51 -07001287 }
1288
1289 if (options_.show_annotations_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001290 DumpClassAnnotations(idx);
David Sehr7629f602016-08-07 16:01:51 -07001291 }
1292
David Sehr7629f602016-08-07 16:01:51 -07001293 // For the XML output, show the package name. Ideally we'd gather
1294 // up the classes, sort them, and dump them alphabetically so the
1295 // package name wouldn't jump around, but that's not a great plan
1296 // for something that needs to run on the device.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001297 const char* class_descriptor =
Jeff Haoea7c6292016-11-14 18:10:16 -08001298 header_->GetCollections().GetClassDef(idx)->ClassType()->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -07001299 if (!(class_descriptor[0] == 'L' &&
1300 class_descriptor[strlen(class_descriptor)-1] == ';')) {
1301 // Arrays and primitives should not be defined explicitly. Keep going?
1302 fprintf(stderr, "Malformed class name '%s'\n", class_descriptor);
1303 } else if (options_.output_format_ == kOutputXml) {
1304 char* mangle = strdup(class_descriptor + 1);
1305 mangle[strlen(mangle)-1] = '\0';
1306
1307 // Reduce to just the package name.
1308 char* last_slash = strrchr(mangle, '/');
1309 if (last_slash != nullptr) {
1310 *last_slash = '\0';
1311 } else {
1312 *mangle = '\0';
1313 }
1314
1315 for (char* cp = mangle; *cp != '\0'; cp++) {
1316 if (*cp == '/') {
1317 *cp = '.';
1318 }
1319 } // for
1320
1321 if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) {
1322 // Start of a new package.
1323 if (*last_package != nullptr) {
1324 fprintf(out_file_, "</package>\n");
1325 }
1326 fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle);
1327 free(*last_package);
1328 *last_package = mangle;
1329 } else {
1330 free(mangle);
1331 }
1332 }
1333
1334 // General class information.
1335 char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass);
1336 const char* superclass_descriptor = nullptr;
1337 if (class_def->Superclass() != nullptr) {
1338 superclass_descriptor = class_def->Superclass()->GetStringId()->Data();
1339 }
1340 if (options_.output_format_ == kOutputPlain) {
1341 fprintf(out_file_, "Class #%d -\n", idx);
1342 fprintf(out_file_, " Class descriptor : '%s'\n", class_descriptor);
1343 fprintf(out_file_, " Access flags : 0x%04x (%s)\n",
1344 class_def->GetAccessFlags(), access_str);
1345 if (superclass_descriptor != nullptr) {
1346 fprintf(out_file_, " Superclass : '%s'\n", superclass_descriptor);
1347 }
1348 fprintf(out_file_, " Interfaces -\n");
1349 } else {
1350 std::string dot(DescriptorClassToDot(class_descriptor));
1351 fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str());
1352 if (superclass_descriptor != nullptr) {
Jeff Haoc3acfc52016-08-29 14:18:26 -07001353 dot = DescriptorToDotWrapper(superclass_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001354 fprintf(out_file_, " extends=\"%s\"\n", dot.c_str());
1355 }
1356 fprintf(out_file_, " interface=%s\n",
1357 QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0));
1358 fprintf(out_file_, " abstract=%s\n",
1359 QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0));
1360 fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0));
1361 fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0));
1362 // The "deprecated=" not knowable w/o parsing annotations.
1363 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags()));
1364 fprintf(out_file_, ">\n");
1365 }
1366
1367 // Interfaces.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001368 const dex_ir::TypeIdVector* interfaces = class_def->Interfaces();
David Sehr853a8e12016-09-01 13:03:50 -07001369 if (interfaces != nullptr) {
1370 for (uint32_t i = 0; i < interfaces->size(); i++) {
1371 DumpInterface((*interfaces)[i], i);
1372 } // for
1373 }
David Sehr7629f602016-08-07 16:01:51 -07001374
1375 // Fields and methods.
1376 dex_ir::ClassData* class_data = class_def->GetClassData();
1377 // Prepare data for static fields.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001378 dex_ir::EncodedArrayItem* static_values = class_def->StaticValues();
1379 dex_ir::EncodedValueVector* encoded_values =
1380 static_values == nullptr ? nullptr : static_values->GetEncodedValues();
1381 const uint32_t encoded_values_size = (encoded_values == nullptr) ? 0 : encoded_values->size();
David Sehr7629f602016-08-07 16:01:51 -07001382
1383 // Static fields.
1384 if (options_.output_format_ == kOutputPlain) {
1385 fprintf(out_file_, " Static fields -\n");
1386 }
David Sehr853a8e12016-09-01 13:03:50 -07001387 if (class_data != nullptr) {
1388 dex_ir::FieldItemVector* static_fields = class_data->StaticFields();
1389 if (static_fields != nullptr) {
1390 for (uint32_t i = 0; i < static_fields->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001391 DumpSField((*static_fields)[i]->GetFieldId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001392 (*static_fields)[i]->GetAccessFlags(),
1393 i,
Jeff Hao3ab96b42016-09-09 18:35:01 -07001394 i < encoded_values_size ? (*encoded_values)[i].get() : nullptr);
David Sehr853a8e12016-09-01 13:03:50 -07001395 } // for
1396 }
1397 }
David Sehr7629f602016-08-07 16:01:51 -07001398
1399 // Instance fields.
1400 if (options_.output_format_ == kOutputPlain) {
1401 fprintf(out_file_, " Instance fields -\n");
1402 }
David Sehr853a8e12016-09-01 13:03:50 -07001403 if (class_data != nullptr) {
1404 dex_ir::FieldItemVector* instance_fields = class_data->InstanceFields();
1405 if (instance_fields != nullptr) {
1406 for (uint32_t i = 0; i < instance_fields->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001407 DumpIField((*instance_fields)[i]->GetFieldId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001408 (*instance_fields)[i]->GetAccessFlags(),
1409 i);
1410 } // for
1411 }
1412 }
David Sehr7629f602016-08-07 16:01:51 -07001413
1414 // Direct methods.
1415 if (options_.output_format_ == kOutputPlain) {
1416 fprintf(out_file_, " Direct methods -\n");
1417 }
David Sehr853a8e12016-09-01 13:03:50 -07001418 if (class_data != nullptr) {
1419 dex_ir::MethodItemVector* direct_methods = class_data->DirectMethods();
1420 if (direct_methods != nullptr) {
1421 for (uint32_t i = 0; i < direct_methods->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001422 DumpMethod((*direct_methods)[i]->GetMethodId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001423 (*direct_methods)[i]->GetAccessFlags(),
1424 (*direct_methods)[i]->GetCodeItem(),
1425 i);
1426 } // for
1427 }
1428 }
David Sehr7629f602016-08-07 16:01:51 -07001429
1430 // Virtual methods.
1431 if (options_.output_format_ == kOutputPlain) {
1432 fprintf(out_file_, " Virtual methods -\n");
1433 }
David Sehr853a8e12016-09-01 13:03:50 -07001434 if (class_data != nullptr) {
1435 dex_ir::MethodItemVector* virtual_methods = class_data->VirtualMethods();
1436 if (virtual_methods != nullptr) {
1437 for (uint32_t i = 0; i < virtual_methods->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001438 DumpMethod((*virtual_methods)[i]->GetMethodId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001439 (*virtual_methods)[i]->GetAccessFlags(),
1440 (*virtual_methods)[i]->GetCodeItem(),
1441 i);
1442 } // for
1443 }
1444 }
David Sehr7629f602016-08-07 16:01:51 -07001445
1446 // End of class.
1447 if (options_.output_format_ == kOutputPlain) {
1448 const char* file_name = "unknown";
1449 if (class_def->SourceFile() != nullptr) {
1450 file_name = class_def->SourceFile()->Data();
1451 }
1452 const dex_ir::StringId* source_file = class_def->SourceFile();
1453 fprintf(out_file_, " source_file_idx : %d (%s)\n\n",
Jeff Hao3ab96b42016-09-09 18:35:01 -07001454 source_file == nullptr ? 0xffffffffU : source_file->GetIndex(), file_name);
David Sehr7629f602016-08-07 16:01:51 -07001455 } else if (options_.output_format_ == kOutputXml) {
1456 fprintf(out_file_, "</class>\n");
1457 }
1458
1459 free(access_str);
1460}
1461
Jeff Haoea7c6292016-11-14 18:10:16 -08001462void DexLayout::DumpDexFile() {
David Sehr7629f602016-08-07 16:01:51 -07001463 // Headers.
1464 if (options_.show_file_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001465 DumpFileHeader();
David Sehr7629f602016-08-07 16:01:51 -07001466 }
1467
1468 // Open XML context.
1469 if (options_.output_format_ == kOutputXml) {
1470 fprintf(out_file_, "<api>\n");
1471 }
1472
1473 // Iterate over all classes.
1474 char* package = nullptr;
Jeff Haoea7c6292016-11-14 18:10:16 -08001475 const uint32_t class_defs_size = header_->GetCollections().ClassDefsSize();
David Sehr7629f602016-08-07 16:01:51 -07001476 for (uint32_t i = 0; i < class_defs_size; i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001477 DumpClass(i, &package);
David Sehr7629f602016-08-07 16:01:51 -07001478 } // for
1479
1480 // Free the last package allocated.
1481 if (package != nullptr) {
1482 fprintf(out_file_, "</package>\n");
1483 free(package);
1484 }
1485
1486 // Close XML context.
1487 if (options_.output_format_ == kOutputXml) {
1488 fprintf(out_file_, "</api>\n");
1489 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001490}
Jeff Hao3ab96b42016-09-09 18:35:01 -07001491
Jeff Hao042e8982016-10-19 11:17:11 -07001492std::vector<dex_ir::ClassDef*> DexLayout::LayoutClassDefsAndClassData(const DexFile* dex_file) {
1493 std::vector<dex_ir::ClassDef*> new_class_def_order;
1494 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
1495 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1496 if (info_->ContainsClass(*dex_file, type_idx)) {
1497 new_class_def_order.push_back(class_def.get());
1498 }
1499 }
1500 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
1501 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1502 if (!info_->ContainsClass(*dex_file, type_idx)) {
1503 new_class_def_order.push_back(class_def.get());
1504 }
1505 }
1506 uint32_t class_defs_offset = header_->GetCollections().ClassDefsOffset();
1507 uint32_t class_data_offset = header_->GetCollections().ClassDatasOffset();
1508 for (uint32_t i = 0; i < new_class_def_order.size(); ++i) {
1509 dex_ir::ClassDef* class_def = new_class_def_order[i];
1510 class_def->SetIndex(i);
1511 class_def->SetOffset(class_defs_offset);
1512 class_defs_offset += dex_ir::ClassDef::ItemSize();
1513 if (class_def->GetClassData() != nullptr) {
1514 class_def->GetClassData()->SetOffset(class_data_offset);
1515 class_data_offset += class_def->GetClassData()->GetSize();
1516 }
1517 }
1518 return new_class_def_order;
1519}
1520
1521int32_t DexLayout::LayoutCodeItems(std::vector<dex_ir::ClassDef*> new_class_def_order) {
1522 int32_t diff = 0;
1523 uint32_t offset = header_->GetCollections().CodeItemsOffset();
1524 for (dex_ir::ClassDef* class_def : new_class_def_order) {
1525 dex_ir::ClassData* class_data = class_def->GetClassData();
1526 if (class_data != nullptr) {
1527 class_data->SetOffset(class_data->GetOffset() + diff);
1528 for (auto& method : *class_data->DirectMethods()) {
1529 dex_ir::CodeItem* code_item = method->GetCodeItem();
1530 if (code_item != nullptr) {
1531 diff += UnsignedLeb128Size(offset) - UnsignedLeb128Size(code_item->GetOffset());
1532 code_item->SetOffset(offset);
1533 offset += RoundUp(code_item->GetSize(), 4);
1534 }
1535 }
1536 for (auto& method : *class_data->VirtualMethods()) {
1537 dex_ir::CodeItem* code_item = method->GetCodeItem();
1538 if (code_item != nullptr) {
1539 diff += UnsignedLeb128Size(offset) - UnsignedLeb128Size(code_item->GetOffset());
1540 code_item->SetOffset(offset);
1541 offset += RoundUp(code_item->GetSize(), 4);
1542 }
1543 }
1544 }
1545 }
1546
1547 return diff;
1548}
1549
1550// Adjust offsets of every item in the specified section by diff bytes.
1551template<class T> void DexLayout::FixupSection(std::map<uint32_t, std::unique_ptr<T>>& map,
1552 uint32_t diff) {
1553 for (auto& pair : map) {
1554 std::unique_ptr<T>& item = pair.second;
1555 item->SetOffset(item->GetOffset() + diff);
1556 }
1557}
1558
1559// Adjust offsets of all sections with an address after the specified offset by diff bytes.
1560void DexLayout::FixupSections(uint32_t offset, uint32_t diff) {
1561 dex_ir::Collections& collections = header_->GetCollections();
1562 uint32_t map_list_offset = collections.MapListOffset();
1563 if (map_list_offset > offset) {
1564 collections.SetMapListOffset(map_list_offset + diff);
1565 }
1566
1567 uint32_t type_lists_offset = collections.TypeListsOffset();
1568 if (type_lists_offset > offset) {
1569 collections.SetTypeListsOffset(type_lists_offset + diff);
1570 FixupSection(collections.TypeLists(), diff);
1571 }
1572
1573 uint32_t annotation_set_ref_lists_offset = collections.AnnotationSetRefListsOffset();
1574 if (annotation_set_ref_lists_offset > offset) {
1575 collections.SetAnnotationSetRefListsOffset(annotation_set_ref_lists_offset + diff);
1576 FixupSection(collections.AnnotationSetRefLists(), diff);
1577 }
1578
1579 uint32_t annotation_set_items_offset = collections.AnnotationSetItemsOffset();
1580 if (annotation_set_items_offset > offset) {
1581 collections.SetAnnotationSetItemsOffset(annotation_set_items_offset + diff);
1582 FixupSection(collections.AnnotationSetItems(), diff);
1583 }
1584
1585 uint32_t class_datas_offset = collections.ClassDatasOffset();
1586 if (class_datas_offset > offset) {
1587 collections.SetClassDatasOffset(class_datas_offset + diff);
1588 FixupSection(collections.ClassDatas(), diff);
1589 }
1590
1591 uint32_t code_items_offset = collections.CodeItemsOffset();
1592 if (code_items_offset > offset) {
1593 collections.SetCodeItemsOffset(code_items_offset + diff);
1594 FixupSection(collections.CodeItems(), diff);
1595 }
1596
1597 uint32_t string_datas_offset = collections.StringDatasOffset();
1598 if (string_datas_offset > offset) {
1599 collections.SetStringDatasOffset(string_datas_offset + diff);
1600 FixupSection(collections.StringDatas(), diff);
1601 }
1602
1603 uint32_t debug_info_items_offset = collections.DebugInfoItemsOffset();
1604 if (debug_info_items_offset > offset) {
1605 collections.SetDebugInfoItemsOffset(debug_info_items_offset + diff);
1606 FixupSection(collections.DebugInfoItems(), diff);
1607 }
1608
1609 uint32_t annotation_items_offset = collections.AnnotationItemsOffset();
1610 if (annotation_items_offset > offset) {
1611 collections.SetAnnotationItemsOffset(annotation_items_offset + diff);
1612 FixupSection(collections.AnnotationItems(), diff);
1613 }
1614
1615 uint32_t encoded_array_items_offset = collections.EncodedArrayItemsOffset();
1616 if (encoded_array_items_offset > offset) {
1617 collections.SetEncodedArrayItemsOffset(encoded_array_items_offset + diff);
1618 FixupSection(collections.EncodedArrayItems(), diff);
1619 }
1620
1621 uint32_t annotations_directory_items_offset = collections.AnnotationsDirectoryItemsOffset();
1622 if (annotations_directory_items_offset > offset) {
1623 collections.SetAnnotationsDirectoryItemsOffset(annotations_directory_items_offset + diff);
1624 FixupSection(collections.AnnotationsDirectoryItems(), diff);
1625 }
1626}
1627
1628void DexLayout::LayoutOutputFile(const DexFile* dex_file) {
1629 std::vector<dex_ir::ClassDef*> new_class_def_order = LayoutClassDefsAndClassData(dex_file);
1630 int32_t diff = LayoutCodeItems(new_class_def_order);
1631 // Adjust diff to be 4-byte aligned.
1632 diff = RoundUp(diff, 4);
1633 // Move sections after ClassData by diff bytes.
1634 FixupSections(header_->GetCollections().ClassDatasOffset(), diff);
1635 // Update file size.
1636 header_->SetFileSize(header_->FileSize() + diff);
1637}
1638
Jeff Haoea7c6292016-11-14 18:10:16 -08001639void DexLayout::OutputDexFile(const std::string& dex_file_location) {
1640 std::string error_msg;
1641 std::unique_ptr<File> new_file;
1642 if (!options_.output_to_memmap_) {
Jeff Haoa8621002016-10-04 18:13:44 +00001643 std::string output_location(options_.output_dex_directory_);
Jeff Haoea7c6292016-11-14 18:10:16 -08001644 size_t last_slash = dex_file_location.rfind("/");
1645 std::string dex_file_directory = dex_file_location.substr(0, last_slash + 1);
1646 if (output_location == dex_file_directory) {
1647 output_location = dex_file_location + ".new";
1648 } else if (last_slash != std::string::npos) {
1649 output_location += dex_file_location.substr(last_slash);
1650 } else {
1651 output_location += "/" + dex_file_location + ".new";
1652 }
1653 new_file.reset(OS::CreateEmptyFile(output_location.c_str()));
1654 ftruncate(new_file->Fd(), header_->FileSize());
1655 mem_map_.reset(MemMap::MapFile(header_->FileSize(), PROT_READ | PROT_WRITE, MAP_SHARED,
1656 new_file->Fd(), 0, /*low_4gb*/ false, output_location.c_str(), &error_msg));
1657 } else {
1658 mem_map_.reset(MemMap::MapAnonymous("layout dex", nullptr, header_->FileSize(),
1659 PROT_READ | PROT_WRITE, /* low_4gb */ false, /* reuse */ false, &error_msg));
1660 }
1661 if (mem_map_ == nullptr) {
1662 LOG(ERROR) << "Could not create mem map for dex writer output: " << error_msg;
1663 if (new_file.get() != nullptr) {
1664 new_file->Erase();
1665 }
1666 return;
1667 }
1668 DexWriter::Output(header_, mem_map_.get());
1669 if (new_file != nullptr) {
1670 UNUSED(new_file->FlushCloseOrErase());
1671 }
1672}
1673
1674/*
1675 * Dumps the requested sections of the file.
1676 */
1677void DexLayout::ProcessDexFile(const char* file_name,
1678 const DexFile* dex_file,
1679 size_t dex_file_index) {
1680 std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file));
1681 SetHeader(header.get());
1682
1683 if (options_.verbose_) {
1684 fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n",
1685 file_name, dex_file->GetHeader().magic_ + 4);
1686 }
1687
1688 if (options_.visualize_pattern_) {
1689 VisualizeDexLayout(header_, dex_file, dex_file_index, info_);
1690 return;
1691 }
1692
1693 // Dump dex file.
1694 if (options_.dump_) {
1695 DumpDexFile();
1696 }
1697
1698 // Output dex file as file or memmap.
1699 if (options_.output_dex_directory_ != nullptr || options_.output_to_memmap_) {
Jeff Hao042e8982016-10-19 11:17:11 -07001700 if (info_ != nullptr) {
1701 LayoutOutputFile(dex_file);
1702 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001703 OutputDexFile(dex_file->GetLocation());
Jeff Hao3ab96b42016-09-09 18:35:01 -07001704 }
David Sehr7629f602016-08-07 16:01:51 -07001705}
1706
1707/*
1708 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1709 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001710int DexLayout::ProcessFile(const char* file_name) {
David Sehr7629f602016-08-07 16:01:51 -07001711 if (options_.verbose_) {
1712 fprintf(out_file_, "Processing '%s'...\n", file_name);
1713 }
1714
1715 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1716 // all of which are Zip archives with "classes.dex" inside.
1717 const bool verify_checksum = !options_.ignore_bad_checksum_;
1718 std::string error_msg;
1719 std::vector<std::unique_ptr<const DexFile>> dex_files;
1720 if (!DexFile::Open(file_name, file_name, verify_checksum, &error_msg, &dex_files)) {
1721 // Display returned error message to user. Note that this error behavior
1722 // differs from the error messages shown by the original Dalvik dexdump.
1723 fputs(error_msg.c_str(), stderr);
1724 fputc('\n', stderr);
1725 return -1;
1726 }
1727
1728 // Success. Either report checksum verification or process
1729 // all dex files found in given file.
1730 if (options_.checksum_only_) {
1731 fprintf(out_file_, "Checksum verified\n");
1732 } else {
1733 for (size_t i = 0; i < dex_files.size(); i++) {
David Sehrcdcfde72016-09-26 07:44:04 -07001734 ProcessDexFile(file_name, dex_files[i].get(), i);
David Sehr7629f602016-08-07 16:01:51 -07001735 }
1736 }
1737 return 0;
1738}
1739
1740} // namespace art