1 /*
2 * Copyright (C) 2008 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
17 /*
18 * The "dexdump" tool is intended to mimic "objdump". When possible, use
19 * similar command-line arguments.
20 *
21 * TODO: rework the "plain" output format to be more regexp-friendly
22 *
23 * Differences between XML output and the "current.xml" file:
24 * - classes in same package are not all grouped together; generally speaking
25 * nothing is sorted
26 * - no "deprecated" on fields and methods
27 * - no "value" on fields
28 * - no parameter names
29 * - no generic signatures on parameters, e.g. type="java.lang.Class<?>"
30 * - class shows declared fields and methods; does not show inherited fields
31 */
32
33 #include "libdex/DexFile.h"
34
35 #include "libdex/CmdUtils.h"
36 #include "libdex/DexCatch.h"
37 #include "libdex/DexClass.h"
38 #include "libdex/DexDebugInfo.h"
39 #include "libdex/DexOpcodes.h"
40 #include "libdex/DexProto.h"
41 #include "libdex/InstrUtils.h"
42 #include "libdex/SysUtil.h"
43
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <getopt.h>
50 #include <errno.h>
51 #include <assert.h>
52
53 static const char* gProgName = "dexdump";
54
55 enum OutputFormat {
56 OUTPUT_PLAIN = 0, /* default */
57 OUTPUT_XML, /* fancy */
58 };
59
60 /* command-line options */
61 struct Options {
62 bool checksumOnly;
63 bool disassemble;
64 bool showFileHeaders;
65 bool showSectionHeaders;
66 bool ignoreBadChecksum;
67 bool dumpRegisterMaps;
68 OutputFormat outputFormat;
69 const char* tempFileName;
70 bool exportsOnly;
71 bool verbose;
72 };
73
74 struct Options gOptions;
75
76 /* basic info about a field or method */
77 struct FieldMethodInfo {
78 const char* classDescriptor;
79 const char* name;
80 const char* signature;
81 };
82
83 /*
84 * Get 2 little-endian bytes.
85 */
get2LE(unsigned char const * pSrc)86 static inline u2 get2LE(unsigned char const* pSrc)
87 {
88 return pSrc[0] | (pSrc[1] << 8);
89 }
90
91 /*
92 * Get 4 little-endian bytes.
93 */
get4LE(unsigned char const * pSrc)94 static inline u4 get4LE(unsigned char const* pSrc)
95 {
96 return pSrc[0] | (pSrc[1] << 8) | (pSrc[2] << 16) | (pSrc[3] << 24);
97 }
98
99 /*
100 * Converts a single-character primitive type into its human-readable
101 * equivalent.
102 */
primitiveTypeLabel(char typeChar)103 static const char* primitiveTypeLabel(char typeChar)
104 {
105 switch (typeChar) {
106 case 'B': return "byte";
107 case 'C': return "char";
108 case 'D': return "double";
109 case 'F': return "float";
110 case 'I': return "int";
111 case 'J': return "long";
112 case 'S': return "short";
113 case 'V': return "void";
114 case 'Z': return "boolean";
115 default:
116 return "UNKNOWN";
117 }
118 }
119
120 /*
121 * Converts a type descriptor to human-readable "dotted" form. For
122 * example, "Ljava/lang/String;" becomes "java.lang.String", and
123 * "[I" becomes "int[]". Also converts '$' to '.', which means this
124 * form can't be converted back to a descriptor.
125 */
descriptorToDot(const char * str)126 static char* descriptorToDot(const char* str)
127 {
128 int targetLen = strlen(str);
129 int offset = 0;
130 int arrayDepth = 0;
131 char* newStr;
132
133 /* strip leading [s; will be added to end */
134 while (targetLen > 1 && str[offset] == '[') {
135 offset++;
136 targetLen--;
137 }
138 arrayDepth = offset;
139
140 if (targetLen == 1) {
141 /* primitive type */
142 str = primitiveTypeLabel(str[offset]);
143 offset = 0;
144 targetLen = strlen(str);
145 } else {
146 /* account for leading 'L' and trailing ';' */
147 if (targetLen >= 2 && str[offset] == 'L' &&
148 str[offset+targetLen-1] == ';')
149 {
150 targetLen -= 2;
151 offset++;
152 }
153 }
154
155 newStr = (char*)malloc(targetLen + arrayDepth * 2 +1);
156
157 /* copy class name over */
158 int i;
159 for (i = 0; i < targetLen; i++) {
160 char ch = str[offset + i];
161 newStr[i] = (ch == '/' || ch == '$') ? '.' : ch;
162 }
163
164 /* add the appropriate number of brackets for arrays */
165 while (arrayDepth-- > 0) {
166 newStr[i++] = '[';
167 newStr[i++] = ']';
168 }
169 newStr[i] = '\0';
170 assert(i == targetLen + arrayDepth * 2);
171
172 return newStr;
173 }
174
175 /*
176 * Converts the class name portion of a type descriptor to human-readable
177 * "dotted" form.
178 *
179 * Returns a newly-allocated string.
180 */
descriptorClassToDot(const char * str)181 static char* descriptorClassToDot(const char* str)
182 {
183 const char* lastSlash;
184 char* newStr;
185 char* cp;
186
187 /* reduce to just the class name, trimming trailing ';' */
188 lastSlash = strrchr(str, '/');
189 if (lastSlash == NULL)
190 lastSlash = str + 1; /* start past 'L' */
191 else
192 lastSlash++; /* start past '/' */
193
194 newStr = strdup(lastSlash);
195 newStr[strlen(lastSlash)-1] = '\0';
196 for (cp = newStr; *cp != '\0'; cp++) {
197 if (*cp == '$')
198 *cp = '.';
199 }
200
201 return newStr;
202 }
203
204 /*
205 * Returns a quoted string representing the boolean value.
206 */
quotedBool(bool val)207 static const char* quotedBool(bool val)
208 {
209 if (val)
210 return "\"true\"";
211 else
212 return "\"false\"";
213 }
214
quotedVisibility(u4 accessFlags)215 static const char* quotedVisibility(u4 accessFlags)
216 {
217 if ((accessFlags & ACC_PUBLIC) != 0)
218 return "\"public\"";
219 else if ((accessFlags & ACC_PROTECTED) != 0)
220 return "\"protected\"";
221 else if ((accessFlags & ACC_PRIVATE) != 0)
222 return "\"private\"";
223 else
224 return "\"package\"";
225 }
226
227 /*
228 * Count the number of '1' bits in a word.
229 */
countOnes(u4 val)230 static int countOnes(u4 val)
231 {
232 int count = 0;
233
234 val = val - ((val >> 1) & 0x55555555);
235 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
236 count = (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
237
238 return count;
239 }
240
241 /*
242 * Flag for use with createAccessFlagStr().
243 */
244 enum AccessFor {
245 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2,
246 kAccessForMAX
247 };
248
249 /*
250 * Create a new string with human-readable access flags.
251 *
252 * In the base language the access_flags fields are type u2; in Dalvik
253 * they're u4.
254 */
createAccessFlagStr(u4 flags,AccessFor forWhat)255 static char* createAccessFlagStr(u4 flags, AccessFor forWhat)
256 {
257 #define NUM_FLAGS 18
258 static const char* kAccessStrings[kAccessForMAX][NUM_FLAGS] = {
259 {
260 /* class, inner class */
261 "PUBLIC", /* 0x0001 */
262 "PRIVATE", /* 0x0002 */
263 "PROTECTED", /* 0x0004 */
264 "STATIC", /* 0x0008 */
265 "FINAL", /* 0x0010 */
266 "?", /* 0x0020 */
267 "?", /* 0x0040 */
268 "?", /* 0x0080 */
269 "?", /* 0x0100 */
270 "INTERFACE", /* 0x0200 */
271 "ABSTRACT", /* 0x0400 */
272 "?", /* 0x0800 */
273 "SYNTHETIC", /* 0x1000 */
274 "ANNOTATION", /* 0x2000 */
275 "ENUM", /* 0x4000 */
276 "?", /* 0x8000 */
277 "VERIFIED", /* 0x10000 */
278 "OPTIMIZED", /* 0x20000 */
279 },
280 {
281 /* method */
282 "PUBLIC", /* 0x0001 */
283 "PRIVATE", /* 0x0002 */
284 "PROTECTED", /* 0x0004 */
285 "STATIC", /* 0x0008 */
286 "FINAL", /* 0x0010 */
287 "SYNCHRONIZED", /* 0x0020 */
288 "BRIDGE", /* 0x0040 */
289 "VARARGS", /* 0x0080 */
290 "NATIVE", /* 0x0100 */
291 "?", /* 0x0200 */
292 "ABSTRACT", /* 0x0400 */
293 "STRICT", /* 0x0800 */
294 "SYNTHETIC", /* 0x1000 */
295 "?", /* 0x2000 */
296 "?", /* 0x4000 */
297 "MIRANDA", /* 0x8000 */
298 "CONSTRUCTOR", /* 0x10000 */
299 "DECLARED_SYNCHRONIZED", /* 0x20000 */
300 },
301 {
302 /* field */
303 "PUBLIC", /* 0x0001 */
304 "PRIVATE", /* 0x0002 */
305 "PROTECTED", /* 0x0004 */
306 "STATIC", /* 0x0008 */
307 "FINAL", /* 0x0010 */
308 "?", /* 0x0020 */
309 "VOLATILE", /* 0x0040 */
310 "TRANSIENT", /* 0x0080 */
311 "?", /* 0x0100 */
312 "?", /* 0x0200 */
313 "?", /* 0x0400 */
314 "?", /* 0x0800 */
315 "SYNTHETIC", /* 0x1000 */
316 "?", /* 0x2000 */
317 "ENUM", /* 0x4000 */
318 "?", /* 0x8000 */
319 "?", /* 0x10000 */
320 "?", /* 0x20000 */
321 },
322 };
323 const int kLongest = 21; /* strlen of longest string above */
324 int i, count;
325 char* str;
326 char* cp;
327
328 /*
329 * Allocate enough storage to hold the expected number of strings,
330 * plus a space between each. We over-allocate, using the longest
331 * string above as the base metric.
332 */
333 count = countOnes(flags);
334 cp = str = (char*) malloc(count * (kLongest+1) +1);
335
336 for (i = 0; i < NUM_FLAGS; i++) {
337 if (flags & 0x01) {
338 const char* accessStr = kAccessStrings[forWhat][i];
339 int len = strlen(accessStr);
340 if (cp != str)
341 *cp++ = ' ';
342
343 memcpy(cp, accessStr, len);
344 cp += len;
345 }
346 flags >>= 1;
347 }
348 *cp = '\0';
349
350 return str;
351 }
352
353
354 /*
355 * Copy character data from "data" to "out", converting non-ASCII values
356 * to printf format chars or an ASCII filler ('.' or '?').
357 *
358 * The output buffer must be able to hold (2*len)+1 bytes. The result is
359 * NUL-terminated.
360 */
asciify(char * out,const unsigned char * data,size_t len)361 static void asciify(char* out, const unsigned char* data, size_t len)
362 {
363 while (len--) {
364 if (*data < 0x20) {
365 /* could do more here, but we don't need them yet */
366 switch (*data) {
367 case '\0':
368 *out++ = '\\';
369 *out++ = '0';
370 break;
371 case '\n':
372 *out++ = '\\';
373 *out++ = 'n';
374 break;
375 default:
376 *out++ = '.';
377 break;
378 }
379 } else if (*data >= 0x80) {
380 *out++ = '?';
381 } else {
382 *out++ = *data;
383 }
384 data++;
385 }
386 *out = '\0';
387 }
388
389 /*
390 * Dump the file header.
391 */
dumpFileHeader(const DexFile * pDexFile)392 void dumpFileHeader(const DexFile* pDexFile)
393 {
394 const DexOptHeader* pOptHeader = pDexFile->pOptHeader;
395 const DexHeader* pHeader = pDexFile->pHeader;
396 char sanitized[sizeof(pHeader->magic)*2 +1];
397
398 assert(sizeof(pHeader->magic) == sizeof(pOptHeader->magic));
399
400 if (pOptHeader != NULL) {
401 printf("Optimized DEX file header:\n");
402
403 asciify(sanitized, pOptHeader->magic, sizeof(pOptHeader->magic));
404 printf("magic : '%s'\n", sanitized);
405 printf("dex_offset : %d (0x%06x)\n",
406 pOptHeader->dexOffset, pOptHeader->dexOffset);
407 printf("dex_length : %d\n", pOptHeader->dexLength);
408 printf("deps_offset : %d (0x%06x)\n",
409 pOptHeader->depsOffset, pOptHeader->depsOffset);
410 printf("deps_length : %d\n", pOptHeader->depsLength);
411 printf("opt_offset : %d (0x%06x)\n",
412 pOptHeader->optOffset, pOptHeader->optOffset);
413 printf("opt_length : %d\n", pOptHeader->optLength);
414 printf("flags : %08x\n", pOptHeader->flags);
415 printf("checksum : %08x\n", pOptHeader->checksum);
416 printf("\n");
417 }
418
419 printf("DEX file header:\n");
420 asciify(sanitized, pHeader->magic, sizeof(pHeader->magic));
421 printf("magic : '%s'\n", sanitized);
422 printf("checksum : %08x\n", pHeader->checksum);
423 printf("signature : %02x%02x...%02x%02x\n",
424 pHeader->signature[0], pHeader->signature[1],
425 pHeader->signature[kSHA1DigestLen-2],
426 pHeader->signature[kSHA1DigestLen-1]);
427 printf("file_size : %d\n", pHeader->fileSize);
428 printf("header_size : %d\n", pHeader->headerSize);
429 printf("link_size : %d\n", pHeader->linkSize);
430 printf("link_off : %d (0x%06x)\n",
431 pHeader->linkOff, pHeader->linkOff);
432 printf("string_ids_size : %d\n", pHeader->stringIdsSize);
433 printf("string_ids_off : %d (0x%06x)\n",
434 pHeader->stringIdsOff, pHeader->stringIdsOff);
435 printf("type_ids_size : %d\n", pHeader->typeIdsSize);
436 printf("type_ids_off : %d (0x%06x)\n",
437 pHeader->typeIdsOff, pHeader->typeIdsOff);
438 printf("field_ids_size : %d\n", pHeader->fieldIdsSize);
439 printf("field_ids_off : %d (0x%06x)\n",
440 pHeader->fieldIdsOff, pHeader->fieldIdsOff);
441 printf("method_ids_size : %d\n", pHeader->methodIdsSize);
442 printf("method_ids_off : %d (0x%06x)\n",
443 pHeader->methodIdsOff, pHeader->methodIdsOff);
444 printf("class_defs_size : %d\n", pHeader->classDefsSize);
445 printf("class_defs_off : %d (0x%06x)\n",
446 pHeader->classDefsOff, pHeader->classDefsOff);
447 printf("data_size : %d\n", pHeader->dataSize);
448 printf("data_off : %d (0x%06x)\n",
449 pHeader->dataOff, pHeader->dataOff);
450 printf("\n");
451 }
452
453 /*
454 * Dump the "table of contents" for the opt area.
455 */
dumpOptDirectory(const DexFile * pDexFile)456 void dumpOptDirectory(const DexFile* pDexFile)
457 {
458 const DexOptHeader* pOptHeader = pDexFile->pOptHeader;
459 if (pOptHeader == NULL)
460 return;
461
462 printf("OPT section contents:\n");
463
464 const u4* pOpt = (const u4*) ((u1*) pOptHeader + pOptHeader->optOffset);
465
466 if (*pOpt == 0) {
467 printf("(1.0 format, only class lookup table is present)\n\n");
468 return;
469 }
470
471 /*
472 * The "opt" section is in "chunk" format: a 32-bit identifier, a 32-bit
473 * length, then the data. Chunks start on 64-bit boundaries.
474 */
475 while (*pOpt != kDexChunkEnd) {
476 const char* verboseStr;
477
478 u4 size = *(pOpt+1);
479
480 switch (*pOpt) {
481 case kDexChunkClassLookup:
482 verboseStr = "class lookup hash table";
483 break;
484 case kDexChunkRegisterMaps:
485 verboseStr = "register maps";
486 break;
487 default:
488 verboseStr = "(unknown chunk type)";
489 break;
490 }
491
492 printf("Chunk %08x (%c%c%c%c) - %s (%d bytes)\n", *pOpt,
493 *pOpt >> 24, (char)(*pOpt >> 16), (char)(*pOpt >> 8), (char)*pOpt,
494 verboseStr, size);
495
496 size = (size + 8 + 7) & ~7;
497 pOpt += size / sizeof(u4);
498 }
499 printf("\n");
500 }
501
502 /*
503 * Dump a class_def_item.
504 */
dumpClassDef(DexFile * pDexFile,int idx)505 void dumpClassDef(DexFile* pDexFile, int idx)
506 {
507 const DexClassDef* pClassDef;
508 const u1* pEncodedData;
509 DexClassData* pClassData;
510
511 pClassDef = dexGetClassDef(pDexFile, idx);
512 pEncodedData = dexGetClassData(pDexFile, pClassDef);
513 pClassData = dexReadAndVerifyClassData(&pEncodedData, NULL);
514
515 if (pClassData == NULL) {
516 fprintf(stderr, "Trouble reading class data\n");
517 return;
518 }
519
520 printf("Class #%d header:\n", idx);
521 printf("class_idx : %d\n", pClassDef->classIdx);
522 printf("access_flags : %d (0x%04x)\n",
523 pClassDef->accessFlags, pClassDef->accessFlags);
524 printf("superclass_idx : %d\n", pClassDef->superclassIdx);
525 printf("interfaces_off : %d (0x%06x)\n",
526 pClassDef->interfacesOff, pClassDef->interfacesOff);
527 printf("source_file_idx : %d\n", pClassDef->sourceFileIdx);
528 printf("annotations_off : %d (0x%06x)\n",
529 pClassDef->annotationsOff, pClassDef->annotationsOff);
530 printf("class_data_off : %d (0x%06x)\n",
531 pClassDef->classDataOff, pClassDef->classDataOff);
532 printf("static_fields_size : %d\n", pClassData->header.staticFieldsSize);
533 printf("instance_fields_size: %d\n",
534 pClassData->header.instanceFieldsSize);
535 printf("direct_methods_size : %d\n", pClassData->header.directMethodsSize);
536 printf("virtual_methods_size: %d\n",
537 pClassData->header.virtualMethodsSize);
538 printf("\n");
539
540 free(pClassData);
541 }
542
543 /*
544 * Dump an interface that a class declares to implement.
545 */
dumpInterface(const DexFile * pDexFile,const DexTypeItem * pTypeItem,int i)546 void dumpInterface(const DexFile* pDexFile, const DexTypeItem* pTypeItem,
547 int i)
548 {
549 const char* interfaceName =
550 dexStringByTypeIdx(pDexFile, pTypeItem->typeIdx);
551
552 if (gOptions.outputFormat == OUTPUT_PLAIN) {
553 printf(" #%d : '%s'\n", i, interfaceName);
554 } else {
555 char* dotted = descriptorToDot(interfaceName);
556 printf("<implements name=\"%s\">\n</implements>\n", dotted);
557 free(dotted);
558 }
559 }
560
561 /*
562 * Dump the catches table associated with the code.
563 */
dumpCatches(DexFile * pDexFile,const DexCode * pCode)564 void dumpCatches(DexFile* pDexFile, const DexCode* pCode)
565 {
566 u4 triesSize = pCode->triesSize;
567
568 if (triesSize == 0) {
569 printf(" catches : (none)\n");
570 return;
571 }
572
573 printf(" catches : %d\n", triesSize);
574
575 const DexTry* pTries = dexGetTries(pCode);
576 u4 i;
577
578 for (i = 0; i < triesSize; i++) {
579 const DexTry* pTry = &pTries[i];
580 u4 start = pTry->startAddr;
581 u4 end = start + pTry->insnCount;
582 DexCatchIterator iterator;
583
584 printf(" 0x%04x - 0x%04x\n", start, end);
585
586 dexCatchIteratorInit(&iterator, pCode, pTry->handlerOff);
587
588 for (;;) {
589 DexCatchHandler* handler = dexCatchIteratorNext(&iterator);
590 const char* descriptor;
591
592 if (handler == NULL) {
593 break;
594 }
595
596 descriptor = (handler->typeIdx == kDexNoIndex) ? "<any>" :
597 dexStringByTypeIdx(pDexFile, handler->typeIdx);
598
599 printf(" %s -> 0x%04x\n", descriptor,
600 handler->address);
601 }
602 }
603 }
604
dumpPositionsCb(void * cnxt,u4 address,u4 lineNum)605 static int dumpPositionsCb(void *cnxt, u4 address, u4 lineNum)
606 {
607 printf(" 0x%04x line=%d\n", address, lineNum);
608 return 0;
609 }
610
611 /*
612 * Dump the positions list.
613 */
dumpPositions(DexFile * pDexFile,const DexCode * pCode,const DexMethod * pDexMethod)614 void dumpPositions(DexFile* pDexFile, const DexCode* pCode,
615 const DexMethod *pDexMethod)
616 {
617 printf(" positions : \n");
618 const DexMethodId *pMethodId
619 = dexGetMethodId(pDexFile, pDexMethod->methodIdx);
620 const char *classDescriptor
621 = dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
622
623 dexDecodeDebugInfo(pDexFile, pCode, classDescriptor, pMethodId->protoIdx,
624 pDexMethod->accessFlags, dumpPositionsCb, NULL, NULL);
625 }
626
dumpLocalsCb(void * cnxt,u2 reg,u4 startAddress,u4 endAddress,const char * name,const char * descriptor,const char * signature)627 static void dumpLocalsCb(void *cnxt, u2 reg, u4 startAddress,
628 u4 endAddress, const char *name, const char *descriptor,
629 const char *signature)
630 {
631 printf(" 0x%04x - 0x%04x reg=%d %s %s %s\n",
632 startAddress, endAddress, reg, name, descriptor,
633 signature);
634 }
635
636 /*
637 * Dump the locals list.
638 */
dumpLocals(DexFile * pDexFile,const DexCode * pCode,const DexMethod * pDexMethod)639 void dumpLocals(DexFile* pDexFile, const DexCode* pCode,
640 const DexMethod *pDexMethod)
641 {
642 printf(" locals : \n");
643
644 const DexMethodId *pMethodId
645 = dexGetMethodId(pDexFile, pDexMethod->methodIdx);
646 const char *classDescriptor
647 = dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
648
649 dexDecodeDebugInfo(pDexFile, pCode, classDescriptor, pMethodId->protoIdx,
650 pDexMethod->accessFlags, NULL, dumpLocalsCb, NULL);
651 }
652
653 /*
654 * Get information about a method.
655 */
getMethodInfo(DexFile * pDexFile,u4 methodIdx,FieldMethodInfo * pMethInfo)656 bool getMethodInfo(DexFile* pDexFile, u4 methodIdx, FieldMethodInfo* pMethInfo)
657 {
658 const DexMethodId* pMethodId;
659
660 if (methodIdx >= pDexFile->pHeader->methodIdsSize)
661 return false;
662
663 pMethodId = dexGetMethodId(pDexFile, methodIdx);
664 pMethInfo->name = dexStringById(pDexFile, pMethodId->nameIdx);
665 pMethInfo->signature = dexCopyDescriptorFromMethodId(pDexFile, pMethodId);
666
667 pMethInfo->classDescriptor =
668 dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
669 return true;
670 }
671
672 /*
673 * Get information about a field.
674 */
getFieldInfo(DexFile * pDexFile,u4 fieldIdx,FieldMethodInfo * pFieldInfo)675 bool getFieldInfo(DexFile* pDexFile, u4 fieldIdx, FieldMethodInfo* pFieldInfo)
676 {
677 const DexFieldId* pFieldId;
678
679 if (fieldIdx >= pDexFile->pHeader->fieldIdsSize)
680 return false;
681
682 pFieldId = dexGetFieldId(pDexFile, fieldIdx);
683 pFieldInfo->name = dexStringById(pDexFile, pFieldId->nameIdx);
684 pFieldInfo->signature = dexStringByTypeIdx(pDexFile, pFieldId->typeIdx);
685 pFieldInfo->classDescriptor =
686 dexStringByTypeIdx(pDexFile, pFieldId->classIdx);
687 return true;
688 }
689
690
691 /*
692 * Look up a class' descriptor.
693 */
getClassDescriptor(DexFile * pDexFile,u4 classIdx)694 const char* getClassDescriptor(DexFile* pDexFile, u4 classIdx)
695 {
696 return dexStringByTypeIdx(pDexFile, classIdx);
697 }
698
699 /*
700 * Helper for dumpInstruction(), which builds the string
701 * representation for the index in the given instruction. This will
702 * first try to use the given buffer, but if the result won't fit,
703 * then this will allocate a new buffer to hold the result. A pointer
704 * to the buffer which holds the full result is always returned, and
705 * this can be compared with the one passed in, to see if the result
706 * needs to be free()d.
707 */
indexString(DexFile * pDexFile,const DecodedInstruction * pDecInsn,char * buf,size_t bufSize)708 static char* indexString(DexFile* pDexFile,
709 const DecodedInstruction* pDecInsn, char* buf, size_t bufSize)
710 {
711 int outSize;
712 u4 index;
713 u4 width;
714
715 /* TODO: Make the index *always* be in field B, to simplify this code. */
716 switch (dexGetFormatFromOpcode(pDecInsn->opcode)) {
717 case kFmt20bc:
718 case kFmt21c:
719 case kFmt35c:
720 case kFmt35ms:
721 case kFmt3rc:
722 case kFmt3rms:
723 case kFmt35mi:
724 case kFmt3rmi:
725 index = pDecInsn->vB;
726 width = 4;
727 break;
728 case kFmt31c:
729 index = pDecInsn->vB;
730 width = 8;
731 break;
732 case kFmt22c:
733 case kFmt22cs:
734 index = pDecInsn->vC;
735 width = 4;
736 break;
737 default:
738 index = 0;
739 width = 4;
740 break;
741 }
742
743 switch (pDecInsn->indexType) {
744 case kIndexUnknown:
745 /*
746 * This function shouldn't ever get called for this type, but do
747 * something sensible here, just to help with debugging.
748 */
749 outSize = snprintf(buf, bufSize, "<unknown-index>");
750 break;
751 case kIndexNone:
752 /*
753 * This function shouldn't ever get called for this type, but do
754 * something sensible here, just to help with debugging.
755 */
756 outSize = snprintf(buf, bufSize, "<no-index>");
757 break;
758 case kIndexVaries:
759 /*
760 * This one should never show up in a dexdump, so no need to try
761 * to get fancy here.
762 */
763 outSize = snprintf(buf, bufSize, "<index-varies> // thing@%0*x",
764 width, index);
765 break;
766 case kIndexTypeRef:
767 outSize = snprintf(buf, bufSize, "%s // type@%0*x",
768 getClassDescriptor(pDexFile, index), width, index);
769 break;
770 case kIndexStringRef:
771 outSize = snprintf(buf, bufSize, "\"%s\" // string@%0*x",
772 dexStringById(pDexFile, index), width, index);
773 break;
774 case kIndexMethodRef:
775 {
776 FieldMethodInfo methInfo;
777 if (getMethodInfo(pDexFile, index, &methInfo)) {
778 outSize = snprintf(buf, bufSize, "%s.%s:%s // method@%0*x",
779 methInfo.classDescriptor, methInfo.name,
780 methInfo.signature, width, index);
781 } else {
782 outSize = snprintf(buf, bufSize, "<method?> // method@%0*x",
783 width, index);
784 }
785 }
786 break;
787 case kIndexFieldRef:
788 {
789 FieldMethodInfo fieldInfo;
790 if (getFieldInfo(pDexFile, index, &fieldInfo)) {
791 outSize = snprintf(buf, bufSize, "%s.%s:%s // field@%0*x",
792 fieldInfo.classDescriptor, fieldInfo.name,
793 fieldInfo.signature, width, index);
794 } else {
795 outSize = snprintf(buf, bufSize, "<field?> // field@%0*x",
796 width, index);
797 }
798 }
799 break;
800 case kIndexInlineMethod:
801 outSize = snprintf(buf, bufSize, "[%0*x] // inline #%0*x",
802 width, index, width, index);
803 break;
804 case kIndexVtableOffset:
805 outSize = snprintf(buf, bufSize, "[%0*x] // vtable #%0*x",
806 width, index, width, index);
807 break;
808 case kIndexFieldOffset:
809 outSize = snprintf(buf, bufSize, "[obj+%0*x]", width, index);
810 break;
811 default:
812 outSize = snprintf(buf, bufSize, "<?>");
813 break;
814 }
815
816 if (outSize >= (int) bufSize) {
817 /*
818 * The buffer wasn't big enough; allocate and retry. Note:
819 * snprintf() doesn't count the '\0' as part of its returned
820 * size, so we add explicit space for it here.
821 */
822 outSize++;
823 buf = (char*)malloc(outSize);
824 if (buf == NULL) {
825 return NULL;
826 }
827 return indexString(pDexFile, pDecInsn, buf, outSize);
828 } else {
829 return buf;
830 }
831 }
832
833 /*
834 * Dump a single instruction.
835 */
dumpInstruction(DexFile * pDexFile,const DexCode * pCode,int insnIdx,int insnWidth,const DecodedInstruction * pDecInsn)836 void dumpInstruction(DexFile* pDexFile, const DexCode* pCode, int insnIdx,
837 int insnWidth, const DecodedInstruction* pDecInsn)
838 {
839 char indexBufChars[200];
840 char *indexBuf = indexBufChars;
841 const u2* insns = pCode->insns;
842 int i;
843
844 printf("%06x:", ((u1*)insns - pDexFile->baseAddr) + insnIdx*2);
845 for (i = 0; i < 8; i++) {
846 if (i < insnWidth) {
847 if (i == 7) {
848 printf(" ... ");
849 } else {
850 /* print 16-bit value in little-endian order */
851 const u1* bytePtr = (const u1*) &insns[insnIdx+i];
852 printf(" %02x%02x", bytePtr[0], bytePtr[1]);
853 }
854 } else {
855 fputs(" ", stdout);
856 }
857 }
858
859 if (pDecInsn->opcode == OP_NOP) {
860 u2 instr = get2LE((const u1*) &insns[insnIdx]);
861 if (instr == kPackedSwitchSignature) {
862 printf("|%04x: packed-switch-data (%d units)",
863 insnIdx, insnWidth);
864 } else if (instr == kSparseSwitchSignature) {
865 printf("|%04x: sparse-switch-data (%d units)",
866 insnIdx, insnWidth);
867 } else if (instr == kArrayDataSignature) {
868 printf("|%04x: array-data (%d units)",
869 insnIdx, insnWidth);
870 } else {
871 printf("|%04x: nop // spacer", insnIdx);
872 }
873 } else {
874 printf("|%04x: %s", insnIdx, dexGetOpcodeName(pDecInsn->opcode));
875 }
876
877 if (pDecInsn->indexType != kIndexNone) {
878 indexBuf = indexString(pDexFile, pDecInsn,
879 indexBufChars, sizeof(indexBufChars));
880 }
881
882 switch (dexGetFormatFromOpcode(pDecInsn->opcode)) {
883 case kFmt10x: // op
884 break;
885 case kFmt12x: // op vA, vB
886 printf(" v%d, v%d", pDecInsn->vA, pDecInsn->vB);
887 break;
888 case kFmt11n: // op vA, #+B
889 printf(" v%d, #int %d // #%x",
890 pDecInsn->vA, (s4)pDecInsn->vB, (u1)pDecInsn->vB);
891 break;
892 case kFmt11x: // op vAA
893 printf(" v%d", pDecInsn->vA);
894 break;
895 case kFmt10t: // op +AA
896 case kFmt20t: // op +AAAA
897 {
898 s4 targ = (s4) pDecInsn->vA;
899 printf(" %04x // %c%04x",
900 insnIdx + targ,
901 (targ < 0) ? '-' : '+',
902 (targ < 0) ? -targ : targ);
903 }
904 break;
905 case kFmt22x: // op vAA, vBBBB
906 printf(" v%d, v%d", pDecInsn->vA, pDecInsn->vB);
907 break;
908 case kFmt21t: // op vAA, +BBBB
909 {
910 s4 targ = (s4) pDecInsn->vB;
911 printf(" v%d, %04x // %c%04x", pDecInsn->vA,
912 insnIdx + targ,
913 (targ < 0) ? '-' : '+',
914 (targ < 0) ? -targ : targ);
915 }
916 break;
917 case kFmt21s: // op vAA, #+BBBB
918 printf(" v%d, #int %d // #%x",
919 pDecInsn->vA, (s4)pDecInsn->vB, (u2)pDecInsn->vB);
920 break;
921 case kFmt21h: // op vAA, #+BBBB0000[00000000]
922 // The printed format varies a bit based on the actual opcode.
923 if (pDecInsn->opcode == OP_CONST_HIGH16) {
924 s4 value = pDecInsn->vB << 16;
925 printf(" v%d, #int %d // #%x",
926 pDecInsn->vA, value, (u2)pDecInsn->vB);
927 } else {
928 s8 value = ((s8) pDecInsn->vB) << 48;
929 printf(" v%d, #long %lld // #%x",
930 pDecInsn->vA, value, (u2)pDecInsn->vB);
931 }
932 break;
933 case kFmt21c: // op vAA, thing@BBBB
934 case kFmt31c: // op vAA, thing@BBBBBBBB
935 printf(" v%d, %s", pDecInsn->vA, indexBuf);
936 break;
937 case kFmt23x: // op vAA, vBB, vCC
938 printf(" v%d, v%d, v%d", pDecInsn->vA, pDecInsn->vB, pDecInsn->vC);
939 break;
940 case kFmt22b: // op vAA, vBB, #+CC
941 printf(" v%d, v%d, #int %d // #%02x",
942 pDecInsn->vA, pDecInsn->vB, (s4)pDecInsn->vC, (u1)pDecInsn->vC);
943 break;
944 case kFmt22t: // op vA, vB, +CCCC
945 {
946 s4 targ = (s4) pDecInsn->vC;
947 printf(" v%d, v%d, %04x // %c%04x", pDecInsn->vA, pDecInsn->vB,
948 insnIdx + targ,
949 (targ < 0) ? '-' : '+',
950 (targ < 0) ? -targ : targ);
951 }
952 break;
953 case kFmt22s: // op vA, vB, #+CCCC
954 printf(" v%d, v%d, #int %d // #%04x",
955 pDecInsn->vA, pDecInsn->vB, (s4)pDecInsn->vC, (u2)pDecInsn->vC);
956 break;
957 case kFmt22c: // op vA, vB, thing@CCCC
958 case kFmt22cs: // [opt] op vA, vB, field offset CCCC
959 printf(" v%d, v%d, %s", pDecInsn->vA, pDecInsn->vB, indexBuf);
960 break;
961 case kFmt30t:
962 printf(" #%08x", pDecInsn->vA);
963 break;
964 case kFmt31i: // op vAA, #+BBBBBBBB
965 {
966 /* this is often, but not always, a float */
967 union {
968 float f;
969 u4 i;
970 } conv;
971 conv.i = pDecInsn->vB;
972 printf(" v%d, #float %f // #%08x",
973 pDecInsn->vA, conv.f, pDecInsn->vB);
974 }
975 break;
976 case kFmt31t: // op vAA, offset +BBBBBBBB
977 printf(" v%d, %08x // +%08x",
978 pDecInsn->vA, insnIdx + pDecInsn->vB, pDecInsn->vB);
979 break;
980 case kFmt32x: // op vAAAA, vBBBB
981 printf(" v%d, v%d", pDecInsn->vA, pDecInsn->vB);
982 break;
983 case kFmt35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
984 case kFmt35ms: // [opt] invoke-virtual+super
985 case kFmt35mi: // [opt] inline invoke
986 {
987 fputs(" {", stdout);
988 for (i = 0; i < (int) pDecInsn->vA; i++) {
989 if (i == 0)
990 printf("v%d", pDecInsn->arg[i]);
991 else
992 printf(", v%d", pDecInsn->arg[i]);
993 }
994 printf("}, %s", indexBuf);
995 }
996 break;
997 case kFmt3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
998 case kFmt3rms: // [opt] invoke-virtual+super/range
999 case kFmt3rmi: // [opt] execute-inline/range
1000 {
1001 /*
1002 * This doesn't match the "dx" output when some of the args are
1003 * 64-bit values -- dx only shows the first register.
1004 */
1005 fputs(" {", stdout);
1006 for (i = 0; i < (int) pDecInsn->vA; i++) {
1007 if (i == 0)
1008 printf("v%d", pDecInsn->vC + i);
1009 else
1010 printf(", v%d", pDecInsn->vC + i);
1011 }
1012 printf("}, %s", indexBuf);
1013 }
1014 break;
1015 case kFmt51l: // op vAA, #+BBBBBBBBBBBBBBBB
1016 {
1017 /* this is often, but not always, a double */
1018 union {
1019 double d;
1020 u8 j;
1021 } conv;
1022 conv.j = pDecInsn->vB_wide;
1023 printf(" v%d, #double %f // #%016llx",
1024 pDecInsn->vA, conv.d, pDecInsn->vB_wide);
1025 }
1026 break;
1027 case kFmt00x: // unknown op or breakpoint
1028 break;
1029 default:
1030 printf(" ???");
1031 break;
1032 }
1033
1034 putchar('\n');
1035
1036 if (indexBuf != indexBufChars) {
1037 free(indexBuf);
1038 }
1039 }
1040
1041 /*
1042 * Dump a bytecode disassembly.
1043 */
dumpBytecodes(DexFile * pDexFile,const DexMethod * pDexMethod)1044 void dumpBytecodes(DexFile* pDexFile, const DexMethod* pDexMethod)
1045 {
1046 const DexCode* pCode = dexGetCode(pDexFile, pDexMethod);
1047 const u2* insns;
1048 int insnIdx;
1049 FieldMethodInfo methInfo;
1050 int startAddr;
1051 char* className = NULL;
1052
1053 assert(pCode->insnsSize > 0);
1054 insns = pCode->insns;
1055
1056 getMethodInfo(pDexFile, pDexMethod->methodIdx, &methInfo);
1057 startAddr = ((u1*)pCode - pDexFile->baseAddr);
1058 className = descriptorToDot(methInfo.classDescriptor);
1059
1060 printf("%06x: |[%06x] %s.%s:%s\n",
1061 startAddr, startAddr,
1062 className, methInfo.name, methInfo.signature);
1063
1064 insnIdx = 0;
1065 while (insnIdx < (int) pCode->insnsSize) {
1066 int insnWidth;
1067 DecodedInstruction decInsn;
1068 u2 instr;
1069
1070 /*
1071 * Note: This code parallels the function
1072 * dexGetWidthFromInstruction() in InstrUtils.c, but this version
1073 * can deal with data in either endianness.
1074 *
1075 * TODO: Figure out if this really matters, and possibly change
1076 * this to just use dexGetWidthFromInstruction().
1077 */
1078 instr = get2LE((const u1*)insns);
1079 if (instr == kPackedSwitchSignature) {
1080 insnWidth = 4 + get2LE((const u1*)(insns+1)) * 2;
1081 } else if (instr == kSparseSwitchSignature) {
1082 insnWidth = 2 + get2LE((const u1*)(insns+1)) * 4;
1083 } else if (instr == kArrayDataSignature) {
1084 int width = get2LE((const u1*)(insns+1));
1085 int size = get2LE((const u1*)(insns+2)) |
1086 (get2LE((const u1*)(insns+3))<<16);
1087 // The plus 1 is to round up for odd size and width.
1088 insnWidth = 4 + ((size * width) + 1) / 2;
1089 } else {
1090 Opcode opcode = dexOpcodeFromCodeUnit(instr);
1091 insnWidth = dexGetWidthFromOpcode(opcode);
1092 if (insnWidth == 0) {
1093 fprintf(stderr,
1094 "GLITCH: zero-width instruction at idx=0x%04x\n", insnIdx);
1095 break;
1096 }
1097 }
1098
1099 dexDecodeInstruction(insns, &decInsn);
1100 dumpInstruction(pDexFile, pCode, insnIdx, insnWidth, &decInsn);
1101
1102 insns += insnWidth;
1103 insnIdx += insnWidth;
1104 }
1105
1106 free(className);
1107 }
1108
1109 /*
1110 * Dump a "code" struct.
1111 */
dumpCode(DexFile * pDexFile,const DexMethod * pDexMethod)1112 void dumpCode(DexFile* pDexFile, const DexMethod* pDexMethod)
1113 {
1114 const DexCode* pCode = dexGetCode(pDexFile, pDexMethod);
1115
1116 printf(" registers : %d\n", pCode->registersSize);
1117 printf(" ins : %d\n", pCode->insSize);
1118 printf(" outs : %d\n", pCode->outsSize);
1119 printf(" insns size : %d 16-bit code units\n", pCode->insnsSize);
1120
1121 if (gOptions.disassemble)
1122 dumpBytecodes(pDexFile, pDexMethod);
1123
1124 dumpCatches(pDexFile, pCode);
1125 /* both of these are encoded in debug info */
1126 dumpPositions(pDexFile, pCode, pDexMethod);
1127 dumpLocals(pDexFile, pCode, pDexMethod);
1128 }
1129
1130 /*
1131 * Dump a method.
1132 */
dumpMethod(DexFile * pDexFile,const DexMethod * pDexMethod,int i)1133 void dumpMethod(DexFile* pDexFile, const DexMethod* pDexMethod, int i)
1134 {
1135 const DexMethodId* pMethodId;
1136 const char* backDescriptor;
1137 const char* name;
1138 char* typeDescriptor = NULL;
1139 char* accessStr = NULL;
1140
1141 if (gOptions.exportsOnly &&
1142 (pDexMethod->accessFlags & (ACC_PUBLIC | ACC_PROTECTED)) == 0)
1143 {
1144 return;
1145 }
1146
1147 pMethodId = dexGetMethodId(pDexFile, pDexMethod->methodIdx);
1148 name = dexStringById(pDexFile, pMethodId->nameIdx);
1149 typeDescriptor = dexCopyDescriptorFromMethodId(pDexFile, pMethodId);
1150
1151 backDescriptor = dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
1152
1153 accessStr = createAccessFlagStr(pDexMethod->accessFlags,
1154 kAccessForMethod);
1155
1156 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1157 printf(" #%d : (in %s)\n", i, backDescriptor);
1158 printf(" name : '%s'\n", name);
1159 printf(" type : '%s'\n", typeDescriptor);
1160 printf(" access : 0x%04x (%s)\n",
1161 pDexMethod->accessFlags, accessStr);
1162
1163 if (pDexMethod->codeOff == 0) {
1164 printf(" code : (none)\n");
1165 } else {
1166 printf(" code -\n");
1167 dumpCode(pDexFile, pDexMethod);
1168 }
1169
1170 if (gOptions.disassemble)
1171 putchar('\n');
1172 } else if (gOptions.outputFormat == OUTPUT_XML) {
1173 bool constructor = (name[0] == '<');
1174
1175 if (constructor) {
1176 char* tmp;
1177
1178 tmp = descriptorClassToDot(backDescriptor);
1179 printf("<constructor name=\"%s\"\n", tmp);
1180 free(tmp);
1181
1182 tmp = descriptorToDot(backDescriptor);
1183 printf(" type=\"%s\"\n", tmp);
1184 free(tmp);
1185 } else {
1186 printf("<method name=\"%s\"\n", name);
1187
1188 const char* returnType = strrchr(typeDescriptor, ')');
1189 if (returnType == NULL) {
1190 fprintf(stderr, "bad method type descriptor '%s'\n",
1191 typeDescriptor);
1192 goto bail;
1193 }
1194
1195 char* tmp = descriptorToDot(returnType+1);
1196 printf(" return=\"%s\"\n", tmp);
1197 free(tmp);
1198
1199 printf(" abstract=%s\n",
1200 quotedBool((pDexMethod->accessFlags & ACC_ABSTRACT) != 0));
1201 printf(" native=%s\n",
1202 quotedBool((pDexMethod->accessFlags & ACC_NATIVE) != 0));
1203
1204 bool isSync =
1205 (pDexMethod->accessFlags & ACC_SYNCHRONIZED) != 0 ||
1206 (pDexMethod->accessFlags & ACC_DECLARED_SYNCHRONIZED) != 0;
1207 printf(" synchronized=%s\n", quotedBool(isSync));
1208 }
1209
1210 printf(" static=%s\n",
1211 quotedBool((pDexMethod->accessFlags & ACC_STATIC) != 0));
1212 printf(" final=%s\n",
1213 quotedBool((pDexMethod->accessFlags & ACC_FINAL) != 0));
1214 // "deprecated=" not knowable w/o parsing annotations
1215 printf(" visibility=%s\n",
1216 quotedVisibility(pDexMethod->accessFlags));
1217
1218 printf(">\n");
1219
1220 /*
1221 * Parameters.
1222 */
1223 if (typeDescriptor[0] != '(') {
1224 fprintf(stderr, "ERROR: bad descriptor '%s'\n", typeDescriptor);
1225 goto bail;
1226 }
1227
1228 char tmpBuf[strlen(typeDescriptor)+1]; /* more than big enough */
1229 int argNum = 0;
1230
1231 const char* base = typeDescriptor+1;
1232
1233 while (*base != ')') {
1234 char* cp = tmpBuf;
1235
1236 while (*base == '[')
1237 *cp++ = *base++;
1238
1239 if (*base == 'L') {
1240 /* copy through ';' */
1241 do {
1242 *cp = *base++;
1243 } while (*cp++ != ';');
1244 } else {
1245 /* primitive char, copy it */
1246 if (strchr("ZBCSIFJD", *base) == NULL) {
1247 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
1248 goto bail;
1249 }
1250 *cp++ = *base++;
1251 }
1252
1253 /* null terminate and display */
1254 *cp++ = '\0';
1255
1256 char* tmp = descriptorToDot(tmpBuf);
1257 printf("<parameter name=\"arg%d\" type=\"%s\">\n</parameter>\n",
1258 argNum++, tmp);
1259 free(tmp);
1260 }
1261
1262 if (constructor)
1263 printf("</constructor>\n");
1264 else
1265 printf("</method>\n");
1266 }
1267
1268 bail:
1269 free(typeDescriptor);
1270 free(accessStr);
1271 }
1272
1273 /*
1274 * Dump a static (class) field.
1275 */
dumpSField(const DexFile * pDexFile,const DexField * pSField,int i)1276 void dumpSField(const DexFile* pDexFile, const DexField* pSField, int i)
1277 {
1278 const DexFieldId* pFieldId;
1279 const char* backDescriptor;
1280 const char* name;
1281 const char* typeDescriptor;
1282 char* accessStr;
1283
1284 if (gOptions.exportsOnly &&
1285 (pSField->accessFlags & (ACC_PUBLIC | ACC_PROTECTED)) == 0)
1286 {
1287 return;
1288 }
1289
1290 pFieldId = dexGetFieldId(pDexFile, pSField->fieldIdx);
1291 name = dexStringById(pDexFile, pFieldId->nameIdx);
1292 typeDescriptor = dexStringByTypeIdx(pDexFile, pFieldId->typeIdx);
1293 backDescriptor = dexStringByTypeIdx(pDexFile, pFieldId->classIdx);
1294
1295 accessStr = createAccessFlagStr(pSField->accessFlags, kAccessForField);
1296
1297 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1298 printf(" #%d : (in %s)\n", i, backDescriptor);
1299 printf(" name : '%s'\n", name);
1300 printf(" type : '%s'\n", typeDescriptor);
1301 printf(" access : 0x%04x (%s)\n",
1302 pSField->accessFlags, accessStr);
1303 } else if (gOptions.outputFormat == OUTPUT_XML) {
1304 char* tmp;
1305
1306 printf("<field name=\"%s\"\n", name);
1307
1308 tmp = descriptorToDot(typeDescriptor);
1309 printf(" type=\"%s\"\n", tmp);
1310 free(tmp);
1311
1312 printf(" transient=%s\n",
1313 quotedBool((pSField->accessFlags & ACC_TRANSIENT) != 0));
1314 printf(" volatile=%s\n",
1315 quotedBool((pSField->accessFlags & ACC_VOLATILE) != 0));
1316 // "value=" not knowable w/o parsing annotations
1317 printf(" static=%s\n",
1318 quotedBool((pSField->accessFlags & ACC_STATIC) != 0));
1319 printf(" final=%s\n",
1320 quotedBool((pSField->accessFlags & ACC_FINAL) != 0));
1321 // "deprecated=" not knowable w/o parsing annotations
1322 printf(" visibility=%s\n",
1323 quotedVisibility(pSField->accessFlags));
1324 printf(">\n</field>\n");
1325 }
1326
1327 free(accessStr);
1328 }
1329
1330 /*
1331 * Dump an instance field.
1332 */
dumpIField(const DexFile * pDexFile,const DexField * pIField,int i)1333 void dumpIField(const DexFile* pDexFile, const DexField* pIField, int i)
1334 {
1335 dumpSField(pDexFile, pIField, i);
1336 }
1337
1338 /*
1339 * Dump the class.
1340 *
1341 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1342 *
1343 * If "*pLastPackage" is NULL or does not match the current class' package,
1344 * the value will be replaced with a newly-allocated string.
1345 */
dumpClass(DexFile * pDexFile,int idx,char ** pLastPackage)1346 void dumpClass(DexFile* pDexFile, int idx, char** pLastPackage)
1347 {
1348 const DexTypeList* pInterfaces;
1349 const DexClassDef* pClassDef;
1350 DexClassData* pClassData = NULL;
1351 const u1* pEncodedData;
1352 const char* fileName;
1353 const char* classDescriptor;
1354 const char* superclassDescriptor;
1355 char* accessStr = NULL;
1356 int i;
1357
1358 pClassDef = dexGetClassDef(pDexFile, idx);
1359
1360 if (gOptions.exportsOnly && (pClassDef->accessFlags & ACC_PUBLIC) == 0) {
1361 //printf("<!-- omitting non-public class %s -->\n",
1362 // classDescriptor);
1363 goto bail;
1364 }
1365
1366 pEncodedData = dexGetClassData(pDexFile, pClassDef);
1367 pClassData = dexReadAndVerifyClassData(&pEncodedData, NULL);
1368
1369 if (pClassData == NULL) {
1370 printf("Trouble reading class data (#%d)\n", idx);
1371 goto bail;
1372 }
1373
1374 classDescriptor = dexStringByTypeIdx(pDexFile, pClassDef->classIdx);
1375
1376 /*
1377 * For the XML output, show the package name. Ideally we'd gather
1378 * up the classes, sort them, and dump them alphabetically so the
1379 * package name wouldn't jump around, but that's not a great plan
1380 * for something that needs to run on the device.
1381 */
1382 if (!(classDescriptor[0] == 'L' &&
1383 classDescriptor[strlen(classDescriptor)-1] == ';'))
1384 {
1385 /* arrays and primitives should not be defined explicitly */
1386 fprintf(stderr, "Malformed class name '%s'\n", classDescriptor);
1387 /* keep going? */
1388 } else if (gOptions.outputFormat == OUTPUT_XML) {
1389 char* mangle;
1390 char* lastSlash;
1391 char* cp;
1392
1393 mangle = strdup(classDescriptor + 1);
1394 mangle[strlen(mangle)-1] = '\0';
1395
1396 /* reduce to just the package name */
1397 lastSlash = strrchr(mangle, '/');
1398 if (lastSlash != NULL) {
1399 *lastSlash = '\0';
1400 } else {
1401 *mangle = '\0';
1402 }
1403
1404 for (cp = mangle; *cp != '\0'; cp++) {
1405 if (*cp == '/')
1406 *cp = '.';
1407 }
1408
1409 if (*pLastPackage == NULL || strcmp(mangle, *pLastPackage) != 0) {
1410 /* start of a new package */
1411 if (*pLastPackage != NULL)
1412 printf("</package>\n");
1413 printf("<package name=\"%s\"\n>\n", mangle);
1414 free(*pLastPackage);
1415 *pLastPackage = mangle;
1416 } else {
1417 free(mangle);
1418 }
1419 }
1420
1421 accessStr = createAccessFlagStr(pClassDef->accessFlags, kAccessForClass);
1422
1423 if (pClassDef->superclassIdx == kDexNoIndex) {
1424 superclassDescriptor = NULL;
1425 } else {
1426 superclassDescriptor =
1427 dexStringByTypeIdx(pDexFile, pClassDef->superclassIdx);
1428 }
1429
1430 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1431 printf("Class #%d -\n", idx);
1432 printf(" Class descriptor : '%s'\n", classDescriptor);
1433 printf(" Access flags : 0x%04x (%s)\n",
1434 pClassDef->accessFlags, accessStr);
1435
1436 if (superclassDescriptor != NULL)
1437 printf(" Superclass : '%s'\n", superclassDescriptor);
1438
1439 printf(" Interfaces -\n");
1440 } else {
1441 char* tmp;
1442
1443 tmp = descriptorClassToDot(classDescriptor);
1444 printf("<class name=\"%s\"\n", tmp);
1445 free(tmp);
1446
1447 if (superclassDescriptor != NULL) {
1448 tmp = descriptorToDot(superclassDescriptor);
1449 printf(" extends=\"%s\"\n", tmp);
1450 free(tmp);
1451 }
1452 printf(" abstract=%s\n",
1453 quotedBool((pClassDef->accessFlags & ACC_ABSTRACT) != 0));
1454 printf(" static=%s\n",
1455 quotedBool((pClassDef->accessFlags & ACC_STATIC) != 0));
1456 printf(" final=%s\n",
1457 quotedBool((pClassDef->accessFlags & ACC_FINAL) != 0));
1458 // "deprecated=" not knowable w/o parsing annotations
1459 printf(" visibility=%s\n",
1460 quotedVisibility(pClassDef->accessFlags));
1461 printf(">\n");
1462 }
1463 pInterfaces = dexGetInterfacesList(pDexFile, pClassDef);
1464 if (pInterfaces != NULL) {
1465 for (i = 0; i < (int) pInterfaces->size; i++)
1466 dumpInterface(pDexFile, dexGetTypeItem(pInterfaces, i), i);
1467 }
1468
1469 if (gOptions.outputFormat == OUTPUT_PLAIN)
1470 printf(" Static fields -\n");
1471 for (i = 0; i < (int) pClassData->header.staticFieldsSize; i++) {
1472 dumpSField(pDexFile, &pClassData->staticFields[i], i);
1473 }
1474
1475 if (gOptions.outputFormat == OUTPUT_PLAIN)
1476 printf(" Instance fields -\n");
1477 for (i = 0; i < (int) pClassData->header.instanceFieldsSize; i++) {
1478 dumpIField(pDexFile, &pClassData->instanceFields[i], i);
1479 }
1480
1481 if (gOptions.outputFormat == OUTPUT_PLAIN)
1482 printf(" Direct methods -\n");
1483 for (i = 0; i < (int) pClassData->header.directMethodsSize; i++) {
1484 dumpMethod(pDexFile, &pClassData->directMethods[i], i);
1485 }
1486
1487 if (gOptions.outputFormat == OUTPUT_PLAIN)
1488 printf(" Virtual methods -\n");
1489 for (i = 0; i < (int) pClassData->header.virtualMethodsSize; i++) {
1490 dumpMethod(pDexFile, &pClassData->virtualMethods[i], i);
1491 }
1492
1493 // TODO: Annotations.
1494
1495 if (pClassDef->sourceFileIdx != kDexNoIndex)
1496 fileName = dexStringById(pDexFile, pClassDef->sourceFileIdx);
1497 else
1498 fileName = "unknown";
1499
1500 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1501 printf(" source_file_idx : %d (%s)\n",
1502 pClassDef->sourceFileIdx, fileName);
1503 printf("\n");
1504 }
1505
1506 if (gOptions.outputFormat == OUTPUT_XML) {
1507 printf("</class>\n");
1508 }
1509
1510 bail:
1511 free(pClassData);
1512 free(accessStr);
1513 }
1514
1515
1516 /*
1517 * Advance "ptr" to ensure 32-bit alignment.
1518 */
align32(const u1 * ptr)1519 static inline const u1* align32(const u1* ptr)
1520 {
1521 return (u1*) (((int) ptr + 3) & ~0x03);
1522 }
1523
1524
1525 /*
1526 * Dump a map in the "differential" format.
1527 *
1528 * TODO: show a hex dump of the compressed data. (We can show the
1529 * uncompressed data if we move the compression code to libdex; otherwise
1530 * it's too complex to merit a fast & fragile implementation here.)
1531 */
dumpDifferentialCompressedMap(const u1 ** pData)1532 void dumpDifferentialCompressedMap(const u1** pData)
1533 {
1534 const u1* data = *pData;
1535 const u1* dataStart = data -1; // format byte already removed
1536 u1 regWidth;
1537 u2 numEntries;
1538
1539 /* standard header */
1540 regWidth = *data++;
1541 numEntries = *data++;
1542 numEntries |= (*data++) << 8;
1543
1544 /* compressed data begins with the compressed data length */
1545 int compressedLen = readUnsignedLeb128(&data);
1546 int addrWidth = 1;
1547 if ((*data & 0x80) != 0)
1548 addrWidth++;
1549
1550 int origLen = 4 + (addrWidth + regWidth) * numEntries;
1551 int compLen = (data - dataStart) + compressedLen;
1552
1553 printf(" (differential compression %d -> %d [%d -> %d])\n",
1554 origLen, compLen,
1555 (addrWidth + regWidth) * numEntries, compressedLen);
1556
1557 /* skip past end of entry */
1558 data += compressedLen;
1559
1560 *pData = data;
1561 }
1562
1563 /*
1564 * Dump register map contents of the current method.
1565 *
1566 * "*pData" should point to the start of the register map data. Advances
1567 * "*pData" to the start of the next map.
1568 */
dumpMethodMap(DexFile * pDexFile,const DexMethod * pDexMethod,int idx,const u1 ** pData)1569 void dumpMethodMap(DexFile* pDexFile, const DexMethod* pDexMethod, int idx,
1570 const u1** pData)
1571 {
1572 const u1* data = *pData;
1573 const DexMethodId* pMethodId;
1574 const char* name;
1575 int offset = data - (u1*) pDexFile->pOptHeader;
1576
1577 pMethodId = dexGetMethodId(pDexFile, pDexMethod->methodIdx);
1578 name = dexStringById(pDexFile, pMethodId->nameIdx);
1579 printf(" #%d: 0x%08x %s\n", idx, offset, name);
1580
1581 u1 format;
1582 int addrWidth;
1583
1584 format = *data++;
1585 if (format == 1) { /* kRegMapFormatNone */
1586 /* no map */
1587 printf(" (no map)\n");
1588 addrWidth = 0;
1589 } else if (format == 2) { /* kRegMapFormatCompact8 */
1590 addrWidth = 1;
1591 } else if (format == 3) { /* kRegMapFormatCompact16 */
1592 addrWidth = 2;
1593 } else if (format == 4) { /* kRegMapFormatDifferential */
1594 dumpDifferentialCompressedMap(&data);
1595 goto bail;
1596 } else {
1597 printf(" (unknown format %d!)\n", format);
1598 /* don't know how to skip data; failure will cascade to end of class */
1599 goto bail;
1600 }
1601
1602 if (addrWidth > 0) {
1603 u1 regWidth;
1604 u2 numEntries;
1605 int idx, addr, byte;
1606
1607 regWidth = *data++;
1608 numEntries = *data++;
1609 numEntries |= (*data++) << 8;
1610
1611 for (idx = 0; idx < numEntries; idx++) {
1612 addr = *data++;
1613 if (addrWidth > 1)
1614 addr |= (*data++) << 8;
1615
1616 printf(" %4x:", addr);
1617 for (byte = 0; byte < regWidth; byte++) {
1618 printf(" %02x", *data++);
1619 }
1620 printf("\n");
1621 }
1622 }
1623
1624 bail:
1625 //if (addrWidth >= 0)
1626 // *pData = align32(data);
1627 *pData = data;
1628 }
1629
1630 /*
1631 * Dump the contents of the register map area.
1632 *
1633 * These are only present in optimized DEX files, and the structure is
1634 * not really exposed to other parts of the VM itself. We're going to
1635 * dig through them here, but this is pretty fragile. DO NOT rely on
1636 * this or derive other code from it.
1637 */
dumpRegisterMaps(DexFile * pDexFile)1638 void dumpRegisterMaps(DexFile* pDexFile)
1639 {
1640 const u1* pClassPool = (const u1*)pDexFile->pRegisterMapPool;
1641 const u4* classOffsets;
1642 const u1* ptr;
1643 u4 numClasses;
1644 int baseFileOffset = (u1*) pClassPool - (u1*) pDexFile->pOptHeader;
1645 int idx;
1646
1647 if (pClassPool == NULL) {
1648 printf("No register maps found\n");
1649 return;
1650 }
1651
1652 ptr = pClassPool;
1653 numClasses = get4LE(ptr);
1654 ptr += sizeof(u4);
1655 classOffsets = (const u4*) ptr;
1656
1657 printf("RMAP begins at offset 0x%07x\n", baseFileOffset);
1658 printf("Maps for %d classes\n", numClasses);
1659 for (idx = 0; idx < (int) numClasses; idx++) {
1660 const DexClassDef* pClassDef;
1661 const char* classDescriptor;
1662
1663 pClassDef = dexGetClassDef(pDexFile, idx);
1664 classDescriptor = dexStringByTypeIdx(pDexFile, pClassDef->classIdx);
1665
1666 printf("%4d: +%d (0x%08x) %s\n", idx, classOffsets[idx],
1667 baseFileOffset + classOffsets[idx], classDescriptor);
1668
1669 if (classOffsets[idx] == 0)
1670 continue;
1671
1672 /*
1673 * What follows is a series of RegisterMap entries, one for every
1674 * direct method, then one for every virtual method.
1675 */
1676 DexClassData* pClassData;
1677 const u1* pEncodedData;
1678 const u1* data = (u1*) pClassPool + classOffsets[idx];
1679 u2 methodCount;
1680 int i;
1681
1682 pEncodedData = dexGetClassData(pDexFile, pClassDef);
1683 pClassData = dexReadAndVerifyClassData(&pEncodedData, NULL);
1684 if (pClassData == NULL) {
1685 fprintf(stderr, "Trouble reading class data\n");
1686 continue;
1687 }
1688
1689 methodCount = *data++;
1690 methodCount |= (*data++) << 8;
1691 data += 2; /* two pad bytes follow methodCount */
1692 if (methodCount != pClassData->header.directMethodsSize
1693 + pClassData->header.virtualMethodsSize)
1694 {
1695 printf("NOTE: method count discrepancy (%d != %d + %d)\n",
1696 methodCount, pClassData->header.directMethodsSize,
1697 pClassData->header.virtualMethodsSize);
1698 /* this is bad, but keep going anyway */
1699 }
1700
1701 printf(" direct methods: %d\n",
1702 pClassData->header.directMethodsSize);
1703 for (i = 0; i < (int) pClassData->header.directMethodsSize; i++) {
1704 dumpMethodMap(pDexFile, &pClassData->directMethods[i], i, &data);
1705 }
1706
1707 printf(" virtual methods: %d\n",
1708 pClassData->header.virtualMethodsSize);
1709 for (i = 0; i < (int) pClassData->header.virtualMethodsSize; i++) {
1710 dumpMethodMap(pDexFile, &pClassData->virtualMethods[i], i, &data);
1711 }
1712
1713 free(pClassData);
1714 }
1715 }
1716
1717 /*
1718 * Dump the requested sections of the file.
1719 */
processDexFile(const char * fileName,DexFile * pDexFile)1720 void processDexFile(const char* fileName, DexFile* pDexFile)
1721 {
1722 char* package = NULL;
1723 int i;
1724
1725 if (gOptions.verbose) {
1726 printf("Opened '%s', DEX version '%.3s'\n", fileName,
1727 pDexFile->pHeader->magic +4);
1728 }
1729
1730 if (gOptions.dumpRegisterMaps) {
1731 dumpRegisterMaps(pDexFile);
1732 return;
1733 }
1734
1735 if (gOptions.showFileHeaders) {
1736 dumpFileHeader(pDexFile);
1737 dumpOptDirectory(pDexFile);
1738 }
1739
1740 if (gOptions.outputFormat == OUTPUT_XML)
1741 printf("<api>\n");
1742
1743 for (i = 0; i < (int) pDexFile->pHeader->classDefsSize; i++) {
1744 if (gOptions.showSectionHeaders)
1745 dumpClassDef(pDexFile, i);
1746
1747 dumpClass(pDexFile, i, &package);
1748 }
1749
1750 /* free the last one allocated */
1751 if (package != NULL) {
1752 printf("</package>\n");
1753 free(package);
1754 }
1755
1756 if (gOptions.outputFormat == OUTPUT_XML)
1757 printf("</api>\n");
1758 }
1759
1760
1761 /*
1762 * Process one file.
1763 */
process(const char * fileName)1764 int process(const char* fileName)
1765 {
1766 DexFile* pDexFile = NULL;
1767 MemMapping map;
1768 bool mapped = false;
1769 int result = -1;
1770
1771 if (gOptions.verbose)
1772 printf("Processing '%s'...\n", fileName);
1773
1774 if (dexOpenAndMap(fileName, gOptions.tempFileName, &map, false) != 0) {
1775 return result;
1776 }
1777 mapped = true;
1778
1779 int flags = kDexParseVerifyChecksum;
1780 if (gOptions.ignoreBadChecksum)
1781 flags |= kDexParseContinueOnError;
1782
1783 pDexFile = dexFileParse((u1*)map.addr, map.length, flags);
1784 if (pDexFile == NULL) {
1785 fprintf(stderr, "ERROR: DEX parse failed\n");
1786 goto bail;
1787 }
1788
1789 if (gOptions.checksumOnly) {
1790 printf("Checksum verified\n");
1791 } else {
1792 processDexFile(fileName, pDexFile);
1793 }
1794
1795 result = 0;
1796
1797 bail:
1798 if (mapped)
1799 sysReleaseShmem(&map);
1800 if (pDexFile != NULL)
1801 dexFileFree(pDexFile);
1802 return result;
1803 }
1804
1805
1806 /*
1807 * Show usage.
1808 */
usage(void)1809 void usage(void)
1810 {
1811 fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
1812 fprintf(stderr,
1813 "%s: [-c] [-d] [-f] [-h] [-i] [-l layout] [-m] [-t tempfile] dexfile...\n",
1814 gProgName);
1815 fprintf(stderr, "\n");
1816 fprintf(stderr, " -c : verify checksum and exit\n");
1817 fprintf(stderr, " -d : disassemble code sections\n");
1818 fprintf(stderr, " -f : display summary information from file header\n");
1819 fprintf(stderr, " -h : display file header details\n");
1820 fprintf(stderr, " -i : ignore checksum failures\n");
1821 fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n");
1822 fprintf(stderr, " -m : dump register maps (and nothing else)\n");
1823 fprintf(stderr, " -t : temp file name (defaults to /sdcard/dex-temp-*)\n");
1824 }
1825
1826 /*
1827 * Parse args.
1828 *
1829 * I'm not using getopt_long() because we may not have it in libc.
1830 */
main(int argc,char * const argv[])1831 int main(int argc, char* const argv[])
1832 {
1833 bool wantUsage = false;
1834 int ic;
1835
1836 memset(&gOptions, 0, sizeof(gOptions));
1837 gOptions.verbose = true;
1838
1839 while (1) {
1840 ic = getopt(argc, argv, "cdfhil:mt:");
1841 if (ic < 0)
1842 break;
1843
1844 switch (ic) {
1845 case 'c': // verify the checksum then exit
1846 gOptions.checksumOnly = true;
1847 break;
1848 case 'd': // disassemble Dalvik instructions
1849 gOptions.disassemble = true;
1850 break;
1851 case 'f': // dump outer file header
1852 gOptions.showFileHeaders = true;
1853 break;
1854 case 'h': // dump section headers, i.e. all meta-data
1855 gOptions.showSectionHeaders = true;
1856 break;
1857 case 'i': // continue even if checksum is bad
1858 gOptions.ignoreBadChecksum = true;
1859 break;
1860 case 'l': // layout
1861 if (strcmp(optarg, "plain") == 0) {
1862 gOptions.outputFormat = OUTPUT_PLAIN;
1863 } else if (strcmp(optarg, "xml") == 0) {
1864 gOptions.outputFormat = OUTPUT_XML;
1865 gOptions.verbose = false;
1866 gOptions.exportsOnly = true;
1867 } else {
1868 wantUsage = true;
1869 }
1870 break;
1871 case 'm': // dump register maps only
1872 gOptions.dumpRegisterMaps = true;
1873 break;
1874 case 't': // temp file, used when opening compressed Jar
1875 gOptions.tempFileName = optarg;
1876 break;
1877 default:
1878 wantUsage = true;
1879 break;
1880 }
1881 }
1882
1883 if (optind == argc) {
1884 fprintf(stderr, "%s: no file specified\n", gProgName);
1885 wantUsage = true;
1886 }
1887
1888 if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) {
1889 fprintf(stderr, "Can't specify both -c and -i\n");
1890 wantUsage = true;
1891 }
1892
1893 if (wantUsage) {
1894 usage();
1895 return 2;
1896 }
1897
1898 int result = 0;
1899 while (optind < argc) {
1900 result |= process(argv[optind++]);
1901 }
1902
1903 return (result != 0);
1904 }
1905