-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreloc.c
More file actions
227 lines (215 loc) · 4.66 KB
/
Copy pathreloc.c
File metadata and controls
227 lines (215 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "reloc.h"
static long long
extractu8(unsigned char const *buf)
{
return *buf;
}
static _Bool
writeu8(unsigned char *buf, long long v)
{
if (v < -0x80LL || v > 0xFFLL) return 0;
buf[0] = v&0xFF;
return 1;
}
static long long
extractu16(unsigned char const *buf)
{
return 0LL + buf[0] + (buf[1]<<8LL);
}
static _Bool
writeu16(unsigned char *buf, long long v)
{
if (v < -0x8000LL || v > 0xFFFFLL) return 0;
buf[0] = v&0xFF;
buf[1] = (v>>8)&0xFF;
return 1;
}
static long long
extractu32(unsigned char const *buf)
{
return 0LL + buf[0] + (buf[1]<<8LL) + (buf[2]<<16LL) + (buf[3]<<24LL);
}
static _Bool
writeu32(unsigned char *buf, long long v)
{
if (v < -0x80000000LL || v > 0xFFFFFFFFLL) return 0;
buf[0] = v&0xFF;
buf[1] = (v>>8)&0xFF;
buf[2] = (v>>16)&0xFF;
buf[3] = (v>>24)&0xFF;
return 1;
}
static long long
unalu4(long long v)
{
long long x = v&0x0700;
long long b = v&0x000F;
return b<<(x>>6);
}
static _Bool
writealu4(unsigned char *buf, long long v)
{
int i = 0;
for (i = 0; i < 32; i += 4) {
if ((v&~(0xF<<i)) == 0) break;
}
if (i >= 32) return 0;
buf[0] = (buf[0]&~0xF)|(v>>i);
buf[1] = (buf[1]&~0x7)|(i>>2);
return 1;
}
static long long
unsset8(long long v)
{
long long sx = v&0x0F00;
long long b = v&0x000F;
return (sx>>4)|b;
}
static _Bool
writesset8(unsigned char *buf, long long v)
{
if (v < 0LL || v > 0xFFLL) return 0;
buf[0] = (buf[0]&~0xF)|(v&0xF);
buf[1] = (buf[1]&~0xF)|((v>>4)&0xF);
return 1;
}
static _Bool
writesset8nc0(unsigned char *buf, long long v)
{
buf[0] = (buf[0]&~0xF)|(v&0xF);
buf[1] = (buf[1]&~0xF)|((v>>4)&0xF);
return 1;
}
static _Bool
writesset8nc1(unsigned char *buf, long long v)
{
v >>= 8;
buf[0] = (buf[0]&~0xF)|(v&0xF);
buf[1] = (buf[1]&~0xF)|((v>>4)&0xF);
return 1;
}
static _Bool
writesset8nc2(unsigned char *buf, long long v)
{
v >>= 16;
buf[0] = (buf[0]&~0xF)|(v&0xF);
buf[1] = (buf[1]&~0xF)|((v>>4)&0xF);
return 1;
}
static _Bool
writesset8nc3(unsigned char *buf, long long v)
{
v >>= 24;
buf[0] = (buf[0]&~0xF)|(v&0xF);
buf[1] = (buf[1]&~0xF)|((v>>4)&0xF);
return 1;
}
static long long
unabs5(long long v)
{
long long s = v&0x0800;
long long b = v&0x000F;
return (s>>7)|b;
}
static _Bool
writeabs5(unsigned char *buf, long long v)
{
if (v < 0LL | v > 0x1FLL) return 0;
buf[0] = (buf[0]&~0xF)|(v&0xF);
buf[1] = (buf[1]&~0x8)|((v&0x10)>>1);
return 1;
}
static long long
unboff3(long long v)
{
long long x = v&0x0700;
return x>>8;
}
static _Bool
writeboff3(unsigned char *buf, long long v)
{
if (v < 0LL || v > 0x7LL) return 0;
buf[1] = (buf[1]&~0x7)|v;
return 1;
}
static long long
unwoff3(long long v)
{
long long x = v&0x0700;
return x>>6;
}
static _Bool
writewoff3(unsigned char *buf, long long v)
{
if (v&0x3LL) return 0;
v >>= 2;
if (v < 0LL || v > 0x7LL) return 0;
buf[1] = (buf[1]&~0x7)|v;
return 1;
}
static long long
unpc8(long long v)
{
long long ab = v&0x00FF;
if (ab > 127) ab -= 256;
return ab<<1;
}
static _Bool
writepc8(unsigned char *buf, long long v)
{
if (v&1) return 0;
v /= 2;
if (v < -128LL || v >= 128LL) return 0;
buf[0] = v;
return 1;
}
_Bool
rel_apply(enum reloc r, unsigned char *buf,
long long place, long long value, long long xadd)
{
long long add = rel_extract(r, buf) + xadd;
long long v = (r == R_URSA_PC8)? value + add - place : value + add;
switch (r) {
case R_URSA_NONE: return 0;
case R_URSA_ABS8: return writeu8(buf, v);
case R_URSA_ABS16: return writeu16(buf, v);
case R_URSA_ABS32: return writeu32(buf, v);
case R_URSA_ALU4: return writealu4(buf, v);
case R_URSA_SSET8: return writesset8(buf, v);
case R_URSA_SSET8_0_NC: return writesset8nc0(buf, v);
case R_URSA_SSET8_1_NC: return writesset8nc1(buf, v);
case R_URSA_SSET8_2_NC: return writesset8nc2(buf, v);
case R_URSA_SSET8_3_NC: return writesset8nc3(buf, v);
case R_URSA_ABS5: return writeabs5(buf, v);
case R_URSA_BOFF3: return writeboff3(buf, v);
case R_URSA_WOFF3: return writewoff3(buf, v);
case R_URSA_PC8: return writepc8(buf, v);
case R_URSA_ILLEGAL: return 0;
default: return 0;
}
return 0;
}
long long
rel_extract(enum reloc r, unsigned char const *buf)
{
if (!buf) return 0;
switch (r) {
case R_URSA_NONE: return 0;
case R_URSA_ABS8: return extractu8(buf);
case R_URSA_ABS16: return extractu16(buf);
case R_URSA_ABS32: return extractu32(buf);
case R_URSA_ALU4: return unalu4(extractu16(buf));
case R_URSA_SSET8:
case R_URSA_SSET8_0_NC:
case R_URSA_SSET8_1_NC:
case R_URSA_SSET8_2_NC:
case R_URSA_SSET8_3_NC:
return unsset8(extractu16(buf));
case R_URSA_ABS5: return unabs5(extractu16(buf));
case R_URSA_BOFF3: return unboff3(extractu16(buf));
case R_URSA_WOFF3: return unwoff3(extractu16(buf));
case R_URSA_PC8: return unpc8(extractu16(buf));
default: return 0;
}
return 0;
}