1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*
18 * 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.
22 *
23 * The structure and field names were chosen to match those in the DEX spec.
24 *
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.
29 *
30 * All memory-mapped structures are 32-bit aligned unless otherwise noted.
31 */
32
33 #ifndef LIBDEX_DEXFILE_H_
34 #define LIBDEX_DEXFILE_H_
35
36 #ifndef LOG_TAG
37 # define LOG_TAG "libdex"
38 #endif
39
40 #include <stdbool.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <assert.h>
44 #include "cutils/log.h"
45
46 /*
47 * If "very verbose" logging is enabled, make it equivalent to ALOGV.
48 * Otherwise, make it disappear.
49 *
50 * Define this above the #include "Dalvik.h" to enable for only a
51 * single file.
52 */
53 /* #define VERY_VERBOSE_LOG */
54 #if defined(VERY_VERBOSE_LOG)
55 # define LOGVV ALOGV
56 # define IF_LOGVV() IF_ALOGV()
57 #else
58 # define LOGVV(...) ((void)0)
59 # define IF_LOGVV() if (false)
60 #endif
61
62 /*
63 * These match the definitions in the VM specification.
64 */
65 typedef uint8_t u1;
66 typedef uint16_t u2;
67 typedef uint32_t u4;
68 typedef uint64_t u8;
69 typedef int8_t s1;
70 typedef int16_t s2;
71 typedef int32_t s4;
72 typedef int64_t s8;
73
74 #include "libdex/SysUtil.h"
75
76 /*
77 * gcc-style inline management -- ensures we have a copy of all functions
78 * in the library, so code that links against us will work whether or not
79 * it was built with optimizations enabled.
80 */
81 #ifndef _DEX_GEN_INLINES /* only defined by DexInlines.c */
82 # define DEX_INLINE extern __inline__
83 #else
84 # define DEX_INLINE
85 #endif
86
87 /* DEX file magic number */
88 #define DEX_MAGIC "dex\n"
89
90 /* The version for android N, encoded in 4 bytes of ASCII. This differentiates dex files that may
91 * use default methods.
92 */
93 #define DEX_MAGIC_VERS_37 "037\0"
94
95 /* The version for android O, encoded in 4 bytes of ASCII. This differentiates dex files that may
96 * contain invoke-custom, invoke-polymorphic, call-sites, and method handles.
97 */
98 #define DEX_MAGIC_VERS_38 "038\0"
99
100 /* current version, encoded in 4 bytes of ASCII */
101 #define DEX_MAGIC_VERS "036\0"
102
103 /*
104 * older but still-recognized version (corresponding to Android API
105 * levels 13 and earlier
106 */
107 #define DEX_MAGIC_VERS_API_13 "035\0"
108
109 /* same, but for optimized DEX header */
110 #define DEX_OPT_MAGIC "dey\n"
111 #define DEX_OPT_MAGIC_VERS "036\0"
112
113 #define DEX_DEP_MAGIC "deps"
114
115 /*
116 * 160-bit SHA-1 digest.
117 */
118 enum { kSHA1DigestLen = 20,
119 kSHA1DigestOutputLen = kSHA1DigestLen*2 +1 };
120
121 /* general constants */
122 enum {
123 kDexEndianConstant = 0x12345678, /* the endianness indicator */
124 kDexNoIndex = 0xffffffff, /* not a valid index value */
125 };
126
127 /*
128 * Enumeration of all the primitive types.
129 */
130 enum PrimitiveType {
131 PRIM_NOT = 0, /* value is a reference type, not a primitive type */
132 PRIM_VOID = 1,
133 PRIM_BOOLEAN = 2,
134 PRIM_BYTE = 3,
135 PRIM_SHORT = 4,
136 PRIM_CHAR = 5,
137 PRIM_INT = 6,
138 PRIM_LONG = 7,
139 PRIM_FLOAT = 8,
140 PRIM_DOUBLE = 9,
141 };
142
143 /*
144 * access flags and masks; the "standard" ones are all <= 0x4000
145 *
146 * Note: There are related declarations in vm/oo/Object.h in the ClassFlags
147 * enum.
148 */
149 enum {
150 ACC_PUBLIC = 0x00000001, // class, field, method, ic
151 ACC_PRIVATE = 0x00000002, // field, method, ic
152 ACC_PROTECTED = 0x00000004, // field, method, ic
153 ACC_STATIC = 0x00000008, // field, method, ic
154 ACC_FINAL = 0x00000010, // class, field, method, ic
155 ACC_SYNCHRONIZED = 0x00000020, // method (only allowed on natives)
156 ACC_SUPER = 0x00000020, // class (not used in Dalvik)
157 ACC_VOLATILE = 0x00000040, // field
158 ACC_BRIDGE = 0x00000040, // method (1.5)
159 ACC_TRANSIENT = 0x00000080, // field
160 ACC_VARARGS = 0x00000080, // method (1.5)
161 ACC_NATIVE = 0x00000100, // method
162 ACC_INTERFACE = 0x00000200, // class, ic
163 ACC_ABSTRACT = 0x00000400, // class, method, ic
164 ACC_STRICT = 0x00000800, // method
165 ACC_SYNTHETIC = 0x00001000, // field, method, ic
166 ACC_ANNOTATION = 0x00002000, // class, ic (1.5)
167 ACC_ENUM = 0x00004000, // class, field, ic (1.5)
168 ACC_CONSTRUCTOR = 0x00010000, // method (Dalvik only)
169 ACC_DECLARED_SYNCHRONIZED =
170 0x00020000, // method (Dalvik only)
171 ACC_CLASS_MASK =
172 (ACC_PUBLIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT
173 | ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM),
174 ACC_INNER_CLASS_MASK =
175 (ACC_CLASS_MASK | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC),
176 ACC_FIELD_MASK =
177 (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL
178 | ACC_VOLATILE | ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM),
179 ACC_METHOD_MASK =
180 (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL
181 | ACC_SYNCHRONIZED | ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE
182 | ACC_ABSTRACT | ACC_STRICT | ACC_SYNTHETIC | ACC_CONSTRUCTOR
183 | ACC_DECLARED_SYNCHRONIZED),
184 };
185
186 /* annotation constants */
187 enum {
188 kDexVisibilityBuild = 0x00, /* annotation visibility */
189 kDexVisibilityRuntime = 0x01,
190 kDexVisibilitySystem = 0x02,
191
192 kDexAnnotationByte = 0x00,
193 kDexAnnotationShort = 0x02,
194 kDexAnnotationChar = 0x03,
195 kDexAnnotationInt = 0x04,
196 kDexAnnotationLong = 0x06,
197 kDexAnnotationFloat = 0x10,
198 kDexAnnotationDouble = 0x11,
199 kDexAnnotationMethodType = 0x15,
200 kDexAnnotationMethodHandle = 0x16,
201 kDexAnnotationString = 0x17,
202 kDexAnnotationType = 0x18,
203 kDexAnnotationField = 0x19,
204 kDexAnnotationMethod = 0x1a,
205 kDexAnnotationEnum = 0x1b,
206 kDexAnnotationArray = 0x1c,
207 kDexAnnotationAnnotation = 0x1d,
208 kDexAnnotationNull = 0x1e,
209 kDexAnnotationBoolean = 0x1f,
210
211 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
212 kDexAnnotationValueArgShift = 5,
213 };
214
215 /* map item type codes */
216 enum {
217 kDexTypeHeaderItem = 0x0000,
218 kDexTypeStringIdItem = 0x0001,
219 kDexTypeTypeIdItem = 0x0002,
220 kDexTypeProtoIdItem = 0x0003,
221 kDexTypeFieldIdItem = 0x0004,
222 kDexTypeMethodIdItem = 0x0005,
223 kDexTypeClassDefItem = 0x0006,
224 kDexTypeCallSiteIdItem = 0x0007,
225 kDexTypeMethodHandleItem = 0x0008,
226 kDexTypeMapList = 0x1000,
227 kDexTypeTypeList = 0x1001,
228 kDexTypeAnnotationSetRefList = 0x1002,
229 kDexTypeAnnotationSetItem = 0x1003,
230 kDexTypeClassDataItem = 0x2000,
231 kDexTypeCodeItem = 0x2001,
232 kDexTypeStringDataItem = 0x2002,
233 kDexTypeDebugInfoItem = 0x2003,
234 kDexTypeAnnotationItem = 0x2004,
235 kDexTypeEncodedArrayItem = 0x2005,
236 kDexTypeAnnotationsDirectoryItem = 0x2006,
237 };
238
239 /* auxillary data section chunk codes */
240 enum {
241 kDexChunkClassLookup = 0x434c4b50, /* CLKP */
242 kDexChunkRegisterMaps = 0x524d4150, /* RMAP */
243
244 kDexChunkEnd = 0x41454e44, /* AEND */
245 };
246
247 /* debug info opcodes and constants */
248 enum {
249 DBG_END_SEQUENCE = 0x00,
250 DBG_ADVANCE_PC = 0x01,
251 DBG_ADVANCE_LINE = 0x02,
252 DBG_START_LOCAL = 0x03,
253 DBG_START_LOCAL_EXTENDED = 0x04,
254 DBG_END_LOCAL = 0x05,
255 DBG_RESTART_LOCAL = 0x06,
256 DBG_SET_PROLOGUE_END = 0x07,
257 DBG_SET_EPILOGUE_BEGIN = 0x08,
258 DBG_SET_FILE = 0x09,
259 DBG_FIRST_SPECIAL = 0x0a,
260 DBG_LINE_BASE = -4,
261 DBG_LINE_RANGE = 15,
262 };
263
264 /*
265 * Direct-mapped "header_item" struct.
266 */
267 struct DexHeader {
268 u1 magic[8]; /* includes version number */
269 u4 checksum; /* adler32 checksum */
270 u1 signature[kSHA1DigestLen]; /* SHA-1 hash */
271 u4 fileSize; /* length of entire file */
272 u4 headerSize; /* offset to start of next section */
273 u4 endianTag;
274 u4 linkSize;
275 u4 linkOff;
276 u4 mapOff;
277 u4 stringIdsSize;
278 u4 stringIdsOff;
279 u4 typeIdsSize;
280 u4 typeIdsOff;
281 u4 protoIdsSize;
282 u4 protoIdsOff;
283 u4 fieldIdsSize;
284 u4 fieldIdsOff;
285 u4 methodIdsSize;
286 u4 methodIdsOff;
287 u4 classDefsSize;
288 u4 classDefsOff;
289 u4 dataSize;
290 u4 dataOff;
291 };
292
293 /*
294 * Direct-mapped "map_item".
295 */
296 struct DexMapItem {
297 u2 type; /* type code (see kDexType* above) */
298 u2 unused;
299 u4 size; /* count of items of the indicated type */
300 u4 offset; /* file offset to the start of data */
301 };
302
303 /*
304 * Direct-mapped "map_list".
305 */
306 struct DexMapList {
307 u4 size; /* #of entries in list */
308 DexMapItem list[1]; /* entries */
309 };
310
311 /*
312 * Direct-mapped "string_id_item".
313 */
314 struct DexStringId {
315 u4 stringDataOff; /* file offset to string_data_item */
316 };
317
318 /*
319 * Direct-mapped "type_id_item".
320 */
321 struct DexTypeId {
322 u4 descriptorIdx; /* index into stringIds list for type descriptor */
323 };
324
325 /*
326 * Direct-mapped "field_id_item".
327 */
328 struct 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 */
337 struct 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 */
346 struct 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 */
355 struct 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 */
369 struct DexCallSiteId {
370 u4 callSiteOff; /* file offset to DexEncodedArray */
371 };
372
373 /*
374 * Enumeration of method handle type codes.
375 */
376 enum MethodHandleType {
377 STATIC_PUT = 0x00,
378 STATIC_GET = 0x01,
379 INSTANCE_PUT = 0x02,
380 INSTANCE_GET = 0x03,
381 INVOKE_STATIC = 0x04,
382 INVOKE_INSTANCE = 0x05,
383 INVOKE_CONSTRUCTOR = 0x06,
384 INVOKE_DIRECT = 0x07,
385 INVOKE_INTERFACE = 0x08
386 };
387
388 /*
389 * Direct-mapped "method_handle_item"
390 */
391 struct DexMethodHandleItem {
392 u2 methodHandleType; /* type of method handle */
393 u2 reserved1; /* reserved for future use */
394 u2 fieldOrMethodIdx; /* index of associated field or method */
395 u2 reserved2; /* reserved for future use */
396 };
397
398 /*
399 * Direct-mapped "type_item".
400 */
401 struct DexTypeItem {
402 u2 typeIdx; /* index into typeIds */
403 };
404
405 /*
406 * Direct-mapped "type_list".
407 */
408 struct DexTypeList {
409 u4 size; /* #of entries in list */
410 DexTypeItem list[1]; /* entries */
411 };
412
413 /*
414 * Direct-mapped "code_item".
415 *
416 * The "catches" table is used when throwing an exception,
417 * "debugInfo" is used when displaying an exception stack trace or
418 * debugging. An offset of zero indicates that there are no entries.
419 */
420 struct DexCode {
421 u2 registersSize;
422 u2 insSize;
423 u2 outsSize;
424 u2 triesSize;
425 u4 debugInfoOff; /* file offset to debug info stream */
426 u4 insnsSize; /* size of the insns array, in u2 units */
427 u2 insns[1];
428 /* followed by optional u2 padding */
429 /* followed by try_item[triesSize] */
430 /* followed by uleb128 handlersSize */
431 /* followed by catch_handler_item[handlersSize] */
432 };
433
434 /*
435 * Direct-mapped "try_item".
436 */
437 struct DexTry {
438 u4 startAddr; /* start address, in 16-bit code units */
439 u2 insnCount; /* instruction count, in 16-bit code units */
440 u2 handlerOff; /* offset in encoded handler data to handlers */
441 };
442
443 /*
444 * Link table. Currently undefined.
445 */
446 struct DexLink {
447 u1 bleargh;
448 };
449
450
451 /*
452 * Direct-mapped "annotations_directory_item".
453 */
454 struct DexAnnotationsDirectoryItem {
455 u4 classAnnotationsOff; /* offset to DexAnnotationSetItem */
456 u4 fieldsSize; /* count of DexFieldAnnotationsItem */
457 u4 methodsSize; /* count of DexMethodAnnotationsItem */
458 u4 parametersSize; /* count of DexParameterAnnotationsItem */
459 /* followed by DexFieldAnnotationsItem[fieldsSize] */
460 /* followed by DexMethodAnnotationsItem[methodsSize] */
461 /* followed by DexParameterAnnotationsItem[parametersSize] */
462 };
463
464 /*
465 * Direct-mapped "field_annotations_item".
466 */
467 struct DexFieldAnnotationsItem {
468 u4 fieldIdx;
469 u4 annotationsOff; /* offset to DexAnnotationSetItem */
470 };
471
472 /*
473 * Direct-mapped "method_annotations_item".
474 */
475 struct DexMethodAnnotationsItem {
476 u4 methodIdx;
477 u4 annotationsOff; /* offset to DexAnnotationSetItem */
478 };
479
480 /*
481 * Direct-mapped "parameter_annotations_item".
482 */
483 struct DexParameterAnnotationsItem {
484 u4 methodIdx;
485 u4 annotationsOff; /* offset to DexAnotationSetRefList */
486 };
487
488 /*
489 * Direct-mapped "annotation_set_ref_item".
490 */
491 struct DexAnnotationSetRefItem {
492 u4 annotationsOff; /* offset to DexAnnotationSetItem */
493 };
494
495 /*
496 * Direct-mapped "annotation_set_ref_list".
497 */
498 struct DexAnnotationSetRefList {
499 u4 size;
500 DexAnnotationSetRefItem list[1];
501 };
502
503 /*
504 * Direct-mapped "annotation_set_item".
505 */
506 struct DexAnnotationSetItem {
507 u4 size;
508 u4 entries[1]; /* offset to DexAnnotationItem */
509 };
510
511 /*
512 * Direct-mapped "annotation_item".
513 *
514 * NOTE: this structure is byte-aligned.
515 */
516 struct DexAnnotationItem {
517 u1 visibility;
518 u1 annotation[1]; /* data in encoded_annotation format */
519 };
520
521 /*
522 * Direct-mapped "encoded_array".
523 *
524 * NOTE: this structure is byte-aligned.
525 */
526 struct DexEncodedArray {
527 u1 array[1]; /* data in encoded_array format */
528 };
529
530 /*
531 * Lookup table for classes. It provides a mapping from class name to
532 * class definition. Used by dexFindClass().
533 *
534 * We calculate this at DEX optimization time and embed it in the file so we
535 * don't need the same hash table in every VM. This is slightly slower than
536 * a hash table with direct pointers to the items, but because it's shared
537 * there's less of a penalty for using a fairly sparse table.
538 */
539 struct DexClassLookup {
540 int size; // total size, including "size"
541 int numEntries; // size of table[]; always power of 2
542 struct {
543 u4 classDescriptorHash; // class descriptor hash code
544 int classDescriptorOffset; // in bytes, from start of DEX
545 int classDefOffset; // in bytes, from start of DEX
546 } table[1];
547 };
548
549 /*
550 * Header added by DEX optimization pass. Values are always written in
551 * local byte and structure padding. The first field (magic + version)
552 * is guaranteed to be present and directly readable for all expected
553 * compiler configurations; the rest is version-dependent.
554 *
555 * Try to keep this simple and fixed-size.
556 */
557 struct DexOptHeader {
558 u1 magic[8]; /* includes version number */
559
560 u4 dexOffset; /* file offset of DEX header */
561 u4 dexLength;
562 u4 depsOffset; /* offset of optimized DEX dependency table */
563 u4 depsLength;
564 u4 optOffset; /* file offset of optimized data tables */
565 u4 optLength;
566
567 u4 flags; /* some info flags */
568 u4 checksum; /* adler32 checksum covering deps/opt */
569
570 /* pad for 64-bit alignment if necessary */
571 };
572
573 #define DEX_OPT_FLAG_BIG (1<<1) /* swapped to big-endian */
574
575 #define DEX_INTERFACE_CACHE_SIZE 128 /* must be power of 2 */
576
577 /*
578 * Structure representing a DEX file.
579 *
580 * Code should regard DexFile as opaque, using the API calls provided here
581 * to access specific structures.
582 */
583 struct DexFile {
584 /* directly-mapped "opt" header */
585 const DexOptHeader* pOptHeader;
586
587 /* pointers to directly-mapped structs and arrays in base DEX */
588 const DexHeader* pHeader;
589 const DexStringId* pStringIds;
590 const DexTypeId* pTypeIds;
591 const DexFieldId* pFieldIds;
592 const DexMethodId* pMethodIds;
593 const DexProtoId* pProtoIds;
594 const DexClassDef* pClassDefs;
595 const DexLink* pLinkData;
596
597 /*
598 * These are mapped out of the "auxillary" section, and may not be
599 * included in the file.
600 */
601 const DexClassLookup* pClassLookup;
602 const void* pRegisterMapPool; // RegisterMapClassPool
603
604 /* points to start of DEX file data */
605 const u1* baseAddr;
606
607 /* track memory overhead for auxillary structures */
608 int overhead;
609
610 /* additional app-specific data structures associated with the DEX */
611 //void* auxData;
612 };
613
614 /*
615 * Utility function -- rounds up to the nearest power of 2.
616 */
617 u4 dexRoundUpPower2(u4 val);
618
619 /*
620 * Parse an optimized or unoptimized .dex file sitting in memory.
621 *
622 * On success, return a newly-allocated DexFile.
623 */
624 DexFile* dexFileParse(const u1* data, size_t length, int flags);
625
626 /* bit values for "flags" argument to dexFileParse */
627 enum {
628 kDexParseDefault = 0,
629 kDexParseVerifyChecksum = 1,
630 kDexParseContinueOnError = (1 << 1),
631 };
632
633 /*
634 * Fix the byte ordering of all fields in the DEX file, and do
635 * structural verification. This is only required for code that opens
636 * "raw" DEX files, such as the DEX optimizer.
637 *
638 * Return 0 on success.
639 */
640 int dexSwapAndVerify(u1* addr, int len);
641
642 /*
643 * Detect the file type of the given memory buffer via magic number.
644 * Call dexSwapAndVerify() on an unoptimized DEX file, do nothing
645 * but return successfully on an optimized DEX file, and report an
646 * error for all other cases.
647 *
648 * Return 0 on success.
649 */
650 int dexSwapAndVerifyIfNecessary(u1* addr, size_t len);
651
652 /*
653 * Check to see if the file magic and format version in the given
654 * header are recognized as valid. Returns true if they are
655 * acceptable.
656 */
657 bool dexHasValidMagic(const DexHeader* pHeader);
658
659 /*
660 * Compute DEX checksum.
661 */
662 u4 dexComputeChecksum(const DexHeader* pHeader);
663
664 /*
665 * Free a DexFile structure, along with any associated structures.
666 */
667 void dexFileFree(DexFile* pDexFile);
668
669 /*
670 * Create class lookup table.
671 */
672 DexClassLookup* dexCreateClassLookup(DexFile* pDexFile);
673
674 /*
675 * Find a class definition by descriptor.
676 */
677 const DexClassDef* dexFindClass(const DexFile* pFile, const char* descriptor);
678
679 /*
680 * Set up the basic raw data pointers of a DexFile. This function isn't
681 * meant for general use.
682 */
683 void dexFileSetupBasicPointers(DexFile* pDexFile, const u1* data);
684
685 /* return the DexMapList of the file, if any */
dexGetMap(const DexFile * pDexFile)686 DEX_INLINE const DexMapList* dexGetMap(const DexFile* pDexFile) {
687 u4 mapOff = pDexFile->pHeader->mapOff;
688
689 if (mapOff == 0) {
690 return NULL;
691 } else {
692 return (const DexMapList*) (pDexFile->baseAddr + mapOff);
693 }
694 }
695
696 /* return the const char* string data referred to by the given string_id */
dexGetStringData(const DexFile * pDexFile,const DexStringId * pStringId)697 DEX_INLINE const char* dexGetStringData(const DexFile* pDexFile,
698 const DexStringId* pStringId) {
699 const u1* ptr = pDexFile->baseAddr + pStringId->stringDataOff;
700
701 // Skip the uleb128 length.
702 while (*(ptr++) > 0x7f) /* empty */ ;
703
704 return (const char*) ptr;
705 }
706 /* return the StringId with the specified index */
dexGetStringId(const DexFile * pDexFile,u4 idx)707 DEX_INLINE const DexStringId* dexGetStringId(const DexFile* pDexFile, u4 idx) {
708 assert(idx < pDexFile->pHeader->stringIdsSize);
709 return &pDexFile->pStringIds[idx];
710 }
711 /* return the UTF-8 encoded string with the specified string_id index */
dexStringById(const DexFile * pDexFile,u4 idx)712 DEX_INLINE const char* dexStringById(const DexFile* pDexFile, u4 idx) {
713 const DexStringId* pStringId = dexGetStringId(pDexFile, idx);
714 return dexGetStringData(pDexFile, pStringId);
715 }
716
717 /* Return the UTF-8 encoded string with the specified string_id index,
718 * also filling in the UTF-16 size (number of 16-bit code points).*/
719 const char* dexStringAndSizeById(const DexFile* pDexFile, u4 idx,
720 u4* utf16Size);
721
722 /* return the TypeId with the specified index */
dexGetTypeId(const DexFile * pDexFile,u4 idx)723 DEX_INLINE const DexTypeId* dexGetTypeId(const DexFile* pDexFile, u4 idx) {
724 assert(idx < pDexFile->pHeader->typeIdsSize);
725 return &pDexFile->pTypeIds[idx];
726 }
727
728 /*
729 * Get the descriptor string associated with a given type index.
730 * The caller should not free() the returned string.
731 */
dexStringByTypeIdx(const DexFile * pDexFile,u4 idx)732 DEX_INLINE const char* dexStringByTypeIdx(const DexFile* pDexFile, u4 idx) {
733 const DexTypeId* typeId = dexGetTypeId(pDexFile, idx);
734 return dexStringById(pDexFile, typeId->descriptorIdx);
735 }
736
737 /* return the MethodId with the specified index */
dexGetMethodId(const DexFile * pDexFile,u4 idx)738 DEX_INLINE const DexMethodId* dexGetMethodId(const DexFile* pDexFile, u4 idx) {
739 assert(idx < pDexFile->pHeader->methodIdsSize);
740 return &pDexFile->pMethodIds[idx];
741 }
742
743 /* return the FieldId with the specified index */
dexGetFieldId(const DexFile * pDexFile,u4 idx)744 DEX_INLINE const DexFieldId* dexGetFieldId(const DexFile* pDexFile, u4 idx) {
745 assert(idx < pDexFile->pHeader->fieldIdsSize);
746 return &pDexFile->pFieldIds[idx];
747 }
748
749 /* return the ProtoId with the specified index */
dexGetProtoId(const DexFile * pDexFile,u4 idx)750 DEX_INLINE const DexProtoId* dexGetProtoId(const DexFile* pDexFile, u4 idx) {
751 assert(idx < pDexFile->pHeader->protoIdsSize);
752 return &pDexFile->pProtoIds[idx];
753 }
754
755 /*
756 * Get the parameter list from a ProtoId. The returns NULL if the ProtoId
757 * does not have a parameter list.
758 */
dexGetProtoParameters(const DexFile * pDexFile,const DexProtoId * pProtoId)759 DEX_INLINE const DexTypeList* dexGetProtoParameters(
760 const DexFile *pDexFile, const DexProtoId* pProtoId) {
761 if (pProtoId->parametersOff == 0) {
762 return NULL;
763 }
764 return (const DexTypeList*)
765 (pDexFile->baseAddr + pProtoId->parametersOff);
766 }
767
768 /* return the ClassDef with the specified index */
dexGetClassDef(const DexFile * pDexFile,u4 idx)769 DEX_INLINE const DexClassDef* dexGetClassDef(const DexFile* pDexFile, u4 idx) {
770 assert(idx < pDexFile->pHeader->classDefsSize);
771 return &pDexFile->pClassDefs[idx];
772 }
773
774 /* given a ClassDef pointer, recover its index */
dexGetIndexForClassDef(const DexFile * pDexFile,const DexClassDef * pClassDef)775 DEX_INLINE u4 dexGetIndexForClassDef(const DexFile* pDexFile,
776 const DexClassDef* pClassDef)
777 {
778 assert(pClassDef >= pDexFile->pClassDefs &&
779 pClassDef < pDexFile->pClassDefs + pDexFile->pHeader->classDefsSize);
780 return pClassDef - pDexFile->pClassDefs;
781 }
782
783 /* get the interface list for a DexClass */
dexGetInterfacesList(const DexFile * pDexFile,const DexClassDef * pClassDef)784 DEX_INLINE const DexTypeList* dexGetInterfacesList(const DexFile* pDexFile,
785 const DexClassDef* pClassDef)
786 {
787 if (pClassDef->interfacesOff == 0)
788 return NULL;
789 return (const DexTypeList*)
790 (pDexFile->baseAddr + pClassDef->interfacesOff);
791 }
792 /* return the Nth entry in a DexTypeList. */
dexGetTypeItem(const DexTypeList * pList,u4 idx)793 DEX_INLINE const DexTypeItem* dexGetTypeItem(const DexTypeList* pList,
794 u4 idx)
795 {
796 assert(idx < pList->size);
797 return &pList->list[idx];
798 }
799 /* return the type_idx for the Nth entry in a TypeList */
dexTypeListGetIdx(const DexTypeList * pList,u4 idx)800 DEX_INLINE u4 dexTypeListGetIdx(const DexTypeList* pList, u4 idx) {
801 const DexTypeItem* pItem = dexGetTypeItem(pList, idx);
802 return pItem->typeIdx;
803 }
804
805 /* get the static values list for a DexClass */
dexGetStaticValuesList(const DexFile * pDexFile,const DexClassDef * pClassDef)806 DEX_INLINE const DexEncodedArray* dexGetStaticValuesList(
807 const DexFile* pDexFile, const DexClassDef* pClassDef)
808 {
809 if (pClassDef->staticValuesOff == 0)
810 return NULL;
811 return (const DexEncodedArray*)
812 (pDexFile->baseAddr + pClassDef->staticValuesOff);
813 }
814
815 /* get the annotations directory item for a DexClass */
dexGetAnnotationsDirectoryItem(const DexFile * pDexFile,const DexClassDef * pClassDef)816 DEX_INLINE const DexAnnotationsDirectoryItem* dexGetAnnotationsDirectoryItem(
817 const DexFile* pDexFile, const DexClassDef* pClassDef)
818 {
819 if (pClassDef->annotationsOff == 0)
820 return NULL;
821 return (const DexAnnotationsDirectoryItem*)
822 (pDexFile->baseAddr + pClassDef->annotationsOff);
823 }
824
825 /* get the source file string */
dexGetSourceFile(const DexFile * pDexFile,const DexClassDef * pClassDef)826 DEX_INLINE const char* dexGetSourceFile(
827 const DexFile* pDexFile, const DexClassDef* pClassDef)
828 {
829 if (pClassDef->sourceFileIdx == 0xffffffff)
830 return NULL;
831 return dexStringById(pDexFile, pClassDef->sourceFileIdx);
832 }
833
834 /* get the size, in bytes, of a DexCode */
835 size_t dexGetDexCodeSize(const DexCode* pCode);
836
837 /* Get the list of "tries" for the given DexCode. */
dexGetTries(const DexCode * pCode)838 DEX_INLINE const DexTry* dexGetTries(const DexCode* pCode) {
839 const u2* insnsEnd = &pCode->insns[pCode->insnsSize];
840
841 // Round to four bytes.
842 if ((((uintptr_t) insnsEnd) & 3) != 0) {
843 insnsEnd++;
844 }
845
846 return (const DexTry*) insnsEnd;
847 }
848
849 /* Get the base of the encoded data for the given DexCode. */
dexGetCatchHandlerData(const DexCode * pCode)850 DEX_INLINE const u1* dexGetCatchHandlerData(const DexCode* pCode) {
851 const DexTry* pTries = dexGetTries(pCode);
852 return (const u1*) &pTries[pCode->triesSize];
853 }
854
855 /* get a pointer to the start of the debugging data */
dexGetDebugInfoStream(const DexFile * pDexFile,const DexCode * pCode)856 DEX_INLINE const u1* dexGetDebugInfoStream(const DexFile* pDexFile,
857 const DexCode* pCode)
858 {
859 if (pCode->debugInfoOff == 0) {
860 return NULL;
861 } else {
862 return pDexFile->baseAddr + pCode->debugInfoOff;
863 }
864 }
865
866 /* DexClassDef convenience - get class descriptor */
dexGetClassDescriptor(const DexFile * pDexFile,const DexClassDef * pClassDef)867 DEX_INLINE const char* dexGetClassDescriptor(const DexFile* pDexFile,
868 const DexClassDef* pClassDef)
869 {
870 return dexStringByTypeIdx(pDexFile, pClassDef->classIdx);
871 }
872
873 /* DexClassDef convenience - get superclass descriptor */
dexGetSuperClassDescriptor(const DexFile * pDexFile,const DexClassDef * pClassDef)874 DEX_INLINE const char* dexGetSuperClassDescriptor(const DexFile* pDexFile,
875 const DexClassDef* pClassDef)
876 {
877 if (pClassDef->superclassIdx == 0)
878 return NULL;
879 return dexStringByTypeIdx(pDexFile, pClassDef->superclassIdx);
880 }
881
882 /* DexClassDef convenience - get class_data_item pointer */
dexGetClassData(const DexFile * pDexFile,const DexClassDef * pClassDef)883 DEX_INLINE const u1* dexGetClassData(const DexFile* pDexFile,
884 const DexClassDef* pClassDef)
885 {
886 if (pClassDef->classDataOff == 0)
887 return NULL;
888 return (const u1*) (pDexFile->baseAddr + pClassDef->classDataOff);
889 }
890
891 /* Get an annotation set at a particular offset. */
dexGetAnnotationSetItem(const DexFile * pDexFile,u4 offset)892 DEX_INLINE const DexAnnotationSetItem* dexGetAnnotationSetItem(
893 const DexFile* pDexFile, u4 offset)
894 {
895 if (offset == 0) {
896 return NULL;
897 }
898 return (const DexAnnotationSetItem*) (pDexFile->baseAddr + offset);
899 }
900 /* get the class' annotation set */
dexGetClassAnnotationSet(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)901 DEX_INLINE const DexAnnotationSetItem* dexGetClassAnnotationSet(
902 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
903 {
904 return dexGetAnnotationSetItem(pDexFile, pAnnoDir->classAnnotationsOff);
905 }
906
907 /* get the class' field annotation list */
dexGetFieldAnnotations(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)908 DEX_INLINE const DexFieldAnnotationsItem* dexGetFieldAnnotations(
909 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
910 {
911 (void) pDexFile;
912 if (pAnnoDir->fieldsSize == 0)
913 return NULL;
914
915 // Skip past the header to the start of the field annotations.
916 return (const DexFieldAnnotationsItem*) &pAnnoDir[1];
917 }
918
919 /* get field annotation list size */
dexGetFieldAnnotationsSize(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)920 DEX_INLINE int dexGetFieldAnnotationsSize(const DexFile* pDexFile,
921 const DexAnnotationsDirectoryItem* pAnnoDir)
922 {
923 (void) pDexFile;
924 return pAnnoDir->fieldsSize;
925 }
926
927 /* return a pointer to the field's annotation set */
dexGetFieldAnnotationSetItem(const DexFile * pDexFile,const DexFieldAnnotationsItem * pItem)928 DEX_INLINE const DexAnnotationSetItem* dexGetFieldAnnotationSetItem(
929 const DexFile* pDexFile, const DexFieldAnnotationsItem* pItem)
930 {
931 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
932 }
933
934 /* get the class' method annotation list */
dexGetMethodAnnotations(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)935 DEX_INLINE const DexMethodAnnotationsItem* dexGetMethodAnnotations(
936 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
937 {
938 (void) pDexFile;
939 if (pAnnoDir->methodsSize == 0)
940 return NULL;
941
942 /*
943 * Skip past the header and field annotations to the start of the
944 * method annotations.
945 */
946 const u1* addr = (const u1*) &pAnnoDir[1];
947 addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
948 return (const DexMethodAnnotationsItem*) addr;
949 }
950
951 /* get method annotation list size */
dexGetMethodAnnotationsSize(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)952 DEX_INLINE int dexGetMethodAnnotationsSize(const DexFile* pDexFile,
953 const DexAnnotationsDirectoryItem* pAnnoDir)
954 {
955 (void) pDexFile;
956 return pAnnoDir->methodsSize;
957 }
958
959 /* return a pointer to the method's annotation set */
dexGetMethodAnnotationSetItem(const DexFile * pDexFile,const DexMethodAnnotationsItem * pItem)960 DEX_INLINE const DexAnnotationSetItem* dexGetMethodAnnotationSetItem(
961 const DexFile* pDexFile, const DexMethodAnnotationsItem* pItem)
962 {
963 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
964 }
965
966 /* get the class' parameter annotation list */
dexGetParameterAnnotations(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)967 DEX_INLINE const DexParameterAnnotationsItem* dexGetParameterAnnotations(
968 const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
969 {
970 (void) pDexFile;
971 if (pAnnoDir->parametersSize == 0)
972 return NULL;
973
974 /*
975 * Skip past the header, field annotations, and method annotations
976 * to the start of the parameter annotations.
977 */
978 const u1* addr = (const u1*) &pAnnoDir[1];
979 addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
980 addr += pAnnoDir->methodsSize * sizeof (DexMethodAnnotationsItem);
981 return (const DexParameterAnnotationsItem*) addr;
982 }
983
984 /* get method annotation list size */
dexGetParameterAnnotationsSize(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)985 DEX_INLINE int dexGetParameterAnnotationsSize(const DexFile* pDexFile,
986 const DexAnnotationsDirectoryItem* pAnnoDir)
987 {
988 (void) pDexFile;
989 return pAnnoDir->parametersSize;
990 }
991
992 /* return the parameter annotation ref list */
dexGetParameterAnnotationSetRefList(const DexFile * pDexFile,const DexParameterAnnotationsItem * pItem)993 DEX_INLINE const DexAnnotationSetRefList* dexGetParameterAnnotationSetRefList(
994 const DexFile* pDexFile, const DexParameterAnnotationsItem* pItem)
995 {
996 if (pItem->annotationsOff == 0) {
997 return NULL;
998 }
999 return (const DexAnnotationSetRefList*) (pDexFile->baseAddr + pItem->annotationsOff);
1000 }
1001
1002 /* get method annotation list size */
dexGetParameterAnnotationSetRefSize(const DexFile * pDexFile,const DexParameterAnnotationsItem * pItem)1003 DEX_INLINE int dexGetParameterAnnotationSetRefSize(const DexFile* pDexFile,
1004 const DexParameterAnnotationsItem* pItem)
1005 {
1006 if (pItem->annotationsOff == 0) {
1007 return 0;
1008 }
1009 return dexGetParameterAnnotationSetRefList(pDexFile, pItem)->size;
1010 }
1011
1012 /* return the Nth entry from an annotation set ref list */
dexGetParameterAnnotationSetRef(const DexAnnotationSetRefList * pList,u4 idx)1013 DEX_INLINE const DexAnnotationSetRefItem* dexGetParameterAnnotationSetRef(
1014 const DexAnnotationSetRefList* pList, u4 idx)
1015 {
1016 assert(idx < pList->size);
1017 return &pList->list[idx];
1018 }
1019
1020 /* given a DexAnnotationSetRefItem, return the DexAnnotationSetItem */
dexGetSetRefItemItem(const DexFile * pDexFile,const DexAnnotationSetRefItem * pItem)1021 DEX_INLINE const DexAnnotationSetItem* dexGetSetRefItemItem(
1022 const DexFile* pDexFile, const DexAnnotationSetRefItem* pItem)
1023 {
1024 return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
1025 }
1026
1027 /* return the Nth annotation offset from a DexAnnotationSetItem */
dexGetAnnotationOff(const DexAnnotationSetItem * pAnnoSet,u4 idx)1028 DEX_INLINE u4 dexGetAnnotationOff(
1029 const DexAnnotationSetItem* pAnnoSet, u4 idx)
1030 {
1031 assert(idx < pAnnoSet->size);
1032 return pAnnoSet->entries[idx];
1033 }
1034
1035 /* return the Nth annotation item from a DexAnnotationSetItem */
dexGetAnnotationItem(const DexFile * pDexFile,const DexAnnotationSetItem * pAnnoSet,u4 idx)1036 DEX_INLINE const DexAnnotationItem* dexGetAnnotationItem(
1037 const DexFile* pDexFile, const DexAnnotationSetItem* pAnnoSet, u4 idx)
1038 {
1039 u4 offset = dexGetAnnotationOff(pAnnoSet, idx);
1040 if (offset == 0) {
1041 return NULL;
1042 }
1043 return (const DexAnnotationItem*) (pDexFile->baseAddr + offset);
1044 }
1045
1046 /*
1047 * Get the type descriptor character associated with a given primitive
1048 * type. This returns '\0' if the type is invalid.
1049 */
1050 char dexGetPrimitiveTypeDescriptorChar(PrimitiveType type);
1051
1052 /*
1053 * Get the type descriptor string associated with a given primitive
1054 * type.
1055 */
1056 const char* dexGetPrimitiveTypeDescriptor(PrimitiveType type);
1057
1058 /*
1059 * Get the boxed type descriptor string associated with a given
1060 * primitive type. This returns NULL for an invalid type, including
1061 * particularly for type "void". In the latter case, even though there
1062 * is a class Void, there's no such thing as a boxed instance of it.
1063 */
1064 const char* dexGetBoxedTypeDescriptor(PrimitiveType type);
1065
1066 /*
1067 * Get the primitive type constant from the given descriptor character.
1068 * This returns PRIM_NOT (note: this is a 0) if the character is invalid
1069 * as a primitive type descriptor.
1070 */
1071 PrimitiveType dexGetPrimitiveTypeFromDescriptorChar(char descriptorChar);
1072
1073 #endif // LIBDEX_DEXFILE_H_
1074