Android - DEX - 文件格式详解

2019-03-27 17:56

DEX文件格式

0. 实验

$ vi test.java class test{

public static void main(String[] argc){ System.out.println(\ } }

$ javac test.java

$ dx --dex --output=test.dex test.class $ hexdump test.dex

0000000 6564 0a78 3330 0035 5eb4 4f7a 94e6 65f0 0000010 fb3e d5f3 e185 dd62 fce7 c887 a7ec 5329 0000020 02d8 0000 0070 0000 5678 1234 0000 0000 0000030 0000 0000 0238 0000 000e 0000 0070 0000 0000040 0007 0000 00a8 0000 0003 0000 00c4 0000 0000050 0001 0000 00e8 0000 0004 0000 00f0 0000 0000060 0001 0000 0110 0000 01a8 0000 0130 0000 0000070 0176 0000 017e 0000 0195 0000 01a9 0000 0000080 01bd 0000 01d1 0000 01d9 0000 01dc 0000 0000090 01e0 0000 01f5 0000 01fb 0000 0200 0000 00000a0 0209 0000 0210 0000 0001 0000 0002 0000 00000b0 0003 0000 0004 0000 0005 0000 0006 0000 00000c0 0008 0000 0006 0000 0005 0000 0000 0000 00000d0 0007 0000 0005 0000 0168 0000 0007 0000 00000e0 0005 0000 0170 0000 0003 0000 000a 0000 00000f0 0000 0001 000b 0000 0001 0000 0000 0000 0000100 0004 0000 0000 0000 0004 0002 0009 0000 0000110 0004 0000 0000 0000 0001 0000 0000 0000 0000120 000d 0000 0000 0000 0227 0000 0000 0000 0000130 0001 0001 0001 0000 021b 0000 0004 0000 0000140 1070 0001 0000 000e 0003 0001 0002 0000 0000150 0220 0000 0008 0000 0062 0000 011a 000c 0000160 206e 0000 0010 000e 0001 0000 0002 0000 0000170 0001 0000 0006 3c06 6e69 7469 003e 4c15 0000180 616a 6176 692f 2f6f 7250 6e69 5374 7274 0000190 6165 3b6d 1200 6a4c 7661 2f61 616c 676e 00001a0 4f2f 6a62 6365 3b74 1200 6a4c 7661 2f61 00001b0 616c 676e 532f 7274 6e69 3b67 1200 6a4c 00001c0 7661 2f61 616c 676e 532f 7379 6574 3b6d 00001d0 0600 744c 7365 3b74 0100 0056 5602 004c 00001e0 5b13 6a4c 7661 2f61 616c 676e 532f 7274 00001f0 6e69 3b67 0400 616d 6e69 0300 756f 0074 0000200 7007 6972 746e 6e6c 0500 6574 7473 0021

0000210 7409 7365 2e74 616a 6176 0100 0700 000e 0000220 0103 0700 780e 0000 0200 0200 8080 b004 0000230 0102 c809 0002 0000 000d 0000 0000 0000 0000240 0001 0000 0000 0000 0001 0000 000e 0000 0000250 0070 0000 0002 0000 0007 0000 00a8 0000 0000260 0003 0000 0003 0000 00c4 0000 0004 0000 0000270 0001 0000 00e8 0000 0005 0000 0004 0000 0000280 00f0 0000 0006 0000 0001 0000 0110 0000 0000290 2001 0000 0002 0000 0130 0000 1001 0000 00002a0 0002 0000 0168 0000 2002 0000 000e 0000 00002b0 0176 0000 2003 0000 0002 0000 021b 0000 00002c0 2000 0000 0001 0000 0227 0000 1000 0000 00002d0 0001 0000 0238 0000 00002d8

1. map_list

map_list数据结构为: Name Format Description size uint size of the list, in entries list map_item[size] elements of the list 第一项为map_list的大小,其中map_item的结构为: Name Format Description type ushort type of the items; see table below unused ushort (unused) count of the number of items to be found at the size uint indicated offset offset from the start of the file to the items in offset uint question

type的值如下表:

Item Type Constant Value header_item TYPE_HEADER_ITEM 0x0000 string_id_item TYPE_STRING_ID_ITEM 0x0001 type_id_item TYPE_TYPE_ID_ITEM 0x0002 proto_id_item TYPE_PROTO_ID_ITEM 0x0003 field_id_item TYPE_FIELD_ID_ITEM 0x0004 method_id_item TYPE_METHOD_ID_ITEM 0x0005 class_def_item TYPE_CLASS_DEF_ITEM 0x0006 map_list TYPE_MAP_LIST 0x1000 type_list TYPE_TYPE_LIST 0x1001 annotation_set_ref_list TYPE_ANNOTATION_SET_REF_LIST 0x1002 annotation_set_item TYPE_ANNOTATION_SET_ITEM 0x1003 class_data_item TYPE_CLASS_DATA_ITEM 0x2000 code_item TYPE_CODE_ITEM 0x2001 string_data_item TYPE_STRING_DATA_ITEM 0x2002 debug_info_item TYPE_DEBUG_INFO_ITEM 0x2003 Item Size In Bytes 0x70 0x04 0x04 0x0c 0x08 0x08 0x20 4 + (item.size * 12) 4 + (item.size * 2) 4 + (item.size * 4) 4 + (item.size * 4) implicit; must parse implicit; must parse implicit; must parse implicit; must parse annotation_item TYPE_ANNOTATION_ITEM 0x2004 implicit; must parse encoded_array_item TYPE_ENCODED_ARRAY_ITEM 0x2005 implicit; must parse annotations_directory_item TYPE_ANNOTATIONS_DIRECTORY_ITEM 0x2006 implicit; must parse

这个map_list有13个map_item,分别是: 值 type size offset 0x0000 header_item 0x1 0x0 0x0001 string_id_item 0xe 0x70 0x0002 type_id_item 0x7 0xa8 0x0003 proto_id_item 0x3 0xc4 0x0004 field_id_item 0x1 0xe8 0x0005 method_id_item 0x4 0xf0 0x0006 class_def_item 0x1 0x110 0x2001 code_item 0x2 0x130 0x1001 type_list 0x2 0x168 0x2002 string_data_item 0xe 0x176 0x2003 debug_info_item 0x2 0x21b 0x2000 class_data_item 0x1 0x227 0x1000 map_list 0x1 0x238 发现这个表中的size和offset和header_item中的值一致。

2. string_id_item

批注10得出string id列表的位置为0x70,批注9得出string id列表中string_id_item的数量为0xe。string id的结构为string_id_item: Name Format Description offset from the start of the file to the string data for this item. The offset should be to a location in the data section, and the data should be in the format specified by \for the offset. string_data_off uint string_data_off指向string的数据,string的数据的结构为string_data_item: Name Format Description size of this string, in UTF-16 code units (which is the \length%utf16_size uleb128 the string. (The encoded length is implied by the position of the 0 byte.)字符串的字节个数。 data

a series of MUTF-8 code units (a.k.a. octets, a.k.a. bytes) followed by a byte of value 0. See \Encoding\above for details and discussion about the data format. Note: It is acceptable to have a string which includes (the encoded

ubyte[] form of) UTF-16 surrogate code units (that is, U+d800 ? U+dfff)

either in isolation or out-of-order with respect to the usual encoding of Unicode into UTF-16. It is up to higher-level uses of strings to reject such invalid encodings, if appropriate. 字符串的实际数据。

LEB128

每个LEB128由1到5个字节组成,所有字节组合到一起代表一个32位值。除了最后一个字节的最高标志位为0,其它的为1.剩下的7位为有效负荷,第二个字节的7位接上。有符号LEB128的符号由最后字节的有效负荷最高位决定。如下:

如果是有符号的LEB128,符号位取决于bit13。如下举例: Encoded Sequence As sleb128 As uleb128 0 0 0 1 1 1 7f -1 127 80 7f -128 16256 uleb128p1的值加1为uleb128。 算法参见附录1。

As uleb128p1 -1 0 126 16255 从文件的0x70得出string id列表如下,共有14个string_id_item。 0176 0000 017e 0000 0195 0000 01a9 0000 01bd 0000 01d1 0000 01d9 0000 01dc 0000 01e0 0000 01f5 0000 01fb 0000 0200 0000 0209 0000 0210 0000 例如:

1)String Data在0x176处,可以从文件的0x176得到以下数据,以0结尾。 3c06 6e69 7469 003e

先读取第一个字节为0x06,得出String Data的长度为6,所以String Data的ASCII码序列为:3c 69 6e 69 74 3e 得到:

2)String Data在0x17e处,可以从文件的0x17e得到以下数据,以0结尾。 4c15 616a 6176 692f 2f6f 7250 6e69 5374 7274 6165 3b6d 1200

先读取第一个字节为0x15,得出String Data的长度为21,所以String Data的ASCII码序列为: 4c 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d 3b 得到:Ljava/io/PrintStream;

以此类推,可得其它String Data。

3) 6a4c 7661 2f61 616c 676e 4f2f 6a62 6365 3b74 得到:Ljava/lang/Object; 4) 6a4c 7661 2f61 616c 676e 532f 7274 6e69 3b67 得到:Ljava/lang/String; 5) 6a4c 7661 2f61 616c 676e 532f 7379 6574 3b6d 得到:Ljava/lang/System; 6) 744c 7365 3b74 得到:Ltest; 7) 56 得到:V 8) 56 4c 得到:VL

9) 5b 6a4c 7661 2f61 616c 676e 532f 7274 6e69 3b67得到:[Ljava/lang/String; 10) 616d 6e69 得到:main 11) 756f 74 得到:out

12) 70 6972 746e 6e6c 得到:println 13) 6574 7473 21 得到:test!

14) 74 7365 2e74 616a 6176 得到:test.java 得出索引如下表: 3. type_id_item

批注12得出type id列表的位置为0xa8,批注11得出type id列表中type_id_item的数量为0x7。type id的结构为type_id_item: Name Format Description index into the string_ids list for the descriptor string of this descriptor_idx uint type. The string must conform to the syntax for TypeDescriptor, defined above. descriptor_idx为String id列表的索引。

索引为:0001 0000 0002 0000 0003 0000 0004 0000 0005 0000 0006 0000 0008 0000

依次代表:Ljava/io/PrintStream; Ljava/lang/Object; Ljava/lang/String; Ljava/lang/System; Ltest; V [Ljava/lang/String;

Type_id_list列表如下:

Ljava/io/PrintStream; 0 Ljava/lang/Object; 1 Ljava/lang/String; 2 Ljava/lang/System; 3 Ltest; 4 V 5 [Ljava/lang/String; 6

4. proto_id_item

批注14得出prototype id列表的位置为0xc4,批注13的处prototype id列表中proto_id_item的数量为0x3。prototype id的结构为proto_id_item:

Name Format Description index into the string_ids list for the short-form descriptor string of this prototype. The string must conform to the syntax for ShortyDescriptor, defined above, and must correspond to the return type and parameters of this item. shorty_idx uint


Android - DEX - 文件格式详解.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:政法综治责任状汇编

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: