@@ -14,35 +14,18 @@ pub struct RawPatch;
14
14
15
15
impl Patch for RawPatch {
16
16
type Error = RawPatchError ;
17
-
18
- #[ cfg( not( target_arch = "arm" ) ) ]
19
17
fn patch ( target : GuestAddr , destination : GuestAddr ) -> Result < ( ) , Self :: Error > {
20
18
debug ! ( "patch - addr: {:#x}, target: {:#x}" , target, destination) ;
21
19
if target == destination {
22
20
Err ( RawPatchError :: IdentityPatch ( target) ) ?;
23
21
}
24
- let patch = Self :: get_patch ( destination) ?;
25
- trace ! ( "patch: {:02x?}" , patch) ;
26
- let dest = unsafe { from_raw_parts_mut ( target as * mut u8 , patch. len ( ) ) } ;
27
- dest. copy_from_slice ( & patch) ;
28
- Ok ( ( ) )
29
- }
22
+ let patch = Self :: get_patch ( target, destination) ?;
30
23
31
- #[ cfg( target_arch = "arm" ) ]
32
- fn patch ( target : GuestAddr , destination : GuestAddr ) -> Result < ( ) , Self :: Error > {
33
- debug ! ( "patch - addr: {:#x}, target: {:#x}" , target, destination) ;
34
- if target == destination {
35
- Err ( RawPatchError :: IdentityPatch ( target) ) ?;
36
- }
37
-
38
- let patch = if target & 1 == 1 {
39
- Self :: get_patch_thumb ( destination) ?
40
- } else {
41
- Self :: get_patch_arm ( destination) ?
42
- } ;
24
+ // Mask the thumb mode indicator bit
25
+ #[ cfg( target_arch = "arm" ) ]
26
+ let target = target & !1 ;
43
27
44
28
trace ! ( "patch: {:02x?}" , patch) ;
45
- let target = target & !1 ;
46
29
let dest = unsafe { from_raw_parts_mut ( target as * mut u8 , patch. len ( ) ) } ;
47
30
dest. copy_from_slice ( & patch) ;
48
31
Ok ( ( ) )
@@ -51,7 +34,7 @@ impl Patch for RawPatch {
51
34
52
35
impl RawPatch {
53
36
#[ cfg( target_arch = "x86_64" ) ]
54
- fn get_patch ( destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
37
+ fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
55
38
// mov rax, 0xdeadfacef00dd00d
56
39
// jmp rax
57
40
let insns = [
@@ -77,7 +60,7 @@ impl RawPatch {
77
60
}
78
61
79
62
#[ cfg( target_arch = "x86" ) ]
80
- fn get_patch ( destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
63
+ fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
81
64
// mov eax, 0xdeadface
82
65
// jmp eax
83
66
let insns = [
@@ -91,37 +74,37 @@ impl RawPatch {
91
74
}
92
75
93
76
#[ cfg( target_arch = "arm" ) ]
94
- fn get_patch_arm ( destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
95
- // ldr ip, [pc]
96
- // bx ip
97
- // .long 0xdeadface
98
- let insns = [
99
- [ 0x00 , 0xc0 , 0x9f , 0xe5 ] . to_vec ( ) ,
100
- [ 0x1c , 0xff , 0x2f , 0xe1 ] . to_vec ( ) ,
101
- [ 0xce , 0xfa , 0xad , 0xde ] . to_vec ( ) ,
102
- ] ;
103
- let addr = destination . to_ne_bytes ( ) . to_vec ( ) ;
104
- let insns_mod = [ & insns [ 0 ] , & insns [ 1 ] , & addr ] ;
105
- Ok ( insns_mod . into_iter ( ) . flatten ( ) . cloned ( ) . collect ( ) )
106
- }
107
-
108
- # [ cfg ( target_arch = "arm" ) ]
109
- fn get_patch_thumb ( destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
110
- // ldr ip, [pc, #2]
111
- // bx ip
112
- // .long 0xdeadface
113
- let insns = [
114
- [ 0xdf , 0xf8 , 0x02 , 0xc0 ] . to_vec ( ) ,
115
- [ 0x60 , 0x47 ] . to_vec ( ) ,
116
- [ 0xce , 0xfa , 0xad , 0xde ] . to_vec ( ) ,
117
- ] ;
118
- let addr = destination . to_ne_bytes ( ) . to_vec ( ) ;
119
- let insns_mod = [ & insns [ 0 ] , & insns [ 1 ] , & addr ] ;
120
- Ok ( insns_mod . into_iter ( ) . flatten ( ) . cloned ( ) . collect ( ) )
77
+ fn get_patch ( target : GuestAddr , destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
78
+ // If our target is in thumb mode
79
+ if target & 1 == 1 {
80
+ // ldr ip, [pc, #2]
81
+ // bx ip
82
+ // .long 0xdeadface
83
+ let insns = [
84
+ [ 0xdf , 0xf8 , 0x02 , 0xc0 ] . to_vec ( ) ,
85
+ [ 0x60 , 0x47 ] . to_vec ( ) ,
86
+ [ 0xce , 0xfa , 0xad , 0xde ] . to_vec ( ) ,
87
+ ] ;
88
+ let addr = destination . to_ne_bytes ( ) . to_vec ( ) ;
89
+ let insns_mod = [ & insns [ 0 ] , & insns [ 1 ] , & addr ] ;
90
+ Ok ( insns_mod . into_iter ( ) . flatten ( ) . cloned ( ) . collect ( ) )
91
+ } else {
92
+ // ldr ip, [pc]
93
+ // bx ip
94
+ // .long 0xdeadface
95
+ let insns = [
96
+ [ 0x00 , 0xc0 , 0x9f , 0xe5 ] . to_vec ( ) ,
97
+ [ 0x1c , 0xff , 0x2f , 0xe1 ] . to_vec ( ) ,
98
+ [ 0xce , 0xfa , 0xad , 0xde ] . to_vec ( ) ,
99
+ ] ;
100
+ let addr = destination . to_ne_bytes ( ) . to_vec ( ) ;
101
+ let insns_mod = [ & insns [ 0 ] , & insns [ 1 ] , & addr ] ;
102
+ Ok ( insns_mod . into_iter ( ) . flatten ( ) . cloned ( ) . collect ( ) )
103
+ }
121
104
}
122
105
123
106
#[ cfg( target_arch = "aarch64" ) ]
124
- fn get_patch ( destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
107
+ fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
125
108
// ldr x16, #8
126
109
// br x16
127
110
// .quad 0xdeadfacef00dd00d
@@ -137,7 +120,7 @@ impl RawPatch {
137
120
}
138
121
139
122
#[ cfg( target_arch = "powerpc" ) ]
140
- fn get_patch ( destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
123
+ fn get_patch ( _target : GuestAddr , destination : GuestAddr ) -> Result < Vec < u8 > , RawPatchError > {
141
124
// lis 12, 0xdead
142
125
// ori 12, 12, 0xface
143
126
// mtctr 12
0 commit comments