-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrelocation.c
More file actions
77 lines (69 loc) · 2.06 KB
/
relocation.c
File metadata and controls
77 lines (69 loc) · 2.06 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
#include "relocation.h"
#include <stdint.h>
size_t getRelocSize(enum reloc_size type) {
switch (type) {
case INT8:
return 1;
case INT16:
return 2;
case INT32:
return 4;
case INT64:
return 8;
}
}
void label_setOffset(label_t *label, unsigned long offset) {
label->offset = offset;
label->hasOffset = 1;
}
void relocation_set(label_t *label, enum reloc_type type, enum reloc_size size,
int bias, size_t offset) {
relocation_t *reloc = dmalloc(sizeof(relocation_t));
reloc->offset = offset;
reloc->bias = bias;
reloc->size = size;
reloc->type = type;
reloc->next = label->relocations;
label->relocations = reloc;
}
void relocation_emit(dbuffer_t *dbuffer, label_t *label, enum reloc_type type,
enum reloc_size size, int bias) {
relocation_set(label, type, size, bias, dbuffer->usage);
dbuffer_pushChars(dbuffer, 0, getRelocSize(size));
}
void relocation_apply(relocation_t *relocation, void *buffer,
unsigned long offset, unsigned long memLocation) {
uint64_t value;
if (relocation->type == ABSOLUTE)
value = offset;
else
value = offset - (memLocation + relocation->offset + relocation->bias);
switch (relocation->size) {
case INT8:
value = (uint8_t)value;
break;
case INT16:
value = (uint16_t)value;
break;
case INT32:
value = (uint32_t)value;
break;
case INT64:
break;
}
writeLong(buffer + relocation->offset, value,
getRelocSize(relocation->size));
}
void label_applyMemory(label_t *label, void *buffer,
unsigned long memLocation) {
for (relocation_t *current = label->relocations, *next; current != NULL;
current = next) {
next = current->next;
relocation_apply(current, buffer, label->offset, memLocation);
free(current);
}
label->relocations = NULL;
}
void label_apply(label_t *label, void *buffer) {
label_applyMemory(label, buffer, 0);
}