-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelocation.c
executable file
·471 lines (429 loc) · 13.3 KB
/
relocation.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define int32_t int
#define uint32_t unsigned int
#include <elf_types.h>
#define R_RISCV_NONE 0
#define R_RISCV_32 1
#define R_RISCV_64 2
#define R_RISCV_RELATIVE 3
#define R_RISCV_COPY 4
#define R_RISCV_JUMP_SLOT 5
#define R_RISCV_TLS_DTPMOD32 6
#define R_RISCV_TLS_DTPMOD64 7
#define R_RISCV_TLS_DTPREL32 8
#define R_RISCV_TLS_DTPREL64 9
#define R_RISCV_TLS_TPREL32 10
#define R_RISCV_TLS_TPREL64 11
#define R_RISCV_TLSDESC 12
#define R_RISCV_BRANCH 16
#define R_RISCV_JAL 17
#define R_RISCV_CALL 18
#define R_RISCV_CALL_PLT 19
#define R_RISCV_GOT_HI20 20
#define R_RISCV_TLS_GOT_HI20 21
#define R_RISCV_TLS_GD_HI20 22
#define R_RISCV_PCREL_HI20 23
#define R_RISCV_PCREL_LO12_I 24
#define R_RISCV_PCREL_LO12_S 25
#define R_RISCV_HI20 26
#define R_RISCV_LO12_I 27
#define R_RISCV_LO12_S 28
#define R_RISCV_TPREL_HI20 29
#define R_RISCV_TPREL_LO12_I 30
#define R_RISCV_TPREL_LO12_S 31
#define R_RISCV_TPREL_ADD 32
#define R_RISCV_ADD8 33
#define R_RISCV_ADD16 34
#define R_RISCV_ADD32 35
#define R_RISCV_ADD64 36
#define R_RISCV_SUB8 37
#define R_RISCV_SUB16 38
#define R_RISCV_SUB32 39
#define R_RISCV_SUB64 40
#define R_RISCV_GOT32_PCREL 41
#define R_RISCV_ALIGN 43
#define R_RISCV_RVC_BRANCH 44
#define R_RISCV_RVC_JUMP 45
#define R_RISCV_RVC_LUI 46
#define R_RISCV_RELAX 51
//#define R_RISCV_SUB6 52
//#define R_RISCV_SET6 53
//#define R_RISCV_SET8 54
//#define R_RISCV_SET16 55
//#define R_RISCV_SET32 56
//#define R_RISCV_32_PCREL 57
//#define R_RISCV_IRELATIVE 58
//#define R_RISCV_PLT32 59
//#define R_RISCV_SET_ULEB128 60
//#define R_RISCV_SUB_ULEB128 61
//#define R_RISCV_TLSDESC_HI20 62
//#define R_RISCV_TLSDESC_LOAD_LO12 63
//#define R_RISCV_TLSDESC_ADD_LO12 64
//#define R_RISCV_TLSDESC_CALL 65
char const* const riscv_type_name[128] =
{
[R_RISCV_NONE ] = "NONE",
[R_RISCV_32 ] = "32",
[R_RISCV_64 ] = "64",
[R_RISCV_RELATIVE ] = "RELATIVE",
[R_RISCV_COPY ] = "COPY",
[R_RISCV_JUMP_SLOT ] = "JUMP_SLOT",
[R_RISCV_TLS_DTPMOD32 ] = "TLS_DTPMOD32",
[R_RISCV_TLS_DTPMOD64 ] = "TLS_DTPMOD64",
[R_RISCV_TLS_DTPREL32 ] = "TLS_DTPREL32",
[R_RISCV_TLS_DTPREL64 ] = "TLS_DTPREL64",
[R_RISCV_TLS_TPREL32 ] = "TLS_TPREL32",
[R_RISCV_TLS_TPREL64 ] = "TLS_TPREL64",
[R_RISCV_TLSDESC ] = "TLSDESC",
[R_RISCV_BRANCH ] = "BRANCH",
[R_RISCV_JAL ] = "JAL",
[R_RISCV_CALL ] = "CALL",
[R_RISCV_CALL_PLT ] = "CALL_PLT",
[R_RISCV_GOT_HI20 ] = "GOT_HI20",
[R_RISCV_TLS_GOT_HI20 ] = "TLS_GOT_HI20",
[R_RISCV_TLS_GD_HI20 ] = "TLS_GD_HI20",
[R_RISCV_PCREL_HI20 ] = "PCREL_HI20",
[R_RISCV_PCREL_LO12_I ] = "PCREL_LO12_I",
[R_RISCV_PCREL_LO12_S ] = "PCREL_LO12_S",
[R_RISCV_HI20 ] = "HI20",
[R_RISCV_LO12_I ] = "LO12_I",
[R_RISCV_LO12_S ] = "LO12_S",
[R_RISCV_TPREL_HI20 ] = "TPREL_HI20",
[R_RISCV_TPREL_LO12_I ] = "TPREL_LO12_I",
[R_RISCV_TPREL_LO12_S ] = "TPREL_LO12_S",
[R_RISCV_TPREL_ADD ] = "TPREL_ADD",
[R_RISCV_ADD8 ] = "ADD8",
[R_RISCV_ADD16 ] = "ADD16",
[R_RISCV_ADD32 ] = "ADD32",
[R_RISCV_ADD64 ] = "ADD64",
[R_RISCV_SUB8 ] = "SUB8",
[R_RISCV_SUB16 ] = "SUB16",
[R_RISCV_SUB32 ] = "SUB32",
[R_RISCV_SUB64 ] = "SUB64",
[R_RISCV_GOT32_PCREL ] = "GOT32_PCREL",
[R_RISCV_ALIGN ] = "ALIGN",
[R_RISCV_RVC_BRANCH ] = "RVC_BRANCH",
[R_RISCV_RVC_JUMP ] = "RVC_JUMP",
[R_RISCV_RVC_LUI ] = "RVC_LUI",
[R_RISCV_RELAX ] = "RELAX",
};
static uint8_t read8(const void *P)
{
return *(uint8_t*)P;
}
static uint16_t read16le(const void *P)
{
return *(uint16_t*)P;
}
static uint32_t read32le(const void *P)
{
return *(uint32_t*)P;
}
static void write8(void *P, uint8_t V)
{
*(uint8_t*)P = V;
}
static void write16le(void *P, uint16_t V)
{
*(uint16_t*)P = V;
}
static void write32le(void *P, uint32_t V)
{
*(uint32_t*)P = V;
}
static uint32_t extractBits(uintptr_t v, uint32_t begin, uint32_t end)
{
return (uint32_t)((v & (((uintptr_t)1 << (begin + 1)) - 1)) >> end);
}
static uint32_t setLO12_I(uint32_t insn, uint32_t imm)
{
return (insn & 0xfffff) | (imm << 20);
}
static uint32_t setLO12_S(uint32_t insn, uint32_t imm)
{
return (insn & 0x1fff07f) | (extractBits(imm, 11, 5) << 25) | (extractBits(imm, 4, 0) << 7);
}
static bool relocate(char* text, struct elf32_rela* rela, uintptr_t addr)
{
uintptr_t val = addr + rela->addend;
unsigned int reloctype = ELF_R_TYPE(rela->info);
char* where = text + rela->offset;
switch (reloctype)
{
case R_RISCV_CALL:
case R_RISCV_CALL_PLT:
case R_RISCV_BRANCH:
case R_RISCV_JAL:
case R_RISCV_RVC_JUMP:
case R_RISCV_RVC_BRANCH:
val -= rela->offset;
}
switch (reloctype)
{
case R_RISCV_CALL:
case R_RISCV_CALL_PLT:
relocate(text, &(elf32_rela_t){rela->offset, R_RISCV_HI20}, val);
relocate(text, &(elf32_rela_t){rela->offset + 4, R_RISCV_LO12_I}, val);
break;
case R_RISCV_BRANCH:
{
uint32_t insn = read32le(where) & 0x1FFF07F;
uint32_t imm12 = extractBits(val, 12, 12) << 31;
uint32_t imm10_5 = extractBits(val, 10, 5) << 25;
uint32_t imm4_1 = extractBits(val, 4, 1) << 8;
uint32_t imm11 = extractBits(val, 11, 11) << 7;
insn |= imm12 | imm10_5 | imm4_1 | imm11;
write32le(where, insn);
break;
}
case R_RISCV_JAL:
{
uint32_t insn = read32le(where) & 0xFFF;
uint32_t imm20 = extractBits(val, 20, 20) << 31;
uint32_t imm10_1 = extractBits(val, 10, 1) << 21;
uint32_t imm11 = extractBits(val, 11, 11) << 20;
uint32_t imm19_12 = extractBits(val, 19, 12) << 12;
insn |= imm20 | imm10_1 | imm11 | imm19_12;
write32le(where, insn);
break;
}
case R_RISCV_HI20:
{
uintptr_t hi = val + 0x800;
write32le(where, (read32le(where) & 0xFFF) | (hi & 0xFFFFF000));
break;
}
case R_RISCV_LO12_I:
{
uintptr_t hi = (val + 0x800) >> 12;
uintptr_t lo = val - (hi << 12);
write32le(where, setLO12_I(read32le(where), lo & 0xFFF));
break;
}
case R_RISCV_RVC_JUMP:
{
uint16_t insn = read16le(where) & 0xE003;
uint16_t imm11 = extractBits(val, 11, 11) << 12;
uint16_t imm4 = extractBits(val, 4, 4) << 11;
uint16_t imm9_8 = extractBits(val, 9, 8) << 9;
uint16_t imm10 = extractBits(val, 10, 10) << 8;
uint16_t imm6 = extractBits(val, 6, 6) << 7;
uint16_t imm7 = extractBits(val, 7, 7) << 6;
uint16_t imm3_1 = extractBits(val, 3, 1) << 3;
uint16_t imm5 = extractBits(val, 5, 5) << 2;
insn |= imm11 | imm4 | imm9_8 | imm10 | imm6 | imm7 | imm3_1 | imm5;
write16le(where, insn);
break;
}
case R_RISCV_RVC_BRANCH:
{
uint16_t insn = read16le(where) & 0xE383;
uint16_t imm8 = extractBits(val, 8, 8) << 12;
uint16_t imm4_3 = extractBits(val, 4, 3) << 10;
uint16_t imm7_6 = extractBits(val, 7, 6) << 5;
uint16_t imm2_1 = extractBits(val, 2, 1) << 3;
uint16_t imm5 = extractBits(val, 5, 5) << 2;
insn |= imm8 | imm4_3 | imm7_6 | imm2_1 | imm5;
write16le(where, insn);
break;
}
default:
return false;
break;
}
return true;
}
typedef struct elf32_rela_link
{
struct elf32_rela_link* next;
struct elf32_rela* begin;
struct elf32_rela* end;
} elf32_rela_link;
int main(int argc, char const* argv[])
{
if (argc <= 2)
{
printf("usage: relocation input output\n");
return 0;
}
FILE* file = fopen(argv[1], "rb");
if (file == NULL)
{
printf("%s is open failed\n", argv[1]);
return 0;
}
// Memory
fseek(file, 0, SEEK_END);
size_t length = ftell(file);
fseek(file, 0, SEEK_SET);
char* memory = malloc(length);
fread(memory, 1, length, file);
fclose(file);
// ELF Header
struct elf32_hdr* elf = ((struct elf32_hdr*)memory);
//printf("%-16s: %s\n", "ident", elf->ident);
//printf("%-16s: %d\n", "type", elf->type);
//printf("%-16s: %d\n", "machine", elf->machine);
//printf("%-16s: %d\n", "version", elf->version);
//printf("%-16s: %d\n", "entry", elf->entry);
//printf("%-16s: %d\n", "phoff", elf->phoff);
//printf("%-16s: %d\n", "shoff", elf->shoff);
//printf("%-16s: %d\n", "flags", elf->flags);
//printf("%-16s: %d\n", "ehsize", elf->ehsize);
//printf("%-16s: %d\n", "phentsize", elf->phentsize);
//printf("%-16s: %d\n", "phnum", elf->phnum);
//printf("%-16s: %d\n", "shentsize", elf->shentsize);
//printf("%-16s: %d\n", "shnum", elf->shnum);
//printf("%-16s: %d\n", "shstrndx", elf->shstrndx);
// Section
struct elf32_rela_link* rela_link = NULL;
struct elf32_sym* symtab = NULL;
struct elf32_sym* symtab_end = NULL;
char* strtab = NULL;
// Section Header
struct elf32_shdr* section = (struct elf32_shdr*)(memory + elf->shoff);
char* strndx = memory + section[elf->shstrndx].offset;
Elf32_Half stl = 0;
for (Elf32_Half i = 0; i < elf->shnum; ++i)
{
if (strstr(strndx + section[i].name, ".rela") == strndx + section[i].name)
{
struct elf32_rela* begin = (struct elf32_rela*)(memory + section[i].offset);
struct elf32_rela* end = (struct elf32_rela*)(memory + section[i].offset + section[i].size);
struct elf32_rela_link* link = malloc(sizeof(struct elf32_rela_link));
link->next = rela_link;
link->begin = begin;
link->end = end;
rela_link = link;
}
if (strstr(strndx + section[i].name, ".symtab") == strndx + section[i].name)
{
symtab = (struct elf32_sym*)(memory + section[i].offset);
symtab_end = (struct elf32_sym*)(memory + section[i].offset + section[i].size);
}
if (strstr(strndx + section[i].name, ".strtab") == strndx + section[i].name)
{
strtab = (memory + section[i].offset);
}
if (strstr(strndx + section[i].name, ".stl") == strndx + section[i].name)
{
stl = i;
printf("Found : %s (%d)\n", ".stl", stl);
}
}
// RELA
for (struct elf32_rela_link* link = rela_link; link; link = link->next)
{
struct elf32_rela* begin = link->begin;
struct elf32_rela* end = link->end;
Elf32_Half ndx = 0;
char* text = NULL;
for (Elf32_Half i = 0; i < elf->shnum; ++i)
{
if ((struct elf32_rela*)(memory + section[i].offset) == begin)
{
char* name = strndx + section[i].name + sizeof(".rela") - 1;
for (Elf32_Half i = 0; i < elf->shnum; ++i)
{
if (strcmp(strndx + section[i].name, name) == 0)
{
ndx = i;
text = memory + section[i].offset;
printf("name: %s (%d)\n", name, ndx);
}
}
}
}
if (ndx == 0)
{
continue;
}
struct elf32_rela* p = begin;
for (struct elf32_rela* rela = begin; rela != end; ++rela)
{
unsigned int addr = rela->offset;
unsigned int reloctype = ELF_R_TYPE(rela->info);
unsigned int relocsym = ELF_R_SYM(rela->info);
struct elf32_sym* sym = &symtab[relocsym];
if (reloctype == R_RISCV_ALIGN || reloctype == R_RISCV_RELAX)
{
continue;
}
#if 0
if (reloctype == R_RISCV_LO12_I && p != begin)
{
struct elf32_rela* prev = (p - 1);
struct elf32_rela* curr = rela;
if (prev->offset + 4 == curr->offset && ELF_R_TYPE(prev->info) == R_RISCV_HI20 && ELF_R_SYM(prev->info) == relocsym)
{
(*rela) = (*prev);
rela->info &= ~0xFF;
rela->info |= R_RISCV_CALL_PLT;
p--;
}
}
#endif
if (sym->shndx == 0)
{
(*p++) = (*rela);
continue;
}
if (sym->shndx != ndx)
{
(*p++) = (*rela);
continue;
}
#if 1
if (reloctype == R_RISCV_HI20 || reloctype == R_RISCV_LO12_I)
{
(*p++) = (*rela);
printf("%08x %08x %-16s %s\n", sym->value, sym->size, riscv_type_name[reloctype], strtab + sym->name);
continue;
}
#endif
if (relocate(text, rela, sym->value) == false)
{
(*p++) = (*rela);
printf("%08x %08x %-16s %s\n", sym->value, sym->size, riscv_type_name[reloctype], strtab + sym->name);
continue;
}
}
for (Elf32_Half i = 0; i < elf->shnum; ++i)
{
if (strstr(strndx + section[i].name, ".rela") == strndx + section[i].name)
{
if ((struct elf32_rela*)(memory + section[i].offset) == begin)
{
section[i].size = (p - begin) * sizeof(struct elf32_rela);
}
}
}
}
// Symbol
for (struct elf32_sym* r = symtab; r != symtab_end; ++r)
{
if (r->shndx == stl)
{
r->shndx = 0;
}
if (r->shndx == 0)
{
continue;
}
r->name = 0;
r->info = 0;
r->other = 0;
}
file = fopen(argv[2], "wb");
if (file == NULL)
{
printf("%s is open failed\n", argv[2]);
return 0;
}
fwrite(memory, 1, length, file);
fclose(file);
return 0;
}