Dummy
Dummy

Reputation: 21

How to use org.jf.dexlib2 get instructions’ byte code in a dexfile

I want to get the instruction's byte code,but this code can only get the opcode's byte code.Such as 0x38 01 FB FF means if-eqz v1, -0x5.I can only get 0x38 means if-eqz ,but I don't know how to get 0x01 FB FF which means v1, -0x5

for (ClassDef classDef: dexfile.getClasses()){
    for (Method method : classDef.getMethods()){
        if (method.getImplementation()==null)
                continue;
        for (Instruction i :method.getImplementation().getInstructions()){
            i.getOpcode().values();
        }
    }
}

Upvotes: 0

Views: 841

Answers (1)

JesusFreke
JesusFreke

Reputation: 20262

You can use baksmali's -D option to print an annotated hex dump of a dex file. This will produce a 2-column hex dump, with the left column containing the raw byte values, and the right column containing annotations about what those bytes are, as per the dex specification.

For example:

> baksmali -N -D penroser.dump penroser.apk
> less penroser.dump

... (lots of other stuff :))

                           |[26] code_item: Lafzkl/development/mColorPicker/views/ColorPickerView;->pointToHue(F)F
0075f4: 0600               |  registers_size = 6
0075f6: 0200               |  ins_size = 2
0075f8: 0100               |  outs_size = 1
0075fa: 0000               |  tries_size = 0
0075fc: 8690 0100          |  debug_info_off = 0x19086
007600: 2100 0000          |  insns_size = 0x21
                           |  instructions:
007604: 1503 b443          |    const/high16 v3, 1135869952 # 360.000000
007608: 5441 1e00          |    iget-object v1, v4, Lafzkl/development/mColorPicker/views/ColorPickerView;->mHueRect:Landroid/graphics/RectF;
00760c: 6e10 a700 0100     |    invoke-virtual {v1}, Landroid/graphics/RectF;->height()F
007612: 0a00               |    move-result v0
007614: 5212 3b00          |    iget v2, v1, Landroid/graphics/RectF;->top:F
007618: 2e02 0502          |    cmpg-float v2, v5, v2
00761c: 3b02 0900          |    if-gez v2, +0x9
007620: 1205               |    const/4 v5, 0
007622: a802 0503          |    mul-float v2, v5, v3
007626: c902               |    div-float/2addr v2, v0
007628: a702 0302          |    sub-float v2, v3, v2
00762c: 0f02               |    return v2
00762e: 5212 3800          |    iget v2, v1, Landroid/graphics/RectF;->bottom:F
007632: 2d02 0502          |    cmpl-float v2, v5, v2
007636: 3d02 0400          |    if-lez v2, +0x4
00763a: 0105               |    move v5, v0
00763c: 28f3               |    goto -0xd
00763e: 5212 3b00          |    iget v2, v1, Landroid/graphics/RectF;->top:F
007642: c725               |    sub-float/2addr v5, v2
007644: 28ef               |    goto -0x11
007646: 0000               |

Upvotes: 1

Related Questions