Dex文件头及解析机制分析

Dex文件头主要包括校验和以及其他结构的偏移地址和长度信息。

字段名称 偏移值 长度 描述
magic 0x0 8 'Magic'值,即魔数字段,格式如”dex/n035/0”,其中的035表示结构的版本。
checksum 0x8 4 校验码。
signature 0xC 20 SHA-1签名。
file_size 0x20 4 Dex文件的总长度。
header_size 0x24 4 文件头长度,009版本=0x5C,035版本=0x70。
endian_tag 0x28 4 标识字节顺序的常量,根据这个常量可以判断文件是否交换了字节顺序,缺省情况下=0x78563412。
link_size 0x2C 4 连接段的大小,如果为0就表示是静态连接。
link_off 0x30 4 连接段的开始位置,从本文件头开始算起。如果连接段的大小为0,这里也是0。
map_off 0x34 4 map数据基地址。
string_ids_size 0x38 4 字符串列表的字符串个数。
string_ids_off 0x3C 4 字符串列表表基地址。
type_ids_size 0x40 4 类型列表里类型个数。
type_ids_off 0x44 4 类型列表基地址。
proto_ids_size 0x48 4 原型列表里原型个数。
proto_ids_off 0x4C 4 原型列表基地址。
field_ids_size 0x50 4 字段列表里字段个数。
field_ids_off 0x54 4 字段列表基地址。
method_ids_size 0x58 4 方法列表里方法个数。
method_ids_off 0x5C 4 方法列表基地址。
class_defs_size 0x60 4 类定义类表中类的个数。
class_defs_off 0x64 4 类定义列表基地址。
data_size 0x68 4 数据段的大小,必须以4字节对齐。
data_off 0x6C 4 数据段基地址

魔数字段

     魔数字段,主要就是Dex文件的标识符,它占用4个字节,在目前的源码里是 “dex\n”,它的作用主要是用来标识dex文件的,比如有一个文件也以dex为后缀名,仅此并不会被认为是Davlik虚拟机运行的文件,还要判断这 四个字节。另外Davlik虚拟机也有优化的Dex,也是通过个字段来区分的,当它是优化的Dex文件时,它的值就变成”dey\n”了。根据这四个字 节,就可以识别不同类型的Dex文件了。

      跟在“dex\n”后面的是版本字段,主要用来标识Dex文件的版本。目前支持的版本号为“035\0”,不管是否优化的版本,都是使用这个版本号。

检验码字段

     主要用来检查从这个字段开始到文件结尾,这段数据是否完整,有没有人修改过,或者传送过程中是否有出错等等。通常用来检查数据是否完整的算法,有 CRC32、有SHA128等,但这里采用并不是这两类,而采用一个比较特别的算法,叫做adler32,这是在开源zlib里常用的算法,用来检查文件 是否完整性。该算法由MarkAdler发明,其可靠程度跟CRC32差不多,不过还是弱一点点,但它有一个很好的优点,就是使用软件来计算检验码时比较 CRC32要快很多。可见Android系统,就算法上就已经为移动设备进行优化了。

     Java中可使用java.util.zip.Adler32类做校验操作

SHA-1签名字段

     dex文件头里,前面已经有了面有一个4字节的检验字段码了,为什么还会有SHA-1签名字段呢?不是重复了吗?可是仔细考虑一下,这样设计自有道理。因 为dex文件一般都不是很小,简单的应用程序都有几十K,这么多数据使用一个4字节的检验码,重复的机率还是有的,也就是说当文件里的数据修改了,还是很 有可能检验不出来的。这时检验码就失去了作用,需要使用更加强大的检验码,这就是SHA-1。SHA-1校验码有20个字节,比前面的检验码多了16个字 节,几乎不会不同的文件计算出来的检验是一样的。设计两个检验码的目的,就是先使用第一个检验码进行快速检查,这样可以先把简单出错的dex文件丢掉了, 接着再使用第二个复杂的检验码进行复杂计算,验证文件是否完整,这样确保执行的文件完整和安全。

      SHA(Secure Hash Algorithm, 安全散列算法)是美国国家安全局设计,美国国家标准与技术研究院发布的一系列密码散列函数。SHA-1看起来和MD5算法很像,也许是Ron Rivest在SHA-1的设计中起了一定的作用。SHA-1的内部比MD5更强,其摘要比MD5的16字节长4个字节,这个算法成功经受了密码分析专家 的攻击,也因而受到密码学界的广泛推崇。这个算法在目前网络上的签名,BT软件里就有大量使用,比如在BT里要计算是否同一个种子时,就是利用文件的签名 来判断的。同一份8G的电影从几千BT用户那里下载,也不会出现错误的数据,导致电影不播放。

map_off字段

这个字段主要保存map开始位置,就是从文件头开始到map数据的长度,通过这个索引就可以找到map数据。map的数据结构如下:

名称 大小 说明
size 4字节 map里项的个数
list 变长 每一项定义为12字节,项的个数由上面项大小决定。

map数据排列结构定义如下:

<span class="coMULTI" style="line-height:20px">/*
*Direct-mapped "map_list".
*/</span>
 
<span class="kw4" style="line-height:20px">typedef</span> <span class="kw4" style="line-height:20px">struct</span> DexMapList <span class="br0" style="line-height:20px">{</span>
    u4 size<span class="sy0" style="line-height:20px">;</span> <span class="coMULTI" style="line-height:20px">/* #of entries inlist */</span>
    DexMapItem list<span class="br0" style="line-height:20px">[</span><span class="nu0" style="line-height:20px">1</span><span class="br0" style="line-height:20px">]</span><span class="sy0" style="line-height:20px">;</span> <span class="coMULTI" style="line-height:20px">/* entries */</span>
<span class="br0" style="line-height:20px">}</span>DexMapList<span class="sy0" style="line-height:20px">;</span>

每一个map项的结构定义如下:

<span class="coMULTI" style="line-height:20px">/*
*Direct-mapped "map_item".
*/</span>
 
<span class="kw4" style="line-height:20px">typedef</span> <span class="kw4" style="line-height:20px">struct</span> DexMapItem <span class="br0" style="line-height:20px">{</span>
    u2 type<span class="sy0" style="line-height:20px">;</span> <span class="coMULTI" style="line-height:20px">/* type code (seekDexType* above) */</span>
    u2 unused<span class="sy0" style="line-height:20px">;</span>
    u4 size<span class="sy0" style="line-height:20px">;</span> <span class="coMULTI" style="line-height:20px">/* count of items ofthe indicated type */</span>
    u4 offset<span class="sy0" style="line-height:20px">;</span> <span class="coMULTI" style="line-height:20px">/* file offset tothe start of data */</span>
<span class="br0" style="line-height:20px">}</span>DexMapItem<span class="sy0" style="line-height:20px">;</span>

DexMapItem结构定义每一项的数据意义:类型、类型个数、类型开始位置。

其中的类型定义如下:

<span class="coMULTI" style="line-height:20px">/*map item type codes */</span>
<span class="kw2" style="line-height:20px">enum</span><span class="br0" style="line-height:20px">{</span>
    kDexTypeHeaderItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x0000</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeStringIdItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x0001</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeTypeIdItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x0002</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeProtoIdItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x0003</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeFieldIdItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x0004</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeMethodIdItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x0005</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeClassDefItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x0006</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeMapList <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x1000</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeTypeList <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x1001</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeAnnotationSetRefList <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x1002</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeAnnotationSetItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x1003</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeClassDataItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x2000</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeCodeItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x2001</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeStringDataItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x2002</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeDebugInfoItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x2003</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeAnnotationItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x2004</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeEncodedArrayItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x2005</span><span class="sy0" style="line-height:20px">,</span>
    kDexTypeAnnotationsDirectoryItem <span class="sy0" style="line-height:20px">=</span> <span class="nu12" style="line-height:20px">0x2006</span><span class="sy0" style="line-height:20px">,</span>
<span class="br0" style="line-height:20px">}</span><span class="sy0" style="line-height:20px">;</span>

从上面的类型可知,它包括了在dex文件里可能出现的所有类型。可以看出这里的类型与文件头里定义的类型有很多是一样的,这里的类型其实就是文件头里定义 的类型。其实这个map的数据,就是头里类型的重复,完全是为了检验作用而存在的。当Android系统加载dex文件时,如果比较文件头类型个数与 map里类型不一致时,就会停止使用这个dex文件

string_ids_size/off字段

这两个字段主要用来标识字符串资源。源程序编译后,程序里用到的字符串都保存在这个数据段里,以便解释执行这个dex文件使用。其中包括调用库函数里的类名称描述,用于输出显示的字符串等。

string_ids_size标识了有多少个字符串,string_ids_off标识字符串数据区的开始位置。字符串的存储结构如下:

<span class="coMULTI" style="line-height:20px">/*
 * Direct-mapped "string_id_item".
 */</span>
<span class="kw4" style="line-height:20px">typedef</span> <span class="kw4" style="line-height:20px">struct</span> DexStringId <span class="br0" style="line-height:20px">{</span>
    u4  stringDataOff<span class="sy0" style="line-height:20px">;</span>      <span class="coMULTI" style="line-height:20px">/* file offset to string_data_item */</span>
<span class="br0" style="line-height:20px">}</span> DexStringId<span class="sy0" style="line-height:20px">;</span>

可以看出这个数据区保存的只是字符串表的地址索引。如果要找到字符串的实际数据,还需要通过个地址索引找到文件的相应开始位置,然后才能得到字符串数据。 每一个字符串项的索引占用4个字节,因此这个数据区的大小就为4*string_ids_size。实际数据区中的字符串采用UTF8格式保存。

例如,如果dex文件使用16进制显示出来内容如下:
063c 696e 6974 3e00
其实际数据则是”<init>\0”

另外这段数据中不仅包括字符串的字符串的内容和结束标志,在最开头的位置还标明了字符串的长度。上例中第一个字节06就是表示这个字符串有6个字符。

关于字符串的长度有两点需要注意的地方:

1、关于长度的编码格式

dex文件里采用了变长方式表示字符串长度。一个字符串的长度可能是一个字节(小于256)或者4个字节(1G大小以上)。字符串的长度大多数都是小于 256个字节,因此需要使用一种编码,既可以表示一个字节的长度,也可以表示4个字节的长度,并且1个字节的长度占绝大多数。能满足这种表示的编码方式有 很多,但dex文件里采用的是uleb128方式。leb128编码是一种变长编码,每个字节采用7位来表达原来的数据,最高位用来表示是否有后继字节。

它的编码算法如下:

<span class="coMULTI" style="line-height:20px">/*
 * Writes a 32-bit value in unsigned ULEB128 format.
 * Returns the updated pointer.
 */</span>
DEX_INLINE u1<span class="sy0" style="line-height:20px">*</span> writeUnsignedLeb128<span class="br0" style="line-height:20px">(</span>u1<span class="sy0" style="line-height:20px">*</span> ptr<span class="sy0" style="line-height:20px">,</span> u4 data<span class="br0" style="line-height:20px">)</span>
<span class="br0" style="line-height:20px">{</span>
    <span class="kw1" style="line-height:20px">while</span> <span class="br0" style="line-height:20px">(</span><span class="kw2" style="line-height:20px">true</span><span class="br0" style="line-height:20px">)</span> <span class="br0" style="line-height:20px">{</span>
        u1 out <span class="sy0" style="line-height:20px">=</span> data <span class="sy0" style="line-height:20px">&</span> <span class="nu12" style="line-height:20px">0x7f</span><span class="sy0" style="line-height:20px">;</span>
        <span class="kw1" style="line-height:20px">if</span> <span class="br0" style="line-height:20px">(</span>out <span class="sy0" style="line-height:20px">!=</span> data<span class="br0" style="line-height:20px">)</span> <span class="br0" style="line-height:20px">{</span>
            <span class="sy0" style="line-height:20px">*</span>ptr<span class="sy0" style="line-height:20px">++</span> <span class="sy0" style="line-height:20px">=</span> out <span class="sy0" style="line-height:20px">|</span> <span class="nu12" style="line-height:20px">0x80</span><span class="sy0" style="line-height:20px">;</span>
            data <span class="sy0" style="line-height:20px">>>=</span> <span class="nu0" style="line-height:20px">7</span><span class="sy0" style="line-height:20px">;</span>
        <span class="br0" style="line-height:20px">}</span> <span class="kw1" style="line-height:20px">else</span> <span class="br0" style="line-height:20px">{</span>
            <span class="sy0" style="line-height:20px">*</span>ptr<span class="sy0" style="line-height:20px">++</span> <span class="sy0" style="line-height:20px">=</span> out<span class="sy0" style="line-height:20px">;</span>
            <span class="kw2" style="line-height:20px">break</span><span class="sy0" style="line-height:20px">;</span>
        <span class="br0" style="line-height:20px">}</span>
    <span class="br0" style="line-height:20px">}</span>
    <span class="kw1" style="line-height:20px">return</span> ptr<span class="sy0" style="line-height:20px">;</span>
<span class="br0" style="line-height:20px">}</span>

它的解码算法如下:

<span class="coMULTI" style="line-height:20px">/*
 * Reads an unsigned LEB128 value, updating the given pointer to point
 * just past the end of the read value. This function tolerates
 * non-zero high-order bits in the fifth encoded byte.
 */</span>
DEX_INLINE <span class="kw4" style="line-height:20px">int</span> readUnsignedLeb128<span class="br0" style="line-height:20px">(</span><span class="kw4" style="line-height:20px">const</span> u1<span class="sy0" style="line-height:20px">**</span> pStream<span class="br0" style="line-height:20px">)</span> <span class="br0" style="line-height:20px">{</span>
    <span class="kw4" style="line-height:20px">const</span> u1<span class="sy0" style="line-height:20px">*</span> ptr <span class="sy0" style="line-height:20px">=</span> <span class="sy0" style="line-height:20px">*</span>pStream<span class="sy0" style="line-height:20px">;</span>
    <span class="kw4" style="line-height:20px">int</span> result <span class="sy0" style="line-height:20px">=</span> <span class="sy0" style="line-height:20px">*</span><span class="br0" style="line-height:20px">(</span>ptr<span class="sy0" style="line-height:20px">++</span><span class="br0" style="line-height:20px">)</span><span class="sy0" style="line-height:20px">;</span>
   <span class="kw1" style="line-height:20px">if</span> <span class="br0" style="line-height:20px">(</span>result <span class="sy0" style="line-height:20px">></span> <span class="nu12" style="line-height:20px">0x7f</span><span class="br0" style="line-height:20px">)</span> <span class="br0" style="line-height:20px">{</span>
        <span class="kw4" style="line-height:20px">int</span> cur <span class="sy0" style="line-height:20px">=</span> <span class="sy0" style="line-height:20px">*</span><span class="br0" style="line-height:20px">(</span>ptr<span class="sy0" style="line-height:20px">++</span><span class="br0" style="line-height:20px">)</span><span class="sy0" style="line-height:20px">;</span>
        result <span class="sy0" style="line-height:20px">=</span> <span class="br0" style="line-height:20px">(</span>result <span class="sy0" style="line-height:20px">&</span> <span class="nu12" style="line-height:20px">0x7f</span><span class="br0" style="line-height:20px">)</span> <span class="sy0" style="line-height:20px">|</span> <span class="br0" style="line-height:20px">(</span><span class="br0" style="line-height:20px">(</span>cur <span class="sy0" style="line-height:20px">&</span> <span class="nu12" style="line-height:20px">0x7f</span><span class="br0" style="line-height:20px">)</span> <span class="sy0" style="line-height:20px"><<</span> <span class="nu0" style="line-height:20px">7</span><span class="br0" style="line-height:20px">)</span><span class="sy0" style="line-height:20px">;</span>
        <span class="kw1" style="line-height:20px">if</span> <span class="br0" style="line-height:20px">(</span>cur <span class="sy0" style="line-height:20px">></span> <span class="nu12" style="line-height:20px">0x7f</span><span class="br0" style="line-height:20px">)</span> <span class="br0" style="line-height:20px">{</span>
            cur <span class="sy0" style="line-height:20px">=</span> <span class="sy0" style="line-height:20px">*</span><span class="br0" style="line-height:20px">(</span>ptr<span class="sy0" style="line-height:20px">++</span><span class="br0" style="line-height:20px">)</span><span class="sy0" style="line-height:20px">;</span>
            result <span class="sy0" style="line-height:20px">|=</span> <span class="br0" style="line-height:20px">(</span>cur <span class="sy0" style="line-height:20px">&</span> <span class="nu12" style="line-height:20px">0x7f</span><span class="br0" style="line-height:20px">)</span> <span class="sy0" style="line-height:20px"><<</span> <span class="nu0" style="line-height:20px">14</span><span class="sy0" style="line-height:20px">;</span>
            <span class="kw1" style="line-height:20px">if</span> <span class="br0" style="line-height:20px">(</span>cur <span class="sy0" style="line-height:20px">></span> <span class="nu12" style="line-height:20px">0x7f</span><span class="br0" style="line-height:20px">)</span> <span class="br0" style="line-height:20px">{</span>
                cur <span class="sy0" style="line-height:20px">=</span> <span class="sy0" style="line-height:20px">*</span><span class="br0" style="line-height:20px">(</span>ptr<span class="sy0" style="line-height:20px">++</span><span class="br0" style="line-height:20px">)</span><span class="sy0" style="line-height:20px">;</span>
                result <span class="sy0" style="line-height:20px">|=</span> <span class="br0" style="line-height:20px">(</span>cur <span class="sy0" style="line-height:20px">&</span> <span class="nu12" style="line-height:20px">0x7f</span><span class="br0" style="line-height:20px">)</span> <span class="sy0" style="line-height:20px"><<</span> <span class="nu0" style="line-height:20px">21</span><span class="sy0" style="line-height:20px">;</span>
                <span class="kw1" style="line-height:20px">if</span> <span class="br0" style="line-height:20px">(</span>cur <span class="sy0" style="line-height:20px">></span> <span class="nu12" style="line-height:20px">0x7f</span><span class="br0" style="line-height:20px">)</span> <span class="br0" style="line-height:20px">{</span>
                    <span class="coMULTI" style="line-height:20px">/*
                     * Note: We don't check to see if cur is out of
                     * range here, meaning we tolerate garbage in the
                     * high four-order bits.
                     */</span>
                    cur <span class="sy0" style="line-height:20px">=</span> <span class="sy0" style="line-height:20px">*</span><span class="br0" style="line-height:20px">(</span>ptr<span class="sy0" style="line-height:20px">++</span><span class="br0" style="line-height:20px">)</span><span class="sy0" style="line-height:20px">;</span>
                    result <span class="sy0" style="line-height:20px">|=</span> cur <span class="sy0" style="line-height:20px"><<</span> <span class="nu0" style="line-height:20px">28</span><span class="sy0" style="line-height:20px">;</span>
                <span class="br0" style="line-height:20px">}</span>
            <span class="br0" style="line-height:20px">}</span>
        <span class="br0" style="line-height:20px">}</span>
    <span class="br0" style="line-height:20px">}</span>
    <span class="sy0" style="line-height:20px">*</span>pStream <span class="sy0" style="line-height:20px">=</span> ptr<span class="sy0" style="line-height:20px">;</span>
    <span class="kw1" style="line-height:20px">return</span> result<span class="sy0" style="line-height:20px">;</span>
<span class="br0" style="line-height:20px">}</span>

根据上面的算法分析上面例子字符串,取得第一个字节是06,最高位为0,因此没有后继字节,那么取出这个字节里7位有效数据,就是6,也就是说这个字符串是6个字节,但不包括结束字符“\0”。

2、关于长度的意义

由于字符串内容采用的是UTF-8格式编码,表示一个字符的字节数是不定的。即有时是一个字节表示一个字符,有时是两个、三个甚至四个字节表示一个字符。 而这里的长度代表的并不是整个字符串所占用的字节数,表示这个字符串包含的字符个数。所以在读取时需要注意,尤其是在包含中文字符时,往往会因为读取的长 度不正确导致字符串被截断。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章