Patching AVR ao wrong output on rjmp instruction (#415)

* Patching AVR ao wrong output on rjmp instruction
* Fixed test output
* Add AVR RJMP test

Co-authored-by: Anton Kochkov <anton.kochkov@gmail.com>
This commit is contained in:
Giovanni 2021-01-21 10:36:56 +01:00 committed by pancake
parent c09e60843e
commit e55d7c033b
2 changed files with 105 additions and 90 deletions

View File

@ -1127,10 +1127,10 @@ INST_HANDLER (rcall) { // RCALL k
return;
}
// target address
op->jump = (op->addr
+ (((((buf[1] & 0xf) << 8) | buf[0]) << 1)
op->jump = op->addr + (
(((((buf[1] & 0xf) << 8) | buf[0]) << 1)
| (((buf[1] & 0x8) ? ~((int) 0x1fff) : 0)))
+ 2) & CPU_PC_MASK (cpu);
+ 2);
op->fail = op->addr + op->size;
// esil
ESIL_A ("pc,"); // esil already points to next
@ -1174,16 +1174,10 @@ INST_HANDLER (reti) { // RETI
}
INST_HANDLER (rjmp) { // RJMP k
op->jump = (op->addr
#ifdef _MSC_VER
#pragma message ("anal_avr.c: WARNING: Probably broken on windows")
+ ((((( buf[1] & 0xf) << 9) | (buf[0] << 1)))
st32 jump = ((((( buf[1] & 0xf) << 9) | (buf[0] << 1)))
| (buf[1] & 0x8 ? ~(0x1fff) : 0))
#else
+ ((((( (typeof (op->jump)) buf[1] & 0xf) << 9) | ((typeof (op->jump)) buf[0] << 1)))
| (buf[1] & 0x8 ? ~((typeof (op->jump)) 0x1fff) : 0))
#endif
+ 2) & CPU_PC_MASK (cpu);
+ 2;
op->jump = op->addr + jump;
ESIL_A ("%"PFMT64d",pc,=,", op->jump);
}
@ -1697,11 +1691,11 @@ static int avr_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf, int len,
!strcmp (mnemonic, "truncated")) {
op->eob = true;
op->mnemonic = strdup(mnemonic);
size = -2;
return -1;
}
if (!op || size < 0) {
return size;
if (!op) {
return -1;
}
// select cpu info

View File

@ -126,6 +126,21 @@ EXPECT=<<EOF
EOF
RUN
NAME=avr rjmp - simple
FILE=malloc://16384
ARGS=-s 0x3434 -a avr
CMDS=<<EOF
e asm.cpu=ATmega8
wx ffcf
pi 1
ao 1~^jump[1]
EOF
EXPECT=<<EOF
rjmp 0x3434
0x00003434
EOF
RUN
NAME=avr rjmp - relative jump backward at 0x1000
FILE=malloc://8192
ARGS=-s 0x1000 -a avr
@ -157,10 +172,10 @@ aoe 4
pi 4
EOF
EXPECT=<<EOF
0x0 61442,pc,=
0x2 61446,pc,=
0x4 61698,pc,=
0x6 61702,pc,=
0x0 -4094,pc,=
0x2 -4090,pc,=
0x4 -3838,pc,=
0x6 -3834,pc,=
rjmp 0xfffff002
rjmp 0xfffff006
rjmp 0xfffff102
@ -679,17 +694,17 @@ EXPECT=<<EOF
0x00000330 1 syscall.255
0x00000342 5256 fcn.00000342
0x00000360 152 fcn.00000360
0x0000036c 1 syscall.0.26
0x0000036c 1 syscall.0.36
0x00000390 4 aav.0x00000390
0x000003ea 1 syscall.0.27
0x000003ea 1 syscall.0.37
0x0000048c 24 fcn.0000048c
0x0000048e 1 syscall.0.19
0x0000048e 1 syscall.0.29
0x000004a4 72 fcn.000004a4
0x000004a6 1 syscall.0.6
0x000004ec 76 fcn.000004ec
0x00000504 4 aav.0x00000504
0x0000054e 84 fcn.0000054e
0x0000054e 1 syscall.0.25
0x0000054e 1 syscall.0.35
0x000005a2 78 fcn.000005a2
0x000005f0 114 fcn.000005f0
0x0000062a 1 syscall.63
@ -708,53 +723,35 @@ EXPECT=<<EOF
0x00000876 40 fcn.00000876
0x000008e6 36 fcn.000008e6
0x00000952 178 fcn.00000952
0x00000972 1 syscall.0.7
0x000009e2 1 syscall.0.8
0x00000972 1 syscall.0.10
0x000009e2 1 syscall.0.11
0x00000a04 148 fcn.00000a04
0x00000afc 26 fcn.00000afc
0x00000bae 240 fcn.00000bae
0x00000bf0 1 syscall.0.17
0x00000bf2 1 syscall.0.18
0x00000c90 1 syscall.0.40
0x00000c92 1 syscall.0.41
0x00000c94 1 syscall.0.42
0x00000c96 1 syscall.0.43
0x00000d2e 1 syscall.0.39
0x00000bae 470 fcn.00000bae
0x00000bf0 1 syscall.0.22
0x00000bf2 1 syscall.0.23
0x00000c90 1 syscall.0.24
0x00000c92 1 syscall.0.25
0x00000c94 1 syscall.0.26
0x00000c96 1 syscall.0.27
0x00000d2e 1 syscall.0.28
0x00000d78 4 aav.0x00000d78
0x00000dac 92 fcn.00000dac
0x00000e08 92 fcn.00000e08
0x00000e64 56 fcn.00000e64
0x00000e9c 12 fcn.00000e9c
0x00000ea8 198 fcn.00000ea8
0x00000ef4 1 syscall.0.22
0x00000f0c 1 syscall.0.21
0x00000f2e 1 syscall.0.20
0x00000ef4 1 syscall.0.32
0x00000f0c 1 syscall.0.31
0x00000f2e 1 syscall.0.30
0x00000fd0 206 fcn.00000fd0
0x00000ff0 1 syscall.0.24
0x00001018 1 syscall.0.23
0x0000110c 1 syscall.208
0x0000113a 136 fcn.0000113a
0x00001174 1 syscall.0.44
0x00001176 1 syscall.0.45
0x00001178 1 syscall.0.46
0x0000117a 1 syscall.0.47
0x00001202 12 fcn.00001202
0x00000ff0 1 syscall.0.34
0x00001018 1 syscall.0.33
0x0000120e 106 fcn.0000120e
0x00001238 1 syscall.0.1
0x00001294 24 fcn.00001294
0x00001296 10 fcn.00001296
0x000012c2 58 fcn.000012c2
0x000012d0 1 syscall.0.48
0x000012fc 26 fcn.000012fc
0x00001316 26 fcn.00001316
0x00001394 14 fcn.00001394
0x000013ee 14 fcn.000013ee
0x000013fc 48 fcn.000013fc
0x0000142c 12 fcn.0000142c
0x00001316 32 fcn.00001316
0x00001438 2 fcn.00001438
0x0000143c 54 int.0000143c
0x00001490 2 int.00001490
0x0000149e 16 fcn.0000149e
0x0000152c 34 fcn.0000152c
0x000017b6 1 syscall.1
0x00001b0c 36 fcn.00001b0c
@ -769,30 +766,29 @@ EXPECT=<<EOF
0x00001f24 14 fcn.00001f24
0x00001f32 76 fcn.00001f32
0x00001f7e 100 fcn.00001f7e
0x00001f92 1 syscall.0.33
0x00001fcc 1 syscall.0.34
0x00001f92 1 syscall.0.43
0x00001fcc 1 syscall.0.44
0x00001fe2 14 fcn.00001fe2
0x00001ff0 80 fcn.00001ff0
0x0000202a 1 syscall.0.32
0x0000202a 1 syscall.0.42
0x00002040 102 fcn.00002040
0x00002054 1 syscall.0.35
0x00002090 1 syscall.0.36
0x00002054 1 syscall.0.45
0x00002090 1 syscall.0.46
0x000020a6 102 fcn.000020a6
0x000020ba 1 syscall.0.37
0x000020f6 1 syscall.0.38
0x000020ba 1 syscall.0.47
0x000020f6 1 syscall.0.48
0x0000220e 96 fcn.0000220e
0x0000246e 34 fcn.0000246e
0x000025dc 156 fcn.000025dc
0x0000268c 138 fcn.0000268c
0x00002818 34 fcn.00002818
0x00002a54 328 fcn.00002a54
0x00002c26 304 fcn.00002c26
0x00002818 78 fcn.00002818
0x00002a54 770 fcn.00002a54
0x00002d56 286 fcn.00002d56
0x00002ec2 1 syscall.0
0x00002ec8 16 fcn.00002ec8
0x00002ec8 1682 fcn.00002ec8
0x00002ed8 16 fcn.00002ed8
0x00002ee8 16 fcn.00002ee8
0x00002ef8 6 fcn.00002ef8
0x00002ef8 18 fcn.00002ef8
0x00002f0a 36 fcn.00002f0a
0x00002f2e 22 fcn.00002f2e
0x00002f44 18 fcn.00002f44
@ -802,37 +798,62 @@ EXPECT=<<EOF
0x00002f9e 22 fcn.00002f9e
0x00002fb4 52 fcn.00002fb4
0x00002fe8 44 fcn.00002fe8
0x0000300a 1 syscall.0.28
0x0000300a 1 syscall.0.38
0x00003014 184 fcn.00003014
0x00003036 1 syscall.0.30
0x00003038 1 syscall.0.31
0x0000303a 1 syscall.255.4
0x00003036 1 syscall.0.40
0x00003038 1 syscall.0.41
0x0000303a 1 syscall.255.5
0x00003048 42 fcn.00003048
0x00003072 24 fcn.00003072
0x00003098 1 syscall.0.29
0x000030cc 1182 fcn.000030cc
0x00003098 1 syscall.0.39
0x000030cc 1198 fcn.000030cc
0x00003116 2 fcn.00003116
0x00003118 8 fcn.00003118
0x00003134 1 syscall.0.51
0x00003136 1 syscall.0.49
0x0000313a 692 fcn.0000313a
0x00003154 1 syscall.0.52
0x000031e0 8 fcn.000031e0
0x000031e8 4 fcn.000031e8
0x000031e8 580 fcn.000031e8
0x00003202 146 fcn.00003202
0x00003290 1 syscall.0.53
0x00003294 2 fcn.00003294
0x00003296 34 fcn.00003296
0x000032b4 1 syscall.0.54
0x000032b8 10 fcn.000032b8
0x000032ba 1 syscall.0.12
0x000032c2 88 fcn.000032c2
0x0000330a 1 syscall.0.9
0x0000330c 1 syscall.0.10
0x0000330e 1 syscall.255.2
0x0000331a 4 fcn.0000331a
0x0000331a 1 syscall.0.11
0x0000331e 118 fcn.0000331e
0x0000331e 1 syscall.0.13
0x00003322 1 syscall.0.15
0x00003324 1 syscall.0.16
0x00003326 1 syscall.255.3
0x00003390 1 syscall.0.14
0x000032ba 1 syscall.0.18
0x000032c2 444 fcn.000032c2
0x0000330a 1 syscall.0.14
0x0000330c 1 syscall.0.15
0x0000330e 1 syscall.255.3
0x0000331a 122 fcn.0000331a
0x0000331a 1 syscall.0.16
0x0000331e 18 fcn.0000331e
0x0000331e 1 syscall.0.19
0x00003322 1 syscall.0.20
0x00003324 1 syscall.0.21
0x00003326 1 syscall.255.4
0x00003390 1 syscall.0.17
0x00003394 72 fcn.00003394
0x000033dc 1 syscall.0.50
0x000033ee 14 fcn.000033ee
0x000033fc 14 fcn.000033fc
0x0000342c 48 fcn.0000342c
0x0000343c 52 fcn.0000343c
0x0000343e 1 syscall.0.55
0x0000346c 1 syscall.0.56
0x00003470 1 syscall.0.12
0x0000347a 1 syscall.0.13
0x0000347e 8 fcn.0000347e
0x00003486 4 fcn.00003486
0x0000349e 174 fcn.0000349e
0x00003548 1 syscall.0.57
0x0000354c 8 fcn.0000354c
0x0000357a 12 int.0000357a
0x0000357a 68 fcn.0000357a
0x000035ac 1 syscall.0.7
0x000035ae 1 syscall.0.8
0x000035b0 1 syscall.0.9
0x000035b2 1 syscall.255.2
0x000035be 22 fcn.000035be
0x000035d4 8 fcn.000035d4
0x000035dc 30 fcn.000035dc