DexFile.h revision fa5e510d770070f8e7f47d126f5be138aa55cb84
17fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 27fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Copyright (C) 2008 The Android Open Source Project 37fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * 47fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Licensed under the Apache License, Version 2.0 (the "License"); 57fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * you may not use this file except in compliance with the License. 67fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * You may obtain a copy of the License at 77fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * 87fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * http://www.apache.org/licenses/LICENSE-2.0 97fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * 107fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Unless required by applicable law or agreed to in writing, software 117fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * distributed under the License is distributed on an "AS IS" BASIS, 127fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * See the License for the specific language governing permissions and 147fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * limitations under the License. 157fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 167fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 177fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 187fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Access .dex (Dalvik Executable Format) files. The code here assumes that 197fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * the DEX file has been rewritten (byte-swapped, word-aligned) and that 207fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * the contents can be directly accessed as a collection of C arrays. Please 217fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * see docs/dalvik/dex-format.html for a detailed description. 227fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * 23a8b089db8a6d495a18dc3ad91d3bd84b042fb004Fan Zhang * The structure and field names were chosen to match those in the DEX spec. 247fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * 257fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * It's generally assumed that the DEX file will be stored in shared memory, 26a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang * obviating the need to copy code and constant pool entries into newly 277fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * allocated storage. Maintaining local pointers to items in the shared area 287fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * is valid and encouraged. 29a8b089db8a6d495a18dc3ad91d3bd84b042fb004Fan Zhang * 30a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang * All memory-mapped structures are 32-bit aligned unless otherwise noted. 31a8b089db8a6d495a18dc3ad91d3bd84b042fb004Fan Zhang */ 327fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 337fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#ifndef LIBDEX_DEXFILE_H_ 347fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#define LIBDEX_DEXFILE_H_ 357fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 36a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang#ifndef LOG_TAG 37a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang# define LOG_TAG "libdex" 38a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang#endif 397fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 407fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#include <stdbool.h> 417fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#include <stdint.h> 427fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#include <stdio.h> 43a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang#include <assert.h> 44a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang#include "cutils/log.h" 45a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang 46a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang/* 47a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang * If "very verbose" logging is enabled, make it equivalent to ALOGV. 487fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Otherwise, make it disappear. 497fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * 507fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Define this above the #include "Dalvik.h" to enable for only a 517fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * single file. 527fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 53a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang/* #define VERY_VERBOSE_LOG */ 54a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang#if defined(VERY_VERBOSE_LOG) 55a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang# define LOGVV ALOGV 56a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang# define IF_LOGVV() IF_ALOGV() 57a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang#else 587fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze# define LOGVV(...) ((void)0) 597fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze# define IF_LOGVV() if (false) 607fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#endif 617fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 627fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 637fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * These match the definitions in the VM specification. 64a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhang */ 657fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzetypedef uint8_t u1; 66a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhangtypedef uint16_t u2; 67a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhangtypedef uint32_t u4; 68a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhangtypedef uint64_t u8; 69a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhangtypedef int8_t s1; 70a96b11f65d9f29dd4037da85e12f5cf9dcfa0176Fan Zhangtypedef int16_t s2; 717fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzetypedef int32_t s4; 727fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzetypedef int64_t s8; 737fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 747fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#include "libdex/SysUtil.h" 757fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 767fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 77a8b089db8a6d495a18dc3ad91d3bd84b042fb004Fan Zhang * gcc-style inline management -- ensures we have a copy of all functions 787fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * in the library, so code that links against us will work whether or not 797fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * it was built with optimizations enabled. 807fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 817fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#ifndef _DEX_GEN_INLINES /* only defined by DexInlines.c */ 827fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze# define DEX_INLINE extern __inline__ 833f3b547e4a2eeeb362d9b7c98b05f9a244cd754eMatthew Fritze#else 843f3b547e4a2eeeb362d9b7c98b05f9a244cd754eMatthew Fritze# define DEX_INLINE 857fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#endif 867fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 877fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* DEX file magic number */ 887fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#define DEX_MAGIC "dex\n" 897fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 903f3b547e4a2eeeb362d9b7c98b05f9a244cd754eMatthew Fritze/* The version for android N, encoded in 4 bytes of ASCII. This differentiates dex files that may 917fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * use default methods. 927fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 937fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#define DEX_MAGIC_VERS_37 "037\0" 947fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 957fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* The version for android O, encoded in 4 bytes of ASCII. This differentiates dex files that may 963f3b547e4a2eeeb362d9b7c98b05f9a244cd754eMatthew Fritze * contain invoke-custom, invoke-polymorphic, call-sites, and method handles. 977fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 987fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#define DEX_MAGIC_VERS_38 "038\0" 997fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 1007fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* current version, encoded in 4 bytes of ASCII */ 1017fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#define DEX_MAGIC_VERS "036\0" 1023f3b547e4a2eeeb362d9b7c98b05f9a244cd754eMatthew Fritze 1037fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 1047fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * older but still-recognized version (corresponding to Android API 1057fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * levels 13 and earlier 1067fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 1077fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#define DEX_MAGIC_VERS_API_13 "035\0" 1087fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 1097fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* same, but for optimized DEX header */ 1107fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#define DEX_OPT_MAGIC "dey\n" 1117fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#define DEX_OPT_MAGIC_VERS "036\0" 1127fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 1137fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze#define DEX_DEP_MAGIC "deps" 1147fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 1157fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 1167fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * 160-bit SHA-1 digest. 1177fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 1187fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzeenum { kSHA1DigestLen = 20, 1197fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kSHA1DigestOutputLen = kSHA1DigestLen*2 +1 }; 1207fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 1217fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* general constants */ 1227fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzeenum { 1237fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexEndianConstant = 0x12345678, /* the endianness indicator */ 1247fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexNoIndex = 0xffffffff, /* not a valid index value */ 1257fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze}; 1267fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 1277fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 1287fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Enumeration of all the primitive types. 1297fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 1307fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzeenum PrimitiveType { 1317fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze PRIM_NOT = 0, /* value is a reference type, not a primitive type */ 1327fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze PRIM_VOID = 1, 1337fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze PRIM_BOOLEAN = 2, 1347fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze PRIM_BYTE = 3, 1357fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze PRIM_SHORT = 4, 1367fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze PRIM_CHAR = 5, 1377fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze PRIM_INT = 6, 1387fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze PRIM_LONG = 7, 1397fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze PRIM_FLOAT = 8, 1407fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze PRIM_DOUBLE = 9, 1417fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze}; 1427fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 1437fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 1447fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * access flags and masks; the "standard" ones are all <= 0x4000 1457fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * 1467fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Note: There are related declarations in vm/oo/Object.h in the ClassFlags 147314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze * enum. 148314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze */ 149314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritzeenum { 150314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_PUBLIC = 0x00000001, // class, field, method, ic 151314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_PRIVATE = 0x00000002, // field, method, ic 152314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_PROTECTED = 0x00000004, // field, method, ic 153314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_STATIC = 0x00000008, // field, method, ic 154314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_FINAL = 0x00000010, // class, field, method, ic 155314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_SYNCHRONIZED = 0x00000020, // method (only allowed on natives) 156314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_SUPER = 0x00000020, // class (not used in Dalvik) 157314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_VOLATILE = 0x00000040, // field 158314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_BRIDGE = 0x00000040, // method (1.5) 159314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_TRANSIENT = 0x00000080, // field 160314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_VARARGS = 0x00000080, // method (1.5) 161314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_NATIVE = 0x00000100, // method 162314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_INTERFACE = 0x00000200, // class, ic 163314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_ABSTRACT = 0x00000400, // class, method, ic 164314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_STRICT = 0x00000800, // method 165314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_SYNTHETIC = 0x00001000, // field, method, ic 166314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_ANNOTATION = 0x00002000, // class, ic (1.5) 167314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_ENUM = 0x00004000, // class, field, ic (1.5) 168314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_CONSTRUCTOR = 0x00010000, // method (Dalvik only) 169314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_DECLARED_SYNCHRONIZED = 170314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze 0x00020000, // method (Dalvik only) 171314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_CLASS_MASK = 172314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze (ACC_PUBLIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT 173314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze | ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM), 174314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_INNER_CLASS_MASK = 175314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze (ACC_CLASS_MASK | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC), 176314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_FIELD_MASK = 177314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL 178314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze | ACC_VOLATILE | ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM), 179314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze ACC_METHOD_MASK = 180314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL 181314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze | ACC_SYNCHRONIZED | ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE 182314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze | ACC_ABSTRACT | ACC_STRICT | ACC_SYNTHETIC | ACC_CONSTRUCTOR 183314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze | ACC_DECLARED_SYNCHRONIZED), 184314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze}; 185314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze 186314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze/* annotation constants */ 187314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritzeenum { 188314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexVisibilityBuild = 0x00, /* annotation visibility */ 189314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexVisibilityRuntime = 0x01, 190314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexVisibilitySystem = 0x02, 191314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze 192314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationByte = 0x00, 193314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationShort = 0x02, 194314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationChar = 0x03, 195314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationInt = 0x04, 196314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationLong = 0x06, 197314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationFloat = 0x10, 198314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationDouble = 0x11, 199314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationMethodType = 0x15, 200314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationMethodHandle = 0x16, 201314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationString = 0x17, 202314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze kDexAnnotationType = 0x18, 2037fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexAnnotationField = 0x19, 2047fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexAnnotationMethod = 0x1a, 2057fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexAnnotationEnum = 0x1b, 2067fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexAnnotationArray = 0x1c, 2077fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexAnnotationAnnotation = 0x1d, 2087fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexAnnotationNull = 0x1e, 2097fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexAnnotationBoolean = 0x1f, 2107fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 2117fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */ 2127fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexAnnotationValueArgShift = 5, 2137fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze}; 2147fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 2157fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* map item type codes */ 2167fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzeenum { 2177fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeHeaderItem = 0x0000, 2187fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeStringIdItem = 0x0001, 2197fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeTypeIdItem = 0x0002, 2207fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeProtoIdItem = 0x0003, 2217fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeFieldIdItem = 0x0004, 2227fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeMethodIdItem = 0x0005, 2237fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeClassDefItem = 0x0006, 2247fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeCallSiteIdItem = 0x0007, 2257fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeMethodHandleItem = 0x0008, 2263f3b547e4a2eeeb362d9b7c98b05f9a244cd754eMatthew Fritze kDexTypeMapList = 0x1000, 2277fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeTypeList = 0x1001, 2287fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeAnnotationSetRefList = 0x1002, 2297fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeAnnotationSetItem = 0x1003, 2307fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeClassDataItem = 0x2000, 2317fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeCodeItem = 0x2001, 2327fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeStringDataItem = 0x2002, 2337fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeDebugInfoItem = 0x2003, 2347fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeAnnotationItem = 0x2004, 2357fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeEncodedArrayItem = 0x2005, 2367fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexTypeAnnotationsDirectoryItem = 0x2006, 2377fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze}; 2387fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 2397fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* auxillary data section chunk codes */ 2407fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzeenum { 2417fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexChunkClassLookup = 0x434c4b50, /* CLKP */ 2427fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexChunkRegisterMaps = 0x524d4150, /* RMAP */ 243314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze 2447fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze kDexChunkEnd = 0x41454e44, /* AEND */ 245314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze}; 246314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze 247314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze/* debug info opcodes and constants */ 2487fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzeenum { 2497fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_END_SEQUENCE = 0x00, 2507fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_ADVANCE_PC = 0x01, 2517fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_ADVANCE_LINE = 0x02, 2527fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_START_LOCAL = 0x03, 2537fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_START_LOCAL_EXTENDED = 0x04, 2547fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_END_LOCAL = 0x05, 2553f3b547e4a2eeeb362d9b7c98b05f9a244cd754eMatthew Fritze DBG_RESTART_LOCAL = 0x06, 2567fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_SET_PROLOGUE_END = 0x07, 2577fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_SET_EPILOGUE_BEGIN = 0x08, 2587fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_SET_FILE = 0x09, 2597fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_FIRST_SPECIAL = 0x0a, 2607fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_LINE_BASE = -4, 2617fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze DBG_LINE_RANGE = 15, 2627fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze}; 2637fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 2647fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 2657fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Direct-mapped "header_item" struct. 2667fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 2677fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzestruct DexHeader { 2687fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u1 magic[8]; /* includes version number */ 2697fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 checksum; /* adler32 checksum */ 270314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze u1 signature[kSHA1DigestLen]; /* SHA-1 hash */ 2717fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 fileSize; /* length of entire file */ 272314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze u4 headerSize; /* offset to start of next section */ 273314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze u4 endianTag; 274314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze u4 linkSize; 2757fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 linkOff; 2767fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 mapOff; 2777fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 stringIdsSize; 2787fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 stringIdsOff; 2797fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 typeIdsSize; 2807fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 typeIdsOff; 2817fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 protoIdsSize; 2823f3b547e4a2eeeb362d9b7c98b05f9a244cd754eMatthew Fritze u4 protoIdsOff; 2837fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 fieldIdsSize; 2847fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 fieldIdsOff; 2857fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 methodIdsSize; 2867fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 methodIdsOff; 2877fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 classDefsSize; 2887fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 classDefsOff; 2897fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 dataSize; 2907fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 dataOff; 2917fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze}; 2927fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 2937fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 2947fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Direct-mapped "map_item". 2957fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 296314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritzestruct DexMapItem { 2977fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u2 type; /* type code (see kDexType* above) */ 298314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze u2 unused; 299314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze u4 size; /* count of items of the indicated type */ 300314b09eb4b6bff2626604155e4273956aeaa24b5Matthew Fritze u4 offset; /* file offset to the start of data */ 3017fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze}; 3027fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 3037fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 3047fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Direct-mapped "map_list". 3057fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 3067fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzestruct DexMapList { 3077fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 size; /* #of entries in list */ 3083f3b547e4a2eeeb362d9b7c98b05f9a244cd754eMatthew Fritze DexMapItem list[1]; /* entries */ 3097fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze}; 3107fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze 3117fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze/* 3127fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze * Direct-mapped "string_id_item". 3137fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze */ 3147fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritzestruct DexStringId { 3157fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze u4 stringDataOff; /* file offset to string_data_item */ 3167fda314980107337d2eb9237ed00e997ae4ee345Matthew Fritze}; 317 318/* 319 * Direct-mapped "type_id_item". 320 */ 321struct DexTypeId { 322 u4 descriptorIdx; /* index into stringIds list for type descriptor */ 323}; 324 325/* 326 * Direct-mapped "field_id_item". 327 */ 328struct DexFieldId { 329 u2 classIdx; /* index into typeIds list for defining class */ 330 u2 typeIdx; /* index into typeIds for field type */ 331 u4 nameIdx; /* index into stringIds for field name */ 332}; 333 334/* 335 * Direct-mapped "method_id_item". 336 */ 337struct DexMethodId { 338 u2 classIdx; /* index into typeIds list for defining class */ 339 u2 protoIdx; /* index into protoIds for method prototype */ 340 u4 nameIdx; /* index into stringIds for method name */ 341}; 342 343/* 344 * Direct-mapped "proto_id_item". 345 */ 346struct DexProtoId { 347 u4 shortyIdx; /* index into stringIds for shorty descriptor */ 348 u4 returnTypeIdx; /* index into typeIds list for return type */ 349 u4 parametersOff; /* file offset to type_list for parameter types */ 350}; 351 352/* 353 * Direct-mapped "class_def_item". 354 */ 355struct DexClassDef { 356 u4 classIdx; /* index into typeIds for this class */ 357 u4 accessFlags; 358 u4 superclassIdx; /* index into typeIds for superclass */ 359 u4 interfacesOff; /* file offset to DexTypeList */ 360 u4 sourceFileIdx; /* index into stringIds for source file name */ 361 u4 annotationsOff; /* file offset to annotations_directory_item */ 362 u4 classDataOff; /* file offset to class_data_item */ 363 u4 staticValuesOff; /* file offset to DexEncodedArray */ 364}; 365 366/* 367 * Direct-mapped "call_site_id_item" 368 */ 369struct DexCallSiteId { 370 u4 callSiteOff; /* file offset to DexEncodedArray */ 371}; 372 373/* 374 * Direct-mapped "method_handle_item" 375 */ 376struct DexMethodHandleItem { 377 u2 methodHandleType; /* type of method handle */ 378 u2 reserved1; /* reserved for future use */ 379 u2 fieldOrMethodIdx; /* index of associated field or method */ 380 u2 reserved2; /* reserved for future use */ 381}; 382 383/* 384 * Direct-mapped "type_item". 385 */ 386struct DexTypeItem { 387 u2 typeIdx; /* index into typeIds */ 388}; 389 390/* 391 * Direct-mapped "type_list". 392 */ 393struct DexTypeList { 394 u4 size; /* #of entries in list */ 395 DexTypeItem list[1]; /* entries */ 396}; 397 398/* 399 * Direct-mapped "code_item". 400 * 401 * The "catches" table is used when throwing an exception, 402 * "debugInfo" is used when displaying an exception stack trace or 403 * debugging. An offset of zero indicates that there are no entries. 404 */ 405struct DexCode { 406 u2 registersSize; 407 u2 insSize; 408 u2 outsSize; 409 u2 triesSize; 410 u4 debugInfoOff; /* file offset to debug info stream */ 411 u4 insnsSize; /* size of the insns array, in u2 units */ 412 u2 insns[1]; 413 /* followed by optional u2 padding */ 414 /* followed by try_item[triesSize] */ 415 /* followed by uleb128 handlersSize */ 416 /* followed by catch_handler_item[handlersSize] */ 417}; 418 419/* 420 * Direct-mapped "try_item". 421 */ 422struct DexTry { 423 u4 startAddr; /* start address, in 16-bit code units */ 424 u2 insnCount; /* instruction count, in 16-bit code units */ 425 u2 handlerOff; /* offset in encoded handler data to handlers */ 426}; 427 428/* 429 * Link table. Currently undefined. 430 */ 431struct DexLink { 432 u1 bleargh; 433}; 434 435 436/* 437 * Direct-mapped "annotations_directory_item". 438 */ 439struct DexAnnotationsDirectoryItem { 440 u4 classAnnotationsOff; /* offset to DexAnnotationSetItem */ 441 u4 fieldsSize; /* count of DexFieldAnnotationsItem */ 442 u4 methodsSize; /* count of DexMethodAnnotationsItem */ 443 u4 parametersSize; /* count of DexParameterAnnotationsItem */ 444 /* followed by DexFieldAnnotationsItem[fieldsSize] */ 445 /* followed by DexMethodAnnotationsItem[methodsSize] */ 446 /* followed by DexParameterAnnotationsItem[parametersSize] */ 447}; 448 449/* 450 * Direct-mapped "field_annotations_item". 451 */ 452struct DexFieldAnnotationsItem { 453 u4 fieldIdx; 454 u4 annotationsOff; /* offset to DexAnnotationSetItem */ 455}; 456 457/* 458 * Direct-mapped "method_annotations_item". 459 */ 460struct DexMethodAnnotationsItem { 461 u4 methodIdx; 462 u4 annotationsOff; /* offset to DexAnnotationSetItem */ 463}; 464 465/* 466 * Direct-mapped "parameter_annotations_item". 467 */ 468struct DexParameterAnnotationsItem { 469 u4 methodIdx; 470 u4 annotationsOff; /* offset to DexAnotationSetRefList */ 471}; 472 473/* 474 * Direct-mapped "annotation_set_ref_item". 475 */ 476struct DexAnnotationSetRefItem { 477 u4 annotationsOff; /* offset to DexAnnotationSetItem */ 478}; 479 480/* 481 * Direct-mapped "annotation_set_ref_list". 482 */ 483struct DexAnnotationSetRefList { 484 u4 size; 485 DexAnnotationSetRefItem list[1]; 486}; 487 488/* 489 * Direct-mapped "annotation_set_item". 490 */ 491struct DexAnnotationSetItem { 492 u4 size; 493 u4 entries[1]; /* offset to DexAnnotationItem */ 494}; 495 496/* 497 * Direct-mapped "annotation_item". 498 * 499 * NOTE: this structure is byte-aligned. 500 */ 501struct DexAnnotationItem { 502 u1 visibility; 503 u1 annotation[1]; /* data in encoded_annotation format */ 504}; 505 506/* 507 * Direct-mapped "encoded_array". 508 * 509 * NOTE: this structure is byte-aligned. 510 */ 511struct DexEncodedArray { 512 u1 array[1]; /* data in encoded_array format */ 513}; 514 515/* 516 * Lookup table for classes. It provides a mapping from class name to 517 * class definition. Used by dexFindClass(). 518 * 519 * We calculate this at DEX optimization time and embed it in the file so we 520 * don't need the same hash table in every VM. This is slightly slower than 521 * a hash table with direct pointers to the items, but because it's shared 522 * there's less of a penalty for using a fairly sparse table. 523 */ 524struct DexClassLookup { 525 int size; // total size, including "size" 526 int numEntries; // size of table[]; always power of 2 527 struct { 528 u4 classDescriptorHash; // class descriptor hash code 529 int classDescriptorOffset; // in bytes, from start of DEX 530 int classDefOffset; // in bytes, from start of DEX 531 } table[1]; 532}; 533 534/* 535 * Header added by DEX optimization pass. Values are always written in 536 * local byte and structure padding. The first field (magic + version) 537 * is guaranteed to be present and directly readable for all expected 538 * compiler configurations; the rest is version-dependent. 539 * 540 * Try to keep this simple and fixed-size. 541 */ 542struct DexOptHeader { 543 u1 magic[8]; /* includes version number */ 544 545 u4 dexOffset; /* file offset of DEX header */ 546 u4 dexLength; 547 u4 depsOffset; /* offset of optimized DEX dependency table */ 548 u4 depsLength; 549 u4 optOffset; /* file offset of optimized data tables */ 550 u4 optLength; 551 552 u4 flags; /* some info flags */ 553 u4 checksum; /* adler32 checksum covering deps/opt */ 554 555 /* pad for 64-bit alignment if necessary */ 556}; 557 558#define DEX_OPT_FLAG_BIG (1<<1) /* swapped to big-endian */ 559 560#define DEX_INTERFACE_CACHE_SIZE 128 /* must be power of 2 */ 561 562/* 563 * Structure representing a DEX file. 564 * 565 * Code should regard DexFile as opaque, using the API calls provided here 566 * to access specific structures. 567 */ 568struct DexFile { 569 /* directly-mapped "opt" header */ 570 const DexOptHeader* pOptHeader; 571 572 /* pointers to directly-mapped structs and arrays in base DEX */ 573 const DexHeader* pHeader; 574 const DexStringId* pStringIds; 575 const DexTypeId* pTypeIds; 576 const DexFieldId* pFieldIds; 577 const DexMethodId* pMethodIds; 578 const DexProtoId* pProtoIds; 579 const DexClassDef* pClassDefs; 580 const DexLink* pLinkData; 581 582 /* 583 * These are mapped out of the "auxillary" section, and may not be 584 * included in the file. 585 */ 586 const DexClassLookup* pClassLookup; 587 const void* pRegisterMapPool; // RegisterMapClassPool 588 589 /* points to start of DEX file data */ 590 const u1* baseAddr; 591 592 /* track memory overhead for auxillary structures */ 593 int overhead; 594 595 /* additional app-specific data structures associated with the DEX */ 596 //void* auxData; 597}; 598 599/* 600 * Utility function -- rounds up to the nearest power of 2. 601 */ 602u4 dexRoundUpPower2(u4 val); 603 604/* 605 * Parse an optimized or unoptimized .dex file sitting in memory. 606 * 607 * On success, return a newly-allocated DexFile. 608 */ 609DexFile* dexFileParse(const u1* data, size_t length, int flags); 610 611/* bit values for "flags" argument to dexFileParse */ 612enum { 613 kDexParseDefault = 0, 614 kDexParseVerifyChecksum = 1, 615 kDexParseContinueOnError = (1 << 1), 616}; 617 618/* 619 * Fix the byte ordering of all fields in the DEX file, and do 620 * structural verification. This is only required for code that opens 621 * "raw" DEX files, such as the DEX optimizer. 622 * 623 * Return 0 on success. 624 */ 625int dexSwapAndVerify(u1* addr, int len); 626 627/* 628 * Detect the file type of the given memory buffer via magic number. 629 * Call dexSwapAndVerify() on an unoptimized DEX file, do nothing 630 * but return successfully on an optimized DEX file, and report an 631 * error for all other cases. 632 * 633 * Return 0 on success. 634 */ 635int dexSwapAndVerifyIfNecessary(u1* addr, size_t len); 636 637/* 638 * Check to see if the file magic and format version in the given 639 * header are recognized as valid. Returns true if they are 640 * acceptable. 641 */ 642bool dexHasValidMagic(const DexHeader* pHeader); 643 644/* 645 * Compute DEX checksum. 646 */ 647u4 dexComputeChecksum(const DexHeader* pHeader); 648 649/* 650 * Free a DexFile structure, along with any associated structures. 651 */ 652void dexFileFree(DexFile* pDexFile); 653 654/* 655 * Create class lookup table. 656 */ 657DexClassLookup* dexCreateClassLookup(DexFile* pDexFile); 658 659/* 660 * Find a class definition by descriptor. 661 */ 662const DexClassDef* dexFindClass(const DexFile* pFile, const char* descriptor); 663 664/* 665 * Set up the basic raw data pointers of a DexFile. This function isn't 666 * meant for general use. 667 */ 668void dexFileSetupBasicPointers(DexFile* pDexFile, const u1* data); 669 670/* return the DexMapList of the file, if any */ 671DEX_INLINE const DexMapList* dexGetMap(const DexFile* pDexFile) { 672 u4 mapOff = pDexFile->pHeader->mapOff; 673 674 if (mapOff == 0) { 675 return NULL; 676 } else { 677 return (const DexMapList*) (pDexFile->baseAddr + mapOff); 678 } 679} 680 681/* return the const char* string data referred to by the given string_id */ 682DEX_INLINE const char* dexGetStringData(const DexFile* pDexFile, 683 const DexStringId* pStringId) { 684 const u1* ptr = pDexFile->baseAddr + pStringId->stringDataOff; 685 686 // Skip the uleb128 length. 687 while (*(ptr++) > 0x7f) /* empty */ ; 688 689 return (const char*) ptr; 690} 691/* return the StringId with the specified index */ 692DEX_INLINE const DexStringId* dexGetStringId(const DexFile* pDexFile, u4 idx) { 693 assert(idx < pDexFile->pHeader->stringIdsSize); 694 return &pDexFile->pStringIds[idx]; 695} 696/* return the UTF-8 encoded string with the specified string_id index */ 697DEX_INLINE const char* dexStringById(const DexFile* pDexFile, u4 idx) { 698 const DexStringId* pStringId = dexGetStringId(pDexFile, idx); 699 return dexGetStringData(pDexFile, pStringId); 700} 701 702/* Return the UTF-8 encoded string with the specified string_id index, 703 * also filling in the UTF-16 size (number of 16-bit code points).*/ 704const char* dexStringAndSizeById(const DexFile* pDexFile, u4 idx, 705 u4* utf16Size); 706 707/* return the TypeId with the specified index */ 708DEX_INLINE const DexTypeId* dexGetTypeId(const DexFile* pDexFile, u4 idx) { 709 assert(idx < pDexFile->pHeader->typeIdsSize); 710 return &pDexFile->pTypeIds[idx]; 711} 712 713/* 714 * Get the descriptor string associated with a given type index. 715 * The caller should not free() the returned string. 716 */ 717DEX_INLINE const char* dexStringByTypeIdx(const DexFile* pDexFile, u4 idx) { 718 const DexTypeId* typeId = dexGetTypeId(pDexFile, idx); 719 return dexStringById(pDexFile, typeId->descriptorIdx); 720} 721 722/* return the MethodId with the specified index */ 723DEX_INLINE const DexMethodId* dexGetMethodId(const DexFile* pDexFile, u4 idx) { 724 assert(idx < pDexFile->pHeader->methodIdsSize); 725 return &pDexFile->pMethodIds[idx]; 726} 727 728/* return the FieldId with the specified index */ 729DEX_INLINE const DexFieldId* dexGetFieldId(const DexFile* pDexFile, u4 idx) { 730 assert(idx < pDexFile->pHeader->fieldIdsSize); 731 return &pDexFile->pFieldIds[idx]; 732} 733 734/* return the ProtoId with the specified index */ 735DEX_INLINE const DexProtoId* dexGetProtoId(const DexFile* pDexFile, u4 idx) { 736 assert(idx < pDexFile->pHeader->protoIdsSize); 737 return &pDexFile->pProtoIds[idx]; 738} 739 740/* 741 * Get the parameter list from a ProtoId. The returns NULL if the ProtoId 742 * does not have a parameter list. 743 */ 744DEX_INLINE const DexTypeList* dexGetProtoParameters( 745 const DexFile *pDexFile, const DexProtoId* pProtoId) { 746 if (pProtoId->parametersOff == 0) { 747 return NULL; 748 } 749 return (const DexTypeList*) 750 (pDexFile->baseAddr + pProtoId->parametersOff); 751} 752 753/* return the ClassDef with the specified index */ 754DEX_INLINE const DexClassDef* dexGetClassDef(const DexFile* pDexFile, u4 idx) { 755 assert(idx < pDexFile->pHeader->classDefsSize); 756 return &pDexFile->pClassDefs[idx]; 757} 758 759/* given a ClassDef pointer, recover its index */ 760DEX_INLINE u4 dexGetIndexForClassDef(const DexFile* pDexFile, 761 const DexClassDef* pClassDef) 762{ 763 assert(pClassDef >= pDexFile->pClassDefs && 764 pClassDef < pDexFile->pClassDefs + pDexFile->pHeader->classDefsSize); 765 return pClassDef - pDexFile->pClassDefs; 766} 767 768/* get the interface list for a DexClass */ 769DEX_INLINE const DexTypeList* dexGetInterfacesList(const DexFile* pDexFile, 770 const DexClassDef* pClassDef) 771{ 772 if (pClassDef->interfacesOff == 0) 773 return NULL; 774 return (const DexTypeList*) 775 (pDexFile->baseAddr + pClassDef->interfacesOff); 776} 777/* return the Nth entry in a DexTypeList. */ 778DEX_INLINE const DexTypeItem* dexGetTypeItem(const DexTypeList* pList, 779 u4 idx) 780{ 781 assert(idx < pList->size); 782 return &pList->list[idx]; 783} 784/* return the type_idx for the Nth entry in a TypeList */ 785DEX_INLINE u4 dexTypeListGetIdx(const DexTypeList* pList, u4 idx) { 786 const DexTypeItem* pItem = dexGetTypeItem(pList, idx); 787 return pItem->typeIdx; 788} 789 790/* get the static values list for a DexClass */ 791DEX_INLINE const DexEncodedArray* dexGetStaticValuesList( 792 const DexFile* pDexFile, const DexClassDef* pClassDef) 793{ 794 if (pClassDef->staticValuesOff == 0) 795 return NULL; 796 return (const DexEncodedArray*) 797 (pDexFile->baseAddr + pClassDef->staticValuesOff); 798} 799 800/* get the annotations directory item for a DexClass */ 801DEX_INLINE const DexAnnotationsDirectoryItem* dexGetAnnotationsDirectoryItem( 802 const DexFile* pDexFile, const DexClassDef* pClassDef) 803{ 804 if (pClassDef->annotationsOff == 0) 805 return NULL; 806 return (const DexAnnotationsDirectoryItem*) 807 (pDexFile->baseAddr + pClassDef->annotationsOff); 808} 809 810/* get the source file string */ 811DEX_INLINE const char* dexGetSourceFile( 812 const DexFile* pDexFile, const DexClassDef* pClassDef) 813{ 814 if (pClassDef->sourceFileIdx == 0xffffffff) 815 return NULL; 816 return dexStringById(pDexFile, pClassDef->sourceFileIdx); 817} 818 819/* get the size, in bytes, of a DexCode */ 820size_t dexGetDexCodeSize(const DexCode* pCode); 821 822/* Get the list of "tries" for the given DexCode. */ 823DEX_INLINE const DexTry* dexGetTries(const DexCode* pCode) { 824 const u2* insnsEnd = &pCode->insns[pCode->insnsSize]; 825 826 // Round to four bytes. 827 if ((((uintptr_t) insnsEnd) & 3) != 0) { 828 insnsEnd++; 829 } 830 831 return (const DexTry*) insnsEnd; 832} 833 834/* Get the base of the encoded data for the given DexCode. */ 835DEX_INLINE const u1* dexGetCatchHandlerData(const DexCode* pCode) { 836 const DexTry* pTries = dexGetTries(pCode); 837 return (const u1*) &pTries[pCode->triesSize]; 838} 839 840/* get a pointer to the start of the debugging data */ 841DEX_INLINE const u1* dexGetDebugInfoStream(const DexFile* pDexFile, 842 const DexCode* pCode) 843{ 844 if (pCode->debugInfoOff == 0) { 845 return NULL; 846 } else { 847 return pDexFile->baseAddr + pCode->debugInfoOff; 848 } 849} 850 851/* DexClassDef convenience - get class descriptor */ 852DEX_INLINE const char* dexGetClassDescriptor(const DexFile* pDexFile, 853 const DexClassDef* pClassDef) 854{ 855 return dexStringByTypeIdx(pDexFile, pClassDef->classIdx); 856} 857 858/* DexClassDef convenience - get superclass descriptor */ 859DEX_INLINE const char* dexGetSuperClassDescriptor(const DexFile* pDexFile, 860 const DexClassDef* pClassDef) 861{ 862 if (pClassDef->superclassIdx == 0) 863 return NULL; 864 return dexStringByTypeIdx(pDexFile, pClassDef->superclassIdx); 865} 866 867/* DexClassDef convenience - get class_data_item pointer */ 868DEX_INLINE const u1* dexGetClassData(const DexFile* pDexFile, 869 const DexClassDef* pClassDef) 870{ 871 if (pClassDef->classDataOff == 0) 872 return NULL; 873 return (const u1*) (pDexFile->baseAddr + pClassDef->classDataOff); 874} 875 876/* Get an annotation set at a particular offset. */ 877DEX_INLINE const DexAnnotationSetItem* dexGetAnnotationSetItem( 878 const DexFile* pDexFile, u4 offset) 879{ 880 if (offset == 0) { 881 return NULL; 882 } 883 return (const DexAnnotationSetItem*) (pDexFile->baseAddr + offset); 884} 885/* get the class' annotation set */ 886DEX_INLINE const DexAnnotationSetItem* dexGetClassAnnotationSet( 887 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir) 888{ 889 return dexGetAnnotationSetItem(pDexFile, pAnnoDir->classAnnotationsOff); 890} 891 892/* get the class' field annotation list */ 893DEX_INLINE const DexFieldAnnotationsItem* dexGetFieldAnnotations( 894 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir) 895{ 896 (void) pDexFile; 897 if (pAnnoDir->fieldsSize == 0) 898 return NULL; 899 900 // Skip past the header to the start of the field annotations. 901 return (const DexFieldAnnotationsItem*) &pAnnoDir[1]; 902} 903 904/* get field annotation list size */ 905DEX_INLINE int dexGetFieldAnnotationsSize(const DexFile* pDexFile, 906 const DexAnnotationsDirectoryItem* pAnnoDir) 907{ 908 (void) pDexFile; 909 return pAnnoDir->fieldsSize; 910} 911 912/* return a pointer to the field's annotation set */ 913DEX_INLINE const DexAnnotationSetItem* dexGetFieldAnnotationSetItem( 914 const DexFile* pDexFile, const DexFieldAnnotationsItem* pItem) 915{ 916 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff); 917} 918 919/* get the class' method annotation list */ 920DEX_INLINE const DexMethodAnnotationsItem* dexGetMethodAnnotations( 921 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir) 922{ 923 (void) pDexFile; 924 if (pAnnoDir->methodsSize == 0) 925 return NULL; 926 927 /* 928 * Skip past the header and field annotations to the start of the 929 * method annotations. 930 */ 931 const u1* addr = (const u1*) &pAnnoDir[1]; 932 addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem); 933 return (const DexMethodAnnotationsItem*) addr; 934} 935 936/* get method annotation list size */ 937DEX_INLINE int dexGetMethodAnnotationsSize(const DexFile* pDexFile, 938 const DexAnnotationsDirectoryItem* pAnnoDir) 939{ 940 (void) pDexFile; 941 return pAnnoDir->methodsSize; 942} 943 944/* return a pointer to the method's annotation set */ 945DEX_INLINE const DexAnnotationSetItem* dexGetMethodAnnotationSetItem( 946 const DexFile* pDexFile, const DexMethodAnnotationsItem* pItem) 947{ 948 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff); 949} 950 951/* get the class' parameter annotation list */ 952DEX_INLINE const DexParameterAnnotationsItem* dexGetParameterAnnotations( 953 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir) 954{ 955 (void) pDexFile; 956 if (pAnnoDir->parametersSize == 0) 957 return NULL; 958 959 /* 960 * Skip past the header, field annotations, and method annotations 961 * to the start of the parameter annotations. 962 */ 963 const u1* addr = (const u1*) &pAnnoDir[1]; 964 addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem); 965 addr += pAnnoDir->methodsSize * sizeof (DexMethodAnnotationsItem); 966 return (const DexParameterAnnotationsItem*) addr; 967} 968 969/* get method annotation list size */ 970DEX_INLINE int dexGetParameterAnnotationsSize(const DexFile* pDexFile, 971 const DexAnnotationsDirectoryItem* pAnnoDir) 972{ 973 (void) pDexFile; 974 return pAnnoDir->parametersSize; 975} 976 977/* return the parameter annotation ref list */ 978DEX_INLINE const DexAnnotationSetRefList* dexGetParameterAnnotationSetRefList( 979 const DexFile* pDexFile, const DexParameterAnnotationsItem* pItem) 980{ 981 if (pItem->annotationsOff == 0) { 982 return NULL; 983 } 984 return (const DexAnnotationSetRefList*) (pDexFile->baseAddr + pItem->annotationsOff); 985} 986 987/* get method annotation list size */ 988DEX_INLINE int dexGetParameterAnnotationSetRefSize(const DexFile* pDexFile, 989 const DexParameterAnnotationsItem* pItem) 990{ 991 if (pItem->annotationsOff == 0) { 992 return 0; 993 } 994 return dexGetParameterAnnotationSetRefList(pDexFile, pItem)->size; 995} 996 997/* return the Nth entry from an annotation set ref list */ 998DEX_INLINE const DexAnnotationSetRefItem* dexGetParameterAnnotationSetRef( 999 const DexAnnotationSetRefList* pList, u4 idx) 1000{ 1001 assert(idx < pList->size); 1002 return &pList->list[idx]; 1003} 1004 1005/* given a DexAnnotationSetRefItem, return the DexAnnotationSetItem */ 1006DEX_INLINE const DexAnnotationSetItem* dexGetSetRefItemItem( 1007 const DexFile* pDexFile, const DexAnnotationSetRefItem* pItem) 1008{ 1009 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff); 1010} 1011 1012/* return the Nth annotation offset from a DexAnnotationSetItem */ 1013DEX_INLINE u4 dexGetAnnotationOff( 1014 const DexAnnotationSetItem* pAnnoSet, u4 idx) 1015{ 1016 assert(idx < pAnnoSet->size); 1017 return pAnnoSet->entries[idx]; 1018} 1019 1020/* return the Nth annotation item from a DexAnnotationSetItem */ 1021DEX_INLINE const DexAnnotationItem* dexGetAnnotationItem( 1022 const DexFile* pDexFile, const DexAnnotationSetItem* pAnnoSet, u4 idx) 1023{ 1024 u4 offset = dexGetAnnotationOff(pAnnoSet, idx); 1025 if (offset == 0) { 1026 return NULL; 1027 } 1028 return (const DexAnnotationItem*) (pDexFile->baseAddr + offset); 1029} 1030 1031/* 1032 * Get the type descriptor character associated with a given primitive 1033 * type. This returns '\0' if the type is invalid. 1034 */ 1035char dexGetPrimitiveTypeDescriptorChar(PrimitiveType type); 1036 1037/* 1038 * Get the type descriptor string associated with a given primitive 1039 * type. 1040 */ 1041const char* dexGetPrimitiveTypeDescriptor(PrimitiveType type); 1042 1043/* 1044 * Get the boxed type descriptor string associated with a given 1045 * primitive type. This returns NULL for an invalid type, including 1046 * particularly for type "void". In the latter case, even though there 1047 * is a class Void, there's no such thing as a boxed instance of it. 1048 */ 1049const char* dexGetBoxedTypeDescriptor(PrimitiveType type); 1050 1051/* 1052 * Get the primitive type constant from the given descriptor character. 1053 * This returns PRIM_NOT (note: this is a 0) if the character is invalid 1054 * as a primitive type descriptor. 1055 */ 1056PrimitiveType dexGetPrimitiveTypeFromDescriptorChar(char descriptorChar); 1057 1058#endif // LIBDEX_DEXFILE_H_ 1059