rlm@1
|
1 /* Bra.c -- Converters for RISC code
|
rlm@1
|
2 2008-10-04 : Igor Pavlov : Public domain */
|
rlm@1
|
3
|
rlm@1
|
4 #include "Bra.h"
|
rlm@1
|
5
|
rlm@1
|
6 SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
|
rlm@1
|
7 {
|
rlm@1
|
8 SizeT i;
|
rlm@1
|
9 if (size < 4)
|
rlm@1
|
10 return 0;
|
rlm@1
|
11 size -= 4;
|
rlm@1
|
12 ip += 8;
|
rlm@1
|
13 for (i = 0; i <= size; i += 4)
|
rlm@1
|
14 {
|
rlm@1
|
15 if (data[i + 3] == 0xEB)
|
rlm@1
|
16 {
|
rlm@1
|
17 UInt32 dest;
|
rlm@1
|
18 UInt32 src = ((UInt32)data[i + 2] << 16) | ((UInt32)data[i + 1] << 8) | (data[i + 0]);
|
rlm@1
|
19 src <<= 2;
|
rlm@1
|
20 if (encoding)
|
rlm@1
|
21 dest = ip + (UInt32)i + src;
|
rlm@1
|
22 else
|
rlm@1
|
23 dest = src - (ip + (UInt32)i);
|
rlm@1
|
24 dest >>= 2;
|
rlm@1
|
25 data[i + 2] = (Byte)(dest >> 16);
|
rlm@1
|
26 data[i + 1] = (Byte)(dest >> 8);
|
rlm@1
|
27 data[i + 0] = (Byte)dest;
|
rlm@1
|
28 }
|
rlm@1
|
29 }
|
rlm@1
|
30 return i;
|
rlm@1
|
31 }
|
rlm@1
|
32
|
rlm@1
|
33 SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
|
rlm@1
|
34 {
|
rlm@1
|
35 SizeT i;
|
rlm@1
|
36 if (size < 4)
|
rlm@1
|
37 return 0;
|
rlm@1
|
38 size -= 4;
|
rlm@1
|
39 ip += 4;
|
rlm@1
|
40 for (i = 0; i <= size; i += 2)
|
rlm@1
|
41 {
|
rlm@1
|
42 if ((data[i + 1] & 0xF8) == 0xF0 &&
|
rlm@1
|
43 (data[i + 3] & 0xF8) == 0xF8)
|
rlm@1
|
44 {
|
rlm@1
|
45 UInt32 dest;
|
rlm@1
|
46 UInt32 src =
|
rlm@1
|
47 (((UInt32)data[i + 1] & 0x7) << 19) |
|
rlm@1
|
48 ((UInt32)data[i + 0] << 11) |
|
rlm@1
|
49 (((UInt32)data[i + 3] & 0x7) << 8) |
|
rlm@1
|
50 (data[i + 2]);
|
rlm@1
|
51
|
rlm@1
|
52 src <<= 1;
|
rlm@1
|
53 if (encoding)
|
rlm@1
|
54 dest = ip + (UInt32)i + src;
|
rlm@1
|
55 else
|
rlm@1
|
56 dest = src - (ip + (UInt32)i);
|
rlm@1
|
57 dest >>= 1;
|
rlm@1
|
58
|
rlm@1
|
59 data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7));
|
rlm@1
|
60 data[i + 0] = (Byte)(dest >> 11);
|
rlm@1
|
61 data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7));
|
rlm@1
|
62 data[i + 2] = (Byte)dest;
|
rlm@1
|
63 i += 2;
|
rlm@1
|
64 }
|
rlm@1
|
65 }
|
rlm@1
|
66 return i;
|
rlm@1
|
67 }
|
rlm@1
|
68
|
rlm@1
|
69 SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
|
rlm@1
|
70 {
|
rlm@1
|
71 SizeT i;
|
rlm@1
|
72 if (size < 4)
|
rlm@1
|
73 return 0;
|
rlm@1
|
74 size -= 4;
|
rlm@1
|
75 for (i = 0; i <= size; i += 4)
|
rlm@1
|
76 {
|
rlm@1
|
77 if ((data[i] >> 2) == 0x12 && (data[i + 3] & 3) == 1)
|
rlm@1
|
78 {
|
rlm@1
|
79 UInt32 src = ((UInt32)(data[i + 0] & 3) << 24) |
|
rlm@1
|
80 ((UInt32)data[i + 1] << 16) |
|
rlm@1
|
81 ((UInt32)data[i + 2] << 8) |
|
rlm@1
|
82 ((UInt32)data[i + 3] & (~3));
|
rlm@1
|
83
|
rlm@1
|
84 UInt32 dest;
|
rlm@1
|
85 if (encoding)
|
rlm@1
|
86 dest = ip + (UInt32)i + src;
|
rlm@1
|
87 else
|
rlm@1
|
88 dest = src - (ip + (UInt32)i);
|
rlm@1
|
89 data[i + 0] = (Byte)(0x48 | ((dest >> 24) & 0x3));
|
rlm@1
|
90 data[i + 1] = (Byte)(dest >> 16);
|
rlm@1
|
91 data[i + 2] = (Byte)(dest >> 8);
|
rlm@1
|
92 data[i + 3] &= 0x3;
|
rlm@1
|
93 data[i + 3] |= dest;
|
rlm@1
|
94 }
|
rlm@1
|
95 }
|
rlm@1
|
96 return i;
|
rlm@1
|
97 }
|
rlm@1
|
98
|
rlm@1
|
99 SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
|
rlm@1
|
100 {
|
rlm@1
|
101 UInt32 i;
|
rlm@1
|
102 if (size < 4)
|
rlm@1
|
103 return 0;
|
rlm@1
|
104 size -= 4;
|
rlm@1
|
105 for (i = 0; i <= size; i += 4)
|
rlm@1
|
106 {
|
rlm@1
|
107 if (data[i] == 0x40 && (data[i + 1] & 0xC0) == 0x00 ||
|
rlm@1
|
108 data[i] == 0x7F && (data[i + 1] & 0xC0) == 0xC0)
|
rlm@1
|
109 {
|
rlm@1
|
110 UInt32 src =
|
rlm@1
|
111 ((UInt32)data[i + 0] << 24) |
|
rlm@1
|
112 ((UInt32)data[i + 1] << 16) |
|
rlm@1
|
113 ((UInt32)data[i + 2] << 8) |
|
rlm@1
|
114 ((UInt32)data[i + 3]);
|
rlm@1
|
115 UInt32 dest;
|
rlm@1
|
116
|
rlm@1
|
117 src <<= 2;
|
rlm@1
|
118 if (encoding)
|
rlm@1
|
119 dest = ip + i + src;
|
rlm@1
|
120 else
|
rlm@1
|
121 dest = src - (ip + i);
|
rlm@1
|
122 dest >>= 2;
|
rlm@1
|
123
|
rlm@1
|
124 dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000;
|
rlm@1
|
125
|
rlm@1
|
126 data[i + 0] = (Byte)(dest >> 24);
|
rlm@1
|
127 data[i + 1] = (Byte)(dest >> 16);
|
rlm@1
|
128 data[i + 2] = (Byte)(dest >> 8);
|
rlm@1
|
129 data[i + 3] = (Byte)dest;
|
rlm@1
|
130 }
|
rlm@1
|
131 }
|
rlm@1
|
132 return i;
|
rlm@1
|
133 }
|