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"
44 * gcc-style inline management -- ensures we have a copy of all functions
45 * in the library, so code that links against us will work whether or not
46 * it was built with optimizations enabled.
48 #ifndef _DEX_GEN_INLINES /* only defined by DexInlines.c */
49 # define DEX_INLINE extern __inline__
54 /* DEX file magic number */
55 #define DEX_MAGIC "dex\n"
56 /* version, encoded in 4 bytes of ASCII */
57 #define DEX_MAGIC_VERS "035\0"
59 /* same, but for optimized DEX header */
60 #define DEX_OPT_MAGIC "dey\n"
61 #define DEX_OPT_MAGIC_VERS "036\0"
63 #define DEX_DEP_MAGIC "deps"
66 * 160-bit SHA-1 digest.
68 enum { kSHA1DigestLen = 20,
69 kSHA1DigestOutputLen = kSHA1DigestLen*2 +1 };
71 /* general constants */
73 kDexEndianConstant = 0x12345678, /* the endianness indicator */
74 kDexNoIndex = 0xffffffff, /* not a valid index value */
78 * Enumeration of all the primitive types.
80 typedef enum PrimitiveType {
81 PRIM_NOT = 0, /* value is a reference type, not a primitive type */
94 * access flags and masks; the "standard" ones are all <= 0x4000
96 * Note: There are related declarations in vm/oo/Object.h in the ClassFlags
100 ACC_PUBLIC = 0x00000001, // class, field, method, ic
101 ACC_PRIVATE = 0x00000002, // field, method, ic
102 ACC_PROTECTED = 0x00000004, // field, method, ic
103 ACC_STATIC = 0x00000008, // field, method, ic
104 ACC_FINAL = 0x00000010, // class, field, method, ic
105 ACC_SYNCHRONIZED = 0x00000020, // method (only allowed on natives)
106 ACC_SUPER = 0x00000020, // class (not used in Dalvik)
107 ACC_VOLATILE = 0x00000040, // field
108 ACC_BRIDGE = 0x00000040, // method (1.5)
109 ACC_TRANSIENT = 0x00000080, // field
110 ACC_VARARGS = 0x00000080, // method (1.5)
111 ACC_NATIVE = 0x00000100, // method
112 ACC_INTERFACE = 0x00000200, // class, ic
113 ACC_ABSTRACT = 0x00000400, // class, method, ic
114 ACC_STRICT = 0x00000800, // method
115 ACC_SYNTHETIC = 0x00001000, // field, method, ic
116 ACC_ANNOTATION = 0x00002000, // class, ic (1.5)
117 ACC_ENUM = 0x00004000, // class, field, ic (1.5)
118 ACC_CONSTRUCTOR = 0x00010000, // method (Dalvik only)
119 ACC_DECLARED_SYNCHRONIZED =
120 0x00020000, // method (Dalvik only)
122 (ACC_PUBLIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT
123 | ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM),
124 ACC_INNER_CLASS_MASK =
125 (ACC_CLASS_MASK | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC),
127 (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL
128 | ACC_VOLATILE | ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM),
130 (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL
131 | ACC_SYNCHRONIZED | ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE
132 | ACC_ABSTRACT | ACC_STRICT | ACC_SYNTHETIC | ACC_CONSTRUCTOR
133 | ACC_DECLARED_SYNCHRONIZED),
136 /* annotation constants */
138 kDexVisibilityBuild = 0x00, /* annotation visibility */
139 kDexVisibilityRuntime = 0x01,
140 kDexVisibilitySystem = 0x02,
142 kDexAnnotationByte = 0x00,
143 kDexAnnotationShort = 0x02,
144 kDexAnnotationChar = 0x03,
145 kDexAnnotationInt = 0x04,
146 kDexAnnotationLong = 0x06,
147 kDexAnnotationFloat = 0x10,
148 kDexAnnotationDouble = 0x11,
149 kDexAnnotationString = 0x17,
150 kDexAnnotationType = 0x18,
151 kDexAnnotationField = 0x19,
152 kDexAnnotationMethod = 0x1a,
153 kDexAnnotationEnum = 0x1b,
154 kDexAnnotationArray = 0x1c,
155 kDexAnnotationAnnotation = 0x1d,
156 kDexAnnotationNull = 0x1e,
157 kDexAnnotationBoolean = 0x1f,
159 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
160 kDexAnnotationValueArgShift = 5,
163 /* map item type codes */
165 kDexTypeHeaderItem = 0x0000,
166 kDexTypeStringIdItem = 0x0001,
167 kDexTypeTypeIdItem = 0x0002,
168 kDexTypeProtoIdItem = 0x0003,
169 kDexTypeFieldIdItem = 0x0004,
170 kDexTypeMethodIdItem = 0x0005,
171 kDexTypeClassDefItem = 0x0006,
172 kDexTypeMapList = 0x1000,
173 kDexTypeTypeList = 0x1001,
174 kDexTypeAnnotationSetRefList = 0x1002,
175 kDexTypeAnnotationSetItem = 0x1003,
176 kDexTypeClassDataItem = 0x2000,
177 kDexTypeCodeItem = 0x2001,
178 kDexTypeStringDataItem = 0x2002,
179 kDexTypeDebugInfoItem = 0x2003,
180 kDexTypeAnnotationItem = 0x2004,
181 kDexTypeEncodedArrayItem = 0x2005,
182 kDexTypeAnnotationsDirectoryItem = 0x2006,
185 /* auxillary data section chunk codes */
187 kDexChunkClassLookup = 0x434c4b50, /* CLKP */
188 kDexChunkRegisterMaps = 0x524d4150, /* RMAP */
190 kDexChunkEnd = 0x41454e44, /* AEND */
193 /* debug info opcodes and constants */
195 DBG_END_SEQUENCE = 0x00,
196 DBG_ADVANCE_PC = 0x01,
197 DBG_ADVANCE_LINE = 0x02,
198 DBG_START_LOCAL = 0x03,
199 DBG_START_LOCAL_EXTENDED = 0x04,
200 DBG_END_LOCAL = 0x05,
201 DBG_RESTART_LOCAL = 0x06,
202 DBG_SET_PROLOGUE_END = 0x07,
203 DBG_SET_EPILOGUE_BEGIN = 0x08,
205 DBG_FIRST_SPECIAL = 0x0a,
211 * Direct-mapped "header_item" struct.
213 typedef struct DexHeader {
214 u1 magic[8]; /* includes version number */
215 u4 checksum; /* adler32 checksum */
216 u1 signature[kSHA1DigestLen]; /* SHA-1 hash */
217 u4 fileSize; /* length of entire file */
218 u4 headerSize; /* offset to start of next section */
240 * Direct-mapped "map_item".
242 typedef struct DexMapItem {
243 u2 type; /* type code (see kDexType* above) */
245 u4 size; /* count of items of the indicated type */
246 u4 offset; /* file offset to the start of data */
250 * Direct-mapped "map_list".
252 typedef struct DexMapList {
253 u4 size; /* #of entries in list */
254 DexMapItem list[1]; /* entries */
258 * Direct-mapped "string_id_item".
260 typedef struct DexStringId {
261 u4 stringDataOff; /* file offset to string_data_item */
265 * Direct-mapped "type_id_item".
267 typedef struct DexTypeId {
268 u4 descriptorIdx; /* index into stringIds list for type descriptor */
272 * Direct-mapped "field_id_item".
274 typedef struct DexFieldId {
275 u2 classIdx; /* index into typeIds list for defining class */
276 u2 typeIdx; /* index into typeIds for field type */
277 u4 nameIdx; /* index into stringIds for field name */
281 * Direct-mapped "method_id_item".
283 typedef struct DexMethodId {
284 u2 classIdx; /* index into typeIds list for defining class */
285 u2 protoIdx; /* index into protoIds for method prototype */
286 u4 nameIdx; /* index into stringIds for method name */
290 * Direct-mapped "proto_id_item".
292 typedef struct DexProtoId {
293 u4 shortyIdx; /* index into stringIds for shorty descriptor */
294 u4 returnTypeIdx; /* index into typeIds list for return type */
295 u4 parametersOff; /* file offset to type_list for parameter types */
299 * Direct-mapped "class_def_item".
301 typedef struct DexClassDef {
302 u4 classIdx; /* index into typeIds for this class */
304 u4 superclassIdx; /* index into typeIds for superclass */
305 u4 interfacesOff; /* file offset to DexTypeList */
306 u4 sourceFileIdx; /* index into stringIds for source file name */
307 u4 annotationsOff; /* file offset to annotations_directory_item */
308 u4 classDataOff; /* file offset to class_data_item */
309 u4 staticValuesOff; /* file offset to DexEncodedArray */
313 * Direct-mapped "type_item".
315 typedef struct DexTypeItem {
316 u2 typeIdx; /* index into typeIds */
320 * Direct-mapped "type_list".
322 typedef struct DexTypeList {
323 u4 size; /* #of entries in list */
324 DexTypeItem list[1]; /* entries */
328 * Direct-mapped "code_item".
330 * The "catches" table is used when throwing an exception,
331 * "debugInfo" is used when displaying an exception stack trace or
332 * debugging. An offset of zero indicates that there are no entries.
334 typedef struct DexCode {
339 u4 debugInfoOff; /* file offset to debug info stream */
340 u4 insnsSize; /* size of the insns array, in u2 units */
342 /* followed by optional u2 padding */
343 /* followed by try_item[triesSize] */
344 /* followed by uleb128 handlersSize */
345 /* followed by catch_handler_item[handlersSize] */
349 * Direct-mapped "try_item".
351 typedef struct DexTry {
352 u4 startAddr; /* start address, in 16-bit code units */
353 u2 insnCount; /* instruction count, in 16-bit code units */
354 u2 handlerOff; /* offset in encoded handler data to handlers */
358 * Link table. Currently undefined.
360 typedef struct DexLink {
366 * Direct-mapped "annotations_directory_item".
368 typedef struct DexAnnotationsDirectoryItem {
369 u4 classAnnotationsOff; /* offset to DexAnnotationSetItem */
370 u4 fieldsSize; /* count of DexFieldAnnotationsItem */
371 u4 methodsSize; /* count of DexMethodAnnotationsItem */
372 u4 parametersSize; /* count of DexParameterAnnotationsItem */
373 /* followed by DexFieldAnnotationsItem[fieldsSize] */
374 /* followed by DexMethodAnnotationsItem[methodsSize] */
375 /* followed by DexParameterAnnotationsItem[parametersSize] */
376 } DexAnnotationsDirectoryItem;
379 * Direct-mapped "field_annotations_item".
381 typedef struct DexFieldAnnotationsItem {
383 u4 annotationsOff; /* offset to DexAnnotationSetItem */
384 } DexFieldAnnotationsItem;
387 * Direct-mapped "method_annotations_item".
389 typedef struct DexMethodAnnotationsItem {
391 u4 annotationsOff; /* offset to DexAnnotationSetItem */
392 } DexMethodAnnotationsItem;
395 * Direct-mapped "parameter_annotations_item".
397 typedef struct DexParameterAnnotationsItem {
399 u4 annotationsOff; /* offset to DexAnotationSetRefList */
400 } DexParameterAnnotationsItem;
403 * Direct-mapped "annotation_set_ref_item".
405 typedef struct DexAnnotationSetRefItem {
406 u4 annotationsOff; /* offset to DexAnnotationSetItem */
407 } DexAnnotationSetRefItem;
410 * Direct-mapped "annotation_set_ref_list".
412 typedef struct DexAnnotationSetRefList {
414 DexAnnotationSetRefItem list[1];
415 } DexAnnotationSetRefList;
418 * Direct-mapped "annotation_set_item".
420 typedef struct DexAnnotationSetItem {
422 u4 entries[1]; /* offset to DexAnnotationItem */
423 } DexAnnotationSetItem;
426 * Direct-mapped "annotation_item".
428 * NOTE: this structure is byte-aligned.
430 typedef struct DexAnnotationItem {
432 u1 annotation[1]; /* data in encoded_annotation format */
436 * Direct-mapped "encoded_array".
438 * NOTE: this structure is byte-aligned.
440 typedef struct DexEncodedArray {
441 u1 array[1]; /* data in encoded_array format */
445 * Lookup table for classes. It provides a mapping from class name to
446 * class definition. Used by dexFindClass().
448 * We calculate this at DEX optimization time and embed it in the file so we
449 * don't need the same hash table in every VM. This is slightly slower than
450 * a hash table with direct pointers to the items, but because it's shared
451 * there's less of a penalty for using a fairly sparse table.
453 typedef struct DexClassLookup {
454 int size; // total size, including "size"
455 int numEntries; // size of table[]; always power of 2
457 u4 classDescriptorHash; // class descriptor hash code
458 int classDescriptorOffset; // in bytes, from start of DEX
459 int classDefOffset; // in bytes, from start of DEX
464 * Header added by DEX optimization pass. Values are always written in
465 * local byte and structure padding. The first field (magic + version)
466 * is guaranteed to be present and directly readable for all expected
467 * compiler configurations; the rest is version-dependent.
469 * Try to keep this simple and fixed-size.
471 typedef struct DexOptHeader {
472 u1 magic[8]; /* includes version number */
474 u4 dexOffset; /* file offset of DEX header */
476 u4 depsOffset; /* offset of optimized DEX dependency table */
478 u4 optOffset; /* file offset of optimized data tables */
481 u4 flags; /* some info flags */
482 u4 checksum; /* adler32 checksum covering deps/opt */
484 /* pad for 64-bit alignment if necessary */
487 #define DEX_OPT_FLAG_BIG (1<<1) /* swapped to big-endian */
489 #define DEX_INTERFACE_CACHE_SIZE 128 /* must be power of 2 */
492 * Structure representing a DEX file.
494 * Code should regard DexFile as opaque, using the API calls provided here
495 * to access specific structures.
497 typedef struct DexFile {
498 /* directly-mapped "opt" header */
499 const DexOptHeader* pOptHeader;
501 /* pointers to directly-mapped structs and arrays in base DEX */
502 const DexHeader* pHeader;
503 const DexStringId* pStringIds;
504 const DexTypeId* pTypeIds;
505 const DexFieldId* pFieldIds;
506 const DexMethodId* pMethodIds;
507 const DexProtoId* pProtoIds;
508 const DexClassDef* pClassDefs;
509 const DexLink* pLinkData;
512 * These are mapped out of the "auxillary" section, and may not be
513 * included in the file.
515 const DexClassLookup* pClassLookup;
516 const void* pRegisterMapPool; // RegisterMapClassPool
518 /* points to start of DEX file data */
521 /* track memory overhead for auxillary structures */
524 /* additional app-specific data structures associated with the DEX */
529 * Utility function -- rounds up to the nearest power of 2.
531 u4 dexRoundUpPower2(u4 val);
534 * Parse an optimized or unoptimized .dex file sitting in memory.
536 * On success, return a newly-allocated DexFile.
538 DexFile* dexFileParse(const u1* data, size_t length, int flags);
540 /* bit values for "flags" argument to dexFileParse */
542 kDexParseDefault = 0,
543 kDexParseVerifyChecksum = 1,
544 kDexParseContinueOnError = (1 << 1),
548 * Fix the byte ordering of all fields in the DEX file, and do
549 * structural verification. This is only required for code that opens
550 * "raw" DEX files, such as the DEX optimizer.
552 * Return 0 on success.
554 int dexSwapAndVerify(u1* addr, int len);
557 * Detect the file type of the given memory buffer via magic number.
558 * Call dexSwapAndVerify() on an unoptimized DEX file, do nothing
559 * but return successfully on an optimized DEX file, and report an
560 * error for all other cases.
562 * Return 0 on success.
564 int dexSwapAndVerifyIfNecessary(u1* addr, int len);
567 * Compute DEX checksum.
569 u4 dexComputeChecksum(const DexHeader* pHeader);
572 * Free a DexFile structure, along with any associated structures.
574 void dexFileFree(DexFile* pDexFile);
577 * Create class lookup table.
579 DexClassLookup* dexCreateClassLookup(DexFile* pDexFile);
582 * Find a class definition by descriptor.
584 const DexClassDef* dexFindClass(const DexFile* pFile, const char* descriptor);
587 * Set up the basic raw data pointers of a DexFile. This function isn't
588 * meant for general use.
590 void dexFileSetupBasicPointers(DexFile* pDexFile, const u1* data);
592 /* return the DexMapList of the file, if any */
593 DEX_INLINE const DexMapList* dexGetMap(const DexFile* pDexFile) {
594 u4 mapOff = pDexFile->pHeader->mapOff;
599 return (const DexMapList*) (pDexFile->baseAddr + mapOff);
603 /* return the const char* string data referred to by the given string_id */
604 DEX_INLINE const char* dexGetStringData(const DexFile* pDexFile,
605 const DexStringId* pStringId) {
606 const u1* ptr = pDexFile->baseAddr + pStringId->stringDataOff;
608 // Skip the uleb128 length.
609 while (*(ptr++) > 0x7f) /* empty */ ;
611 return (const char*) ptr;
613 /* return the StringId with the specified index */
614 DEX_INLINE const DexStringId* dexGetStringId(const DexFile* pDexFile, u4 idx) {
615 assert(idx < pDexFile->pHeader->stringIdsSize);
616 return &pDexFile->pStringIds[idx];
618 /* return the UTF-8 encoded string with the specified string_id index */
619 DEX_INLINE const char* dexStringById(const DexFile* pDexFile, u4 idx) {
620 const DexStringId* pStringId = dexGetStringId(pDexFile, idx);
621 return dexGetStringData(pDexFile, pStringId);
624 /* Return the UTF-8 encoded string with the specified string_id index,
625 * also filling in the UTF-16 size (number of 16-bit code points).*/
626 const char* dexStringAndSizeById(const DexFile* pDexFile, u4 idx,
629 /* return the TypeId with the specified index */
630 DEX_INLINE const DexTypeId* dexGetTypeId(const DexFile* pDexFile, u4 idx) {
631 assert(idx < pDexFile->pHeader->typeIdsSize);
632 return &pDexFile->pTypeIds[idx];
636 * Get the descriptor string associated with a given type index.
637 * The caller should not free() the returned string.
639 DEX_INLINE const char* dexStringByTypeIdx(const DexFile* pDexFile, u4 idx) {
640 const DexTypeId* typeId = dexGetTypeId(pDexFile, idx);
641 return dexStringById(pDexFile, typeId->descriptorIdx);
644 /* return the MethodId with the specified index */
645 DEX_INLINE const DexMethodId* dexGetMethodId(const DexFile* pDexFile, u4 idx) {
646 assert(idx < pDexFile->pHeader->methodIdsSize);
647 return &pDexFile->pMethodIds[idx];
650 /* return the FieldId with the specified index */
651 DEX_INLINE const DexFieldId* dexGetFieldId(const DexFile* pDexFile, u4 idx) {
652 assert(idx < pDexFile->pHeader->fieldIdsSize);
653 return &pDexFile->pFieldIds[idx];
656 /* return the ProtoId with the specified index */
657 DEX_INLINE const DexProtoId* dexGetProtoId(const DexFile* pDexFile, u4 idx) {
658 assert(idx < pDexFile->pHeader->protoIdsSize);
659 return &pDexFile->pProtoIds[idx];
663 * Get the parameter list from a ProtoId. The returns NULL if the ProtoId
664 * does not have a parameter list.
666 DEX_INLINE const DexTypeList* dexGetProtoParameters(
667 const DexFile *pDexFile, const DexProtoId* pProtoId) {
668 if (pProtoId->parametersOff == 0) {
671 return (const DexTypeList*)
672 (pDexFile->baseAddr + pProtoId->parametersOff);
675 /* return the ClassDef with the specified index */
676 DEX_INLINE const DexClassDef* dexGetClassDef(const DexFile* pDexFile, u4 idx) {
677 assert(idx < pDexFile->pHeader->classDefsSize);
678 return &pDexFile->pClassDefs[idx];
681 /* given a ClassDef pointer, recover its index */
682 DEX_INLINE u4 dexGetIndexForClassDef(const DexFile* pDexFile,
683 const DexClassDef* pClassDef)
685 assert(pClassDef >= pDexFile->pClassDefs &&
686 pClassDef < pDexFile->pClassDefs + pDexFile->pHeader->classDefsSize);
687 return pClassDef - pDexFile->pClassDefs;
690 /* get the interface list for a DexClass */
691 DEX_INLINE const DexTypeList* dexGetInterfacesList(const DexFile* pDexFile,
692 const DexClassDef* pClassDef)
694 if (pClassDef->interfacesOff == 0)
696 return (const DexTypeList*)
697 (pDexFile->baseAddr + pClassDef->interfacesOff);
699 /* return the Nth entry in a DexTypeList. */
700 DEX_INLINE const DexTypeItem* dexGetTypeItem(const DexTypeList* pList,
703 assert(idx < pList->size);
704 return &pList->list[idx];
706 /* return the type_idx for the Nth entry in a TypeList */
707 DEX_INLINE u4 dexTypeListGetIdx(const DexTypeList* pList, u4 idx) {
708 const DexTypeItem* pItem = dexGetTypeItem(pList, idx);
709 return pItem->typeIdx;
712 /* get the static values list for a DexClass */
713 DEX_INLINE const DexEncodedArray* dexGetStaticValuesList(
714 const DexFile* pDexFile, const DexClassDef* pClassDef)
716 if (pClassDef->staticValuesOff == 0)
718 return (const DexEncodedArray*)
719 (pDexFile->baseAddr + pClassDef->staticValuesOff);
722 /* get the annotations directory item for a DexClass */
723 DEX_INLINE const DexAnnotationsDirectoryItem* dexGetAnnotationsDirectoryItem(
724 const DexFile* pDexFile, const DexClassDef* pClassDef)
726 if (pClassDef->annotationsOff == 0)
728 return (const DexAnnotationsDirectoryItem*)
729 (pDexFile->baseAddr + pClassDef->annotationsOff);
732 /* get the source file string */
733 DEX_INLINE const char* dexGetSourceFile(
734 const DexFile* pDexFile, const DexClassDef* pClassDef)
736 if (pClassDef->sourceFileIdx == 0xffffffff)
738 return dexStringById(pDexFile, pClassDef->sourceFileIdx);
741 /* get the size, in bytes, of a DexCode */
742 size_t dexGetDexCodeSize(const DexCode* pCode);
744 /* Get the list of "tries" for the given DexCode. */
745 DEX_INLINE const DexTry* dexGetTries(const DexCode* pCode) {
746 const u2* insnsEnd = &pCode->insns[pCode->insnsSize];
748 // Round to four bytes.
749 if ((((u4) insnsEnd) & 3) != 0) {
753 return (const DexTry*) insnsEnd;
756 /* Get the base of the encoded data for the given DexCode. */
757 DEX_INLINE const u1* dexGetCatchHandlerData(const DexCode* pCode) {
758 const DexTry* pTries = dexGetTries(pCode);
759 return (const u1*) &pTries[pCode->triesSize];
762 /* get a pointer to the start of the debugging data */
763 DEX_INLINE const u1* dexGetDebugInfoStream(const DexFile* pDexFile,
764 const DexCode* pCode)
766 if (pCode->debugInfoOff == 0) {
769 return pDexFile->baseAddr + pCode->debugInfoOff;
773 /* DexClassDef convenience - get class descriptor */
774 DEX_INLINE const char* dexGetClassDescriptor(const DexFile* pDexFile,
775 const DexClassDef* pClassDef)
777 return dexStringByTypeIdx(pDexFile, pClassDef->classIdx);
780 /* DexClassDef convenience - get superclass descriptor */
781 DEX_INLINE const char* dexGetSuperClassDescriptor(const DexFile* pDexFile,
782 const DexClassDef* pClassDef)
784 if (pClassDef->superclassIdx == 0)
786 return dexStringByTypeIdx(pDexFile, pClassDef->superclassIdx);
789 /* DexClassDef convenience - get class_data_item pointer */
790 DEX_INLINE const u1* dexGetClassData(const DexFile* pDexFile,
791 const DexClassDef* pClassDef)
793 if (pClassDef->classDataOff == 0)
795 return (const u1*) (pDexFile->baseAddr + pClassDef->classDataOff);
798 /* Get an annotation set at a particular offset. */
799 DEX_INLINE const DexAnnotationSetItem* dexGetAnnotationSetItem(
800 const DexFile* pDexFile, u4 offset)
802 return (const DexAnnotationSetItem*) (pDexFile->baseAddr + offset);
804 /* get the class' annotation set */
805 DEX_INLINE const DexAnnotationSetItem* dexGetClassAnnotationSet(
806 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
808 if (pAnnoDir->classAnnotationsOff == 0)
810 return dexGetAnnotationSetItem(pDexFile, pAnnoDir->classAnnotationsOff);
813 /* get the class' field annotation list */
814 DEX_INLINE const DexFieldAnnotationsItem* dexGetFieldAnnotations(
815 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
817 if (pAnnoDir->fieldsSize == 0)
820 // Skip past the header to the start of the field annotations.
821 return (const DexFieldAnnotationsItem*) &pAnnoDir[1];
824 /* get field annotation list size */
825 DEX_INLINE int dexGetFieldAnnotationsSize(const DexFile* pDexFile,
826 const DexAnnotationsDirectoryItem* pAnnoDir)
828 return pAnnoDir->fieldsSize;
831 /* return a pointer to the field's annotation set */
832 DEX_INLINE const DexAnnotationSetItem* dexGetFieldAnnotationSetItem(
833 const DexFile* pDexFile, const DexFieldAnnotationsItem* pItem)
835 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
838 /* get the class' method annotation list */
839 DEX_INLINE const DexMethodAnnotationsItem* dexGetMethodAnnotations(
840 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
842 if (pAnnoDir->methodsSize == 0)
846 * Skip past the header and field annotations to the start of the
847 * method annotations.
849 const u1* addr = (const u1*) &pAnnoDir[1];
850 addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
851 return (const DexMethodAnnotationsItem*) addr;
854 /* get method annotation list size */
855 DEX_INLINE int dexGetMethodAnnotationsSize(const DexFile* pDexFile,
856 const DexAnnotationsDirectoryItem* pAnnoDir)
858 return pAnnoDir->methodsSize;
861 /* return a pointer to the method's annotation set */
862 DEX_INLINE const DexAnnotationSetItem* dexGetMethodAnnotationSetItem(
863 const DexFile* pDexFile, const DexMethodAnnotationsItem* pItem)
865 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
868 /* get the class' parameter annotation list */
869 DEX_INLINE const DexParameterAnnotationsItem* dexGetParameterAnnotations(
870 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
872 if (pAnnoDir->parametersSize == 0)
876 * Skip past the header, field annotations, and method annotations
877 * to the start of the parameter annotations.
879 const u1* addr = (const u1*) &pAnnoDir[1];
880 addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
881 addr += pAnnoDir->methodsSize * sizeof (DexMethodAnnotationsItem);
882 return (const DexParameterAnnotationsItem*) addr;
885 /* get method annotation list size */
886 DEX_INLINE int dexGetParameterAnnotationsSize(const DexFile* pDexFile,
887 const DexAnnotationsDirectoryItem* pAnnoDir)
889 return pAnnoDir->parametersSize;
892 /* return the parameter annotation ref list */
893 DEX_INLINE const DexAnnotationSetRefList* dexGetParameterAnnotationSetRefList(
894 const DexFile* pDexFile, const DexParameterAnnotationsItem* pItem)
896 return (const DexAnnotationSetRefList*)
897 (pDexFile->baseAddr + pItem->annotationsOff);
900 /* get method annotation list size */
901 DEX_INLINE int dexGetParameterAnnotationSetRefSize(const DexFile* pDexFile,
902 const DexParameterAnnotationsItem* pItem)
904 if (pItem->annotationsOff == 0)
906 return dexGetParameterAnnotationSetRefList(pDexFile, pItem)->size;
909 /* return the Nth entry from an annotation set ref list */
910 DEX_INLINE const DexAnnotationSetRefItem* dexGetParameterAnnotationSetRef(
911 const DexAnnotationSetRefList* pList, u4 idx)
913 assert(idx < pList->size);
914 return &pList->list[idx];
917 /* given a DexAnnotationSetRefItem, return the DexAnnotationSetItem */
918 DEX_INLINE const DexAnnotationSetItem* dexGetSetRefItemItem(
919 const DexFile* pDexFile, const DexAnnotationSetRefItem* pItem)
921 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
924 /* return the Nth annotation offset from a DexAnnotationSetItem */
925 DEX_INLINE u4 dexGetAnnotationOff(
926 const DexAnnotationSetItem* pAnnoSet, u4 idx)
928 assert(idx < pAnnoSet->size);
929 return pAnnoSet->entries[idx];
932 /* return the Nth annotation item from a DexAnnotationSetItem */
933 DEX_INLINE const DexAnnotationItem* dexGetAnnotationItem(
934 const DexFile* pDexFile, const DexAnnotationSetItem* pAnnoSet, u4 idx)
936 return (const DexAnnotationItem*)
937 (pDexFile->baseAddr + dexGetAnnotationOff(pAnnoSet, idx));
941 * Get the type descriptor character associated with a given primitive
942 * type. This returns '\0' if the type is invalid.
944 char dexGetPrimitiveTypeDescriptorChar(PrimitiveType type);
947 * Get the type descriptor string associated with a given primitive
950 const char* dexGetPrimitiveTypeDescriptor(PrimitiveType type);
953 * Get the boxed type descriptor string associated with a given
954 * primitive type. This returns NULL for an invalid type, including
955 * particularly for type "void". In the latter case, even though there
956 * is a class Void, there's no such thing as a boxed instance of it.
958 const char* dexGetBoxedTypeDescriptor(PrimitiveType type);
961 * Get the primitive type constant from the given descriptor character.
962 * This returns PRIM_NOT (note: this is a 0) if the character is invalid
963 * as a primitive type descriptor.
965 PrimitiveType dexGetPrimitiveTypeFromDescriptorChar(char descriptorChar);
971 #endif /*_LIBDEX_DEXFILE*/