1.dex文件中的数据结构
u1/uint8_t=>表示1字节的无符号数
u2/uint16_t=>表示2字节的无符号数
u4/uint32_t=>表示4字节的无符号数
u8/unit64_t=>表示8字节的无符号数
sleb128=>有符号leb128,可变长度为1-5字节
uleb128=>无符号符号leb128,可变长度为1-5字节
uleb128p1=>无符号符号leb128值加1,可变长度为1-5字节
2.dex文件整体结构
struct DexFile {
DexHeader
DexStringId
DexTypeId
DexProtoId//对DexType进一步说明
DexFieldId
DexMethodId
DexClassDef
DexData
DexLink
}
以索引为线索
3.DEX的内存映射
与静态类似,只是变为xxxItem结构
ClassObject结构由六个部分组成:
PDvmDex:// DEX文件字段
super://超类
sfields://对应DexClassData结构中的staticFields静态字段
iFields://对应DexClassData结构中的instanceFields实例字段
directMethods://对应DexClassData结构中的directMethods直接方法字段
virtualMethods:///对应DexClassData结构中的virtualMethods虚方法字段
DexClassDef : class_def_item
DexClassData: class_data_item
DexFiled(staticFields): sfileds
DexFiled(instanceFields): ifileds
DexMethod(directMethods):directMethods
DexMethod(virtualMethods):virtualMethods
DexCode: code_item
/******************************************
DexClassDef结构
*******************************************/
struct DexClassDef {
u4 classIdx; /* 类的类型,指向DexTypeId列表的索引 */
u4 accessFlags; /* accessFlags是类的访问标志,以ACC_开头的一个枚举值 */
u4 superclassIdx; /* 父类类型,指向DexTypeId列表的索引 */
u4 interfacesOff; /* 接口,指向DexTypeList的偏移 */
u4 sourceFileIdx; /* 源文件名,指向DexStringId列表的索引 */
u4 annotationsOff; /* 注解,指向DexAnnotationsDirectoryItem结构 annotationsOff字段指向注解目录结构,根据类型不同会有注解类、注解方法、注解字段与注解参数*/
u4 classDataOff; /* 指向DexClassData结构的偏移 classDataOff字段是类的数据部分*/
u4 staticValuesOff; /* 指向DexEncodedArray结构的偏移 staticValuesOff字段记录类中的静态数据*/
};
/* classDataOff */
struct DexClassData {
DexClassDataHeader header; //指定字段与方法的个数
DexField* staticFields; //静态字段,DexField结构
DexField* instanceFields;//实例字段,DexField结构
DexMethod* directMethods; //直接方法,DexMethod结构
DexMethod* virtualMethods;//虚方法,DexMethod结构
};
/* DexClassDataHeader结构记录了当前类中字段与方法的数目 */
struct DexClassDataHeader {
u4 staticFieldsSize; //静态字段个数
u4 instanceFieldsSize; //实例字段个数
u4 directMethodsSize; //直接方法个数
u4 virtualMethodsSize; //虚方法个数
};
/* DexField结构描述了字段的类型与访问标志 */
struct DexField {
u4 fieldIdx; /* 指向DexFieldId的索引 */
u4 accessFlags; /* 访问标志 */
};
/* DexMethod结构描述方法的原型、名称、访问标志以及代码数据块 */
struct DexMethod {
u4 methodIdx; /* 指向DexMethodId的索引 */
u4 accessFlags; /* 访问标志 */
u4 codeOff; /* 指向DexCode结构的偏移 */
};
struct DexCode {
u2 registersSize; /* 使用的寄存器个数 .register*/
u2 insSize; /* 参数个数 .paramter*/
u2 outsSize; /* 调用其他方法时使用的寄存器个数 outsSize指定方法调用外部方法时使用的寄存器个数*/
u2 triesSize; /* try/catch个数 */
u4 debugInfoOff; /* 指向调式信息的偏移 */
u4 insnsSize; /* 指令集个数,以2字节为单位 */
u2 insns[1]; /* 指令集 */
/* 2字节空间用于结构对齐 */
/* try_item[triesSize] DexTry结构 */
/* try/catch中handler的个数 */
/* catch_handler_item[handlersSize] DexCatchHandler结构 */
};
DexFile.png