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 * Direct-mapped "method_handle_item"
375 */
376 struct 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 */
386 struct DexTypeItem {
387 u2 typeIdx; /* index into typeIds */
388 };
389
390 /*
391 * Direct-mapped "type_list".
392 */
393 struct 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 */
405 struct 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 */
422 struct 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 */
431 struct DexLink {
432 u1 bleargh;
433 };
434
435
436 /*
437 * Direct-mapped "annotations_directory_item".
438 */
439 struct 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 */
452 struct DexFieldAnnotationsItem {
453 u4 fieldIdx;
454 u4 annotationsOff; /* offset to DexAnnotationSetItem */
455 };
456
457 /*
458 * Direct-mapped "method_annotations_item".
459 */
460 struct DexMethodAnnotationsItem {
461 u4 methodIdx;
462 u4 annotationsOff; /* offset to DexAnnotationSetItem */
463 };
464
465 /*
466 * Direct-mapped "parameter_annotations_item".
467 */
468 struct DexParameterAnnotationsItem {
469 u4 methodIdx;
470 u4 annotationsOff; /* offset to DexAnotationSetRefList */
471 };
472
473 /*
474 * Direct-mapped "annotation_set_ref_item".
475 */
476 struct DexAnnotationSetRefItem {
477 u4 annotationsOff; /* offset to DexAnnotationSetItem */
478 };
479
480 /*
481 * Direct-mapped "annotation_set_ref_list".
482 */
483 struct DexAnnotationSetRefList {
484 u4 size;
485 DexAnnotationSetRefItem list[1];
486 };
487
488 /*
489 * Direct-mapped "annotation_set_item".
490 */
491 struct 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 */
501 struct 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 */
511 struct 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 */
524 struct 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 */
542 struct 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 */
568 struct 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 */
602 u4 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 */
609 DexFile* dexFileParse(const u1* data, size_t length, int flags);
610
611 /* bit values for "flags" argument to dexFileParse */
612 enum {
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 */
625 int 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 */
635 int 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 */
642 bool dexHasValidMagic(const DexHeader* pHeader);
643
644 /*
645 * Compute DEX checksum.
646 */
647 u4 dexComputeChecksum(const DexHeader* pHeader);
648
649 /*
650 * Free a DexFile structure, along with any associated structures.
651 */
652 void dexFileFree(DexFile* pDexFile);
653
654 /*
655 * Create class lookup table.
656 */
657 DexClassLookup* dexCreateClassLookup(DexFile* pDexFile);
658
659 /*
660 * Find a class definition by descriptor.
661 */
662 const 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 */
668 void dexFileSetupBasicPointers(DexFile* pDexFile, const u1* data);
669
670 /* return the DexMapList of the file, if any */
dexGetMap(const DexFile * pDexFile)671 DEX_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 */
dexGetStringData(const DexFile * pDexFile,const DexStringId * pStringId)682 DEX_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 */
dexGetStringId(const DexFile * pDexFile,u4 idx)692 DEX_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 */
dexStringById(const DexFile * pDexFile,u4 idx)697 DEX_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).*/
704 const char* dexStringAndSizeById(const DexFile* pDexFile, u4 idx,
705 u4* utf16Size);
706
707 /* return the TypeId with the specified index */
dexGetTypeId(const DexFile * pDexFile,u4 idx)708 DEX_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 */
dexStringByTypeIdx(const DexFile * pDexFile,u4 idx)717 DEX_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 */
dexGetMethodId(const DexFile * pDexFile,u4 idx)723 DEX_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 */
dexGetFieldId(const DexFile * pDexFile,u4 idx)729 DEX_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 */
dexGetProtoId(const DexFile * pDexFile,u4 idx)735 DEX_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 */
dexGetProtoParameters(const DexFile * pDexFile,const DexProtoId * pProtoId)744 DEX_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 */
dexGetClassDef(const DexFile * pDexFile,u4 idx)754 DEX_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 */
dexGetIndexForClassDef(const DexFile * pDexFile,const DexClassDef * pClassDef)760 DEX_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 */
dexGetInterfacesList(const DexFile * pDexFile,const DexClassDef * pClassDef)769 DEX_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. */
dexGetTypeItem(const DexTypeList * pList,u4 idx)778 DEX_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 */
dexTypeListGetIdx(const DexTypeList * pList,u4 idx)785 DEX_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 */
dexGetStaticValuesList(const DexFile * pDexFile,const DexClassDef * pClassDef)791 DEX_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 */
dexGetAnnotationsDirectoryItem(const DexFile * pDexFile,const DexClassDef * pClassDef)801 DEX_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 */
dexGetSourceFile(const DexFile * pDexFile,const DexClassDef * pClassDef)811 DEX_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 */
820 size_t dexGetDexCodeSize(const DexCode* pCode);
821
822 /* Get the list of "tries" for the given DexCode. */
dexGetTries(const DexCode * pCode)823 DEX_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. */
dexGetCatchHandlerData(const DexCode * pCode)835 DEX_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 */
dexGetDebugInfoStream(const DexFile * pDexFile,const DexCode * pCode)841 DEX_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 */
dexGetClassDescriptor(const DexFile * pDexFile,const DexClassDef * pClassDef)852 DEX_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 */
dexGetSuperClassDescriptor(const DexFile * pDexFile,const DexClassDef * pClassDef)859 DEX_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 */
dexGetClassData(const DexFile * pDexFile,const DexClassDef * pClassDef)868 DEX_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. */
dexGetAnnotationSetItem(const DexFile * pDexFile,u4 offset)877 DEX_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 */
dexGetClassAnnotationSet(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)886 DEX_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 */
dexGetFieldAnnotations(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)893 DEX_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 */
dexGetFieldAnnotationsSize(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)905 DEX_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 */
dexGetFieldAnnotationSetItem(const DexFile * pDexFile,const DexFieldAnnotationsItem * pItem)913 DEX_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 */
dexGetMethodAnnotations(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)920 DEX_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 */
dexGetMethodAnnotationsSize(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)937 DEX_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 */
dexGetMethodAnnotationSetItem(const DexFile * pDexFile,const DexMethodAnnotationsItem * pItem)945 DEX_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 */
dexGetParameterAnnotations(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)952 DEX_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 */
dexGetParameterAnnotationsSize(const DexFile * pDexFile,const DexAnnotationsDirectoryItem * pAnnoDir)970 DEX_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 */
dexGetParameterAnnotationSetRefList(const DexFile * pDexFile,const DexParameterAnnotationsItem * pItem)978 DEX_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 */
dexGetParameterAnnotationSetRefSize(const DexFile * pDexFile,const DexParameterAnnotationsItem * pItem)988 DEX_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 */
dexGetParameterAnnotationSetRef(const DexAnnotationSetRefList * pList,u4 idx)998 DEX_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 */
dexGetSetRefItemItem(const DexFile * pDexFile,const DexAnnotationSetRefItem * pItem)1006 DEX_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 */
dexGetAnnotationOff(const DexAnnotationSetItem * pAnnoSet,u4 idx)1013 DEX_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 */
dexGetAnnotationItem(const DexFile * pDexFile,const DexAnnotationSetItem * pAnnoSet,u4 idx)1021 DEX_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 */
1035 char dexGetPrimitiveTypeDescriptorChar(PrimitiveType type);
1036
1037 /*
1038 * Get the type descriptor string associated with a given primitive
1039 * type.
1040 */
1041 const 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 */
1049 const 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 */
1056 PrimitiveType dexGetPrimitiveTypeFromDescriptorChar(char descriptorChar);
1057
1058 #endif // LIBDEX_DEXFILE_H_
1059