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