Android Learning Tips (+)---DEX File Structure Example parsing (2)

Source: Internet
Author: User

I am on the blog to publish some of my Android learning experience, I hope that everyone can help.
This article describes the structure and composition of the Dex file through an example.

Reference Leb128 data type Android learning experience (5)---Dex data type LEB128
Reference example analysis learn to understand Dex file Structure Android learning experience (1)---Dex File Structure analysis
Refer to Baksmali Tools using Android Learning Tips (4)---Mac Smali file authoring and running

1. Compiling

We use an example to analyze the composition of the Dex file.

创建一个Hello.java文件,输入下面的值,这个是我们学习的样本
publicclass Hello {    publicstaticvoidmain(String[] argc) {        System.out.println("Hello world");    }}
通过编译成.class文件之后再编译成.dex文件


2. Header

We parse through the previous one in Dex header
Query and fill in the table with the header data from the previous 0x70 in the Dex file

前0x70数据



3, Maplist

In the header, Mapoff points to the maplist structure.
Maplist structure in section fourth has been defined, we query and fill out the form

map_off中偏移地址为0x0240,查询相应地址第一个16位是MapItem的数目:0x000D(14个)


4, String_id_item

First we look at Maplist in String_id_item,size is 14, starting from 0x70

我们分别从0x176、0x17E等读取出14个字符串,并填入表格

5, Type_id_item

We look at the maplist in Type_id_item, the number is 7, starting from 0xa8

由于这个是表示stringids的索引,查string_id_item表得出

6, Proto_id_item

View Proto_id_item, number is 3, starting from 0xc4

通过查看proto_id_item结构,填入表格

7, Field_id_item

We look at the maplist in Field_id_item, the number is 1, starting from 0xe8

通过查看field_id_item结构,填入表格

8, Method_id_item

We look at the maplist in Method_id_item, the number is 4, starting from 0xf0

通过查看上一节中对method_id_item描述,填入表格

9, Class_def_item

We look at the maplist in Class_def_item, the number is 1, starting from 0x110

通过上一节中DexClassDef结构,我们填写表格

我们查看MapList中class_def_item,的确偏移地址和个数都相同。

由于DexClass.h定义的u4是属于Leb128

下面紧跟的是两个直接方法

The first one is B0 02

从0x130开始分析code_item结构



The analytical thinking is organized as follows

(1) 《Dalvik VM Instruction Format》 里操作符 op 都是位于首个 16bit 数据的低 8 bit 起始的是 op = 0x70(2) 在 《Bytecode for Dalvik VM》 里找到对应的 Syntax 和 format 。syntax = invoke-directformat = 0x35c (3) 在《Dalvik VM Instruction Format》里查找 35c , 得知 op = 0x70 的指令占据 2 个 16 bit 数据 ,格式是 B|A|op CCCC G|F|E|D ,其中,B=1,A=0。我们查询到: [B=1] op {vD}, [email protected]有上述得出CCCC = 3,D = 0;0E操作码是对应的是return-void

The command is given:

invoke-direct {v0}, Ljava/lang/Object;.<init>:()V return-void
The second one is C8 02

从0x148开始分析code_item结构



The analytical thinking is organized as follows

(1) 《Dalvik VM Instruction Format》 里操作符 op 都是位于首个 16bit 数据的低 8 bit 起始的是 op = 0x62(2) 在 《Bytecode for Dalvik VM》 里找到对应的 Syntax 和 format 。syntax = sget-objectformat = 0x21c (3) 在《Dalvik VM Instruction Format》里查找 21c , 得知 op = 0x62 的指令占据 2 个 16 bit 数据 ,格式是 AA|op BBBB ,其中,AA=00,BBBB=0。

The command is given:

sget-object v0, Ljava/lang/System;.out:Ljava/io/PrintStream;

Continue with

(1) 《Dalvik VM Instruction Format》 里操作符 op 都是位于首个 16bit 数据的低 8 bit 起始的是 op = 0x1A(2) 在 《Bytecode for Dalvik VM》 里找到对应的 Syntax 和 format 。syntax = const-string vAA, [email protected]format = 0x21c (3) 在《Dalvik VM Instruction Format》里查找 21c , 得知 op = 0x1A 的指令占据 2 个 16 bit 数据 ,格式是 onst-string vAA, [email protected] ,其中,AA=01,BBBB=1。

The command is given:

const-string v1, "Hello World"

Continue with

(1) 《Dalvik VM Instruction Format》 里操作符 op 都是位于首个 16bit 数据的低 8 bit 起始的是 op = 0x6E(2) 在 《Bytecode for Dalvik VM》 里找到对应的 Syntax 和 format 。syntax = invoke-virtualformat = 0x35c (3) 在《Dalvik VM Instruction Format》里查找 21c , 得知 op = 0x6E 的指令占据 2 个 16 bit 数据 ,格式是 B|A|op CCCC G|F|E|D,其中,B=2, CCCC=2, E=1, D=0。其格式为:[B=2] op {vD, vE}, [email protected]

The command is given:

invoke-virtual {v0, v1}, Ljava/io/PrintStream; -> println(Ljava/lang/String;)Vreturn-void
Final analysis of core code
invoke-direct {v0}, Ljava/lang/Object;.<init>:()V sget-object v0, Ljava/lang/System;.out:Ljava/io/PrintStream;const-string"Hello World"invoke-virtual-> println(Ljava/lang/String;)Vreturn-void

Compare Baksmali files to generate Smali file

. class PublicLhello;. Super Ljava/lang/object;. Source"Hello.java"# Direct Methods. method PublicConstructor <init> () v. Registers 1. Prologue. Line 1 Invoke-direct {p0}, Ljava/lang/object;-><init > () Vreturn-void. End Method.method Public Static Main([ljava/lang/string;) v. Registers3. Prologue. Line3sget-ObjectV0, ljava/lang/system;-> out: Ljava/io/printstream;Const-stringV1,"Hello World"invoke-Virtual{v0, v1}, Ljava/io/printstream;->println (ljava/lang/string;) v. Line4    return-void. End method

The core code is the same and the analysis is correct.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Learning Tips (+)---DEX File Structure Example parsing (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.