2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Access .dex (Dalvik Executable Format) files. The code here assumes that
19 * the DEX file has been rewritten (byte-swapped, word-aligned) and that
20 * the contents can be directly accessed as a collection of C arrays. Please
21 * see docs/dalvik/dex-format.html for a detailed description.
23 * The structure and field names were chosen to match those in the DEX spec.
25 * It's generally assumed that the DEX file will be stored in shared memory,
26 * obviating the need to copy code and constant pool entries into newly
27 * allocated storage. Maintaining local pointers to items in the shared area
28 * is valid and encouraged.
30 * All memory-mapped structures are 32-bit aligned unless otherwise noted.
33 #ifndef _LIBDEX_DEXFILE
34 #define _LIBDEX_DEXFILE
36 #include "vm/Common.h" // basic type defs, e.g. u1/u2/u4/u8, and LOG
37 #include "libdex/SysUtil.h"
40 * gcc-style inline management -- ensures we have a copy of all functions
41 * in the library, so code that links against us will work whether or not
42 * it was built with optimizations enabled.
44 #ifndef _DEX_GEN_INLINES /* only defined by DexInlines.c */
45 # define DEX_INLINE extern __inline__
50 /* DEX file magic number */
51 #define DEX_MAGIC "dex\n"
52 /* version, encoded in 4 bytes of ASCII */
53 #define DEX_MAGIC_VERS "035\0"
55 /* same, but for optimized DEX header */
56 #define DEX_OPT_MAGIC "dey\n"
57 #define DEX_OPT_MAGIC_VERS "036\0"
59 #define DEX_DEP_MAGIC "deps"
62 * 160-bit SHA-1 digest.
64 enum { kSHA1DigestLen = 20,
65 kSHA1DigestOutputLen = kSHA1DigestLen*2 +1 };
67 /* general constants */
69 kDexEndianConstant = 0x12345678, /* the endianness indicator */
70 kDexNoIndex = 0xffffffff, /* not a valid index value */
74 * Enumeration of all the primitive types.
76 typedef enum PrimitiveType {
77 PRIM_NOT = 0, /* value is a reference type, not a primitive type */
90 * access flags and masks; the "standard" ones are all <= 0x4000
92 * Note: There are related declarations in vm/oo/Object.h in the ClassFlags
96 ACC_PUBLIC = 0x00000001, // class, field, method, ic
97 ACC_PRIVATE = 0x00000002, // field, method, ic
98 ACC_PROTECTED = 0x00000004, // field, method, ic
99 ACC_STATIC = 0x00000008, // field, method, ic
100 ACC_FINAL = 0x00000010, // class, field, method, ic
101 ACC_SYNCHRONIZED = 0x00000020, // method (only allowed on natives)
102 ACC_SUPER = 0x00000020, // class (not used in Dalvik)
103 ACC_VOLATILE = 0x00000040, // field
104 ACC_BRIDGE = 0x00000040, // method (1.5)
105 ACC_TRANSIENT = 0x00000080, // field
106 ACC_VARARGS = 0x00000080, // method (1.5)
107 ACC_NATIVE = 0x00000100, // method
108 ACC_INTERFACE = 0x00000200, // class, ic
109 ACC_ABSTRACT = 0x00000400, // class, method, ic
110 ACC_STRICT = 0x00000800, // method
111 ACC_SYNTHETIC = 0x00001000, // field, method, ic
112 ACC_ANNOTATION = 0x00002000, // class, ic (1.5)
113 ACC_ENUM = 0x00004000, // class, field, ic (1.5)
114 ACC_CONSTRUCTOR = 0x00010000, // method (Dalvik only)
115 ACC_DECLARED_SYNCHRONIZED =
116 0x00020000, // method (Dalvik only)
118 (ACC_PUBLIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT
119 | ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM),
120 ACC_INNER_CLASS_MASK =
121 (ACC_CLASS_MASK | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC),
123 (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL
124 | ACC_VOLATILE | ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM),
126 (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL
127 | ACC_SYNCHRONIZED | ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE
128 | ACC_ABSTRACT | ACC_STRICT | ACC_SYNTHETIC | ACC_CONSTRUCTOR
129 | ACC_DECLARED_SYNCHRONIZED),
132 /* annotation constants */
134 kDexVisibilityBuild = 0x00, /* annotation visibility */
135 kDexVisibilityRuntime = 0x01,
136 kDexVisibilitySystem = 0x02,
138 kDexAnnotationByte = 0x00,
139 kDexAnnotationShort = 0x02,
140 kDexAnnotationChar = 0x03,
141 kDexAnnotationInt = 0x04,
142 kDexAnnotationLong = 0x06,
143 kDexAnnotationFloat = 0x10,
144 kDexAnnotationDouble = 0x11,
145 kDexAnnotationString = 0x17,
146 kDexAnnotationType = 0x18,
147 kDexAnnotationField = 0x19,
148 kDexAnnotationMethod = 0x1a,
149 kDexAnnotationEnum = 0x1b,
150 kDexAnnotationArray = 0x1c,
151 kDexAnnotationAnnotation = 0x1d,
152 kDexAnnotationNull = 0x1e,
153 kDexAnnotationBoolean = 0x1f,
155 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
156 kDexAnnotationValueArgShift = 5,
159 /* map item type codes */
161 kDexTypeHeaderItem = 0x0000,
162 kDexTypeStringIdItem = 0x0001,
163 kDexTypeTypeIdItem = 0x0002,
164 kDexTypeProtoIdItem = 0x0003,
165 kDexTypeFieldIdItem = 0x0004,
166 kDexTypeMethodIdItem = 0x0005,
167 kDexTypeClassDefItem = 0x0006,
168 kDexTypeMapList = 0x1000,
169 kDexTypeTypeList = 0x1001,
170 kDexTypeAnnotationSetRefList = 0x1002,
171 kDexTypeAnnotationSetItem = 0x1003,
172 kDexTypeClassDataItem = 0x2000,
173 kDexTypeCodeItem = 0x2001,
174 kDexTypeStringDataItem = 0x2002,
175 kDexTypeDebugInfoItem = 0x2003,
176 kDexTypeAnnotationItem = 0x2004,
177 kDexTypeEncodedArrayItem = 0x2005,
178 kDexTypeAnnotationsDirectoryItem = 0x2006,
181 /* auxillary data section chunk codes */
183 kDexChunkClassLookup = 0x434c4b50, /* CLKP */
184 kDexChunkRegisterMaps = 0x524d4150, /* RMAP */
186 kDexChunkEnd = 0x41454e44, /* AEND */
189 /* debug info opcodes and constants */
191 DBG_END_SEQUENCE = 0x00,
192 DBG_ADVANCE_PC = 0x01,
193 DBG_ADVANCE_LINE = 0x02,
194 DBG_START_LOCAL = 0x03,
195 DBG_START_LOCAL_EXTENDED = 0x04,
196 DBG_END_LOCAL = 0x05,
197 DBG_RESTART_LOCAL = 0x06,
198 DBG_SET_PROLOGUE_END = 0x07,
199 DBG_SET_EPILOGUE_BEGIN = 0x08,
201 DBG_FIRST_SPECIAL = 0x0a,
207 * Direct-mapped "header_item" struct.
209 typedef struct DexHeader {
210 u1 magic[8]; /* includes version number */
211 u4 checksum; /* adler32 checksum */
212 u1 signature[kSHA1DigestLen]; /* SHA-1 hash */
213 u4 fileSize; /* length of entire file */
214 u4 headerSize; /* offset to start of next section */
236 * Direct-mapped "map_item".
238 typedef struct DexMapItem {
239 u2 type; /* type code (see kDexType* above) */
241 u4 size; /* count of items of the indicated type */
242 u4 offset; /* file offset to the start of data */
246 * Direct-mapped "map_list".
248 typedef struct DexMapList {
249 u4 size; /* #of entries in list */
250 DexMapItem list[1]; /* entries */
254 * Direct-mapped "string_id_item".
256 typedef struct DexStringId {
257 u4 stringDataOff; /* file offset to string_data_item */
261 * Direct-mapped "type_id_item".
263 typedef struct DexTypeId {
264 u4 descriptorIdx; /* index into stringIds list for type descriptor */
268 * Direct-mapped "field_id_item".
270 typedef struct DexFieldId {
271 u2 classIdx; /* index into typeIds list for defining class */
272 u2 typeIdx; /* index into typeIds for field type */
273 u4 nameIdx; /* index into stringIds for field name */
277 * Direct-mapped "method_id_item".
279 typedef struct DexMethodId {
280 u2 classIdx; /* index into typeIds list for defining class */
281 u2 protoIdx; /* index into protoIds for method prototype */
282 u4 nameIdx; /* index into stringIds for method name */
286 * Direct-mapped "proto_id_item".
288 typedef struct DexProtoId {
289 u4 shortyIdx; /* index into stringIds for shorty descriptor */
290 u4 returnTypeIdx; /* index into typeIds list for return type */
291 u4 parametersOff; /* file offset to type_list for parameter types */
295 * Direct-mapped "class_def_item".
297 typedef struct DexClassDef {
298 u4 classIdx; /* index into typeIds for this class */
300 u4 superclassIdx; /* index into typeIds for superclass */
301 u4 interfacesOff; /* file offset to DexTypeList */
302 u4 sourceFileIdx; /* index into stringIds for source file name */
303 u4 annotationsOff; /* file offset to annotations_directory_item */
304 u4 classDataOff; /* file offset to class_data_item */
305 u4 staticValuesOff; /* file offset to DexEncodedArray */
309 * Direct-mapped "type_item".
311 typedef struct DexTypeItem {
312 u2 typeIdx; /* index into typeIds */
316 * Direct-mapped "type_list".
318 typedef struct DexTypeList {
319 u4 size; /* #of entries in list */
320 DexTypeItem list[1]; /* entries */
324 * Direct-mapped "code_item".
326 * The "catches" table is used when throwing an exception,
327 * "debugInfo" is used when displaying an exception stack trace or
328 * debugging. An offset of zero indicates that there are no entries.
330 typedef struct DexCode {
335 u4 debugInfoOff; /* file offset to debug info stream */
336 u4 insnsSize; /* size of the insns array, in u2 units */
338 /* followed by optional u2 padding */
339 /* followed by try_item[triesSize] */
340 /* followed by uleb128 handlersSize */
341 /* followed by catch_handler_item[handlersSize] */
345 * Direct-mapped "try_item".
347 typedef struct DexTry {
348 u4 startAddr; /* start address, in 16-bit code units */
349 u2 insnCount; /* instruction count, in 16-bit code units */
350 u2 handlerOff; /* offset in encoded handler data to handlers */
354 * Link table. Currently undefined.
356 typedef struct DexLink {
362 * Direct-mapped "annotations_directory_item".
364 typedef struct DexAnnotationsDirectoryItem {
365 u4 classAnnotationsOff; /* offset to DexAnnotationSetItem */
366 u4 fieldsSize; /* count of DexFieldAnnotationsItem */
367 u4 methodsSize; /* count of DexMethodAnnotationsItem */
368 u4 parametersSize; /* count of DexParameterAnnotationsItem */
369 /* followed by DexFieldAnnotationsItem[fieldsSize] */
370 /* followed by DexMethodAnnotationsItem[methodsSize] */
371 /* followed by DexParameterAnnotationsItem[parametersSize] */
372 } DexAnnotationsDirectoryItem;
375 * Direct-mapped "field_annotations_item".
377 typedef struct DexFieldAnnotationsItem {
379 u4 annotationsOff; /* offset to DexAnnotationSetItem */
380 } DexFieldAnnotationsItem;
383 * Direct-mapped "method_annotations_item".
385 typedef struct DexMethodAnnotationsItem {
387 u4 annotationsOff; /* offset to DexAnnotationSetItem */
388 } DexMethodAnnotationsItem;
391 * Direct-mapped "parameter_annotations_item".
393 typedef struct DexParameterAnnotationsItem {
395 u4 annotationsOff; /* offset to DexAnotationSetRefList */
396 } DexParameterAnnotationsItem;
399 * Direct-mapped "annotation_set_ref_item".
401 typedef struct DexAnnotationSetRefItem {
402 u4 annotationsOff; /* offset to DexAnnotationSetItem */
403 } DexAnnotationSetRefItem;
406 * Direct-mapped "annotation_set_ref_list".
408 typedef struct DexAnnotationSetRefList {
410 DexAnnotationSetRefItem list[1];
411 } DexAnnotationSetRefList;
414 * Direct-mapped "anotation_set_item".
416 typedef struct DexAnnotationSetItem {
418 u4 entries[1]; /* offset to DexAnnotationItem */
419 } DexAnnotationSetItem;
422 * Direct-mapped "annotation_item".
424 * NOTE: this structure is byte-aligned.
426 typedef struct DexAnnotationItem {
428 u1 annotation[1]; /* data in encoded_annotation format */
432 * Direct-mapped "encoded_array".
434 * NOTE: this structure is byte-aligned.
436 typedef struct DexEncodedArray {
437 u1 array[1]; /* data in encoded_array format */
441 * Lookup table for classes. It provides a mapping from class name to
442 * class definition. Used by dexFindClass().
444 * We calculate this at DEX optimization time and embed it in the file so we
445 * don't need the same hash table in every VM. This is slightly slower than
446 * a hash table with direct pointers to the items, but because it's shared
447 * there's less of a penalty for using a fairly sparse table.
449 typedef struct DexClassLookup {
450 int size; // total size, including "size"
451 int numEntries; // size of table[]; always power of 2
453 u4 classDescriptorHash; // class descriptor hash code
454 int classDescriptorOffset; // in bytes, from start of DEX
455 int classDefOffset; // in bytes, from start of DEX
460 * Header added by DEX optimization pass. Values are always written in
461 * local byte and structure padding. The first field (magic + version)
462 * is guaranteed to be present and directly readable for all expected
463 * compiler configurations; the rest is version-dependent.
465 * Try to keep this simple and fixed-size.
467 typedef struct DexOptHeader {
468 u1 magic[8]; /* includes version number */
470 u4 dexOffset; /* file offset of DEX header */
472 u4 depsOffset; /* offset of optimized DEX dependency table */
474 u4 optOffset; /* file offset of optimized data tables */
477 u4 flags; /* some info flags */
478 u4 checksum; /* adler32 checksum covering deps/opt */
480 /* pad for 64-bit alignment if necessary */
483 #define DEX_OPT_FLAG_BIG (1<<1) /* swapped to big-endian */
485 #define DEX_INTERFACE_CACHE_SIZE 128 /* must be power of 2 */
488 * Structure representing a DEX file.
490 * Code should regard DexFile as opaque, using the API calls provided here
491 * to access specific structures.
493 typedef struct DexFile {
494 /* directly-mapped "opt" header */
495 const DexOptHeader* pOptHeader;
497 /* pointers to directly-mapped structs and arrays in base DEX */
498 const DexHeader* pHeader;
499 const DexStringId* pStringIds;
500 const DexTypeId* pTypeIds;
501 const DexFieldId* pFieldIds;
502 const DexMethodId* pMethodIds;
503 const DexProtoId* pProtoIds;
504 const DexClassDef* pClassDefs;
505 const DexLink* pLinkData;
508 * These are mapped out of the "auxillary" section, and may not be
509 * included in the file.
511 const DexClassLookup* pClassLookup;
512 const void* pRegisterMapPool; // RegisterMapClassPool
514 /* points to start of DEX file data */
517 /* track memory overhead for auxillary structures */
520 /* additional app-specific data structures associated with the DEX */
525 * Utility function -- rounds up to the nearest power of 2.
527 u4 dexRoundUpPower2(u4 val);
530 * Parse an optimized or unoptimized .dex file sitting in memory.
532 * On success, return a newly-allocated DexFile.
534 DexFile* dexFileParse(const u1* data, size_t length, int flags);
536 /* bit values for "flags" argument to dexFileParse */
538 kDexParseDefault = 0,
539 kDexParseVerifyChecksum = 1,
540 kDexParseContinueOnError = (1 << 1),
544 * Fix the byte ordering of all fields in the DEX file, and do
545 * structural verification. This is only required for code that opens
546 * "raw" DEX files, such as the DEX optimizer.
548 * Return 0 on success.
550 int dexSwapAndVerify(u1* addr, int len);
553 * Detect the file type of the given memory buffer via magic number.
554 * Call dexSwapAndVerify() on an unoptimized DEX file, do nothing
555 * but return successfully on an optimized DEX file, and report an
556 * error for all other cases.
558 * Return 0 on success.
560 int dexSwapAndVerifyIfNecessary(u1* addr, int len);
563 * Compute DEX checksum.
565 u4 dexComputeChecksum(const DexHeader* pHeader);
568 * Free a DexFile structure, along with any associated structures.
570 void dexFileFree(DexFile* pDexFile);
573 * Create class lookup table.
575 DexClassLookup* dexCreateClassLookup(DexFile* pDexFile);
578 * Find a class definition by descriptor.
580 const DexClassDef* dexFindClass(const DexFile* pFile, const char* descriptor);
583 * Set up the basic raw data pointers of a DexFile. This function isn't
584 * meant for general use.
586 void dexFileSetupBasicPointers(DexFile* pDexFile, const u1* data);
588 /* return the DexMapList of the file, if any */
589 DEX_INLINE const DexMapList* dexGetMap(const DexFile* pDexFile) {
590 u4 mapOff = pDexFile->pHeader->mapOff;
595 return (const DexMapList*) (pDexFile->baseAddr + mapOff);
599 /* return the const char* string data referred to by the given string_id */
600 DEX_INLINE const char* dexGetStringData(const DexFile* pDexFile,
601 const DexStringId* pStringId) {
602 const u1* ptr = pDexFile->baseAddr + pStringId->stringDataOff;
604 // Skip the uleb128 length.
605 while (*(ptr++) > 0x7f) /* empty */ ;
607 return (const char*) ptr;
609 /* return the StringId with the specified index */
610 DEX_INLINE const DexStringId* dexGetStringId(const DexFile* pDexFile, u4 idx) {
611 assert(idx < pDexFile->pHeader->stringIdsSize);
612 return &pDexFile->pStringIds[idx];
614 /* return the UTF-8 encoded string with the specified string_id index */
615 DEX_INLINE const char* dexStringById(const DexFile* pDexFile, u4 idx) {
616 const DexStringId* pStringId = dexGetStringId(pDexFile, idx);
617 return dexGetStringData(pDexFile, pStringId);
620 /* Return the UTF-8 encoded string with the specified string_id index,
621 * also filling in the UTF-16 size (number of 16-bit code points).*/
622 const char* dexStringAndSizeById(const DexFile* pDexFile, u4 idx,
625 /* return the TypeId with the specified index */
626 DEX_INLINE const DexTypeId* dexGetTypeId(const DexFile* pDexFile, u4 idx) {
627 assert(idx < pDexFile->pHeader->typeIdsSize);
628 return &pDexFile->pTypeIds[idx];
632 * Get the descriptor string associated with a given type index.
633 * The caller should not free() the returned string.
635 DEX_INLINE const char* dexStringByTypeIdx(const DexFile* pDexFile, u4 idx) {
636 const DexTypeId* typeId = dexGetTypeId(pDexFile, idx);
637 return dexStringById(pDexFile, typeId->descriptorIdx);
640 /* return the MethodId with the specified index */
641 DEX_INLINE const DexMethodId* dexGetMethodId(const DexFile* pDexFile, u4 idx) {
642 assert(idx < pDexFile->pHeader->methodIdsSize);
643 return &pDexFile->pMethodIds[idx];
646 /* return the FieldId with the specified index */
647 DEX_INLINE const DexFieldId* dexGetFieldId(const DexFile* pDexFile, u4 idx) {
648 assert(idx < pDexFile->pHeader->fieldIdsSize);
649 return &pDexFile->pFieldIds[idx];
652 /* return the ProtoId with the specified index */
653 DEX_INLINE const DexProtoId* dexGetProtoId(const DexFile* pDexFile, u4 idx) {
654 assert(idx < pDexFile->pHeader->protoIdsSize);
655 return &pDexFile->pProtoIds[idx];
659 * Get the parameter list from a ProtoId. The returns NULL if the ProtoId
660 * does not have a parameter list.
662 DEX_INLINE const DexTypeList* dexGetProtoParameters(
663 const DexFile *pDexFile, const DexProtoId* pProtoId) {
664 if (pProtoId->parametersOff == 0) {
667 return (const DexTypeList*)
668 (pDexFile->baseAddr + pProtoId->parametersOff);
671 /* return the ClassDef with the specified index */
672 DEX_INLINE const DexClassDef* dexGetClassDef(const DexFile* pDexFile, u4 idx) {
673 assert(idx < pDexFile->pHeader->classDefsSize);
674 return &pDexFile->pClassDefs[idx];
677 /* given a ClassDef pointer, recover its index */
678 DEX_INLINE u4 dexGetIndexForClassDef(const DexFile* pDexFile,
679 const DexClassDef* pClassDef)
681 assert(pClassDef >= pDexFile->pClassDefs &&
682 pClassDef < pDexFile->pClassDefs + pDexFile->pHeader->classDefsSize);
683 return pClassDef - pDexFile->pClassDefs;
686 /* get the interface list for a DexClass */
687 DEX_INLINE const DexTypeList* dexGetInterfacesList(const DexFile* pDexFile,
688 const DexClassDef* pClassDef)
690 if (pClassDef->interfacesOff == 0)
692 return (const DexTypeList*)
693 (pDexFile->baseAddr + pClassDef->interfacesOff);
695 /* return the Nth entry in a DexTypeList. */
696 DEX_INLINE const DexTypeItem* dexGetTypeItem(const DexTypeList* pList,
699 assert(idx < pList->size);
700 return &pList->list[idx];
702 /* return the type_idx for the Nth entry in a TypeList */
703 DEX_INLINE u4 dexTypeListGetIdx(const DexTypeList* pList, u4 idx) {
704 const DexTypeItem* pItem = dexGetTypeItem(pList, idx);
705 return pItem->typeIdx;
708 /* get the static values list for a DexClass */
709 DEX_INLINE const DexEncodedArray* dexGetStaticValuesList(
710 const DexFile* pDexFile, const DexClassDef* pClassDef)
712 if (pClassDef->staticValuesOff == 0)
714 return (const DexEncodedArray*)
715 (pDexFile->baseAddr + pClassDef->staticValuesOff);
718 /* get the annotations directory item for a DexClass */
719 DEX_INLINE const DexAnnotationsDirectoryItem* dexGetAnnotationsDirectoryItem(
720 const DexFile* pDexFile, const DexClassDef* pClassDef)
722 if (pClassDef->annotationsOff == 0)
724 return (const DexAnnotationsDirectoryItem*)
725 (pDexFile->baseAddr + pClassDef->annotationsOff);
728 /* get the source file string */
729 DEX_INLINE const char* dexGetSourceFile(
730 const DexFile* pDexFile, const DexClassDef* pClassDef)
732 if (pClassDef->sourceFileIdx == 0xffffffff)
734 return dexStringById(pDexFile, pClassDef->sourceFileIdx);
737 /* get the size, in bytes, of a DexCode */
738 size_t dexGetDexCodeSize(const DexCode* pCode);
740 /* Get the list of "tries" for the given DexCode. */
741 DEX_INLINE const DexTry* dexGetTries(const DexCode* pCode) {
742 const u2* insnsEnd = &pCode->insns[pCode->insnsSize];
744 // Round to four bytes.
745 if ((((u4) insnsEnd) & 3) != 0) {
749 return (const DexTry*) insnsEnd;
752 /* Get the base of the encoded data for the given DexCode. */
753 DEX_INLINE const u1* dexGetCatchHandlerData(const DexCode* pCode) {
754 const DexTry* pTries = dexGetTries(pCode);
755 return (const u1*) &pTries[pCode->triesSize];
758 /* get a pointer to the start of the debugging data */
759 DEX_INLINE const u1* dexGetDebugInfoStream(const DexFile* pDexFile,
760 const DexCode* pCode)
762 if (pCode->debugInfoOff == 0) {
765 return pDexFile->baseAddr + pCode->debugInfoOff;
769 /* DexClassDef convenience - get class descriptor */
770 DEX_INLINE const char* dexGetClassDescriptor(const DexFile* pDexFile,
771 const DexClassDef* pClassDef)
773 return dexStringByTypeIdx(pDexFile, pClassDef->classIdx);
776 /* DexClassDef convenience - get superclass descriptor */
777 DEX_INLINE const char* dexGetSuperClassDescriptor(const DexFile* pDexFile,
778 const DexClassDef* pClassDef)
780 if (pClassDef->superclassIdx == 0)
782 return dexStringByTypeIdx(pDexFile, pClassDef->superclassIdx);
785 /* DexClassDef convenience - get class_data_item pointer */
786 DEX_INLINE const u1* dexGetClassData(const DexFile* pDexFile,
787 const DexClassDef* pClassDef)
789 if (pClassDef->classDataOff == 0)
791 return (const u1*) (pDexFile->baseAddr + pClassDef->classDataOff);
794 /* Get an annotation set at a particular offset. */
795 DEX_INLINE const DexAnnotationSetItem* dexGetAnnotationSetItem(
796 const DexFile* pDexFile, u4 offset)
798 return (const DexAnnotationSetItem*) (pDexFile->baseAddr + offset);
800 /* get the class' annotation set */
801 DEX_INLINE const DexAnnotationSetItem* dexGetClassAnnotationSet(
802 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
804 if (pAnnoDir->classAnnotationsOff == 0)
806 return dexGetAnnotationSetItem(pDexFile, pAnnoDir->classAnnotationsOff);
809 /* get the class' field annotation list */
810 DEX_INLINE const DexFieldAnnotationsItem* dexGetFieldAnnotations(
811 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
813 if (pAnnoDir->fieldsSize == 0)
816 // Skip past the header to the start of the field annotations.
817 return (const DexFieldAnnotationsItem*) &pAnnoDir[1];
820 /* get field annotation list size */
821 DEX_INLINE int dexGetFieldAnnotationsSize(const DexFile* pDexFile,
822 const DexAnnotationsDirectoryItem* pAnnoDir)
824 return pAnnoDir->fieldsSize;
827 /* return a pointer to the field's annotation set */
828 DEX_INLINE const DexAnnotationSetItem* dexGetFieldAnnotationSetItem(
829 const DexFile* pDexFile, const DexFieldAnnotationsItem* pItem)
831 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
834 /* get the class' method annotation list */
835 DEX_INLINE const DexMethodAnnotationsItem* dexGetMethodAnnotations(
836 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
838 if (pAnnoDir->methodsSize == 0)
842 * Skip past the header and field annotations to the start of the
843 * method annotations.
845 const u1* addr = (const u1*) &pAnnoDir[1];
846 addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
847 return (const DexMethodAnnotationsItem*) addr;
850 /* get method annotation list size */
851 DEX_INLINE int dexGetMethodAnnotationsSize(const DexFile* pDexFile,
852 const DexAnnotationsDirectoryItem* pAnnoDir)
854 return pAnnoDir->methodsSize;
857 /* return a pointer to the method's annotation set */
858 DEX_INLINE const DexAnnotationSetItem* dexGetMethodAnnotationSetItem(
859 const DexFile* pDexFile, const DexMethodAnnotationsItem* pItem)
861 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
864 /* get the class' parameter annotation list */
865 DEX_INLINE const DexParameterAnnotationsItem* dexGetParameterAnnotations(
866 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
868 if (pAnnoDir->parametersSize == 0)
872 * Skip past the header, field annotations, and method annotations
873 * to the start of the parameter annotations.
875 const u1* addr = (const u1*) &pAnnoDir[1];
876 addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
877 addr += pAnnoDir->methodsSize * sizeof (DexMethodAnnotationsItem);
878 return (const DexParameterAnnotationsItem*) addr;
881 /* get method annotation list size */
882 DEX_INLINE int dexGetParameterAnnotationsSize(const DexFile* pDexFile,
883 const DexAnnotationsDirectoryItem* pAnnoDir)
885 return pAnnoDir->parametersSize;
888 /* return the parameter annotation ref list */
889 DEX_INLINE const DexAnnotationSetRefList* dexGetParameterAnnotationSetRefList(
890 const DexFile* pDexFile, const DexParameterAnnotationsItem* pItem)
892 return (const DexAnnotationSetRefList*)
893 (pDexFile->baseAddr + pItem->annotationsOff);
896 /* get method annotation list size */
897 DEX_INLINE int dexGetParameterAnnotationSetRefSize(const DexFile* pDexFile,
898 const DexParameterAnnotationsItem* pItem)
900 if (pItem->annotationsOff == 0)
902 return dexGetParameterAnnotationSetRefList(pDexFile, pItem)->size;
905 /* return the Nth entry from an annotation set ref list */
906 DEX_INLINE const DexAnnotationSetRefItem* dexGetParameterAnnotationSetRef(
907 const DexAnnotationSetRefList* pList, u4 idx)
909 assert(idx < pList->size);
910 return &pList->list[idx];
913 /* given a DexAnnotationSetRefItem, return the DexAnnotationSetItem */
914 DEX_INLINE const DexAnnotationSetItem* dexGetSetRefItemItem(
915 const DexFile* pDexFile, const DexAnnotationSetRefItem* pItem)
917 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
920 /* return the Nth annotation offset from a DexAnnotationSetItem */
921 DEX_INLINE u4 dexGetAnnotationOff(
922 const DexAnnotationSetItem* pAnnoSet, u4 idx)
924 assert(idx < pAnnoSet->size);
925 return pAnnoSet->entries[idx];
928 /* return the Nth annotation item from a DexAnnotationSetItem */
929 DEX_INLINE const DexAnnotationItem* dexGetAnnotationItem(
930 const DexFile* pDexFile, const DexAnnotationSetItem* pAnnoSet, u4 idx)
932 return (const DexAnnotationItem*)
933 (pDexFile->baseAddr + dexGetAnnotationOff(pAnnoSet, idx));
937 * Get the type descriptor character associated with a given primitive
938 * type. This returns '\0' if the type is invalid.
940 char dexGetPrimitiveTypeDescriptorChar(PrimitiveType type);
943 * Get the type descriptor string associated with a given primitive
946 const char* dexGetPrimitiveTypeDescriptor(PrimitiveType type);
949 * Get the boxed type descriptor string associated with a given
950 * primitive type. This returns NULL for an invalid type, including
951 * particularly for type "void". In the latter case, even though there
952 * is a class Void, there's no such thing as a boxed instance of it.
954 const char* dexGetBoxedTypeDescriptor(PrimitiveType type);
957 * Get the primitive type constant from the given descriptor character.
958 * This returns PRIM_NOT (note: this is a 0) if the character is invalid
959 * as a primitive type descriptor.
961 PrimitiveType dexGetPrimitiveTypeFromDescriptorChar(char descriptorChar);
963 #endif /*_LIBDEX_DEXFILE*/